blob: 5a96efd9baa478222bb0258ba4ff452bf8731a80 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307
1308 static final int[] VIEW_STATE_IDS = new int[] {
1309 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1310 R.attr.state_selected, VIEW_STATE_SELECTED,
1311 R.attr.state_focused, VIEW_STATE_FOCUSED,
1312 R.attr.state_enabled, VIEW_STATE_ENABLED,
1313 R.attr.state_pressed, VIEW_STATE_PRESSED,
1314 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001315 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 };
1317
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001319 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1320 throw new IllegalStateException(
1321 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1322 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001326 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001328 orderedIds[i * 2] = viewState;
1329 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 }
1331 }
1332 }
Romain Guyb051e892010-09-28 19:09:36 -07001333 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1334 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1335 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 int numBits = Integer.bitCount(i);
1337 int[] set = new int[numBits];
1338 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001339 for (int j = 0; j < orderedIds.length; j += 2) {
1340 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 set[pos++] = orderedIds[j];
1342 }
1343 }
1344 VIEW_STATE_SETS[i] = set;
1345 }
1346
1347 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1348 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1349 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1350 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1351 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1352 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1353 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1357 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1358 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1359 | VIEW_STATE_FOCUSED];
1360 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1361 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1365 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1367 | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1370 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1372 | VIEW_STATE_ENABLED];
1373 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1375 | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1378 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1379
1380 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1381 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1385 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1387 | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1390 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1392 | VIEW_STATE_PRESSED];
1393 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1395 | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1398 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1401 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1403 | VIEW_STATE_PRESSED];
1404 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1406 | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1418 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 }
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * Temporary Rect currently for use in setBackground(). This will probably
1427 * be extended in the future to hold our own class with more than just
1428 * a Rect. :)
1429 */
1430 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001431
1432 /**
1433 * Map used to store views' tags.
1434 */
1435 private static WeakHashMap<View, SparseArray<Object>> sTags;
1436
1437 /**
1438 * Lock used to access sTags.
1439 */
1440 private static final Object sTagsLock = new Object();
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
1443 * The animation currently associated with this view.
1444 * @hide
1445 */
1446 protected Animation mCurrentAnimation = null;
1447
1448 /**
1449 * Width as measured during measure pass.
1450 * {@hide}
1451 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001452 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001453 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 /**
1456 * Height as measured during measure pass.
1457 * {@hide}
1458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001459 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001460 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
1462 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001463 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1464 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1465 * its display list. This flag, used only when hw accelerated, allows us to clear the
1466 * flag while retaining this information until it's needed (at getDisplayList() time and
1467 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1468 *
1469 * {@hide}
1470 */
1471 boolean mRecreateDisplayList = false;
1472
1473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 * The view's identifier.
1475 * {@hide}
1476 *
1477 * @see #setId(int)
1478 * @see #getId()
1479 */
1480 @ViewDebug.ExportedProperty(resolveId = true)
1481 int mID = NO_ID;
1482
1483 /**
1484 * The view's tag.
1485 * {@hide}
1486 *
1487 * @see #setTag(Object)
1488 * @see #getTag()
1489 */
1490 protected Object mTag;
1491
1492 // for mPrivateFlags:
1493 /** {@hide} */
1494 static final int WANTS_FOCUS = 0x00000001;
1495 /** {@hide} */
1496 static final int FOCUSED = 0x00000002;
1497 /** {@hide} */
1498 static final int SELECTED = 0x00000004;
1499 /** {@hide} */
1500 static final int IS_ROOT_NAMESPACE = 0x00000008;
1501 /** {@hide} */
1502 static final int HAS_BOUNDS = 0x00000010;
1503 /** {@hide} */
1504 static final int DRAWN = 0x00000020;
1505 /**
1506 * When this flag is set, this view is running an animation on behalf of its
1507 * children and should therefore not cancel invalidate requests, even if they
1508 * lie outside of this view's bounds.
1509 *
1510 * {@hide}
1511 */
1512 static final int DRAW_ANIMATION = 0x00000040;
1513 /** {@hide} */
1514 static final int SKIP_DRAW = 0x00000080;
1515 /** {@hide} */
1516 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1517 /** {@hide} */
1518 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1519 /** {@hide} */
1520 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1521 /** {@hide} */
1522 static final int MEASURED_DIMENSION_SET = 0x00000800;
1523 /** {@hide} */
1524 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001525 /** {@hide} */
1526 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 private static final int PRESSED = 0x00004000;
1529
1530 /** {@hide} */
1531 static final int DRAWING_CACHE_VALID = 0x00008000;
1532 /**
1533 * Flag used to indicate that this view should be drawn once more (and only once
1534 * more) after its animation has completed.
1535 * {@hide}
1536 */
1537 static final int ANIMATION_STARTED = 0x00010000;
1538
1539 private static final int SAVE_STATE_CALLED = 0x00020000;
1540
1541 /**
1542 * Indicates that the View returned true when onSetAlpha() was called and that
1543 * the alpha must be restored.
1544 * {@hide}
1545 */
1546 static final int ALPHA_SET = 0x00040000;
1547
1548 /**
1549 * Set by {@link #setScrollContainer(boolean)}.
1550 */
1551 static final int SCROLL_CONTAINER = 0x00080000;
1552
1553 /**
1554 * Set by {@link #setScrollContainer(boolean)}.
1555 */
1556 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1557
1558 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001559 * View flag indicating whether this view was invalidated (fully or partially.)
1560 *
1561 * @hide
1562 */
1563 static final int DIRTY = 0x00200000;
1564
1565 /**
1566 * View flag indicating whether this view was invalidated by an opaque
1567 * invalidate request.
1568 *
1569 * @hide
1570 */
1571 static final int DIRTY_OPAQUE = 0x00400000;
1572
1573 /**
1574 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1575 *
1576 * @hide
1577 */
1578 static final int DIRTY_MASK = 0x00600000;
1579
1580 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001581 * Indicates whether the background is opaque.
1582 *
1583 * @hide
1584 */
1585 static final int OPAQUE_BACKGROUND = 0x00800000;
1586
1587 /**
1588 * Indicates whether the scrollbars are opaque.
1589 *
1590 * @hide
1591 */
1592 static final int OPAQUE_SCROLLBARS = 0x01000000;
1593
1594 /**
1595 * Indicates whether the view is opaque.
1596 *
1597 * @hide
1598 */
1599 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001600
Adam Powelle14579b2009-12-16 18:39:52 -08001601 /**
1602 * Indicates a prepressed state;
1603 * the short time between ACTION_DOWN and recognizing
1604 * a 'real' press. Prepressed is used to recognize quick taps
1605 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001606 *
Adam Powelle14579b2009-12-16 18:39:52 -08001607 * @hide
1608 */
1609 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001610
Adam Powellc9fbaab2010-02-16 17:16:19 -08001611 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001612 * Indicates whether the view is temporarily detached.
1613 *
1614 * @hide
1615 */
1616 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001617
Adam Powell8568c3a2010-04-19 14:26:11 -07001618 /**
1619 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001620 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001621 * @hide
1622 */
1623 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001624
1625 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001626 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1627 * for transform operations
1628 *
1629 * @hide
1630 */
Adam Powellf37df072010-09-17 16:22:49 -07001631 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001632
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001633 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001634 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001635
Chet Haasefd2b0022010-08-06 13:08:56 -07001636 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001637 * Indicates that this view was specifically invalidated, not just dirtied because some
1638 * child view was invalidated. The flag is used to determine when we need to recreate
1639 * a view's display list (as opposed to just returning a reference to its existing
1640 * display list).
1641 *
1642 * @hide
1643 */
1644 static final int INVALIDATED = 0x80000000;
1645
1646 /**
Adam Powell637d3372010-08-25 14:37:03 -07001647 * Always allow a user to over-scroll this view, provided it is a
1648 * view that can scroll.
1649 *
1650 * @see #getOverScrollMode()
1651 * @see #setOverScrollMode(int)
1652 */
1653 public static final int OVER_SCROLL_ALWAYS = 0;
1654
1655 /**
1656 * Allow a user to over-scroll this view only if the content is large
1657 * enough to meaningfully scroll, provided it is a view that can scroll.
1658 *
1659 * @see #getOverScrollMode()
1660 * @see #setOverScrollMode(int)
1661 */
1662 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1663
1664 /**
1665 * Never allow a user to over-scroll this view.
1666 *
1667 * @see #getOverScrollMode()
1668 * @see #setOverScrollMode(int)
1669 */
1670 public static final int OVER_SCROLL_NEVER = 2;
1671
1672 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001673 * View has requested the status bar to be visible (the default).
1674 *
Joe Malin32736f02011-01-19 16:14:20 -08001675 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001676 */
1677 public static final int STATUS_BAR_VISIBLE = 0;
1678
1679 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001680 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001681 *
Joe Malin32736f02011-01-19 16:14:20 -08001682 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001683 */
1684 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1685
1686 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001687 * @hide
1688 *
1689 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1690 * out of the public fields to keep the undefined bits out of the developer's way.
1691 *
1692 * Flag to make the status bar not expandable. Unless you also
1693 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1694 */
1695 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1696
1697 /**
1698 * @hide
1699 *
1700 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1701 * out of the public fields to keep the undefined bits out of the developer's way.
1702 *
1703 * Flag to hide notification icons and scrolling ticker text.
1704 */
1705 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1706
1707 /**
1708 * @hide
1709 *
1710 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1711 * out of the public fields to keep the undefined bits out of the developer's way.
1712 *
1713 * Flag to disable incoming notification alerts. This will not block
1714 * icons, but it will block sound, vibrating and other visual or aural notifications.
1715 */
1716 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1717
1718 /**
1719 * @hide
1720 *
1721 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1722 * out of the public fields to keep the undefined bits out of the developer's way.
1723 *
1724 * Flag to hide only the scrolling ticker. Note that
1725 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1726 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1727 */
1728 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1729
1730 /**
1731 * @hide
1732 *
1733 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1734 * out of the public fields to keep the undefined bits out of the developer's way.
1735 *
1736 * Flag to hide the center system info area.
1737 */
1738 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1739
1740 /**
1741 * @hide
1742 *
1743 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1744 * out of the public fields to keep the undefined bits out of the developer's way.
1745 *
1746 * Flag to hide only the navigation buttons. Don't use this
1747 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001748 *
1749 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001750 */
1751 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1752
1753 /**
1754 * @hide
1755 *
1756 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1757 * out of the public fields to keep the undefined bits out of the developer's way.
1758 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001759 * Flag to hide only the back button. Don't use this
1760 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1761 */
1762 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1763
1764 /**
1765 * @hide
1766 *
1767 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1768 * out of the public fields to keep the undefined bits out of the developer's way.
1769 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001770 * Flag to hide only the clock. You might use this if your activity has
1771 * its own clock making the status bar's clock redundant.
1772 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001773 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1774
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001775 /**
1776 * @hide
1777 */
1778 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001779
1780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Controls the over-scroll mode for this view.
1782 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1783 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1784 * and {@link #OVER_SCROLL_NEVER}.
1785 */
1786 private int mOverScrollMode;
1787
1788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * The parent this view is attached to.
1790 * {@hide}
1791 *
1792 * @see #getParent()
1793 */
1794 protected ViewParent mParent;
1795
1796 /**
1797 * {@hide}
1798 */
1799 AttachInfo mAttachInfo;
1800
1801 /**
1802 * {@hide}
1803 */
Romain Guy809a7f62009-05-14 15:44:42 -07001804 @ViewDebug.ExportedProperty(flagMapping = {
1805 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1806 name = "FORCE_LAYOUT"),
1807 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1808 name = "LAYOUT_REQUIRED"),
1809 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001810 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001811 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1813 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1815 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int mPrivateFlags;
1817
1818 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001819 * This view's request for the visibility of the status bar.
1820 * @hide
1821 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001822 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001823 int mSystemUiVisibility;
1824
1825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 * Count of how many windows this view has been attached to.
1827 */
1828 int mWindowAttachCount;
1829
1830 /**
1831 * The layout parameters associated with this view and used by the parent
1832 * {@link android.view.ViewGroup} to determine how this view should be
1833 * laid out.
1834 * {@hide}
1835 */
1836 protected ViewGroup.LayoutParams mLayoutParams;
1837
1838 /**
1839 * The view flags hold various views states.
1840 * {@hide}
1841 */
1842 @ViewDebug.ExportedProperty
1843 int mViewFlags;
1844
1845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001846 * The transform matrix for the View. This transform is calculated internally
1847 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1848 * is used by default. Do *not* use this variable directly; instead call
1849 * getMatrix(), which will automatically recalculate the matrix if necessary
1850 * to get the correct matrix based on the latest rotation and scale properties.
1851 */
1852 private final Matrix mMatrix = new Matrix();
1853
1854 /**
1855 * The transform matrix for the View. This transform is calculated internally
1856 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1857 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001858 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001859 * to get the correct matrix based on the latest rotation and scale properties.
1860 */
1861 private Matrix mInverseMatrix;
1862
1863 /**
1864 * An internal variable that tracks whether we need to recalculate the
1865 * transform matrix, based on whether the rotation or scaleX/Y properties
1866 * have changed since the matrix was last calculated.
1867 */
Chet Haasea00f3862011-02-22 06:34:40 -08001868 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001869
1870 /**
1871 * An internal variable that tracks whether we need to recalculate the
1872 * transform matrix, based on whether the rotation or scaleX/Y properties
1873 * have changed since the matrix was last calculated.
1874 */
1875 private boolean mInverseMatrixDirty = true;
1876
1877 /**
1878 * A variable that tracks whether we need to recalculate the
1879 * transform matrix, based on whether the rotation or scaleX/Y properties
1880 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001881 * is only valid after a call to updateMatrix() or to a function that
1882 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001883 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001884 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001885
1886 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001887 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1888 */
1889 private Camera mCamera = null;
1890
1891 /**
1892 * This matrix is used when computing the matrix for 3D rotations.
1893 */
1894 private Matrix matrix3D = null;
1895
1896 /**
1897 * These prev values are used to recalculate a centered pivot point when necessary. The
1898 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1899 * set), so thes values are only used then as well.
1900 */
1901 private int mPrevWidth = -1;
1902 private int mPrevHeight = -1;
1903
Joe Malin32736f02011-01-19 16:14:20 -08001904 private boolean mLastIsOpaque;
1905
Chet Haasefd2b0022010-08-06 13:08:56 -07001906 /**
1907 * Convenience value to check for float values that are close enough to zero to be considered
1908 * zero.
1909 */
Romain Guy2542d192010-08-18 11:47:12 -07001910 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001911
1912 /**
1913 * The degrees rotation around the vertical axis through the pivot point.
1914 */
1915 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001916 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001917
1918 /**
1919 * The degrees rotation around the horizontal axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001922 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001923
1924 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001925 * The degrees rotation around the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001928 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001929
1930 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001931 * The amount of translation of the object away from its left property (post-layout).
1932 */
1933 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001934 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001935
1936 /**
1937 * The amount of translation of the object away from its top property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001940 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001941
1942 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001943 * The amount of scale in the x direction around the pivot point. A
1944 * value of 1 means no scaling is applied.
1945 */
1946 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001947 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001948
1949 /**
1950 * The amount of scale in the y direction around the pivot point. A
1951 * value of 1 means no scaling is applied.
1952 */
1953 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001954 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001955
1956 /**
1957 * The amount of scale in the x direction around the pivot point. A
1958 * value of 1 means no scaling is applied.
1959 */
1960 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001961 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001962
1963 /**
1964 * The amount of scale in the y direction around the pivot point. A
1965 * value of 1 means no scaling is applied.
1966 */
1967 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001968 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001969
1970 /**
1971 * The opacity of the View. This is a value from 0 to 1, where 0 means
1972 * completely transparent and 1 means completely opaque.
1973 */
1974 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001975 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001976
1977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * The distance in pixels from the left edge of this view's parent
1979 * to the left edge of this view.
1980 * {@hide}
1981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 protected int mLeft;
1984 /**
1985 * The distance in pixels from the left edge of this view's parent
1986 * to the right edge of this view.
1987 * {@hide}
1988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 protected int mRight;
1991 /**
1992 * The distance in pixels from the top edge of this view's parent
1993 * to the top edge of this view.
1994 * {@hide}
1995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001996 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected int mTop;
1998 /**
1999 * The distance in pixels from the top edge of this view's parent
2000 * to the bottom edge of this view.
2001 * {@hide}
2002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002003 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 protected int mBottom;
2005
2006 /**
2007 * The offset, in pixels, by which the content of this view is scrolled
2008 * horizontally.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mScrollX;
2013 /**
2014 * The offset, in pixels, by which the content of this view is scrolled
2015 * vertically.
2016 * {@hide}
2017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002018 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 protected int mScrollY;
2020
2021 /**
2022 * The left padding in pixels, that is the distance in pixels between the
2023 * left edge of this view and the left edge of its content.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mPaddingLeft;
2028 /**
2029 * The right padding in pixels, that is the distance in pixels between the
2030 * right edge of this view and the right edge of its content.
2031 * {@hide}
2032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002033 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 protected int mPaddingRight;
2035 /**
2036 * The top padding in pixels, that is the distance in pixels between the
2037 * top edge of this view and the top edge of its content.
2038 * {@hide}
2039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002040 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 protected int mPaddingTop;
2042 /**
2043 * The bottom padding in pixels, that is the distance in pixels between the
2044 * bottom edge of this view and the bottom edge of its content.
2045 * {@hide}
2046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002047 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 protected int mPaddingBottom;
2049
2050 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002051 * Briefly describes the view and is primarily used for accessibility support.
2052 */
2053 private CharSequence mContentDescription;
2054
2055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * Cache the paddingRight set by the user to append to the scrollbar's size.
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int mUserPaddingRight;
2060
2061 /**
2062 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingBottom;
2066
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002067 /**
Adam Powell20232d02010-12-08 21:08:53 -08002068 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2069 */
2070 @ViewDebug.ExportedProperty(category = "padding")
2071 int mUserPaddingLeft;
2072
2073 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002074 * @hide
2075 */
2076 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2077 /**
2078 * @hide
2079 */
2080 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 private Resources mResources = null;
2083
2084 private Drawable mBGDrawable;
2085
2086 private int mBackgroundResource;
2087 private boolean mBackgroundSizeChanged;
2088
2089 /**
2090 * Listener used to dispatch focus change events.
2091 * This field should be made private, so it is hidden from the SDK.
2092 * {@hide}
2093 */
2094 protected OnFocusChangeListener mOnFocusChangeListener;
2095
2096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002097 * Listeners for layout change events.
2098 */
2099 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2100
2101 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002102 * Listeners for attach events.
2103 */
2104 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2105
2106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 * Listener used to dispatch click events.
2108 * This field should be made private, so it is hidden from the SDK.
2109 * {@hide}
2110 */
2111 protected OnClickListener mOnClickListener;
2112
2113 /**
2114 * Listener used to dispatch long click events.
2115 * This field should be made private, so it is hidden from the SDK.
2116 * {@hide}
2117 */
2118 protected OnLongClickListener mOnLongClickListener;
2119
2120 /**
2121 * Listener used to build the context menu.
2122 * This field should be made private, so it is hidden from the SDK.
2123 * {@hide}
2124 */
2125 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2126
2127 private OnKeyListener mOnKeyListener;
2128
2129 private OnTouchListener mOnTouchListener;
2130
Jeff Brown33bbfd22011-02-24 20:55:35 -08002131 private OnGenericMotionListener mOnGenericMotionListener;
2132
Chris Tate32affef2010-10-18 15:29:21 -07002133 private OnDragListener mOnDragListener;
2134
Joe Onorato664644d2011-01-23 17:53:23 -08002135 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 /**
2138 * The application environment this view lives in.
2139 * This field should be made private, so it is hidden from the SDK.
2140 * {@hide}
2141 */
2142 protected Context mContext;
2143
2144 private ScrollabilityCache mScrollCache;
2145
2146 private int[] mDrawableState = null;
2147
Romain Guy0211a0a2011-02-14 16:34:59 -08002148 /**
2149 * Set to true when drawing cache is enabled and cannot be created.
2150 *
2151 * @hide
2152 */
2153 public boolean mCachingFailed;
2154
Romain Guy02890fd2010-08-06 17:58:44 -07002155 private Bitmap mDrawingCache;
2156 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002157 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002158 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159
2160 /**
2161 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2162 * the user may specify which view to go to next.
2163 */
2164 private int mNextFocusLeftId = View.NO_ID;
2165
2166 /**
2167 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2168 * the user may specify which view to go to next.
2169 */
2170 private int mNextFocusRightId = View.NO_ID;
2171
2172 /**
2173 * When this view has focus and the next focus is {@link #FOCUS_UP},
2174 * the user may specify which view to go to next.
2175 */
2176 private int mNextFocusUpId = View.NO_ID;
2177
2178 /**
2179 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2180 * the user may specify which view to go to next.
2181 */
2182 private int mNextFocusDownId = View.NO_ID;
2183
Jeff Brown4e6319b2010-12-13 10:36:51 -08002184 /**
2185 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2186 * the user may specify which view to go to next.
2187 */
2188 int mNextFocusForwardId = View.NO_ID;
2189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002191 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002192 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 private UnsetPressedState mUnsetPressedState;
2195
2196 /**
2197 * Whether the long press's action has been invoked. The tap's action is invoked on the
2198 * up event while a long press is invoked as soon as the long press duration is reached, so
2199 * a long press could be performed before the tap is checked, in which case the tap's action
2200 * should not be invoked.
2201 */
2202 private boolean mHasPerformedLongPress;
2203
2204 /**
2205 * The minimum height of the view. We'll try our best to have the height
2206 * of this view to at least this amount.
2207 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002208 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 private int mMinHeight;
2210
2211 /**
2212 * The minimum width of the view. We'll try our best to have the width
2213 * of this view to at least this amount.
2214 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002215 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002216 private int mMinWidth;
2217
2218 /**
2219 * The delegate to handle touch events that are physically in this view
2220 * but should be handled by another view.
2221 */
2222 private TouchDelegate mTouchDelegate = null;
2223
2224 /**
2225 * Solid color to use as a background when creating the drawing cache. Enables
2226 * the cache to use 16 bit bitmaps instead of 32 bit.
2227 */
2228 private int mDrawingCacheBackgroundColor = 0;
2229
2230 /**
2231 * Special tree observer used when mAttachInfo is null.
2232 */
2233 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002234
Adam Powelle14579b2009-12-16 18:39:52 -08002235 /**
2236 * Cache the touch slop from the context that created the view.
2237 */
2238 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002240 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002241 * Object that handles automatic animation of view properties.
2242 */
2243 private ViewPropertyAnimator mAnimator = null;
2244
2245 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002246 * Cache drag/drop state
2247 *
2248 */
2249 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002250
2251 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002252 * Flag indicating that a drag can cross window boundaries. When
2253 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2254 * with this flag set, all visible applications will be able to participate
2255 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002256 *
2257 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002258 */
2259 public static final int DRAG_FLAG_GLOBAL = 1;
2260
2261 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002262 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2263 */
2264 private float mVerticalScrollFactor;
2265
2266 /**
Adam Powell20232d02010-12-08 21:08:53 -08002267 * Position of the vertical scroll bar.
2268 */
2269 private int mVerticalScrollbarPosition;
2270
2271 /**
2272 * Position the scroll bar at the default position as determined by the system.
2273 */
2274 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2275
2276 /**
2277 * Position the scroll bar along the left edge.
2278 */
2279 public static final int SCROLLBAR_POSITION_LEFT = 1;
2280
2281 /**
2282 * Position the scroll bar along the right edge.
2283 */
2284 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2285
2286 /**
Romain Guy171c5922011-01-06 10:04:23 -08002287 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002288 *
2289 * @see #getLayerType()
2290 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002291 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002292 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002293 */
2294 public static final int LAYER_TYPE_NONE = 0;
2295
2296 /**
2297 * <p>Indicates that the view has a software layer. A software layer is backed
2298 * by a bitmap and causes the view to be rendered using Android's software
2299 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002300 *
Romain Guy171c5922011-01-06 10:04:23 -08002301 * <p>Software layers have various usages:</p>
2302 * <p>When the application is not using hardware acceleration, a software layer
2303 * is useful to apply a specific color filter and/or blending mode and/or
2304 * translucency to a view and all its children.</p>
2305 * <p>When the application is using hardware acceleration, a software layer
2306 * is useful to render drawing primitives not supported by the hardware
2307 * accelerated pipeline. It can also be used to cache a complex view tree
2308 * into a texture and reduce the complexity of drawing operations. For instance,
2309 * when animating a complex view tree with a translation, a software layer can
2310 * be used to render the view tree only once.</p>
2311 * <p>Software layers should be avoided when the affected view tree updates
2312 * often. Every update will require to re-render the software layer, which can
2313 * potentially be slow (particularly when hardware acceleration is turned on
2314 * since the layer will have to be uploaded into a hardware texture after every
2315 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002316 *
2317 * @see #getLayerType()
2318 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002319 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002320 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002321 */
2322 public static final int LAYER_TYPE_SOFTWARE = 1;
2323
2324 /**
2325 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2326 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2327 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2328 * rendering pipeline, but only if hardware acceleration is turned on for the
2329 * view hierarchy. When hardware acceleration is turned off, hardware layers
2330 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002331 *
Romain Guy171c5922011-01-06 10:04:23 -08002332 * <p>A hardware layer is useful to apply a specific color filter and/or
2333 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002334 * <p>A hardware layer can be used to cache a complex view tree into a
2335 * texture and reduce the complexity of drawing operations. For instance,
2336 * when animating a complex view tree with a translation, a hardware layer can
2337 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002338 * <p>A hardware layer can also be used to increase the rendering quality when
2339 * rotation transformations are applied on a view. It can also be used to
2340 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002341 *
2342 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002343 * @see #setLayerType(int, android.graphics.Paint)
2344 * @see #LAYER_TYPE_NONE
2345 * @see #LAYER_TYPE_SOFTWARE
2346 */
2347 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002348
Romain Guy3aaff3a2011-01-12 14:18:47 -08002349 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2350 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2351 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2352 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2353 })
Romain Guy171c5922011-01-06 10:04:23 -08002354 int mLayerType = LAYER_TYPE_NONE;
2355 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002356 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002357
2358 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 * Simple constructor to use when creating a view from code.
2360 *
2361 * @param context The Context the view is running in, through which it can
2362 * access the current theme, resources, etc.
2363 */
2364 public View(Context context) {
2365 mContext = context;
2366 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002367 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002368 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002369 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370 }
2371
2372 /**
2373 * Constructor that is called when inflating a view from XML. This is called
2374 * when a view is being constructed from an XML file, supplying attributes
2375 * that were specified in the XML file. This version uses a default style of
2376 * 0, so the only attribute values applied are those in the Context's Theme
2377 * and the given AttributeSet.
2378 *
2379 * <p>
2380 * The method onFinishInflate() will be called after all children have been
2381 * added.
2382 *
2383 * @param context The Context the view is running in, through which it can
2384 * access the current theme, resources, etc.
2385 * @param attrs The attributes of the XML tag that is inflating the view.
2386 * @see #View(Context, AttributeSet, int)
2387 */
2388 public View(Context context, AttributeSet attrs) {
2389 this(context, attrs, 0);
2390 }
2391
2392 /**
2393 * Perform inflation from XML and apply a class-specific base style. This
2394 * constructor of View allows subclasses to use their own base style when
2395 * they are inflating. For example, a Button class's constructor would call
2396 * this version of the super class constructor and supply
2397 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2398 * the theme's button style to modify all of the base view attributes (in
2399 * particular its background) as well as the Button class's attributes.
2400 *
2401 * @param context The Context the view is running in, through which it can
2402 * access the current theme, resources, etc.
2403 * @param attrs The attributes of the XML tag that is inflating the view.
2404 * @param defStyle The default style to apply to this view. If 0, no style
2405 * will be applied (beyond what is included in the theme). This may
2406 * either be an attribute resource, whose value will be retrieved
2407 * from the current theme, or an explicit style resource.
2408 * @see #View(Context, AttributeSet)
2409 */
2410 public View(Context context, AttributeSet attrs, int defStyle) {
2411 this(context);
2412
2413 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2414 defStyle, 0);
2415
2416 Drawable background = null;
2417
2418 int leftPadding = -1;
2419 int topPadding = -1;
2420 int rightPadding = -1;
2421 int bottomPadding = -1;
2422
2423 int padding = -1;
2424
2425 int viewFlagValues = 0;
2426 int viewFlagMasks = 0;
2427
2428 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 int x = 0;
2431 int y = 0;
2432
Chet Haase73066682010-11-29 15:55:32 -08002433 float tx = 0;
2434 float ty = 0;
2435 float rotation = 0;
2436 float rotationX = 0;
2437 float rotationY = 0;
2438 float sx = 1f;
2439 float sy = 1f;
2440 boolean transformSet = false;
2441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002442 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2443
Adam Powell637d3372010-08-25 14:37:03 -07002444 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002445 final int N = a.getIndexCount();
2446 for (int i = 0; i < N; i++) {
2447 int attr = a.getIndex(i);
2448 switch (attr) {
2449 case com.android.internal.R.styleable.View_background:
2450 background = a.getDrawable(attr);
2451 break;
2452 case com.android.internal.R.styleable.View_padding:
2453 padding = a.getDimensionPixelSize(attr, -1);
2454 break;
2455 case com.android.internal.R.styleable.View_paddingLeft:
2456 leftPadding = a.getDimensionPixelSize(attr, -1);
2457 break;
2458 case com.android.internal.R.styleable.View_paddingTop:
2459 topPadding = a.getDimensionPixelSize(attr, -1);
2460 break;
2461 case com.android.internal.R.styleable.View_paddingRight:
2462 rightPadding = a.getDimensionPixelSize(attr, -1);
2463 break;
2464 case com.android.internal.R.styleable.View_paddingBottom:
2465 bottomPadding = a.getDimensionPixelSize(attr, -1);
2466 break;
2467 case com.android.internal.R.styleable.View_scrollX:
2468 x = a.getDimensionPixelOffset(attr, 0);
2469 break;
2470 case com.android.internal.R.styleable.View_scrollY:
2471 y = a.getDimensionPixelOffset(attr, 0);
2472 break;
Chet Haase73066682010-11-29 15:55:32 -08002473 case com.android.internal.R.styleable.View_alpha:
2474 setAlpha(a.getFloat(attr, 1f));
2475 break;
2476 case com.android.internal.R.styleable.View_transformPivotX:
2477 setPivotX(a.getDimensionPixelOffset(attr, 0));
2478 break;
2479 case com.android.internal.R.styleable.View_transformPivotY:
2480 setPivotY(a.getDimensionPixelOffset(attr, 0));
2481 break;
2482 case com.android.internal.R.styleable.View_translationX:
2483 tx = a.getDimensionPixelOffset(attr, 0);
2484 transformSet = true;
2485 break;
2486 case com.android.internal.R.styleable.View_translationY:
2487 ty = a.getDimensionPixelOffset(attr, 0);
2488 transformSet = true;
2489 break;
2490 case com.android.internal.R.styleable.View_rotation:
2491 rotation = a.getFloat(attr, 0);
2492 transformSet = true;
2493 break;
2494 case com.android.internal.R.styleable.View_rotationX:
2495 rotationX = a.getFloat(attr, 0);
2496 transformSet = true;
2497 break;
2498 case com.android.internal.R.styleable.View_rotationY:
2499 rotationY = a.getFloat(attr, 0);
2500 transformSet = true;
2501 break;
2502 case com.android.internal.R.styleable.View_scaleX:
2503 sx = a.getFloat(attr, 1f);
2504 transformSet = true;
2505 break;
2506 case com.android.internal.R.styleable.View_scaleY:
2507 sy = a.getFloat(attr, 1f);
2508 transformSet = true;
2509 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002510 case com.android.internal.R.styleable.View_id:
2511 mID = a.getResourceId(attr, NO_ID);
2512 break;
2513 case com.android.internal.R.styleable.View_tag:
2514 mTag = a.getText(attr);
2515 break;
2516 case com.android.internal.R.styleable.View_fitsSystemWindows:
2517 if (a.getBoolean(attr, false)) {
2518 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2519 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2520 }
2521 break;
2522 case com.android.internal.R.styleable.View_focusable:
2523 if (a.getBoolean(attr, false)) {
2524 viewFlagValues |= FOCUSABLE;
2525 viewFlagMasks |= FOCUSABLE_MASK;
2526 }
2527 break;
2528 case com.android.internal.R.styleable.View_focusableInTouchMode:
2529 if (a.getBoolean(attr, false)) {
2530 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2531 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2532 }
2533 break;
2534 case com.android.internal.R.styleable.View_clickable:
2535 if (a.getBoolean(attr, false)) {
2536 viewFlagValues |= CLICKABLE;
2537 viewFlagMasks |= CLICKABLE;
2538 }
2539 break;
2540 case com.android.internal.R.styleable.View_longClickable:
2541 if (a.getBoolean(attr, false)) {
2542 viewFlagValues |= LONG_CLICKABLE;
2543 viewFlagMasks |= LONG_CLICKABLE;
2544 }
2545 break;
2546 case com.android.internal.R.styleable.View_saveEnabled:
2547 if (!a.getBoolean(attr, true)) {
2548 viewFlagValues |= SAVE_DISABLED;
2549 viewFlagMasks |= SAVE_DISABLED_MASK;
2550 }
2551 break;
2552 case com.android.internal.R.styleable.View_duplicateParentState:
2553 if (a.getBoolean(attr, false)) {
2554 viewFlagValues |= DUPLICATE_PARENT_STATE;
2555 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_visibility:
2559 final int visibility = a.getInt(attr, 0);
2560 if (visibility != 0) {
2561 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2562 viewFlagMasks |= VISIBILITY_MASK;
2563 }
2564 break;
2565 case com.android.internal.R.styleable.View_drawingCacheQuality:
2566 final int cacheQuality = a.getInt(attr, 0);
2567 if (cacheQuality != 0) {
2568 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2569 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2570 }
2571 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002572 case com.android.internal.R.styleable.View_contentDescription:
2573 mContentDescription = a.getString(attr);
2574 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2576 if (!a.getBoolean(attr, true)) {
2577 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2578 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2579 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002580 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002581 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2582 if (!a.getBoolean(attr, true)) {
2583 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2584 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2585 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002586 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002587 case R.styleable.View_scrollbars:
2588 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2589 if (scrollbars != SCROLLBARS_NONE) {
2590 viewFlagValues |= scrollbars;
2591 viewFlagMasks |= SCROLLBARS_MASK;
2592 initializeScrollbars(a);
2593 }
2594 break;
2595 case R.styleable.View_fadingEdge:
2596 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2597 if (fadingEdge != FADING_EDGE_NONE) {
2598 viewFlagValues |= fadingEdge;
2599 viewFlagMasks |= FADING_EDGE_MASK;
2600 initializeFadingEdge(a);
2601 }
2602 break;
2603 case R.styleable.View_scrollbarStyle:
2604 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2605 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2606 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2607 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2608 }
2609 break;
2610 case R.styleable.View_isScrollContainer:
2611 setScrollContainer = true;
2612 if (a.getBoolean(attr, false)) {
2613 setScrollContainer(true);
2614 }
2615 break;
2616 case com.android.internal.R.styleable.View_keepScreenOn:
2617 if (a.getBoolean(attr, false)) {
2618 viewFlagValues |= KEEP_SCREEN_ON;
2619 viewFlagMasks |= KEEP_SCREEN_ON;
2620 }
2621 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002622 case R.styleable.View_filterTouchesWhenObscured:
2623 if (a.getBoolean(attr, false)) {
2624 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2625 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2626 }
2627 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 case R.styleable.View_nextFocusLeft:
2629 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2630 break;
2631 case R.styleable.View_nextFocusRight:
2632 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2633 break;
2634 case R.styleable.View_nextFocusUp:
2635 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2636 break;
2637 case R.styleable.View_nextFocusDown:
2638 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2639 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002640 case R.styleable.View_nextFocusForward:
2641 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2642 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002643 case R.styleable.View_minWidth:
2644 mMinWidth = a.getDimensionPixelSize(attr, 0);
2645 break;
2646 case R.styleable.View_minHeight:
2647 mMinHeight = a.getDimensionPixelSize(attr, 0);
2648 break;
Romain Guy9a817362009-05-01 10:57:14 -07002649 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002650 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002651 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002652 + "be used within a restricted context");
2653 }
2654
Romain Guy9a817362009-05-01 10:57:14 -07002655 final String handlerName = a.getString(attr);
2656 if (handlerName != null) {
2657 setOnClickListener(new OnClickListener() {
2658 private Method mHandler;
2659
2660 public void onClick(View v) {
2661 if (mHandler == null) {
2662 try {
2663 mHandler = getContext().getClass().getMethod(handlerName,
2664 View.class);
2665 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002666 int id = getId();
2667 String idText = id == NO_ID ? "" : " with id '"
2668 + getContext().getResources().getResourceEntryName(
2669 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002670 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002671 handlerName + "(View) in the activity "
2672 + getContext().getClass() + " for onClick handler"
2673 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002674 }
2675 }
2676
2677 try {
2678 mHandler.invoke(getContext(), View.this);
2679 } catch (IllegalAccessException e) {
2680 throw new IllegalStateException("Could not execute non "
2681 + "public method of the activity", e);
2682 } catch (InvocationTargetException e) {
2683 throw new IllegalStateException("Could not execute "
2684 + "method of the activity", e);
2685 }
2686 }
2687 });
2688 }
2689 break;
Adam Powell637d3372010-08-25 14:37:03 -07002690 case R.styleable.View_overScrollMode:
2691 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2692 break;
Adam Powell20232d02010-12-08 21:08:53 -08002693 case R.styleable.View_verticalScrollbarPosition:
2694 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2695 break;
Romain Guy171c5922011-01-06 10:04:23 -08002696 case R.styleable.View_layerType:
2697 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2698 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002699 }
2700 }
2701
Adam Powell637d3372010-08-25 14:37:03 -07002702 setOverScrollMode(overScrollMode);
2703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002704 if (background != null) {
2705 setBackgroundDrawable(background);
2706 }
2707
2708 if (padding >= 0) {
2709 leftPadding = padding;
2710 topPadding = padding;
2711 rightPadding = padding;
2712 bottomPadding = padding;
2713 }
2714
2715 // If the user specified the padding (either with android:padding or
2716 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2717 // use the default padding or the padding from the background drawable
2718 // (stored at this point in mPadding*)
2719 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2720 topPadding >= 0 ? topPadding : mPaddingTop,
2721 rightPadding >= 0 ? rightPadding : mPaddingRight,
2722 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2723
2724 if (viewFlagMasks != 0) {
2725 setFlags(viewFlagValues, viewFlagMasks);
2726 }
2727
2728 // Needs to be called after mViewFlags is set
2729 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2730 recomputePadding();
2731 }
2732
2733 if (x != 0 || y != 0) {
2734 scrollTo(x, y);
2735 }
2736
Chet Haase73066682010-11-29 15:55:32 -08002737 if (transformSet) {
2738 setTranslationX(tx);
2739 setTranslationY(ty);
2740 setRotation(rotation);
2741 setRotationX(rotationX);
2742 setRotationY(rotationY);
2743 setScaleX(sx);
2744 setScaleY(sy);
2745 }
2746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002747 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2748 setScrollContainer(true);
2749 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002750
2751 computeOpaqueFlags();
2752
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002753 a.recycle();
2754 }
2755
2756 /**
2757 * Non-public constructor for use in testing
2758 */
2759 View() {
2760 }
2761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002762 /**
2763 * <p>
2764 * Initializes the fading edges from a given set of styled attributes. This
2765 * method should be called by subclasses that need fading edges and when an
2766 * instance of these subclasses is created programmatically rather than
2767 * being inflated from XML. This method is automatically called when the XML
2768 * is inflated.
2769 * </p>
2770 *
2771 * @param a the styled attributes set to initialize the fading edges from
2772 */
2773 protected void initializeFadingEdge(TypedArray a) {
2774 initScrollCache();
2775
2776 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2777 R.styleable.View_fadingEdgeLength,
2778 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2779 }
2780
2781 /**
2782 * Returns the size of the vertical faded edges used to indicate that more
2783 * content in this view is visible.
2784 *
2785 * @return The size in pixels of the vertical faded edge or 0 if vertical
2786 * faded edges are not enabled for this view.
2787 * @attr ref android.R.styleable#View_fadingEdgeLength
2788 */
2789 public int getVerticalFadingEdgeLength() {
2790 if (isVerticalFadingEdgeEnabled()) {
2791 ScrollabilityCache cache = mScrollCache;
2792 if (cache != null) {
2793 return cache.fadingEdgeLength;
2794 }
2795 }
2796 return 0;
2797 }
2798
2799 /**
2800 * Set the size of the faded edge used to indicate that more content in this
2801 * view is available. Will not change whether the fading edge is enabled; use
2802 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2803 * to enable the fading edge for the vertical or horizontal fading edges.
2804 *
2805 * @param length The size in pixels of the faded edge used to indicate that more
2806 * content in this view is visible.
2807 */
2808 public void setFadingEdgeLength(int length) {
2809 initScrollCache();
2810 mScrollCache.fadingEdgeLength = length;
2811 }
2812
2813 /**
2814 * Returns the size of the horizontal faded edges used to indicate that more
2815 * content in this view is visible.
2816 *
2817 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2818 * faded edges are not enabled for this view.
2819 * @attr ref android.R.styleable#View_fadingEdgeLength
2820 */
2821 public int getHorizontalFadingEdgeLength() {
2822 if (isHorizontalFadingEdgeEnabled()) {
2823 ScrollabilityCache cache = mScrollCache;
2824 if (cache != null) {
2825 return cache.fadingEdgeLength;
2826 }
2827 }
2828 return 0;
2829 }
2830
2831 /**
2832 * Returns the width of the vertical scrollbar.
2833 *
2834 * @return The width in pixels of the vertical scrollbar or 0 if there
2835 * is no vertical scrollbar.
2836 */
2837 public int getVerticalScrollbarWidth() {
2838 ScrollabilityCache cache = mScrollCache;
2839 if (cache != null) {
2840 ScrollBarDrawable scrollBar = cache.scrollBar;
2841 if (scrollBar != null) {
2842 int size = scrollBar.getSize(true);
2843 if (size <= 0) {
2844 size = cache.scrollBarSize;
2845 }
2846 return size;
2847 }
2848 return 0;
2849 }
2850 return 0;
2851 }
2852
2853 /**
2854 * Returns the height of the horizontal scrollbar.
2855 *
2856 * @return The height in pixels of the horizontal scrollbar or 0 if
2857 * there is no horizontal scrollbar.
2858 */
2859 protected int getHorizontalScrollbarHeight() {
2860 ScrollabilityCache cache = mScrollCache;
2861 if (cache != null) {
2862 ScrollBarDrawable scrollBar = cache.scrollBar;
2863 if (scrollBar != null) {
2864 int size = scrollBar.getSize(false);
2865 if (size <= 0) {
2866 size = cache.scrollBarSize;
2867 }
2868 return size;
2869 }
2870 return 0;
2871 }
2872 return 0;
2873 }
2874
2875 /**
2876 * <p>
2877 * Initializes the scrollbars from a given set of styled attributes. This
2878 * method should be called by subclasses that need scrollbars and when an
2879 * instance of these subclasses is created programmatically rather than
2880 * being inflated from XML. This method is automatically called when the XML
2881 * is inflated.
2882 * </p>
2883 *
2884 * @param a the styled attributes set to initialize the scrollbars from
2885 */
2886 protected void initializeScrollbars(TypedArray a) {
2887 initScrollCache();
2888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002890
Mike Cleronf116bf82009-09-27 19:14:12 -07002891 if (scrollabilityCache.scrollBar == null) {
2892 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2893 }
Joe Malin32736f02011-01-19 16:14:20 -08002894
Romain Guy8bda2482010-03-02 11:42:11 -08002895 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896
Mike Cleronf116bf82009-09-27 19:14:12 -07002897 if (!fadeScrollbars) {
2898 scrollabilityCache.state = ScrollabilityCache.ON;
2899 }
2900 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002901
2902
Mike Cleronf116bf82009-09-27 19:14:12 -07002903 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2904 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2905 .getScrollBarFadeDuration());
2906 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2907 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2908 ViewConfiguration.getScrollDefaultDelay());
2909
Joe Malin32736f02011-01-19 16:14:20 -08002910
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002911 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2912 com.android.internal.R.styleable.View_scrollbarSize,
2913 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2914
2915 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2916 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2917
2918 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2919 if (thumb != null) {
2920 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2921 }
2922
2923 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2924 false);
2925 if (alwaysDraw) {
2926 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2927 }
2928
2929 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2930 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2931
2932 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2933 if (thumb != null) {
2934 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2935 }
2936
2937 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2938 false);
2939 if (alwaysDraw) {
2940 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2941 }
2942
2943 // Re-apply user/background padding so that scrollbar(s) get added
2944 recomputePadding();
2945 }
2946
2947 /**
2948 * <p>
2949 * Initalizes the scrollability cache if necessary.
2950 * </p>
2951 */
2952 private void initScrollCache() {
2953 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002954 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002955 }
2956 }
2957
2958 /**
Adam Powell20232d02010-12-08 21:08:53 -08002959 * Set the position of the vertical scroll bar. Should be one of
2960 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2961 * {@link #SCROLLBAR_POSITION_RIGHT}.
2962 *
2963 * @param position Where the vertical scroll bar should be positioned.
2964 */
2965 public void setVerticalScrollbarPosition(int position) {
2966 if (mVerticalScrollbarPosition != position) {
2967 mVerticalScrollbarPosition = position;
2968 computeOpaqueFlags();
2969 recomputePadding();
2970 }
2971 }
2972
2973 /**
2974 * @return The position where the vertical scroll bar will show, if applicable.
2975 * @see #setVerticalScrollbarPosition(int)
2976 */
2977 public int getVerticalScrollbarPosition() {
2978 return mVerticalScrollbarPosition;
2979 }
2980
2981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002982 * Register a callback to be invoked when focus of this view changed.
2983 *
2984 * @param l The callback that will run.
2985 */
2986 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2987 mOnFocusChangeListener = l;
2988 }
2989
2990 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002991 * Add a listener that will be called when the bounds of the view change due to
2992 * layout processing.
2993 *
2994 * @param listener The listener that will be called when layout bounds change.
2995 */
2996 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2997 if (mOnLayoutChangeListeners == null) {
2998 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2999 }
3000 mOnLayoutChangeListeners.add(listener);
3001 }
3002
3003 /**
3004 * Remove a listener for layout changes.
3005 *
3006 * @param listener The listener for layout bounds change.
3007 */
3008 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3009 if (mOnLayoutChangeListeners == null) {
3010 return;
3011 }
3012 mOnLayoutChangeListeners.remove(listener);
3013 }
3014
3015 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003016 * Add a listener for attach state changes.
3017 *
3018 * This listener will be called whenever this view is attached or detached
3019 * from a window. Remove the listener using
3020 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3021 *
3022 * @param listener Listener to attach
3023 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3024 */
3025 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3026 if (mOnAttachStateChangeListeners == null) {
3027 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3028 }
3029 mOnAttachStateChangeListeners.add(listener);
3030 }
3031
3032 /**
3033 * Remove a listener for attach state changes. The listener will receive no further
3034 * notification of window attach/detach events.
3035 *
3036 * @param listener Listener to remove
3037 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3038 */
3039 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3040 if (mOnAttachStateChangeListeners == null) {
3041 return;
3042 }
3043 mOnAttachStateChangeListeners.remove(listener);
3044 }
3045
3046 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003047 * Returns the focus-change callback registered for this view.
3048 *
3049 * @return The callback, or null if one is not registered.
3050 */
3051 public OnFocusChangeListener getOnFocusChangeListener() {
3052 return mOnFocusChangeListener;
3053 }
3054
3055 /**
3056 * Register a callback to be invoked when this view is clicked. If this view is not
3057 * clickable, it becomes clickable.
3058 *
3059 * @param l The callback that will run
3060 *
3061 * @see #setClickable(boolean)
3062 */
3063 public void setOnClickListener(OnClickListener l) {
3064 if (!isClickable()) {
3065 setClickable(true);
3066 }
3067 mOnClickListener = l;
3068 }
3069
3070 /**
3071 * Register a callback to be invoked when this view is clicked and held. If this view is not
3072 * long clickable, it becomes long clickable.
3073 *
3074 * @param l The callback that will run
3075 *
3076 * @see #setLongClickable(boolean)
3077 */
3078 public void setOnLongClickListener(OnLongClickListener l) {
3079 if (!isLongClickable()) {
3080 setLongClickable(true);
3081 }
3082 mOnLongClickListener = l;
3083 }
3084
3085 /**
3086 * Register a callback to be invoked when the context menu for this view is
3087 * being built. If this view is not long clickable, it becomes long clickable.
3088 *
3089 * @param l The callback that will run
3090 *
3091 */
3092 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3093 if (!isLongClickable()) {
3094 setLongClickable(true);
3095 }
3096 mOnCreateContextMenuListener = l;
3097 }
3098
3099 /**
3100 * Call this view's OnClickListener, if it is defined.
3101 *
3102 * @return True there was an assigned OnClickListener that was called, false
3103 * otherwise is returned.
3104 */
3105 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003106 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 if (mOnClickListener != null) {
3109 playSoundEffect(SoundEffectConstants.CLICK);
3110 mOnClickListener.onClick(this);
3111 return true;
3112 }
3113
3114 return false;
3115 }
3116
3117 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003118 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3119 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003120 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003121 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003122 */
3123 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003124 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 boolean handled = false;
3127 if (mOnLongClickListener != null) {
3128 handled = mOnLongClickListener.onLongClick(View.this);
3129 }
3130 if (!handled) {
3131 handled = showContextMenu();
3132 }
3133 if (handled) {
3134 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3135 }
3136 return handled;
3137 }
3138
3139 /**
3140 * Bring up the context menu for this view.
3141 *
3142 * @return Whether a context menu was displayed.
3143 */
3144 public boolean showContextMenu() {
3145 return getParent().showContextMenuForChild(this);
3146 }
3147
3148 /**
Adam Powell6e346362010-07-23 10:18:23 -07003149 * Start an action mode.
3150 *
3151 * @param callback Callback that will control the lifecycle of the action mode
3152 * @return The new action mode if it is started, null otherwise
3153 *
3154 * @see ActionMode
3155 */
3156 public ActionMode startActionMode(ActionMode.Callback callback) {
3157 return getParent().startActionModeForChild(this, callback);
3158 }
3159
3160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 * Register a callback to be invoked when a key is pressed in this view.
3162 * @param l the key listener to attach to this view
3163 */
3164 public void setOnKeyListener(OnKeyListener l) {
3165 mOnKeyListener = l;
3166 }
3167
3168 /**
3169 * Register a callback to be invoked when a touch event is sent to this view.
3170 * @param l the touch listener to attach to this view
3171 */
3172 public void setOnTouchListener(OnTouchListener l) {
3173 mOnTouchListener = l;
3174 }
3175
3176 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003177 * Register a callback to be invoked when a generic motion event is sent to this view.
3178 * @param l the generic motion listener to attach to this view
3179 */
3180 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3181 mOnGenericMotionListener = l;
3182 }
3183
3184 /**
Joe Malin32736f02011-01-19 16:14:20 -08003185 * Register a drag event listener callback object for this View. The parameter is
3186 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3187 * View, the system calls the
3188 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3189 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003190 */
3191 public void setOnDragListener(OnDragListener l) {
3192 mOnDragListener = l;
3193 }
3194
3195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003196 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3197 *
3198 * Note: this does not check whether this {@link View} should get focus, it just
3199 * gives it focus no matter what. It should only be called internally by framework
3200 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3201 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003202 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3203 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204 * focus moved when requestFocus() is called. It may not always
3205 * apply, in which case use the default View.FOCUS_DOWN.
3206 * @param previouslyFocusedRect The rectangle of the view that had focus
3207 * prior in this View's coordinate system.
3208 */
3209 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3210 if (DBG) {
3211 System.out.println(this + " requestFocus()");
3212 }
3213
3214 if ((mPrivateFlags & FOCUSED) == 0) {
3215 mPrivateFlags |= FOCUSED;
3216
3217 if (mParent != null) {
3218 mParent.requestChildFocus(this, this);
3219 }
3220
3221 onFocusChanged(true, direction, previouslyFocusedRect);
3222 refreshDrawableState();
3223 }
3224 }
3225
3226 /**
3227 * Request that a rectangle of this view be visible on the screen,
3228 * scrolling if necessary just enough.
3229 *
3230 * <p>A View should call this if it maintains some notion of which part
3231 * of its content is interesting. For example, a text editing view
3232 * should call this when its cursor moves.
3233 *
3234 * @param rectangle The rectangle.
3235 * @return Whether any parent scrolled.
3236 */
3237 public boolean requestRectangleOnScreen(Rect rectangle) {
3238 return requestRectangleOnScreen(rectangle, false);
3239 }
3240
3241 /**
3242 * Request that a rectangle of this view be visible on the screen,
3243 * scrolling if necessary just enough.
3244 *
3245 * <p>A View should call this if it maintains some notion of which part
3246 * of its content is interesting. For example, a text editing view
3247 * should call this when its cursor moves.
3248 *
3249 * <p>When <code>immediate</code> is set to true, scrolling will not be
3250 * animated.
3251 *
3252 * @param rectangle The rectangle.
3253 * @param immediate True to forbid animated scrolling, false otherwise
3254 * @return Whether any parent scrolled.
3255 */
3256 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3257 View child = this;
3258 ViewParent parent = mParent;
3259 boolean scrolled = false;
3260 while (parent != null) {
3261 scrolled |= parent.requestChildRectangleOnScreen(child,
3262 rectangle, immediate);
3263
3264 // offset rect so next call has the rectangle in the
3265 // coordinate system of its direct child.
3266 rectangle.offset(child.getLeft(), child.getTop());
3267 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3268
3269 if (!(parent instanceof View)) {
3270 break;
3271 }
Romain Guy8506ab42009-06-11 17:35:47 -07003272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003273 child = (View) parent;
3274 parent = child.getParent();
3275 }
3276 return scrolled;
3277 }
3278
3279 /**
3280 * Called when this view wants to give up focus. This will cause
3281 * {@link #onFocusChanged} to be called.
3282 */
3283 public void clearFocus() {
3284 if (DBG) {
3285 System.out.println(this + " clearFocus()");
3286 }
3287
3288 if ((mPrivateFlags & FOCUSED) != 0) {
3289 mPrivateFlags &= ~FOCUSED;
3290
3291 if (mParent != null) {
3292 mParent.clearChildFocus(this);
3293 }
3294
3295 onFocusChanged(false, 0, null);
3296 refreshDrawableState();
3297 }
3298 }
3299
3300 /**
3301 * Called to clear the focus of a view that is about to be removed.
3302 * Doesn't call clearChildFocus, which prevents this view from taking
3303 * focus again before it has been removed from the parent
3304 */
3305 void clearFocusForRemoval() {
3306 if ((mPrivateFlags & FOCUSED) != 0) {
3307 mPrivateFlags &= ~FOCUSED;
3308
3309 onFocusChanged(false, 0, null);
3310 refreshDrawableState();
3311 }
3312 }
3313
3314 /**
3315 * Called internally by the view system when a new view is getting focus.
3316 * This is what clears the old focus.
3317 */
3318 void unFocus() {
3319 if (DBG) {
3320 System.out.println(this + " unFocus()");
3321 }
3322
3323 if ((mPrivateFlags & FOCUSED) != 0) {
3324 mPrivateFlags &= ~FOCUSED;
3325
3326 onFocusChanged(false, 0, null);
3327 refreshDrawableState();
3328 }
3329 }
3330
3331 /**
3332 * Returns true if this view has focus iteself, or is the ancestor of the
3333 * view that has focus.
3334 *
3335 * @return True if this view has or contains focus, false otherwise.
3336 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003337 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003338 public boolean hasFocus() {
3339 return (mPrivateFlags & FOCUSED) != 0;
3340 }
3341
3342 /**
3343 * Returns true if this view is focusable or if it contains a reachable View
3344 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3345 * is a View whose parents do not block descendants focus.
3346 *
3347 * Only {@link #VISIBLE} views are considered focusable.
3348 *
3349 * @return True if the view is focusable or if the view contains a focusable
3350 * View, false otherwise.
3351 *
3352 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3353 */
3354 public boolean hasFocusable() {
3355 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3356 }
3357
3358 /**
3359 * Called by the view system when the focus state of this view changes.
3360 * When the focus change event is caused by directional navigation, direction
3361 * and previouslyFocusedRect provide insight into where the focus is coming from.
3362 * When overriding, be sure to call up through to the super class so that
3363 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003365 * @param gainFocus True if the View has focus; false otherwise.
3366 * @param direction The direction focus has moved when requestFocus()
3367 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003368 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3369 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3370 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3372 * system, of the previously focused view. If applicable, this will be
3373 * passed in as finer grained information about where the focus is coming
3374 * from (in addition to direction). Will be <code>null</code> otherwise.
3375 */
3376 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003377 if (gainFocus) {
3378 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3379 }
3380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003381 InputMethodManager imm = InputMethodManager.peekInstance();
3382 if (!gainFocus) {
3383 if (isPressed()) {
3384 setPressed(false);
3385 }
3386 if (imm != null && mAttachInfo != null
3387 && mAttachInfo.mHasWindowFocus) {
3388 imm.focusOut(this);
3389 }
Romain Guya2431d02009-04-30 16:30:00 -07003390 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003391 } else if (imm != null && mAttachInfo != null
3392 && mAttachInfo.mHasWindowFocus) {
3393 imm.focusIn(this);
3394 }
Romain Guy8506ab42009-06-11 17:35:47 -07003395
Romain Guy0fd89bf2011-01-26 15:41:30 -08003396 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003397 if (mOnFocusChangeListener != null) {
3398 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3399 }
Joe Malin32736f02011-01-19 16:14:20 -08003400
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003401 if (mAttachInfo != null) {
3402 mAttachInfo.mKeyDispatchState.reset(this);
3403 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 }
3405
3406 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003407 * {@inheritDoc}
3408 */
3409 public void sendAccessibilityEvent(int eventType) {
3410 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3411 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3412 }
3413 }
3414
3415 /**
3416 * {@inheritDoc}
3417 */
3418 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003419 if (!isShown()) {
3420 return;
3421 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003422 event.setClassName(getClass().getName());
3423 event.setPackageName(getContext().getPackageName());
3424 event.setEnabled(isEnabled());
3425 event.setContentDescription(mContentDescription);
3426
3427 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3428 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3429 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3430 event.setItemCount(focusablesTempList.size());
3431 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3432 focusablesTempList.clear();
3433 }
3434
3435 dispatchPopulateAccessibilityEvent(event);
3436
3437 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3438 }
3439
3440 /**
3441 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3442 * to be populated.
3443 *
3444 * @param event The event.
3445 *
3446 * @return True if the event population was completed.
3447 */
3448 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3449 return false;
3450 }
3451
3452 /**
3453 * Gets the {@link View} description. It briefly describes the view and is
3454 * primarily used for accessibility support. Set this property to enable
3455 * better accessibility support for your application. This is especially
3456 * true for views that do not have textual representation (For example,
3457 * ImageButton).
3458 *
3459 * @return The content descriptiopn.
3460 *
3461 * @attr ref android.R.styleable#View_contentDescription
3462 */
3463 public CharSequence getContentDescription() {
3464 return mContentDescription;
3465 }
3466
3467 /**
3468 * Sets the {@link View} description. It briefly describes the view and is
3469 * primarily used for accessibility support. Set this property to enable
3470 * better accessibility support for your application. This is especially
3471 * true for views that do not have textual representation (For example,
3472 * ImageButton).
3473 *
3474 * @param contentDescription The content description.
3475 *
3476 * @attr ref android.R.styleable#View_contentDescription
3477 */
3478 public void setContentDescription(CharSequence contentDescription) {
3479 mContentDescription = contentDescription;
3480 }
3481
3482 /**
Romain Guya2431d02009-04-30 16:30:00 -07003483 * Invoked whenever this view loses focus, either by losing window focus or by losing
3484 * focus within its window. This method can be used to clear any state tied to the
3485 * focus. For instance, if a button is held pressed with the trackball and the window
3486 * loses focus, this method can be used to cancel the press.
3487 *
3488 * Subclasses of View overriding this method should always call super.onFocusLost().
3489 *
3490 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003491 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003492 *
3493 * @hide pending API council approval
3494 */
3495 protected void onFocusLost() {
3496 resetPressedState();
3497 }
3498
3499 private void resetPressedState() {
3500 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3501 return;
3502 }
3503
3504 if (isPressed()) {
3505 setPressed(false);
3506
3507 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003508 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003509 }
3510 }
3511 }
3512
3513 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003514 * Returns true if this view has focus
3515 *
3516 * @return True if this view has focus, false otherwise.
3517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003518 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003519 public boolean isFocused() {
3520 return (mPrivateFlags & FOCUSED) != 0;
3521 }
3522
3523 /**
3524 * Find the view in the hierarchy rooted at this view that currently has
3525 * focus.
3526 *
3527 * @return The view that currently has focus, or null if no focused view can
3528 * be found.
3529 */
3530 public View findFocus() {
3531 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3532 }
3533
3534 /**
3535 * Change whether this view is one of the set of scrollable containers in
3536 * its window. This will be used to determine whether the window can
3537 * resize or must pan when a soft input area is open -- scrollable
3538 * containers allow the window to use resize mode since the container
3539 * will appropriately shrink.
3540 */
3541 public void setScrollContainer(boolean isScrollContainer) {
3542 if (isScrollContainer) {
3543 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3544 mAttachInfo.mScrollContainers.add(this);
3545 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3546 }
3547 mPrivateFlags |= SCROLL_CONTAINER;
3548 } else {
3549 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3550 mAttachInfo.mScrollContainers.remove(this);
3551 }
3552 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3553 }
3554 }
3555
3556 /**
3557 * Returns the quality of the drawing cache.
3558 *
3559 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3560 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3561 *
3562 * @see #setDrawingCacheQuality(int)
3563 * @see #setDrawingCacheEnabled(boolean)
3564 * @see #isDrawingCacheEnabled()
3565 *
3566 * @attr ref android.R.styleable#View_drawingCacheQuality
3567 */
3568 public int getDrawingCacheQuality() {
3569 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3570 }
3571
3572 /**
3573 * Set the drawing cache quality of this view. This value is used only when the
3574 * drawing cache is enabled
3575 *
3576 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3577 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3578 *
3579 * @see #getDrawingCacheQuality()
3580 * @see #setDrawingCacheEnabled(boolean)
3581 * @see #isDrawingCacheEnabled()
3582 *
3583 * @attr ref android.R.styleable#View_drawingCacheQuality
3584 */
3585 public void setDrawingCacheQuality(int quality) {
3586 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3587 }
3588
3589 /**
3590 * Returns whether the screen should remain on, corresponding to the current
3591 * value of {@link #KEEP_SCREEN_ON}.
3592 *
3593 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3594 *
3595 * @see #setKeepScreenOn(boolean)
3596 *
3597 * @attr ref android.R.styleable#View_keepScreenOn
3598 */
3599 public boolean getKeepScreenOn() {
3600 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3601 }
3602
3603 /**
3604 * Controls whether the screen should remain on, modifying the
3605 * value of {@link #KEEP_SCREEN_ON}.
3606 *
3607 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3608 *
3609 * @see #getKeepScreenOn()
3610 *
3611 * @attr ref android.R.styleable#View_keepScreenOn
3612 */
3613 public void setKeepScreenOn(boolean keepScreenOn) {
3614 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3615 }
3616
3617 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003618 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3619 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003620 *
3621 * @attr ref android.R.styleable#View_nextFocusLeft
3622 */
3623 public int getNextFocusLeftId() {
3624 return mNextFocusLeftId;
3625 }
3626
3627 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003628 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3629 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3630 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 *
3632 * @attr ref android.R.styleable#View_nextFocusLeft
3633 */
3634 public void setNextFocusLeftId(int nextFocusLeftId) {
3635 mNextFocusLeftId = nextFocusLeftId;
3636 }
3637
3638 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003639 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3640 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003641 *
3642 * @attr ref android.R.styleable#View_nextFocusRight
3643 */
3644 public int getNextFocusRightId() {
3645 return mNextFocusRightId;
3646 }
3647
3648 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003649 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3650 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3651 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003652 *
3653 * @attr ref android.R.styleable#View_nextFocusRight
3654 */
3655 public void setNextFocusRightId(int nextFocusRightId) {
3656 mNextFocusRightId = nextFocusRightId;
3657 }
3658
3659 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003660 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3661 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003662 *
3663 * @attr ref android.R.styleable#View_nextFocusUp
3664 */
3665 public int getNextFocusUpId() {
3666 return mNextFocusUpId;
3667 }
3668
3669 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003670 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3671 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3672 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003673 *
3674 * @attr ref android.R.styleable#View_nextFocusUp
3675 */
3676 public void setNextFocusUpId(int nextFocusUpId) {
3677 mNextFocusUpId = nextFocusUpId;
3678 }
3679
3680 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003681 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3682 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 *
3684 * @attr ref android.R.styleable#View_nextFocusDown
3685 */
3686 public int getNextFocusDownId() {
3687 return mNextFocusDownId;
3688 }
3689
3690 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003691 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3692 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3693 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003694 *
3695 * @attr ref android.R.styleable#View_nextFocusDown
3696 */
3697 public void setNextFocusDownId(int nextFocusDownId) {
3698 mNextFocusDownId = nextFocusDownId;
3699 }
3700
3701 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003702 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3703 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3704 *
3705 * @attr ref android.R.styleable#View_nextFocusForward
3706 */
3707 public int getNextFocusForwardId() {
3708 return mNextFocusForwardId;
3709 }
3710
3711 /**
3712 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3713 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3714 * decide automatically.
3715 *
3716 * @attr ref android.R.styleable#View_nextFocusForward
3717 */
3718 public void setNextFocusForwardId(int nextFocusForwardId) {
3719 mNextFocusForwardId = nextFocusForwardId;
3720 }
3721
3722 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003723 * Returns the visibility of this view and all of its ancestors
3724 *
3725 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3726 */
3727 public boolean isShown() {
3728 View current = this;
3729 //noinspection ConstantConditions
3730 do {
3731 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3732 return false;
3733 }
3734 ViewParent parent = current.mParent;
3735 if (parent == null) {
3736 return false; // We are not attached to the view root
3737 }
3738 if (!(parent instanceof View)) {
3739 return true;
3740 }
3741 current = (View) parent;
3742 } while (current != null);
3743
3744 return false;
3745 }
3746
3747 /**
3748 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3749 * is set
3750 *
3751 * @param insets Insets for system windows
3752 *
3753 * @return True if this view applied the insets, false otherwise
3754 */
3755 protected boolean fitSystemWindows(Rect insets) {
3756 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3757 mPaddingLeft = insets.left;
3758 mPaddingTop = insets.top;
3759 mPaddingRight = insets.right;
3760 mPaddingBottom = insets.bottom;
3761 requestLayout();
3762 return true;
3763 }
3764 return false;
3765 }
3766
3767 /**
3768 * Returns the visibility status for this view.
3769 *
3770 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3771 * @attr ref android.R.styleable#View_visibility
3772 */
3773 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003774 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3775 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3776 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003777 })
3778 public int getVisibility() {
3779 return mViewFlags & VISIBILITY_MASK;
3780 }
3781
3782 /**
3783 * Set the enabled state of this view.
3784 *
3785 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3786 * @attr ref android.R.styleable#View_visibility
3787 */
3788 @RemotableViewMethod
3789 public void setVisibility(int visibility) {
3790 setFlags(visibility, VISIBILITY_MASK);
3791 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3792 }
3793
3794 /**
3795 * Returns the enabled status for this view. The interpretation of the
3796 * enabled state varies by subclass.
3797 *
3798 * @return True if this view is enabled, false otherwise.
3799 */
3800 @ViewDebug.ExportedProperty
3801 public boolean isEnabled() {
3802 return (mViewFlags & ENABLED_MASK) == ENABLED;
3803 }
3804
3805 /**
3806 * Set the enabled state of this view. The interpretation of the enabled
3807 * state varies by subclass.
3808 *
3809 * @param enabled True if this view is enabled, false otherwise.
3810 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003811 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003812 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003813 if (enabled == isEnabled()) return;
3814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003815 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3816
3817 /*
3818 * The View most likely has to change its appearance, so refresh
3819 * the drawable state.
3820 */
3821 refreshDrawableState();
3822
3823 // Invalidate too, since the default behavior for views is to be
3824 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003825 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003826 }
3827
3828 /**
3829 * Set whether this view can receive the focus.
3830 *
3831 * Setting this to false will also ensure that this view is not focusable
3832 * in touch mode.
3833 *
3834 * @param focusable If true, this view can receive the focus.
3835 *
3836 * @see #setFocusableInTouchMode(boolean)
3837 * @attr ref android.R.styleable#View_focusable
3838 */
3839 public void setFocusable(boolean focusable) {
3840 if (!focusable) {
3841 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3842 }
3843 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3844 }
3845
3846 /**
3847 * Set whether this view can receive focus while in touch mode.
3848 *
3849 * Setting this to true will also ensure that this view is focusable.
3850 *
3851 * @param focusableInTouchMode If true, this view can receive the focus while
3852 * in touch mode.
3853 *
3854 * @see #setFocusable(boolean)
3855 * @attr ref android.R.styleable#View_focusableInTouchMode
3856 */
3857 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3858 // Focusable in touch mode should always be set before the focusable flag
3859 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3860 // which, in touch mode, will not successfully request focus on this view
3861 // because the focusable in touch mode flag is not set
3862 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3863 if (focusableInTouchMode) {
3864 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3865 }
3866 }
3867
3868 /**
3869 * Set whether this view should have sound effects enabled for events such as
3870 * clicking and touching.
3871 *
3872 * <p>You may wish to disable sound effects for a view if you already play sounds,
3873 * for instance, a dial key that plays dtmf tones.
3874 *
3875 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3876 * @see #isSoundEffectsEnabled()
3877 * @see #playSoundEffect(int)
3878 * @attr ref android.R.styleable#View_soundEffectsEnabled
3879 */
3880 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3881 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3882 }
3883
3884 /**
3885 * @return whether this view should have sound effects enabled for events such as
3886 * clicking and touching.
3887 *
3888 * @see #setSoundEffectsEnabled(boolean)
3889 * @see #playSoundEffect(int)
3890 * @attr ref android.R.styleable#View_soundEffectsEnabled
3891 */
3892 @ViewDebug.ExportedProperty
3893 public boolean isSoundEffectsEnabled() {
3894 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3895 }
3896
3897 /**
3898 * Set whether this view should have haptic feedback for events such as
3899 * long presses.
3900 *
3901 * <p>You may wish to disable haptic feedback if your view already controls
3902 * its own haptic feedback.
3903 *
3904 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3905 * @see #isHapticFeedbackEnabled()
3906 * @see #performHapticFeedback(int)
3907 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3908 */
3909 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3910 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3911 }
3912
3913 /**
3914 * @return whether this view should have haptic feedback enabled for events
3915 * long presses.
3916 *
3917 * @see #setHapticFeedbackEnabled(boolean)
3918 * @see #performHapticFeedback(int)
3919 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3920 */
3921 @ViewDebug.ExportedProperty
3922 public boolean isHapticFeedbackEnabled() {
3923 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3924 }
3925
3926 /**
3927 * If this view doesn't do any drawing on its own, set this flag to
3928 * allow further optimizations. By default, this flag is not set on
3929 * View, but could be set on some View subclasses such as ViewGroup.
3930 *
3931 * Typically, if you override {@link #onDraw} you should clear this flag.
3932 *
3933 * @param willNotDraw whether or not this View draw on its own
3934 */
3935 public void setWillNotDraw(boolean willNotDraw) {
3936 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3937 }
3938
3939 /**
3940 * Returns whether or not this View draws on its own.
3941 *
3942 * @return true if this view has nothing to draw, false otherwise
3943 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003944 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003945 public boolean willNotDraw() {
3946 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3947 }
3948
3949 /**
3950 * When a View's drawing cache is enabled, drawing is redirected to an
3951 * offscreen bitmap. Some views, like an ImageView, must be able to
3952 * bypass this mechanism if they already draw a single bitmap, to avoid
3953 * unnecessary usage of the memory.
3954 *
3955 * @param willNotCacheDrawing true if this view does not cache its
3956 * drawing, false otherwise
3957 */
3958 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3959 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3960 }
3961
3962 /**
3963 * Returns whether or not this View can cache its drawing or not.
3964 *
3965 * @return true if this view does not cache its drawing, false otherwise
3966 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003967 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003968 public boolean willNotCacheDrawing() {
3969 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3970 }
3971
3972 /**
3973 * Indicates whether this view reacts to click events or not.
3974 *
3975 * @return true if the view is clickable, false otherwise
3976 *
3977 * @see #setClickable(boolean)
3978 * @attr ref android.R.styleable#View_clickable
3979 */
3980 @ViewDebug.ExportedProperty
3981 public boolean isClickable() {
3982 return (mViewFlags & CLICKABLE) == CLICKABLE;
3983 }
3984
3985 /**
3986 * Enables or disables click events for this view. When a view
3987 * is clickable it will change its state to "pressed" on every click.
3988 * Subclasses should set the view clickable to visually react to
3989 * user's clicks.
3990 *
3991 * @param clickable true to make the view clickable, false otherwise
3992 *
3993 * @see #isClickable()
3994 * @attr ref android.R.styleable#View_clickable
3995 */
3996 public void setClickable(boolean clickable) {
3997 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3998 }
3999
4000 /**
4001 * Indicates whether this view reacts to long click events or not.
4002 *
4003 * @return true if the view is long clickable, false otherwise
4004 *
4005 * @see #setLongClickable(boolean)
4006 * @attr ref android.R.styleable#View_longClickable
4007 */
4008 public boolean isLongClickable() {
4009 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4010 }
4011
4012 /**
4013 * Enables or disables long click events for this view. When a view is long
4014 * clickable it reacts to the user holding down the button for a longer
4015 * duration than a tap. This event can either launch the listener or a
4016 * context menu.
4017 *
4018 * @param longClickable true to make the view long clickable, false otherwise
4019 * @see #isLongClickable()
4020 * @attr ref android.R.styleable#View_longClickable
4021 */
4022 public void setLongClickable(boolean longClickable) {
4023 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4024 }
4025
4026 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004027 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 *
4029 * @see #isClickable()
4030 * @see #setClickable(boolean)
4031 *
4032 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4033 * the View's internal state from a previously set "pressed" state.
4034 */
4035 public void setPressed(boolean pressed) {
4036 if (pressed) {
4037 mPrivateFlags |= PRESSED;
4038 } else {
4039 mPrivateFlags &= ~PRESSED;
4040 }
4041 refreshDrawableState();
4042 dispatchSetPressed(pressed);
4043 }
4044
4045 /**
4046 * Dispatch setPressed to all of this View's children.
4047 *
4048 * @see #setPressed(boolean)
4049 *
4050 * @param pressed The new pressed state
4051 */
4052 protected void dispatchSetPressed(boolean pressed) {
4053 }
4054
4055 /**
4056 * Indicates whether the view is currently in pressed state. Unless
4057 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4058 * the pressed state.
4059 *
4060 * @see #setPressed
4061 * @see #isClickable()
4062 * @see #setClickable(boolean)
4063 *
4064 * @return true if the view is currently pressed, false otherwise
4065 */
4066 public boolean isPressed() {
4067 return (mPrivateFlags & PRESSED) == PRESSED;
4068 }
4069
4070 /**
4071 * Indicates whether this view will save its state (that is,
4072 * whether its {@link #onSaveInstanceState} method will be called).
4073 *
4074 * @return Returns true if the view state saving is enabled, else false.
4075 *
4076 * @see #setSaveEnabled(boolean)
4077 * @attr ref android.R.styleable#View_saveEnabled
4078 */
4079 public boolean isSaveEnabled() {
4080 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4081 }
4082
4083 /**
4084 * Controls whether the saving of this view's state is
4085 * enabled (that is, whether its {@link #onSaveInstanceState} method
4086 * will be called). Note that even if freezing is enabled, the
4087 * view still must have an id assigned to it (via {@link #setId setId()})
4088 * for its state to be saved. This flag can only disable the
4089 * saving of this view; any child views may still have their state saved.
4090 *
4091 * @param enabled Set to false to <em>disable</em> state saving, or true
4092 * (the default) to allow it.
4093 *
4094 * @see #isSaveEnabled()
4095 * @see #setId(int)
4096 * @see #onSaveInstanceState()
4097 * @attr ref android.R.styleable#View_saveEnabled
4098 */
4099 public void setSaveEnabled(boolean enabled) {
4100 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4101 }
4102
Jeff Brown85a31762010-09-01 17:01:00 -07004103 /**
4104 * Gets whether the framework should discard touches when the view's
4105 * window is obscured by another visible window.
4106 * Refer to the {@link View} security documentation for more details.
4107 *
4108 * @return True if touch filtering is enabled.
4109 *
4110 * @see #setFilterTouchesWhenObscured(boolean)
4111 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4112 */
4113 @ViewDebug.ExportedProperty
4114 public boolean getFilterTouchesWhenObscured() {
4115 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4116 }
4117
4118 /**
4119 * Sets whether the framework should discard touches when the view's
4120 * window is obscured by another visible window.
4121 * Refer to the {@link View} security documentation for more details.
4122 *
4123 * @param enabled True if touch filtering should be enabled.
4124 *
4125 * @see #getFilterTouchesWhenObscured
4126 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4127 */
4128 public void setFilterTouchesWhenObscured(boolean enabled) {
4129 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4130 FILTER_TOUCHES_WHEN_OBSCURED);
4131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004132
4133 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004134 * Indicates whether the entire hierarchy under this view will save its
4135 * state when a state saving traversal occurs from its parent. The default
4136 * is true; if false, these views will not be saved unless
4137 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4138 *
4139 * @return Returns true if the view state saving from parent is enabled, else false.
4140 *
4141 * @see #setSaveFromParentEnabled(boolean)
4142 */
4143 public boolean isSaveFromParentEnabled() {
4144 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4145 }
4146
4147 /**
4148 * Controls whether the entire hierarchy under this view will save its
4149 * state when a state saving traversal occurs from its parent. The default
4150 * is true; if false, these views will not be saved unless
4151 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4152 *
4153 * @param enabled Set to false to <em>disable</em> state saving, or true
4154 * (the default) to allow it.
4155 *
4156 * @see #isSaveFromParentEnabled()
4157 * @see #setId(int)
4158 * @see #onSaveInstanceState()
4159 */
4160 public void setSaveFromParentEnabled(boolean enabled) {
4161 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4162 }
4163
4164
4165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 * Returns whether this View is able to take focus.
4167 *
4168 * @return True if this view can take focus, or false otherwise.
4169 * @attr ref android.R.styleable#View_focusable
4170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004171 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 public final boolean isFocusable() {
4173 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4174 }
4175
4176 /**
4177 * When a view is focusable, it may not want to take focus when in touch mode.
4178 * For example, a button would like focus when the user is navigating via a D-pad
4179 * so that the user can click on it, but once the user starts touching the screen,
4180 * the button shouldn't take focus
4181 * @return Whether the view is focusable in touch mode.
4182 * @attr ref android.R.styleable#View_focusableInTouchMode
4183 */
4184 @ViewDebug.ExportedProperty
4185 public final boolean isFocusableInTouchMode() {
4186 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4187 }
4188
4189 /**
4190 * Find the nearest view in the specified direction that can take focus.
4191 * This does not actually give focus to that view.
4192 *
4193 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4194 *
4195 * @return The nearest focusable in the specified direction, or null if none
4196 * can be found.
4197 */
4198 public View focusSearch(int direction) {
4199 if (mParent != null) {
4200 return mParent.focusSearch(this, direction);
4201 } else {
4202 return null;
4203 }
4204 }
4205
4206 /**
4207 * This method is the last chance for the focused view and its ancestors to
4208 * respond to an arrow key. This is called when the focused view did not
4209 * consume the key internally, nor could the view system find a new view in
4210 * the requested direction to give focus to.
4211 *
4212 * @param focused The currently focused view.
4213 * @param direction The direction focus wants to move. One of FOCUS_UP,
4214 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4215 * @return True if the this view consumed this unhandled move.
4216 */
4217 public boolean dispatchUnhandledMove(View focused, int direction) {
4218 return false;
4219 }
4220
4221 /**
4222 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004223 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004225 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4226 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 * @return The user specified next view, or null if there is none.
4228 */
4229 View findUserSetNextFocus(View root, int direction) {
4230 switch (direction) {
4231 case FOCUS_LEFT:
4232 if (mNextFocusLeftId == View.NO_ID) return null;
4233 return findViewShouldExist(root, mNextFocusLeftId);
4234 case FOCUS_RIGHT:
4235 if (mNextFocusRightId == View.NO_ID) return null;
4236 return findViewShouldExist(root, mNextFocusRightId);
4237 case FOCUS_UP:
4238 if (mNextFocusUpId == View.NO_ID) return null;
4239 return findViewShouldExist(root, mNextFocusUpId);
4240 case FOCUS_DOWN:
4241 if (mNextFocusDownId == View.NO_ID) return null;
4242 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004243 case FOCUS_FORWARD:
4244 if (mNextFocusForwardId == View.NO_ID) return null;
4245 return findViewShouldExist(root, mNextFocusForwardId);
4246 case FOCUS_BACKWARD: {
4247 final int id = mID;
4248 return root.findViewByPredicate(new Predicate<View>() {
4249 @Override
4250 public boolean apply(View t) {
4251 return t.mNextFocusForwardId == id;
4252 }
4253 });
4254 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004255 }
4256 return null;
4257 }
4258
4259 private static View findViewShouldExist(View root, int childViewId) {
4260 View result = root.findViewById(childViewId);
4261 if (result == null) {
4262 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4263 + "by user for id " + childViewId);
4264 }
4265 return result;
4266 }
4267
4268 /**
4269 * Find and return all focusable views that are descendants of this view,
4270 * possibly including this view if it is focusable itself.
4271 *
4272 * @param direction The direction of the focus
4273 * @return A list of focusable views
4274 */
4275 public ArrayList<View> getFocusables(int direction) {
4276 ArrayList<View> result = new ArrayList<View>(24);
4277 addFocusables(result, direction);
4278 return result;
4279 }
4280
4281 /**
4282 * Add any focusable views that are descendants of this view (possibly
4283 * including this view if it is focusable itself) to views. If we are in touch mode,
4284 * only add views that are also focusable in touch mode.
4285 *
4286 * @param views Focusable views found so far
4287 * @param direction The direction of the focus
4288 */
4289 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004290 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292
svetoslavganov75986cf2009-05-14 22:28:01 -07004293 /**
4294 * Adds any focusable views that are descendants of this view (possibly
4295 * including this view if it is focusable itself) to views. This method
4296 * adds all focusable views regardless if we are in touch mode or
4297 * only views focusable in touch mode if we are in touch mode depending on
4298 * the focusable mode paramater.
4299 *
4300 * @param views Focusable views found so far or null if all we are interested is
4301 * the number of focusables.
4302 * @param direction The direction of the focus.
4303 * @param focusableMode The type of focusables to be added.
4304 *
4305 * @see #FOCUSABLES_ALL
4306 * @see #FOCUSABLES_TOUCH_MODE
4307 */
4308 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4309 if (!isFocusable()) {
4310 return;
4311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004312
svetoslavganov75986cf2009-05-14 22:28:01 -07004313 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4314 isInTouchMode() && !isFocusableInTouchMode()) {
4315 return;
4316 }
4317
4318 if (views != null) {
4319 views.add(this);
4320 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 }
4322
4323 /**
4324 * Find and return all touchable views that are descendants of this view,
4325 * possibly including this view if it is touchable itself.
4326 *
4327 * @return A list of touchable views
4328 */
4329 public ArrayList<View> getTouchables() {
4330 ArrayList<View> result = new ArrayList<View>();
4331 addTouchables(result);
4332 return result;
4333 }
4334
4335 /**
4336 * Add any touchable views that are descendants of this view (possibly
4337 * including this view if it is touchable itself) to views.
4338 *
4339 * @param views Touchable views found so far
4340 */
4341 public void addTouchables(ArrayList<View> views) {
4342 final int viewFlags = mViewFlags;
4343
4344 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4345 && (viewFlags & ENABLED_MASK) == ENABLED) {
4346 views.add(this);
4347 }
4348 }
4349
4350 /**
4351 * Call this to try to give focus to a specific view or to one of its
4352 * descendants.
4353 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004354 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4355 * false), or if it is focusable and it is not focusable in touch mode
4356 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 *
4358 * See also {@link #focusSearch}, which is what you call to say that you
4359 * have focus, and you want your parent to look for the next one.
4360 *
4361 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4362 * {@link #FOCUS_DOWN} and <code>null</code>.
4363 *
4364 * @return Whether this view or one of its descendants actually took focus.
4365 */
4366 public final boolean requestFocus() {
4367 return requestFocus(View.FOCUS_DOWN);
4368 }
4369
4370
4371 /**
4372 * Call this to try to give focus to a specific view or to one of its
4373 * descendants and give it a hint about what direction focus is heading.
4374 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004375 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4376 * false), or if it is focusable and it is not focusable in touch mode
4377 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004378 *
4379 * See also {@link #focusSearch}, which is what you call to say that you
4380 * have focus, and you want your parent to look for the next one.
4381 *
4382 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4383 * <code>null</code> set for the previously focused rectangle.
4384 *
4385 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4386 * @return Whether this view or one of its descendants actually took focus.
4387 */
4388 public final boolean requestFocus(int direction) {
4389 return requestFocus(direction, null);
4390 }
4391
4392 /**
4393 * Call this to try to give focus to a specific view or to one of its descendants
4394 * and give it hints about the direction and a specific rectangle that the focus
4395 * is coming from. The rectangle can help give larger views a finer grained hint
4396 * about where focus is coming from, and therefore, where to show selection, or
4397 * forward focus change internally.
4398 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004399 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4400 * false), or if it is focusable and it is not focusable in touch mode
4401 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004402 *
4403 * A View will not take focus if it is not visible.
4404 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004405 * A View will not take focus if one of its parents has
4406 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4407 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 *
4409 * See also {@link #focusSearch}, which is what you call to say that you
4410 * have focus, and you want your parent to look for the next one.
4411 *
4412 * You may wish to override this method if your custom {@link View} has an internal
4413 * {@link View} that it wishes to forward the request to.
4414 *
4415 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4416 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4417 * to give a finer grained hint about where focus is coming from. May be null
4418 * if there is no hint.
4419 * @return Whether this view or one of its descendants actually took focus.
4420 */
4421 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4422 // need to be focusable
4423 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4424 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4425 return false;
4426 }
4427
4428 // need to be focusable in touch mode if in touch mode
4429 if (isInTouchMode() &&
4430 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4431 return false;
4432 }
4433
4434 // need to not have any parents blocking us
4435 if (hasAncestorThatBlocksDescendantFocus()) {
4436 return false;
4437 }
4438
4439 handleFocusGainInternal(direction, previouslyFocusedRect);
4440 return true;
4441 }
4442
Christopher Tate2c095f32010-10-04 14:13:40 -07004443 /** Gets the ViewRoot, or null if not attached. */
4444 /*package*/ ViewRoot getViewRoot() {
4445 View root = getRootView();
4446 return root != null ? (ViewRoot)root.getParent() : null;
4447 }
4448
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004449 /**
4450 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4451 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4452 * touch mode to request focus when they are touched.
4453 *
4454 * @return Whether this view or one of its descendants actually took focus.
4455 *
4456 * @see #isInTouchMode()
4457 *
4458 */
4459 public final boolean requestFocusFromTouch() {
4460 // Leave touch mode if we need to
4461 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004462 ViewRoot viewRoot = getViewRoot();
4463 if (viewRoot != null) {
4464 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 }
4466 }
4467 return requestFocus(View.FOCUS_DOWN);
4468 }
4469
4470 /**
4471 * @return Whether any ancestor of this view blocks descendant focus.
4472 */
4473 private boolean hasAncestorThatBlocksDescendantFocus() {
4474 ViewParent ancestor = mParent;
4475 while (ancestor instanceof ViewGroup) {
4476 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4477 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4478 return true;
4479 } else {
4480 ancestor = vgAncestor.getParent();
4481 }
4482 }
4483 return false;
4484 }
4485
4486 /**
Romain Guya440b002010-02-24 15:57:54 -08004487 * @hide
4488 */
4489 public void dispatchStartTemporaryDetach() {
4490 onStartTemporaryDetach();
4491 }
4492
4493 /**
4494 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4496 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004497 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 */
4499 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004500 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004501 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004502 }
4503
4504 /**
4505 * @hide
4506 */
4507 public void dispatchFinishTemporaryDetach() {
4508 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 }
Romain Guy8506ab42009-06-11 17:35:47 -07004510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 /**
4512 * Called after {@link #onStartTemporaryDetach} when the container is done
4513 * changing the view.
4514 */
4515 public void onFinishTemporaryDetach() {
4516 }
Romain Guy8506ab42009-06-11 17:35:47 -07004517
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 /**
4519 * capture information of this view for later analysis: developement only
4520 * check dynamic switch to make sure we only dump view
4521 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4522 */
4523 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004524 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004525 return;
4526 }
4527 ViewDebug.dumpCapturedView(subTag, v);
4528 }
4529
4530 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004531 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4532 * for this view's window. Returns null if the view is not currently attached
4533 * to the window. Normally you will not need to use this directly, but
4534 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4535 */
4536 public KeyEvent.DispatcherState getKeyDispatcherState() {
4537 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4538 }
Joe Malin32736f02011-01-19 16:14:20 -08004539
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004541 * Dispatch a key event before it is processed by any input method
4542 * associated with the view hierarchy. This can be used to intercept
4543 * key events in special situations before the IME consumes them; a
4544 * typical example would be handling the BACK key to update the application's
4545 * UI instead of allowing the IME to see it and close itself.
4546 *
4547 * @param event The key event to be dispatched.
4548 * @return True if the event was handled, false otherwise.
4549 */
4550 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4551 return onKeyPreIme(event.getKeyCode(), event);
4552 }
4553
4554 /**
4555 * Dispatch a key event to the next view on the focus path. This path runs
4556 * from the top of the view tree down to the currently focused view. If this
4557 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4558 * the next node down the focus path. This method also fires any key
4559 * listeners.
4560 *
4561 * @param event The key event to be dispatched.
4562 * @return True if the event was handled, false otherwise.
4563 */
4564 public boolean dispatchKeyEvent(KeyEvent event) {
4565 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004566
Romain Guyf607bdc2010-09-10 19:20:06 -07004567 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004568 if (android.util.Config.LOGV) {
4569 captureViewInfo("captureViewKeyEvent", this);
4570 }
4571
Romain Guyf607bdc2010-09-10 19:20:06 -07004572 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004573 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4574 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4575 return true;
4576 }
4577
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004578 return event.dispatch(this, mAttachInfo != null
4579 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004580 }
4581
4582 /**
4583 * Dispatches a key shortcut event.
4584 *
4585 * @param event The key event to be dispatched.
4586 * @return True if the event was handled by the view, false otherwise.
4587 */
4588 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4589 return onKeyShortcut(event.getKeyCode(), event);
4590 }
4591
4592 /**
4593 * Pass the touch screen motion event down to the target view, or this
4594 * view if it is the target.
4595 *
4596 * @param event The motion event to be dispatched.
4597 * @return True if the event was handled by the view, false otherwise.
4598 */
4599 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004600 if (!onFilterTouchEventForSecurity(event)) {
4601 return false;
4602 }
4603
Romain Guyf607bdc2010-09-10 19:20:06 -07004604 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4606 mOnTouchListener.onTouch(this, event)) {
4607 return true;
4608 }
4609 return onTouchEvent(event);
4610 }
4611
4612 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004613 * Filter the touch event to apply security policies.
4614 *
4615 * @param event The motion event to be filtered.
4616 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004617 *
Jeff Brown85a31762010-09-01 17:01:00 -07004618 * @see #getFilterTouchesWhenObscured
4619 */
4620 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004621 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004622 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4623 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4624 // Window is obscured, drop this touch.
4625 return false;
4626 }
4627 return true;
4628 }
4629
4630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004631 * Pass a trackball motion event down to the focused view.
4632 *
4633 * @param event The motion event to be dispatched.
4634 * @return True if the event was handled by the view, false otherwise.
4635 */
4636 public boolean dispatchTrackballEvent(MotionEvent event) {
4637 //Log.i("view", "view=" + this + ", " + event.toString());
4638 return onTrackballEvent(event);
4639 }
4640
4641 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004642 * Dispatch a generic motion event.
4643 * <p>
4644 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4645 * are delivered to the view under the pointer. All other generic motion events are
4646 * delivered to the focused view.
4647 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004648 *
4649 * @param event The motion event to be dispatched.
4650 * @return True if the event was handled by the view, false otherwise.
4651 */
4652 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004653 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004654 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4655 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4656 return true;
4657 }
4658
Jeff Browncb1404e2011-01-15 18:14:15 -08004659 return onGenericMotionEvent(event);
4660 }
4661
4662 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004663 * Dispatch a pointer event.
4664 * <p>
4665 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4666 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4667 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4668 * and should not be expected to handle other pointing device features.
4669 * </p>
4670 *
4671 * @param event The motion event to be dispatched.
4672 * @return True if the event was handled by the view, false otherwise.
4673 * @hide
4674 */
4675 public final boolean dispatchPointerEvent(MotionEvent event) {
4676 if (event.isTouchEvent()) {
4677 return dispatchTouchEvent(event);
4678 } else {
4679 return dispatchGenericMotionEvent(event);
4680 }
4681 }
4682
4683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 * Called when the window containing this view gains or loses window focus.
4685 * ViewGroups should override to route to their children.
4686 *
4687 * @param hasFocus True if the window containing this view now has focus,
4688 * false otherwise.
4689 */
4690 public void dispatchWindowFocusChanged(boolean hasFocus) {
4691 onWindowFocusChanged(hasFocus);
4692 }
4693
4694 /**
4695 * Called when the window containing this view gains or loses focus. Note
4696 * that this is separate from view focus: to receive key events, both
4697 * your view and its window must have focus. If a window is displayed
4698 * on top of yours that takes input focus, then your own window will lose
4699 * focus but the view focus will remain unchanged.
4700 *
4701 * @param hasWindowFocus True if the window containing this view now has
4702 * focus, false otherwise.
4703 */
4704 public void onWindowFocusChanged(boolean hasWindowFocus) {
4705 InputMethodManager imm = InputMethodManager.peekInstance();
4706 if (!hasWindowFocus) {
4707 if (isPressed()) {
4708 setPressed(false);
4709 }
4710 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4711 imm.focusOut(this);
4712 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004713 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004714 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004715 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004716 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4717 imm.focusIn(this);
4718 }
4719 refreshDrawableState();
4720 }
4721
4722 /**
4723 * Returns true if this view is in a window that currently has window focus.
4724 * Note that this is not the same as the view itself having focus.
4725 *
4726 * @return True if this view is in a window that currently has window focus.
4727 */
4728 public boolean hasWindowFocus() {
4729 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4730 }
4731
4732 /**
Adam Powell326d8082009-12-09 15:10:07 -08004733 * Dispatch a view visibility change down the view hierarchy.
4734 * ViewGroups should override to route to their children.
4735 * @param changedView The view whose visibility changed. Could be 'this' or
4736 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004737 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4738 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004739 */
4740 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4741 onVisibilityChanged(changedView, visibility);
4742 }
4743
4744 /**
4745 * Called when the visibility of the view or an ancestor of the view is changed.
4746 * @param changedView The view whose visibility changed. Could be 'this' or
4747 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004748 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4749 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004750 */
4751 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004752 if (visibility == VISIBLE) {
4753 if (mAttachInfo != null) {
4754 initialAwakenScrollBars();
4755 } else {
4756 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4757 }
4758 }
Adam Powell326d8082009-12-09 15:10:07 -08004759 }
4760
4761 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004762 * Dispatch a hint about whether this view is displayed. For instance, when
4763 * a View moves out of the screen, it might receives a display hint indicating
4764 * the view is not displayed. Applications should not <em>rely</em> on this hint
4765 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004766 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004767 * @param hint A hint about whether or not this view is displayed:
4768 * {@link #VISIBLE} or {@link #INVISIBLE}.
4769 */
4770 public void dispatchDisplayHint(int hint) {
4771 onDisplayHint(hint);
4772 }
4773
4774 /**
4775 * Gives this view a hint about whether is displayed or not. For instance, when
4776 * a View moves out of the screen, it might receives a display hint indicating
4777 * the view is not displayed. Applications should not <em>rely</em> on this hint
4778 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004779 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004780 * @param hint A hint about whether or not this view is displayed:
4781 * {@link #VISIBLE} or {@link #INVISIBLE}.
4782 */
4783 protected void onDisplayHint(int hint) {
4784 }
4785
4786 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004787 * Dispatch a window visibility change down the view hierarchy.
4788 * ViewGroups should override to route to their children.
4789 *
4790 * @param visibility The new visibility of the window.
4791 *
4792 * @see #onWindowVisibilityChanged
4793 */
4794 public void dispatchWindowVisibilityChanged(int visibility) {
4795 onWindowVisibilityChanged(visibility);
4796 }
4797
4798 /**
4799 * Called when the window containing has change its visibility
4800 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4801 * that this tells you whether or not your window is being made visible
4802 * to the window manager; this does <em>not</em> tell you whether or not
4803 * your window is obscured by other windows on the screen, even if it
4804 * is itself visible.
4805 *
4806 * @param visibility The new visibility of the window.
4807 */
4808 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004809 if (visibility == VISIBLE) {
4810 initialAwakenScrollBars();
4811 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004812 }
4813
4814 /**
4815 * Returns the current visibility of the window this view is attached to
4816 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4817 *
4818 * @return Returns the current visibility of the view's window.
4819 */
4820 public int getWindowVisibility() {
4821 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4822 }
4823
4824 /**
4825 * Retrieve the overall visible display size in which the window this view is
4826 * attached to has been positioned in. This takes into account screen
4827 * decorations above the window, for both cases where the window itself
4828 * is being position inside of them or the window is being placed under
4829 * then and covered insets are used for the window to position its content
4830 * inside. In effect, this tells you the available area where content can
4831 * be placed and remain visible to users.
4832 *
4833 * <p>This function requires an IPC back to the window manager to retrieve
4834 * the requested information, so should not be used in performance critical
4835 * code like drawing.
4836 *
4837 * @param outRect Filled in with the visible display frame. If the view
4838 * is not attached to a window, this is simply the raw display size.
4839 */
4840 public void getWindowVisibleDisplayFrame(Rect outRect) {
4841 if (mAttachInfo != null) {
4842 try {
4843 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4844 } catch (RemoteException e) {
4845 return;
4846 }
4847 // XXX This is really broken, and probably all needs to be done
4848 // in the window manager, and we need to know more about whether
4849 // we want the area behind or in front of the IME.
4850 final Rect insets = mAttachInfo.mVisibleInsets;
4851 outRect.left += insets.left;
4852 outRect.top += insets.top;
4853 outRect.right -= insets.right;
4854 outRect.bottom -= insets.bottom;
4855 return;
4856 }
4857 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4858 outRect.set(0, 0, d.getWidth(), d.getHeight());
4859 }
4860
4861 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004862 * Dispatch a notification about a resource configuration change down
4863 * the view hierarchy.
4864 * ViewGroups should override to route to their children.
4865 *
4866 * @param newConfig The new resource configuration.
4867 *
4868 * @see #onConfigurationChanged
4869 */
4870 public void dispatchConfigurationChanged(Configuration newConfig) {
4871 onConfigurationChanged(newConfig);
4872 }
4873
4874 /**
4875 * Called when the current configuration of the resources being used
4876 * by the application have changed. You can use this to decide when
4877 * to reload resources that can changed based on orientation and other
4878 * configuration characterstics. You only need to use this if you are
4879 * not relying on the normal {@link android.app.Activity} mechanism of
4880 * recreating the activity instance upon a configuration change.
4881 *
4882 * @param newConfig The new resource configuration.
4883 */
4884 protected void onConfigurationChanged(Configuration newConfig) {
4885 }
4886
4887 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004888 * Private function to aggregate all per-view attributes in to the view
4889 * root.
4890 */
4891 void dispatchCollectViewAttributes(int visibility) {
4892 performCollectViewAttributes(visibility);
4893 }
4894
4895 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004896 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004897 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4898 mAttachInfo.mKeepScreenOn = true;
4899 }
4900 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4901 if (mOnSystemUiVisibilityChangeListener != null) {
4902 mAttachInfo.mHasSystemUiListeners = true;
4903 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 }
4905 }
4906
4907 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004908 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004909 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004910 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4911 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004912 ai.mRecomputeGlobalAttributes = true;
4913 }
4914 }
4915 }
4916
4917 /**
4918 * Returns whether the device is currently in touch mode. Touch mode is entered
4919 * once the user begins interacting with the device by touch, and affects various
4920 * things like whether focus is always visible to the user.
4921 *
4922 * @return Whether the device is in touch mode.
4923 */
4924 @ViewDebug.ExportedProperty
4925 public boolean isInTouchMode() {
4926 if (mAttachInfo != null) {
4927 return mAttachInfo.mInTouchMode;
4928 } else {
4929 return ViewRoot.isInTouchMode();
4930 }
4931 }
4932
4933 /**
4934 * Returns the context the view is running in, through which it can
4935 * access the current theme, resources, etc.
4936 *
4937 * @return The view's Context.
4938 */
4939 @ViewDebug.CapturedViewProperty
4940 public final Context getContext() {
4941 return mContext;
4942 }
4943
4944 /**
4945 * Handle a key event before it is processed by any input method
4946 * associated with the view hierarchy. This can be used to intercept
4947 * key events in special situations before the IME consumes them; a
4948 * typical example would be handling the BACK key to update the application's
4949 * UI instead of allowing the IME to see it and close itself.
4950 *
4951 * @param keyCode The value in event.getKeyCode().
4952 * @param event Description of the key event.
4953 * @return If you handled the event, return true. If you want to allow the
4954 * event to be handled by the next receiver, return false.
4955 */
4956 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4957 return false;
4958 }
4959
4960 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004961 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
4962 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004963 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4964 * is released, if the view is enabled and clickable.
4965 *
4966 * @param keyCode A key code that represents the button pressed, from
4967 * {@link android.view.KeyEvent}.
4968 * @param event The KeyEvent object that defines the button action.
4969 */
4970 public boolean onKeyDown(int keyCode, KeyEvent event) {
4971 boolean result = false;
4972
4973 switch (keyCode) {
4974 case KeyEvent.KEYCODE_DPAD_CENTER:
4975 case KeyEvent.KEYCODE_ENTER: {
4976 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4977 return true;
4978 }
4979 // Long clickable items don't necessarily have to be clickable
4980 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4981 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4982 (event.getRepeatCount() == 0)) {
4983 setPressed(true);
4984 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004985 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986 }
4987 return true;
4988 }
4989 break;
4990 }
4991 }
4992 return result;
4993 }
4994
4995 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004996 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4997 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4998 * the event).
4999 */
5000 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5001 return false;
5002 }
5003
5004 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005005 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5006 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5008 * {@link KeyEvent#KEYCODE_ENTER} is released.
5009 *
5010 * @param keyCode A key code that represents the button pressed, from
5011 * {@link android.view.KeyEvent}.
5012 * @param event The KeyEvent object that defines the button action.
5013 */
5014 public boolean onKeyUp(int keyCode, KeyEvent event) {
5015 boolean result = false;
5016
5017 switch (keyCode) {
5018 case KeyEvent.KEYCODE_DPAD_CENTER:
5019 case KeyEvent.KEYCODE_ENTER: {
5020 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5021 return true;
5022 }
5023 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5024 setPressed(false);
5025
5026 if (!mHasPerformedLongPress) {
5027 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005028 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029
5030 result = performClick();
5031 }
5032 }
5033 break;
5034 }
5035 }
5036 return result;
5037 }
5038
5039 /**
5040 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5041 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5042 * the event).
5043 *
5044 * @param keyCode A key code that represents the button pressed, from
5045 * {@link android.view.KeyEvent}.
5046 * @param repeatCount The number of times the action was made.
5047 * @param event The KeyEvent object that defines the button action.
5048 */
5049 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5050 return false;
5051 }
5052
5053 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005054 * Called on the focused view when a key shortcut event is not handled.
5055 * Override this method to implement local key shortcuts for the View.
5056 * Key shortcuts can also be implemented by setting the
5057 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005058 *
5059 * @param keyCode The value in event.getKeyCode().
5060 * @param event Description of the key event.
5061 * @return If you handled the event, return true. If you want to allow the
5062 * event to be handled by the next receiver, return false.
5063 */
5064 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5065 return false;
5066 }
5067
5068 /**
5069 * Check whether the called view is a text editor, in which case it
5070 * would make sense to automatically display a soft input window for
5071 * it. Subclasses should override this if they implement
5072 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005073 * a call on that method would return a non-null InputConnection, and
5074 * they are really a first-class editor that the user would normally
5075 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005076 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005077 * <p>The default implementation always returns false. This does
5078 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5079 * will not be called or the user can not otherwise perform edits on your
5080 * view; it is just a hint to the system that this is not the primary
5081 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005082 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005083 * @return Returns true if this view is a text editor, else false.
5084 */
5085 public boolean onCheckIsTextEditor() {
5086 return false;
5087 }
Romain Guy8506ab42009-06-11 17:35:47 -07005088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005089 /**
5090 * Create a new InputConnection for an InputMethod to interact
5091 * with the view. The default implementation returns null, since it doesn't
5092 * support input methods. You can override this to implement such support.
5093 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005094 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 * <p>When implementing this, you probably also want to implement
5096 * {@link #onCheckIsTextEditor()} to indicate you will return a
5097 * non-null InputConnection.
5098 *
5099 * @param outAttrs Fill in with attribute information about the connection.
5100 */
5101 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5102 return null;
5103 }
5104
5105 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005106 * Called by the {@link android.view.inputmethod.InputMethodManager}
5107 * when a view who is not the current
5108 * input connection target is trying to make a call on the manager. The
5109 * default implementation returns false; you can override this to return
5110 * true for certain views if you are performing InputConnection proxying
5111 * to them.
5112 * @param view The View that is making the InputMethodManager call.
5113 * @return Return true to allow the call, false to reject.
5114 */
5115 public boolean checkInputConnectionProxy(View view) {
5116 return false;
5117 }
Romain Guy8506ab42009-06-11 17:35:47 -07005118
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 * Show the context menu for this view. It is not safe to hold on to the
5121 * menu after returning from this method.
5122 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005123 * You should normally not overload this method. Overload
5124 * {@link #onCreateContextMenu(ContextMenu)} or define an
5125 * {@link OnCreateContextMenuListener} to add items to the context menu.
5126 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005127 * @param menu The context menu to populate
5128 */
5129 public void createContextMenu(ContextMenu menu) {
5130 ContextMenuInfo menuInfo = getContextMenuInfo();
5131
5132 // Sets the current menu info so all items added to menu will have
5133 // my extra info set.
5134 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5135
5136 onCreateContextMenu(menu);
5137 if (mOnCreateContextMenuListener != null) {
5138 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5139 }
5140
5141 // Clear the extra information so subsequent items that aren't mine don't
5142 // have my extra info.
5143 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5144
5145 if (mParent != null) {
5146 mParent.createContextMenu(menu);
5147 }
5148 }
5149
5150 /**
5151 * Views should implement this if they have extra information to associate
5152 * with the context menu. The return result is supplied as a parameter to
5153 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5154 * callback.
5155 *
5156 * @return Extra information about the item for which the context menu
5157 * should be shown. This information will vary across different
5158 * subclasses of View.
5159 */
5160 protected ContextMenuInfo getContextMenuInfo() {
5161 return null;
5162 }
5163
5164 /**
5165 * Views should implement this if the view itself is going to add items to
5166 * the context menu.
5167 *
5168 * @param menu the context menu to populate
5169 */
5170 protected void onCreateContextMenu(ContextMenu menu) {
5171 }
5172
5173 /**
5174 * Implement this method to handle trackball motion events. The
5175 * <em>relative</em> movement of the trackball since the last event
5176 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5177 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5178 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5179 * they will often be fractional values, representing the more fine-grained
5180 * movement information available from a trackball).
5181 *
5182 * @param event The motion event.
5183 * @return True if the event was handled, false otherwise.
5184 */
5185 public boolean onTrackballEvent(MotionEvent event) {
5186 return false;
5187 }
5188
5189 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005190 * Implement this method to handle generic motion events.
5191 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005192 * Generic motion events describe joystick movements, mouse hovers, track pad
5193 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005194 * {@link MotionEvent#getSource() source} of the motion event specifies
5195 * the class of input that was received. Implementations of this method
5196 * must examine the bits in the source before processing the event.
5197 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005198 * </p><p>
5199 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5200 * are delivered to the view under the pointer. All other generic motion events are
5201 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005202 * </p>
5203 * <code>
5204 * public boolean onGenericMotionEvent(MotionEvent event) {
5205 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005206 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5207 * // process the joystick movement...
5208 * return true;
5209 * }
5210 * }
5211 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5212 * switch (event.getAction()) {
5213 * case MotionEvent.ACTION_HOVER_MOVE:
5214 * // process the mouse hover movement...
5215 * return true;
5216 * case MotionEvent.ACTION_SCROLL:
5217 * // process the scroll wheel movement...
5218 * return true;
5219 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005220 * }
5221 * return super.onGenericMotionEvent(event);
5222 * }
5223 * </code>
5224 *
5225 * @param event The generic motion event being processed.
5226 *
5227 * @return Return true if you have consumed the event, false if you haven't.
5228 * The default implementation always returns false.
5229 */
5230 public boolean onGenericMotionEvent(MotionEvent event) {
5231 return false;
5232 }
5233
5234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005235 * Implement this method to handle touch screen motion events.
5236 *
5237 * @param event The motion event.
5238 * @return True if the event was handled, false otherwise.
5239 */
5240 public boolean onTouchEvent(MotionEvent event) {
5241 final int viewFlags = mViewFlags;
5242
5243 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5244 // A disabled view that is clickable still consumes the touch
5245 // events, it just doesn't respond to them.
5246 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5247 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5248 }
5249
5250 if (mTouchDelegate != null) {
5251 if (mTouchDelegate.onTouchEvent(event)) {
5252 return true;
5253 }
5254 }
5255
5256 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5257 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5258 switch (event.getAction()) {
5259 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005260 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5261 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005262 // take focus if we don't have it already and we should in
5263 // touch mode.
5264 boolean focusTaken = false;
5265 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5266 focusTaken = requestFocus();
5267 }
5268
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005269 if (prepressed) {
5270 // The button is being released before we actually
5271 // showed it as pressed. Make it show the pressed
5272 // state now (before scheduling the click) to ensure
5273 // the user sees it.
5274 mPrivateFlags |= PRESSED;
5275 refreshDrawableState();
5276 }
Joe Malin32736f02011-01-19 16:14:20 -08005277
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005278 if (!mHasPerformedLongPress) {
5279 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005280 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281
5282 // Only perform take click actions if we were in the pressed state
5283 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005284 // Use a Runnable and post this rather than calling
5285 // performClick directly. This lets other visual state
5286 // of the view update before click actions start.
5287 if (mPerformClick == null) {
5288 mPerformClick = new PerformClick();
5289 }
5290 if (!post(mPerformClick)) {
5291 performClick();
5292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005293 }
5294 }
5295
5296 if (mUnsetPressedState == null) {
5297 mUnsetPressedState = new UnsetPressedState();
5298 }
5299
Adam Powelle14579b2009-12-16 18:39:52 -08005300 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005301 postDelayed(mUnsetPressedState,
5302 ViewConfiguration.getPressedStateDuration());
5303 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005304 // If the post failed, unpress right now
5305 mUnsetPressedState.run();
5306 }
Adam Powelle14579b2009-12-16 18:39:52 -08005307 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005308 }
5309 break;
5310
5311 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005312 if (mPendingCheckForTap == null) {
5313 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005314 }
Adam Powelle14579b2009-12-16 18:39:52 -08005315 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005316 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005317 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 break;
5319
5320 case MotionEvent.ACTION_CANCEL:
5321 mPrivateFlags &= ~PRESSED;
5322 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005323 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 break;
5325
5326 case MotionEvent.ACTION_MOVE:
5327 final int x = (int) event.getX();
5328 final int y = (int) event.getY();
5329
5330 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005331 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005332 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005333 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005334 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005335 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005336 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005337
5338 // Need to switch from pressed to not pressed
5339 mPrivateFlags &= ~PRESSED;
5340 refreshDrawableState();
5341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005342 }
5343 break;
5344 }
5345 return true;
5346 }
5347
5348 return false;
5349 }
5350
5351 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005352 * Remove the longpress detection timer.
5353 */
5354 private void removeLongPressCallback() {
5355 if (mPendingCheckForLongPress != null) {
5356 removeCallbacks(mPendingCheckForLongPress);
5357 }
5358 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005359
5360 /**
5361 * Remove the pending click action
5362 */
5363 private void removePerformClickCallback() {
5364 if (mPerformClick != null) {
5365 removeCallbacks(mPerformClick);
5366 }
5367 }
5368
Adam Powelle14579b2009-12-16 18:39:52 -08005369 /**
Romain Guya440b002010-02-24 15:57:54 -08005370 * Remove the prepress detection timer.
5371 */
5372 private void removeUnsetPressCallback() {
5373 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5374 setPressed(false);
5375 removeCallbacks(mUnsetPressedState);
5376 }
5377 }
5378
5379 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005380 * Remove the tap detection timer.
5381 */
5382 private void removeTapCallback() {
5383 if (mPendingCheckForTap != null) {
5384 mPrivateFlags &= ~PREPRESSED;
5385 removeCallbacks(mPendingCheckForTap);
5386 }
5387 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005388
5389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005390 * Cancels a pending long press. Your subclass can use this if you
5391 * want the context menu to come up if the user presses and holds
5392 * at the same place, but you don't want it to come up if they press
5393 * and then move around enough to cause scrolling.
5394 */
5395 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005396 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005397
5398 /*
5399 * The prepressed state handled by the tap callback is a display
5400 * construct, but the tap callback will post a long press callback
5401 * less its own timeout. Remove it here.
5402 */
5403 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005404 }
5405
5406 /**
5407 * Sets the TouchDelegate for this View.
5408 */
5409 public void setTouchDelegate(TouchDelegate delegate) {
5410 mTouchDelegate = delegate;
5411 }
5412
5413 /**
5414 * Gets the TouchDelegate for this View.
5415 */
5416 public TouchDelegate getTouchDelegate() {
5417 return mTouchDelegate;
5418 }
5419
5420 /**
5421 * Set flags controlling behavior of this view.
5422 *
5423 * @param flags Constant indicating the value which should be set
5424 * @param mask Constant indicating the bit range that should be changed
5425 */
5426 void setFlags(int flags, int mask) {
5427 int old = mViewFlags;
5428 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5429
5430 int changed = mViewFlags ^ old;
5431 if (changed == 0) {
5432 return;
5433 }
5434 int privateFlags = mPrivateFlags;
5435
5436 /* Check if the FOCUSABLE bit has changed */
5437 if (((changed & FOCUSABLE_MASK) != 0) &&
5438 ((privateFlags & HAS_BOUNDS) !=0)) {
5439 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5440 && ((privateFlags & FOCUSED) != 0)) {
5441 /* Give up focus if we are no longer focusable */
5442 clearFocus();
5443 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5444 && ((privateFlags & FOCUSED) == 0)) {
5445 /*
5446 * Tell the view system that we are now available to take focus
5447 * if no one else already has it.
5448 */
5449 if (mParent != null) mParent.focusableViewAvailable(this);
5450 }
5451 }
5452
5453 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5454 if ((changed & VISIBILITY_MASK) != 0) {
5455 /*
5456 * If this view is becoming visible, set the DRAWN flag so that
5457 * the next invalidate() will not be skipped.
5458 */
5459 mPrivateFlags |= DRAWN;
5460
5461 needGlobalAttributesUpdate(true);
5462
5463 // a view becoming visible is worth notifying the parent
5464 // about in case nothing has focus. even if this specific view
5465 // isn't focusable, it may contain something that is, so let
5466 // the root view try to give this focus if nothing else does.
5467 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5468 mParent.focusableViewAvailable(this);
5469 }
5470 }
5471 }
5472
5473 /* Check if the GONE bit has changed */
5474 if ((changed & GONE) != 0) {
5475 needGlobalAttributesUpdate(false);
5476 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005477 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478
Romain Guyecd80ee2009-12-03 17:13:02 -08005479 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5480 if (hasFocus()) clearFocus();
5481 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005482 }
5483 if (mAttachInfo != null) {
5484 mAttachInfo.mViewVisibilityChanged = true;
5485 }
5486 }
5487
5488 /* Check if the VISIBLE bit has changed */
5489 if ((changed & INVISIBLE) != 0) {
5490 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005491 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492
5493 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5494 // root view becoming invisible shouldn't clear focus
5495 if (getRootView() != this) {
5496 clearFocus();
5497 }
5498 }
5499 if (mAttachInfo != null) {
5500 mAttachInfo.mViewVisibilityChanged = true;
5501 }
5502 }
5503
Adam Powell326d8082009-12-09 15:10:07 -08005504 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005505 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005506 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5507 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005508 }
Adam Powell326d8082009-12-09 15:10:07 -08005509 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5510 }
5511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005512 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5513 destroyDrawingCache();
5514 }
5515
5516 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5517 destroyDrawingCache();
5518 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005519 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005520 }
5521
5522 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5523 destroyDrawingCache();
5524 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5525 }
5526
5527 if ((changed & DRAW_MASK) != 0) {
5528 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5529 if (mBGDrawable != null) {
5530 mPrivateFlags &= ~SKIP_DRAW;
5531 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5532 } else {
5533 mPrivateFlags |= SKIP_DRAW;
5534 }
5535 } else {
5536 mPrivateFlags &= ~SKIP_DRAW;
5537 }
5538 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005539 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 }
5541
5542 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005543 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 mParent.recomputeViewAttributes(this);
5545 }
5546 }
5547 }
5548
5549 /**
5550 * Change the view's z order in the tree, so it's on top of other sibling
5551 * views
5552 */
5553 public void bringToFront() {
5554 if (mParent != null) {
5555 mParent.bringChildToFront(this);
5556 }
5557 }
5558
5559 /**
5560 * This is called in response to an internal scroll in this view (i.e., the
5561 * view scrolled its own contents). This is typically as a result of
5562 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5563 * called.
5564 *
5565 * @param l Current horizontal scroll origin.
5566 * @param t Current vertical scroll origin.
5567 * @param oldl Previous horizontal scroll origin.
5568 * @param oldt Previous vertical scroll origin.
5569 */
5570 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5571 mBackgroundSizeChanged = true;
5572
5573 final AttachInfo ai = mAttachInfo;
5574 if (ai != null) {
5575 ai.mViewScrollChanged = true;
5576 }
5577 }
5578
5579 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005580 * Interface definition for a callback to be invoked when the layout bounds of a view
5581 * changes due to layout processing.
5582 */
5583 public interface OnLayoutChangeListener {
5584 /**
5585 * Called when the focus state of a view has changed.
5586 *
5587 * @param v The view whose state has changed.
5588 * @param left The new value of the view's left property.
5589 * @param top The new value of the view's top property.
5590 * @param right The new value of the view's right property.
5591 * @param bottom The new value of the view's bottom property.
5592 * @param oldLeft The previous value of the view's left property.
5593 * @param oldTop The previous value of the view's top property.
5594 * @param oldRight The previous value of the view's right property.
5595 * @param oldBottom The previous value of the view's bottom property.
5596 */
5597 void onLayoutChange(View v, int left, int top, int right, int bottom,
5598 int oldLeft, int oldTop, int oldRight, int oldBottom);
5599 }
5600
5601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 * This is called during layout when the size of this view has changed. If
5603 * you were just added to the view hierarchy, you're called with the old
5604 * values of 0.
5605 *
5606 * @param w Current width of this view.
5607 * @param h Current height of this view.
5608 * @param oldw Old width of this view.
5609 * @param oldh Old height of this view.
5610 */
5611 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5612 }
5613
5614 /**
5615 * Called by draw to draw the child views. This may be overridden
5616 * by derived classes to gain control just before its children are drawn
5617 * (but after its own view has been drawn).
5618 * @param canvas the canvas on which to draw the view
5619 */
5620 protected void dispatchDraw(Canvas canvas) {
5621 }
5622
5623 /**
5624 * Gets the parent of this view. Note that the parent is a
5625 * ViewParent and not necessarily a View.
5626 *
5627 * @return Parent of this view.
5628 */
5629 public final ViewParent getParent() {
5630 return mParent;
5631 }
5632
5633 /**
5634 * Return the scrolled left position of this view. This is the left edge of
5635 * the displayed part of your view. You do not need to draw any pixels
5636 * farther left, since those are outside of the frame of your view on
5637 * screen.
5638 *
5639 * @return The left edge of the displayed part of your view, in pixels.
5640 */
5641 public final int getScrollX() {
5642 return mScrollX;
5643 }
5644
5645 /**
5646 * Return the scrolled top position of this view. This is the top edge of
5647 * the displayed part of your view. You do not need to draw any pixels above
5648 * it, since those are outside of the frame of your view on screen.
5649 *
5650 * @return The top edge of the displayed part of your view, in pixels.
5651 */
5652 public final int getScrollY() {
5653 return mScrollY;
5654 }
5655
5656 /**
5657 * Return the width of the your view.
5658 *
5659 * @return The width of your view, in pixels.
5660 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005661 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005662 public final int getWidth() {
5663 return mRight - mLeft;
5664 }
5665
5666 /**
5667 * Return the height of your view.
5668 *
5669 * @return The height of your view, in pixels.
5670 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005671 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 public final int getHeight() {
5673 return mBottom - mTop;
5674 }
5675
5676 /**
5677 * Return the visible drawing bounds of your view. Fills in the output
5678 * rectangle with the values from getScrollX(), getScrollY(),
5679 * getWidth(), and getHeight().
5680 *
5681 * @param outRect The (scrolled) drawing bounds of the view.
5682 */
5683 public void getDrawingRect(Rect outRect) {
5684 outRect.left = mScrollX;
5685 outRect.top = mScrollY;
5686 outRect.right = mScrollX + (mRight - mLeft);
5687 outRect.bottom = mScrollY + (mBottom - mTop);
5688 }
5689
5690 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005691 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5692 * raw width component (that is the result is masked by
5693 * {@link #MEASURED_SIZE_MASK}).
5694 *
5695 * @return The raw measured width of this view.
5696 */
5697 public final int getMeasuredWidth() {
5698 return mMeasuredWidth & MEASURED_SIZE_MASK;
5699 }
5700
5701 /**
5702 * Return the full width measurement information for this view as computed
5703 * by the most recent call to {@link #measure}. This result is a bit mask
5704 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005705 * This should be used during measurement and layout calculations only. Use
5706 * {@link #getWidth()} to see how wide a view is after layout.
5707 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005708 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005710 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005711 return mMeasuredWidth;
5712 }
5713
5714 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005715 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5716 * raw width component (that is the result is masked by
5717 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005719 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005720 */
5721 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005722 return mMeasuredHeight & MEASURED_SIZE_MASK;
5723 }
5724
5725 /**
5726 * Return the full height measurement information for this view as computed
5727 * by the most recent call to {@link #measure}. This result is a bit mask
5728 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5729 * This should be used during measurement and layout calculations only. Use
5730 * {@link #getHeight()} to see how wide a view is after layout.
5731 *
5732 * @return The measured width of this view as a bit mask.
5733 */
5734 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 return mMeasuredHeight;
5736 }
5737
5738 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005739 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5740 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5741 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5742 * and the height component is at the shifted bits
5743 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5744 */
5745 public final int getMeasuredState() {
5746 return (mMeasuredWidth&MEASURED_STATE_MASK)
5747 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5748 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5749 }
5750
5751 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005752 * The transform matrix of this view, which is calculated based on the current
5753 * roation, scale, and pivot properties.
5754 *
5755 * @see #getRotation()
5756 * @see #getScaleX()
5757 * @see #getScaleY()
5758 * @see #getPivotX()
5759 * @see #getPivotY()
5760 * @return The current transform matrix for the view
5761 */
5762 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005763 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005764 return mMatrix;
5765 }
5766
5767 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005768 * Utility function to determine if the value is far enough away from zero to be
5769 * considered non-zero.
5770 * @param value A floating point value to check for zero-ness
5771 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5772 */
5773 private static boolean nonzero(float value) {
5774 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5775 }
5776
5777 /**
Jeff Brown86671742010-09-30 20:00:15 -07005778 * Returns true if the transform matrix is the identity matrix.
5779 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005780 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005781 * @return True if the transform matrix is the identity matrix, false otherwise.
5782 */
Jeff Brown86671742010-09-30 20:00:15 -07005783 final boolean hasIdentityMatrix() {
5784 updateMatrix();
5785 return mMatrixIsIdentity;
5786 }
5787
5788 /**
5789 * Recomputes the transform matrix if necessary.
5790 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005791 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005792 if (mMatrixDirty) {
5793 // transform-related properties have changed since the last time someone
5794 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005795
5796 // Figure out if we need to update the pivot point
5797 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005798 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005799 mPrevWidth = mRight - mLeft;
5800 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005801 mPivotX = mPrevWidth / 2f;
5802 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005803 }
5804 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005805 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005806 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5807 mMatrix.setTranslate(mTranslationX, mTranslationY);
5808 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5809 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5810 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005811 if (mCamera == null) {
5812 mCamera = new Camera();
5813 matrix3D = new Matrix();
5814 }
5815 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005816 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08005817 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005818 mCamera.getMatrix(matrix3D);
5819 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005820 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005821 mMatrix.postConcat(matrix3D);
5822 mCamera.restore();
5823 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005824 mMatrixDirty = false;
5825 mMatrixIsIdentity = mMatrix.isIdentity();
5826 mInverseMatrixDirty = true;
5827 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005828 }
5829
5830 /**
5831 * Utility method to retrieve the inverse of the current mMatrix property.
5832 * We cache the matrix to avoid recalculating it when transform properties
5833 * have not changed.
5834 *
5835 * @return The inverse of the current matrix of this view.
5836 */
Jeff Brown86671742010-09-30 20:00:15 -07005837 final Matrix getInverseMatrix() {
5838 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005839 if (mInverseMatrixDirty) {
5840 if (mInverseMatrix == null) {
5841 mInverseMatrix = new Matrix();
5842 }
5843 mMatrix.invert(mInverseMatrix);
5844 mInverseMatrixDirty = false;
5845 }
5846 return mInverseMatrix;
5847 }
5848
5849 /**
Romain Guya5364ee2011-02-24 14:46:04 -08005850 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
5851 * views are drawn) from the camera to this view. The camera's distance
5852 * affects 3D transformations, for instance rotations around the X and Y
5853 * axis. If the rotationX or rotationY properties are changed and this view is
5854 * large (more than half the size of the screen), it is recommended to always
5855 * use a camera distance that's greater than the height (X axis rotation) or
5856 * the width (Y axis rotation) of this view.</p>
5857 *
5858 * <p>The distance of the camera from the view plane can have an affect on the
5859 * perspective distortion of the view when it is rotated around the x or y axis.
5860 * For example, a large distance will result in a large viewing angle, and there
5861 * will not be much perspective distortion of the view as it rotates. A short
5862 * distance may cause much more perspective distortion upon rotation, and can
5863 * also result in some drawing artifacts if the rotated view ends up partially
5864 * behind the camera (which is why the recommendation is to use a distance at
5865 * least as far as the size of the view, if the view is to be rotated.)</p>
5866 *
5867 * <p>The distance is expressed in "depth pixels." The default distance depends
5868 * on the screen density. For instance, on a medium density display, the
5869 * default distance is 1280. On a high density display, the default distance
5870 * is 1920.</p>
5871 *
5872 * <p>If you want to specify a distance that leads to visually consistent
5873 * results across various densities, use the following formula:</p>
5874 * <pre>
5875 * float scale = context.getResources().getDisplayMetrics().density;
5876 * view.setCameraDistance(distance * scale);
5877 * </pre>
5878 *
5879 * <p>The density scale factor of a high density display is 1.5,
5880 * and 1920 = 1280 * 1.5.</p>
5881 *
5882 * @param distance The distance in "depth pixels", if negative the opposite
5883 * value is used
5884 *
5885 * @see #setRotationX(float)
5886 * @see #setRotationY(float)
5887 */
5888 public void setCameraDistance(float distance) {
5889 invalidateParentCaches();
5890 invalidate(false);
5891
5892 final float dpi = mResources.getDisplayMetrics().densityDpi;
5893 if (mCamera == null) {
5894 mCamera = new Camera();
5895 matrix3D = new Matrix();
5896 }
5897
5898 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
5899 mMatrixDirty = true;
5900
5901 invalidate(false);
5902 }
5903
5904 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005905 * The degrees that the view is rotated around the pivot point.
5906 *
Romain Guya5364ee2011-02-24 14:46:04 -08005907 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07005908 * @see #getPivotX()
5909 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005910 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005911 * @return The degrees of rotation.
5912 */
5913 public float getRotation() {
5914 return mRotation;
5915 }
5916
5917 /**
Chet Haase897247b2010-09-09 14:54:47 -07005918 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5919 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005920 *
5921 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005922 *
5923 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07005924 * @see #getPivotX()
5925 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005926 * @see #setRotationX(float)
5927 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08005928 *
5929 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005930 */
5931 public void setRotation(float rotation) {
5932 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005933 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005934 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005935 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005936 mRotation = rotation;
5937 mMatrixDirty = true;
5938 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005939 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005940 }
5941 }
5942
5943 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005944 * The degrees that the view is rotated around the vertical axis through the pivot point.
5945 *
5946 * @see #getPivotX()
5947 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005948 * @see #setRotationY(float)
5949 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005950 * @return The degrees of Y rotation.
5951 */
5952 public float getRotationY() {
5953 return mRotationY;
5954 }
5955
5956 /**
Chet Haase897247b2010-09-09 14:54:47 -07005957 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5958 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5959 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08005960 *
5961 * When rotating large views, it is recommended to adjust the camera distance
5962 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07005963 *
5964 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005965 *
5966 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07005967 * @see #getPivotX()
5968 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005969 * @see #setRotation(float)
5970 * @see #setRotationX(float)
5971 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08005972 *
5973 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005974 */
5975 public void setRotationY(float rotationY) {
5976 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005977 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005978 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005979 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005980 mRotationY = rotationY;
5981 mMatrixDirty = true;
5982 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005983 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005984 }
5985 }
5986
5987 /**
5988 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5989 *
5990 * @see #getPivotX()
5991 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005992 * @see #setRotationX(float)
5993 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005994 * @return The degrees of X rotation.
5995 */
5996 public float getRotationX() {
5997 return mRotationX;
5998 }
5999
6000 /**
Chet Haase897247b2010-09-09 14:54:47 -07006001 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6002 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6003 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006004 *
6005 * When rotating large views, it is recommended to adjust the camera distance
6006 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006007 *
6008 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006009 *
6010 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006011 * @see #getPivotX()
6012 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006013 * @see #setRotation(float)
6014 * @see #setRotationY(float)
6015 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006016 *
6017 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006018 */
6019 public void setRotationX(float rotationX) {
6020 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006021 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006022 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006023 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006024 mRotationX = rotationX;
6025 mMatrixDirty = true;
6026 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006027 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006028 }
6029 }
6030
6031 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006032 * The amount that the view is scaled in x around the pivot point, as a proportion of
6033 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6034 *
Joe Onorato93162322010-09-16 15:42:01 -04006035 * <p>By default, this is 1.0f.
6036 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006037 * @see #getPivotX()
6038 * @see #getPivotY()
6039 * @return The scaling factor.
6040 */
6041 public float getScaleX() {
6042 return mScaleX;
6043 }
6044
6045 /**
6046 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6047 * the view's unscaled width. A value of 1 means that no scaling is applied.
6048 *
6049 * @param scaleX The scaling factor.
6050 * @see #getPivotX()
6051 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006052 *
6053 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006054 */
6055 public void setScaleX(float scaleX) {
6056 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006057 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006058 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006059 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006060 mScaleX = scaleX;
6061 mMatrixDirty = true;
6062 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006063 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006064 }
6065 }
6066
6067 /**
6068 * The amount that the view is scaled in y around the pivot point, as a proportion of
6069 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6070 *
Joe Onorato93162322010-09-16 15:42:01 -04006071 * <p>By default, this is 1.0f.
6072 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006073 * @see #getPivotX()
6074 * @see #getPivotY()
6075 * @return The scaling factor.
6076 */
6077 public float getScaleY() {
6078 return mScaleY;
6079 }
6080
6081 /**
6082 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6083 * the view's unscaled width. A value of 1 means that no scaling is applied.
6084 *
6085 * @param scaleY The scaling factor.
6086 * @see #getPivotX()
6087 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006088 *
6089 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006090 */
6091 public void setScaleY(float scaleY) {
6092 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006093 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006094 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006095 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006096 mScaleY = scaleY;
6097 mMatrixDirty = true;
6098 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006099 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006100 }
6101 }
6102
6103 /**
6104 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6105 * and {@link #setScaleX(float) scaled}.
6106 *
6107 * @see #getRotation()
6108 * @see #getScaleX()
6109 * @see #getScaleY()
6110 * @see #getPivotY()
6111 * @return The x location of the pivot point.
6112 */
6113 public float getPivotX() {
6114 return mPivotX;
6115 }
6116
6117 /**
6118 * Sets the x location of the point around which the view is
6119 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006120 * By default, the pivot point is centered on the object.
6121 * Setting this property disables this behavior and causes the view to use only the
6122 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006123 *
6124 * @param pivotX The x location of the pivot point.
6125 * @see #getRotation()
6126 * @see #getScaleX()
6127 * @see #getScaleY()
6128 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006129 *
6130 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006131 */
6132 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006133 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006134 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006135 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006136 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006137 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006138 mPivotX = pivotX;
6139 mMatrixDirty = true;
6140 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006141 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006142 }
6143 }
6144
6145 /**
6146 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6147 * and {@link #setScaleY(float) scaled}.
6148 *
6149 * @see #getRotation()
6150 * @see #getScaleX()
6151 * @see #getScaleY()
6152 * @see #getPivotY()
6153 * @return The y location of the pivot point.
6154 */
6155 public float getPivotY() {
6156 return mPivotY;
6157 }
6158
6159 /**
6160 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006161 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6162 * Setting this property disables this behavior and causes the view to use only the
6163 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006164 *
6165 * @param pivotY The y location of the pivot point.
6166 * @see #getRotation()
6167 * @see #getScaleX()
6168 * @see #getScaleY()
6169 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006170 *
6171 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006172 */
6173 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006174 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006175 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006176 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006177 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006178 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006179 mPivotY = pivotY;
6180 mMatrixDirty = true;
6181 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006182 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006183 }
6184 }
6185
6186 /**
6187 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6188 * completely transparent and 1 means the view is completely opaque.
6189 *
Joe Onorato93162322010-09-16 15:42:01 -04006190 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006191 * @return The opacity of the view.
6192 */
6193 public float getAlpha() {
6194 return mAlpha;
6195 }
6196
6197 /**
Romain Guy171c5922011-01-06 10:04:23 -08006198 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6199 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006200 *
Romain Guy171c5922011-01-06 10:04:23 -08006201 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6202 * responsible for applying the opacity itself. Otherwise, calling this method is
6203 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006204 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006205 *
6206 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006207 *
Joe Malin32736f02011-01-19 16:14:20 -08006208 * @see #setLayerType(int, android.graphics.Paint)
6209 *
Chet Haase73066682010-11-29 15:55:32 -08006210 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006211 */
6212 public void setAlpha(float alpha) {
6213 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006214 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006215 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006216 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006217 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006218 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006219 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006220 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006221 invalidate(false);
6222 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006223 }
6224
6225 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006226 * Faster version of setAlpha() which performs the same steps except there are
6227 * no calls to invalidate(). The caller of this function should perform proper invalidation
6228 * on the parent and this object. The return value indicates whether the subclass handles
6229 * alpha (the return value for onSetAlpha()).
6230 *
6231 * @param alpha The new value for the alpha property
6232 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6233 */
6234 boolean setAlphaNoInvalidation(float alpha) {
6235 mAlpha = alpha;
6236 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6237 if (subclassHandlesAlpha) {
6238 mPrivateFlags |= ALPHA_SET;
6239 } else {
6240 mPrivateFlags &= ~ALPHA_SET;
6241 }
6242 return subclassHandlesAlpha;
6243 }
6244
6245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006246 * Top position of this view relative to its parent.
6247 *
6248 * @return The top of this view, in pixels.
6249 */
6250 @ViewDebug.CapturedViewProperty
6251 public final int getTop() {
6252 return mTop;
6253 }
6254
6255 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006256 * Sets the top position of this view relative to its parent. This method is meant to be called
6257 * by the layout system and should not generally be called otherwise, because the property
6258 * may be changed at any time by the layout.
6259 *
6260 * @param top The top of this view, in pixels.
6261 */
6262 public final void setTop(int top) {
6263 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006264 updateMatrix();
6265 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006266 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006267 int minTop;
6268 int yLoc;
6269 if (top < mTop) {
6270 minTop = top;
6271 yLoc = top - mTop;
6272 } else {
6273 minTop = mTop;
6274 yLoc = 0;
6275 }
Chet Haasee9140a72011-02-16 16:23:29 -08006276 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006277 }
6278 } else {
6279 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006280 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006281 }
6282
Chet Haaseed032702010-10-01 14:05:54 -07006283 int width = mRight - mLeft;
6284 int oldHeight = mBottom - mTop;
6285
Chet Haase21cd1382010-09-01 17:42:29 -07006286 mTop = top;
6287
Chet Haaseed032702010-10-01 14:05:54 -07006288 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6289
Chet Haase21cd1382010-09-01 17:42:29 -07006290 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006291 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6292 // A change in dimension means an auto-centered pivot point changes, too
6293 mMatrixDirty = true;
6294 }
Chet Haase21cd1382010-09-01 17:42:29 -07006295 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006296 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006297 }
Chet Haase55dbb652010-12-21 20:15:08 -08006298 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006299 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006300 }
6301 }
6302
6303 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006304 * Bottom position of this view relative to its parent.
6305 *
6306 * @return The bottom of this view, in pixels.
6307 */
6308 @ViewDebug.CapturedViewProperty
6309 public final int getBottom() {
6310 return mBottom;
6311 }
6312
6313 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006314 * True if this view has changed since the last time being drawn.
6315 *
6316 * @return The dirty state of this view.
6317 */
6318 public boolean isDirty() {
6319 return (mPrivateFlags & DIRTY_MASK) != 0;
6320 }
6321
6322 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006323 * Sets the bottom position of this view relative to its parent. This method is meant to be
6324 * called by the layout system and should not generally be called otherwise, because the
6325 * property may be changed at any time by the layout.
6326 *
6327 * @param bottom The bottom of this view, in pixels.
6328 */
6329 public final void setBottom(int bottom) {
6330 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006331 updateMatrix();
6332 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006333 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006334 int maxBottom;
6335 if (bottom < mBottom) {
6336 maxBottom = mBottom;
6337 } else {
6338 maxBottom = bottom;
6339 }
Chet Haasee9140a72011-02-16 16:23:29 -08006340 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006341 }
6342 } else {
6343 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006344 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006345 }
6346
Chet Haaseed032702010-10-01 14:05:54 -07006347 int width = mRight - mLeft;
6348 int oldHeight = mBottom - mTop;
6349
Chet Haase21cd1382010-09-01 17:42:29 -07006350 mBottom = bottom;
6351
Chet Haaseed032702010-10-01 14:05:54 -07006352 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6353
Chet Haase21cd1382010-09-01 17:42:29 -07006354 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006355 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6356 // A change in dimension means an auto-centered pivot point changes, too
6357 mMatrixDirty = true;
6358 }
Chet Haase21cd1382010-09-01 17:42:29 -07006359 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006360 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006361 }
Chet Haase55dbb652010-12-21 20:15:08 -08006362 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006363 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006364 }
6365 }
6366
6367 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 * Left position of this view relative to its parent.
6369 *
6370 * @return The left edge of this view, in pixels.
6371 */
6372 @ViewDebug.CapturedViewProperty
6373 public final int getLeft() {
6374 return mLeft;
6375 }
6376
6377 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006378 * Sets the left position of this view relative to its parent. This method is meant to be called
6379 * by the layout system and should not generally be called otherwise, because the property
6380 * may be changed at any time by the layout.
6381 *
6382 * @param left The bottom of this view, in pixels.
6383 */
6384 public final void setLeft(int left) {
6385 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006386 updateMatrix();
6387 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006388 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006389 int minLeft;
6390 int xLoc;
6391 if (left < mLeft) {
6392 minLeft = left;
6393 xLoc = left - mLeft;
6394 } else {
6395 minLeft = mLeft;
6396 xLoc = 0;
6397 }
Chet Haasee9140a72011-02-16 16:23:29 -08006398 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006399 }
6400 } else {
6401 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006402 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006403 }
6404
Chet Haaseed032702010-10-01 14:05:54 -07006405 int oldWidth = mRight - mLeft;
6406 int height = mBottom - mTop;
6407
Chet Haase21cd1382010-09-01 17:42:29 -07006408 mLeft = left;
6409
Chet Haaseed032702010-10-01 14:05:54 -07006410 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6411
Chet Haase21cd1382010-09-01 17:42:29 -07006412 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006413 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6414 // A change in dimension means an auto-centered pivot point changes, too
6415 mMatrixDirty = true;
6416 }
Chet Haase21cd1382010-09-01 17:42:29 -07006417 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006418 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006419 }
Chet Haase55dbb652010-12-21 20:15:08 -08006420 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006421 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006422 }
6423 }
6424
6425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006426 * Right position of this view relative to its parent.
6427 *
6428 * @return The right edge of this view, in pixels.
6429 */
6430 @ViewDebug.CapturedViewProperty
6431 public final int getRight() {
6432 return mRight;
6433 }
6434
6435 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006436 * Sets the right position of this view relative to its parent. This method is meant to be called
6437 * by the layout system and should not generally be called otherwise, because the property
6438 * may be changed at any time by the layout.
6439 *
6440 * @param right The bottom of this view, in pixels.
6441 */
6442 public final void setRight(int right) {
6443 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006444 updateMatrix();
6445 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006446 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006447 int maxRight;
6448 if (right < mRight) {
6449 maxRight = mRight;
6450 } else {
6451 maxRight = right;
6452 }
Chet Haasee9140a72011-02-16 16:23:29 -08006453 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006454 }
6455 } else {
6456 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006457 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006458 }
6459
Chet Haaseed032702010-10-01 14:05:54 -07006460 int oldWidth = mRight - mLeft;
6461 int height = mBottom - mTop;
6462
Chet Haase21cd1382010-09-01 17:42:29 -07006463 mRight = right;
6464
Chet Haaseed032702010-10-01 14:05:54 -07006465 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6466
Chet Haase21cd1382010-09-01 17:42:29 -07006467 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006468 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6469 // A change in dimension means an auto-centered pivot point changes, too
6470 mMatrixDirty = true;
6471 }
Chet Haase21cd1382010-09-01 17:42:29 -07006472 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006473 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006474 }
Chet Haase55dbb652010-12-21 20:15:08 -08006475 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006476 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006477 }
6478 }
6479
6480 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006481 * The visual x position of this view, in pixels. This is equivalent to the
6482 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006483 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006484 *
Chet Haasedf030d22010-07-30 17:22:38 -07006485 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006486 */
Chet Haasedf030d22010-07-30 17:22:38 -07006487 public float getX() {
6488 return mLeft + mTranslationX;
6489 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006490
Chet Haasedf030d22010-07-30 17:22:38 -07006491 /**
6492 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6493 * {@link #setTranslationX(float) translationX} property to be the difference between
6494 * the x value passed in and the current {@link #getLeft() left} property.
6495 *
6496 * @param x The visual x position of this view, in pixels.
6497 */
6498 public void setX(float x) {
6499 setTranslationX(x - mLeft);
6500 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006501
Chet Haasedf030d22010-07-30 17:22:38 -07006502 /**
6503 * The visual y position of this view, in pixels. This is equivalent to the
6504 * {@link #setTranslationY(float) translationY} property plus the current
6505 * {@link #getTop() top} property.
6506 *
6507 * @return The visual y position of this view, in pixels.
6508 */
6509 public float getY() {
6510 return mTop + mTranslationY;
6511 }
6512
6513 /**
6514 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6515 * {@link #setTranslationY(float) translationY} property to be the difference between
6516 * the y value passed in and the current {@link #getTop() top} property.
6517 *
6518 * @param y The visual y position of this view, in pixels.
6519 */
6520 public void setY(float y) {
6521 setTranslationY(y - mTop);
6522 }
6523
6524
6525 /**
6526 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6527 * This position is post-layout, in addition to wherever the object's
6528 * layout placed it.
6529 *
6530 * @return The horizontal position of this view relative to its left position, in pixels.
6531 */
6532 public float getTranslationX() {
6533 return mTranslationX;
6534 }
6535
6536 /**
6537 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6538 * This effectively positions the object post-layout, in addition to wherever the object's
6539 * layout placed it.
6540 *
6541 * @param translationX The horizontal position of this view relative to its left position,
6542 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006543 *
6544 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006545 */
6546 public void setTranslationX(float translationX) {
6547 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006548 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006549 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006550 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006551 mTranslationX = translationX;
6552 mMatrixDirty = true;
6553 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006554 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006555 }
6556 }
6557
6558 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006559 * The horizontal location of this view relative to its {@link #getTop() top} position.
6560 * This position is post-layout, in addition to wherever the object's
6561 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006562 *
Chet Haasedf030d22010-07-30 17:22:38 -07006563 * @return The vertical position of this view relative to its top position,
6564 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006565 */
Chet Haasedf030d22010-07-30 17:22:38 -07006566 public float getTranslationY() {
6567 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006568 }
6569
6570 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006571 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6572 * This effectively positions the object post-layout, in addition to wherever the object's
6573 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006574 *
Chet Haasedf030d22010-07-30 17:22:38 -07006575 * @param translationY The vertical position of this view relative to its top position,
6576 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006577 *
6578 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006579 */
Chet Haasedf030d22010-07-30 17:22:38 -07006580 public void setTranslationY(float translationY) {
6581 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006582 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006583 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006584 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006585 mTranslationY = translationY;
6586 mMatrixDirty = true;
6587 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006588 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006589 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 }
6591
6592 /**
Romain Guyda489792011-02-03 01:05:15 -08006593 * @hide
6594 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006595 public void setFastTranslationX(float x) {
6596 mTranslationX = x;
6597 mMatrixDirty = true;
6598 }
6599
6600 /**
6601 * @hide
6602 */
6603 public void setFastTranslationY(float y) {
6604 mTranslationY = y;
6605 mMatrixDirty = true;
6606 }
6607
6608 /**
6609 * @hide
6610 */
Romain Guyda489792011-02-03 01:05:15 -08006611 public void setFastX(float x) {
6612 mTranslationX = x - mLeft;
6613 mMatrixDirty = true;
6614 }
6615
6616 /**
6617 * @hide
6618 */
6619 public void setFastY(float y) {
6620 mTranslationY = y - mTop;
6621 mMatrixDirty = true;
6622 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006623
Romain Guyda489792011-02-03 01:05:15 -08006624 /**
6625 * @hide
6626 */
6627 public void setFastScaleX(float x) {
6628 mScaleX = x;
6629 mMatrixDirty = true;
6630 }
6631
6632 /**
6633 * @hide
6634 */
6635 public void setFastScaleY(float y) {
6636 mScaleY = y;
6637 mMatrixDirty = true;
6638 }
6639
6640 /**
6641 * @hide
6642 */
6643 public void setFastAlpha(float alpha) {
6644 mAlpha = alpha;
6645 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006646
Romain Guyda489792011-02-03 01:05:15 -08006647 /**
6648 * @hide
6649 */
6650 public void setFastRotationY(float y) {
6651 mRotationY = y;
6652 mMatrixDirty = true;
6653 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006654
Romain Guyda489792011-02-03 01:05:15 -08006655 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 * Hit rectangle in parent's coordinates
6657 *
6658 * @param outRect The hit rectangle of the view.
6659 */
6660 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006661 updateMatrix();
6662 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006663 outRect.set(mLeft, mTop, mRight, mBottom);
6664 } else {
6665 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006666 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006667 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006668 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6669 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006670 }
6671 }
6672
6673 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006674 * Determines whether the given point, in local coordinates is inside the view.
6675 */
6676 /*package*/ final boolean pointInView(float localX, float localY) {
6677 return localX >= 0 && localX < (mRight - mLeft)
6678 && localY >= 0 && localY < (mBottom - mTop);
6679 }
6680
6681 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006682 * Utility method to determine whether the given point, in local coordinates,
6683 * is inside the view, where the area of the view is expanded by the slop factor.
6684 * This method is called while processing touch-move events to determine if the event
6685 * is still within the view.
6686 */
6687 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006688 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006689 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006690 }
6691
6692 /**
6693 * When a view has focus and the user navigates away from it, the next view is searched for
6694 * starting from the rectangle filled in by this method.
6695 *
6696 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6697 * view maintains some idea of internal selection, such as a cursor, or a selected row
6698 * or column, you should override this method and fill in a more specific rectangle.
6699 *
6700 * @param r The rectangle to fill in, in this view's coordinates.
6701 */
6702 public void getFocusedRect(Rect r) {
6703 getDrawingRect(r);
6704 }
6705
6706 /**
6707 * If some part of this view is not clipped by any of its parents, then
6708 * return that area in r in global (root) coordinates. To convert r to local
6709 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6710 * -globalOffset.y)) If the view is completely clipped or translated out,
6711 * return false.
6712 *
6713 * @param r If true is returned, r holds the global coordinates of the
6714 * visible portion of this view.
6715 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6716 * between this view and its root. globalOffet may be null.
6717 * @return true if r is non-empty (i.e. part of the view is visible at the
6718 * root level.
6719 */
6720 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6721 int width = mRight - mLeft;
6722 int height = mBottom - mTop;
6723 if (width > 0 && height > 0) {
6724 r.set(0, 0, width, height);
6725 if (globalOffset != null) {
6726 globalOffset.set(-mScrollX, -mScrollY);
6727 }
6728 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6729 }
6730 return false;
6731 }
6732
6733 public final boolean getGlobalVisibleRect(Rect r) {
6734 return getGlobalVisibleRect(r, null);
6735 }
6736
6737 public final boolean getLocalVisibleRect(Rect r) {
6738 Point offset = new Point();
6739 if (getGlobalVisibleRect(r, offset)) {
6740 r.offset(-offset.x, -offset.y); // make r local
6741 return true;
6742 }
6743 return false;
6744 }
6745
6746 /**
6747 * Offset this view's vertical location by the specified number of pixels.
6748 *
6749 * @param offset the number of pixels to offset the view by
6750 */
6751 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006752 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006753 updateMatrix();
6754 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006755 final ViewParent p = mParent;
6756 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006757 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006758 int minTop;
6759 int maxBottom;
6760 int yLoc;
6761 if (offset < 0) {
6762 minTop = mTop + offset;
6763 maxBottom = mBottom;
6764 yLoc = offset;
6765 } else {
6766 minTop = mTop;
6767 maxBottom = mBottom + offset;
6768 yLoc = 0;
6769 }
6770 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6771 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006772 }
6773 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006774 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006775 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006776
Chet Haasec3aa3612010-06-17 08:50:37 -07006777 mTop += offset;
6778 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006779
Chet Haasec3aa3612010-06-17 08:50:37 -07006780 if (!mMatrixIsIdentity) {
6781 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006782 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006783 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006784 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006785 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006786 }
6787
6788 /**
6789 * Offset this view's horizontal location by the specified amount of pixels.
6790 *
6791 * @param offset the numer of pixels to offset the view by
6792 */
6793 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006794 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006795 updateMatrix();
6796 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006797 final ViewParent p = mParent;
6798 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006799 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006800 int minLeft;
6801 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006802 if (offset < 0) {
6803 minLeft = mLeft + offset;
6804 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006805 } else {
6806 minLeft = mLeft;
6807 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006808 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006809 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006810 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006811 }
6812 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006813 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006814 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006815
Chet Haasec3aa3612010-06-17 08:50:37 -07006816 mLeft += offset;
6817 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006818
Chet Haasec3aa3612010-06-17 08:50:37 -07006819 if (!mMatrixIsIdentity) {
6820 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006821 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006822 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006823 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006825 }
6826
6827 /**
6828 * Get the LayoutParams associated with this view. All views should have
6829 * layout parameters. These supply parameters to the <i>parent</i> of this
6830 * view specifying how it should be arranged. There are many subclasses of
6831 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6832 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006833 *
6834 * This method may return null if this View is not attached to a parent
6835 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6836 * was not invoked successfully. When a View is attached to a parent
6837 * ViewGroup, this method must not return null.
6838 *
6839 * @return The LayoutParams associated with this view, or null if no
6840 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006841 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006842 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006843 public ViewGroup.LayoutParams getLayoutParams() {
6844 return mLayoutParams;
6845 }
6846
6847 /**
6848 * Set the layout parameters associated with this view. These supply
6849 * parameters to the <i>parent</i> of this view specifying how it should be
6850 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6851 * correspond to the different subclasses of ViewGroup that are responsible
6852 * for arranging their children.
6853 *
Romain Guy01c174b2011-02-22 11:51:06 -08006854 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006855 */
6856 public void setLayoutParams(ViewGroup.LayoutParams params) {
6857 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08006858 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006859 }
6860 mLayoutParams = params;
6861 requestLayout();
6862 }
6863
6864 /**
6865 * Set the scrolled position of your view. This will cause a call to
6866 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6867 * invalidated.
6868 * @param x the x position to scroll to
6869 * @param y the y position to scroll to
6870 */
6871 public void scrollTo(int x, int y) {
6872 if (mScrollX != x || mScrollY != y) {
6873 int oldX = mScrollX;
6874 int oldY = mScrollY;
6875 mScrollX = x;
6876 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006877 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006878 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006879 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006880 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 }
6883 }
6884
6885 /**
6886 * Move the scrolled position of your view. This will cause a call to
6887 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6888 * invalidated.
6889 * @param x the amount of pixels to scroll by horizontally
6890 * @param y the amount of pixels to scroll by vertically
6891 */
6892 public void scrollBy(int x, int y) {
6893 scrollTo(mScrollX + x, mScrollY + y);
6894 }
6895
6896 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006897 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6898 * animation to fade the scrollbars out after a default delay. If a subclass
6899 * provides animated scrolling, the start delay should equal the duration
6900 * of the scrolling animation.</p>
6901 *
6902 * <p>The animation starts only if at least one of the scrollbars is
6903 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6904 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6905 * this method returns true, and false otherwise. If the animation is
6906 * started, this method calls {@link #invalidate()}; in that case the
6907 * caller should not call {@link #invalidate()}.</p>
6908 *
6909 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006910 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006911 *
6912 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6913 * and {@link #scrollTo(int, int)}.</p>
6914 *
6915 * @return true if the animation is played, false otherwise
6916 *
6917 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006918 * @see #scrollBy(int, int)
6919 * @see #scrollTo(int, int)
6920 * @see #isHorizontalScrollBarEnabled()
6921 * @see #isVerticalScrollBarEnabled()
6922 * @see #setHorizontalScrollBarEnabled(boolean)
6923 * @see #setVerticalScrollBarEnabled(boolean)
6924 */
6925 protected boolean awakenScrollBars() {
6926 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006927 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006928 }
6929
6930 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006931 * Trigger the scrollbars to draw.
6932 * This method differs from awakenScrollBars() only in its default duration.
6933 * initialAwakenScrollBars() will show the scroll bars for longer than
6934 * usual to give the user more of a chance to notice them.
6935 *
6936 * @return true if the animation is played, false otherwise.
6937 */
6938 private boolean initialAwakenScrollBars() {
6939 return mScrollCache != null &&
6940 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6941 }
6942
6943 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006944 * <p>
6945 * Trigger the scrollbars to draw. When invoked this method starts an
6946 * animation to fade the scrollbars out after a fixed delay. If a subclass
6947 * provides animated scrolling, the start delay should equal the duration of
6948 * the scrolling animation.
6949 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006950 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006951 * <p>
6952 * The animation starts only if at least one of the scrollbars is enabled,
6953 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6954 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6955 * this method returns true, and false otherwise. If the animation is
6956 * started, this method calls {@link #invalidate()}; in that case the caller
6957 * should not call {@link #invalidate()}.
6958 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006959 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006960 * <p>
6961 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006962 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006963 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006964 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006965 * @param startDelay the delay, in milliseconds, after which the animation
6966 * should start; when the delay is 0, the animation starts
6967 * immediately
6968 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006969 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006970 * @see #scrollBy(int, int)
6971 * @see #scrollTo(int, int)
6972 * @see #isHorizontalScrollBarEnabled()
6973 * @see #isVerticalScrollBarEnabled()
6974 * @see #setHorizontalScrollBarEnabled(boolean)
6975 * @see #setVerticalScrollBarEnabled(boolean)
6976 */
6977 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006978 return awakenScrollBars(startDelay, true);
6979 }
Joe Malin32736f02011-01-19 16:14:20 -08006980
Mike Cleron290947b2009-09-29 18:34:32 -07006981 /**
6982 * <p>
6983 * Trigger the scrollbars to draw. When invoked this method starts an
6984 * animation to fade the scrollbars out after a fixed delay. If a subclass
6985 * provides animated scrolling, the start delay should equal the duration of
6986 * the scrolling animation.
6987 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006988 *
Mike Cleron290947b2009-09-29 18:34:32 -07006989 * <p>
6990 * The animation starts only if at least one of the scrollbars is enabled,
6991 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6992 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6993 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006994 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006995 * is set to true; in that case the caller
6996 * should not call {@link #invalidate()}.
6997 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006998 *
Mike Cleron290947b2009-09-29 18:34:32 -07006999 * <p>
7000 * This method should be invoked everytime a subclass directly updates the
7001 * scroll parameters.
7002 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007003 *
Mike Cleron290947b2009-09-29 18:34:32 -07007004 * @param startDelay the delay, in milliseconds, after which the animation
7005 * should start; when the delay is 0, the animation starts
7006 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007007 *
Mike Cleron290947b2009-09-29 18:34:32 -07007008 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007009 *
Mike Cleron290947b2009-09-29 18:34:32 -07007010 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007011 *
Mike Cleron290947b2009-09-29 18:34:32 -07007012 * @see #scrollBy(int, int)
7013 * @see #scrollTo(int, int)
7014 * @see #isHorizontalScrollBarEnabled()
7015 * @see #isVerticalScrollBarEnabled()
7016 * @see #setHorizontalScrollBarEnabled(boolean)
7017 * @see #setVerticalScrollBarEnabled(boolean)
7018 */
7019 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007020 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007021
Mike Cleronf116bf82009-09-27 19:14:12 -07007022 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7023 return false;
7024 }
7025
7026 if (scrollCache.scrollBar == null) {
7027 scrollCache.scrollBar = new ScrollBarDrawable();
7028 }
7029
7030 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7031
Mike Cleron290947b2009-09-29 18:34:32 -07007032 if (invalidate) {
7033 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007034 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007035 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007036
7037 if (scrollCache.state == ScrollabilityCache.OFF) {
7038 // FIXME: this is copied from WindowManagerService.
7039 // We should get this value from the system when it
7040 // is possible to do so.
7041 final int KEY_REPEAT_FIRST_DELAY = 750;
7042 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7043 }
7044
7045 // Tell mScrollCache when we should start fading. This may
7046 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007047 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007048 scrollCache.fadeStartTime = fadeStartTime;
7049 scrollCache.state = ScrollabilityCache.ON;
7050
7051 // Schedule our fader to run, unscheduling any old ones first
7052 if (mAttachInfo != null) {
7053 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7054 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7055 }
7056
7057 return true;
7058 }
7059
7060 return false;
7061 }
7062
7063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007064 * Mark the the area defined by dirty as needing to be drawn. If the view is
7065 * visible, {@link #onDraw} will be called at some point in the future.
7066 * This must be called from a UI thread. To call from a non-UI thread, call
7067 * {@link #postInvalidate()}.
7068 *
7069 * WARNING: This method is destructive to dirty.
7070 * @param dirty the rectangle representing the bounds of the dirty region
7071 */
7072 public void invalidate(Rect dirty) {
7073 if (ViewDebug.TRACE_HIERARCHY) {
7074 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7075 }
7076
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007077 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007078 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7079 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007080 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007081 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007082 final ViewParent p = mParent;
7083 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007084 //noinspection PointlessBooleanExpression,ConstantConditions
7085 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7086 if (p != null && ai != null && ai.mHardwareAccelerated) {
7087 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7088 // with a null dirty rect, which tells the ViewRoot to redraw everything
7089 p.invalidateChild(this, null);
7090 return;
7091 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007093 if (p != null && ai != null) {
7094 final int scrollX = mScrollX;
7095 final int scrollY = mScrollY;
7096 final Rect r = ai.mTmpInvalRect;
7097 r.set(dirty.left - scrollX, dirty.top - scrollY,
7098 dirty.right - scrollX, dirty.bottom - scrollY);
7099 mParent.invalidateChild(this, r);
7100 }
7101 }
7102 }
7103
7104 /**
7105 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7106 * The coordinates of the dirty rect are relative to the view.
7107 * If the view is visible, {@link #onDraw} will be called at some point
7108 * in the future. This must be called from a UI thread. To call
7109 * from a non-UI thread, call {@link #postInvalidate()}.
7110 * @param l the left position of the dirty region
7111 * @param t the top position of the dirty region
7112 * @param r the right position of the dirty region
7113 * @param b the bottom position of the dirty region
7114 */
7115 public void invalidate(int l, int t, int r, int b) {
7116 if (ViewDebug.TRACE_HIERARCHY) {
7117 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7118 }
7119
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007120 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007121 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7122 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007124 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007125 final ViewParent p = mParent;
7126 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007127 //noinspection PointlessBooleanExpression,ConstantConditions
7128 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7129 if (p != null && ai != null && ai.mHardwareAccelerated) {
7130 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7131 // with a null dirty rect, which tells the ViewRoot to redraw everything
7132 p.invalidateChild(this, null);
7133 return;
7134 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007135 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007136 if (p != null && ai != null && l < r && t < b) {
7137 final int scrollX = mScrollX;
7138 final int scrollY = mScrollY;
7139 final Rect tmpr = ai.mTmpInvalRect;
7140 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7141 p.invalidateChild(this, tmpr);
7142 }
7143 }
7144 }
7145
7146 /**
7147 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7148 * be called at some point in the future. This must be called from a
7149 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7150 */
7151 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007152 invalidate(true);
7153 }
Joe Malin32736f02011-01-19 16:14:20 -08007154
Chet Haaseed032702010-10-01 14:05:54 -07007155 /**
7156 * This is where the invalidate() work actually happens. A full invalidate()
7157 * causes the drawing cache to be invalidated, but this function can be called with
7158 * invalidateCache set to false to skip that invalidation step for cases that do not
7159 * need it (for example, a component that remains at the same dimensions with the same
7160 * content).
7161 *
7162 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7163 * well. This is usually true for a full invalidate, but may be set to false if the
7164 * View's contents or dimensions have not changed.
7165 */
Romain Guy849d0a32011-02-01 17:20:48 -08007166 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007167 if (ViewDebug.TRACE_HIERARCHY) {
7168 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7169 }
7170
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007171 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007172 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007173 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7174 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007175 mPrivateFlags &= ~DRAWN;
7176 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007177 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007178 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7179 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007180 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007181 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007182 //noinspection PointlessBooleanExpression,ConstantConditions
7183 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7184 if (p != null && ai != null && ai.mHardwareAccelerated) {
7185 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7186 // with a null dirty rect, which tells the ViewRoot to redraw everything
7187 p.invalidateChild(this, null);
7188 return;
7189 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007190 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007192 if (p != null && ai != null) {
7193 final Rect r = ai.mTmpInvalRect;
7194 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7195 // Don't call invalidate -- we don't want to internally scroll
7196 // our own bounds
7197 p.invalidateChild(this, r);
7198 }
7199 }
7200 }
7201
7202 /**
Romain Guyda489792011-02-03 01:05:15 -08007203 * @hide
7204 */
7205 public void fastInvalidate() {
7206 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7207 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7208 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7209 if (mParent instanceof View) {
7210 ((View) mParent).mPrivateFlags |= INVALIDATED;
7211 }
7212 mPrivateFlags &= ~DRAWN;
7213 mPrivateFlags |= INVALIDATED;
7214 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7215 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7216 mParent.invalidateChild(this, null);
7217 }
7218 }
7219 }
7220
7221 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007222 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007223 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7224 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007225 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7226 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007227 *
7228 * @hide
7229 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007230 protected void invalidateParentCaches() {
7231 if (mParent instanceof View) {
7232 ((View) mParent).mPrivateFlags |= INVALIDATED;
7233 }
7234 }
Joe Malin32736f02011-01-19 16:14:20 -08007235
Romain Guy0fd89bf2011-01-26 15:41:30 -08007236 /**
7237 * Used to indicate that the parent of this view should be invalidated. This functionality
7238 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7239 * which is necessary when various parent-managed properties of the view change, such as
7240 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7241 * an invalidation event to the parent.
7242 *
7243 * @hide
7244 */
7245 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007246 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007247 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007248 }
7249 }
7250
7251 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007252 * Indicates whether this View is opaque. An opaque View guarantees that it will
7253 * draw all the pixels overlapping its bounds using a fully opaque color.
7254 *
7255 * Subclasses of View should override this method whenever possible to indicate
7256 * whether an instance is opaque. Opaque Views are treated in a special way by
7257 * the View hierarchy, possibly allowing it to perform optimizations during
7258 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007259 *
Romain Guy24443ea2009-05-11 11:56:30 -07007260 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007261 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007262 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007263 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007264 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7265 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007266 }
7267
Adam Powell20232d02010-12-08 21:08:53 -08007268 /**
7269 * @hide
7270 */
7271 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007272 // Opaque if:
7273 // - Has a background
7274 // - Background is opaque
7275 // - Doesn't have scrollbars or scrollbars are inside overlay
7276
7277 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7278 mPrivateFlags |= OPAQUE_BACKGROUND;
7279 } else {
7280 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7281 }
7282
7283 final int flags = mViewFlags;
7284 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7285 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7286 mPrivateFlags |= OPAQUE_SCROLLBARS;
7287 } else {
7288 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7289 }
7290 }
7291
7292 /**
7293 * @hide
7294 */
7295 protected boolean hasOpaqueScrollbars() {
7296 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007297 }
7298
7299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007300 * @return A handler associated with the thread running the View. This
7301 * handler can be used to pump events in the UI events queue.
7302 */
7303 public Handler getHandler() {
7304 if (mAttachInfo != null) {
7305 return mAttachInfo.mHandler;
7306 }
7307 return null;
7308 }
7309
7310 /**
7311 * Causes the Runnable to be added to the message queue.
7312 * The runnable will be run on the user interface thread.
7313 *
7314 * @param action The Runnable that will be executed.
7315 *
7316 * @return Returns true if the Runnable was successfully placed in to the
7317 * message queue. Returns false on failure, usually because the
7318 * looper processing the message queue is exiting.
7319 */
7320 public boolean post(Runnable action) {
7321 Handler handler;
7322 if (mAttachInfo != null) {
7323 handler = mAttachInfo.mHandler;
7324 } else {
7325 // Assume that post will succeed later
7326 ViewRoot.getRunQueue().post(action);
7327 return true;
7328 }
7329
7330 return handler.post(action);
7331 }
7332
7333 /**
7334 * Causes the Runnable to be added to the message queue, to be run
7335 * after the specified amount of time elapses.
7336 * The runnable will be run on the user interface thread.
7337 *
7338 * @param action The Runnable that will be executed.
7339 * @param delayMillis The delay (in milliseconds) until the Runnable
7340 * will be executed.
7341 *
7342 * @return true if the Runnable was successfully placed in to the
7343 * message queue. Returns false on failure, usually because the
7344 * looper processing the message queue is exiting. Note that a
7345 * result of true does not mean the Runnable will be processed --
7346 * if the looper is quit before the delivery time of the message
7347 * occurs then the message will be dropped.
7348 */
7349 public boolean postDelayed(Runnable action, long delayMillis) {
7350 Handler handler;
7351 if (mAttachInfo != null) {
7352 handler = mAttachInfo.mHandler;
7353 } else {
7354 // Assume that post will succeed later
7355 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7356 return true;
7357 }
7358
7359 return handler.postDelayed(action, delayMillis);
7360 }
7361
7362 /**
7363 * Removes the specified Runnable from the message queue.
7364 *
7365 * @param action The Runnable to remove from the message handling queue
7366 *
7367 * @return true if this view could ask the Handler to remove the Runnable,
7368 * false otherwise. When the returned value is true, the Runnable
7369 * may or may not have been actually removed from the message queue
7370 * (for instance, if the Runnable was not in the queue already.)
7371 */
7372 public boolean removeCallbacks(Runnable action) {
7373 Handler handler;
7374 if (mAttachInfo != null) {
7375 handler = mAttachInfo.mHandler;
7376 } else {
7377 // Assume that post will succeed later
7378 ViewRoot.getRunQueue().removeCallbacks(action);
7379 return true;
7380 }
7381
7382 handler.removeCallbacks(action);
7383 return true;
7384 }
7385
7386 /**
7387 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7388 * Use this to invalidate the View from a non-UI thread.
7389 *
7390 * @see #invalidate()
7391 */
7392 public void postInvalidate() {
7393 postInvalidateDelayed(0);
7394 }
7395
7396 /**
7397 * Cause an invalidate of the specified area to happen on a subsequent cycle
7398 * through the event loop. Use this to invalidate the View from a non-UI thread.
7399 *
7400 * @param left The left coordinate of the rectangle to invalidate.
7401 * @param top The top coordinate of the rectangle to invalidate.
7402 * @param right The right coordinate of the rectangle to invalidate.
7403 * @param bottom The bottom coordinate of the rectangle to invalidate.
7404 *
7405 * @see #invalidate(int, int, int, int)
7406 * @see #invalidate(Rect)
7407 */
7408 public void postInvalidate(int left, int top, int right, int bottom) {
7409 postInvalidateDelayed(0, left, top, right, bottom);
7410 }
7411
7412 /**
7413 * Cause an invalidate to happen on a subsequent cycle through the event
7414 * loop. Waits for the specified amount of time.
7415 *
7416 * @param delayMilliseconds the duration in milliseconds to delay the
7417 * invalidation by
7418 */
7419 public void postInvalidateDelayed(long delayMilliseconds) {
7420 // We try only with the AttachInfo because there's no point in invalidating
7421 // if we are not attached to our window
7422 if (mAttachInfo != null) {
7423 Message msg = Message.obtain();
7424 msg.what = AttachInfo.INVALIDATE_MSG;
7425 msg.obj = this;
7426 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7427 }
7428 }
7429
7430 /**
7431 * Cause an invalidate of the specified area to happen on a subsequent cycle
7432 * through the event loop. Waits for the specified amount of time.
7433 *
7434 * @param delayMilliseconds the duration in milliseconds to delay the
7435 * invalidation by
7436 * @param left The left coordinate of the rectangle to invalidate.
7437 * @param top The top coordinate of the rectangle to invalidate.
7438 * @param right The right coordinate of the rectangle to invalidate.
7439 * @param bottom The bottom coordinate of the rectangle to invalidate.
7440 */
7441 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7442 int right, int bottom) {
7443
7444 // We try only with the AttachInfo because there's no point in invalidating
7445 // if we are not attached to our window
7446 if (mAttachInfo != null) {
7447 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7448 info.target = this;
7449 info.left = left;
7450 info.top = top;
7451 info.right = right;
7452 info.bottom = bottom;
7453
7454 final Message msg = Message.obtain();
7455 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7456 msg.obj = info;
7457 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7458 }
7459 }
7460
7461 /**
7462 * Called by a parent to request that a child update its values for mScrollX
7463 * and mScrollY if necessary. This will typically be done if the child is
7464 * animating a scroll using a {@link android.widget.Scroller Scroller}
7465 * object.
7466 */
7467 public void computeScroll() {
7468 }
7469
7470 /**
7471 * <p>Indicate whether the horizontal edges are faded when the view is
7472 * scrolled horizontally.</p>
7473 *
7474 * @return true if the horizontal edges should are faded on scroll, false
7475 * otherwise
7476 *
7477 * @see #setHorizontalFadingEdgeEnabled(boolean)
7478 * @attr ref android.R.styleable#View_fadingEdge
7479 */
7480 public boolean isHorizontalFadingEdgeEnabled() {
7481 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7482 }
7483
7484 /**
7485 * <p>Define whether the horizontal edges should be faded when this view
7486 * is scrolled horizontally.</p>
7487 *
7488 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7489 * be faded when the view is scrolled
7490 * horizontally
7491 *
7492 * @see #isHorizontalFadingEdgeEnabled()
7493 * @attr ref android.R.styleable#View_fadingEdge
7494 */
7495 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7496 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7497 if (horizontalFadingEdgeEnabled) {
7498 initScrollCache();
7499 }
7500
7501 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7502 }
7503 }
7504
7505 /**
7506 * <p>Indicate whether the vertical edges are faded when the view is
7507 * scrolled horizontally.</p>
7508 *
7509 * @return true if the vertical edges should are faded on scroll, false
7510 * otherwise
7511 *
7512 * @see #setVerticalFadingEdgeEnabled(boolean)
7513 * @attr ref android.R.styleable#View_fadingEdge
7514 */
7515 public boolean isVerticalFadingEdgeEnabled() {
7516 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7517 }
7518
7519 /**
7520 * <p>Define whether the vertical edges should be faded when this view
7521 * is scrolled vertically.</p>
7522 *
7523 * @param verticalFadingEdgeEnabled true if the vertical edges should
7524 * be faded when the view is scrolled
7525 * vertically
7526 *
7527 * @see #isVerticalFadingEdgeEnabled()
7528 * @attr ref android.R.styleable#View_fadingEdge
7529 */
7530 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7531 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7532 if (verticalFadingEdgeEnabled) {
7533 initScrollCache();
7534 }
7535
7536 mViewFlags ^= FADING_EDGE_VERTICAL;
7537 }
7538 }
7539
7540 /**
7541 * Returns the strength, or intensity, of the top faded edge. The strength is
7542 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7543 * returns 0.0 or 1.0 but no value in between.
7544 *
7545 * Subclasses should override this method to provide a smoother fade transition
7546 * when scrolling occurs.
7547 *
7548 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7549 */
7550 protected float getTopFadingEdgeStrength() {
7551 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7552 }
7553
7554 /**
7555 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7556 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7557 * returns 0.0 or 1.0 but no value in between.
7558 *
7559 * Subclasses should override this method to provide a smoother fade transition
7560 * when scrolling occurs.
7561 *
7562 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7563 */
7564 protected float getBottomFadingEdgeStrength() {
7565 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7566 computeVerticalScrollRange() ? 1.0f : 0.0f;
7567 }
7568
7569 /**
7570 * Returns the strength, or intensity, of the left faded edge. The strength is
7571 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7572 * returns 0.0 or 1.0 but no value in between.
7573 *
7574 * Subclasses should override this method to provide a smoother fade transition
7575 * when scrolling occurs.
7576 *
7577 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7578 */
7579 protected float getLeftFadingEdgeStrength() {
7580 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7581 }
7582
7583 /**
7584 * Returns the strength, or intensity, of the right faded edge. The strength is
7585 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7586 * returns 0.0 or 1.0 but no value in between.
7587 *
7588 * Subclasses should override this method to provide a smoother fade transition
7589 * when scrolling occurs.
7590 *
7591 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7592 */
7593 protected float getRightFadingEdgeStrength() {
7594 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7595 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7596 }
7597
7598 /**
7599 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7600 * scrollbar is not drawn by default.</p>
7601 *
7602 * @return true if the horizontal scrollbar should be painted, false
7603 * otherwise
7604 *
7605 * @see #setHorizontalScrollBarEnabled(boolean)
7606 */
7607 public boolean isHorizontalScrollBarEnabled() {
7608 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7609 }
7610
7611 /**
7612 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7613 * scrollbar is not drawn by default.</p>
7614 *
7615 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7616 * be painted
7617 *
7618 * @see #isHorizontalScrollBarEnabled()
7619 */
7620 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7621 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7622 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007623 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007624 recomputePadding();
7625 }
7626 }
7627
7628 /**
7629 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7630 * scrollbar is not drawn by default.</p>
7631 *
7632 * @return true if the vertical scrollbar should be painted, false
7633 * otherwise
7634 *
7635 * @see #setVerticalScrollBarEnabled(boolean)
7636 */
7637 public boolean isVerticalScrollBarEnabled() {
7638 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7639 }
7640
7641 /**
7642 * <p>Define whether the vertical scrollbar should be drawn or not. The
7643 * scrollbar is not drawn by default.</p>
7644 *
7645 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7646 * be painted
7647 *
7648 * @see #isVerticalScrollBarEnabled()
7649 */
7650 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7651 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7652 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007653 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007654 recomputePadding();
7655 }
7656 }
7657
Adam Powell20232d02010-12-08 21:08:53 -08007658 /**
7659 * @hide
7660 */
7661 protected void recomputePadding() {
7662 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 }
Joe Malin32736f02011-01-19 16:14:20 -08007664
Mike Cleronfe81d382009-09-28 14:22:16 -07007665 /**
7666 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007667 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007668 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007669 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007670 */
7671 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7672 initScrollCache();
7673 final ScrollabilityCache scrollabilityCache = mScrollCache;
7674 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007675 if (fadeScrollbars) {
7676 scrollabilityCache.state = ScrollabilityCache.OFF;
7677 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007678 scrollabilityCache.state = ScrollabilityCache.ON;
7679 }
7680 }
Joe Malin32736f02011-01-19 16:14:20 -08007681
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007682 /**
Joe Malin32736f02011-01-19 16:14:20 -08007683 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007684 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007685 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007686 * @return true if scrollbar fading is enabled
7687 */
7688 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007689 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007690 }
Joe Malin32736f02011-01-19 16:14:20 -08007691
Mike Cleron52f0a642009-09-28 18:21:37 -07007692 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7694 * inset. When inset, they add to the padding of the view. And the scrollbars
7695 * can be drawn inside the padding area or on the edge of the view. For example,
7696 * if a view has a background drawable and you want to draw the scrollbars
7697 * inside the padding specified by the drawable, you can use
7698 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7699 * appear at the edge of the view, ignoring the padding, then you can use
7700 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7701 * @param style the style of the scrollbars. Should be one of
7702 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7703 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7704 * @see #SCROLLBARS_INSIDE_OVERLAY
7705 * @see #SCROLLBARS_INSIDE_INSET
7706 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7707 * @see #SCROLLBARS_OUTSIDE_INSET
7708 */
7709 public void setScrollBarStyle(int style) {
7710 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7711 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007712 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007713 recomputePadding();
7714 }
7715 }
7716
7717 /**
7718 * <p>Returns the current scrollbar style.</p>
7719 * @return the current scrollbar style
7720 * @see #SCROLLBARS_INSIDE_OVERLAY
7721 * @see #SCROLLBARS_INSIDE_INSET
7722 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7723 * @see #SCROLLBARS_OUTSIDE_INSET
7724 */
7725 public int getScrollBarStyle() {
7726 return mViewFlags & SCROLLBARS_STYLE_MASK;
7727 }
7728
7729 /**
7730 * <p>Compute the horizontal range that the horizontal scrollbar
7731 * represents.</p>
7732 *
7733 * <p>The range is expressed in arbitrary units that must be the same as the
7734 * units used by {@link #computeHorizontalScrollExtent()} and
7735 * {@link #computeHorizontalScrollOffset()}.</p>
7736 *
7737 * <p>The default range is the drawing width of this view.</p>
7738 *
7739 * @return the total horizontal range represented by the horizontal
7740 * scrollbar
7741 *
7742 * @see #computeHorizontalScrollExtent()
7743 * @see #computeHorizontalScrollOffset()
7744 * @see android.widget.ScrollBarDrawable
7745 */
7746 protected int computeHorizontalScrollRange() {
7747 return getWidth();
7748 }
7749
7750 /**
7751 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7752 * within the horizontal range. This value is used to compute the position
7753 * of the thumb within the scrollbar's track.</p>
7754 *
7755 * <p>The range is expressed in arbitrary units that must be the same as the
7756 * units used by {@link #computeHorizontalScrollRange()} and
7757 * {@link #computeHorizontalScrollExtent()}.</p>
7758 *
7759 * <p>The default offset is the scroll offset of this view.</p>
7760 *
7761 * @return the horizontal offset of the scrollbar's thumb
7762 *
7763 * @see #computeHorizontalScrollRange()
7764 * @see #computeHorizontalScrollExtent()
7765 * @see android.widget.ScrollBarDrawable
7766 */
7767 protected int computeHorizontalScrollOffset() {
7768 return mScrollX;
7769 }
7770
7771 /**
7772 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7773 * within the horizontal range. This value is used to compute the length
7774 * of the thumb within the scrollbar's track.</p>
7775 *
7776 * <p>The range is expressed in arbitrary units that must be the same as the
7777 * units used by {@link #computeHorizontalScrollRange()} and
7778 * {@link #computeHorizontalScrollOffset()}.</p>
7779 *
7780 * <p>The default extent is the drawing width of this view.</p>
7781 *
7782 * @return the horizontal extent of the scrollbar's thumb
7783 *
7784 * @see #computeHorizontalScrollRange()
7785 * @see #computeHorizontalScrollOffset()
7786 * @see android.widget.ScrollBarDrawable
7787 */
7788 protected int computeHorizontalScrollExtent() {
7789 return getWidth();
7790 }
7791
7792 /**
7793 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7794 *
7795 * <p>The range is expressed in arbitrary units that must be the same as the
7796 * units used by {@link #computeVerticalScrollExtent()} and
7797 * {@link #computeVerticalScrollOffset()}.</p>
7798 *
7799 * @return the total vertical range represented by the vertical scrollbar
7800 *
7801 * <p>The default range is the drawing height of this view.</p>
7802 *
7803 * @see #computeVerticalScrollExtent()
7804 * @see #computeVerticalScrollOffset()
7805 * @see android.widget.ScrollBarDrawable
7806 */
7807 protected int computeVerticalScrollRange() {
7808 return getHeight();
7809 }
7810
7811 /**
7812 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7813 * within the horizontal range. This value is used to compute the position
7814 * of the thumb within the scrollbar's track.</p>
7815 *
7816 * <p>The range is expressed in arbitrary units that must be the same as the
7817 * units used by {@link #computeVerticalScrollRange()} and
7818 * {@link #computeVerticalScrollExtent()}.</p>
7819 *
7820 * <p>The default offset is the scroll offset of this view.</p>
7821 *
7822 * @return the vertical offset of the scrollbar's thumb
7823 *
7824 * @see #computeVerticalScrollRange()
7825 * @see #computeVerticalScrollExtent()
7826 * @see android.widget.ScrollBarDrawable
7827 */
7828 protected int computeVerticalScrollOffset() {
7829 return mScrollY;
7830 }
7831
7832 /**
7833 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7834 * within the vertical range. This value is used to compute the length
7835 * of the thumb within the scrollbar's track.</p>
7836 *
7837 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007838 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839 * {@link #computeVerticalScrollOffset()}.</p>
7840 *
7841 * <p>The default extent is the drawing height of this view.</p>
7842 *
7843 * @return the vertical extent of the scrollbar's thumb
7844 *
7845 * @see #computeVerticalScrollRange()
7846 * @see #computeVerticalScrollOffset()
7847 * @see android.widget.ScrollBarDrawable
7848 */
7849 protected int computeVerticalScrollExtent() {
7850 return getHeight();
7851 }
7852
7853 /**
7854 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7855 * scrollbars are painted only if they have been awakened first.</p>
7856 *
7857 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007858 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007859 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007861 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007862 // scrollbars are drawn only when the animation is running
7863 final ScrollabilityCache cache = mScrollCache;
7864 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007865
Mike Cleronf116bf82009-09-27 19:14:12 -07007866 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007867
Mike Cleronf116bf82009-09-27 19:14:12 -07007868 if (state == ScrollabilityCache.OFF) {
7869 return;
7870 }
Joe Malin32736f02011-01-19 16:14:20 -08007871
Mike Cleronf116bf82009-09-27 19:14:12 -07007872 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007873
Mike Cleronf116bf82009-09-27 19:14:12 -07007874 if (state == ScrollabilityCache.FADING) {
7875 // We're fading -- get our fade interpolation
7876 if (cache.interpolatorValues == null) {
7877 cache.interpolatorValues = new float[1];
7878 }
Joe Malin32736f02011-01-19 16:14:20 -08007879
Mike Cleronf116bf82009-09-27 19:14:12 -07007880 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007881
Mike Cleronf116bf82009-09-27 19:14:12 -07007882 // Stops the animation if we're done
7883 if (cache.scrollBarInterpolator.timeToValues(values) ==
7884 Interpolator.Result.FREEZE_END) {
7885 cache.state = ScrollabilityCache.OFF;
7886 } else {
7887 cache.scrollBar.setAlpha(Math.round(values[0]));
7888 }
Joe Malin32736f02011-01-19 16:14:20 -08007889
7890 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007891 // drawing. We only want this when we're fading so that
7892 // we prevent excessive redraws
7893 invalidate = true;
7894 } else {
7895 // We're just on -- but we may have been fading before so
7896 // reset alpha
7897 cache.scrollBar.setAlpha(255);
7898 }
7899
Joe Malin32736f02011-01-19 16:14:20 -08007900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 final int viewFlags = mViewFlags;
7902
7903 final boolean drawHorizontalScrollBar =
7904 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7905 final boolean drawVerticalScrollBar =
7906 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7907 && !isVerticalScrollBarHidden();
7908
7909 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7910 final int width = mRight - mLeft;
7911 final int height = mBottom - mTop;
7912
7913 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007914
Mike Reede8853fc2009-09-04 14:01:48 -04007915 final int scrollX = mScrollX;
7916 final int scrollY = mScrollY;
7917 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7918
Mike Cleronf116bf82009-09-27 19:14:12 -07007919 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007922 int size = scrollBar.getSize(false);
7923 if (size <= 0) {
7924 size = cache.scrollBarSize;
7925 }
7926
Mike Cleronf116bf82009-09-27 19:14:12 -07007927 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007928 computeHorizontalScrollOffset(),
7929 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007930 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007931 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007932 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007933 left = scrollX + (mPaddingLeft & inside);
7934 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7935 bottom = top + size;
7936 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7937 if (invalidate) {
7938 invalidate(left, top, right, bottom);
7939 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007940 }
7941
7942 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007943 int size = scrollBar.getSize(true);
7944 if (size <= 0) {
7945 size = cache.scrollBarSize;
7946 }
7947
Mike Reede8853fc2009-09-04 14:01:48 -04007948 scrollBar.setParameters(computeVerticalScrollRange(),
7949 computeVerticalScrollOffset(),
7950 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007951 switch (mVerticalScrollbarPosition) {
7952 default:
7953 case SCROLLBAR_POSITION_DEFAULT:
7954 case SCROLLBAR_POSITION_RIGHT:
7955 left = scrollX + width - size - (mUserPaddingRight & inside);
7956 break;
7957 case SCROLLBAR_POSITION_LEFT:
7958 left = scrollX + (mUserPaddingLeft & inside);
7959 break;
7960 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007961 top = scrollY + (mPaddingTop & inside);
7962 right = left + size;
7963 bottom = scrollY + height - (mUserPaddingBottom & inside);
7964 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7965 if (invalidate) {
7966 invalidate(left, top, right, bottom);
7967 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007968 }
7969 }
7970 }
7971 }
Romain Guy8506ab42009-06-11 17:35:47 -07007972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007973 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007974 * 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 -08007975 * FastScroller is visible.
7976 * @return whether to temporarily hide the vertical scrollbar
7977 * @hide
7978 */
7979 protected boolean isVerticalScrollBarHidden() {
7980 return false;
7981 }
7982
7983 /**
7984 * <p>Draw the horizontal scrollbar if
7985 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7986 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007987 * @param canvas the canvas on which to draw the scrollbar
7988 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007989 *
7990 * @see #isHorizontalScrollBarEnabled()
7991 * @see #computeHorizontalScrollRange()
7992 * @see #computeHorizontalScrollExtent()
7993 * @see #computeHorizontalScrollOffset()
7994 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07007995 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007996 */
Romain Guy8fb95422010-08-17 18:38:51 -07007997 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
7998 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007999 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008000 scrollBar.draw(canvas);
8001 }
Mike Reede8853fc2009-09-04 14:01:48 -04008002
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008003 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8005 * returns true.</p>
8006 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008007 * @param canvas the canvas on which to draw the scrollbar
8008 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008009 *
8010 * @see #isVerticalScrollBarEnabled()
8011 * @see #computeVerticalScrollRange()
8012 * @see #computeVerticalScrollExtent()
8013 * @see #computeVerticalScrollOffset()
8014 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008015 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008016 */
Romain Guy8fb95422010-08-17 18:38:51 -07008017 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8018 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008019 scrollBar.setBounds(l, t, r, b);
8020 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008021 }
8022
8023 /**
8024 * Implement this to do your drawing.
8025 *
8026 * @param canvas the canvas on which the background will be drawn
8027 */
8028 protected void onDraw(Canvas canvas) {
8029 }
8030
8031 /*
8032 * Caller is responsible for calling requestLayout if necessary.
8033 * (This allows addViewInLayout to not request a new layout.)
8034 */
8035 void assignParent(ViewParent parent) {
8036 if (mParent == null) {
8037 mParent = parent;
8038 } else if (parent == null) {
8039 mParent = null;
8040 } else {
8041 throw new RuntimeException("view " + this + " being added, but"
8042 + " it already has a parent");
8043 }
8044 }
8045
8046 /**
8047 * This is called when the view is attached to a window. At this point it
8048 * has a Surface and will start drawing. Note that this function is
8049 * guaranteed to be called before {@link #onDraw}, however it may be called
8050 * any time before the first onDraw -- including before or after
8051 * {@link #onMeasure}.
8052 *
8053 * @see #onDetachedFromWindow()
8054 */
8055 protected void onAttachedToWindow() {
8056 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8057 mParent.requestTransparentRegion(this);
8058 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008059 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8060 initialAwakenScrollBars();
8061 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8062 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008063 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008064 }
8065
8066 /**
8067 * This is called when the view is detached from a window. At this point it
8068 * no longer has a surface for drawing.
8069 *
8070 * @see #onAttachedToWindow()
8071 */
8072 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008073 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008074
Romain Guya440b002010-02-24 15:57:54 -08008075 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008076 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008077 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008078
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008079 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008080
8081 if (mHardwareLayer != null) {
8082 mHardwareLayer.destroy();
8083 mHardwareLayer = null;
8084 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008085
Chet Haasedaf98e92011-01-10 14:10:36 -08008086 if (mDisplayList != null) {
8087 mDisplayList.invalidate();
8088 }
8089
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008090 if (mAttachInfo != null) {
8091 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8092 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8093 }
8094
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008095 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 }
8097
8098 /**
8099 * @return The number of times this view has been attached to a window
8100 */
8101 protected int getWindowAttachCount() {
8102 return mWindowAttachCount;
8103 }
8104
8105 /**
8106 * Retrieve a unique token identifying the window this view is attached to.
8107 * @return Return the window's token for use in
8108 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8109 */
8110 public IBinder getWindowToken() {
8111 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8112 }
8113
8114 /**
8115 * Retrieve a unique token identifying the top-level "real" window of
8116 * the window that this view is attached to. That is, this is like
8117 * {@link #getWindowToken}, except if the window this view in is a panel
8118 * window (attached to another containing window), then the token of
8119 * the containing window is returned instead.
8120 *
8121 * @return Returns the associated window token, either
8122 * {@link #getWindowToken()} or the containing window's token.
8123 */
8124 public IBinder getApplicationWindowToken() {
8125 AttachInfo ai = mAttachInfo;
8126 if (ai != null) {
8127 IBinder appWindowToken = ai.mPanelParentWindowToken;
8128 if (appWindowToken == null) {
8129 appWindowToken = ai.mWindowToken;
8130 }
8131 return appWindowToken;
8132 }
8133 return null;
8134 }
8135
8136 /**
8137 * Retrieve private session object this view hierarchy is using to
8138 * communicate with the window manager.
8139 * @return the session object to communicate with the window manager
8140 */
8141 /*package*/ IWindowSession getWindowSession() {
8142 return mAttachInfo != null ? mAttachInfo.mSession : null;
8143 }
8144
8145 /**
8146 * @param info the {@link android.view.View.AttachInfo} to associated with
8147 * this view
8148 */
8149 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8150 //System.out.println("Attached! " + this);
8151 mAttachInfo = info;
8152 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008153 // We will need to evaluate the drawable state at least once.
8154 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 if (mFloatingTreeObserver != null) {
8156 info.mTreeObserver.merge(mFloatingTreeObserver);
8157 mFloatingTreeObserver = null;
8158 }
8159 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8160 mAttachInfo.mScrollContainers.add(this);
8161 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8162 }
8163 performCollectViewAttributes(visibility);
8164 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008165
8166 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8167 mOnAttachStateChangeListeners;
8168 if (listeners != null && listeners.size() > 0) {
8169 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8170 // perform the dispatching. The iterator is a safe guard against listeners that
8171 // could mutate the list by calling the various add/remove methods. This prevents
8172 // the array from being modified while we iterate it.
8173 for (OnAttachStateChangeListener listener : listeners) {
8174 listener.onViewAttachedToWindow(this);
8175 }
8176 }
8177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 int vis = info.mWindowVisibility;
8179 if (vis != GONE) {
8180 onWindowVisibilityChanged(vis);
8181 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008182 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8183 // If nobody has evaluated the drawable state yet, then do it now.
8184 refreshDrawableState();
8185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008186 }
8187
8188 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 AttachInfo info = mAttachInfo;
8190 if (info != null) {
8191 int vis = info.mWindowVisibility;
8192 if (vis != GONE) {
8193 onWindowVisibilityChanged(GONE);
8194 }
8195 }
8196
8197 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008198
Adam Powell4afd62b2011-02-18 15:02:18 -08008199 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8200 mOnAttachStateChangeListeners;
8201 if (listeners != null && listeners.size() > 0) {
8202 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8203 // perform the dispatching. The iterator is a safe guard against listeners that
8204 // could mutate the list by calling the various add/remove methods. This prevents
8205 // the array from being modified while we iterate it.
8206 for (OnAttachStateChangeListener listener : listeners) {
8207 listener.onViewDetachedFromWindow(this);
8208 }
8209 }
8210
Romain Guy01d5edc2011-01-28 11:28:53 -08008211 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 mAttachInfo.mScrollContainers.remove(this);
8213 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8214 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008215
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 mAttachInfo = null;
8217 }
8218
8219 /**
8220 * Store this view hierarchy's frozen state into the given container.
8221 *
8222 * @param container The SparseArray in which to save the view's state.
8223 *
8224 * @see #restoreHierarchyState
8225 * @see #dispatchSaveInstanceState
8226 * @see #onSaveInstanceState
8227 */
8228 public void saveHierarchyState(SparseArray<Parcelable> container) {
8229 dispatchSaveInstanceState(container);
8230 }
8231
8232 /**
8233 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8234 * May be overridden to modify how freezing happens to a view's children; for example, some
8235 * views may want to not store state for their children.
8236 *
8237 * @param container The SparseArray in which to save the view's state.
8238 *
8239 * @see #dispatchRestoreInstanceState
8240 * @see #saveHierarchyState
8241 * @see #onSaveInstanceState
8242 */
8243 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8244 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8245 mPrivateFlags &= ~SAVE_STATE_CALLED;
8246 Parcelable state = onSaveInstanceState();
8247 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8248 throw new IllegalStateException(
8249 "Derived class did not call super.onSaveInstanceState()");
8250 }
8251 if (state != null) {
8252 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8253 // + ": " + state);
8254 container.put(mID, state);
8255 }
8256 }
8257 }
8258
8259 /**
8260 * Hook allowing a view to generate a representation of its internal state
8261 * that can later be used to create a new instance with that same state.
8262 * This state should only contain information that is not persistent or can
8263 * not be reconstructed later. For example, you will never store your
8264 * current position on screen because that will be computed again when a
8265 * new instance of the view is placed in its view hierarchy.
8266 * <p>
8267 * Some examples of things you may store here: the current cursor position
8268 * in a text view (but usually not the text itself since that is stored in a
8269 * content provider or other persistent storage), the currently selected
8270 * item in a list view.
8271 *
8272 * @return Returns a Parcelable object containing the view's current dynamic
8273 * state, or null if there is nothing interesting to save. The
8274 * default implementation returns null.
8275 * @see #onRestoreInstanceState
8276 * @see #saveHierarchyState
8277 * @see #dispatchSaveInstanceState
8278 * @see #setSaveEnabled(boolean)
8279 */
8280 protected Parcelable onSaveInstanceState() {
8281 mPrivateFlags |= SAVE_STATE_CALLED;
8282 return BaseSavedState.EMPTY_STATE;
8283 }
8284
8285 /**
8286 * Restore this view hierarchy's frozen state from the given container.
8287 *
8288 * @param container The SparseArray which holds previously frozen states.
8289 *
8290 * @see #saveHierarchyState
8291 * @see #dispatchRestoreInstanceState
8292 * @see #onRestoreInstanceState
8293 */
8294 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8295 dispatchRestoreInstanceState(container);
8296 }
8297
8298 /**
8299 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8300 * children. May be overridden to modify how restoreing happens to a view's children; for
8301 * example, some views may want to not store state for their children.
8302 *
8303 * @param container The SparseArray which holds previously saved state.
8304 *
8305 * @see #dispatchSaveInstanceState
8306 * @see #restoreHierarchyState
8307 * @see #onRestoreInstanceState
8308 */
8309 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8310 if (mID != NO_ID) {
8311 Parcelable state = container.get(mID);
8312 if (state != null) {
8313 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8314 // + ": " + state);
8315 mPrivateFlags &= ~SAVE_STATE_CALLED;
8316 onRestoreInstanceState(state);
8317 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8318 throw new IllegalStateException(
8319 "Derived class did not call super.onRestoreInstanceState()");
8320 }
8321 }
8322 }
8323 }
8324
8325 /**
8326 * Hook allowing a view to re-apply a representation of its internal state that had previously
8327 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8328 * null state.
8329 *
8330 * @param state The frozen state that had previously been returned by
8331 * {@link #onSaveInstanceState}.
8332 *
8333 * @see #onSaveInstanceState
8334 * @see #restoreHierarchyState
8335 * @see #dispatchRestoreInstanceState
8336 */
8337 protected void onRestoreInstanceState(Parcelable state) {
8338 mPrivateFlags |= SAVE_STATE_CALLED;
8339 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008340 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8341 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008342 + "when two views of different type have the same id in the same hierarchy. "
8343 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008344 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 }
8346 }
8347
8348 /**
8349 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8350 *
8351 * @return the drawing start time in milliseconds
8352 */
8353 public long getDrawingTime() {
8354 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8355 }
8356
8357 /**
8358 * <p>Enables or disables the duplication of the parent's state into this view. When
8359 * duplication is enabled, this view gets its drawable state from its parent rather
8360 * than from its own internal properties.</p>
8361 *
8362 * <p>Note: in the current implementation, setting this property to true after the
8363 * view was added to a ViewGroup might have no effect at all. This property should
8364 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8365 *
8366 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8367 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008368 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008369 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8370 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008371 *
8372 * @param enabled True to enable duplication of the parent's drawable state, false
8373 * to disable it.
8374 *
8375 * @see #getDrawableState()
8376 * @see #isDuplicateParentStateEnabled()
8377 */
8378 public void setDuplicateParentStateEnabled(boolean enabled) {
8379 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8380 }
8381
8382 /**
8383 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8384 *
8385 * @return True if this view's drawable state is duplicated from the parent,
8386 * false otherwise
8387 *
8388 * @see #getDrawableState()
8389 * @see #setDuplicateParentStateEnabled(boolean)
8390 */
8391 public boolean isDuplicateParentStateEnabled() {
8392 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8393 }
8394
8395 /**
Romain Guy171c5922011-01-06 10:04:23 -08008396 * <p>Specifies the type of layer backing this view. The layer can be
8397 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8398 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008399 *
Romain Guy171c5922011-01-06 10:04:23 -08008400 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8401 * instance that controls how the layer is composed on screen. The following
8402 * properties of the paint are taken into account when composing the layer:</p>
8403 * <ul>
8404 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8405 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8406 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8407 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008408 *
Romain Guy171c5922011-01-06 10:04:23 -08008409 * <p>If this view has an alpha value set to < 1.0 by calling
8410 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8411 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8412 * equivalent to setting a hardware layer on this view and providing a paint with
8413 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008414 *
Romain Guy171c5922011-01-06 10:04:23 -08008415 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8416 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8417 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008418 *
Romain Guy171c5922011-01-06 10:04:23 -08008419 * @param layerType The ype of layer to use with this view, must be one of
8420 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8421 * {@link #LAYER_TYPE_HARDWARE}
8422 * @param paint The paint used to compose the layer. This argument is optional
8423 * and can be null. It is ignored when the layer type is
8424 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008425 *
8426 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008427 * @see #LAYER_TYPE_NONE
8428 * @see #LAYER_TYPE_SOFTWARE
8429 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008430 * @see #setAlpha(float)
8431 *
Romain Guy171c5922011-01-06 10:04:23 -08008432 * @attr ref android.R.styleable#View_layerType
8433 */
8434 public void setLayerType(int layerType, Paint paint) {
8435 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008436 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008437 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8438 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008439
Romain Guyd6cd5722011-01-17 14:42:41 -08008440 if (layerType == mLayerType) {
8441 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8442 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008443 invalidateParentCaches();
8444 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008445 }
8446 return;
8447 }
Romain Guy171c5922011-01-06 10:04:23 -08008448
8449 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008450 switch (mLayerType) {
8451 case LAYER_TYPE_SOFTWARE:
8452 if (mDrawingCache != null) {
8453 mDrawingCache.recycle();
8454 mDrawingCache = null;
8455 }
Joe Malin32736f02011-01-19 16:14:20 -08008456
Romain Guy6c319ca2011-01-11 14:29:25 -08008457 if (mUnscaledDrawingCache != null) {
8458 mUnscaledDrawingCache.recycle();
8459 mUnscaledDrawingCache = null;
8460 }
8461 break;
8462 case LAYER_TYPE_HARDWARE:
8463 if (mHardwareLayer != null) {
8464 mHardwareLayer.destroy();
8465 mHardwareLayer = null;
8466 }
8467 break;
8468 default:
8469 break;
Romain Guy171c5922011-01-06 10:04:23 -08008470 }
8471
8472 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008473 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8474 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8475 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008476
Romain Guy0fd89bf2011-01-26 15:41:30 -08008477 invalidateParentCaches();
8478 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008479 }
8480
8481 /**
8482 * Indicates what type of layer is currently associated with this view. By default
8483 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8484 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8485 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008486 *
Romain Guy171c5922011-01-06 10:04:23 -08008487 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8488 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008489 *
8490 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008491 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008492 * @see #LAYER_TYPE_NONE
8493 * @see #LAYER_TYPE_SOFTWARE
8494 * @see #LAYER_TYPE_HARDWARE
8495 */
8496 public int getLayerType() {
8497 return mLayerType;
8498 }
Joe Malin32736f02011-01-19 16:14:20 -08008499
Romain Guy6c319ca2011-01-11 14:29:25 -08008500 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008501 * Forces this view's layer to be created and this view to be rendered
8502 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8503 * invoking this method will have no effect.
8504 *
8505 * This method can for instance be used to render a view into its layer before
8506 * starting an animation. If this view is complex, rendering into the layer
8507 * before starting the animation will avoid skipping frames.
8508 *
8509 * @throws IllegalStateException If this view is not attached to a window
8510 *
8511 * @see #setLayerType(int, android.graphics.Paint)
8512 */
8513 public void buildLayer() {
8514 if (mLayerType == LAYER_TYPE_NONE) return;
8515
8516 if (mAttachInfo == null) {
8517 throw new IllegalStateException("This view must be attached to a window first");
8518 }
8519
8520 switch (mLayerType) {
8521 case LAYER_TYPE_HARDWARE:
8522 getHardwareLayer();
8523 break;
8524 case LAYER_TYPE_SOFTWARE:
8525 buildDrawingCache(true);
8526 break;
8527 }
8528 }
8529
8530 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008531 * <p>Returns a hardware layer that can be used to draw this view again
8532 * without executing its draw method.</p>
8533 *
8534 * @return A HardwareLayer ready to render, or null if an error occurred.
8535 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008536 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008537 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8538 return null;
8539 }
8540
8541 final int width = mRight - mLeft;
8542 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008543
Romain Guy6c319ca2011-01-11 14:29:25 -08008544 if (width == 0 || height == 0) {
8545 return null;
8546 }
8547
8548 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8549 if (mHardwareLayer == null) {
8550 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8551 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008552 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008553 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8554 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008555 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008556 }
8557
Romain Guy5e7f7662011-01-24 22:35:56 -08008558 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8559 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8560 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008561 try {
8562 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008563 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008564 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008565
Chet Haase88172fe2011-03-07 17:36:33 -08008566 final int restoreCount = canvas.save();
8567
Romain Guy6c319ca2011-01-11 14:29:25 -08008568 computeScroll();
8569 canvas.translate(-mScrollX, -mScrollY);
8570
Romain Guy6c319ca2011-01-11 14:29:25 -08008571 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008572
Romain Guy6c319ca2011-01-11 14:29:25 -08008573 // Fast path for layouts with no backgrounds
8574 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8575 mPrivateFlags &= ~DIRTY_MASK;
8576 dispatchDraw(canvas);
8577 } else {
8578 draw(canvas);
8579 }
Joe Malin32736f02011-01-19 16:14:20 -08008580
Chet Haase88172fe2011-03-07 17:36:33 -08008581 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008582 } finally {
8583 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008584 mHardwareLayer.end(currentCanvas);
8585 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008586 }
8587 }
8588
8589 return mHardwareLayer;
8590 }
Romain Guy171c5922011-01-06 10:04:23 -08008591
8592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008593 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8594 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8595 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8596 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8597 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8598 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008599 *
Romain Guy171c5922011-01-06 10:04:23 -08008600 * <p>Enabling the drawing cache is similar to
8601 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008602 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8603 * drawing cache has no effect on rendering because the system uses a different mechanism
8604 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8605 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8606 * for information on how to enable software and hardware layers.</p>
8607 *
8608 * <p>This API can be used to manually generate
8609 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8610 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 *
8612 * @param enabled true to enable the drawing cache, false otherwise
8613 *
8614 * @see #isDrawingCacheEnabled()
8615 * @see #getDrawingCache()
8616 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008617 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008618 */
8619 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008620 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008621 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8622 }
8623
8624 /**
8625 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8626 *
8627 * @return true if the drawing cache is enabled
8628 *
8629 * @see #setDrawingCacheEnabled(boolean)
8630 * @see #getDrawingCache()
8631 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008632 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008633 public boolean isDrawingCacheEnabled() {
8634 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8635 }
8636
8637 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008638 * Debugging utility which recursively outputs the dirty state of a view and its
8639 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008640 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008641 * @hide
8642 */
Romain Guy676b1732011-02-14 14:45:33 -08008643 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008644 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8645 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8646 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8647 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8648 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8649 if (clear) {
8650 mPrivateFlags &= clearMask;
8651 }
8652 if (this instanceof ViewGroup) {
8653 ViewGroup parent = (ViewGroup) this;
8654 final int count = parent.getChildCount();
8655 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008656 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008657 child.outputDirtyFlags(indent + " ", clear, clearMask);
8658 }
8659 }
8660 }
8661
8662 /**
8663 * This method is used by ViewGroup to cause its children to restore or recreate their
8664 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8665 * to recreate its own display list, which would happen if it went through the normal
8666 * draw/dispatchDraw mechanisms.
8667 *
8668 * @hide
8669 */
8670 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008671
8672 /**
8673 * A view that is not attached or hardware accelerated cannot create a display list.
8674 * This method checks these conditions and returns the appropriate result.
8675 *
8676 * @return true if view has the ability to create a display list, false otherwise.
8677 *
8678 * @hide
8679 */
8680 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008681 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008682 }
Joe Malin32736f02011-01-19 16:14:20 -08008683
Chet Haasedaf98e92011-01-10 14:10:36 -08008684 /**
Romain Guyb051e892010-09-28 19:09:36 -07008685 * <p>Returns a display list that can be used to draw this view again
8686 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008687 *
Romain Guyb051e892010-09-28 19:09:36 -07008688 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008689 *
8690 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008691 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008692 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008693 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008694 return null;
8695 }
8696
Chet Haasedaf98e92011-01-10 14:10:36 -08008697 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8698 mDisplayList == null || !mDisplayList.isValid() ||
8699 mRecreateDisplayList)) {
8700 // Don't need to recreate the display list, just need to tell our
8701 // children to restore/recreate theirs
8702 if (mDisplayList != null && mDisplayList.isValid() &&
8703 !mRecreateDisplayList) {
8704 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8705 mPrivateFlags &= ~DIRTY_MASK;
8706 dispatchGetDisplayList();
8707
8708 return mDisplayList;
8709 }
8710
8711 // If we got here, we're recreating it. Mark it as such to ensure that
8712 // we copy in child display lists into ours in drawChild()
8713 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008714 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008715 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8716 // If we're creating a new display list, make sure our parent gets invalidated
8717 // since they will need to recreate their display list to account for this
8718 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008719 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008720 }
Romain Guyb051e892010-09-28 19:09:36 -07008721
8722 final HardwareCanvas canvas = mDisplayList.start();
8723 try {
8724 int width = mRight - mLeft;
8725 int height = mBottom - mTop;
8726
8727 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008728 // The dirty rect should always be null for a display list
8729 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008730
Chet Haase88172fe2011-03-07 17:36:33 -08008731 final int restoreCount = canvas.save();
8732
Chet Haasedaf98e92011-01-10 14:10:36 -08008733 computeScroll();
8734 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008735 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008736
Romain Guyb051e892010-09-28 19:09:36 -07008737 // Fast path for layouts with no backgrounds
8738 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8739 mPrivateFlags &= ~DIRTY_MASK;
8740 dispatchDraw(canvas);
8741 } else {
8742 draw(canvas);
8743 }
Joe Malin32736f02011-01-19 16:14:20 -08008744
Chet Haase88172fe2011-03-07 17:36:33 -08008745 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008746 } finally {
8747 canvas.onPostDraw();
8748
8749 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008750 }
Chet Haase77785f92011-01-25 23:22:09 -08008751 } else {
8752 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8753 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008754 }
8755
8756 return mDisplayList;
8757 }
8758
8759 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008760 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008761 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008762 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008763 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008764 * @see #getDrawingCache(boolean)
8765 */
8766 public Bitmap getDrawingCache() {
8767 return getDrawingCache(false);
8768 }
8769
8770 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8772 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8773 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8774 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8775 * request the drawing cache by calling this method and draw it on screen if the
8776 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008777 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008778 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8779 * this method will create a bitmap of the same size as this view. Because this bitmap
8780 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8781 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8782 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8783 * size than the view. This implies that your application must be able to handle this
8784 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008785 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008786 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8787 * the current density of the screen when the application is in compatibility
8788 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008790 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008791 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008792 * @see #setDrawingCacheEnabled(boolean)
8793 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008794 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008795 * @see #destroyDrawingCache()
8796 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008797 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008798 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8799 return null;
8800 }
8801 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008802 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 }
Romain Guy02890fd2010-08-06 17:58:44 -07008804 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805 }
8806
8807 /**
8808 * <p>Frees the resources used by the drawing cache. If you call
8809 * {@link #buildDrawingCache()} manually without calling
8810 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8811 * should cleanup the cache with this method afterwards.</p>
8812 *
8813 * @see #setDrawingCacheEnabled(boolean)
8814 * @see #buildDrawingCache()
8815 * @see #getDrawingCache()
8816 */
8817 public void destroyDrawingCache() {
8818 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008819 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 mDrawingCache = null;
8821 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008822 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008823 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008824 mUnscaledDrawingCache = null;
8825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008826 }
8827
8828 /**
8829 * Setting a solid background color for the drawing cache's bitmaps will improve
8830 * perfromance and memory usage. Note, though that this should only be used if this
8831 * view will always be drawn on top of a solid color.
8832 *
8833 * @param color The background color to use for the drawing cache's bitmap
8834 *
8835 * @see #setDrawingCacheEnabled(boolean)
8836 * @see #buildDrawingCache()
8837 * @see #getDrawingCache()
8838 */
8839 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008840 if (color != mDrawingCacheBackgroundColor) {
8841 mDrawingCacheBackgroundColor = color;
8842 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008844 }
8845
8846 /**
8847 * @see #setDrawingCacheBackgroundColor(int)
8848 *
8849 * @return The background color to used for the drawing cache's bitmap
8850 */
8851 public int getDrawingCacheBackgroundColor() {
8852 return mDrawingCacheBackgroundColor;
8853 }
8854
8855 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008856 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008857 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008858 * @see #buildDrawingCache(boolean)
8859 */
8860 public void buildDrawingCache() {
8861 buildDrawingCache(false);
8862 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008863
Romain Guyfbd8f692009-06-26 14:51:58 -07008864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008865 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8866 *
8867 * <p>If you call {@link #buildDrawingCache()} manually without calling
8868 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8869 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008870 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008871 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8872 * this method will create a bitmap of the same size as this view. Because this bitmap
8873 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8874 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8875 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8876 * size than the view. This implies that your application must be able to handle this
8877 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008878 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008879 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8880 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008881 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008882 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 *
8884 * @see #getDrawingCache()
8885 * @see #destroyDrawingCache()
8886 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008887 public void buildDrawingCache(boolean autoScale) {
8888 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008889 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008890 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008891
8892 if (ViewDebug.TRACE_HIERARCHY) {
8893 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895
Romain Guy8506ab42009-06-11 17:35:47 -07008896 int width = mRight - mLeft;
8897 int height = mBottom - mTop;
8898
8899 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008900 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008901
Romain Guye1123222009-06-29 14:24:56 -07008902 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008903 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8904 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008905 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008906
8907 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008908 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008909 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008910
8911 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008912 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008913 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008914 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8915 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008916 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008917 return;
8918 }
8919
8920 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008921 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008922
8923 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008924 Bitmap.Config quality;
8925 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008926 // Never pick ARGB_4444 because it looks awful
8927 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8929 case DRAWING_CACHE_QUALITY_AUTO:
8930 quality = Bitmap.Config.ARGB_8888;
8931 break;
8932 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008933 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 break;
8935 case DRAWING_CACHE_QUALITY_HIGH:
8936 quality = Bitmap.Config.ARGB_8888;
8937 break;
8938 default:
8939 quality = Bitmap.Config.ARGB_8888;
8940 break;
8941 }
8942 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008943 // Optimization for translucent windows
8944 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008945 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008946 }
8947
8948 // Try to cleanup memory
8949 if (bitmap != null) bitmap.recycle();
8950
8951 try {
8952 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008953 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008954 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008955 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008956 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008957 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008958 }
Adam Powell26153a32010-11-08 15:22:27 -08008959 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008960 } catch (OutOfMemoryError e) {
8961 // If there is not enough memory to create the bitmap cache, just
8962 // ignore the issue as bitmap caches are not required to draw the
8963 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008964 if (autoScale) {
8965 mDrawingCache = null;
8966 } else {
8967 mUnscaledDrawingCache = null;
8968 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008969 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008970 return;
8971 }
8972
8973 clear = drawingCacheBackgroundColor != 0;
8974 }
8975
8976 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 if (attachInfo != null) {
8978 canvas = attachInfo.mCanvas;
8979 if (canvas == null) {
8980 canvas = new Canvas();
8981 }
8982 canvas.setBitmap(bitmap);
8983 // Temporarily clobber the cached Canvas in case one of our children
8984 // is also using a drawing cache. Without this, the children would
8985 // steal the canvas by attaching their own bitmap to it and bad, bad
8986 // thing would happen (invisible views, corrupted drawings, etc.)
8987 attachInfo.mCanvas = null;
8988 } else {
8989 // This case should hopefully never or seldom happen
8990 canvas = new Canvas(bitmap);
8991 }
8992
8993 if (clear) {
8994 bitmap.eraseColor(drawingCacheBackgroundColor);
8995 }
8996
8997 computeScroll();
8998 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08008999
Romain Guye1123222009-06-29 14:24:56 -07009000 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009001 final float scale = attachInfo.mApplicationScale;
9002 canvas.scale(scale, scale);
9003 }
Joe Malin32736f02011-01-19 16:14:20 -08009004
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009005 canvas.translate(-mScrollX, -mScrollY);
9006
Romain Guy5bcdff42009-05-14 21:27:18 -07009007 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009008 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9009 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009010 mPrivateFlags |= DRAWING_CACHE_VALID;
9011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009012
9013 // Fast path for layouts with no backgrounds
9014 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9015 if (ViewDebug.TRACE_HIERARCHY) {
9016 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9017 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009018 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009019 dispatchDraw(canvas);
9020 } else {
9021 draw(canvas);
9022 }
9023
9024 canvas.restoreToCount(restoreCount);
9025
9026 if (attachInfo != null) {
9027 // Restore the cached Canvas for our siblings
9028 attachInfo.mCanvas = canvas;
9029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 }
9031 }
9032
9033 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009034 * Create a snapshot of the view into a bitmap. We should probably make
9035 * some form of this public, but should think about the API.
9036 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009037 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009038 int width = mRight - mLeft;
9039 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009040
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009041 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009042 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009043 width = (int) ((width * scale) + 0.5f);
9044 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009045
Romain Guy8c11e312009-09-14 15:15:30 -07009046 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009047 if (bitmap == null) {
9048 throw new OutOfMemoryError();
9049 }
9050
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009051 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009052
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009053 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009054 if (attachInfo != null) {
9055 canvas = attachInfo.mCanvas;
9056 if (canvas == null) {
9057 canvas = new Canvas();
9058 }
9059 canvas.setBitmap(bitmap);
9060 // Temporarily clobber the cached Canvas in case one of our children
9061 // is also using a drawing cache. Without this, the children would
9062 // steal the canvas by attaching their own bitmap to it and bad, bad
9063 // things would happen (invisible views, corrupted drawings, etc.)
9064 attachInfo.mCanvas = null;
9065 } else {
9066 // This case should hopefully never or seldom happen
9067 canvas = new Canvas(bitmap);
9068 }
9069
Romain Guy5bcdff42009-05-14 21:27:18 -07009070 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009071 bitmap.eraseColor(backgroundColor);
9072 }
9073
9074 computeScroll();
9075 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009076 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009077 canvas.translate(-mScrollX, -mScrollY);
9078
Romain Guy5bcdff42009-05-14 21:27:18 -07009079 // Temporarily remove the dirty mask
9080 int flags = mPrivateFlags;
9081 mPrivateFlags &= ~DIRTY_MASK;
9082
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009083 // Fast path for layouts with no backgrounds
9084 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9085 dispatchDraw(canvas);
9086 } else {
9087 draw(canvas);
9088 }
9089
Romain Guy5bcdff42009-05-14 21:27:18 -07009090 mPrivateFlags = flags;
9091
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009092 canvas.restoreToCount(restoreCount);
9093
9094 if (attachInfo != null) {
9095 // Restore the cached Canvas for our siblings
9096 attachInfo.mCanvas = canvas;
9097 }
Romain Guy8506ab42009-06-11 17:35:47 -07009098
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009099 return bitmap;
9100 }
9101
9102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 * Indicates whether this View is currently in edit mode. A View is usually
9104 * in edit mode when displayed within a developer tool. For instance, if
9105 * this View is being drawn by a visual user interface builder, this method
9106 * should return true.
9107 *
9108 * Subclasses should check the return value of this method to provide
9109 * different behaviors if their normal behavior might interfere with the
9110 * host environment. For instance: the class spawns a thread in its
9111 * constructor, the drawing code relies on device-specific features, etc.
9112 *
9113 * This method is usually checked in the drawing code of custom widgets.
9114 *
9115 * @return True if this View is in edit mode, false otherwise.
9116 */
9117 public boolean isInEditMode() {
9118 return false;
9119 }
9120
9121 /**
9122 * If the View draws content inside its padding and enables fading edges,
9123 * it needs to support padding offsets. Padding offsets are added to the
9124 * fading edges to extend the length of the fade so that it covers pixels
9125 * drawn inside the padding.
9126 *
9127 * Subclasses of this class should override this method if they need
9128 * to draw content inside the padding.
9129 *
9130 * @return True if padding offset must be applied, false otherwise.
9131 *
9132 * @see #getLeftPaddingOffset()
9133 * @see #getRightPaddingOffset()
9134 * @see #getTopPaddingOffset()
9135 * @see #getBottomPaddingOffset()
9136 *
9137 * @since CURRENT
9138 */
9139 protected boolean isPaddingOffsetRequired() {
9140 return false;
9141 }
9142
9143 /**
9144 * Amount by which to extend the left fading region. Called only when
9145 * {@link #isPaddingOffsetRequired()} returns true.
9146 *
9147 * @return The left padding offset in pixels.
9148 *
9149 * @see #isPaddingOffsetRequired()
9150 *
9151 * @since CURRENT
9152 */
9153 protected int getLeftPaddingOffset() {
9154 return 0;
9155 }
9156
9157 /**
9158 * Amount by which to extend the right fading region. Called only when
9159 * {@link #isPaddingOffsetRequired()} returns true.
9160 *
9161 * @return The right padding offset in pixels.
9162 *
9163 * @see #isPaddingOffsetRequired()
9164 *
9165 * @since CURRENT
9166 */
9167 protected int getRightPaddingOffset() {
9168 return 0;
9169 }
9170
9171 /**
9172 * Amount by which to extend the top fading region. Called only when
9173 * {@link #isPaddingOffsetRequired()} returns true.
9174 *
9175 * @return The top padding offset in pixels.
9176 *
9177 * @see #isPaddingOffsetRequired()
9178 *
9179 * @since CURRENT
9180 */
9181 protected int getTopPaddingOffset() {
9182 return 0;
9183 }
9184
9185 /**
9186 * Amount by which to extend the bottom fading region. Called only when
9187 * {@link #isPaddingOffsetRequired()} returns true.
9188 *
9189 * @return The bottom padding offset in pixels.
9190 *
9191 * @see #isPaddingOffsetRequired()
9192 *
9193 * @since CURRENT
9194 */
9195 protected int getBottomPaddingOffset() {
9196 return 0;
9197 }
9198
9199 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009200 * <p>Indicates whether this view is attached to an hardware accelerated
9201 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009202 *
Romain Guy2bffd262010-09-12 17:40:02 -07009203 * <p>Even if this method returns true, it does not mean that every call
9204 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9205 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9206 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9207 * window is hardware accelerated,
9208 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9209 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009210 *
Romain Guy2bffd262010-09-12 17:40:02 -07009211 * @return True if the view is attached to a window and the window is
9212 * hardware accelerated; false in any other case.
9213 */
9214 public boolean isHardwareAccelerated() {
9215 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9216 }
Joe Malin32736f02011-01-19 16:14:20 -08009217
Romain Guy2bffd262010-09-12 17:40:02 -07009218 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009219 * Manually render this view (and all of its children) to the given Canvas.
9220 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009221 * called. When implementing a view, implement {@link #onDraw} instead of
9222 * overriding this method. If you do need to override this method, call
9223 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 *
9225 * @param canvas The Canvas to which the View is rendered.
9226 */
9227 public void draw(Canvas canvas) {
9228 if (ViewDebug.TRACE_HIERARCHY) {
9229 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9230 }
9231
Romain Guy5bcdff42009-05-14 21:27:18 -07009232 final int privateFlags = mPrivateFlags;
9233 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9234 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9235 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 /*
9238 * Draw traversal performs several drawing steps which must be executed
9239 * in the appropriate order:
9240 *
9241 * 1. Draw the background
9242 * 2. If necessary, save the canvas' layers to prepare for fading
9243 * 3. Draw view's content
9244 * 4. Draw children
9245 * 5. If necessary, draw the fading edges and restore layers
9246 * 6. Draw decorations (scrollbars for instance)
9247 */
9248
9249 // Step 1, draw the background, if needed
9250 int saveCount;
9251
Romain Guy24443ea2009-05-11 11:56:30 -07009252 if (!dirtyOpaque) {
9253 final Drawable background = mBGDrawable;
9254 if (background != null) {
9255 final int scrollX = mScrollX;
9256 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009257
Romain Guy24443ea2009-05-11 11:56:30 -07009258 if (mBackgroundSizeChanged) {
9259 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9260 mBackgroundSizeChanged = false;
9261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009262
Romain Guy24443ea2009-05-11 11:56:30 -07009263 if ((scrollX | scrollY) == 0) {
9264 background.draw(canvas);
9265 } else {
9266 canvas.translate(scrollX, scrollY);
9267 background.draw(canvas);
9268 canvas.translate(-scrollX, -scrollY);
9269 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 }
9271 }
9272
9273 // skip step 2 & 5 if possible (common case)
9274 final int viewFlags = mViewFlags;
9275 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9276 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9277 if (!verticalEdges && !horizontalEdges) {
9278 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009279 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009280
9281 // Step 4, draw the children
9282 dispatchDraw(canvas);
9283
9284 // Step 6, draw decorations (scrollbars)
9285 onDrawScrollBars(canvas);
9286
9287 // we're done...
9288 return;
9289 }
9290
9291 /*
9292 * Here we do the full fledged routine...
9293 * (this is an uncommon case where speed matters less,
9294 * this is why we repeat some of the tests that have been
9295 * done above)
9296 */
9297
9298 boolean drawTop = false;
9299 boolean drawBottom = false;
9300 boolean drawLeft = false;
9301 boolean drawRight = false;
9302
9303 float topFadeStrength = 0.0f;
9304 float bottomFadeStrength = 0.0f;
9305 float leftFadeStrength = 0.0f;
9306 float rightFadeStrength = 0.0f;
9307
9308 // Step 2, save the canvas' layers
9309 int paddingLeft = mPaddingLeft;
9310 int paddingTop = mPaddingTop;
9311
9312 final boolean offsetRequired = isPaddingOffsetRequired();
9313 if (offsetRequired) {
9314 paddingLeft += getLeftPaddingOffset();
9315 paddingTop += getTopPaddingOffset();
9316 }
9317
9318 int left = mScrollX + paddingLeft;
9319 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9320 int top = mScrollY + paddingTop;
9321 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9322
9323 if (offsetRequired) {
9324 right += getRightPaddingOffset();
9325 bottom += getBottomPaddingOffset();
9326 }
9327
9328 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009329 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9330 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331
9332 // clip the fade length if top and bottom fades overlap
9333 // overlapping fades produce odd-looking artifacts
9334 if (verticalEdges && (top + length > bottom - length)) {
9335 length = (bottom - top) / 2;
9336 }
9337
9338 // also clip horizontal fades if necessary
9339 if (horizontalEdges && (left + length > right - length)) {
9340 length = (right - left) / 2;
9341 }
9342
9343 if (verticalEdges) {
9344 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009345 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009347 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009348 }
9349
9350 if (horizontalEdges) {
9351 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009352 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009353 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009354 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 }
9356
9357 saveCount = canvas.getSaveCount();
9358
9359 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009360 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9362
9363 if (drawTop) {
9364 canvas.saveLayer(left, top, right, top + length, null, flags);
9365 }
9366
9367 if (drawBottom) {
9368 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9369 }
9370
9371 if (drawLeft) {
9372 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9373 }
9374
9375 if (drawRight) {
9376 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9377 }
9378 } else {
9379 scrollabilityCache.setFadeColor(solidColor);
9380 }
9381
9382 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009383 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009384
9385 // Step 4, draw the children
9386 dispatchDraw(canvas);
9387
9388 // Step 5, draw the fade effect and restore layers
9389 final Paint p = scrollabilityCache.paint;
9390 final Matrix matrix = scrollabilityCache.matrix;
9391 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009392
9393 if (drawTop) {
9394 matrix.setScale(1, fadeHeight * topFadeStrength);
9395 matrix.postTranslate(left, top);
9396 fade.setLocalMatrix(matrix);
9397 canvas.drawRect(left, top, right, top + length, p);
9398 }
9399
9400 if (drawBottom) {
9401 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9402 matrix.postRotate(180);
9403 matrix.postTranslate(left, bottom);
9404 fade.setLocalMatrix(matrix);
9405 canvas.drawRect(left, bottom - length, right, bottom, p);
9406 }
9407
9408 if (drawLeft) {
9409 matrix.setScale(1, fadeHeight * leftFadeStrength);
9410 matrix.postRotate(-90);
9411 matrix.postTranslate(left, top);
9412 fade.setLocalMatrix(matrix);
9413 canvas.drawRect(left, top, left + length, bottom, p);
9414 }
9415
9416 if (drawRight) {
9417 matrix.setScale(1, fadeHeight * rightFadeStrength);
9418 matrix.postRotate(90);
9419 matrix.postTranslate(right, top);
9420 fade.setLocalMatrix(matrix);
9421 canvas.drawRect(right - length, top, right, bottom, p);
9422 }
9423
9424 canvas.restoreToCount(saveCount);
9425
9426 // Step 6, draw decorations (scrollbars)
9427 onDrawScrollBars(canvas);
9428 }
9429
9430 /**
9431 * Override this if your view is known to always be drawn on top of a solid color background,
9432 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9433 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9434 * should be set to 0xFF.
9435 *
9436 * @see #setVerticalFadingEdgeEnabled
9437 * @see #setHorizontalFadingEdgeEnabled
9438 *
9439 * @return The known solid color background for this view, or 0 if the color may vary
9440 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009441 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 public int getSolidColor() {
9443 return 0;
9444 }
9445
9446 /**
9447 * Build a human readable string representation of the specified view flags.
9448 *
9449 * @param flags the view flags to convert to a string
9450 * @return a String representing the supplied flags
9451 */
9452 private static String printFlags(int flags) {
9453 String output = "";
9454 int numFlags = 0;
9455 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9456 output += "TAKES_FOCUS";
9457 numFlags++;
9458 }
9459
9460 switch (flags & VISIBILITY_MASK) {
9461 case INVISIBLE:
9462 if (numFlags > 0) {
9463 output += " ";
9464 }
9465 output += "INVISIBLE";
9466 // USELESS HERE numFlags++;
9467 break;
9468 case GONE:
9469 if (numFlags > 0) {
9470 output += " ";
9471 }
9472 output += "GONE";
9473 // USELESS HERE numFlags++;
9474 break;
9475 default:
9476 break;
9477 }
9478 return output;
9479 }
9480
9481 /**
9482 * Build a human readable string representation of the specified private
9483 * view flags.
9484 *
9485 * @param privateFlags the private view flags to convert to a string
9486 * @return a String representing the supplied flags
9487 */
9488 private static String printPrivateFlags(int privateFlags) {
9489 String output = "";
9490 int numFlags = 0;
9491
9492 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9493 output += "WANTS_FOCUS";
9494 numFlags++;
9495 }
9496
9497 if ((privateFlags & FOCUSED) == FOCUSED) {
9498 if (numFlags > 0) {
9499 output += " ";
9500 }
9501 output += "FOCUSED";
9502 numFlags++;
9503 }
9504
9505 if ((privateFlags & SELECTED) == SELECTED) {
9506 if (numFlags > 0) {
9507 output += " ";
9508 }
9509 output += "SELECTED";
9510 numFlags++;
9511 }
9512
9513 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9514 if (numFlags > 0) {
9515 output += " ";
9516 }
9517 output += "IS_ROOT_NAMESPACE";
9518 numFlags++;
9519 }
9520
9521 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9522 if (numFlags > 0) {
9523 output += " ";
9524 }
9525 output += "HAS_BOUNDS";
9526 numFlags++;
9527 }
9528
9529 if ((privateFlags & DRAWN) == DRAWN) {
9530 if (numFlags > 0) {
9531 output += " ";
9532 }
9533 output += "DRAWN";
9534 // USELESS HERE numFlags++;
9535 }
9536 return output;
9537 }
9538
9539 /**
9540 * <p>Indicates whether or not this view's layout will be requested during
9541 * the next hierarchy layout pass.</p>
9542 *
9543 * @return true if the layout will be forced during next layout pass
9544 */
9545 public boolean isLayoutRequested() {
9546 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9547 }
9548
9549 /**
9550 * Assign a size and position to a view and all of its
9551 * descendants
9552 *
9553 * <p>This is the second phase of the layout mechanism.
9554 * (The first is measuring). In this phase, each parent calls
9555 * layout on all of its children to position them.
9556 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009557 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 *
Chet Haase9c087442011-01-12 16:20:16 -08009559 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 * Derived classes with children should override
9561 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009562 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 *
9564 * @param l Left position, relative to parent
9565 * @param t Top position, relative to parent
9566 * @param r Right position, relative to parent
9567 * @param b Bottom position, relative to parent
9568 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009569 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009570 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009571 int oldL = mLeft;
9572 int oldT = mTop;
9573 int oldB = mBottom;
9574 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009575 boolean changed = setFrame(l, t, r, b);
9576 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9577 if (ViewDebug.TRACE_HIERARCHY) {
9578 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9579 }
9580
9581 onLayout(changed, l, t, r, b);
9582 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009583
9584 if (mOnLayoutChangeListeners != null) {
9585 ArrayList<OnLayoutChangeListener> listenersCopy =
9586 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9587 int numListeners = listenersCopy.size();
9588 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009589 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009590 }
9591 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 }
9593 mPrivateFlags &= ~FORCE_LAYOUT;
9594 }
9595
9596 /**
9597 * Called from layout when this view should
9598 * assign a size and position to each of its children.
9599 *
9600 * Derived classes with children should override
9601 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009602 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603 * @param changed This is a new size or position for this view
9604 * @param left Left position, relative to parent
9605 * @param top Top position, relative to parent
9606 * @param right Right position, relative to parent
9607 * @param bottom Bottom position, relative to parent
9608 */
9609 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9610 }
9611
9612 /**
9613 * Assign a size and position to this view.
9614 *
9615 * This is called from layout.
9616 *
9617 * @param left Left position, relative to parent
9618 * @param top Top position, relative to parent
9619 * @param right Right position, relative to parent
9620 * @param bottom Bottom position, relative to parent
9621 * @return true if the new size and position are different than the
9622 * previous ones
9623 * {@hide}
9624 */
9625 protected boolean setFrame(int left, int top, int right, int bottom) {
9626 boolean changed = false;
9627
9628 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009629 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009630 + right + "," + bottom + ")");
9631 }
9632
9633 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9634 changed = true;
9635
9636 // Remember our drawn bit
9637 int drawn = mPrivateFlags & DRAWN;
9638
9639 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009640 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641
9642
9643 int oldWidth = mRight - mLeft;
9644 int oldHeight = mBottom - mTop;
9645
9646 mLeft = left;
9647 mTop = top;
9648 mRight = right;
9649 mBottom = bottom;
9650
9651 mPrivateFlags |= HAS_BOUNDS;
9652
9653 int newWidth = right - left;
9654 int newHeight = bottom - top;
9655
9656 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009657 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9658 // A change in dimension means an auto-centered pivot point changes, too
9659 mMatrixDirty = true;
9660 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009661 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9662 }
9663
9664 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9665 // If we are visible, force the DRAWN bit to on so that
9666 // this invalidate will go through (at least to our parent).
9667 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009668 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009669 // the DRAWN bit.
9670 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009671 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009672 // parent display list may need to be recreated based on a change in the bounds
9673 // of any child
9674 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009675 }
9676
9677 // Reset drawn bit to original value (invalidate turns it off)
9678 mPrivateFlags |= drawn;
9679
9680 mBackgroundSizeChanged = true;
9681 }
9682 return changed;
9683 }
9684
9685 /**
9686 * Finalize inflating a view from XML. This is called as the last phase
9687 * of inflation, after all child views have been added.
9688 *
9689 * <p>Even if the subclass overrides onFinishInflate, they should always be
9690 * sure to call the super method, so that we get called.
9691 */
9692 protected void onFinishInflate() {
9693 }
9694
9695 /**
9696 * Returns the resources associated with this view.
9697 *
9698 * @return Resources object.
9699 */
9700 public Resources getResources() {
9701 return mResources;
9702 }
9703
9704 /**
9705 * Invalidates the specified Drawable.
9706 *
9707 * @param drawable the drawable to invalidate
9708 */
9709 public void invalidateDrawable(Drawable drawable) {
9710 if (verifyDrawable(drawable)) {
9711 final Rect dirty = drawable.getBounds();
9712 final int scrollX = mScrollX;
9713 final int scrollY = mScrollY;
9714
9715 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9716 dirty.right + scrollX, dirty.bottom + scrollY);
9717 }
9718 }
9719
9720 /**
9721 * Schedules an action on a drawable to occur at a specified time.
9722 *
9723 * @param who the recipient of the action
9724 * @param what the action to run on the drawable
9725 * @param when the time at which the action must occur. Uses the
9726 * {@link SystemClock#uptimeMillis} timebase.
9727 */
9728 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9729 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9730 mAttachInfo.mHandler.postAtTime(what, who, when);
9731 }
9732 }
9733
9734 /**
9735 * Cancels a scheduled action on a drawable.
9736 *
9737 * @param who the recipient of the action
9738 * @param what the action to cancel
9739 */
9740 public void unscheduleDrawable(Drawable who, Runnable what) {
9741 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9742 mAttachInfo.mHandler.removeCallbacks(what, who);
9743 }
9744 }
9745
9746 /**
9747 * Unschedule any events associated with the given Drawable. This can be
9748 * used when selecting a new Drawable into a view, so that the previous
9749 * one is completely unscheduled.
9750 *
9751 * @param who The Drawable to unschedule.
9752 *
9753 * @see #drawableStateChanged
9754 */
9755 public void unscheduleDrawable(Drawable who) {
9756 if (mAttachInfo != null) {
9757 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9758 }
9759 }
9760
9761 /**
9762 * If your view subclass is displaying its own Drawable objects, it should
9763 * override this function and return true for any Drawable it is
9764 * displaying. This allows animations for those drawables to be
9765 * scheduled.
9766 *
9767 * <p>Be sure to call through to the super class when overriding this
9768 * function.
9769 *
9770 * @param who The Drawable to verify. Return true if it is one you are
9771 * displaying, else return the result of calling through to the
9772 * super class.
9773 *
9774 * @return boolean If true than the Drawable is being displayed in the
9775 * view; else false and it is not allowed to animate.
9776 *
9777 * @see #unscheduleDrawable
9778 * @see #drawableStateChanged
9779 */
9780 protected boolean verifyDrawable(Drawable who) {
9781 return who == mBGDrawable;
9782 }
9783
9784 /**
9785 * This function is called whenever the state of the view changes in such
9786 * a way that it impacts the state of drawables being shown.
9787 *
9788 * <p>Be sure to call through to the superclass when overriding this
9789 * function.
9790 *
9791 * @see Drawable#setState
9792 */
9793 protected void drawableStateChanged() {
9794 Drawable d = mBGDrawable;
9795 if (d != null && d.isStateful()) {
9796 d.setState(getDrawableState());
9797 }
9798 }
9799
9800 /**
9801 * Call this to force a view to update its drawable state. This will cause
9802 * drawableStateChanged to be called on this view. Views that are interested
9803 * in the new state should call getDrawableState.
9804 *
9805 * @see #drawableStateChanged
9806 * @see #getDrawableState
9807 */
9808 public void refreshDrawableState() {
9809 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9810 drawableStateChanged();
9811
9812 ViewParent parent = mParent;
9813 if (parent != null) {
9814 parent.childDrawableStateChanged(this);
9815 }
9816 }
9817
9818 /**
9819 * Return an array of resource IDs of the drawable states representing the
9820 * current state of the view.
9821 *
9822 * @return The current drawable state
9823 *
9824 * @see Drawable#setState
9825 * @see #drawableStateChanged
9826 * @see #onCreateDrawableState
9827 */
9828 public final int[] getDrawableState() {
9829 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9830 return mDrawableState;
9831 } else {
9832 mDrawableState = onCreateDrawableState(0);
9833 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9834 return mDrawableState;
9835 }
9836 }
9837
9838 /**
9839 * Generate the new {@link android.graphics.drawable.Drawable} state for
9840 * this view. This is called by the view
9841 * system when the cached Drawable state is determined to be invalid. To
9842 * retrieve the current state, you should use {@link #getDrawableState}.
9843 *
9844 * @param extraSpace if non-zero, this is the number of extra entries you
9845 * would like in the returned array in which you can place your own
9846 * states.
9847 *
9848 * @return Returns an array holding the current {@link Drawable} state of
9849 * the view.
9850 *
9851 * @see #mergeDrawableStates
9852 */
9853 protected int[] onCreateDrawableState(int extraSpace) {
9854 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9855 mParent instanceof View) {
9856 return ((View) mParent).onCreateDrawableState(extraSpace);
9857 }
9858
9859 int[] drawableState;
9860
9861 int privateFlags = mPrivateFlags;
9862
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009863 int viewStateIndex = 0;
9864 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9865 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9866 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009867 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009868 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9869 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009870 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9871 // This is set if HW acceleration is requested, even if the current
9872 // process doesn't allow it. This is just to allow app preview
9873 // windows to better match their app.
9874 viewStateIndex |= VIEW_STATE_ACCELERATED;
9875 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876
9877 drawableState = VIEW_STATE_SETS[viewStateIndex];
9878
9879 //noinspection ConstantIfStatement
9880 if (false) {
9881 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9882 Log.i("View", toString()
9883 + " pressed=" + ((privateFlags & PRESSED) != 0)
9884 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9885 + " fo=" + hasFocus()
9886 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009887 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 + ": " + Arrays.toString(drawableState));
9889 }
9890
9891 if (extraSpace == 0) {
9892 return drawableState;
9893 }
9894
9895 final int[] fullState;
9896 if (drawableState != null) {
9897 fullState = new int[drawableState.length + extraSpace];
9898 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9899 } else {
9900 fullState = new int[extraSpace];
9901 }
9902
9903 return fullState;
9904 }
9905
9906 /**
9907 * Merge your own state values in <var>additionalState</var> into the base
9908 * state values <var>baseState</var> that were returned by
9909 * {@link #onCreateDrawableState}.
9910 *
9911 * @param baseState The base state values returned by
9912 * {@link #onCreateDrawableState}, which will be modified to also hold your
9913 * own additional state values.
9914 *
9915 * @param additionalState The additional state values you would like
9916 * added to <var>baseState</var>; this array is not modified.
9917 *
9918 * @return As a convenience, the <var>baseState</var> array you originally
9919 * passed into the function is returned.
9920 *
9921 * @see #onCreateDrawableState
9922 */
9923 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9924 final int N = baseState.length;
9925 int i = N - 1;
9926 while (i >= 0 && baseState[i] == 0) {
9927 i--;
9928 }
9929 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9930 return baseState;
9931 }
9932
9933 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009934 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9935 * on all Drawable objects associated with this view.
9936 */
9937 public void jumpDrawablesToCurrentState() {
9938 if (mBGDrawable != null) {
9939 mBGDrawable.jumpToCurrentState();
9940 }
9941 }
9942
9943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 * Sets the background color for this view.
9945 * @param color the color of the background
9946 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009947 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009948 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009949 if (mBGDrawable instanceof ColorDrawable) {
9950 ((ColorDrawable) mBGDrawable).setColor(color);
9951 } else {
9952 setBackgroundDrawable(new ColorDrawable(color));
9953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 }
9955
9956 /**
9957 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009958 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009959 * @param resid The identifier of the resource.
9960 * @attr ref android.R.styleable#View_background
9961 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009962 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009963 public void setBackgroundResource(int resid) {
9964 if (resid != 0 && resid == mBackgroundResource) {
9965 return;
9966 }
9967
9968 Drawable d= null;
9969 if (resid != 0) {
9970 d = mResources.getDrawable(resid);
9971 }
9972 setBackgroundDrawable(d);
9973
9974 mBackgroundResource = resid;
9975 }
9976
9977 /**
9978 * Set the background to a given Drawable, or remove the background. If the
9979 * background has padding, this View's padding is set to the background's
9980 * padding. However, when a background is removed, this View's padding isn't
9981 * touched. If setting the padding is desired, please use
9982 * {@link #setPadding(int, int, int, int)}.
9983 *
9984 * @param d The Drawable to use as the background, or null to remove the
9985 * background
9986 */
9987 public void setBackgroundDrawable(Drawable d) {
9988 boolean requestLayout = false;
9989
9990 mBackgroundResource = 0;
9991
9992 /*
9993 * Regardless of whether we're setting a new background or not, we want
9994 * to clear the previous drawable.
9995 */
9996 if (mBGDrawable != null) {
9997 mBGDrawable.setCallback(null);
9998 unscheduleDrawable(mBGDrawable);
9999 }
10000
10001 if (d != null) {
10002 Rect padding = sThreadLocal.get();
10003 if (padding == null) {
10004 padding = new Rect();
10005 sThreadLocal.set(padding);
10006 }
10007 if (d.getPadding(padding)) {
10008 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10009 }
10010
10011 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10012 // if it has a different minimum size, we should layout again
10013 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10014 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10015 requestLayout = true;
10016 }
10017
10018 d.setCallback(this);
10019 if (d.isStateful()) {
10020 d.setState(getDrawableState());
10021 }
10022 d.setVisible(getVisibility() == VISIBLE, false);
10023 mBGDrawable = d;
10024
10025 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10026 mPrivateFlags &= ~SKIP_DRAW;
10027 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10028 requestLayout = true;
10029 }
10030 } else {
10031 /* Remove the background */
10032 mBGDrawable = null;
10033
10034 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10035 /*
10036 * This view ONLY drew the background before and we're removing
10037 * the background, so now it won't draw anything
10038 * (hence we SKIP_DRAW)
10039 */
10040 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10041 mPrivateFlags |= SKIP_DRAW;
10042 }
10043
10044 /*
10045 * When the background is set, we try to apply its padding to this
10046 * View. When the background is removed, we don't touch this View's
10047 * padding. This is noted in the Javadocs. Hence, we don't need to
10048 * requestLayout(), the invalidate() below is sufficient.
10049 */
10050
10051 // The old background's minimum size could have affected this
10052 // View's layout, so let's requestLayout
10053 requestLayout = true;
10054 }
10055
Romain Guy8f1344f52009-05-15 16:03:59 -070010056 computeOpaqueFlags();
10057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010058 if (requestLayout) {
10059 requestLayout();
10060 }
10061
10062 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010063 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010064 }
10065
10066 /**
10067 * Gets the background drawable
10068 * @return The drawable used as the background for this view, if any.
10069 */
10070 public Drawable getBackground() {
10071 return mBGDrawable;
10072 }
10073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 /**
10075 * Sets the padding. The view may add on the space required to display
10076 * the scrollbars, depending on the style and visibility of the scrollbars.
10077 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10078 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10079 * from the values set in this call.
10080 *
10081 * @attr ref android.R.styleable#View_padding
10082 * @attr ref android.R.styleable#View_paddingBottom
10083 * @attr ref android.R.styleable#View_paddingLeft
10084 * @attr ref android.R.styleable#View_paddingRight
10085 * @attr ref android.R.styleable#View_paddingTop
10086 * @param left the left padding in pixels
10087 * @param top the top padding in pixels
10088 * @param right the right padding in pixels
10089 * @param bottom the bottom padding in pixels
10090 */
10091 public void setPadding(int left, int top, int right, int bottom) {
10092 boolean changed = false;
10093
Adam Powell20232d02010-12-08 21:08:53 -080010094 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 mUserPaddingRight = right;
10096 mUserPaddingBottom = bottom;
10097
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010098 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010099
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010100 // Common case is there are no scroll bars.
10101 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010102 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010103 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10104 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010105 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010106 switch (mVerticalScrollbarPosition) {
10107 case SCROLLBAR_POSITION_DEFAULT:
10108 case SCROLLBAR_POSITION_RIGHT:
10109 right += offset;
10110 break;
10111 case SCROLLBAR_POSITION_LEFT:
10112 left += offset;
10113 break;
10114 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010115 }
Adam Powell20232d02010-12-08 21:08:53 -080010116 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010117 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10118 ? 0 : getHorizontalScrollbarHeight();
10119 }
10120 }
Romain Guy8506ab42009-06-11 17:35:47 -070010121
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010122 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010123 changed = true;
10124 mPaddingLeft = left;
10125 }
10126 if (mPaddingTop != top) {
10127 changed = true;
10128 mPaddingTop = top;
10129 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010130 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010131 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010132 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010133 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010134 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010136 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010137 }
10138
10139 if (changed) {
10140 requestLayout();
10141 }
10142 }
10143
10144 /**
10145 * Returns the top padding of this view.
10146 *
10147 * @return the top padding in pixels
10148 */
10149 public int getPaddingTop() {
10150 return mPaddingTop;
10151 }
10152
10153 /**
10154 * Returns the bottom padding of this view. If there are inset and enabled
10155 * scrollbars, this value may include the space required to display the
10156 * scrollbars as well.
10157 *
10158 * @return the bottom padding in pixels
10159 */
10160 public int getPaddingBottom() {
10161 return mPaddingBottom;
10162 }
10163
10164 /**
10165 * Returns the left padding of this view. If there are inset and enabled
10166 * scrollbars, this value may include the space required to display the
10167 * scrollbars as well.
10168 *
10169 * @return the left padding in pixels
10170 */
10171 public int getPaddingLeft() {
10172 return mPaddingLeft;
10173 }
10174
10175 /**
10176 * Returns the right padding of this view. If there are inset and enabled
10177 * scrollbars, this value may include the space required to display the
10178 * scrollbars as well.
10179 *
10180 * @return the right padding in pixels
10181 */
10182 public int getPaddingRight() {
10183 return mPaddingRight;
10184 }
10185
10186 /**
10187 * Changes the selection state of this view. A view can be selected or not.
10188 * Note that selection is not the same as focus. Views are typically
10189 * selected in the context of an AdapterView like ListView or GridView;
10190 * the selected view is the view that is highlighted.
10191 *
10192 * @param selected true if the view must be selected, false otherwise
10193 */
10194 public void setSelected(boolean selected) {
10195 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10196 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010197 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010198 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 refreshDrawableState();
10200 dispatchSetSelected(selected);
10201 }
10202 }
10203
10204 /**
10205 * Dispatch setSelected to all of this View's children.
10206 *
10207 * @see #setSelected(boolean)
10208 *
10209 * @param selected The new selected state
10210 */
10211 protected void dispatchSetSelected(boolean selected) {
10212 }
10213
10214 /**
10215 * Indicates the selection state of this view.
10216 *
10217 * @return true if the view is selected, false otherwise
10218 */
10219 @ViewDebug.ExportedProperty
10220 public boolean isSelected() {
10221 return (mPrivateFlags & SELECTED) != 0;
10222 }
10223
10224 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010225 * Changes the activated state of this view. A view can be activated or not.
10226 * Note that activation is not the same as selection. Selection is
10227 * a transient property, representing the view (hierarchy) the user is
10228 * currently interacting with. Activation is a longer-term state that the
10229 * user can move views in and out of. For example, in a list view with
10230 * single or multiple selection enabled, the views in the current selection
10231 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10232 * here.) The activated state is propagated down to children of the view it
10233 * is set on.
10234 *
10235 * @param activated true if the view must be activated, false otherwise
10236 */
10237 public void setActivated(boolean activated) {
10238 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10239 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010240 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010241 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010242 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010243 }
10244 }
10245
10246 /**
10247 * Dispatch setActivated to all of this View's children.
10248 *
10249 * @see #setActivated(boolean)
10250 *
10251 * @param activated The new activated state
10252 */
10253 protected void dispatchSetActivated(boolean activated) {
10254 }
10255
10256 /**
10257 * Indicates the activation state of this view.
10258 *
10259 * @return true if the view is activated, false otherwise
10260 */
10261 @ViewDebug.ExportedProperty
10262 public boolean isActivated() {
10263 return (mPrivateFlags & ACTIVATED) != 0;
10264 }
10265
10266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010267 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10268 * observer can be used to get notifications when global events, like
10269 * layout, happen.
10270 *
10271 * The returned ViewTreeObserver observer is not guaranteed to remain
10272 * valid for the lifetime of this View. If the caller of this method keeps
10273 * a long-lived reference to ViewTreeObserver, it should always check for
10274 * the return value of {@link ViewTreeObserver#isAlive()}.
10275 *
10276 * @return The ViewTreeObserver for this view's hierarchy.
10277 */
10278 public ViewTreeObserver getViewTreeObserver() {
10279 if (mAttachInfo != null) {
10280 return mAttachInfo.mTreeObserver;
10281 }
10282 if (mFloatingTreeObserver == null) {
10283 mFloatingTreeObserver = new ViewTreeObserver();
10284 }
10285 return mFloatingTreeObserver;
10286 }
10287
10288 /**
10289 * <p>Finds the topmost view in the current view hierarchy.</p>
10290 *
10291 * @return the topmost view containing this view
10292 */
10293 public View getRootView() {
10294 if (mAttachInfo != null) {
10295 final View v = mAttachInfo.mRootView;
10296 if (v != null) {
10297 return v;
10298 }
10299 }
Romain Guy8506ab42009-06-11 17:35:47 -070010300
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 View parent = this;
10302
10303 while (parent.mParent != null && parent.mParent instanceof View) {
10304 parent = (View) parent.mParent;
10305 }
10306
10307 return parent;
10308 }
10309
10310 /**
10311 * <p>Computes the coordinates of this view on the screen. The argument
10312 * must be an array of two integers. After the method returns, the array
10313 * contains the x and y location in that order.</p>
10314 *
10315 * @param location an array of two integers in which to hold the coordinates
10316 */
10317 public void getLocationOnScreen(int[] location) {
10318 getLocationInWindow(location);
10319
10320 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010321 if (info != null) {
10322 location[0] += info.mWindowLeft;
10323 location[1] += info.mWindowTop;
10324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010325 }
10326
10327 /**
10328 * <p>Computes the coordinates of this view in its window. The argument
10329 * must be an array of two integers. After the method returns, the array
10330 * contains the x and y location in that order.</p>
10331 *
10332 * @param location an array of two integers in which to hold the coordinates
10333 */
10334 public void getLocationInWindow(int[] location) {
10335 if (location == null || location.length < 2) {
10336 throw new IllegalArgumentException("location must be an array of "
10337 + "two integers");
10338 }
10339
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010340 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10341 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342
10343 ViewParent viewParent = mParent;
10344 while (viewParent instanceof View) {
10345 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010346 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10347 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010348 viewParent = view.mParent;
10349 }
Romain Guy8506ab42009-06-11 17:35:47 -070010350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010351 if (viewParent instanceof ViewRoot) {
10352 // *cough*
10353 final ViewRoot vr = (ViewRoot)viewParent;
10354 location[1] -= vr.mCurScrollY;
10355 }
10356 }
10357
10358 /**
10359 * {@hide}
10360 * @param id the id of the view to be found
10361 * @return the view of the specified id, null if cannot be found
10362 */
10363 protected View findViewTraversal(int id) {
10364 if (id == mID) {
10365 return this;
10366 }
10367 return null;
10368 }
10369
10370 /**
10371 * {@hide}
10372 * @param tag the tag of the view to be found
10373 * @return the view of specified tag, null if cannot be found
10374 */
10375 protected View findViewWithTagTraversal(Object tag) {
10376 if (tag != null && tag.equals(mTag)) {
10377 return this;
10378 }
10379 return null;
10380 }
10381
10382 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010383 * {@hide}
10384 * @param predicate The predicate to evaluate.
10385 * @return The first view that matches the predicate or null.
10386 */
10387 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10388 if (predicate.apply(this)) {
10389 return this;
10390 }
10391 return null;
10392 }
10393
10394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010395 * Look for a child view with the given id. If this view has the given
10396 * id, return this view.
10397 *
10398 * @param id The id to search for.
10399 * @return The view that has the given id in the hierarchy or null
10400 */
10401 public final View findViewById(int id) {
10402 if (id < 0) {
10403 return null;
10404 }
10405 return findViewTraversal(id);
10406 }
10407
10408 /**
10409 * Look for a child view with the given tag. If this view has the given
10410 * tag, return this view.
10411 *
10412 * @param tag The tag to search for, using "tag.equals(getTag())".
10413 * @return The View that has the given tag in the hierarchy or null
10414 */
10415 public final View findViewWithTag(Object tag) {
10416 if (tag == null) {
10417 return null;
10418 }
10419 return findViewWithTagTraversal(tag);
10420 }
10421
10422 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010423 * {@hide}
10424 * Look for a child view that matches the specified predicate.
10425 * If this view matches the predicate, return this view.
10426 *
10427 * @param predicate The predicate to evaluate.
10428 * @return The first view that matches the predicate or null.
10429 */
10430 public final View findViewByPredicate(Predicate<View> predicate) {
10431 return findViewByPredicateTraversal(predicate);
10432 }
10433
10434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010435 * Sets the identifier for this view. The identifier does not have to be
10436 * unique in this view's hierarchy. The identifier should be a positive
10437 * number.
10438 *
10439 * @see #NO_ID
10440 * @see #getId
10441 * @see #findViewById
10442 *
10443 * @param id a number used to identify the view
10444 *
10445 * @attr ref android.R.styleable#View_id
10446 */
10447 public void setId(int id) {
10448 mID = id;
10449 }
10450
10451 /**
10452 * {@hide}
10453 *
10454 * @param isRoot true if the view belongs to the root namespace, false
10455 * otherwise
10456 */
10457 public void setIsRootNamespace(boolean isRoot) {
10458 if (isRoot) {
10459 mPrivateFlags |= IS_ROOT_NAMESPACE;
10460 } else {
10461 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10462 }
10463 }
10464
10465 /**
10466 * {@hide}
10467 *
10468 * @return true if the view belongs to the root namespace, false otherwise
10469 */
10470 public boolean isRootNamespace() {
10471 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10472 }
10473
10474 /**
10475 * Returns this view's identifier.
10476 *
10477 * @return a positive integer used to identify the view or {@link #NO_ID}
10478 * if the view has no ID
10479 *
10480 * @see #setId
10481 * @see #findViewById
10482 * @attr ref android.R.styleable#View_id
10483 */
10484 @ViewDebug.CapturedViewProperty
10485 public int getId() {
10486 return mID;
10487 }
10488
10489 /**
10490 * Returns this view's tag.
10491 *
10492 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010493 *
10494 * @see #setTag(Object)
10495 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010496 */
10497 @ViewDebug.ExportedProperty
10498 public Object getTag() {
10499 return mTag;
10500 }
10501
10502 /**
10503 * Sets the tag associated with this view. A tag can be used to mark
10504 * a view in its hierarchy and does not have to be unique within the
10505 * hierarchy. Tags can also be used to store data within a view without
10506 * resorting to another data structure.
10507 *
10508 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010509 *
10510 * @see #getTag()
10511 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 */
10513 public void setTag(final Object tag) {
10514 mTag = tag;
10515 }
10516
10517 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010518 * Returns the tag associated with this view and the specified key.
10519 *
10520 * @param key The key identifying the tag
10521 *
10522 * @return the Object stored in this view as a tag
10523 *
10524 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010525 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010526 */
10527 public Object getTag(int key) {
10528 SparseArray<Object> tags = null;
10529 synchronized (sTagsLock) {
10530 if (sTags != null) {
10531 tags = sTags.get(this);
10532 }
10533 }
10534
10535 if (tags != null) return tags.get(key);
10536 return null;
10537 }
10538
10539 /**
10540 * Sets a tag associated with this view and a key. A tag can be used
10541 * to mark a view in its hierarchy and does not have to be unique within
10542 * the hierarchy. Tags can also be used to store data within a view
10543 * without resorting to another data structure.
10544 *
10545 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010546 * application to ensure it is unique (see the <a
10547 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10548 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010549 * the Android framework or not associated with any package will cause
10550 * an {@link IllegalArgumentException} to be thrown.
10551 *
10552 * @param key The key identifying the tag
10553 * @param tag An Object to tag the view with
10554 *
10555 * @throws IllegalArgumentException If they specified key is not valid
10556 *
10557 * @see #setTag(Object)
10558 * @see #getTag(int)
10559 */
10560 public void setTag(int key, final Object tag) {
10561 // If the package id is 0x00 or 0x01, it's either an undefined package
10562 // or a framework id
10563 if ((key >>> 24) < 2) {
10564 throw new IllegalArgumentException("The key must be an application-specific "
10565 + "resource id.");
10566 }
10567
10568 setTagInternal(this, key, tag);
10569 }
10570
10571 /**
10572 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10573 * framework id.
10574 *
10575 * @hide
10576 */
10577 public void setTagInternal(int key, Object tag) {
10578 if ((key >>> 24) != 0x1) {
10579 throw new IllegalArgumentException("The key must be a framework-specific "
10580 + "resource id.");
10581 }
10582
Romain Guy8506ab42009-06-11 17:35:47 -070010583 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010584 }
10585
10586 private static void setTagInternal(View view, int key, Object tag) {
10587 SparseArray<Object> tags = null;
10588 synchronized (sTagsLock) {
10589 if (sTags == null) {
10590 sTags = new WeakHashMap<View, SparseArray<Object>>();
10591 } else {
10592 tags = sTags.get(view);
10593 }
10594 }
10595
10596 if (tags == null) {
10597 tags = new SparseArray<Object>(2);
10598 synchronized (sTagsLock) {
10599 sTags.put(view, tags);
10600 }
10601 }
10602
10603 tags.put(key, tag);
10604 }
10605
10606 /**
Romain Guy13922e02009-05-12 17:56:14 -070010607 * @param consistency The type of consistency. See ViewDebug for more information.
10608 *
10609 * @hide
10610 */
10611 protected boolean dispatchConsistencyCheck(int consistency) {
10612 return onConsistencyCheck(consistency);
10613 }
10614
10615 /**
10616 * Method that subclasses should implement to check their consistency. The type of
10617 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010618 *
Romain Guy13922e02009-05-12 17:56:14 -070010619 * @param consistency The type of consistency. See ViewDebug for more information.
10620 *
10621 * @throws IllegalStateException if the view is in an inconsistent state.
10622 *
10623 * @hide
10624 */
10625 protected boolean onConsistencyCheck(int consistency) {
10626 boolean result = true;
10627
10628 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10629 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10630
10631 if (checkLayout) {
10632 if (getParent() == null) {
10633 result = false;
10634 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10635 "View " + this + " does not have a parent.");
10636 }
10637
10638 if (mAttachInfo == null) {
10639 result = false;
10640 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10641 "View " + this + " is not attached to a window.");
10642 }
10643 }
10644
10645 if (checkDrawing) {
10646 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10647 // from their draw() method
10648
10649 if ((mPrivateFlags & DRAWN) != DRAWN &&
10650 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10651 result = false;
10652 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10653 "View " + this + " was invalidated but its drawing cache is valid.");
10654 }
10655 }
10656
10657 return result;
10658 }
10659
10660 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661 * Prints information about this view in the log output, with the tag
10662 * {@link #VIEW_LOG_TAG}.
10663 *
10664 * @hide
10665 */
10666 public void debug() {
10667 debug(0);
10668 }
10669
10670 /**
10671 * Prints information about this view in the log output, with the tag
10672 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10673 * indentation defined by the <code>depth</code>.
10674 *
10675 * @param depth the indentation level
10676 *
10677 * @hide
10678 */
10679 protected void debug(int depth) {
10680 String output = debugIndent(depth - 1);
10681
10682 output += "+ " + this;
10683 int id = getId();
10684 if (id != -1) {
10685 output += " (id=" + id + ")";
10686 }
10687 Object tag = getTag();
10688 if (tag != null) {
10689 output += " (tag=" + tag + ")";
10690 }
10691 Log.d(VIEW_LOG_TAG, output);
10692
10693 if ((mPrivateFlags & FOCUSED) != 0) {
10694 output = debugIndent(depth) + " FOCUSED";
10695 Log.d(VIEW_LOG_TAG, output);
10696 }
10697
10698 output = debugIndent(depth);
10699 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10700 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10701 + "} ";
10702 Log.d(VIEW_LOG_TAG, output);
10703
10704 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10705 || mPaddingBottom != 0) {
10706 output = debugIndent(depth);
10707 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10708 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10709 Log.d(VIEW_LOG_TAG, output);
10710 }
10711
10712 output = debugIndent(depth);
10713 output += "mMeasureWidth=" + mMeasuredWidth +
10714 " mMeasureHeight=" + mMeasuredHeight;
10715 Log.d(VIEW_LOG_TAG, output);
10716
10717 output = debugIndent(depth);
10718 if (mLayoutParams == null) {
10719 output += "BAD! no layout params";
10720 } else {
10721 output = mLayoutParams.debug(output);
10722 }
10723 Log.d(VIEW_LOG_TAG, output);
10724
10725 output = debugIndent(depth);
10726 output += "flags={";
10727 output += View.printFlags(mViewFlags);
10728 output += "}";
10729 Log.d(VIEW_LOG_TAG, output);
10730
10731 output = debugIndent(depth);
10732 output += "privateFlags={";
10733 output += View.printPrivateFlags(mPrivateFlags);
10734 output += "}";
10735 Log.d(VIEW_LOG_TAG, output);
10736 }
10737
10738 /**
10739 * Creates an string of whitespaces used for indentation.
10740 *
10741 * @param depth the indentation level
10742 * @return a String containing (depth * 2 + 3) * 2 white spaces
10743 *
10744 * @hide
10745 */
10746 protected static String debugIndent(int depth) {
10747 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10748 for (int i = 0; i < (depth * 2) + 3; i++) {
10749 spaces.append(' ').append(' ');
10750 }
10751 return spaces.toString();
10752 }
10753
10754 /**
10755 * <p>Return the offset of the widget's text baseline from the widget's top
10756 * boundary. If this widget does not support baseline alignment, this
10757 * method returns -1. </p>
10758 *
10759 * @return the offset of the baseline within the widget's bounds or -1
10760 * if baseline alignment is not supported
10761 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010762 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 public int getBaseline() {
10764 return -1;
10765 }
10766
10767 /**
10768 * Call this when something has changed which has invalidated the
10769 * layout of this view. This will schedule a layout pass of the view
10770 * tree.
10771 */
10772 public void requestLayout() {
10773 if (ViewDebug.TRACE_HIERARCHY) {
10774 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10775 }
10776
10777 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010778 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010779
10780 if (mParent != null && !mParent.isLayoutRequested()) {
10781 mParent.requestLayout();
10782 }
10783 }
10784
10785 /**
10786 * Forces this view to be laid out during the next layout pass.
10787 * This method does not call requestLayout() or forceLayout()
10788 * on the parent.
10789 */
10790 public void forceLayout() {
10791 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010792 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010793 }
10794
10795 /**
10796 * <p>
10797 * This is called to find out how big a view should be. The parent
10798 * supplies constraint information in the width and height parameters.
10799 * </p>
10800 *
10801 * <p>
10802 * The actual mesurement work of a view is performed in
10803 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10804 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10805 * </p>
10806 *
10807 *
10808 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10809 * parent
10810 * @param heightMeasureSpec Vertical space requirements as imposed by the
10811 * parent
10812 *
10813 * @see #onMeasure(int, int)
10814 */
10815 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10816 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10817 widthMeasureSpec != mOldWidthMeasureSpec ||
10818 heightMeasureSpec != mOldHeightMeasureSpec) {
10819
10820 // first clears the measured dimension flag
10821 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10822
10823 if (ViewDebug.TRACE_HIERARCHY) {
10824 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10825 }
10826
10827 // measure ourselves, this should set the measured dimension flag back
10828 onMeasure(widthMeasureSpec, heightMeasureSpec);
10829
10830 // flag not set, setMeasuredDimension() was not invoked, we raise
10831 // an exception to warn the developer
10832 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10833 throw new IllegalStateException("onMeasure() did not set the"
10834 + " measured dimension by calling"
10835 + " setMeasuredDimension()");
10836 }
10837
10838 mPrivateFlags |= LAYOUT_REQUIRED;
10839 }
10840
10841 mOldWidthMeasureSpec = widthMeasureSpec;
10842 mOldHeightMeasureSpec = heightMeasureSpec;
10843 }
10844
10845 /**
10846 * <p>
10847 * Measure the view and its content to determine the measured width and the
10848 * measured height. This method is invoked by {@link #measure(int, int)} and
10849 * should be overriden by subclasses to provide accurate and efficient
10850 * measurement of their contents.
10851 * </p>
10852 *
10853 * <p>
10854 * <strong>CONTRACT:</strong> When overriding this method, you
10855 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10856 * measured width and height of this view. Failure to do so will trigger an
10857 * <code>IllegalStateException</code>, thrown by
10858 * {@link #measure(int, int)}. Calling the superclass'
10859 * {@link #onMeasure(int, int)} is a valid use.
10860 * </p>
10861 *
10862 * <p>
10863 * The base class implementation of measure defaults to the background size,
10864 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10865 * override {@link #onMeasure(int, int)} to provide better measurements of
10866 * their content.
10867 * </p>
10868 *
10869 * <p>
10870 * If this method is overridden, it is the subclass's responsibility to make
10871 * sure the measured height and width are at least the view's minimum height
10872 * and width ({@link #getSuggestedMinimumHeight()} and
10873 * {@link #getSuggestedMinimumWidth()}).
10874 * </p>
10875 *
10876 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10877 * The requirements are encoded with
10878 * {@link android.view.View.MeasureSpec}.
10879 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10880 * The requirements are encoded with
10881 * {@link android.view.View.MeasureSpec}.
10882 *
10883 * @see #getMeasuredWidth()
10884 * @see #getMeasuredHeight()
10885 * @see #setMeasuredDimension(int, int)
10886 * @see #getSuggestedMinimumHeight()
10887 * @see #getSuggestedMinimumWidth()
10888 * @see android.view.View.MeasureSpec#getMode(int)
10889 * @see android.view.View.MeasureSpec#getSize(int)
10890 */
10891 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10892 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10893 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10894 }
10895
10896 /**
10897 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10898 * measured width and measured height. Failing to do so will trigger an
10899 * exception at measurement time.</p>
10900 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010901 * @param measuredWidth The measured width of this view. May be a complex
10902 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10903 * {@link #MEASURED_STATE_TOO_SMALL}.
10904 * @param measuredHeight The measured height of this view. May be a complex
10905 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10906 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010907 */
10908 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10909 mMeasuredWidth = measuredWidth;
10910 mMeasuredHeight = measuredHeight;
10911
10912 mPrivateFlags |= MEASURED_DIMENSION_SET;
10913 }
10914
10915 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010916 * Merge two states as returned by {@link #getMeasuredState()}.
10917 * @param curState The current state as returned from a view or the result
10918 * of combining multiple views.
10919 * @param newState The new view state to combine.
10920 * @return Returns a new integer reflecting the combination of the two
10921 * states.
10922 */
10923 public static int combineMeasuredStates(int curState, int newState) {
10924 return curState | newState;
10925 }
10926
10927 /**
10928 * Version of {@link #resolveSizeAndState(int, int, int)}
10929 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10930 */
10931 public static int resolveSize(int size, int measureSpec) {
10932 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10933 }
10934
10935 /**
10936 * Utility to reconcile a desired size and state, with constraints imposed
10937 * by a MeasureSpec. Will take the desired size, unless a different size
10938 * is imposed by the constraints. The returned value is a compound integer,
10939 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10940 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10941 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010942 *
10943 * @param size How big the view wants to be
10944 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010945 * @return Size information bit mask as defined by
10946 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010947 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010948 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949 int result = size;
10950 int specMode = MeasureSpec.getMode(measureSpec);
10951 int specSize = MeasureSpec.getSize(measureSpec);
10952 switch (specMode) {
10953 case MeasureSpec.UNSPECIFIED:
10954 result = size;
10955 break;
10956 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010957 if (specSize < size) {
10958 result = specSize | MEASURED_STATE_TOO_SMALL;
10959 } else {
10960 result = size;
10961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 break;
10963 case MeasureSpec.EXACTLY:
10964 result = specSize;
10965 break;
10966 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010967 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 }
10969
10970 /**
10971 * Utility to return a default size. Uses the supplied size if the
10972 * MeasureSpec imposed no contraints. Will get larger if allowed
10973 * by the MeasureSpec.
10974 *
10975 * @param size Default size for this view
10976 * @param measureSpec Constraints imposed by the parent
10977 * @return The size this view should be.
10978 */
10979 public static int getDefaultSize(int size, int measureSpec) {
10980 int result = size;
10981 int specMode = MeasureSpec.getMode(measureSpec);
10982 int specSize = MeasureSpec.getSize(measureSpec);
10983
10984 switch (specMode) {
10985 case MeasureSpec.UNSPECIFIED:
10986 result = size;
10987 break;
10988 case MeasureSpec.AT_MOST:
10989 case MeasureSpec.EXACTLY:
10990 result = specSize;
10991 break;
10992 }
10993 return result;
10994 }
10995
10996 /**
10997 * Returns the suggested minimum height that the view should use. This
10998 * returns the maximum of the view's minimum height
10999 * and the background's minimum height
11000 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11001 * <p>
11002 * When being used in {@link #onMeasure(int, int)}, the caller should still
11003 * ensure the returned height is within the requirements of the parent.
11004 *
11005 * @return The suggested minimum height of the view.
11006 */
11007 protected int getSuggestedMinimumHeight() {
11008 int suggestedMinHeight = mMinHeight;
11009
11010 if (mBGDrawable != null) {
11011 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11012 if (suggestedMinHeight < bgMinHeight) {
11013 suggestedMinHeight = bgMinHeight;
11014 }
11015 }
11016
11017 return suggestedMinHeight;
11018 }
11019
11020 /**
11021 * Returns the suggested minimum width that the view should use. This
11022 * returns the maximum of the view's minimum width)
11023 * and the background's minimum width
11024 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11025 * <p>
11026 * When being used in {@link #onMeasure(int, int)}, the caller should still
11027 * ensure the returned width is within the requirements of the parent.
11028 *
11029 * @return The suggested minimum width of the view.
11030 */
11031 protected int getSuggestedMinimumWidth() {
11032 int suggestedMinWidth = mMinWidth;
11033
11034 if (mBGDrawable != null) {
11035 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11036 if (suggestedMinWidth < bgMinWidth) {
11037 suggestedMinWidth = bgMinWidth;
11038 }
11039 }
11040
11041 return suggestedMinWidth;
11042 }
11043
11044 /**
11045 * Sets the minimum height of the view. It is not guaranteed the view will
11046 * be able to achieve this minimum height (for example, if its parent layout
11047 * constrains it with less available height).
11048 *
11049 * @param minHeight The minimum height the view will try to be.
11050 */
11051 public void setMinimumHeight(int minHeight) {
11052 mMinHeight = minHeight;
11053 }
11054
11055 /**
11056 * Sets the minimum width of the view. It is not guaranteed the view will
11057 * be able to achieve this minimum width (for example, if its parent layout
11058 * constrains it with less available width).
11059 *
11060 * @param minWidth The minimum width the view will try to be.
11061 */
11062 public void setMinimumWidth(int minWidth) {
11063 mMinWidth = minWidth;
11064 }
11065
11066 /**
11067 * Get the animation currently associated with this view.
11068 *
11069 * @return The animation that is currently playing or
11070 * scheduled to play for this view.
11071 */
11072 public Animation getAnimation() {
11073 return mCurrentAnimation;
11074 }
11075
11076 /**
11077 * Start the specified animation now.
11078 *
11079 * @param animation the animation to start now
11080 */
11081 public void startAnimation(Animation animation) {
11082 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11083 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011084 invalidateParentCaches();
11085 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011086 }
11087
11088 /**
11089 * Cancels any animations for this view.
11090 */
11091 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011092 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011093 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011094 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011095 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011096 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 }
11098
11099 /**
11100 * Sets the next animation to play for this view.
11101 * If you want the animation to play immediately, use
11102 * startAnimation. This method provides allows fine-grained
11103 * control over the start time and invalidation, but you
11104 * must make sure that 1) the animation has a start time set, and
11105 * 2) the view will be invalidated when the animation is supposed to
11106 * start.
11107 *
11108 * @param animation The next animation, or null.
11109 */
11110 public void setAnimation(Animation animation) {
11111 mCurrentAnimation = animation;
11112 if (animation != null) {
11113 animation.reset();
11114 }
11115 }
11116
11117 /**
11118 * Invoked by a parent ViewGroup to notify the start of the animation
11119 * currently associated with this view. If you override this method,
11120 * always call super.onAnimationStart();
11121 *
11122 * @see #setAnimation(android.view.animation.Animation)
11123 * @see #getAnimation()
11124 */
11125 protected void onAnimationStart() {
11126 mPrivateFlags |= ANIMATION_STARTED;
11127 }
11128
11129 /**
11130 * Invoked by a parent ViewGroup to notify the end of the animation
11131 * currently associated with this view. If you override this method,
11132 * always call super.onAnimationEnd();
11133 *
11134 * @see #setAnimation(android.view.animation.Animation)
11135 * @see #getAnimation()
11136 */
11137 protected void onAnimationEnd() {
11138 mPrivateFlags &= ~ANIMATION_STARTED;
11139 }
11140
11141 /**
11142 * Invoked if there is a Transform that involves alpha. Subclass that can
11143 * draw themselves with the specified alpha should return true, and then
11144 * respect that alpha when their onDraw() is called. If this returns false
11145 * then the view may be redirected to draw into an offscreen buffer to
11146 * fulfill the request, which will look fine, but may be slower than if the
11147 * subclass handles it internally. The default implementation returns false.
11148 *
11149 * @param alpha The alpha (0..255) to apply to the view's drawing
11150 * @return true if the view can draw with the specified alpha.
11151 */
11152 protected boolean onSetAlpha(int alpha) {
11153 return false;
11154 }
11155
11156 /**
11157 * This is used by the RootView to perform an optimization when
11158 * the view hierarchy contains one or several SurfaceView.
11159 * SurfaceView is always considered transparent, but its children are not,
11160 * therefore all View objects remove themselves from the global transparent
11161 * region (passed as a parameter to this function).
11162 *
11163 * @param region The transparent region for this ViewRoot (window).
11164 *
11165 * @return Returns true if the effective visibility of the view at this
11166 * point is opaque, regardless of the transparent region; returns false
11167 * if it is possible for underlying windows to be seen behind the view.
11168 *
11169 * {@hide}
11170 */
11171 public boolean gatherTransparentRegion(Region region) {
11172 final AttachInfo attachInfo = mAttachInfo;
11173 if (region != null && attachInfo != null) {
11174 final int pflags = mPrivateFlags;
11175 if ((pflags & SKIP_DRAW) == 0) {
11176 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11177 // remove it from the transparent region.
11178 final int[] location = attachInfo.mTransparentLocation;
11179 getLocationInWindow(location);
11180 region.op(location[0], location[1], location[0] + mRight - mLeft,
11181 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11182 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11183 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11184 // exists, so we remove the background drawable's non-transparent
11185 // parts from this transparent region.
11186 applyDrawableToTransparentRegion(mBGDrawable, region);
11187 }
11188 }
11189 return true;
11190 }
11191
11192 /**
11193 * Play a sound effect for this view.
11194 *
11195 * <p>The framework will play sound effects for some built in actions, such as
11196 * clicking, but you may wish to play these effects in your widget,
11197 * for instance, for internal navigation.
11198 *
11199 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11200 * {@link #isSoundEffectsEnabled()} is true.
11201 *
11202 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11203 */
11204 public void playSoundEffect(int soundConstant) {
11205 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11206 return;
11207 }
11208 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11209 }
11210
11211 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011212 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011213 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011214 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011215 *
11216 * <p>The framework will provide haptic feedback for some built in actions,
11217 * such as long presses, but you may wish to provide feedback for your
11218 * own widget.
11219 *
11220 * <p>The feedback will only be performed if
11221 * {@link #isHapticFeedbackEnabled()} is true.
11222 *
11223 * @param feedbackConstant One of the constants defined in
11224 * {@link HapticFeedbackConstants}
11225 */
11226 public boolean performHapticFeedback(int feedbackConstant) {
11227 return performHapticFeedback(feedbackConstant, 0);
11228 }
11229
11230 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011231 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011232 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011233 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011234 *
11235 * @param feedbackConstant One of the constants defined in
11236 * {@link HapticFeedbackConstants}
11237 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11238 */
11239 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11240 if (mAttachInfo == null) {
11241 return false;
11242 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011243 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011244 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011245 && !isHapticFeedbackEnabled()) {
11246 return false;
11247 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011248 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11249 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011250 }
11251
11252 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011253 * Request that the visibility of the status bar be changed.
11254 */
11255 public void setSystemUiVisibility(int visibility) {
11256 if (visibility != mSystemUiVisibility) {
11257 mSystemUiVisibility = visibility;
11258 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11259 mParent.recomputeViewAttributes(this);
11260 }
11261 }
11262 }
11263
11264 /**
11265 * Returns the status bar visibility that this view has requested.
11266 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011267 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011268 return mSystemUiVisibility;
11269 }
11270
11271 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11272 mOnSystemUiVisibilityChangeListener = l;
11273 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11274 mParent.recomputeViewAttributes(this);
11275 }
11276 }
11277
11278 /**
11279 */
11280 public void dispatchSystemUiVisibilityChanged(int visibility) {
11281 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011282 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011283 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011284 }
11285 }
11286
11287 /**
Joe Malin32736f02011-01-19 16:14:20 -080011288 * Creates an image that the system displays during the drag and drop
11289 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11290 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11291 * appearance as the given View. The default also positions the center of the drag shadow
11292 * directly under the touch point. If no View is provided (the constructor with no parameters
11293 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11294 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11295 * default is an invisible drag shadow.
11296 * <p>
11297 * You are not required to use the View you provide to the constructor as the basis of the
11298 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11299 * anything you want as the drag shadow.
11300 * </p>
11301 * <p>
11302 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11303 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11304 * size and position of the drag shadow. It uses this data to construct a
11305 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11306 * so that your application can draw the shadow image in the Canvas.
11307 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011308 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011309 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011310 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011311
11312 /**
Joe Malin32736f02011-01-19 16:14:20 -080011313 * Constructs a shadow image builder based on a View. By default, the resulting drag
11314 * shadow will have the same appearance and dimensions as the View, with the touch point
11315 * over the center of the View.
11316 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011317 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011318 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011319 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011320 }
11321
Christopher Tate17ed60c2011-01-18 12:50:26 -080011322 /**
11323 * Construct a shadow builder object with no associated View. This
11324 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11325 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11326 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011327 * reference to any View object. If they are not overridden, then the result is an
11328 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011329 */
11330 public DragShadowBuilder() {
11331 mView = new WeakReference<View>(null);
11332 }
11333
11334 /**
11335 * Returns the View object that had been passed to the
11336 * {@link #View.DragShadowBuilder(View)}
11337 * constructor. If that View parameter was {@code null} or if the
11338 * {@link #View.DragShadowBuilder()}
11339 * constructor was used to instantiate the builder object, this method will return
11340 * null.
11341 *
11342 * @return The View object associate with this builder object.
11343 */
Chris Tate6b391282010-10-14 15:48:59 -070011344 final public View getView() {
11345 return mView.get();
11346 }
11347
Christopher Tate2c095f32010-10-04 14:13:40 -070011348 /**
Joe Malin32736f02011-01-19 16:14:20 -080011349 * Provides the metrics for the shadow image. These include the dimensions of
11350 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011351 * be centered under the touch location while dragging.
11352 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011353 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011354 * same as the dimensions of the View itself and centers the shadow under
11355 * the touch point.
11356 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011357 *
Joe Malin32736f02011-01-19 16:14:20 -080011358 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11359 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11360 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11361 * image.
11362 *
11363 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11364 * shadow image that should be underneath the touch point during the drag and drop
11365 * operation. Your application must set {@link android.graphics.Point#x} to the
11366 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011367 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011368 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011369 final View view = mView.get();
11370 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011371 shadowSize.set(view.getWidth(), view.getHeight());
11372 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011373 } else {
11374 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11375 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011376 }
11377
11378 /**
Joe Malin32736f02011-01-19 16:14:20 -080011379 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11380 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011381 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011382 *
Joe Malin32736f02011-01-19 16:14:20 -080011383 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011384 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011385 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011386 final View view = mView.get();
11387 if (view != null) {
11388 view.draw(canvas);
11389 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011390 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011391 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011392 }
11393 }
11394
11395 /**
Joe Malin32736f02011-01-19 16:14:20 -080011396 * Starts a drag and drop operation. When your application calls this method, it passes a
11397 * {@link android.view.View.DragShadowBuilder} object to the system. The
11398 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11399 * to get metrics for the drag shadow, and then calls the object's
11400 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11401 * <p>
11402 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11403 * drag events to all the View objects in your application that are currently visible. It does
11404 * this either by calling the View object's drag listener (an implementation of
11405 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11406 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11407 * Both are passed a {@link android.view.DragEvent} object that has a
11408 * {@link android.view.DragEvent#getAction()} value of
11409 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11410 * </p>
11411 * <p>
11412 * Your application can invoke startDrag() on any attached View object. The View object does not
11413 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11414 * be related to the View the user selected for dragging.
11415 * </p>
11416 * @param data A {@link android.content.ClipData} object pointing to the data to be
11417 * transferred by the drag and drop operation.
11418 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11419 * drag shadow.
11420 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11421 * drop operation. This Object is put into every DragEvent object sent by the system during the
11422 * current drag.
11423 * <p>
11424 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11425 * to the target Views. For example, it can contain flags that differentiate between a
11426 * a copy operation and a move operation.
11427 * </p>
11428 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11429 * so the parameter should be set to 0.
11430 * @return {@code true} if the method completes successfully, or
11431 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11432 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011433 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011434 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011435 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011436 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011437 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011438 }
11439 boolean okay = false;
11440
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011441 Point shadowSize = new Point();
11442 Point shadowTouchPoint = new Point();
11443 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011444
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011445 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11446 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11447 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011448 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011449
Chris Tatea32dcf72010-10-14 12:13:50 -070011450 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011451 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11452 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011453 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011454 Surface surface = new Surface();
11455 try {
11456 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011457 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011458 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011459 + " surface=" + surface);
11460 if (token != null) {
11461 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011462 try {
Chris Tate6b391282010-10-14 15:48:59 -070011463 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011464 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011465 } finally {
11466 surface.unlockCanvasAndPost(canvas);
11467 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011468
Christopher Tate407b4e92010-11-30 17:14:08 -080011469 final ViewRoot root = getViewRoot();
11470
11471 // Cache the local state object for delivery with DragEvents
11472 root.setLocalDragState(myLocalState);
11473
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011474 // repurpose 'shadowSize' for the last touch point
11475 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011476
Christopher Tatea53146c2010-09-07 11:57:52 -070011477 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011478 shadowSize.x, shadowSize.y,
11479 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011480 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011481 }
11482 } catch (Exception e) {
11483 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11484 surface.destroy();
11485 }
11486
11487 return okay;
11488 }
11489
Christopher Tatea53146c2010-09-07 11:57:52 -070011490 /**
Joe Malin32736f02011-01-19 16:14:20 -080011491 * Handles drag events sent by the system following a call to
11492 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11493 *<p>
11494 * When the system calls this method, it passes a
11495 * {@link android.view.DragEvent} object. A call to
11496 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11497 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11498 * operation.
11499 * @param event The {@link android.view.DragEvent} sent by the system.
11500 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11501 * in DragEvent, indicating the type of drag event represented by this object.
11502 * @return {@code true} if the method was successful, otherwise {@code false}.
11503 * <p>
11504 * The method should return {@code true} in response to an action type of
11505 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11506 * operation.
11507 * </p>
11508 * <p>
11509 * The method should also return {@code true} in response to an action type of
11510 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11511 * {@code false} if it didn't.
11512 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011513 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011514 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011515 return false;
11516 }
11517
11518 /**
Joe Malin32736f02011-01-19 16:14:20 -080011519 * Detects if this View is enabled and has a drag event listener.
11520 * If both are true, then it calls the drag event listener with the
11521 * {@link android.view.DragEvent} it received. If the drag event listener returns
11522 * {@code true}, then dispatchDragEvent() returns {@code true}.
11523 * <p>
11524 * For all other cases, the method calls the
11525 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11526 * method and returns its result.
11527 * </p>
11528 * <p>
11529 * This ensures that a drag event is always consumed, even if the View does not have a drag
11530 * event listener. However, if the View has a listener and the listener returns true, then
11531 * onDragEvent() is not called.
11532 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011533 */
11534 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011535 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011536 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11537 && mOnDragListener.onDrag(this, event)) {
11538 return true;
11539 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011540 return onDragEvent(event);
11541 }
11542
11543 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011544 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11545 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011546 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011547 */
11548 public void onCloseSystemDialogs(String reason) {
11549 }
Joe Malin32736f02011-01-19 16:14:20 -080011550
Dianne Hackbornffa42482009-09-23 22:20:11 -070011551 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011552 * Given a Drawable whose bounds have been set to draw into this view,
11553 * update a Region being computed for {@link #gatherTransparentRegion} so
11554 * that any non-transparent parts of the Drawable are removed from the
11555 * given transparent region.
11556 *
11557 * @param dr The Drawable whose transparency is to be applied to the region.
11558 * @param region A Region holding the current transparency information,
11559 * where any parts of the region that are set are considered to be
11560 * transparent. On return, this region will be modified to have the
11561 * transparency information reduced by the corresponding parts of the
11562 * Drawable that are not transparent.
11563 * {@hide}
11564 */
11565 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11566 if (DBG) {
11567 Log.i("View", "Getting transparent region for: " + this);
11568 }
11569 final Region r = dr.getTransparentRegion();
11570 final Rect db = dr.getBounds();
11571 final AttachInfo attachInfo = mAttachInfo;
11572 if (r != null && attachInfo != null) {
11573 final int w = getRight()-getLeft();
11574 final int h = getBottom()-getTop();
11575 if (db.left > 0) {
11576 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11577 r.op(0, 0, db.left, h, Region.Op.UNION);
11578 }
11579 if (db.right < w) {
11580 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11581 r.op(db.right, 0, w, h, Region.Op.UNION);
11582 }
11583 if (db.top > 0) {
11584 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11585 r.op(0, 0, w, db.top, Region.Op.UNION);
11586 }
11587 if (db.bottom < h) {
11588 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11589 r.op(0, db.bottom, w, h, Region.Op.UNION);
11590 }
11591 final int[] location = attachInfo.mTransparentLocation;
11592 getLocationInWindow(location);
11593 r.translate(location[0], location[1]);
11594 region.op(r, Region.Op.INTERSECT);
11595 } else {
11596 region.op(db, Region.Op.DIFFERENCE);
11597 }
11598 }
11599
Adam Powelle14579b2009-12-16 18:39:52 -080011600 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011601 mHasPerformedLongPress = false;
11602
11603 if (mPendingCheckForLongPress == null) {
11604 mPendingCheckForLongPress = new CheckForLongPress();
11605 }
11606 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011607 postDelayed(mPendingCheckForLongPress,
11608 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011609 }
11610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011611 /**
11612 * Inflate a view from an XML resource. This convenience method wraps the {@link
11613 * LayoutInflater} class, which provides a full range of options for view inflation.
11614 *
11615 * @param context The Context object for your activity or application.
11616 * @param resource The resource ID to inflate
11617 * @param root A view group that will be the parent. Used to properly inflate the
11618 * layout_* parameters.
11619 * @see LayoutInflater
11620 */
11621 public static View inflate(Context context, int resource, ViewGroup root) {
11622 LayoutInflater factory = LayoutInflater.from(context);
11623 return factory.inflate(resource, root);
11624 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011626 /**
Adam Powell637d3372010-08-25 14:37:03 -070011627 * Scroll the view with standard behavior for scrolling beyond the normal
11628 * content boundaries. Views that call this method should override
11629 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11630 * results of an over-scroll operation.
11631 *
11632 * Views can use this method to handle any touch or fling-based scrolling.
11633 *
11634 * @param deltaX Change in X in pixels
11635 * @param deltaY Change in Y in pixels
11636 * @param scrollX Current X scroll value in pixels before applying deltaX
11637 * @param scrollY Current Y scroll value in pixels before applying deltaY
11638 * @param scrollRangeX Maximum content scroll range along the X axis
11639 * @param scrollRangeY Maximum content scroll range along the Y axis
11640 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11641 * along the X axis.
11642 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11643 * along the Y axis.
11644 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11645 * @return true if scrolling was clamped to an over-scroll boundary along either
11646 * axis, false otherwise.
11647 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011648 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011649 protected boolean overScrollBy(int deltaX, int deltaY,
11650 int scrollX, int scrollY,
11651 int scrollRangeX, int scrollRangeY,
11652 int maxOverScrollX, int maxOverScrollY,
11653 boolean isTouchEvent) {
11654 final int overScrollMode = mOverScrollMode;
11655 final boolean canScrollHorizontal =
11656 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11657 final boolean canScrollVertical =
11658 computeVerticalScrollRange() > computeVerticalScrollExtent();
11659 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11660 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11661 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11662 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11663
11664 int newScrollX = scrollX + deltaX;
11665 if (!overScrollHorizontal) {
11666 maxOverScrollX = 0;
11667 }
11668
11669 int newScrollY = scrollY + deltaY;
11670 if (!overScrollVertical) {
11671 maxOverScrollY = 0;
11672 }
11673
11674 // Clamp values if at the limits and record
11675 final int left = -maxOverScrollX;
11676 final int right = maxOverScrollX + scrollRangeX;
11677 final int top = -maxOverScrollY;
11678 final int bottom = maxOverScrollY + scrollRangeY;
11679
11680 boolean clampedX = false;
11681 if (newScrollX > right) {
11682 newScrollX = right;
11683 clampedX = true;
11684 } else if (newScrollX < left) {
11685 newScrollX = left;
11686 clampedX = true;
11687 }
11688
11689 boolean clampedY = false;
11690 if (newScrollY > bottom) {
11691 newScrollY = bottom;
11692 clampedY = true;
11693 } else if (newScrollY < top) {
11694 newScrollY = top;
11695 clampedY = true;
11696 }
11697
11698 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11699
11700 return clampedX || clampedY;
11701 }
11702
11703 /**
11704 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11705 * respond to the results of an over-scroll operation.
11706 *
11707 * @param scrollX New X scroll value in pixels
11708 * @param scrollY New Y scroll value in pixels
11709 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11710 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11711 */
11712 protected void onOverScrolled(int scrollX, int scrollY,
11713 boolean clampedX, boolean clampedY) {
11714 // Intentionally empty.
11715 }
11716
11717 /**
11718 * Returns the over-scroll mode for this view. The result will be
11719 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11720 * (allow over-scrolling only if the view content is larger than the container),
11721 * or {@link #OVER_SCROLL_NEVER}.
11722 *
11723 * @return This view's over-scroll mode.
11724 */
11725 public int getOverScrollMode() {
11726 return mOverScrollMode;
11727 }
11728
11729 /**
11730 * Set the over-scroll mode for this view. Valid over-scroll modes are
11731 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11732 * (allow over-scrolling only if the view content is larger than the container),
11733 * or {@link #OVER_SCROLL_NEVER}.
11734 *
11735 * Setting the over-scroll mode of a view will have an effect only if the
11736 * view is capable of scrolling.
11737 *
11738 * @param overScrollMode The new over-scroll mode for this view.
11739 */
11740 public void setOverScrollMode(int overScrollMode) {
11741 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11742 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11743 overScrollMode != OVER_SCROLL_NEVER) {
11744 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11745 }
11746 mOverScrollMode = overScrollMode;
11747 }
11748
11749 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011750 * Gets a scale factor that determines the distance the view should scroll
11751 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
11752 * @return The vertical scroll scale factor.
11753 * @hide
11754 */
11755 protected float getVerticalScrollFactor() {
11756 if (mVerticalScrollFactor == 0) {
11757 TypedValue outValue = new TypedValue();
11758 if (!mContext.getTheme().resolveAttribute(
11759 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
11760 throw new IllegalStateException(
11761 "Expected theme to define listPreferredItemHeight.");
11762 }
11763 mVerticalScrollFactor = outValue.getDimension(
11764 mContext.getResources().getDisplayMetrics());
11765 }
11766 return mVerticalScrollFactor;
11767 }
11768
11769 /**
11770 * Gets a scale factor that determines the distance the view should scroll
11771 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
11772 * @return The horizontal scroll scale factor.
11773 * @hide
11774 */
11775 protected float getHorizontalScrollFactor() {
11776 // TODO: Should use something else.
11777 return getVerticalScrollFactor();
11778 }
11779
11780 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011781 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11782 * Each MeasureSpec represents a requirement for either the width or the height.
11783 * A MeasureSpec is comprised of a size and a mode. There are three possible
11784 * modes:
11785 * <dl>
11786 * <dt>UNSPECIFIED</dt>
11787 * <dd>
11788 * The parent has not imposed any constraint on the child. It can be whatever size
11789 * it wants.
11790 * </dd>
11791 *
11792 * <dt>EXACTLY</dt>
11793 * <dd>
11794 * The parent has determined an exact size for the child. The child is going to be
11795 * given those bounds regardless of how big it wants to be.
11796 * </dd>
11797 *
11798 * <dt>AT_MOST</dt>
11799 * <dd>
11800 * The child can be as large as it wants up to the specified size.
11801 * </dd>
11802 * </dl>
11803 *
11804 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11805 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11806 */
11807 public static class MeasureSpec {
11808 private static final int MODE_SHIFT = 30;
11809 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11810
11811 /**
11812 * Measure specification mode: The parent has not imposed any constraint
11813 * on the child. It can be whatever size it wants.
11814 */
11815 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11816
11817 /**
11818 * Measure specification mode: The parent has determined an exact size
11819 * for the child. The child is going to be given those bounds regardless
11820 * of how big it wants to be.
11821 */
11822 public static final int EXACTLY = 1 << MODE_SHIFT;
11823
11824 /**
11825 * Measure specification mode: The child can be as large as it wants up
11826 * to the specified size.
11827 */
11828 public static final int AT_MOST = 2 << MODE_SHIFT;
11829
11830 /**
11831 * Creates a measure specification based on the supplied size and mode.
11832 *
11833 * The mode must always be one of the following:
11834 * <ul>
11835 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11836 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11837 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11838 * </ul>
11839 *
11840 * @param size the size of the measure specification
11841 * @param mode the mode of the measure specification
11842 * @return the measure specification based on size and mode
11843 */
11844 public static int makeMeasureSpec(int size, int mode) {
11845 return size + mode;
11846 }
11847
11848 /**
11849 * Extracts the mode from the supplied measure specification.
11850 *
11851 * @param measureSpec the measure specification to extract the mode from
11852 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11853 * {@link android.view.View.MeasureSpec#AT_MOST} or
11854 * {@link android.view.View.MeasureSpec#EXACTLY}
11855 */
11856 public static int getMode(int measureSpec) {
11857 return (measureSpec & MODE_MASK);
11858 }
11859
11860 /**
11861 * Extracts the size from the supplied measure specification.
11862 *
11863 * @param measureSpec the measure specification to extract the size from
11864 * @return the size in pixels defined in the supplied measure specification
11865 */
11866 public static int getSize(int measureSpec) {
11867 return (measureSpec & ~MODE_MASK);
11868 }
11869
11870 /**
11871 * Returns a String representation of the specified measure
11872 * specification.
11873 *
11874 * @param measureSpec the measure specification to convert to a String
11875 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11876 */
11877 public static String toString(int measureSpec) {
11878 int mode = getMode(measureSpec);
11879 int size = getSize(measureSpec);
11880
11881 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11882
11883 if (mode == UNSPECIFIED)
11884 sb.append("UNSPECIFIED ");
11885 else if (mode == EXACTLY)
11886 sb.append("EXACTLY ");
11887 else if (mode == AT_MOST)
11888 sb.append("AT_MOST ");
11889 else
11890 sb.append(mode).append(" ");
11891
11892 sb.append(size);
11893 return sb.toString();
11894 }
11895 }
11896
11897 class CheckForLongPress implements Runnable {
11898
11899 private int mOriginalWindowAttachCount;
11900
11901 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011902 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011903 && mOriginalWindowAttachCount == mWindowAttachCount) {
11904 if (performLongClick()) {
11905 mHasPerformedLongPress = true;
11906 }
11907 }
11908 }
11909
11910 public void rememberWindowAttachCount() {
11911 mOriginalWindowAttachCount = mWindowAttachCount;
11912 }
11913 }
Joe Malin32736f02011-01-19 16:14:20 -080011914
Adam Powelle14579b2009-12-16 18:39:52 -080011915 private final class CheckForTap implements Runnable {
11916 public void run() {
11917 mPrivateFlags &= ~PREPRESSED;
11918 mPrivateFlags |= PRESSED;
11919 refreshDrawableState();
11920 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11921 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11922 }
11923 }
11924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011925
Adam Powella35d7682010-03-12 14:48:13 -080011926 private final class PerformClick implements Runnable {
11927 public void run() {
11928 performClick();
11929 }
11930 }
11931
Dianne Hackborn63042d62011-01-26 18:56:29 -080011932 /** @hide */
11933 public void hackTurnOffWindowResizeAnim(boolean off) {
11934 mAttachInfo.mTurnOffWindowResizeAnim = off;
11935 }
Joe Malin32736f02011-01-19 16:14:20 -080011936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 /**
Chet Haasea00f3862011-02-22 06:34:40 -080011938 * This method returns a ViewPropertyAnimator object, which can be used to animate
11939 * specific properties on this View.
11940 *
11941 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
11942 */
11943 public ViewPropertyAnimator animate() {
11944 if (mAnimator == null) {
11945 mAnimator = new ViewPropertyAnimator(this);
11946 }
11947 return mAnimator;
11948 }
11949
11950 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011951 * Interface definition for a callback to be invoked when a key event is
11952 * dispatched to this view. The callback will be invoked before the key
11953 * event is given to the view.
11954 */
11955 public interface OnKeyListener {
11956 /**
11957 * Called when a key is dispatched to a view. This allows listeners to
11958 * get a chance to respond before the target view.
11959 *
11960 * @param v The view the key has been dispatched to.
11961 * @param keyCode The code for the physical key that was pressed
11962 * @param event The KeyEvent object containing full information about
11963 * the event.
11964 * @return True if the listener has consumed the event, false otherwise.
11965 */
11966 boolean onKey(View v, int keyCode, KeyEvent event);
11967 }
11968
11969 /**
11970 * Interface definition for a callback to be invoked when a touch event is
11971 * dispatched to this view. The callback will be invoked before the touch
11972 * event is given to the view.
11973 */
11974 public interface OnTouchListener {
11975 /**
11976 * Called when a touch event is dispatched to a view. This allows listeners to
11977 * get a chance to respond before the target view.
11978 *
11979 * @param v The view the touch event has been dispatched to.
11980 * @param event The MotionEvent object containing full information about
11981 * the event.
11982 * @return True if the listener has consumed the event, false otherwise.
11983 */
11984 boolean onTouch(View v, MotionEvent event);
11985 }
11986
11987 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011988 * Interface definition for a callback to be invoked when a generic motion event is
11989 * dispatched to this view. The callback will be invoked before the generic motion
11990 * event is given to the view.
11991 */
11992 public interface OnGenericMotionListener {
11993 /**
11994 * Called when a generic motion event is dispatched to a view. This allows listeners to
11995 * get a chance to respond before the target view.
11996 *
11997 * @param v The view the generic motion event has been dispatched to.
11998 * @param event The MotionEvent object containing full information about
11999 * the event.
12000 * @return True if the listener has consumed the event, false otherwise.
12001 */
12002 boolean onGenericMotion(View v, MotionEvent event);
12003 }
12004
12005 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012006 * Interface definition for a callback to be invoked when a view has been clicked and held.
12007 */
12008 public interface OnLongClickListener {
12009 /**
12010 * Called when a view has been clicked and held.
12011 *
12012 * @param v The view that was clicked and held.
12013 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012014 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012015 */
12016 boolean onLongClick(View v);
12017 }
12018
12019 /**
Chris Tate32affef2010-10-18 15:29:21 -070012020 * Interface definition for a callback to be invoked when a drag is being dispatched
12021 * to this view. The callback will be invoked before the hosting view's own
12022 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12023 * onDrag(event) behavior, it should return 'false' from this callback.
12024 */
12025 public interface OnDragListener {
12026 /**
12027 * Called when a drag event is dispatched to a view. This allows listeners
12028 * to get a chance to override base View behavior.
12029 *
Joe Malin32736f02011-01-19 16:14:20 -080012030 * @param v The View that received the drag event.
12031 * @param event The {@link android.view.DragEvent} object for the drag event.
12032 * @return {@code true} if the drag event was handled successfully, or {@code false}
12033 * if the drag event was not handled. Note that {@code false} will trigger the View
12034 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012035 */
12036 boolean onDrag(View v, DragEvent event);
12037 }
12038
12039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012040 * Interface definition for a callback to be invoked when the focus state of
12041 * a view changed.
12042 */
12043 public interface OnFocusChangeListener {
12044 /**
12045 * Called when the focus state of a view has changed.
12046 *
12047 * @param v The view whose state has changed.
12048 * @param hasFocus The new focus state of v.
12049 */
12050 void onFocusChange(View v, boolean hasFocus);
12051 }
12052
12053 /**
12054 * Interface definition for a callback to be invoked when a view is clicked.
12055 */
12056 public interface OnClickListener {
12057 /**
12058 * Called when a view has been clicked.
12059 *
12060 * @param v The view that was clicked.
12061 */
12062 void onClick(View v);
12063 }
12064
12065 /**
12066 * Interface definition for a callback to be invoked when the context menu
12067 * for this view is being built.
12068 */
12069 public interface OnCreateContextMenuListener {
12070 /**
12071 * Called when the context menu for this view is being built. It is not
12072 * safe to hold onto the menu after this method returns.
12073 *
12074 * @param menu The context menu that is being built
12075 * @param v The view for which the context menu is being built
12076 * @param menuInfo Extra information about the item for which the
12077 * context menu should be shown. This information will vary
12078 * depending on the class of v.
12079 */
12080 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12081 }
12082
Joe Onorato664644d2011-01-23 17:53:23 -080012083 /**
12084 * Interface definition for a callback to be invoked when the status bar changes
12085 * visibility.
12086 *
12087 * @see #setOnSystemUiVisibilityChangeListener
12088 */
12089 public interface OnSystemUiVisibilityChangeListener {
12090 /**
12091 * Called when the status bar changes visibility because of a call to
12092 * {@link #setSystemUiVisibility}.
12093 *
12094 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12095 */
12096 public void onSystemUiVisibilityChange(int visibility);
12097 }
12098
Adam Powell4afd62b2011-02-18 15:02:18 -080012099 /**
12100 * Interface definition for a callback to be invoked when this view is attached
12101 * or detached from its window.
12102 */
12103 public interface OnAttachStateChangeListener {
12104 /**
12105 * Called when the view is attached to a window.
12106 * @param v The view that was attached
12107 */
12108 public void onViewAttachedToWindow(View v);
12109 /**
12110 * Called when the view is detached from a window.
12111 * @param v The view that was detached
12112 */
12113 public void onViewDetachedFromWindow(View v);
12114 }
12115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012116 private final class UnsetPressedState implements Runnable {
12117 public void run() {
12118 setPressed(false);
12119 }
12120 }
12121
12122 /**
12123 * Base class for derived classes that want to save and restore their own
12124 * state in {@link android.view.View#onSaveInstanceState()}.
12125 */
12126 public static class BaseSavedState extends AbsSavedState {
12127 /**
12128 * Constructor used when reading from a parcel. Reads the state of the superclass.
12129 *
12130 * @param source
12131 */
12132 public BaseSavedState(Parcel source) {
12133 super(source);
12134 }
12135
12136 /**
12137 * Constructor called by derived classes when creating their SavedState objects
12138 *
12139 * @param superState The state of the superclass of this view
12140 */
12141 public BaseSavedState(Parcelable superState) {
12142 super(superState);
12143 }
12144
12145 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12146 new Parcelable.Creator<BaseSavedState>() {
12147 public BaseSavedState createFromParcel(Parcel in) {
12148 return new BaseSavedState(in);
12149 }
12150
12151 public BaseSavedState[] newArray(int size) {
12152 return new BaseSavedState[size];
12153 }
12154 };
12155 }
12156
12157 /**
12158 * A set of information given to a view when it is attached to its parent
12159 * window.
12160 */
12161 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 interface Callbacks {
12163 void playSoundEffect(int effectId);
12164 boolean performHapticFeedback(int effectId, boolean always);
12165 }
12166
12167 /**
12168 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12169 * to a Handler. This class contains the target (View) to invalidate and
12170 * the coordinates of the dirty rectangle.
12171 *
12172 * For performance purposes, this class also implements a pool of up to
12173 * POOL_LIMIT objects that get reused. This reduces memory allocations
12174 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012175 */
Romain Guyd928d682009-03-31 17:52:16 -070012176 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012177 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012178 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12179 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012180 public InvalidateInfo newInstance() {
12181 return new InvalidateInfo();
12182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012183
Romain Guyd928d682009-03-31 17:52:16 -070012184 public void onAcquired(InvalidateInfo element) {
12185 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186
Romain Guyd928d682009-03-31 17:52:16 -070012187 public void onReleased(InvalidateInfo element) {
12188 }
12189 }, POOL_LIMIT)
12190 );
12191
12192 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012193
12194 View target;
12195
12196 int left;
12197 int top;
12198 int right;
12199 int bottom;
12200
Romain Guyd928d682009-03-31 17:52:16 -070012201 public void setNextPoolable(InvalidateInfo element) {
12202 mNext = element;
12203 }
12204
12205 public InvalidateInfo getNextPoolable() {
12206 return mNext;
12207 }
12208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012209 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012210 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012211 }
12212
12213 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012214 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012215 }
12216 }
12217
12218 final IWindowSession mSession;
12219
12220 final IWindow mWindow;
12221
12222 final IBinder mWindowToken;
12223
12224 final Callbacks mRootCallbacks;
12225
Chet Haasedaf98e92011-01-10 14:10:36 -080012226 Canvas mHardwareCanvas;
12227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 /**
12229 * The top view of the hierarchy.
12230 */
12231 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233 IBinder mPanelParentWindowToken;
12234 Surface mSurface;
12235
Romain Guyb051e892010-09-28 19:09:36 -070012236 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012237 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012238 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012241 * Scale factor used by the compatibility mode
12242 */
12243 float mApplicationScale;
12244
12245 /**
12246 * Indicates whether the application is in compatibility mode
12247 */
12248 boolean mScalingRequired;
12249
12250 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012251 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12252 */
12253 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012254
Dianne Hackborn63042d62011-01-26 18:56:29 -080012255 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012256 * Left position of this view's window
12257 */
12258 int mWindowLeft;
12259
12260 /**
12261 * Top position of this view's window
12262 */
12263 int mWindowTop;
12264
12265 /**
Adam Powell26153a32010-11-08 15:22:27 -080012266 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012267 */
Adam Powell26153a32010-11-08 15:22:27 -080012268 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012269
12270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012271 * For windows that are full-screen but using insets to layout inside
12272 * of the screen decorations, these are the current insets for the
12273 * content of the window.
12274 */
12275 final Rect mContentInsets = new Rect();
12276
12277 /**
12278 * For windows that are full-screen but using insets to layout inside
12279 * of the screen decorations, these are the current insets for the
12280 * actual visible parts of the window.
12281 */
12282 final Rect mVisibleInsets = new Rect();
12283
12284 /**
12285 * The internal insets given by this window. This value is
12286 * supplied by the client (through
12287 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12288 * be given to the window manager when changed to be used in laying
12289 * out windows behind it.
12290 */
12291 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12292 = new ViewTreeObserver.InternalInsetsInfo();
12293
12294 /**
12295 * All views in the window's hierarchy that serve as scroll containers,
12296 * used to determine if the window can be resized or must be panned
12297 * to adjust for a soft input area.
12298 */
12299 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12300
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012301 final KeyEvent.DispatcherState mKeyDispatchState
12302 = new KeyEvent.DispatcherState();
12303
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012304 /**
12305 * Indicates whether the view's window currently has the focus.
12306 */
12307 boolean mHasWindowFocus;
12308
12309 /**
12310 * The current visibility of the window.
12311 */
12312 int mWindowVisibility;
12313
12314 /**
12315 * Indicates the time at which drawing started to occur.
12316 */
12317 long mDrawingTime;
12318
12319 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012320 * Indicates whether or not ignoring the DIRTY_MASK flags.
12321 */
12322 boolean mIgnoreDirtyState;
12323
12324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012325 * Indicates whether the view's window is currently in touch mode.
12326 */
12327 boolean mInTouchMode;
12328
12329 /**
12330 * Indicates that ViewRoot should trigger a global layout change
12331 * the next time it performs a traversal
12332 */
12333 boolean mRecomputeGlobalAttributes;
12334
12335 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012336 * Set during a traveral if any views want to keep the screen on.
12337 */
12338 boolean mKeepScreenOn;
12339
12340 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012341 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12342 */
12343 int mSystemUiVisibility;
12344
12345 /**
12346 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12347 * attached.
12348 */
12349 boolean mHasSystemUiListeners;
12350
12351 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012352 * Set if the visibility of any views has changed.
12353 */
12354 boolean mViewVisibilityChanged;
12355
12356 /**
12357 * Set to true if a view has been scrolled.
12358 */
12359 boolean mViewScrollChanged;
12360
12361 /**
12362 * Global to the view hierarchy used as a temporary for dealing with
12363 * x/y points in the transparent region computations.
12364 */
12365 final int[] mTransparentLocation = new int[2];
12366
12367 /**
12368 * Global to the view hierarchy used as a temporary for dealing with
12369 * x/y points in the ViewGroup.invalidateChild implementation.
12370 */
12371 final int[] mInvalidateChildLocation = new int[2];
12372
Chet Haasec3aa3612010-06-17 08:50:37 -070012373
12374 /**
12375 * Global to the view hierarchy used as a temporary for dealing with
12376 * x/y location when view is transformed.
12377 */
12378 final float[] mTmpTransformLocation = new float[2];
12379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012380 /**
12381 * The view tree observer used to dispatch global events like
12382 * layout, pre-draw, touch mode change, etc.
12383 */
12384 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12385
12386 /**
12387 * A Canvas used by the view hierarchy to perform bitmap caching.
12388 */
12389 Canvas mCanvas;
12390
12391 /**
12392 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12393 * handler can be used to pump events in the UI events queue.
12394 */
12395 final Handler mHandler;
12396
12397 /**
12398 * Identifier for messages requesting the view to be invalidated.
12399 * Such messages should be sent to {@link #mHandler}.
12400 */
12401 static final int INVALIDATE_MSG = 0x1;
12402
12403 /**
12404 * Identifier for messages requesting the view to invalidate a region.
12405 * Such messages should be sent to {@link #mHandler}.
12406 */
12407 static final int INVALIDATE_RECT_MSG = 0x2;
12408
12409 /**
12410 * Temporary for use in computing invalidate rectangles while
12411 * calling up the hierarchy.
12412 */
12413 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012414
12415 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012416 * Temporary for use in computing hit areas with transformed views
12417 */
12418 final RectF mTmpTransformRect = new RectF();
12419
12420 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012421 * Temporary list for use in collecting focusable descendents of a view.
12422 */
12423 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 /**
12426 * Creates a new set of attachment information with the specified
12427 * events handler and thread.
12428 *
12429 * @param handler the events handler the view must use
12430 */
12431 AttachInfo(IWindowSession session, IWindow window,
12432 Handler handler, Callbacks effectPlayer) {
12433 mSession = session;
12434 mWindow = window;
12435 mWindowToken = window.asBinder();
12436 mHandler = handler;
12437 mRootCallbacks = effectPlayer;
12438 }
12439 }
12440
12441 /**
12442 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12443 * is supported. This avoids keeping too many unused fields in most
12444 * instances of View.</p>
12445 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012446 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012447
Mike Cleronf116bf82009-09-27 19:14:12 -070012448 /**
12449 * Scrollbars are not visible
12450 */
12451 public static final int OFF = 0;
12452
12453 /**
12454 * Scrollbars are visible
12455 */
12456 public static final int ON = 1;
12457
12458 /**
12459 * Scrollbars are fading away
12460 */
12461 public static final int FADING = 2;
12462
12463 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012466 public int scrollBarDefaultDelayBeforeFade;
12467 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468
12469 public int scrollBarSize;
12470 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012471 public float[] interpolatorValues;
12472 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012473
12474 public final Paint paint;
12475 public final Matrix matrix;
12476 public Shader shader;
12477
Mike Cleronf116bf82009-09-27 19:14:12 -070012478 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12479
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012480 private static final float[] OPAQUE = { 255 };
12481 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012482
Mike Cleronf116bf82009-09-27 19:14:12 -070012483 /**
12484 * When fading should start. This time moves into the future every time
12485 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12486 */
12487 public long fadeStartTime;
12488
12489
12490 /**
12491 * The current state of the scrollbars: ON, OFF, or FADING
12492 */
12493 public int state = OFF;
12494
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012495 private int mLastColor;
12496
Mike Cleronf116bf82009-09-27 19:14:12 -070012497 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012498 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12499 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012500 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12501 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012502
12503 paint = new Paint();
12504 matrix = new Matrix();
12505 // use use a height of 1, and then wack the matrix each time we
12506 // actually use it.
12507 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012509 paint.setShader(shader);
12510 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012511 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012512 }
Romain Guy8506ab42009-06-11 17:35:47 -070012513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 public void setFadeColor(int color) {
12515 if (color != 0 && color != mLastColor) {
12516 mLastColor = color;
12517 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012518
Romain Guye55e1a72009-08-27 10:42:26 -070012519 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12520 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012521
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012522 paint.setShader(shader);
12523 // Restore the default transfer mode (src_over)
12524 paint.setXfermode(null);
12525 }
12526 }
Joe Malin32736f02011-01-19 16:14:20 -080012527
Mike Cleronf116bf82009-09-27 19:14:12 -070012528 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012529 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012530 if (now >= fadeStartTime) {
12531
12532 // the animation fades the scrollbars out by changing
12533 // the opacity (alpha) from fully opaque to fully
12534 // transparent
12535 int nextFrame = (int) now;
12536 int framesCount = 0;
12537
12538 Interpolator interpolator = scrollBarInterpolator;
12539
12540 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012541 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012542
12543 // End transparent
12544 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012545 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012546
12547 state = FADING;
12548
12549 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012550 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012551 }
12552 }
12553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012554 }
12555}