blob: 4bc7f39a584c4e0e4fb894cc8b33624de8c4658d [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001307 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001308 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1309 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310
1311 static final int[] VIEW_STATE_IDS = new int[] {
1312 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1313 R.attr.state_selected, VIEW_STATE_SELECTED,
1314 R.attr.state_focused, VIEW_STATE_FOCUSED,
1315 R.attr.state_enabled, VIEW_STATE_ENABLED,
1316 R.attr.state_pressed, VIEW_STATE_PRESSED,
1317 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001319 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001320 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1321 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 };
1323
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001325 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1326 throw new IllegalStateException(
1327 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1328 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001330 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001332 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001334 orderedIds[i * 2] = viewState;
1335 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 }
1337 }
1338 }
Romain Guyb051e892010-09-28 19:09:36 -07001339 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1340 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1341 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 int numBits = Integer.bitCount(i);
1343 int[] set = new int[numBits];
1344 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001345 for (int j = 0; j < orderedIds.length; j += 2) {
1346 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 set[pos++] = orderedIds[j];
1348 }
1349 }
1350 VIEW_STATE_SETS[i] = set;
1351 }
1352
1353 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1354 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1355 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1356 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1357 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1358 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1359 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1361 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1363 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1365 | VIEW_STATE_FOCUSED];
1366 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1367 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1369 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1370 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1371 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1373 | VIEW_STATE_ENABLED];
1374 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1378 | VIEW_STATE_ENABLED];
1379 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1381 | VIEW_STATE_ENABLED];
1382 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1384 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1385
1386 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1387 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1389 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1390 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1391 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1393 | VIEW_STATE_PRESSED];
1394 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1398 | VIEW_STATE_PRESSED];
1399 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1401 | VIEW_STATE_PRESSED];
1402 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1404 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1405 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1409 | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1418 | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1421 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1428 | VIEW_STATE_PRESSED];
1429 }
1430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * Temporary Rect currently for use in setBackground(). This will probably
1433 * be extended in the future to hold our own class with more than just
1434 * a Rect. :)
1435 */
1436 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001437
1438 /**
1439 * Map used to store views' tags.
1440 */
1441 private static WeakHashMap<View, SparseArray<Object>> sTags;
1442
1443 /**
1444 * Lock used to access sTags.
1445 */
1446 private static final Object sTagsLock = new Object();
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 /**
1449 * The animation currently associated with this view.
1450 * @hide
1451 */
1452 protected Animation mCurrentAnimation = null;
1453
1454 /**
1455 * Width as measured during measure pass.
1456 * {@hide}
1457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001458 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001459 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460
1461 /**
1462 * Height as measured during measure pass.
1463 * {@hide}
1464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001465 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001466 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
1468 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001469 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1470 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1471 * its display list. This flag, used only when hw accelerated, allows us to clear the
1472 * flag while retaining this information until it's needed (at getDisplayList() time and
1473 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1474 *
1475 * {@hide}
1476 */
1477 boolean mRecreateDisplayList = false;
1478
1479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * The view's identifier.
1481 * {@hide}
1482 *
1483 * @see #setId(int)
1484 * @see #getId()
1485 */
1486 @ViewDebug.ExportedProperty(resolveId = true)
1487 int mID = NO_ID;
1488
1489 /**
1490 * The view's tag.
1491 * {@hide}
1492 *
1493 * @see #setTag(Object)
1494 * @see #getTag()
1495 */
1496 protected Object mTag;
1497
1498 // for mPrivateFlags:
1499 /** {@hide} */
1500 static final int WANTS_FOCUS = 0x00000001;
1501 /** {@hide} */
1502 static final int FOCUSED = 0x00000002;
1503 /** {@hide} */
1504 static final int SELECTED = 0x00000004;
1505 /** {@hide} */
1506 static final int IS_ROOT_NAMESPACE = 0x00000008;
1507 /** {@hide} */
1508 static final int HAS_BOUNDS = 0x00000010;
1509 /** {@hide} */
1510 static final int DRAWN = 0x00000020;
1511 /**
1512 * When this flag is set, this view is running an animation on behalf of its
1513 * children and should therefore not cancel invalidate requests, even if they
1514 * lie outside of this view's bounds.
1515 *
1516 * {@hide}
1517 */
1518 static final int DRAW_ANIMATION = 0x00000040;
1519 /** {@hide} */
1520 static final int SKIP_DRAW = 0x00000080;
1521 /** {@hide} */
1522 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1523 /** {@hide} */
1524 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1525 /** {@hide} */
1526 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1527 /** {@hide} */
1528 static final int MEASURED_DIMENSION_SET = 0x00000800;
1529 /** {@hide} */
1530 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001531 /** {@hide} */
1532 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 private static final int PRESSED = 0x00004000;
1535
1536 /** {@hide} */
1537 static final int DRAWING_CACHE_VALID = 0x00008000;
1538 /**
1539 * Flag used to indicate that this view should be drawn once more (and only once
1540 * more) after its animation has completed.
1541 * {@hide}
1542 */
1543 static final int ANIMATION_STARTED = 0x00010000;
1544
1545 private static final int SAVE_STATE_CALLED = 0x00020000;
1546
1547 /**
1548 * Indicates that the View returned true when onSetAlpha() was called and that
1549 * the alpha must be restored.
1550 * {@hide}
1551 */
1552 static final int ALPHA_SET = 0x00040000;
1553
1554 /**
1555 * Set by {@link #setScrollContainer(boolean)}.
1556 */
1557 static final int SCROLL_CONTAINER = 0x00080000;
1558
1559 /**
1560 * Set by {@link #setScrollContainer(boolean)}.
1561 */
1562 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1563
1564 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001565 * View flag indicating whether this view was invalidated (fully or partially.)
1566 *
1567 * @hide
1568 */
1569 static final int DIRTY = 0x00200000;
1570
1571 /**
1572 * View flag indicating whether this view was invalidated by an opaque
1573 * invalidate request.
1574 *
1575 * @hide
1576 */
1577 static final int DIRTY_OPAQUE = 0x00400000;
1578
1579 /**
1580 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1581 *
1582 * @hide
1583 */
1584 static final int DIRTY_MASK = 0x00600000;
1585
1586 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001587 * Indicates whether the background is opaque.
1588 *
1589 * @hide
1590 */
1591 static final int OPAQUE_BACKGROUND = 0x00800000;
1592
1593 /**
1594 * Indicates whether the scrollbars are opaque.
1595 *
1596 * @hide
1597 */
1598 static final int OPAQUE_SCROLLBARS = 0x01000000;
1599
1600 /**
1601 * Indicates whether the view is opaque.
1602 *
1603 * @hide
1604 */
1605 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001606
Adam Powelle14579b2009-12-16 18:39:52 -08001607 /**
1608 * Indicates a prepressed state;
1609 * the short time between ACTION_DOWN and recognizing
1610 * a 'real' press. Prepressed is used to recognize quick taps
1611 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001612 *
Adam Powelle14579b2009-12-16 18:39:52 -08001613 * @hide
1614 */
1615 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001616
Adam Powellc9fbaab2010-02-16 17:16:19 -08001617 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001618 * Indicates whether the view is temporarily detached.
1619 *
1620 * @hide
1621 */
1622 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001623
Adam Powell8568c3a2010-04-19 14:26:11 -07001624 /**
1625 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001626 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001627 * @hide
1628 */
1629 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001630
1631 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001632 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1633 * @hide
1634 */
1635 private static final int HOVERED = 0x10000000;
1636
1637 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001638 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1639 * for transform operations
1640 *
1641 * @hide
1642 */
Adam Powellf37df072010-09-17 16:22:49 -07001643 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001644
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001645 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001646 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001647
Chet Haasefd2b0022010-08-06 13:08:56 -07001648 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001649 * Indicates that this view was specifically invalidated, not just dirtied because some
1650 * child view was invalidated. The flag is used to determine when we need to recreate
1651 * a view's display list (as opposed to just returning a reference to its existing
1652 * display list).
1653 *
1654 * @hide
1655 */
1656 static final int INVALIDATED = 0x80000000;
1657
Christopher Tate3d4bf172011-03-28 16:16:46 -07001658 /* Masks for mPrivateFlags2 */
1659
1660 /**
1661 * Indicates that this view has reported that it can accept the current drag's content.
1662 * Cleared when the drag operation concludes.
1663 * @hide
1664 */
1665 static final int DRAG_CAN_ACCEPT = 0x00000001;
1666
1667 /**
1668 * Indicates that this view is currently directly under the drag location in a
1669 * drag-and-drop operation involving content that it can accept. Cleared when
1670 * the drag exits the view, or when the drag operation concludes.
1671 * @hide
1672 */
1673 static final int DRAG_HOVERED = 0x00000002;
1674
1675 /* End of masks for mPrivateFlags2 */
1676
1677 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1678
Chet Haasedaf98e92011-01-10 14:10:36 -08001679 /**
Adam Powell637d3372010-08-25 14:37:03 -07001680 * Always allow a user to over-scroll this view, provided it is a
1681 * view that can scroll.
1682 *
1683 * @see #getOverScrollMode()
1684 * @see #setOverScrollMode(int)
1685 */
1686 public static final int OVER_SCROLL_ALWAYS = 0;
1687
1688 /**
1689 * Allow a user to over-scroll this view only if the content is large
1690 * enough to meaningfully scroll, provided it is a view that can scroll.
1691 *
1692 * @see #getOverScrollMode()
1693 * @see #setOverScrollMode(int)
1694 */
1695 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1696
1697 /**
1698 * Never allow a user to over-scroll this view.
1699 *
1700 * @see #getOverScrollMode()
1701 * @see #setOverScrollMode(int)
1702 */
1703 public static final int OVER_SCROLL_NEVER = 2;
1704
1705 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001706 * View has requested the status bar to be visible (the default).
1707 *
Joe Malin32736f02011-01-19 16:14:20 -08001708 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001709 */
1710 public static final int STATUS_BAR_VISIBLE = 0;
1711
1712 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001713 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001714 *
Joe Malin32736f02011-01-19 16:14:20 -08001715 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001716 */
1717 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1718
1719 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001720 * @hide
1721 *
1722 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1723 * out of the public fields to keep the undefined bits out of the developer's way.
1724 *
1725 * Flag to make the status bar not expandable. Unless you also
1726 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1727 */
1728 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1729
1730 /**
1731 * @hide
1732 *
1733 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1734 * out of the public fields to keep the undefined bits out of the developer's way.
1735 *
1736 * Flag to hide notification icons and scrolling ticker text.
1737 */
1738 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1739
1740 /**
1741 * @hide
1742 *
1743 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1744 * out of the public fields to keep the undefined bits out of the developer's way.
1745 *
1746 * Flag to disable incoming notification alerts. This will not block
1747 * icons, but it will block sound, vibrating and other visual or aural notifications.
1748 */
1749 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1750
1751 /**
1752 * @hide
1753 *
1754 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1755 * out of the public fields to keep the undefined bits out of the developer's way.
1756 *
1757 * Flag to hide only the scrolling ticker. Note that
1758 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1759 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1760 */
1761 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1762
1763 /**
1764 * @hide
1765 *
1766 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1767 * out of the public fields to keep the undefined bits out of the developer's way.
1768 *
1769 * Flag to hide the center system info area.
1770 */
1771 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1772
1773 /**
1774 * @hide
1775 *
1776 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1777 * out of the public fields to keep the undefined bits out of the developer's way.
1778 *
1779 * Flag to hide only the navigation buttons. Don't use this
1780 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001781 *
1782 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001783 */
1784 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1785
1786 /**
1787 * @hide
1788 *
1789 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1790 * out of the public fields to keep the undefined bits out of the developer's way.
1791 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001792 * Flag to hide only the back button. Don't use this
1793 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1794 */
1795 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1796
1797 /**
1798 * @hide
1799 *
1800 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1801 * out of the public fields to keep the undefined bits out of the developer's way.
1802 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001803 * Flag to hide only the clock. You might use this if your activity has
1804 * its own clock making the status bar's clock redundant.
1805 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001806 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1807
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001808 /**
1809 * @hide
1810 */
1811 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001812
1813 /**
Adam Powell637d3372010-08-25 14:37:03 -07001814 * Controls the over-scroll mode for this view.
1815 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1816 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1817 * and {@link #OVER_SCROLL_NEVER}.
1818 */
1819 private int mOverScrollMode;
1820
1821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 * The parent this view is attached to.
1823 * {@hide}
1824 *
1825 * @see #getParent()
1826 */
1827 protected ViewParent mParent;
1828
1829 /**
1830 * {@hide}
1831 */
1832 AttachInfo mAttachInfo;
1833
1834 /**
1835 * {@hide}
1836 */
Romain Guy809a7f62009-05-14 15:44:42 -07001837 @ViewDebug.ExportedProperty(flagMapping = {
1838 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1839 name = "FORCE_LAYOUT"),
1840 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1841 name = "LAYOUT_REQUIRED"),
1842 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001843 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001844 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1845 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1846 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1847 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1848 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001850 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
1852 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001853 * This view's request for the visibility of the status bar.
1854 * @hide
1855 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001856 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001857 int mSystemUiVisibility;
1858
1859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * Count of how many windows this view has been attached to.
1861 */
1862 int mWindowAttachCount;
1863
1864 /**
1865 * The layout parameters associated with this view and used by the parent
1866 * {@link android.view.ViewGroup} to determine how this view should be
1867 * laid out.
1868 * {@hide}
1869 */
1870 protected ViewGroup.LayoutParams mLayoutParams;
1871
1872 /**
1873 * The view flags hold various views states.
1874 * {@hide}
1875 */
1876 @ViewDebug.ExportedProperty
1877 int mViewFlags;
1878
1879 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001880 * The transform matrix for the View. This transform is calculated internally
1881 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1882 * is used by default. Do *not* use this variable directly; instead call
1883 * getMatrix(), which will automatically recalculate the matrix if necessary
1884 * to get the correct matrix based on the latest rotation and scale properties.
1885 */
1886 private final Matrix mMatrix = new Matrix();
1887
1888 /**
1889 * The transform matrix for the View. This transform is calculated internally
1890 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1891 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001892 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001893 * to get the correct matrix based on the latest rotation and scale properties.
1894 */
1895 private Matrix mInverseMatrix;
1896
1897 /**
1898 * An internal variable that tracks whether we need to recalculate the
1899 * transform matrix, based on whether the rotation or scaleX/Y properties
1900 * have changed since the matrix was last calculated.
1901 */
Chet Haasea00f3862011-02-22 06:34:40 -08001902 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001903
1904 /**
1905 * An internal variable that tracks whether we need to recalculate the
1906 * transform matrix, based on whether the rotation or scaleX/Y properties
1907 * have changed since the matrix was last calculated.
1908 */
1909 private boolean mInverseMatrixDirty = true;
1910
1911 /**
1912 * A variable that tracks whether we need to recalculate the
1913 * transform matrix, based on whether the rotation or scaleX/Y properties
1914 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001915 * is only valid after a call to updateMatrix() or to a function that
1916 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001917 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001918 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001919
1920 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001921 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1922 */
1923 private Camera mCamera = null;
1924
1925 /**
1926 * This matrix is used when computing the matrix for 3D rotations.
1927 */
1928 private Matrix matrix3D = null;
1929
1930 /**
1931 * These prev values are used to recalculate a centered pivot point when necessary. The
1932 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1933 * set), so thes values are only used then as well.
1934 */
1935 private int mPrevWidth = -1;
1936 private int mPrevHeight = -1;
1937
Joe Malin32736f02011-01-19 16:14:20 -08001938 private boolean mLastIsOpaque;
1939
Chet Haasefd2b0022010-08-06 13:08:56 -07001940 /**
1941 * Convenience value to check for float values that are close enough to zero to be considered
1942 * zero.
1943 */
Romain Guy2542d192010-08-18 11:47:12 -07001944 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001945
1946 /**
1947 * The degrees rotation around the vertical axis through the pivot point.
1948 */
1949 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001950 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001951
1952 /**
1953 * The degrees rotation around the horizontal axis through the pivot point.
1954 */
1955 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001956 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The degrees rotation around the pivot point.
1960 */
1961 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001962 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001963
1964 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001965 * The amount of translation of the object away from its left property (post-layout).
1966 */
1967 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001968 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001969
1970 /**
1971 * The amount of translation of the object away from its top property (post-layout).
1972 */
1973 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001974 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001975
1976 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001977 * The amount of scale in the x direction around the pivot point. A
1978 * value of 1 means no scaling is applied.
1979 */
1980 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001981 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * The amount of scale in the y direction around the pivot point. A
1985 * value of 1 means no scaling is applied.
1986 */
1987 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001988 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001989
1990 /**
1991 * The amount of scale in the x direction around the pivot point. A
1992 * value of 1 means no scaling is applied.
1993 */
1994 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001995 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001996
1997 /**
1998 * The amount of scale in the y direction around the pivot point. A
1999 * value of 1 means no scaling is applied.
2000 */
2001 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002002 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002003
2004 /**
2005 * The opacity of the View. This is a value from 0 to 1, where 0 means
2006 * completely transparent and 1 means completely opaque.
2007 */
2008 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002009 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002010
2011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * The distance in pixels from the left edge of this view's parent
2013 * to the left edge of this view.
2014 * {@hide}
2015 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002016 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 protected int mLeft;
2018 /**
2019 * The distance in pixels from the left edge of this view's parent
2020 * to the right edge of this view.
2021 * {@hide}
2022 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002023 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 protected int mRight;
2025 /**
2026 * The distance in pixels from the top edge of this view's parent
2027 * to the top edge of this view.
2028 * {@hide}
2029 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002030 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 protected int mTop;
2032 /**
2033 * The distance in pixels from the top edge of this view's parent
2034 * to the bottom edge of this view.
2035 * {@hide}
2036 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002037 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 protected int mBottom;
2039
2040 /**
2041 * The offset, in pixels, by which the content of this view is scrolled
2042 * horizontally.
2043 * {@hide}
2044 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002045 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 protected int mScrollX;
2047 /**
2048 * The offset, in pixels, by which the content of this view is scrolled
2049 * vertically.
2050 * {@hide}
2051 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002052 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 protected int mScrollY;
2054
2055 /**
2056 * The left padding in pixels, that is the distance in pixels between the
2057 * left edge of this view and the left edge of its content.
2058 * {@hide}
2059 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002060 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 protected int mPaddingLeft;
2062 /**
2063 * The right padding in pixels, that is the distance in pixels between the
2064 * right edge of this view and the right edge of its content.
2065 * {@hide}
2066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002067 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 protected int mPaddingRight;
2069 /**
2070 * The top padding in pixels, that is the distance in pixels between the
2071 * top edge of this view and the top edge of its content.
2072 * {@hide}
2073 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002074 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 protected int mPaddingTop;
2076 /**
2077 * The bottom padding in pixels, that is the distance in pixels between the
2078 * bottom edge of this view and the bottom edge of its content.
2079 * {@hide}
2080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002081 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 protected int mPaddingBottom;
2083
2084 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002085 * Briefly describes the view and is primarily used for accessibility support.
2086 */
2087 private CharSequence mContentDescription;
2088
2089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 * Cache the paddingRight set by the user to append to the scrollbar's size.
2091 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002092 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 int mUserPaddingRight;
2094
2095 /**
2096 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002098 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 int mUserPaddingBottom;
2100
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002101 /**
Adam Powell20232d02010-12-08 21:08:53 -08002102 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2103 */
2104 @ViewDebug.ExportedProperty(category = "padding")
2105 int mUserPaddingLeft;
2106
2107 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002108 * @hide
2109 */
2110 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2111 /**
2112 * @hide
2113 */
2114 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 private Resources mResources = null;
2117
2118 private Drawable mBGDrawable;
2119
2120 private int mBackgroundResource;
2121 private boolean mBackgroundSizeChanged;
2122
2123 /**
2124 * Listener used to dispatch focus change events.
2125 * This field should be made private, so it is hidden from the SDK.
2126 * {@hide}
2127 */
2128 protected OnFocusChangeListener mOnFocusChangeListener;
2129
2130 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002131 * Listeners for layout change events.
2132 */
2133 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2134
2135 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002136 * Listeners for attach events.
2137 */
2138 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2139
2140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 * Listener used to dispatch click events.
2142 * This field should be made private, so it is hidden from the SDK.
2143 * {@hide}
2144 */
2145 protected OnClickListener mOnClickListener;
2146
2147 /**
2148 * Listener used to dispatch long click events.
2149 * This field should be made private, so it is hidden from the SDK.
2150 * {@hide}
2151 */
2152 protected OnLongClickListener mOnLongClickListener;
2153
2154 /**
2155 * Listener used to build the context menu.
2156 * This field should be made private, so it is hidden from the SDK.
2157 * {@hide}
2158 */
2159 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2160
2161 private OnKeyListener mOnKeyListener;
2162
2163 private OnTouchListener mOnTouchListener;
2164
Jeff Brown33bbfd22011-02-24 20:55:35 -08002165 private OnGenericMotionListener mOnGenericMotionListener;
2166
Chris Tate32affef2010-10-18 15:29:21 -07002167 private OnDragListener mOnDragListener;
2168
Joe Onorato664644d2011-01-23 17:53:23 -08002169 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 /**
2172 * The application environment this view lives in.
2173 * This field should be made private, so it is hidden from the SDK.
2174 * {@hide}
2175 */
2176 protected Context mContext;
2177
2178 private ScrollabilityCache mScrollCache;
2179
2180 private int[] mDrawableState = null;
2181
Romain Guy0211a0a2011-02-14 16:34:59 -08002182 /**
2183 * Set to true when drawing cache is enabled and cannot be created.
2184 *
2185 * @hide
2186 */
2187 public boolean mCachingFailed;
2188
Romain Guy02890fd2010-08-06 17:58:44 -07002189 private Bitmap mDrawingCache;
2190 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002191 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002192 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193
2194 /**
2195 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2196 * the user may specify which view to go to next.
2197 */
2198 private int mNextFocusLeftId = View.NO_ID;
2199
2200 /**
2201 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2202 * the user may specify which view to go to next.
2203 */
2204 private int mNextFocusRightId = View.NO_ID;
2205
2206 /**
2207 * When this view has focus and the next focus is {@link #FOCUS_UP},
2208 * the user may specify which view to go to next.
2209 */
2210 private int mNextFocusUpId = View.NO_ID;
2211
2212 /**
2213 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2214 * the user may specify which view to go to next.
2215 */
2216 private int mNextFocusDownId = View.NO_ID;
2217
Jeff Brown4e6319b2010-12-13 10:36:51 -08002218 /**
2219 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2220 * the user may specify which view to go to next.
2221 */
2222 int mNextFocusForwardId = View.NO_ID;
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002225 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002226 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 private UnsetPressedState mUnsetPressedState;
2229
2230 /**
2231 * Whether the long press's action has been invoked. The tap's action is invoked on the
2232 * up event while a long press is invoked as soon as the long press duration is reached, so
2233 * a long press could be performed before the tap is checked, in which case the tap's action
2234 * should not be invoked.
2235 */
2236 private boolean mHasPerformedLongPress;
2237
2238 /**
2239 * The minimum height of the view. We'll try our best to have the height
2240 * of this view to at least this amount.
2241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002242 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 private int mMinHeight;
2244
2245 /**
2246 * The minimum width of the view. We'll try our best to have the width
2247 * of this view to at least this amount.
2248 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002249 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 private int mMinWidth;
2251
2252 /**
2253 * The delegate to handle touch events that are physically in this view
2254 * but should be handled by another view.
2255 */
2256 private TouchDelegate mTouchDelegate = null;
2257
2258 /**
2259 * Solid color to use as a background when creating the drawing cache. Enables
2260 * the cache to use 16 bit bitmaps instead of 32 bit.
2261 */
2262 private int mDrawingCacheBackgroundColor = 0;
2263
2264 /**
2265 * Special tree observer used when mAttachInfo is null.
2266 */
2267 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002268
Adam Powelle14579b2009-12-16 18:39:52 -08002269 /**
2270 * Cache the touch slop from the context that created the view.
2271 */
2272 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002275 * Object that handles automatic animation of view properties.
2276 */
2277 private ViewPropertyAnimator mAnimator = null;
2278
2279 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002280 * Flag indicating that a drag can cross window boundaries. When
2281 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2282 * with this flag set, all visible applications will be able to participate
2283 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002284 *
2285 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002286 */
2287 public static final int DRAG_FLAG_GLOBAL = 1;
2288
2289 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002290 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2291 */
2292 private float mVerticalScrollFactor;
2293
2294 /**
Adam Powell20232d02010-12-08 21:08:53 -08002295 * Position of the vertical scroll bar.
2296 */
2297 private int mVerticalScrollbarPosition;
2298
2299 /**
2300 * Position the scroll bar at the default position as determined by the system.
2301 */
2302 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2303
2304 /**
2305 * Position the scroll bar along the left edge.
2306 */
2307 public static final int SCROLLBAR_POSITION_LEFT = 1;
2308
2309 /**
2310 * Position the scroll bar along the right edge.
2311 */
2312 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2313
2314 /**
Romain Guy171c5922011-01-06 10:04:23 -08002315 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002316 *
2317 * @see #getLayerType()
2318 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002319 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002320 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002321 */
2322 public static final int LAYER_TYPE_NONE = 0;
2323
2324 /**
2325 * <p>Indicates that the view has a software layer. A software layer is backed
2326 * by a bitmap and causes the view to be rendered using Android's software
2327 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002328 *
Romain Guy171c5922011-01-06 10:04:23 -08002329 * <p>Software layers have various usages:</p>
2330 * <p>When the application is not using hardware acceleration, a software layer
2331 * is useful to apply a specific color filter and/or blending mode and/or
2332 * translucency to a view and all its children.</p>
2333 * <p>When the application is using hardware acceleration, a software layer
2334 * is useful to render drawing primitives not supported by the hardware
2335 * accelerated pipeline. It can also be used to cache a complex view tree
2336 * into a texture and reduce the complexity of drawing operations. For instance,
2337 * when animating a complex view tree with a translation, a software layer can
2338 * be used to render the view tree only once.</p>
2339 * <p>Software layers should be avoided when the affected view tree updates
2340 * often. Every update will require to re-render the software layer, which can
2341 * potentially be slow (particularly when hardware acceleration is turned on
2342 * since the layer will have to be uploaded into a hardware texture after every
2343 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002344 *
2345 * @see #getLayerType()
2346 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002347 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002348 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002349 */
2350 public static final int LAYER_TYPE_SOFTWARE = 1;
2351
2352 /**
2353 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2354 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2355 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2356 * rendering pipeline, but only if hardware acceleration is turned on for the
2357 * view hierarchy. When hardware acceleration is turned off, hardware layers
2358 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002359 *
Romain Guy171c5922011-01-06 10:04:23 -08002360 * <p>A hardware layer is useful to apply a specific color filter and/or
2361 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002362 * <p>A hardware layer can be used to cache a complex view tree into a
2363 * texture and reduce the complexity of drawing operations. For instance,
2364 * when animating a complex view tree with a translation, a hardware layer can
2365 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002366 * <p>A hardware layer can also be used to increase the rendering quality when
2367 * rotation transformations are applied on a view. It can also be used to
2368 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002369 *
2370 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002371 * @see #setLayerType(int, android.graphics.Paint)
2372 * @see #LAYER_TYPE_NONE
2373 * @see #LAYER_TYPE_SOFTWARE
2374 */
2375 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002376
Romain Guy3aaff3a2011-01-12 14:18:47 -08002377 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2378 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2379 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2380 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2381 })
Romain Guy171c5922011-01-06 10:04:23 -08002382 int mLayerType = LAYER_TYPE_NONE;
2383 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002384 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002385
2386 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002387 * Consistency verifier for debugging purposes.
2388 * @hide
2389 */
2390 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2391 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2392 new InputEventConsistencyVerifier(this, 0) : null;
2393
2394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * Simple constructor to use when creating a view from code.
2396 *
2397 * @param context The Context the view is running in, through which it can
2398 * access the current theme, resources, etc.
2399 */
2400 public View(Context context) {
2401 mContext = context;
2402 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002403 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002404 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002405 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
2407
2408 /**
2409 * Constructor that is called when inflating a view from XML. This is called
2410 * when a view is being constructed from an XML file, supplying attributes
2411 * that were specified in the XML file. This version uses a default style of
2412 * 0, so the only attribute values applied are those in the Context's Theme
2413 * and the given AttributeSet.
2414 *
2415 * <p>
2416 * The method onFinishInflate() will be called after all children have been
2417 * added.
2418 *
2419 * @param context The Context the view is running in, through which it can
2420 * access the current theme, resources, etc.
2421 * @param attrs The attributes of the XML tag that is inflating the view.
2422 * @see #View(Context, AttributeSet, int)
2423 */
2424 public View(Context context, AttributeSet attrs) {
2425 this(context, attrs, 0);
2426 }
2427
2428 /**
2429 * Perform inflation from XML and apply a class-specific base style. This
2430 * constructor of View allows subclasses to use their own base style when
2431 * they are inflating. For example, a Button class's constructor would call
2432 * this version of the super class constructor and supply
2433 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2434 * the theme's button style to modify all of the base view attributes (in
2435 * particular its background) as well as the Button class's attributes.
2436 *
2437 * @param context The Context the view is running in, through which it can
2438 * access the current theme, resources, etc.
2439 * @param attrs The attributes of the XML tag that is inflating the view.
2440 * @param defStyle The default style to apply to this view. If 0, no style
2441 * will be applied (beyond what is included in the theme). This may
2442 * either be an attribute resource, whose value will be retrieved
2443 * from the current theme, or an explicit style resource.
2444 * @see #View(Context, AttributeSet)
2445 */
2446 public View(Context context, AttributeSet attrs, int defStyle) {
2447 this(context);
2448
2449 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2450 defStyle, 0);
2451
2452 Drawable background = null;
2453
2454 int leftPadding = -1;
2455 int topPadding = -1;
2456 int rightPadding = -1;
2457 int bottomPadding = -1;
2458
2459 int padding = -1;
2460
2461 int viewFlagValues = 0;
2462 int viewFlagMasks = 0;
2463
2464 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 int x = 0;
2467 int y = 0;
2468
Chet Haase73066682010-11-29 15:55:32 -08002469 float tx = 0;
2470 float ty = 0;
2471 float rotation = 0;
2472 float rotationX = 0;
2473 float rotationY = 0;
2474 float sx = 1f;
2475 float sy = 1f;
2476 boolean transformSet = false;
2477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2479
Adam Powell637d3372010-08-25 14:37:03 -07002480 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 final int N = a.getIndexCount();
2482 for (int i = 0; i < N; i++) {
2483 int attr = a.getIndex(i);
2484 switch (attr) {
2485 case com.android.internal.R.styleable.View_background:
2486 background = a.getDrawable(attr);
2487 break;
2488 case com.android.internal.R.styleable.View_padding:
2489 padding = a.getDimensionPixelSize(attr, -1);
2490 break;
2491 case com.android.internal.R.styleable.View_paddingLeft:
2492 leftPadding = a.getDimensionPixelSize(attr, -1);
2493 break;
2494 case com.android.internal.R.styleable.View_paddingTop:
2495 topPadding = a.getDimensionPixelSize(attr, -1);
2496 break;
2497 case com.android.internal.R.styleable.View_paddingRight:
2498 rightPadding = a.getDimensionPixelSize(attr, -1);
2499 break;
2500 case com.android.internal.R.styleable.View_paddingBottom:
2501 bottomPadding = a.getDimensionPixelSize(attr, -1);
2502 break;
2503 case com.android.internal.R.styleable.View_scrollX:
2504 x = a.getDimensionPixelOffset(attr, 0);
2505 break;
2506 case com.android.internal.R.styleable.View_scrollY:
2507 y = a.getDimensionPixelOffset(attr, 0);
2508 break;
Chet Haase73066682010-11-29 15:55:32 -08002509 case com.android.internal.R.styleable.View_alpha:
2510 setAlpha(a.getFloat(attr, 1f));
2511 break;
2512 case com.android.internal.R.styleable.View_transformPivotX:
2513 setPivotX(a.getDimensionPixelOffset(attr, 0));
2514 break;
2515 case com.android.internal.R.styleable.View_transformPivotY:
2516 setPivotY(a.getDimensionPixelOffset(attr, 0));
2517 break;
2518 case com.android.internal.R.styleable.View_translationX:
2519 tx = a.getDimensionPixelOffset(attr, 0);
2520 transformSet = true;
2521 break;
2522 case com.android.internal.R.styleable.View_translationY:
2523 ty = a.getDimensionPixelOffset(attr, 0);
2524 transformSet = true;
2525 break;
2526 case com.android.internal.R.styleable.View_rotation:
2527 rotation = a.getFloat(attr, 0);
2528 transformSet = true;
2529 break;
2530 case com.android.internal.R.styleable.View_rotationX:
2531 rotationX = a.getFloat(attr, 0);
2532 transformSet = true;
2533 break;
2534 case com.android.internal.R.styleable.View_rotationY:
2535 rotationY = a.getFloat(attr, 0);
2536 transformSet = true;
2537 break;
2538 case com.android.internal.R.styleable.View_scaleX:
2539 sx = a.getFloat(attr, 1f);
2540 transformSet = true;
2541 break;
2542 case com.android.internal.R.styleable.View_scaleY:
2543 sy = a.getFloat(attr, 1f);
2544 transformSet = true;
2545 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 case com.android.internal.R.styleable.View_id:
2547 mID = a.getResourceId(attr, NO_ID);
2548 break;
2549 case com.android.internal.R.styleable.View_tag:
2550 mTag = a.getText(attr);
2551 break;
2552 case com.android.internal.R.styleable.View_fitsSystemWindows:
2553 if (a.getBoolean(attr, false)) {
2554 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2555 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_focusable:
2559 if (a.getBoolean(attr, false)) {
2560 viewFlagValues |= FOCUSABLE;
2561 viewFlagMasks |= FOCUSABLE_MASK;
2562 }
2563 break;
2564 case com.android.internal.R.styleable.View_focusableInTouchMode:
2565 if (a.getBoolean(attr, false)) {
2566 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2567 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2568 }
2569 break;
2570 case com.android.internal.R.styleable.View_clickable:
2571 if (a.getBoolean(attr, false)) {
2572 viewFlagValues |= CLICKABLE;
2573 viewFlagMasks |= CLICKABLE;
2574 }
2575 break;
2576 case com.android.internal.R.styleable.View_longClickable:
2577 if (a.getBoolean(attr, false)) {
2578 viewFlagValues |= LONG_CLICKABLE;
2579 viewFlagMasks |= LONG_CLICKABLE;
2580 }
2581 break;
2582 case com.android.internal.R.styleable.View_saveEnabled:
2583 if (!a.getBoolean(attr, true)) {
2584 viewFlagValues |= SAVE_DISABLED;
2585 viewFlagMasks |= SAVE_DISABLED_MASK;
2586 }
2587 break;
2588 case com.android.internal.R.styleable.View_duplicateParentState:
2589 if (a.getBoolean(attr, false)) {
2590 viewFlagValues |= DUPLICATE_PARENT_STATE;
2591 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2592 }
2593 break;
2594 case com.android.internal.R.styleable.View_visibility:
2595 final int visibility = a.getInt(attr, 0);
2596 if (visibility != 0) {
2597 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2598 viewFlagMasks |= VISIBILITY_MASK;
2599 }
2600 break;
2601 case com.android.internal.R.styleable.View_drawingCacheQuality:
2602 final int cacheQuality = a.getInt(attr, 0);
2603 if (cacheQuality != 0) {
2604 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2605 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2606 }
2607 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002608 case com.android.internal.R.styleable.View_contentDescription:
2609 mContentDescription = a.getString(attr);
2610 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2612 if (!a.getBoolean(attr, true)) {
2613 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2614 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2615 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002616 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2618 if (!a.getBoolean(attr, true)) {
2619 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2620 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2621 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 case R.styleable.View_scrollbars:
2624 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2625 if (scrollbars != SCROLLBARS_NONE) {
2626 viewFlagValues |= scrollbars;
2627 viewFlagMasks |= SCROLLBARS_MASK;
2628 initializeScrollbars(a);
2629 }
2630 break;
2631 case R.styleable.View_fadingEdge:
2632 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2633 if (fadingEdge != FADING_EDGE_NONE) {
2634 viewFlagValues |= fadingEdge;
2635 viewFlagMasks |= FADING_EDGE_MASK;
2636 initializeFadingEdge(a);
2637 }
2638 break;
2639 case R.styleable.View_scrollbarStyle:
2640 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2641 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2642 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2643 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2644 }
2645 break;
2646 case R.styleable.View_isScrollContainer:
2647 setScrollContainer = true;
2648 if (a.getBoolean(attr, false)) {
2649 setScrollContainer(true);
2650 }
2651 break;
2652 case com.android.internal.R.styleable.View_keepScreenOn:
2653 if (a.getBoolean(attr, false)) {
2654 viewFlagValues |= KEEP_SCREEN_ON;
2655 viewFlagMasks |= KEEP_SCREEN_ON;
2656 }
2657 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002658 case R.styleable.View_filterTouchesWhenObscured:
2659 if (a.getBoolean(attr, false)) {
2660 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2661 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2662 }
2663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 case R.styleable.View_nextFocusLeft:
2665 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2666 break;
2667 case R.styleable.View_nextFocusRight:
2668 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2669 break;
2670 case R.styleable.View_nextFocusUp:
2671 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2672 break;
2673 case R.styleable.View_nextFocusDown:
2674 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2675 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002676 case R.styleable.View_nextFocusForward:
2677 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2678 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 case R.styleable.View_minWidth:
2680 mMinWidth = a.getDimensionPixelSize(attr, 0);
2681 break;
2682 case R.styleable.View_minHeight:
2683 mMinHeight = a.getDimensionPixelSize(attr, 0);
2684 break;
Romain Guy9a817362009-05-01 10:57:14 -07002685 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002686 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002687 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002688 + "be used within a restricted context");
2689 }
2690
Romain Guy9a817362009-05-01 10:57:14 -07002691 final String handlerName = a.getString(attr);
2692 if (handlerName != null) {
2693 setOnClickListener(new OnClickListener() {
2694 private Method mHandler;
2695
2696 public void onClick(View v) {
2697 if (mHandler == null) {
2698 try {
2699 mHandler = getContext().getClass().getMethod(handlerName,
2700 View.class);
2701 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002702 int id = getId();
2703 String idText = id == NO_ID ? "" : " with id '"
2704 + getContext().getResources().getResourceEntryName(
2705 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002706 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002707 handlerName + "(View) in the activity "
2708 + getContext().getClass() + " for onClick handler"
2709 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002710 }
2711 }
2712
2713 try {
2714 mHandler.invoke(getContext(), View.this);
2715 } catch (IllegalAccessException e) {
2716 throw new IllegalStateException("Could not execute non "
2717 + "public method of the activity", e);
2718 } catch (InvocationTargetException e) {
2719 throw new IllegalStateException("Could not execute "
2720 + "method of the activity", e);
2721 }
2722 }
2723 });
2724 }
2725 break;
Adam Powell637d3372010-08-25 14:37:03 -07002726 case R.styleable.View_overScrollMode:
2727 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2728 break;
Adam Powell20232d02010-12-08 21:08:53 -08002729 case R.styleable.View_verticalScrollbarPosition:
2730 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2731 break;
Romain Guy171c5922011-01-06 10:04:23 -08002732 case R.styleable.View_layerType:
2733 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2734 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 }
2737
Adam Powell637d3372010-08-25 14:37:03 -07002738 setOverScrollMode(overScrollMode);
2739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (background != null) {
2741 setBackgroundDrawable(background);
2742 }
2743
2744 if (padding >= 0) {
2745 leftPadding = padding;
2746 topPadding = padding;
2747 rightPadding = padding;
2748 bottomPadding = padding;
2749 }
2750
2751 // If the user specified the padding (either with android:padding or
2752 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2753 // use the default padding or the padding from the background drawable
2754 // (stored at this point in mPadding*)
2755 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2756 topPadding >= 0 ? topPadding : mPaddingTop,
2757 rightPadding >= 0 ? rightPadding : mPaddingRight,
2758 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2759
2760 if (viewFlagMasks != 0) {
2761 setFlags(viewFlagValues, viewFlagMasks);
2762 }
2763
2764 // Needs to be called after mViewFlags is set
2765 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2766 recomputePadding();
2767 }
2768
2769 if (x != 0 || y != 0) {
2770 scrollTo(x, y);
2771 }
2772
Chet Haase73066682010-11-29 15:55:32 -08002773 if (transformSet) {
2774 setTranslationX(tx);
2775 setTranslationY(ty);
2776 setRotation(rotation);
2777 setRotationX(rotationX);
2778 setRotationY(rotationY);
2779 setScaleX(sx);
2780 setScaleY(sy);
2781 }
2782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2784 setScrollContainer(true);
2785 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002786
2787 computeOpaqueFlags();
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 a.recycle();
2790 }
2791
2792 /**
2793 * Non-public constructor for use in testing
2794 */
2795 View() {
2796 }
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * <p>
2800 * Initializes the fading edges from a given set of styled attributes. This
2801 * method should be called by subclasses that need fading edges and when an
2802 * instance of these subclasses is created programmatically rather than
2803 * being inflated from XML. This method is automatically called when the XML
2804 * is inflated.
2805 * </p>
2806 *
2807 * @param a the styled attributes set to initialize the fading edges from
2808 */
2809 protected void initializeFadingEdge(TypedArray a) {
2810 initScrollCache();
2811
2812 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2813 R.styleable.View_fadingEdgeLength,
2814 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2815 }
2816
2817 /**
2818 * Returns the size of the vertical faded edges used to indicate that more
2819 * content in this view is visible.
2820 *
2821 * @return The size in pixels of the vertical faded edge or 0 if vertical
2822 * faded edges are not enabled for this view.
2823 * @attr ref android.R.styleable#View_fadingEdgeLength
2824 */
2825 public int getVerticalFadingEdgeLength() {
2826 if (isVerticalFadingEdgeEnabled()) {
2827 ScrollabilityCache cache = mScrollCache;
2828 if (cache != null) {
2829 return cache.fadingEdgeLength;
2830 }
2831 }
2832 return 0;
2833 }
2834
2835 /**
2836 * Set the size of the faded edge used to indicate that more content in this
2837 * view is available. Will not change whether the fading edge is enabled; use
2838 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2839 * to enable the fading edge for the vertical or horizontal fading edges.
2840 *
2841 * @param length The size in pixels of the faded edge used to indicate that more
2842 * content in this view is visible.
2843 */
2844 public void setFadingEdgeLength(int length) {
2845 initScrollCache();
2846 mScrollCache.fadingEdgeLength = length;
2847 }
2848
2849 /**
2850 * Returns the size of the horizontal faded edges used to indicate that more
2851 * content in this view is visible.
2852 *
2853 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2854 * faded edges are not enabled for this view.
2855 * @attr ref android.R.styleable#View_fadingEdgeLength
2856 */
2857 public int getHorizontalFadingEdgeLength() {
2858 if (isHorizontalFadingEdgeEnabled()) {
2859 ScrollabilityCache cache = mScrollCache;
2860 if (cache != null) {
2861 return cache.fadingEdgeLength;
2862 }
2863 }
2864 return 0;
2865 }
2866
2867 /**
2868 * Returns the width of the vertical scrollbar.
2869 *
2870 * @return The width in pixels of the vertical scrollbar or 0 if there
2871 * is no vertical scrollbar.
2872 */
2873 public int getVerticalScrollbarWidth() {
2874 ScrollabilityCache cache = mScrollCache;
2875 if (cache != null) {
2876 ScrollBarDrawable scrollBar = cache.scrollBar;
2877 if (scrollBar != null) {
2878 int size = scrollBar.getSize(true);
2879 if (size <= 0) {
2880 size = cache.scrollBarSize;
2881 }
2882 return size;
2883 }
2884 return 0;
2885 }
2886 return 0;
2887 }
2888
2889 /**
2890 * Returns the height of the horizontal scrollbar.
2891 *
2892 * @return The height in pixels of the horizontal scrollbar or 0 if
2893 * there is no horizontal scrollbar.
2894 */
2895 protected int getHorizontalScrollbarHeight() {
2896 ScrollabilityCache cache = mScrollCache;
2897 if (cache != null) {
2898 ScrollBarDrawable scrollBar = cache.scrollBar;
2899 if (scrollBar != null) {
2900 int size = scrollBar.getSize(false);
2901 if (size <= 0) {
2902 size = cache.scrollBarSize;
2903 }
2904 return size;
2905 }
2906 return 0;
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * <p>
2913 * Initializes the scrollbars from a given set of styled attributes. This
2914 * method should be called by subclasses that need scrollbars and when an
2915 * instance of these subclasses is created programmatically rather than
2916 * being inflated from XML. This method is automatically called when the XML
2917 * is inflated.
2918 * </p>
2919 *
2920 * @param a the styled attributes set to initialize the scrollbars from
2921 */
2922 protected void initializeScrollbars(TypedArray a) {
2923 initScrollCache();
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Mike Cleronf116bf82009-09-27 19:14:12 -07002927 if (scrollabilityCache.scrollBar == null) {
2928 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2929 }
Joe Malin32736f02011-01-19 16:14:20 -08002930
Romain Guy8bda2482010-03-02 11:42:11 -08002931 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932
Mike Cleronf116bf82009-09-27 19:14:12 -07002933 if (!fadeScrollbars) {
2934 scrollabilityCache.state = ScrollabilityCache.ON;
2935 }
2936 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002937
2938
Mike Cleronf116bf82009-09-27 19:14:12 -07002939 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2940 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2941 .getScrollBarFadeDuration());
2942 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2943 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2944 ViewConfiguration.getScrollDefaultDelay());
2945
Joe Malin32736f02011-01-19 16:14:20 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2948 com.android.internal.R.styleable.View_scrollbarSize,
2949 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2950
2951 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2952 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2953
2954 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2955 if (thumb != null) {
2956 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2957 }
2958
2959 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2960 false);
2961 if (alwaysDraw) {
2962 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2963 }
2964
2965 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2966 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2967
2968 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2969 if (thumb != null) {
2970 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2971 }
2972
2973 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2974 false);
2975 if (alwaysDraw) {
2976 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2977 }
2978
2979 // Re-apply user/background padding so that scrollbar(s) get added
2980 recomputePadding();
2981 }
2982
2983 /**
2984 * <p>
2985 * Initalizes the scrollability cache if necessary.
2986 * </p>
2987 */
2988 private void initScrollCache() {
2989 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002990 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
2992 }
2993
2994 /**
Adam Powell20232d02010-12-08 21:08:53 -08002995 * Set the position of the vertical scroll bar. Should be one of
2996 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2997 * {@link #SCROLLBAR_POSITION_RIGHT}.
2998 *
2999 * @param position Where the vertical scroll bar should be positioned.
3000 */
3001 public void setVerticalScrollbarPosition(int position) {
3002 if (mVerticalScrollbarPosition != position) {
3003 mVerticalScrollbarPosition = position;
3004 computeOpaqueFlags();
3005 recomputePadding();
3006 }
3007 }
3008
3009 /**
3010 * @return The position where the vertical scroll bar will show, if applicable.
3011 * @see #setVerticalScrollbarPosition(int)
3012 */
3013 public int getVerticalScrollbarPosition() {
3014 return mVerticalScrollbarPosition;
3015 }
3016
3017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * Register a callback to be invoked when focus of this view changed.
3019 *
3020 * @param l The callback that will run.
3021 */
3022 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3023 mOnFocusChangeListener = l;
3024 }
3025
3026 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003027 * Add a listener that will be called when the bounds of the view change due to
3028 * layout processing.
3029 *
3030 * @param listener The listener that will be called when layout bounds change.
3031 */
3032 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3033 if (mOnLayoutChangeListeners == null) {
3034 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3035 }
3036 mOnLayoutChangeListeners.add(listener);
3037 }
3038
3039 /**
3040 * Remove a listener for layout changes.
3041 *
3042 * @param listener The listener for layout bounds change.
3043 */
3044 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3045 if (mOnLayoutChangeListeners == null) {
3046 return;
3047 }
3048 mOnLayoutChangeListeners.remove(listener);
3049 }
3050
3051 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003052 * Add a listener for attach state changes.
3053 *
3054 * This listener will be called whenever this view is attached or detached
3055 * from a window. Remove the listener using
3056 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3057 *
3058 * @param listener Listener to attach
3059 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3060 */
3061 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3062 if (mOnAttachStateChangeListeners == null) {
3063 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3064 }
3065 mOnAttachStateChangeListeners.add(listener);
3066 }
3067
3068 /**
3069 * Remove a listener for attach state changes. The listener will receive no further
3070 * notification of window attach/detach events.
3071 *
3072 * @param listener Listener to remove
3073 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3074 */
3075 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3076 if (mOnAttachStateChangeListeners == null) {
3077 return;
3078 }
3079 mOnAttachStateChangeListeners.remove(listener);
3080 }
3081
3082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Returns the focus-change callback registered for this view.
3084 *
3085 * @return The callback, or null if one is not registered.
3086 */
3087 public OnFocusChangeListener getOnFocusChangeListener() {
3088 return mOnFocusChangeListener;
3089 }
3090
3091 /**
3092 * Register a callback to be invoked when this view is clicked. If this view is not
3093 * clickable, it becomes clickable.
3094 *
3095 * @param l The callback that will run
3096 *
3097 * @see #setClickable(boolean)
3098 */
3099 public void setOnClickListener(OnClickListener l) {
3100 if (!isClickable()) {
3101 setClickable(true);
3102 }
3103 mOnClickListener = l;
3104 }
3105
3106 /**
3107 * Register a callback to be invoked when this view is clicked and held. If this view is not
3108 * long clickable, it becomes long clickable.
3109 *
3110 * @param l The callback that will run
3111 *
3112 * @see #setLongClickable(boolean)
3113 */
3114 public void setOnLongClickListener(OnLongClickListener l) {
3115 if (!isLongClickable()) {
3116 setLongClickable(true);
3117 }
3118 mOnLongClickListener = l;
3119 }
3120
3121 /**
3122 * Register a callback to be invoked when the context menu for this view is
3123 * being built. If this view is not long clickable, it becomes long clickable.
3124 *
3125 * @param l The callback that will run
3126 *
3127 */
3128 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3129 if (!isLongClickable()) {
3130 setLongClickable(true);
3131 }
3132 mOnCreateContextMenuListener = l;
3133 }
3134
3135 /**
3136 * Call this view's OnClickListener, if it is defined.
3137 *
3138 * @return True there was an assigned OnClickListener that was called, false
3139 * otherwise is returned.
3140 */
3141 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003142 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (mOnClickListener != null) {
3145 playSoundEffect(SoundEffectConstants.CLICK);
3146 mOnClickListener.onClick(this);
3147 return true;
3148 }
3149
3150 return false;
3151 }
3152
3153 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003154 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3155 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003157 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 */
3159 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003160 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 boolean handled = false;
3163 if (mOnLongClickListener != null) {
3164 handled = mOnLongClickListener.onLongClick(View.this);
3165 }
3166 if (!handled) {
3167 handled = showContextMenu();
3168 }
3169 if (handled) {
3170 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3171 }
3172 return handled;
3173 }
3174
3175 /**
3176 * Bring up the context menu for this view.
3177 *
3178 * @return Whether a context menu was displayed.
3179 */
3180 public boolean showContextMenu() {
3181 return getParent().showContextMenuForChild(this);
3182 }
3183
3184 /**
Adam Powell6e346362010-07-23 10:18:23 -07003185 * Start an action mode.
3186 *
3187 * @param callback Callback that will control the lifecycle of the action mode
3188 * @return The new action mode if it is started, null otherwise
3189 *
3190 * @see ActionMode
3191 */
3192 public ActionMode startActionMode(ActionMode.Callback callback) {
3193 return getParent().startActionModeForChild(this, callback);
3194 }
3195
3196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 * Register a callback to be invoked when a key is pressed in this view.
3198 * @param l the key listener to attach to this view
3199 */
3200 public void setOnKeyListener(OnKeyListener l) {
3201 mOnKeyListener = l;
3202 }
3203
3204 /**
3205 * Register a callback to be invoked when a touch event is sent to this view.
3206 * @param l the touch listener to attach to this view
3207 */
3208 public void setOnTouchListener(OnTouchListener l) {
3209 mOnTouchListener = l;
3210 }
3211
3212 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003213 * Register a callback to be invoked when a generic motion event is sent to this view.
3214 * @param l the generic motion listener to attach to this view
3215 */
3216 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3217 mOnGenericMotionListener = l;
3218 }
3219
3220 /**
Joe Malin32736f02011-01-19 16:14:20 -08003221 * Register a drag event listener callback object for this View. The parameter is
3222 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3223 * View, the system calls the
3224 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3225 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003226 */
3227 public void setOnDragListener(OnDragListener l) {
3228 mOnDragListener = l;
3229 }
3230
3231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3233 *
3234 * Note: this does not check whether this {@link View} should get focus, it just
3235 * gives it focus no matter what. It should only be called internally by framework
3236 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3237 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003238 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3239 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 * focus moved when requestFocus() is called. It may not always
3241 * apply, in which case use the default View.FOCUS_DOWN.
3242 * @param previouslyFocusedRect The rectangle of the view that had focus
3243 * prior in this View's coordinate system.
3244 */
3245 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3246 if (DBG) {
3247 System.out.println(this + " requestFocus()");
3248 }
3249
3250 if ((mPrivateFlags & FOCUSED) == 0) {
3251 mPrivateFlags |= FOCUSED;
3252
3253 if (mParent != null) {
3254 mParent.requestChildFocus(this, this);
3255 }
3256
3257 onFocusChanged(true, direction, previouslyFocusedRect);
3258 refreshDrawableState();
3259 }
3260 }
3261
3262 /**
3263 * Request that a rectangle of this view be visible on the screen,
3264 * scrolling if necessary just enough.
3265 *
3266 * <p>A View should call this if it maintains some notion of which part
3267 * of its content is interesting. For example, a text editing view
3268 * should call this when its cursor moves.
3269 *
3270 * @param rectangle The rectangle.
3271 * @return Whether any parent scrolled.
3272 */
3273 public boolean requestRectangleOnScreen(Rect rectangle) {
3274 return requestRectangleOnScreen(rectangle, false);
3275 }
3276
3277 /**
3278 * Request that a rectangle of this view be visible on the screen,
3279 * scrolling if necessary just enough.
3280 *
3281 * <p>A View should call this if it maintains some notion of which part
3282 * of its content is interesting. For example, a text editing view
3283 * should call this when its cursor moves.
3284 *
3285 * <p>When <code>immediate</code> is set to true, scrolling will not be
3286 * animated.
3287 *
3288 * @param rectangle The rectangle.
3289 * @param immediate True to forbid animated scrolling, false otherwise
3290 * @return Whether any parent scrolled.
3291 */
3292 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3293 View child = this;
3294 ViewParent parent = mParent;
3295 boolean scrolled = false;
3296 while (parent != null) {
3297 scrolled |= parent.requestChildRectangleOnScreen(child,
3298 rectangle, immediate);
3299
3300 // offset rect so next call has the rectangle in the
3301 // coordinate system of its direct child.
3302 rectangle.offset(child.getLeft(), child.getTop());
3303 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3304
3305 if (!(parent instanceof View)) {
3306 break;
3307 }
Romain Guy8506ab42009-06-11 17:35:47 -07003308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 child = (View) parent;
3310 parent = child.getParent();
3311 }
3312 return scrolled;
3313 }
3314
3315 /**
3316 * Called when this view wants to give up focus. This will cause
3317 * {@link #onFocusChanged} to be called.
3318 */
3319 public void clearFocus() {
3320 if (DBG) {
3321 System.out.println(this + " clearFocus()");
3322 }
3323
3324 if ((mPrivateFlags & FOCUSED) != 0) {
3325 mPrivateFlags &= ~FOCUSED;
3326
3327 if (mParent != null) {
3328 mParent.clearChildFocus(this);
3329 }
3330
3331 onFocusChanged(false, 0, null);
3332 refreshDrawableState();
3333 }
3334 }
3335
3336 /**
3337 * Called to clear the focus of a view that is about to be removed.
3338 * Doesn't call clearChildFocus, which prevents this view from taking
3339 * focus again before it has been removed from the parent
3340 */
3341 void clearFocusForRemoval() {
3342 if ((mPrivateFlags & FOCUSED) != 0) {
3343 mPrivateFlags &= ~FOCUSED;
3344
3345 onFocusChanged(false, 0, null);
3346 refreshDrawableState();
3347 }
3348 }
3349
3350 /**
3351 * Called internally by the view system when a new view is getting focus.
3352 * This is what clears the old focus.
3353 */
3354 void unFocus() {
3355 if (DBG) {
3356 System.out.println(this + " unFocus()");
3357 }
3358
3359 if ((mPrivateFlags & FOCUSED) != 0) {
3360 mPrivateFlags &= ~FOCUSED;
3361
3362 onFocusChanged(false, 0, null);
3363 refreshDrawableState();
3364 }
3365 }
3366
3367 /**
3368 * Returns true if this view has focus iteself, or is the ancestor of the
3369 * view that has focus.
3370 *
3371 * @return True if this view has or contains focus, false otherwise.
3372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 public boolean hasFocus() {
3375 return (mPrivateFlags & FOCUSED) != 0;
3376 }
3377
3378 /**
3379 * Returns true if this view is focusable or if it contains a reachable View
3380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3381 * is a View whose parents do not block descendants focus.
3382 *
3383 * Only {@link #VISIBLE} views are considered focusable.
3384 *
3385 * @return True if the view is focusable or if the view contains a focusable
3386 * View, false otherwise.
3387 *
3388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3389 */
3390 public boolean hasFocusable() {
3391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3392 }
3393
3394 /**
3395 * Called by the view system when the focus state of this view changes.
3396 * When the focus change event is caused by directional navigation, direction
3397 * and previouslyFocusedRect provide insight into where the focus is coming from.
3398 * When overriding, be sure to call up through to the super class so that
3399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 * @param gainFocus True if the View has focus; false otherwise.
3402 * @param direction The direction focus has moved when requestFocus()
3403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3408 * system, of the previously focused view. If applicable, this will be
3409 * passed in as finer grained information about where the focus is coming
3410 * from (in addition to direction). Will be <code>null</code> otherwise.
3411 */
3412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003413 if (gainFocus) {
3414 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3415 }
3416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 InputMethodManager imm = InputMethodManager.peekInstance();
3418 if (!gainFocus) {
3419 if (isPressed()) {
3420 setPressed(false);
3421 }
3422 if (imm != null && mAttachInfo != null
3423 && mAttachInfo.mHasWindowFocus) {
3424 imm.focusOut(this);
3425 }
Romain Guya2431d02009-04-30 16:30:00 -07003426 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 } else if (imm != null && mAttachInfo != null
3428 && mAttachInfo.mHasWindowFocus) {
3429 imm.focusIn(this);
3430 }
Romain Guy8506ab42009-06-11 17:35:47 -07003431
Romain Guy0fd89bf2011-01-26 15:41:30 -08003432 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 if (mOnFocusChangeListener != null) {
3434 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3435 }
Joe Malin32736f02011-01-19 16:14:20 -08003436
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003437 if (mAttachInfo != null) {
3438 mAttachInfo.mKeyDispatchState.reset(this);
3439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
3441
3442 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003443 * {@inheritDoc}
3444 */
3445 public void sendAccessibilityEvent(int eventType) {
3446 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3447 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3448 }
3449 }
3450
3451 /**
3452 * {@inheritDoc}
3453 */
3454 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003455 if (!isShown()) {
3456 return;
3457 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003458
3459 // Populate these here since they are related to the View that
3460 // sends the event and should not be modified while dispatching
3461 // to descendants.
svetoslavganov75986cf2009-05-14 22:28:01 -07003462 event.setClassName(getClass().getName());
3463 event.setPackageName(getContext().getPackageName());
3464 event.setEnabled(isEnabled());
3465 event.setContentDescription(mContentDescription);
3466
3467 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3468 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3469 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3470 event.setItemCount(focusablesTempList.size());
3471 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3472 focusablesTempList.clear();
3473 }
3474
3475 dispatchPopulateAccessibilityEvent(event);
3476
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003477 // In the beginning we called #isShown(), so we know that getParent() is not null.
3478 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003479 }
3480
3481 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003482 * Dispatches an {@link AccessibilityEvent} to the {@link View} children to be populated.
3483 * This method first calls {@link #onPopulateAccessibilityEvent(AccessibilityEvent)}
3484 * on this view allowing it to populate information about itself and also decide
3485 * whether to intercept the population i.e. to prevent its children from populating
3486 * the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07003487 *
3488 * @param event The event.
3489 *
3490 * @return True if the event population was completed.
3491 */
3492 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003493 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003494 return false;
3495 }
3496
3497 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003498 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3499 * giving a chance to this View to populate the accessibility evnet with
3500 * information about itself.
3501 *
3502 * @param event The accessibility event which to populate.
3503 */
3504 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3505
3506 }
3507
3508 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003509 * Gets the {@link View} description. It briefly describes the view and is
3510 * primarily used for accessibility support. Set this property to enable
3511 * better accessibility support for your application. This is especially
3512 * true for views that do not have textual representation (For example,
3513 * ImageButton).
3514 *
3515 * @return The content descriptiopn.
3516 *
3517 * @attr ref android.R.styleable#View_contentDescription
3518 */
3519 public CharSequence getContentDescription() {
3520 return mContentDescription;
3521 }
3522
3523 /**
3524 * Sets the {@link View} description. It briefly describes the view and is
3525 * primarily used for accessibility support. Set this property to enable
3526 * better accessibility support for your application. This is especially
3527 * true for views that do not have textual representation (For example,
3528 * ImageButton).
3529 *
3530 * @param contentDescription The content description.
3531 *
3532 * @attr ref android.R.styleable#View_contentDescription
3533 */
3534 public void setContentDescription(CharSequence contentDescription) {
3535 mContentDescription = contentDescription;
3536 }
3537
3538 /**
Romain Guya2431d02009-04-30 16:30:00 -07003539 * Invoked whenever this view loses focus, either by losing window focus or by losing
3540 * focus within its window. This method can be used to clear any state tied to the
3541 * focus. For instance, if a button is held pressed with the trackball and the window
3542 * loses focus, this method can be used to cancel the press.
3543 *
3544 * Subclasses of View overriding this method should always call super.onFocusLost().
3545 *
3546 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003547 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003548 *
3549 * @hide pending API council approval
3550 */
3551 protected void onFocusLost() {
3552 resetPressedState();
3553 }
3554
3555 private void resetPressedState() {
3556 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3557 return;
3558 }
3559
3560 if (isPressed()) {
3561 setPressed(false);
3562
3563 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003564 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003565 }
3566 }
3567 }
3568
3569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003570 * Returns true if this view has focus
3571 *
3572 * @return True if this view has focus, false otherwise.
3573 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003574 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 public boolean isFocused() {
3576 return (mPrivateFlags & FOCUSED) != 0;
3577 }
3578
3579 /**
3580 * Find the view in the hierarchy rooted at this view that currently has
3581 * focus.
3582 *
3583 * @return The view that currently has focus, or null if no focused view can
3584 * be found.
3585 */
3586 public View findFocus() {
3587 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3588 }
3589
3590 /**
3591 * Change whether this view is one of the set of scrollable containers in
3592 * its window. This will be used to determine whether the window can
3593 * resize or must pan when a soft input area is open -- scrollable
3594 * containers allow the window to use resize mode since the container
3595 * will appropriately shrink.
3596 */
3597 public void setScrollContainer(boolean isScrollContainer) {
3598 if (isScrollContainer) {
3599 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3600 mAttachInfo.mScrollContainers.add(this);
3601 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3602 }
3603 mPrivateFlags |= SCROLL_CONTAINER;
3604 } else {
3605 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3606 mAttachInfo.mScrollContainers.remove(this);
3607 }
3608 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3609 }
3610 }
3611
3612 /**
3613 * Returns the quality of the drawing cache.
3614 *
3615 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3616 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3617 *
3618 * @see #setDrawingCacheQuality(int)
3619 * @see #setDrawingCacheEnabled(boolean)
3620 * @see #isDrawingCacheEnabled()
3621 *
3622 * @attr ref android.R.styleable#View_drawingCacheQuality
3623 */
3624 public int getDrawingCacheQuality() {
3625 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3626 }
3627
3628 /**
3629 * Set the drawing cache quality of this view. This value is used only when the
3630 * drawing cache is enabled
3631 *
3632 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3633 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3634 *
3635 * @see #getDrawingCacheQuality()
3636 * @see #setDrawingCacheEnabled(boolean)
3637 * @see #isDrawingCacheEnabled()
3638 *
3639 * @attr ref android.R.styleable#View_drawingCacheQuality
3640 */
3641 public void setDrawingCacheQuality(int quality) {
3642 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3643 }
3644
3645 /**
3646 * Returns whether the screen should remain on, corresponding to the current
3647 * value of {@link #KEEP_SCREEN_ON}.
3648 *
3649 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3650 *
3651 * @see #setKeepScreenOn(boolean)
3652 *
3653 * @attr ref android.R.styleable#View_keepScreenOn
3654 */
3655 public boolean getKeepScreenOn() {
3656 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3657 }
3658
3659 /**
3660 * Controls whether the screen should remain on, modifying the
3661 * value of {@link #KEEP_SCREEN_ON}.
3662 *
3663 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3664 *
3665 * @see #getKeepScreenOn()
3666 *
3667 * @attr ref android.R.styleable#View_keepScreenOn
3668 */
3669 public void setKeepScreenOn(boolean keepScreenOn) {
3670 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3671 }
3672
3673 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003674 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3675 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003676 *
3677 * @attr ref android.R.styleable#View_nextFocusLeft
3678 */
3679 public int getNextFocusLeftId() {
3680 return mNextFocusLeftId;
3681 }
3682
3683 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003684 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3685 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3686 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003687 *
3688 * @attr ref android.R.styleable#View_nextFocusLeft
3689 */
3690 public void setNextFocusLeftId(int nextFocusLeftId) {
3691 mNextFocusLeftId = nextFocusLeftId;
3692 }
3693
3694 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003695 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3696 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003697 *
3698 * @attr ref android.R.styleable#View_nextFocusRight
3699 */
3700 public int getNextFocusRightId() {
3701 return mNextFocusRightId;
3702 }
3703
3704 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003705 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3706 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3707 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003708 *
3709 * @attr ref android.R.styleable#View_nextFocusRight
3710 */
3711 public void setNextFocusRightId(int nextFocusRightId) {
3712 mNextFocusRightId = nextFocusRightId;
3713 }
3714
3715 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003716 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3717 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 *
3719 * @attr ref android.R.styleable#View_nextFocusUp
3720 */
3721 public int getNextFocusUpId() {
3722 return mNextFocusUpId;
3723 }
3724
3725 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003726 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3727 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3728 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 *
3730 * @attr ref android.R.styleable#View_nextFocusUp
3731 */
3732 public void setNextFocusUpId(int nextFocusUpId) {
3733 mNextFocusUpId = nextFocusUpId;
3734 }
3735
3736 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003737 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3738 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 *
3740 * @attr ref android.R.styleable#View_nextFocusDown
3741 */
3742 public int getNextFocusDownId() {
3743 return mNextFocusDownId;
3744 }
3745
3746 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003747 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3748 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3749 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003750 *
3751 * @attr ref android.R.styleable#View_nextFocusDown
3752 */
3753 public void setNextFocusDownId(int nextFocusDownId) {
3754 mNextFocusDownId = nextFocusDownId;
3755 }
3756
3757 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003758 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3759 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3760 *
3761 * @attr ref android.R.styleable#View_nextFocusForward
3762 */
3763 public int getNextFocusForwardId() {
3764 return mNextFocusForwardId;
3765 }
3766
3767 /**
3768 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3769 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3770 * decide automatically.
3771 *
3772 * @attr ref android.R.styleable#View_nextFocusForward
3773 */
3774 public void setNextFocusForwardId(int nextFocusForwardId) {
3775 mNextFocusForwardId = nextFocusForwardId;
3776 }
3777
3778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003779 * Returns the visibility of this view and all of its ancestors
3780 *
3781 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3782 */
3783 public boolean isShown() {
3784 View current = this;
3785 //noinspection ConstantConditions
3786 do {
3787 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3788 return false;
3789 }
3790 ViewParent parent = current.mParent;
3791 if (parent == null) {
3792 return false; // We are not attached to the view root
3793 }
3794 if (!(parent instanceof View)) {
3795 return true;
3796 }
3797 current = (View) parent;
3798 } while (current != null);
3799
3800 return false;
3801 }
3802
3803 /**
3804 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3805 * is set
3806 *
3807 * @param insets Insets for system windows
3808 *
3809 * @return True if this view applied the insets, false otherwise
3810 */
3811 protected boolean fitSystemWindows(Rect insets) {
3812 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3813 mPaddingLeft = insets.left;
3814 mPaddingTop = insets.top;
3815 mPaddingRight = insets.right;
3816 mPaddingBottom = insets.bottom;
3817 requestLayout();
3818 return true;
3819 }
3820 return false;
3821 }
3822
3823 /**
3824 * Returns the visibility status for this view.
3825 *
3826 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3827 * @attr ref android.R.styleable#View_visibility
3828 */
3829 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003830 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3831 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3832 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003833 })
3834 public int getVisibility() {
3835 return mViewFlags & VISIBILITY_MASK;
3836 }
3837
3838 /**
3839 * Set the enabled state of this view.
3840 *
3841 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3842 * @attr ref android.R.styleable#View_visibility
3843 */
3844 @RemotableViewMethod
3845 public void setVisibility(int visibility) {
3846 setFlags(visibility, VISIBILITY_MASK);
3847 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3848 }
3849
3850 /**
3851 * Returns the enabled status for this view. The interpretation of the
3852 * enabled state varies by subclass.
3853 *
3854 * @return True if this view is enabled, false otherwise.
3855 */
3856 @ViewDebug.ExportedProperty
3857 public boolean isEnabled() {
3858 return (mViewFlags & ENABLED_MASK) == ENABLED;
3859 }
3860
3861 /**
3862 * Set the enabled state of this view. The interpretation of the enabled
3863 * state varies by subclass.
3864 *
3865 * @param enabled True if this view is enabled, false otherwise.
3866 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003867 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003868 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003869 if (enabled == isEnabled()) return;
3870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003871 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3872
3873 /*
3874 * The View most likely has to change its appearance, so refresh
3875 * the drawable state.
3876 */
3877 refreshDrawableState();
3878
3879 // Invalidate too, since the default behavior for views is to be
3880 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003881 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003882 }
3883
3884 /**
3885 * Set whether this view can receive the focus.
3886 *
3887 * Setting this to false will also ensure that this view is not focusable
3888 * in touch mode.
3889 *
3890 * @param focusable If true, this view can receive the focus.
3891 *
3892 * @see #setFocusableInTouchMode(boolean)
3893 * @attr ref android.R.styleable#View_focusable
3894 */
3895 public void setFocusable(boolean focusable) {
3896 if (!focusable) {
3897 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3898 }
3899 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3900 }
3901
3902 /**
3903 * Set whether this view can receive focus while in touch mode.
3904 *
3905 * Setting this to true will also ensure that this view is focusable.
3906 *
3907 * @param focusableInTouchMode If true, this view can receive the focus while
3908 * in touch mode.
3909 *
3910 * @see #setFocusable(boolean)
3911 * @attr ref android.R.styleable#View_focusableInTouchMode
3912 */
3913 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3914 // Focusable in touch mode should always be set before the focusable flag
3915 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3916 // which, in touch mode, will not successfully request focus on this view
3917 // because the focusable in touch mode flag is not set
3918 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3919 if (focusableInTouchMode) {
3920 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3921 }
3922 }
3923
3924 /**
3925 * Set whether this view should have sound effects enabled for events such as
3926 * clicking and touching.
3927 *
3928 * <p>You may wish to disable sound effects for a view if you already play sounds,
3929 * for instance, a dial key that plays dtmf tones.
3930 *
3931 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3932 * @see #isSoundEffectsEnabled()
3933 * @see #playSoundEffect(int)
3934 * @attr ref android.R.styleable#View_soundEffectsEnabled
3935 */
3936 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3937 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3938 }
3939
3940 /**
3941 * @return whether this view should have sound effects enabled for events such as
3942 * clicking and touching.
3943 *
3944 * @see #setSoundEffectsEnabled(boolean)
3945 * @see #playSoundEffect(int)
3946 * @attr ref android.R.styleable#View_soundEffectsEnabled
3947 */
3948 @ViewDebug.ExportedProperty
3949 public boolean isSoundEffectsEnabled() {
3950 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3951 }
3952
3953 /**
3954 * Set whether this view should have haptic feedback for events such as
3955 * long presses.
3956 *
3957 * <p>You may wish to disable haptic feedback if your view already controls
3958 * its own haptic feedback.
3959 *
3960 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3961 * @see #isHapticFeedbackEnabled()
3962 * @see #performHapticFeedback(int)
3963 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3964 */
3965 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3966 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3967 }
3968
3969 /**
3970 * @return whether this view should have haptic feedback enabled for events
3971 * long presses.
3972 *
3973 * @see #setHapticFeedbackEnabled(boolean)
3974 * @see #performHapticFeedback(int)
3975 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3976 */
3977 @ViewDebug.ExportedProperty
3978 public boolean isHapticFeedbackEnabled() {
3979 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3980 }
3981
3982 /**
3983 * If this view doesn't do any drawing on its own, set this flag to
3984 * allow further optimizations. By default, this flag is not set on
3985 * View, but could be set on some View subclasses such as ViewGroup.
3986 *
3987 * Typically, if you override {@link #onDraw} you should clear this flag.
3988 *
3989 * @param willNotDraw whether or not this View draw on its own
3990 */
3991 public void setWillNotDraw(boolean willNotDraw) {
3992 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3993 }
3994
3995 /**
3996 * Returns whether or not this View draws on its own.
3997 *
3998 * @return true if this view has nothing to draw, false otherwise
3999 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004000 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004001 public boolean willNotDraw() {
4002 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4003 }
4004
4005 /**
4006 * When a View's drawing cache is enabled, drawing is redirected to an
4007 * offscreen bitmap. Some views, like an ImageView, must be able to
4008 * bypass this mechanism if they already draw a single bitmap, to avoid
4009 * unnecessary usage of the memory.
4010 *
4011 * @param willNotCacheDrawing true if this view does not cache its
4012 * drawing, false otherwise
4013 */
4014 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4015 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4016 }
4017
4018 /**
4019 * Returns whether or not this View can cache its drawing or not.
4020 *
4021 * @return true if this view does not cache its drawing, false otherwise
4022 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004023 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004024 public boolean willNotCacheDrawing() {
4025 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4026 }
4027
4028 /**
4029 * Indicates whether this view reacts to click events or not.
4030 *
4031 * @return true if the view is clickable, false otherwise
4032 *
4033 * @see #setClickable(boolean)
4034 * @attr ref android.R.styleable#View_clickable
4035 */
4036 @ViewDebug.ExportedProperty
4037 public boolean isClickable() {
4038 return (mViewFlags & CLICKABLE) == CLICKABLE;
4039 }
4040
4041 /**
4042 * Enables or disables click events for this view. When a view
4043 * is clickable it will change its state to "pressed" on every click.
4044 * Subclasses should set the view clickable to visually react to
4045 * user's clicks.
4046 *
4047 * @param clickable true to make the view clickable, false otherwise
4048 *
4049 * @see #isClickable()
4050 * @attr ref android.R.styleable#View_clickable
4051 */
4052 public void setClickable(boolean clickable) {
4053 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4054 }
4055
4056 /**
4057 * Indicates whether this view reacts to long click events or not.
4058 *
4059 * @return true if the view is long clickable, false otherwise
4060 *
4061 * @see #setLongClickable(boolean)
4062 * @attr ref android.R.styleable#View_longClickable
4063 */
4064 public boolean isLongClickable() {
4065 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4066 }
4067
4068 /**
4069 * Enables or disables long click events for this view. When a view is long
4070 * clickable it reacts to the user holding down the button for a longer
4071 * duration than a tap. This event can either launch the listener or a
4072 * context menu.
4073 *
4074 * @param longClickable true to make the view long clickable, false otherwise
4075 * @see #isLongClickable()
4076 * @attr ref android.R.styleable#View_longClickable
4077 */
4078 public void setLongClickable(boolean longClickable) {
4079 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4080 }
4081
4082 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004083 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004084 *
4085 * @see #isClickable()
4086 * @see #setClickable(boolean)
4087 *
4088 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4089 * the View's internal state from a previously set "pressed" state.
4090 */
4091 public void setPressed(boolean pressed) {
4092 if (pressed) {
4093 mPrivateFlags |= PRESSED;
4094 } else {
4095 mPrivateFlags &= ~PRESSED;
4096 }
4097 refreshDrawableState();
4098 dispatchSetPressed(pressed);
4099 }
4100
4101 /**
4102 * Dispatch setPressed to all of this View's children.
4103 *
4104 * @see #setPressed(boolean)
4105 *
4106 * @param pressed The new pressed state
4107 */
4108 protected void dispatchSetPressed(boolean pressed) {
4109 }
4110
4111 /**
4112 * Indicates whether the view is currently in pressed state. Unless
4113 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4114 * the pressed state.
4115 *
4116 * @see #setPressed
4117 * @see #isClickable()
4118 * @see #setClickable(boolean)
4119 *
4120 * @return true if the view is currently pressed, false otherwise
4121 */
4122 public boolean isPressed() {
4123 return (mPrivateFlags & PRESSED) == PRESSED;
4124 }
4125
4126 /**
4127 * Indicates whether this view will save its state (that is,
4128 * whether its {@link #onSaveInstanceState} method will be called).
4129 *
4130 * @return Returns true if the view state saving is enabled, else false.
4131 *
4132 * @see #setSaveEnabled(boolean)
4133 * @attr ref android.R.styleable#View_saveEnabled
4134 */
4135 public boolean isSaveEnabled() {
4136 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4137 }
4138
4139 /**
4140 * Controls whether the saving of this view's state is
4141 * enabled (that is, whether its {@link #onSaveInstanceState} method
4142 * will be called). Note that even if freezing is enabled, the
4143 * view still must have an id assigned to it (via {@link #setId setId()})
4144 * for its state to be saved. This flag can only disable the
4145 * saving of this view; any child views may still have their state saved.
4146 *
4147 * @param enabled Set to false to <em>disable</em> state saving, or true
4148 * (the default) to allow it.
4149 *
4150 * @see #isSaveEnabled()
4151 * @see #setId(int)
4152 * @see #onSaveInstanceState()
4153 * @attr ref android.R.styleable#View_saveEnabled
4154 */
4155 public void setSaveEnabled(boolean enabled) {
4156 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4157 }
4158
Jeff Brown85a31762010-09-01 17:01:00 -07004159 /**
4160 * Gets whether the framework should discard touches when the view's
4161 * window is obscured by another visible window.
4162 * Refer to the {@link View} security documentation for more details.
4163 *
4164 * @return True if touch filtering is enabled.
4165 *
4166 * @see #setFilterTouchesWhenObscured(boolean)
4167 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4168 */
4169 @ViewDebug.ExportedProperty
4170 public boolean getFilterTouchesWhenObscured() {
4171 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4172 }
4173
4174 /**
4175 * Sets whether the framework should discard touches when the view's
4176 * window is obscured by another visible window.
4177 * Refer to the {@link View} security documentation for more details.
4178 *
4179 * @param enabled True if touch filtering should be enabled.
4180 *
4181 * @see #getFilterTouchesWhenObscured
4182 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4183 */
4184 public void setFilterTouchesWhenObscured(boolean enabled) {
4185 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4186 FILTER_TOUCHES_WHEN_OBSCURED);
4187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004188
4189 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004190 * Indicates whether the entire hierarchy under this view will save its
4191 * state when a state saving traversal occurs from its parent. The default
4192 * is true; if false, these views will not be saved unless
4193 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4194 *
4195 * @return Returns true if the view state saving from parent is enabled, else false.
4196 *
4197 * @see #setSaveFromParentEnabled(boolean)
4198 */
4199 public boolean isSaveFromParentEnabled() {
4200 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4201 }
4202
4203 /**
4204 * Controls whether the entire hierarchy under this view will save its
4205 * state when a state saving traversal occurs from its parent. The default
4206 * is true; if false, these views will not be saved unless
4207 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4208 *
4209 * @param enabled Set to false to <em>disable</em> state saving, or true
4210 * (the default) to allow it.
4211 *
4212 * @see #isSaveFromParentEnabled()
4213 * @see #setId(int)
4214 * @see #onSaveInstanceState()
4215 */
4216 public void setSaveFromParentEnabled(boolean enabled) {
4217 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4218 }
4219
4220
4221 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004222 * Returns whether this View is able to take focus.
4223 *
4224 * @return True if this view can take focus, or false otherwise.
4225 * @attr ref android.R.styleable#View_focusable
4226 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004227 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004228 public final boolean isFocusable() {
4229 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4230 }
4231
4232 /**
4233 * When a view is focusable, it may not want to take focus when in touch mode.
4234 * For example, a button would like focus when the user is navigating via a D-pad
4235 * so that the user can click on it, but once the user starts touching the screen,
4236 * the button shouldn't take focus
4237 * @return Whether the view is focusable in touch mode.
4238 * @attr ref android.R.styleable#View_focusableInTouchMode
4239 */
4240 @ViewDebug.ExportedProperty
4241 public final boolean isFocusableInTouchMode() {
4242 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4243 }
4244
4245 /**
4246 * Find the nearest view in the specified direction that can take focus.
4247 * This does not actually give focus to that view.
4248 *
4249 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4250 *
4251 * @return The nearest focusable in the specified direction, or null if none
4252 * can be found.
4253 */
4254 public View focusSearch(int direction) {
4255 if (mParent != null) {
4256 return mParent.focusSearch(this, direction);
4257 } else {
4258 return null;
4259 }
4260 }
4261
4262 /**
4263 * This method is the last chance for the focused view and its ancestors to
4264 * respond to an arrow key. This is called when the focused view did not
4265 * consume the key internally, nor could the view system find a new view in
4266 * the requested direction to give focus to.
4267 *
4268 * @param focused The currently focused view.
4269 * @param direction The direction focus wants to move. One of FOCUS_UP,
4270 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4271 * @return True if the this view consumed this unhandled move.
4272 */
4273 public boolean dispatchUnhandledMove(View focused, int direction) {
4274 return false;
4275 }
4276
4277 /**
4278 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004279 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004280 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004281 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4282 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004283 * @return The user specified next view, or null if there is none.
4284 */
4285 View findUserSetNextFocus(View root, int direction) {
4286 switch (direction) {
4287 case FOCUS_LEFT:
4288 if (mNextFocusLeftId == View.NO_ID) return null;
4289 return findViewShouldExist(root, mNextFocusLeftId);
4290 case FOCUS_RIGHT:
4291 if (mNextFocusRightId == View.NO_ID) return null;
4292 return findViewShouldExist(root, mNextFocusRightId);
4293 case FOCUS_UP:
4294 if (mNextFocusUpId == View.NO_ID) return null;
4295 return findViewShouldExist(root, mNextFocusUpId);
4296 case FOCUS_DOWN:
4297 if (mNextFocusDownId == View.NO_ID) return null;
4298 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004299 case FOCUS_FORWARD:
4300 if (mNextFocusForwardId == View.NO_ID) return null;
4301 return findViewShouldExist(root, mNextFocusForwardId);
4302 case FOCUS_BACKWARD: {
4303 final int id = mID;
4304 return root.findViewByPredicate(new Predicate<View>() {
4305 @Override
4306 public boolean apply(View t) {
4307 return t.mNextFocusForwardId == id;
4308 }
4309 });
4310 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 }
4312 return null;
4313 }
4314
4315 private static View findViewShouldExist(View root, int childViewId) {
4316 View result = root.findViewById(childViewId);
4317 if (result == null) {
4318 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4319 + "by user for id " + childViewId);
4320 }
4321 return result;
4322 }
4323
4324 /**
4325 * Find and return all focusable views that are descendants of this view,
4326 * possibly including this view if it is focusable itself.
4327 *
4328 * @param direction The direction of the focus
4329 * @return A list of focusable views
4330 */
4331 public ArrayList<View> getFocusables(int direction) {
4332 ArrayList<View> result = new ArrayList<View>(24);
4333 addFocusables(result, direction);
4334 return result;
4335 }
4336
4337 /**
4338 * Add any focusable views that are descendants of this view (possibly
4339 * including this view if it is focusable itself) to views. If we are in touch mode,
4340 * only add views that are also focusable in touch mode.
4341 *
4342 * @param views Focusable views found so far
4343 * @param direction The direction of the focus
4344 */
4345 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004346 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348
svetoslavganov75986cf2009-05-14 22:28:01 -07004349 /**
4350 * Adds any focusable views that are descendants of this view (possibly
4351 * including this view if it is focusable itself) to views. This method
4352 * adds all focusable views regardless if we are in touch mode or
4353 * only views focusable in touch mode if we are in touch mode depending on
4354 * the focusable mode paramater.
4355 *
4356 * @param views Focusable views found so far or null if all we are interested is
4357 * the number of focusables.
4358 * @param direction The direction of the focus.
4359 * @param focusableMode The type of focusables to be added.
4360 *
4361 * @see #FOCUSABLES_ALL
4362 * @see #FOCUSABLES_TOUCH_MODE
4363 */
4364 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4365 if (!isFocusable()) {
4366 return;
4367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004368
svetoslavganov75986cf2009-05-14 22:28:01 -07004369 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4370 isInTouchMode() && !isFocusableInTouchMode()) {
4371 return;
4372 }
4373
4374 if (views != null) {
4375 views.add(this);
4376 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004377 }
4378
4379 /**
4380 * Find and return all touchable views that are descendants of this view,
4381 * possibly including this view if it is touchable itself.
4382 *
4383 * @return A list of touchable views
4384 */
4385 public ArrayList<View> getTouchables() {
4386 ArrayList<View> result = new ArrayList<View>();
4387 addTouchables(result);
4388 return result;
4389 }
4390
4391 /**
4392 * Add any touchable views that are descendants of this view (possibly
4393 * including this view if it is touchable itself) to views.
4394 *
4395 * @param views Touchable views found so far
4396 */
4397 public void addTouchables(ArrayList<View> views) {
4398 final int viewFlags = mViewFlags;
4399
4400 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4401 && (viewFlags & ENABLED_MASK) == ENABLED) {
4402 views.add(this);
4403 }
4404 }
4405
4406 /**
4407 * Call this to try to give focus to a specific view or to one of its
4408 * descendants.
4409 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004410 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4411 * false), or if it is focusable and it is not focusable in touch mode
4412 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004413 *
4414 * See also {@link #focusSearch}, which is what you call to say that you
4415 * have focus, and you want your parent to look for the next one.
4416 *
4417 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4418 * {@link #FOCUS_DOWN} and <code>null</code>.
4419 *
4420 * @return Whether this view or one of its descendants actually took focus.
4421 */
4422 public final boolean requestFocus() {
4423 return requestFocus(View.FOCUS_DOWN);
4424 }
4425
4426
4427 /**
4428 * Call this to try to give focus to a specific view or to one of its
4429 * descendants and give it a hint about what direction focus is heading.
4430 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004431 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4432 * false), or if it is focusable and it is not focusable in touch mode
4433 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004434 *
4435 * See also {@link #focusSearch}, which is what you call to say that you
4436 * have focus, and you want your parent to look for the next one.
4437 *
4438 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4439 * <code>null</code> set for the previously focused rectangle.
4440 *
4441 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4442 * @return Whether this view or one of its descendants actually took focus.
4443 */
4444 public final boolean requestFocus(int direction) {
4445 return requestFocus(direction, null);
4446 }
4447
4448 /**
4449 * Call this to try to give focus to a specific view or to one of its descendants
4450 * and give it hints about the direction and a specific rectangle that the focus
4451 * is coming from. The rectangle can help give larger views a finer grained hint
4452 * about where focus is coming from, and therefore, where to show selection, or
4453 * forward focus change internally.
4454 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004455 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4456 * false), or if it is focusable and it is not focusable in touch mode
4457 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004458 *
4459 * A View will not take focus if it is not visible.
4460 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004461 * A View will not take focus if one of its parents has
4462 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4463 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004464 *
4465 * See also {@link #focusSearch}, which is what you call to say that you
4466 * have focus, and you want your parent to look for the next one.
4467 *
4468 * You may wish to override this method if your custom {@link View} has an internal
4469 * {@link View} that it wishes to forward the request to.
4470 *
4471 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4472 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4473 * to give a finer grained hint about where focus is coming from. May be null
4474 * if there is no hint.
4475 * @return Whether this view or one of its descendants actually took focus.
4476 */
4477 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4478 // need to be focusable
4479 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4480 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4481 return false;
4482 }
4483
4484 // need to be focusable in touch mode if in touch mode
4485 if (isInTouchMode() &&
4486 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4487 return false;
4488 }
4489
4490 // need to not have any parents blocking us
4491 if (hasAncestorThatBlocksDescendantFocus()) {
4492 return false;
4493 }
4494
4495 handleFocusGainInternal(direction, previouslyFocusedRect);
4496 return true;
4497 }
4498
Christopher Tate2c095f32010-10-04 14:13:40 -07004499 /** Gets the ViewRoot, or null if not attached. */
4500 /*package*/ ViewRoot getViewRoot() {
4501 View root = getRootView();
4502 return root != null ? (ViewRoot)root.getParent() : null;
4503 }
4504
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 /**
4506 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4507 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4508 * touch mode to request focus when they are touched.
4509 *
4510 * @return Whether this view or one of its descendants actually took focus.
4511 *
4512 * @see #isInTouchMode()
4513 *
4514 */
4515 public final boolean requestFocusFromTouch() {
4516 // Leave touch mode if we need to
4517 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004518 ViewRoot viewRoot = getViewRoot();
4519 if (viewRoot != null) {
4520 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522 }
4523 return requestFocus(View.FOCUS_DOWN);
4524 }
4525
4526 /**
4527 * @return Whether any ancestor of this view blocks descendant focus.
4528 */
4529 private boolean hasAncestorThatBlocksDescendantFocus() {
4530 ViewParent ancestor = mParent;
4531 while (ancestor instanceof ViewGroup) {
4532 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4533 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4534 return true;
4535 } else {
4536 ancestor = vgAncestor.getParent();
4537 }
4538 }
4539 return false;
4540 }
4541
4542 /**
Romain Guya440b002010-02-24 15:57:54 -08004543 * @hide
4544 */
4545 public void dispatchStartTemporaryDetach() {
4546 onStartTemporaryDetach();
4547 }
4548
4549 /**
4550 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004551 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4552 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004553 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 */
4555 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004556 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004557 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004558 }
4559
4560 /**
4561 * @hide
4562 */
4563 public void dispatchFinishTemporaryDetach() {
4564 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004565 }
Romain Guy8506ab42009-06-11 17:35:47 -07004566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 /**
4568 * Called after {@link #onStartTemporaryDetach} when the container is done
4569 * changing the view.
4570 */
4571 public void onFinishTemporaryDetach() {
4572 }
Romain Guy8506ab42009-06-11 17:35:47 -07004573
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 /**
4575 * capture information of this view for later analysis: developement only
4576 * check dynamic switch to make sure we only dump view
4577 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4578 */
4579 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004580 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 return;
4582 }
4583 ViewDebug.dumpCapturedView(subTag, v);
4584 }
4585
4586 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004587 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4588 * for this view's window. Returns null if the view is not currently attached
4589 * to the window. Normally you will not need to use this directly, but
4590 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4591 */
4592 public KeyEvent.DispatcherState getKeyDispatcherState() {
4593 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4594 }
Joe Malin32736f02011-01-19 16:14:20 -08004595
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004596 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004597 * Dispatch a key event before it is processed by any input method
4598 * associated with the view hierarchy. This can be used to intercept
4599 * key events in special situations before the IME consumes them; a
4600 * typical example would be handling the BACK key to update the application's
4601 * UI instead of allowing the IME to see it and close itself.
4602 *
4603 * @param event The key event to be dispatched.
4604 * @return True if the event was handled, false otherwise.
4605 */
4606 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4607 return onKeyPreIme(event.getKeyCode(), event);
4608 }
4609
4610 /**
4611 * Dispatch a key event to the next view on the focus path. This path runs
4612 * from the top of the view tree down to the currently focused view. If this
4613 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4614 * the next node down the focus path. This method also fires any key
4615 * listeners.
4616 *
4617 * @param event The key event to be dispatched.
4618 * @return True if the event was handled, false otherwise.
4619 */
4620 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004621 if (mInputEventConsistencyVerifier != null) {
4622 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624
Romain Guyf607bdc2010-09-10 19:20:06 -07004625 //noinspection SimplifiableIfStatement,deprecation
Joe Onorato43a17652011-04-06 19:22:23 -07004626 if (false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 captureViewInfo("captureViewKeyEvent", this);
4628 }
4629
Jeff Brown21bc5c92011-02-28 18:27:14 -08004630 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004631 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4633 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4634 return true;
4635 }
4636
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004637 if (event.dispatch(this, mAttachInfo != null
4638 ? mAttachInfo.mKeyDispatchState : null, this)) {
4639 return true;
4640 }
4641
4642 if (mInputEventConsistencyVerifier != null) {
4643 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4644 }
4645 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 }
4647
4648 /**
4649 * Dispatches a key shortcut event.
4650 *
4651 * @param event The key event to be dispatched.
4652 * @return True if the event was handled by the view, false otherwise.
4653 */
4654 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4655 return onKeyShortcut(event.getKeyCode(), event);
4656 }
4657
4658 /**
4659 * Pass the touch screen motion event down to the target view, or this
4660 * view if it is the target.
4661 *
4662 * @param event The motion event to be dispatched.
4663 * @return True if the event was handled by the view, false otherwise.
4664 */
4665 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004666 if (mInputEventConsistencyVerifier != null) {
4667 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4668 }
4669
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004670 if (onFilterTouchEventForSecurity(event)) {
4671 //noinspection SimplifiableIfStatement
4672 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4673 mOnTouchListener.onTouch(this, event)) {
4674 return true;
4675 }
4676
4677 if (onTouchEvent(event)) {
4678 return true;
4679 }
Jeff Brown85a31762010-09-01 17:01:00 -07004680 }
4681
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004682 if (mInputEventConsistencyVerifier != null) {
4683 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004685 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004686 }
4687
4688 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004689 * Filter the touch event to apply security policies.
4690 *
4691 * @param event The motion event to be filtered.
4692 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004693 *
Jeff Brown85a31762010-09-01 17:01:00 -07004694 * @see #getFilterTouchesWhenObscured
4695 */
4696 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004697 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004698 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4699 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4700 // Window is obscured, drop this touch.
4701 return false;
4702 }
4703 return true;
4704 }
4705
4706 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004707 * Pass a trackball motion event down to the focused view.
4708 *
4709 * @param event The motion event to be dispatched.
4710 * @return True if the event was handled by the view, false otherwise.
4711 */
4712 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004713 if (mInputEventConsistencyVerifier != null) {
4714 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4715 }
4716
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004718 if (onTrackballEvent(event)) {
4719 return true;
4720 }
4721
4722 if (mInputEventConsistencyVerifier != null) {
4723 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4724 }
4725 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004726 }
4727
4728 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004729 * Dispatch a generic motion event.
4730 * <p>
4731 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4732 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004733 * delivered to the focused view. Hover events are handled specially and are delivered
4734 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004735 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004736 *
4737 * @param event The motion event to be dispatched.
4738 * @return True if the event was handled by the view, false otherwise.
4739 */
4740 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004741 if (mInputEventConsistencyVerifier != null) {
4742 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4743 }
4744
Jeff Browna032cc02011-03-07 16:56:21 -08004745 final int source = event.getSource();
4746 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4747 final int action = event.getAction();
4748 if (action == MotionEvent.ACTION_HOVER_ENTER
4749 || action == MotionEvent.ACTION_HOVER_MOVE
4750 || action == MotionEvent.ACTION_HOVER_EXIT) {
4751 if (dispatchHoverEvent(event)) {
4752 return true;
4753 }
4754 } else if (dispatchGenericPointerEvent(event)) {
4755 return true;
4756 }
4757 } else if (dispatchGenericFocusedEvent(event)) {
4758 return true;
4759 }
4760
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004761 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004762 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4763 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4764 return true;
4765 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004766
4767 if (onGenericMotionEvent(event)) {
4768 return true;
4769 }
4770
4771 if (mInputEventConsistencyVerifier != null) {
4772 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4773 }
4774 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004775 }
4776
4777 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004778 * Dispatch a hover event.
4779 * <p>
4780 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4781 * </p>
4782 *
4783 * @param event The motion event to be dispatched.
4784 * @return True if the event was handled by the view, false otherwise.
4785 * @hide
4786 */
4787 protected boolean dispatchHoverEvent(MotionEvent event) {
4788 return onHoverEvent(event);
4789 }
4790
4791 /**
4792 * Dispatch a generic motion event to the view under the first pointer.
4793 * <p>
4794 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4795 * </p>
4796 *
4797 * @param event The motion event to be dispatched.
4798 * @return True if the event was handled by the view, false otherwise.
4799 * @hide
4800 */
4801 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4802 return false;
4803 }
4804
4805 /**
4806 * Dispatch a generic motion event to the currently focused view.
4807 * <p>
4808 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4809 * </p>
4810 *
4811 * @param event The motion event to be dispatched.
4812 * @return True if the event was handled by the view, false otherwise.
4813 * @hide
4814 */
4815 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4816 return false;
4817 }
4818
4819 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004820 * Dispatch a pointer event.
4821 * <p>
4822 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4823 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4824 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4825 * and should not be expected to handle other pointing device features.
4826 * </p>
4827 *
4828 * @param event The motion event to be dispatched.
4829 * @return True if the event was handled by the view, false otherwise.
4830 * @hide
4831 */
4832 public final boolean dispatchPointerEvent(MotionEvent event) {
4833 if (event.isTouchEvent()) {
4834 return dispatchTouchEvent(event);
4835 } else {
4836 return dispatchGenericMotionEvent(event);
4837 }
4838 }
4839
4840 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004841 * Called when the window containing this view gains or loses window focus.
4842 * ViewGroups should override to route to their children.
4843 *
4844 * @param hasFocus True if the window containing this view now has focus,
4845 * false otherwise.
4846 */
4847 public void dispatchWindowFocusChanged(boolean hasFocus) {
4848 onWindowFocusChanged(hasFocus);
4849 }
4850
4851 /**
4852 * Called when the window containing this view gains or loses focus. Note
4853 * that this is separate from view focus: to receive key events, both
4854 * your view and its window must have focus. If a window is displayed
4855 * on top of yours that takes input focus, then your own window will lose
4856 * focus but the view focus will remain unchanged.
4857 *
4858 * @param hasWindowFocus True if the window containing this view now has
4859 * focus, false otherwise.
4860 */
4861 public void onWindowFocusChanged(boolean hasWindowFocus) {
4862 InputMethodManager imm = InputMethodManager.peekInstance();
4863 if (!hasWindowFocus) {
4864 if (isPressed()) {
4865 setPressed(false);
4866 }
4867 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4868 imm.focusOut(this);
4869 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004870 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004871 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004872 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004873 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4874 imm.focusIn(this);
4875 }
4876 refreshDrawableState();
4877 }
4878
4879 /**
4880 * Returns true if this view is in a window that currently has window focus.
4881 * Note that this is not the same as the view itself having focus.
4882 *
4883 * @return True if this view is in a window that currently has window focus.
4884 */
4885 public boolean hasWindowFocus() {
4886 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4887 }
4888
4889 /**
Adam Powell326d8082009-12-09 15:10:07 -08004890 * Dispatch a view visibility change down the view hierarchy.
4891 * ViewGroups should override to route to their children.
4892 * @param changedView The view whose visibility changed. Could be 'this' or
4893 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004894 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4895 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004896 */
4897 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4898 onVisibilityChanged(changedView, visibility);
4899 }
4900
4901 /**
4902 * Called when the visibility of the view or an ancestor of the view is changed.
4903 * @param changedView The view whose visibility changed. Could be 'this' or
4904 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004905 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4906 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004907 */
4908 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004909 if (visibility == VISIBLE) {
4910 if (mAttachInfo != null) {
4911 initialAwakenScrollBars();
4912 } else {
4913 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4914 }
4915 }
Adam Powell326d8082009-12-09 15:10:07 -08004916 }
4917
4918 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004919 * Dispatch a hint about whether this view is displayed. For instance, when
4920 * a View moves out of the screen, it might receives a display hint indicating
4921 * the view is not displayed. Applications should not <em>rely</em> on this hint
4922 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004923 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004924 * @param hint A hint about whether or not this view is displayed:
4925 * {@link #VISIBLE} or {@link #INVISIBLE}.
4926 */
4927 public void dispatchDisplayHint(int hint) {
4928 onDisplayHint(hint);
4929 }
4930
4931 /**
4932 * Gives this view a hint about whether is displayed or not. For instance, when
4933 * a View moves out of the screen, it might receives a display hint indicating
4934 * the view is not displayed. Applications should not <em>rely</em> on this hint
4935 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004936 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004937 * @param hint A hint about whether or not this view is displayed:
4938 * {@link #VISIBLE} or {@link #INVISIBLE}.
4939 */
4940 protected void onDisplayHint(int hint) {
4941 }
4942
4943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004944 * Dispatch a window visibility change down the view hierarchy.
4945 * ViewGroups should override to route to their children.
4946 *
4947 * @param visibility The new visibility of the window.
4948 *
4949 * @see #onWindowVisibilityChanged
4950 */
4951 public void dispatchWindowVisibilityChanged(int visibility) {
4952 onWindowVisibilityChanged(visibility);
4953 }
4954
4955 /**
4956 * Called when the window containing has change its visibility
4957 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4958 * that this tells you whether or not your window is being made visible
4959 * to the window manager; this does <em>not</em> tell you whether or not
4960 * your window is obscured by other windows on the screen, even if it
4961 * is itself visible.
4962 *
4963 * @param visibility The new visibility of the window.
4964 */
4965 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004966 if (visibility == VISIBLE) {
4967 initialAwakenScrollBars();
4968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 }
4970
4971 /**
4972 * Returns the current visibility of the window this view is attached to
4973 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4974 *
4975 * @return Returns the current visibility of the view's window.
4976 */
4977 public int getWindowVisibility() {
4978 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4979 }
4980
4981 /**
4982 * Retrieve the overall visible display size in which the window this view is
4983 * attached to has been positioned in. This takes into account screen
4984 * decorations above the window, for both cases where the window itself
4985 * is being position inside of them or the window is being placed under
4986 * then and covered insets are used for the window to position its content
4987 * inside. In effect, this tells you the available area where content can
4988 * be placed and remain visible to users.
4989 *
4990 * <p>This function requires an IPC back to the window manager to retrieve
4991 * the requested information, so should not be used in performance critical
4992 * code like drawing.
4993 *
4994 * @param outRect Filled in with the visible display frame. If the view
4995 * is not attached to a window, this is simply the raw display size.
4996 */
4997 public void getWindowVisibleDisplayFrame(Rect outRect) {
4998 if (mAttachInfo != null) {
4999 try {
5000 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5001 } catch (RemoteException e) {
5002 return;
5003 }
5004 // XXX This is really broken, and probably all needs to be done
5005 // in the window manager, and we need to know more about whether
5006 // we want the area behind or in front of the IME.
5007 final Rect insets = mAttachInfo.mVisibleInsets;
5008 outRect.left += insets.left;
5009 outRect.top += insets.top;
5010 outRect.right -= insets.right;
5011 outRect.bottom -= insets.bottom;
5012 return;
5013 }
5014 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005015 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005016 }
5017
5018 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005019 * Dispatch a notification about a resource configuration change down
5020 * the view hierarchy.
5021 * ViewGroups should override to route to their children.
5022 *
5023 * @param newConfig The new resource configuration.
5024 *
5025 * @see #onConfigurationChanged
5026 */
5027 public void dispatchConfigurationChanged(Configuration newConfig) {
5028 onConfigurationChanged(newConfig);
5029 }
5030
5031 /**
5032 * Called when the current configuration of the resources being used
5033 * by the application have changed. You can use this to decide when
5034 * to reload resources that can changed based on orientation and other
5035 * configuration characterstics. You only need to use this if you are
5036 * not relying on the normal {@link android.app.Activity} mechanism of
5037 * recreating the activity instance upon a configuration change.
5038 *
5039 * @param newConfig The new resource configuration.
5040 */
5041 protected void onConfigurationChanged(Configuration newConfig) {
5042 }
5043
5044 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005045 * Private function to aggregate all per-view attributes in to the view
5046 * root.
5047 */
5048 void dispatchCollectViewAttributes(int visibility) {
5049 performCollectViewAttributes(visibility);
5050 }
5051
5052 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005053 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005054 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5055 mAttachInfo.mKeepScreenOn = true;
5056 }
5057 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5058 if (mOnSystemUiVisibilityChangeListener != null) {
5059 mAttachInfo.mHasSystemUiListeners = true;
5060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005061 }
5062 }
5063
5064 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005065 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005066 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005067 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5068 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005069 ai.mRecomputeGlobalAttributes = true;
5070 }
5071 }
5072 }
5073
5074 /**
5075 * Returns whether the device is currently in touch mode. Touch mode is entered
5076 * once the user begins interacting with the device by touch, and affects various
5077 * things like whether focus is always visible to the user.
5078 *
5079 * @return Whether the device is in touch mode.
5080 */
5081 @ViewDebug.ExportedProperty
5082 public boolean isInTouchMode() {
5083 if (mAttachInfo != null) {
5084 return mAttachInfo.mInTouchMode;
5085 } else {
5086 return ViewRoot.isInTouchMode();
5087 }
5088 }
5089
5090 /**
5091 * Returns the context the view is running in, through which it can
5092 * access the current theme, resources, etc.
5093 *
5094 * @return The view's Context.
5095 */
5096 @ViewDebug.CapturedViewProperty
5097 public final Context getContext() {
5098 return mContext;
5099 }
5100
5101 /**
5102 * Handle a key event before it is processed by any input method
5103 * associated with the view hierarchy. This can be used to intercept
5104 * key events in special situations before the IME consumes them; a
5105 * typical example would be handling the BACK key to update the application's
5106 * UI instead of allowing the IME to see it and close itself.
5107 *
5108 * @param keyCode The value in event.getKeyCode().
5109 * @param event Description of the key event.
5110 * @return If you handled the event, return true. If you want to allow the
5111 * event to be handled by the next receiver, return false.
5112 */
5113 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5114 return false;
5115 }
5116
5117 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005118 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5119 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005120 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5121 * is released, if the view is enabled and clickable.
5122 *
5123 * @param keyCode A key code that represents the button pressed, from
5124 * {@link android.view.KeyEvent}.
5125 * @param event The KeyEvent object that defines the button action.
5126 */
5127 public boolean onKeyDown(int keyCode, KeyEvent event) {
5128 boolean result = false;
5129
5130 switch (keyCode) {
5131 case KeyEvent.KEYCODE_DPAD_CENTER:
5132 case KeyEvent.KEYCODE_ENTER: {
5133 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5134 return true;
5135 }
5136 // Long clickable items don't necessarily have to be clickable
5137 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5138 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5139 (event.getRepeatCount() == 0)) {
5140 setPressed(true);
5141 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08005142 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 }
5144 return true;
5145 }
5146 break;
5147 }
5148 }
5149 return result;
5150 }
5151
5152 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005153 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5154 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5155 * the event).
5156 */
5157 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5158 return false;
5159 }
5160
5161 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005162 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5163 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005164 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5165 * {@link KeyEvent#KEYCODE_ENTER} is released.
5166 *
5167 * @param keyCode A key code that represents the button pressed, from
5168 * {@link android.view.KeyEvent}.
5169 * @param event The KeyEvent object that defines the button action.
5170 */
5171 public boolean onKeyUp(int keyCode, KeyEvent event) {
5172 boolean result = false;
5173
5174 switch (keyCode) {
5175 case KeyEvent.KEYCODE_DPAD_CENTER:
5176 case KeyEvent.KEYCODE_ENTER: {
5177 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5178 return true;
5179 }
5180 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5181 setPressed(false);
5182
5183 if (!mHasPerformedLongPress) {
5184 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005185 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005186
5187 result = performClick();
5188 }
5189 }
5190 break;
5191 }
5192 }
5193 return result;
5194 }
5195
5196 /**
5197 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5198 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5199 * the event).
5200 *
5201 * @param keyCode A key code that represents the button pressed, from
5202 * {@link android.view.KeyEvent}.
5203 * @param repeatCount The number of times the action was made.
5204 * @param event The KeyEvent object that defines the button action.
5205 */
5206 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5207 return false;
5208 }
5209
5210 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005211 * Called on the focused view when a key shortcut event is not handled.
5212 * Override this method to implement local key shortcuts for the View.
5213 * Key shortcuts can also be implemented by setting the
5214 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 *
5216 * @param keyCode The value in event.getKeyCode().
5217 * @param event Description of the key event.
5218 * @return If you handled the event, return true. If you want to allow the
5219 * event to be handled by the next receiver, return false.
5220 */
5221 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5222 return false;
5223 }
5224
5225 /**
5226 * Check whether the called view is a text editor, in which case it
5227 * would make sense to automatically display a soft input window for
5228 * it. Subclasses should override this if they implement
5229 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005230 * a call on that method would return a non-null InputConnection, and
5231 * they are really a first-class editor that the user would normally
5232 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005233 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005234 * <p>The default implementation always returns false. This does
5235 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5236 * will not be called or the user can not otherwise perform edits on your
5237 * view; it is just a hint to the system that this is not the primary
5238 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005239 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 * @return Returns true if this view is a text editor, else false.
5241 */
5242 public boolean onCheckIsTextEditor() {
5243 return false;
5244 }
Romain Guy8506ab42009-06-11 17:35:47 -07005245
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 /**
5247 * Create a new InputConnection for an InputMethod to interact
5248 * with the view. The default implementation returns null, since it doesn't
5249 * support input methods. You can override this to implement such support.
5250 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005251 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 * <p>When implementing this, you probably also want to implement
5253 * {@link #onCheckIsTextEditor()} to indicate you will return a
5254 * non-null InputConnection.
5255 *
5256 * @param outAttrs Fill in with attribute information about the connection.
5257 */
5258 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5259 return null;
5260 }
5261
5262 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005263 * Called by the {@link android.view.inputmethod.InputMethodManager}
5264 * when a view who is not the current
5265 * input connection target is trying to make a call on the manager. The
5266 * default implementation returns false; you can override this to return
5267 * true for certain views if you are performing InputConnection proxying
5268 * to them.
5269 * @param view The View that is making the InputMethodManager call.
5270 * @return Return true to allow the call, false to reject.
5271 */
5272 public boolean checkInputConnectionProxy(View view) {
5273 return false;
5274 }
Romain Guy8506ab42009-06-11 17:35:47 -07005275
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005277 * Show the context menu for this view. It is not safe to hold on to the
5278 * menu after returning from this method.
5279 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005280 * You should normally not overload this method. Overload
5281 * {@link #onCreateContextMenu(ContextMenu)} or define an
5282 * {@link OnCreateContextMenuListener} to add items to the context menu.
5283 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 * @param menu The context menu to populate
5285 */
5286 public void createContextMenu(ContextMenu menu) {
5287 ContextMenuInfo menuInfo = getContextMenuInfo();
5288
5289 // Sets the current menu info so all items added to menu will have
5290 // my extra info set.
5291 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5292
5293 onCreateContextMenu(menu);
5294 if (mOnCreateContextMenuListener != null) {
5295 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5296 }
5297
5298 // Clear the extra information so subsequent items that aren't mine don't
5299 // have my extra info.
5300 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5301
5302 if (mParent != null) {
5303 mParent.createContextMenu(menu);
5304 }
5305 }
5306
5307 /**
5308 * Views should implement this if they have extra information to associate
5309 * with the context menu. The return result is supplied as a parameter to
5310 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5311 * callback.
5312 *
5313 * @return Extra information about the item for which the context menu
5314 * should be shown. This information will vary across different
5315 * subclasses of View.
5316 */
5317 protected ContextMenuInfo getContextMenuInfo() {
5318 return null;
5319 }
5320
5321 /**
5322 * Views should implement this if the view itself is going to add items to
5323 * the context menu.
5324 *
5325 * @param menu the context menu to populate
5326 */
5327 protected void onCreateContextMenu(ContextMenu menu) {
5328 }
5329
5330 /**
5331 * Implement this method to handle trackball motion events. The
5332 * <em>relative</em> movement of the trackball since the last event
5333 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5334 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5335 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5336 * they will often be fractional values, representing the more fine-grained
5337 * movement information available from a trackball).
5338 *
5339 * @param event The motion event.
5340 * @return True if the event was handled, false otherwise.
5341 */
5342 public boolean onTrackballEvent(MotionEvent event) {
5343 return false;
5344 }
5345
5346 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005347 * Implement this method to handle generic motion events.
5348 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005349 * Generic motion events describe joystick movements, mouse hovers, track pad
5350 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005351 * {@link MotionEvent#getSource() source} of the motion event specifies
5352 * the class of input that was received. Implementations of this method
5353 * must examine the bits in the source before processing the event.
5354 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005355 * </p><p>
5356 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5357 * are delivered to the view under the pointer. All other generic motion events are
5358 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005359 * </p>
5360 * <code>
5361 * public boolean onGenericMotionEvent(MotionEvent event) {
5362 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005363 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5364 * // process the joystick movement...
5365 * return true;
5366 * }
5367 * }
5368 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5369 * switch (event.getAction()) {
5370 * case MotionEvent.ACTION_HOVER_MOVE:
5371 * // process the mouse hover movement...
5372 * return true;
5373 * case MotionEvent.ACTION_SCROLL:
5374 * // process the scroll wheel movement...
5375 * return true;
5376 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005377 * }
5378 * return super.onGenericMotionEvent(event);
5379 * }
5380 * </code>
5381 *
5382 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005383 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005384 */
5385 public boolean onGenericMotionEvent(MotionEvent event) {
5386 return false;
5387 }
5388
5389 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005390 * Implement this method to handle hover events.
5391 * <p>
5392 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5393 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5394 * </p><p>
5395 * The view receives hover enter as the pointer enters the bounds of the view and hover
5396 * exit as the pointer exits the bound of the view or just before the pointer goes down
5397 * (which implies that {@link #onTouchEvent} will be called soon).
5398 * </p><p>
5399 * If the view would like to handle the hover event itself and prevent its children
5400 * from receiving hover, it should return true from this method. If this method returns
5401 * true and a child has already received a hover enter event, the child will
5402 * automatically receive a hover exit event.
5403 * </p><p>
5404 * The default implementation sets the hovered state of the view if the view is
5405 * clickable.
5406 * </p>
5407 *
5408 * @param event The motion event that describes the hover.
5409 * @return True if this view handled the hover event and does not want its children
5410 * to receive the hover event.
5411 */
5412 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005413 switch (event.getAction()) {
5414 case MotionEvent.ACTION_HOVER_ENTER:
5415 setHovered(true);
5416 break;
5417
5418 case MotionEvent.ACTION_HOVER_EXIT:
5419 setHovered(false);
5420 break;
5421 }
5422
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005423 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005424 }
5425
5426 /**
5427 * Returns true if the view is currently hovered.
5428 *
5429 * @return True if the view is currently hovered.
5430 */
5431 public boolean isHovered() {
5432 return (mPrivateFlags & HOVERED) != 0;
5433 }
5434
5435 /**
5436 * Sets whether the view is currently hovered.
5437 *
5438 * @param hovered True if the view is hovered.
5439 */
5440 public void setHovered(boolean hovered) {
5441 if (hovered) {
5442 if ((mPrivateFlags & HOVERED) == 0) {
5443 mPrivateFlags |= HOVERED;
5444 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005445 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005446 }
5447 } else {
5448 if ((mPrivateFlags & HOVERED) != 0) {
5449 mPrivateFlags &= ~HOVERED;
5450 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005451 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005452 }
5453 }
5454 }
5455
5456 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005457 * Implement this method to handle touch screen motion events.
5458 *
5459 * @param event The motion event.
5460 * @return True if the event was handled, false otherwise.
5461 */
5462 public boolean onTouchEvent(MotionEvent event) {
5463 final int viewFlags = mViewFlags;
5464
5465 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005466 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5467 mPrivateFlags &= ~PRESSED;
5468 refreshDrawableState();
5469 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005470 // A disabled view that is clickable still consumes the touch
5471 // events, it just doesn't respond to them.
5472 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5473 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5474 }
5475
5476 if (mTouchDelegate != null) {
5477 if (mTouchDelegate.onTouchEvent(event)) {
5478 return true;
5479 }
5480 }
5481
5482 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5483 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5484 switch (event.getAction()) {
5485 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005486 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5487 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005488 // take focus if we don't have it already and we should in
5489 // touch mode.
5490 boolean focusTaken = false;
5491 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5492 focusTaken = requestFocus();
5493 }
5494
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005495 if (prepressed) {
5496 // The button is being released before we actually
5497 // showed it as pressed. Make it show the pressed
5498 // state now (before scheduling the click) to ensure
5499 // the user sees it.
5500 mPrivateFlags |= PRESSED;
5501 refreshDrawableState();
5502 }
Joe Malin32736f02011-01-19 16:14:20 -08005503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 if (!mHasPerformedLongPress) {
5505 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005506 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005507
5508 // Only perform take click actions if we were in the pressed state
5509 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005510 // Use a Runnable and post this rather than calling
5511 // performClick directly. This lets other visual state
5512 // of the view update before click actions start.
5513 if (mPerformClick == null) {
5514 mPerformClick = new PerformClick();
5515 }
5516 if (!post(mPerformClick)) {
5517 performClick();
5518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005519 }
5520 }
5521
5522 if (mUnsetPressedState == null) {
5523 mUnsetPressedState = new UnsetPressedState();
5524 }
5525
Adam Powelle14579b2009-12-16 18:39:52 -08005526 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005527 postDelayed(mUnsetPressedState,
5528 ViewConfiguration.getPressedStateDuration());
5529 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005530 // If the post failed, unpress right now
5531 mUnsetPressedState.run();
5532 }
Adam Powelle14579b2009-12-16 18:39:52 -08005533 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 }
5535 break;
5536
5537 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005538 if (mPendingCheckForTap == null) {
5539 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005540 }
Adam Powelle14579b2009-12-16 18:39:52 -08005541 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005542 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005543 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005544 break;
5545
5546 case MotionEvent.ACTION_CANCEL:
5547 mPrivateFlags &= ~PRESSED;
5548 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005549 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005550 break;
5551
5552 case MotionEvent.ACTION_MOVE:
5553 final int x = (int) event.getX();
5554 final int y = (int) event.getY();
5555
5556 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005557 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005558 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005559 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005560 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005561 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005562 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563
5564 // Need to switch from pressed to not pressed
5565 mPrivateFlags &= ~PRESSED;
5566 refreshDrawableState();
5567 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005568 }
5569 break;
5570 }
5571 return true;
5572 }
5573
5574 return false;
5575 }
5576
5577 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005578 * Remove the longpress detection timer.
5579 */
5580 private void removeLongPressCallback() {
5581 if (mPendingCheckForLongPress != null) {
5582 removeCallbacks(mPendingCheckForLongPress);
5583 }
5584 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005585
5586 /**
5587 * Remove the pending click action
5588 */
5589 private void removePerformClickCallback() {
5590 if (mPerformClick != null) {
5591 removeCallbacks(mPerformClick);
5592 }
5593 }
5594
Adam Powelle14579b2009-12-16 18:39:52 -08005595 /**
Romain Guya440b002010-02-24 15:57:54 -08005596 * Remove the prepress detection timer.
5597 */
5598 private void removeUnsetPressCallback() {
5599 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5600 setPressed(false);
5601 removeCallbacks(mUnsetPressedState);
5602 }
5603 }
5604
5605 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005606 * Remove the tap detection timer.
5607 */
5608 private void removeTapCallback() {
5609 if (mPendingCheckForTap != null) {
5610 mPrivateFlags &= ~PREPRESSED;
5611 removeCallbacks(mPendingCheckForTap);
5612 }
5613 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005614
5615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005616 * Cancels a pending long press. Your subclass can use this if you
5617 * want the context menu to come up if the user presses and holds
5618 * at the same place, but you don't want it to come up if they press
5619 * and then move around enough to cause scrolling.
5620 */
5621 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005622 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005623
5624 /*
5625 * The prepressed state handled by the tap callback is a display
5626 * construct, but the tap callback will post a long press callback
5627 * less its own timeout. Remove it here.
5628 */
5629 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 }
5631
5632 /**
5633 * Sets the TouchDelegate for this View.
5634 */
5635 public void setTouchDelegate(TouchDelegate delegate) {
5636 mTouchDelegate = delegate;
5637 }
5638
5639 /**
5640 * Gets the TouchDelegate for this View.
5641 */
5642 public TouchDelegate getTouchDelegate() {
5643 return mTouchDelegate;
5644 }
5645
5646 /**
5647 * Set flags controlling behavior of this view.
5648 *
5649 * @param flags Constant indicating the value which should be set
5650 * @param mask Constant indicating the bit range that should be changed
5651 */
5652 void setFlags(int flags, int mask) {
5653 int old = mViewFlags;
5654 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5655
5656 int changed = mViewFlags ^ old;
5657 if (changed == 0) {
5658 return;
5659 }
5660 int privateFlags = mPrivateFlags;
5661
5662 /* Check if the FOCUSABLE bit has changed */
5663 if (((changed & FOCUSABLE_MASK) != 0) &&
5664 ((privateFlags & HAS_BOUNDS) !=0)) {
5665 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5666 && ((privateFlags & FOCUSED) != 0)) {
5667 /* Give up focus if we are no longer focusable */
5668 clearFocus();
5669 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5670 && ((privateFlags & FOCUSED) == 0)) {
5671 /*
5672 * Tell the view system that we are now available to take focus
5673 * if no one else already has it.
5674 */
5675 if (mParent != null) mParent.focusableViewAvailable(this);
5676 }
5677 }
5678
5679 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5680 if ((changed & VISIBILITY_MASK) != 0) {
5681 /*
5682 * If this view is becoming visible, set the DRAWN flag so that
5683 * the next invalidate() will not be skipped.
5684 */
5685 mPrivateFlags |= DRAWN;
5686
5687 needGlobalAttributesUpdate(true);
5688
5689 // a view becoming visible is worth notifying the parent
5690 // about in case nothing has focus. even if this specific view
5691 // isn't focusable, it may contain something that is, so let
5692 // the root view try to give this focus if nothing else does.
5693 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5694 mParent.focusableViewAvailable(this);
5695 }
5696 }
5697 }
5698
5699 /* Check if the GONE bit has changed */
5700 if ((changed & GONE) != 0) {
5701 needGlobalAttributesUpdate(false);
5702 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005703 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005704
Romain Guyecd80ee2009-12-03 17:13:02 -08005705 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5706 if (hasFocus()) clearFocus();
5707 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005708 }
5709 if (mAttachInfo != null) {
5710 mAttachInfo.mViewVisibilityChanged = true;
5711 }
5712 }
5713
5714 /* Check if the VISIBLE bit has changed */
5715 if ((changed & INVISIBLE) != 0) {
5716 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005717 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718
5719 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5720 // root view becoming invisible shouldn't clear focus
5721 if (getRootView() != this) {
5722 clearFocus();
5723 }
5724 }
5725 if (mAttachInfo != null) {
5726 mAttachInfo.mViewVisibilityChanged = true;
5727 }
5728 }
5729
Adam Powell326d8082009-12-09 15:10:07 -08005730 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005731 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005732 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5733 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005734 }
Adam Powell326d8082009-12-09 15:10:07 -08005735 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5736 }
5737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005738 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5739 destroyDrawingCache();
5740 }
5741
5742 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5743 destroyDrawingCache();
5744 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005745 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746 }
5747
5748 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5749 destroyDrawingCache();
5750 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5751 }
5752
5753 if ((changed & DRAW_MASK) != 0) {
5754 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5755 if (mBGDrawable != null) {
5756 mPrivateFlags &= ~SKIP_DRAW;
5757 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5758 } else {
5759 mPrivateFlags |= SKIP_DRAW;
5760 }
5761 } else {
5762 mPrivateFlags &= ~SKIP_DRAW;
5763 }
5764 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005765 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005766 }
5767
5768 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005769 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005770 mParent.recomputeViewAttributes(this);
5771 }
5772 }
5773 }
5774
5775 /**
5776 * Change the view's z order in the tree, so it's on top of other sibling
5777 * views
5778 */
5779 public void bringToFront() {
5780 if (mParent != null) {
5781 mParent.bringChildToFront(this);
5782 }
5783 }
5784
5785 /**
5786 * This is called in response to an internal scroll in this view (i.e., the
5787 * view scrolled its own contents). This is typically as a result of
5788 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5789 * called.
5790 *
5791 * @param l Current horizontal scroll origin.
5792 * @param t Current vertical scroll origin.
5793 * @param oldl Previous horizontal scroll origin.
5794 * @param oldt Previous vertical scroll origin.
5795 */
5796 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5797 mBackgroundSizeChanged = true;
5798
5799 final AttachInfo ai = mAttachInfo;
5800 if (ai != null) {
5801 ai.mViewScrollChanged = true;
5802 }
5803 }
5804
5805 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005806 * Interface definition for a callback to be invoked when the layout bounds of a view
5807 * changes due to layout processing.
5808 */
5809 public interface OnLayoutChangeListener {
5810 /**
5811 * Called when the focus state of a view has changed.
5812 *
5813 * @param v The view whose state has changed.
5814 * @param left The new value of the view's left property.
5815 * @param top The new value of the view's top property.
5816 * @param right The new value of the view's right property.
5817 * @param bottom The new value of the view's bottom property.
5818 * @param oldLeft The previous value of the view's left property.
5819 * @param oldTop The previous value of the view's top property.
5820 * @param oldRight The previous value of the view's right property.
5821 * @param oldBottom The previous value of the view's bottom property.
5822 */
5823 void onLayoutChange(View v, int left, int top, int right, int bottom,
5824 int oldLeft, int oldTop, int oldRight, int oldBottom);
5825 }
5826
5827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005828 * This is called during layout when the size of this view has changed. If
5829 * you were just added to the view hierarchy, you're called with the old
5830 * values of 0.
5831 *
5832 * @param w Current width of this view.
5833 * @param h Current height of this view.
5834 * @param oldw Old width of this view.
5835 * @param oldh Old height of this view.
5836 */
5837 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5838 }
5839
5840 /**
5841 * Called by draw to draw the child views. This may be overridden
5842 * by derived classes to gain control just before its children are drawn
5843 * (but after its own view has been drawn).
5844 * @param canvas the canvas on which to draw the view
5845 */
5846 protected void dispatchDraw(Canvas canvas) {
5847 }
5848
5849 /**
5850 * Gets the parent of this view. Note that the parent is a
5851 * ViewParent and not necessarily a View.
5852 *
5853 * @return Parent of this view.
5854 */
5855 public final ViewParent getParent() {
5856 return mParent;
5857 }
5858
5859 /**
5860 * Return the scrolled left position of this view. This is the left edge of
5861 * the displayed part of your view. You do not need to draw any pixels
5862 * farther left, since those are outside of the frame of your view on
5863 * screen.
5864 *
5865 * @return The left edge of the displayed part of your view, in pixels.
5866 */
5867 public final int getScrollX() {
5868 return mScrollX;
5869 }
5870
5871 /**
5872 * Return the scrolled top position of this view. This is the top edge of
5873 * the displayed part of your view. You do not need to draw any pixels above
5874 * it, since those are outside of the frame of your view on screen.
5875 *
5876 * @return The top edge of the displayed part of your view, in pixels.
5877 */
5878 public final int getScrollY() {
5879 return mScrollY;
5880 }
5881
5882 /**
5883 * Return the width of the your view.
5884 *
5885 * @return The width of your view, in pixels.
5886 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005887 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005888 public final int getWidth() {
5889 return mRight - mLeft;
5890 }
5891
5892 /**
5893 * Return the height of your view.
5894 *
5895 * @return The height of your view, in pixels.
5896 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005897 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005898 public final int getHeight() {
5899 return mBottom - mTop;
5900 }
5901
5902 /**
5903 * Return the visible drawing bounds of your view. Fills in the output
5904 * rectangle with the values from getScrollX(), getScrollY(),
5905 * getWidth(), and getHeight().
5906 *
5907 * @param outRect The (scrolled) drawing bounds of the view.
5908 */
5909 public void getDrawingRect(Rect outRect) {
5910 outRect.left = mScrollX;
5911 outRect.top = mScrollY;
5912 outRect.right = mScrollX + (mRight - mLeft);
5913 outRect.bottom = mScrollY + (mBottom - mTop);
5914 }
5915
5916 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005917 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5918 * raw width component (that is the result is masked by
5919 * {@link #MEASURED_SIZE_MASK}).
5920 *
5921 * @return The raw measured width of this view.
5922 */
5923 public final int getMeasuredWidth() {
5924 return mMeasuredWidth & MEASURED_SIZE_MASK;
5925 }
5926
5927 /**
5928 * Return the full width measurement information for this view as computed
5929 * by the most recent call to {@link #measure}. This result is a bit mask
5930 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005931 * This should be used during measurement and layout calculations only. Use
5932 * {@link #getWidth()} to see how wide a view is after layout.
5933 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005934 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005935 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005936 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005937 return mMeasuredWidth;
5938 }
5939
5940 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005941 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5942 * raw width component (that is the result is masked by
5943 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005944 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005945 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005946 */
5947 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005948 return mMeasuredHeight & MEASURED_SIZE_MASK;
5949 }
5950
5951 /**
5952 * Return the full height measurement information for this view as computed
5953 * by the most recent call to {@link #measure}. This result is a bit mask
5954 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5955 * This should be used during measurement and layout calculations only. Use
5956 * {@link #getHeight()} to see how wide a view is after layout.
5957 *
5958 * @return The measured width of this view as a bit mask.
5959 */
5960 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005961 return mMeasuredHeight;
5962 }
5963
5964 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005965 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5966 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5967 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5968 * and the height component is at the shifted bits
5969 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5970 */
5971 public final int getMeasuredState() {
5972 return (mMeasuredWidth&MEASURED_STATE_MASK)
5973 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5974 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5975 }
5976
5977 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005978 * The transform matrix of this view, which is calculated based on the current
5979 * roation, scale, and pivot properties.
5980 *
5981 * @see #getRotation()
5982 * @see #getScaleX()
5983 * @see #getScaleY()
5984 * @see #getPivotX()
5985 * @see #getPivotY()
5986 * @return The current transform matrix for the view
5987 */
5988 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005989 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005990 return mMatrix;
5991 }
5992
5993 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005994 * Utility function to determine if the value is far enough away from zero to be
5995 * considered non-zero.
5996 * @param value A floating point value to check for zero-ness
5997 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5998 */
5999 private static boolean nonzero(float value) {
6000 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6001 }
6002
6003 /**
Jeff Brown86671742010-09-30 20:00:15 -07006004 * Returns true if the transform matrix is the identity matrix.
6005 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006006 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006007 * @return True if the transform matrix is the identity matrix, false otherwise.
6008 */
Jeff Brown86671742010-09-30 20:00:15 -07006009 final boolean hasIdentityMatrix() {
6010 updateMatrix();
6011 return mMatrixIsIdentity;
6012 }
6013
6014 /**
6015 * Recomputes the transform matrix if necessary.
6016 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006017 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006018 if (mMatrixDirty) {
6019 // transform-related properties have changed since the last time someone
6020 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006021
6022 // Figure out if we need to update the pivot point
6023 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006024 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006025 mPrevWidth = mRight - mLeft;
6026 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006027 mPivotX = mPrevWidth / 2f;
6028 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006029 }
6030 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006031 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006032 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6033 mMatrix.setTranslate(mTranslationX, mTranslationY);
6034 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6035 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6036 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006037 if (mCamera == null) {
6038 mCamera = new Camera();
6039 matrix3D = new Matrix();
6040 }
6041 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006042 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006043 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006044 mCamera.getMatrix(matrix3D);
6045 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006046 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006047 mMatrix.postConcat(matrix3D);
6048 mCamera.restore();
6049 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006050 mMatrixDirty = false;
6051 mMatrixIsIdentity = mMatrix.isIdentity();
6052 mInverseMatrixDirty = true;
6053 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006054 }
6055
6056 /**
6057 * Utility method to retrieve the inverse of the current mMatrix property.
6058 * We cache the matrix to avoid recalculating it when transform properties
6059 * have not changed.
6060 *
6061 * @return The inverse of the current matrix of this view.
6062 */
Jeff Brown86671742010-09-30 20:00:15 -07006063 final Matrix getInverseMatrix() {
6064 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006065 if (mInverseMatrixDirty) {
6066 if (mInverseMatrix == null) {
6067 mInverseMatrix = new Matrix();
6068 }
6069 mMatrix.invert(mInverseMatrix);
6070 mInverseMatrixDirty = false;
6071 }
6072 return mInverseMatrix;
6073 }
6074
6075 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006076 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6077 * views are drawn) from the camera to this view. The camera's distance
6078 * affects 3D transformations, for instance rotations around the X and Y
6079 * axis. If the rotationX or rotationY properties are changed and this view is
6080 * large (more than half the size of the screen), it is recommended to always
6081 * use a camera distance that's greater than the height (X axis rotation) or
6082 * the width (Y axis rotation) of this view.</p>
6083 *
6084 * <p>The distance of the camera from the view plane can have an affect on the
6085 * perspective distortion of the view when it is rotated around the x or y axis.
6086 * For example, a large distance will result in a large viewing angle, and there
6087 * will not be much perspective distortion of the view as it rotates. A short
6088 * distance may cause much more perspective distortion upon rotation, and can
6089 * also result in some drawing artifacts if the rotated view ends up partially
6090 * behind the camera (which is why the recommendation is to use a distance at
6091 * least as far as the size of the view, if the view is to be rotated.)</p>
6092 *
6093 * <p>The distance is expressed in "depth pixels." The default distance depends
6094 * on the screen density. For instance, on a medium density display, the
6095 * default distance is 1280. On a high density display, the default distance
6096 * is 1920.</p>
6097 *
6098 * <p>If you want to specify a distance that leads to visually consistent
6099 * results across various densities, use the following formula:</p>
6100 * <pre>
6101 * float scale = context.getResources().getDisplayMetrics().density;
6102 * view.setCameraDistance(distance * scale);
6103 * </pre>
6104 *
6105 * <p>The density scale factor of a high density display is 1.5,
6106 * and 1920 = 1280 * 1.5.</p>
6107 *
6108 * @param distance The distance in "depth pixels", if negative the opposite
6109 * value is used
6110 *
6111 * @see #setRotationX(float)
6112 * @see #setRotationY(float)
6113 */
6114 public void setCameraDistance(float distance) {
6115 invalidateParentCaches();
6116 invalidate(false);
6117
6118 final float dpi = mResources.getDisplayMetrics().densityDpi;
6119 if (mCamera == null) {
6120 mCamera = new Camera();
6121 matrix3D = new Matrix();
6122 }
6123
6124 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6125 mMatrixDirty = true;
6126
6127 invalidate(false);
6128 }
6129
6130 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006131 * The degrees that the view is rotated around the pivot point.
6132 *
Romain Guya5364ee2011-02-24 14:46:04 -08006133 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006134 * @see #getPivotX()
6135 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006136 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006137 * @return The degrees of rotation.
6138 */
6139 public float getRotation() {
6140 return mRotation;
6141 }
6142
6143 /**
Chet Haase897247b2010-09-09 14:54:47 -07006144 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6145 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006146 *
6147 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006148 *
6149 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006150 * @see #getPivotX()
6151 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006152 * @see #setRotationX(float)
6153 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006154 *
6155 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006156 */
6157 public void setRotation(float rotation) {
6158 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006159 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006160 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006161 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006162 mRotation = rotation;
6163 mMatrixDirty = true;
6164 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006165 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006166 }
6167 }
6168
6169 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006170 * The degrees that the view is rotated around the vertical axis through the pivot point.
6171 *
6172 * @see #getPivotX()
6173 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006174 * @see #setRotationY(float)
6175 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006176 * @return The degrees of Y rotation.
6177 */
6178 public float getRotationY() {
6179 return mRotationY;
6180 }
6181
6182 /**
Chet Haase897247b2010-09-09 14:54:47 -07006183 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6184 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6185 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006186 *
6187 * When rotating large views, it is recommended to adjust the camera distance
6188 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006189 *
6190 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006191 *
6192 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006193 * @see #getPivotX()
6194 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006195 * @see #setRotation(float)
6196 * @see #setRotationX(float)
6197 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006198 *
6199 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006200 */
6201 public void setRotationY(float rotationY) {
6202 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006203 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006204 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006205 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006206 mRotationY = rotationY;
6207 mMatrixDirty = true;
6208 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006209 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006210 }
6211 }
6212
6213 /**
6214 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6215 *
6216 * @see #getPivotX()
6217 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006218 * @see #setRotationX(float)
6219 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006220 * @return The degrees of X rotation.
6221 */
6222 public float getRotationX() {
6223 return mRotationX;
6224 }
6225
6226 /**
Chet Haase897247b2010-09-09 14:54:47 -07006227 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6228 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6229 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006230 *
6231 * When rotating large views, it is recommended to adjust the camera distance
6232 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006233 *
6234 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006235 *
6236 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006237 * @see #getPivotX()
6238 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006239 * @see #setRotation(float)
6240 * @see #setRotationY(float)
6241 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006242 *
6243 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006244 */
6245 public void setRotationX(float rotationX) {
6246 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006247 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006248 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006249 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006250 mRotationX = rotationX;
6251 mMatrixDirty = true;
6252 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006253 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006254 }
6255 }
6256
6257 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006258 * The amount that the view is scaled in x around the pivot point, as a proportion of
6259 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6260 *
Joe Onorato93162322010-09-16 15:42:01 -04006261 * <p>By default, this is 1.0f.
6262 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006263 * @see #getPivotX()
6264 * @see #getPivotY()
6265 * @return The scaling factor.
6266 */
6267 public float getScaleX() {
6268 return mScaleX;
6269 }
6270
6271 /**
6272 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6273 * the view's unscaled width. A value of 1 means that no scaling is applied.
6274 *
6275 * @param scaleX The scaling factor.
6276 * @see #getPivotX()
6277 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006278 *
6279 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006280 */
6281 public void setScaleX(float scaleX) {
6282 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006283 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006284 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006285 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006286 mScaleX = scaleX;
6287 mMatrixDirty = true;
6288 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006289 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006290 }
6291 }
6292
6293 /**
6294 * The amount that the view is scaled in y around the pivot point, as a proportion of
6295 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6296 *
Joe Onorato93162322010-09-16 15:42:01 -04006297 * <p>By default, this is 1.0f.
6298 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006299 * @see #getPivotX()
6300 * @see #getPivotY()
6301 * @return The scaling factor.
6302 */
6303 public float getScaleY() {
6304 return mScaleY;
6305 }
6306
6307 /**
6308 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6309 * the view's unscaled width. A value of 1 means that no scaling is applied.
6310 *
6311 * @param scaleY The scaling factor.
6312 * @see #getPivotX()
6313 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006314 *
6315 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006316 */
6317 public void setScaleY(float scaleY) {
6318 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006319 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006320 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006321 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006322 mScaleY = scaleY;
6323 mMatrixDirty = true;
6324 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006325 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006326 }
6327 }
6328
6329 /**
6330 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6331 * and {@link #setScaleX(float) scaled}.
6332 *
6333 * @see #getRotation()
6334 * @see #getScaleX()
6335 * @see #getScaleY()
6336 * @see #getPivotY()
6337 * @return The x location of the pivot point.
6338 */
6339 public float getPivotX() {
6340 return mPivotX;
6341 }
6342
6343 /**
6344 * Sets the x location of the point around which the view is
6345 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006346 * By default, the pivot point is centered on the object.
6347 * Setting this property disables this behavior and causes the view to use only the
6348 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006349 *
6350 * @param pivotX The x location of the pivot point.
6351 * @see #getRotation()
6352 * @see #getScaleX()
6353 * @see #getScaleY()
6354 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006355 *
6356 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006357 */
6358 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006359 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006360 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006361 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006362 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006363 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006364 mPivotX = pivotX;
6365 mMatrixDirty = true;
6366 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006367 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006368 }
6369 }
6370
6371 /**
6372 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6373 * and {@link #setScaleY(float) scaled}.
6374 *
6375 * @see #getRotation()
6376 * @see #getScaleX()
6377 * @see #getScaleY()
6378 * @see #getPivotY()
6379 * @return The y location of the pivot point.
6380 */
6381 public float getPivotY() {
6382 return mPivotY;
6383 }
6384
6385 /**
6386 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006387 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6388 * Setting this property disables this behavior and causes the view to use only the
6389 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006390 *
6391 * @param pivotY The y location of the pivot point.
6392 * @see #getRotation()
6393 * @see #getScaleX()
6394 * @see #getScaleY()
6395 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006396 *
6397 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006398 */
6399 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006400 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006401 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006402 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006403 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006404 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006405 mPivotY = pivotY;
6406 mMatrixDirty = true;
6407 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006408 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006409 }
6410 }
6411
6412 /**
6413 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6414 * completely transparent and 1 means the view is completely opaque.
6415 *
Joe Onorato93162322010-09-16 15:42:01 -04006416 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006417 * @return The opacity of the view.
6418 */
6419 public float getAlpha() {
6420 return mAlpha;
6421 }
6422
6423 /**
Romain Guy171c5922011-01-06 10:04:23 -08006424 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6425 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006426 *
Romain Guy171c5922011-01-06 10:04:23 -08006427 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6428 * responsible for applying the opacity itself. Otherwise, calling this method is
6429 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006430 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006431 *
6432 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006433 *
Joe Malin32736f02011-01-19 16:14:20 -08006434 * @see #setLayerType(int, android.graphics.Paint)
6435 *
Chet Haase73066682010-11-29 15:55:32 -08006436 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006437 */
6438 public void setAlpha(float alpha) {
6439 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006440 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006441 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006442 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006443 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006444 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006445 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006446 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006447 invalidate(false);
6448 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006449 }
6450
6451 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006452 * Faster version of setAlpha() which performs the same steps except there are
6453 * no calls to invalidate(). The caller of this function should perform proper invalidation
6454 * on the parent and this object. The return value indicates whether the subclass handles
6455 * alpha (the return value for onSetAlpha()).
6456 *
6457 * @param alpha The new value for the alpha property
6458 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6459 */
6460 boolean setAlphaNoInvalidation(float alpha) {
6461 mAlpha = alpha;
6462 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6463 if (subclassHandlesAlpha) {
6464 mPrivateFlags |= ALPHA_SET;
6465 } else {
6466 mPrivateFlags &= ~ALPHA_SET;
6467 }
6468 return subclassHandlesAlpha;
6469 }
6470
6471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006472 * Top position of this view relative to its parent.
6473 *
6474 * @return The top of this view, in pixels.
6475 */
6476 @ViewDebug.CapturedViewProperty
6477 public final int getTop() {
6478 return mTop;
6479 }
6480
6481 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006482 * Sets the top position of this view relative to its parent. This method is meant to be called
6483 * by the layout system and should not generally be called otherwise, because the property
6484 * may be changed at any time by the layout.
6485 *
6486 * @param top The top of this view, in pixels.
6487 */
6488 public final void setTop(int top) {
6489 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006490 updateMatrix();
6491 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006492 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006493 int minTop;
6494 int yLoc;
6495 if (top < mTop) {
6496 minTop = top;
6497 yLoc = top - mTop;
6498 } else {
6499 minTop = mTop;
6500 yLoc = 0;
6501 }
Chet Haasee9140a72011-02-16 16:23:29 -08006502 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006503 }
6504 } else {
6505 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006506 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006507 }
6508
Chet Haaseed032702010-10-01 14:05:54 -07006509 int width = mRight - mLeft;
6510 int oldHeight = mBottom - mTop;
6511
Chet Haase21cd1382010-09-01 17:42:29 -07006512 mTop = top;
6513
Chet Haaseed032702010-10-01 14:05:54 -07006514 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6515
Chet Haase21cd1382010-09-01 17:42:29 -07006516 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006517 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6518 // A change in dimension means an auto-centered pivot point changes, too
6519 mMatrixDirty = true;
6520 }
Chet Haase21cd1382010-09-01 17:42:29 -07006521 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006522 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006523 }
Chet Haase55dbb652010-12-21 20:15:08 -08006524 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006525 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006526 }
6527 }
6528
6529 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006530 * Bottom position of this view relative to its parent.
6531 *
6532 * @return The bottom of this view, in pixels.
6533 */
6534 @ViewDebug.CapturedViewProperty
6535 public final int getBottom() {
6536 return mBottom;
6537 }
6538
6539 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006540 * True if this view has changed since the last time being drawn.
6541 *
6542 * @return The dirty state of this view.
6543 */
6544 public boolean isDirty() {
6545 return (mPrivateFlags & DIRTY_MASK) != 0;
6546 }
6547
6548 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006549 * Sets the bottom position of this view relative to its parent. This method is meant to be
6550 * called by the layout system and should not generally be called otherwise, because the
6551 * property may be changed at any time by the layout.
6552 *
6553 * @param bottom The bottom of this view, in pixels.
6554 */
6555 public final void setBottom(int bottom) {
6556 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006557 updateMatrix();
6558 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006559 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006560 int maxBottom;
6561 if (bottom < mBottom) {
6562 maxBottom = mBottom;
6563 } else {
6564 maxBottom = bottom;
6565 }
Chet Haasee9140a72011-02-16 16:23:29 -08006566 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006567 }
6568 } else {
6569 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006570 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006571 }
6572
Chet Haaseed032702010-10-01 14:05:54 -07006573 int width = mRight - mLeft;
6574 int oldHeight = mBottom - mTop;
6575
Chet Haase21cd1382010-09-01 17:42:29 -07006576 mBottom = bottom;
6577
Chet Haaseed032702010-10-01 14:05:54 -07006578 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6579
Chet Haase21cd1382010-09-01 17:42:29 -07006580 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006581 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6582 // A change in dimension means an auto-centered pivot point changes, too
6583 mMatrixDirty = true;
6584 }
Chet Haase21cd1382010-09-01 17:42:29 -07006585 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006586 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006587 }
Chet Haase55dbb652010-12-21 20:15:08 -08006588 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006589 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006590 }
6591 }
6592
6593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 * Left position of this view relative to its parent.
6595 *
6596 * @return The left edge of this view, in pixels.
6597 */
6598 @ViewDebug.CapturedViewProperty
6599 public final int getLeft() {
6600 return mLeft;
6601 }
6602
6603 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006604 * Sets the left position of this view relative to its parent. This method is meant to be called
6605 * by the layout system and should not generally be called otherwise, because the property
6606 * may be changed at any time by the layout.
6607 *
6608 * @param left The bottom of this view, in pixels.
6609 */
6610 public final void setLeft(int left) {
6611 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006612 updateMatrix();
6613 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006614 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006615 int minLeft;
6616 int xLoc;
6617 if (left < mLeft) {
6618 minLeft = left;
6619 xLoc = left - mLeft;
6620 } else {
6621 minLeft = mLeft;
6622 xLoc = 0;
6623 }
Chet Haasee9140a72011-02-16 16:23:29 -08006624 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006625 }
6626 } else {
6627 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006628 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006629 }
6630
Chet Haaseed032702010-10-01 14:05:54 -07006631 int oldWidth = mRight - mLeft;
6632 int height = mBottom - mTop;
6633
Chet Haase21cd1382010-09-01 17:42:29 -07006634 mLeft = left;
6635
Chet Haaseed032702010-10-01 14:05:54 -07006636 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6637
Chet Haase21cd1382010-09-01 17:42:29 -07006638 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006639 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6640 // A change in dimension means an auto-centered pivot point changes, too
6641 mMatrixDirty = true;
6642 }
Chet Haase21cd1382010-09-01 17:42:29 -07006643 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006644 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006645 }
Chet Haase55dbb652010-12-21 20:15:08 -08006646 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006647 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006648 }
6649 }
6650
6651 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 * Right position of this view relative to its parent.
6653 *
6654 * @return The right edge of this view, in pixels.
6655 */
6656 @ViewDebug.CapturedViewProperty
6657 public final int getRight() {
6658 return mRight;
6659 }
6660
6661 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006662 * Sets the right position of this view relative to its parent. This method is meant to be called
6663 * by the layout system and should not generally be called otherwise, because the property
6664 * may be changed at any time by the layout.
6665 *
6666 * @param right The bottom of this view, in pixels.
6667 */
6668 public final void setRight(int right) {
6669 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006670 updateMatrix();
6671 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006672 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006673 int maxRight;
6674 if (right < mRight) {
6675 maxRight = mRight;
6676 } else {
6677 maxRight = right;
6678 }
Chet Haasee9140a72011-02-16 16:23:29 -08006679 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006680 }
6681 } else {
6682 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006683 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006684 }
6685
Chet Haaseed032702010-10-01 14:05:54 -07006686 int oldWidth = mRight - mLeft;
6687 int height = mBottom - mTop;
6688
Chet Haase21cd1382010-09-01 17:42:29 -07006689 mRight = right;
6690
Chet Haaseed032702010-10-01 14:05:54 -07006691 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6692
Chet Haase21cd1382010-09-01 17:42:29 -07006693 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006694 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6695 // A change in dimension means an auto-centered pivot point changes, too
6696 mMatrixDirty = true;
6697 }
Chet Haase21cd1382010-09-01 17:42:29 -07006698 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006699 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006700 }
Chet Haase55dbb652010-12-21 20:15:08 -08006701 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006702 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006703 }
6704 }
6705
6706 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006707 * The visual x position of this view, in pixels. This is equivalent to the
6708 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006709 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006710 *
Chet Haasedf030d22010-07-30 17:22:38 -07006711 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006712 */
Chet Haasedf030d22010-07-30 17:22:38 -07006713 public float getX() {
6714 return mLeft + mTranslationX;
6715 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006716
Chet Haasedf030d22010-07-30 17:22:38 -07006717 /**
6718 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6719 * {@link #setTranslationX(float) translationX} property to be the difference between
6720 * the x value passed in and the current {@link #getLeft() left} property.
6721 *
6722 * @param x The visual x position of this view, in pixels.
6723 */
6724 public void setX(float x) {
6725 setTranslationX(x - mLeft);
6726 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006727
Chet Haasedf030d22010-07-30 17:22:38 -07006728 /**
6729 * The visual y position of this view, in pixels. This is equivalent to the
6730 * {@link #setTranslationY(float) translationY} property plus the current
6731 * {@link #getTop() top} property.
6732 *
6733 * @return The visual y position of this view, in pixels.
6734 */
6735 public float getY() {
6736 return mTop + mTranslationY;
6737 }
6738
6739 /**
6740 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6741 * {@link #setTranslationY(float) translationY} property to be the difference between
6742 * the y value passed in and the current {@link #getTop() top} property.
6743 *
6744 * @param y The visual y position of this view, in pixels.
6745 */
6746 public void setY(float y) {
6747 setTranslationY(y - mTop);
6748 }
6749
6750
6751 /**
6752 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6753 * This position is post-layout, in addition to wherever the object's
6754 * layout placed it.
6755 *
6756 * @return The horizontal position of this view relative to its left position, in pixels.
6757 */
6758 public float getTranslationX() {
6759 return mTranslationX;
6760 }
6761
6762 /**
6763 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6764 * This effectively positions the object post-layout, in addition to wherever the object's
6765 * layout placed it.
6766 *
6767 * @param translationX The horizontal position of this view relative to its left position,
6768 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006769 *
6770 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006771 */
6772 public void setTranslationX(float translationX) {
6773 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006774 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006775 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006776 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006777 mTranslationX = translationX;
6778 mMatrixDirty = true;
6779 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006780 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006781 }
6782 }
6783
6784 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006785 * The horizontal location of this view relative to its {@link #getTop() top} position.
6786 * This position is post-layout, in addition to wherever the object's
6787 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006788 *
Chet Haasedf030d22010-07-30 17:22:38 -07006789 * @return The vertical position of this view relative to its top position,
6790 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006791 */
Chet Haasedf030d22010-07-30 17:22:38 -07006792 public float getTranslationY() {
6793 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006794 }
6795
6796 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006797 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6798 * This effectively positions the object post-layout, in addition to wherever the object's
6799 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006800 *
Chet Haasedf030d22010-07-30 17:22:38 -07006801 * @param translationY The vertical position of this view relative to its top position,
6802 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006803 *
6804 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006805 */
Chet Haasedf030d22010-07-30 17:22:38 -07006806 public void setTranslationY(float translationY) {
6807 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006808 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006809 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006810 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006811 mTranslationY = translationY;
6812 mMatrixDirty = true;
6813 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006814 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006815 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006816 }
6817
6818 /**
Romain Guyda489792011-02-03 01:05:15 -08006819 * @hide
6820 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006821 public void setFastTranslationX(float x) {
6822 mTranslationX = x;
6823 mMatrixDirty = true;
6824 }
6825
6826 /**
6827 * @hide
6828 */
6829 public void setFastTranslationY(float y) {
6830 mTranslationY = y;
6831 mMatrixDirty = true;
6832 }
6833
6834 /**
6835 * @hide
6836 */
Romain Guyda489792011-02-03 01:05:15 -08006837 public void setFastX(float x) {
6838 mTranslationX = x - mLeft;
6839 mMatrixDirty = true;
6840 }
6841
6842 /**
6843 * @hide
6844 */
6845 public void setFastY(float y) {
6846 mTranslationY = y - mTop;
6847 mMatrixDirty = true;
6848 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006849
Romain Guyda489792011-02-03 01:05:15 -08006850 /**
6851 * @hide
6852 */
6853 public void setFastScaleX(float x) {
6854 mScaleX = x;
6855 mMatrixDirty = true;
6856 }
6857
6858 /**
6859 * @hide
6860 */
6861 public void setFastScaleY(float y) {
6862 mScaleY = y;
6863 mMatrixDirty = true;
6864 }
6865
6866 /**
6867 * @hide
6868 */
6869 public void setFastAlpha(float alpha) {
6870 mAlpha = alpha;
6871 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006872
Romain Guyda489792011-02-03 01:05:15 -08006873 /**
6874 * @hide
6875 */
6876 public void setFastRotationY(float y) {
6877 mRotationY = y;
6878 mMatrixDirty = true;
6879 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006880
Romain Guyda489792011-02-03 01:05:15 -08006881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006882 * Hit rectangle in parent's coordinates
6883 *
6884 * @param outRect The hit rectangle of the view.
6885 */
6886 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006887 updateMatrix();
6888 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006889 outRect.set(mLeft, mTop, mRight, mBottom);
6890 } else {
6891 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006892 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006893 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006894 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6895 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006896 }
6897 }
6898
6899 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006900 * Determines whether the given point, in local coordinates is inside the view.
6901 */
6902 /*package*/ final boolean pointInView(float localX, float localY) {
6903 return localX >= 0 && localX < (mRight - mLeft)
6904 && localY >= 0 && localY < (mBottom - mTop);
6905 }
6906
6907 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006908 * Utility method to determine whether the given point, in local coordinates,
6909 * is inside the view, where the area of the view is expanded by the slop factor.
6910 * This method is called while processing touch-move events to determine if the event
6911 * is still within the view.
6912 */
6913 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006914 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006915 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006916 }
6917
6918 /**
6919 * When a view has focus and the user navigates away from it, the next view is searched for
6920 * starting from the rectangle filled in by this method.
6921 *
6922 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6923 * view maintains some idea of internal selection, such as a cursor, or a selected row
6924 * or column, you should override this method and fill in a more specific rectangle.
6925 *
6926 * @param r The rectangle to fill in, in this view's coordinates.
6927 */
6928 public void getFocusedRect(Rect r) {
6929 getDrawingRect(r);
6930 }
6931
6932 /**
6933 * If some part of this view is not clipped by any of its parents, then
6934 * return that area in r in global (root) coordinates. To convert r to local
6935 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6936 * -globalOffset.y)) If the view is completely clipped or translated out,
6937 * return false.
6938 *
6939 * @param r If true is returned, r holds the global coordinates of the
6940 * visible portion of this view.
6941 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6942 * between this view and its root. globalOffet may be null.
6943 * @return true if r is non-empty (i.e. part of the view is visible at the
6944 * root level.
6945 */
6946 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6947 int width = mRight - mLeft;
6948 int height = mBottom - mTop;
6949 if (width > 0 && height > 0) {
6950 r.set(0, 0, width, height);
6951 if (globalOffset != null) {
6952 globalOffset.set(-mScrollX, -mScrollY);
6953 }
6954 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6955 }
6956 return false;
6957 }
6958
6959 public final boolean getGlobalVisibleRect(Rect r) {
6960 return getGlobalVisibleRect(r, null);
6961 }
6962
6963 public final boolean getLocalVisibleRect(Rect r) {
6964 Point offset = new Point();
6965 if (getGlobalVisibleRect(r, offset)) {
6966 r.offset(-offset.x, -offset.y); // make r local
6967 return true;
6968 }
6969 return false;
6970 }
6971
6972 /**
6973 * Offset this view's vertical location by the specified number of pixels.
6974 *
6975 * @param offset the number of pixels to offset the view by
6976 */
6977 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006978 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006979 updateMatrix();
6980 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006981 final ViewParent p = mParent;
6982 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006983 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006984 int minTop;
6985 int maxBottom;
6986 int yLoc;
6987 if (offset < 0) {
6988 minTop = mTop + offset;
6989 maxBottom = mBottom;
6990 yLoc = offset;
6991 } else {
6992 minTop = mTop;
6993 maxBottom = mBottom + offset;
6994 yLoc = 0;
6995 }
6996 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6997 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006998 }
6999 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007000 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007001 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007002
Chet Haasec3aa3612010-06-17 08:50:37 -07007003 mTop += offset;
7004 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007005
Chet Haasec3aa3612010-06-17 08:50:37 -07007006 if (!mMatrixIsIdentity) {
7007 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007008 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007009 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007010 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007012 }
7013
7014 /**
7015 * Offset this view's horizontal location by the specified amount of pixels.
7016 *
7017 * @param offset the numer of pixels to offset the view by
7018 */
7019 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007020 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007021 updateMatrix();
7022 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007023 final ViewParent p = mParent;
7024 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007025 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007026 int minLeft;
7027 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007028 if (offset < 0) {
7029 minLeft = mLeft + offset;
7030 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007031 } else {
7032 minLeft = mLeft;
7033 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007034 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007035 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007036 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007037 }
7038 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007039 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007040 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007041
Chet Haasec3aa3612010-06-17 08:50:37 -07007042 mLeft += offset;
7043 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007044
Chet Haasec3aa3612010-06-17 08:50:37 -07007045 if (!mMatrixIsIdentity) {
7046 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007047 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007048 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007049 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007050 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007051 }
7052
7053 /**
7054 * Get the LayoutParams associated with this view. All views should have
7055 * layout parameters. These supply parameters to the <i>parent</i> of this
7056 * view specifying how it should be arranged. There are many subclasses of
7057 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7058 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007059 *
7060 * This method may return null if this View is not attached to a parent
7061 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7062 * was not invoked successfully. When a View is attached to a parent
7063 * ViewGroup, this method must not return null.
7064 *
7065 * @return The LayoutParams associated with this view, or null if no
7066 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007068 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007069 public ViewGroup.LayoutParams getLayoutParams() {
7070 return mLayoutParams;
7071 }
7072
7073 /**
7074 * Set the layout parameters associated with this view. These supply
7075 * parameters to the <i>parent</i> of this view specifying how it should be
7076 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7077 * correspond to the different subclasses of ViewGroup that are responsible
7078 * for arranging their children.
7079 *
Romain Guy01c174b2011-02-22 11:51:06 -08007080 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 */
7082 public void setLayoutParams(ViewGroup.LayoutParams params) {
7083 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007084 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007085 }
7086 mLayoutParams = params;
7087 requestLayout();
7088 }
7089
7090 /**
7091 * Set the scrolled position of your view. This will cause a call to
7092 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7093 * invalidated.
7094 * @param x the x position to scroll to
7095 * @param y the y position to scroll to
7096 */
7097 public void scrollTo(int x, int y) {
7098 if (mScrollX != x || mScrollY != y) {
7099 int oldX = mScrollX;
7100 int oldY = mScrollY;
7101 mScrollX = x;
7102 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007103 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007104 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007105 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007106 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007107 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007108 }
7109 }
7110
7111 /**
7112 * Move the scrolled position of your view. This will cause a call to
7113 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7114 * invalidated.
7115 * @param x the amount of pixels to scroll by horizontally
7116 * @param y the amount of pixels to scroll by vertically
7117 */
7118 public void scrollBy(int x, int y) {
7119 scrollTo(mScrollX + x, mScrollY + y);
7120 }
7121
7122 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007123 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7124 * animation to fade the scrollbars out after a default delay. If a subclass
7125 * provides animated scrolling, the start delay should equal the duration
7126 * of the scrolling animation.</p>
7127 *
7128 * <p>The animation starts only if at least one of the scrollbars is
7129 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7130 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7131 * this method returns true, and false otherwise. If the animation is
7132 * started, this method calls {@link #invalidate()}; in that case the
7133 * caller should not call {@link #invalidate()}.</p>
7134 *
7135 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007136 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007137 *
7138 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7139 * and {@link #scrollTo(int, int)}.</p>
7140 *
7141 * @return true if the animation is played, false otherwise
7142 *
7143 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007144 * @see #scrollBy(int, int)
7145 * @see #scrollTo(int, int)
7146 * @see #isHorizontalScrollBarEnabled()
7147 * @see #isVerticalScrollBarEnabled()
7148 * @see #setHorizontalScrollBarEnabled(boolean)
7149 * @see #setVerticalScrollBarEnabled(boolean)
7150 */
7151 protected boolean awakenScrollBars() {
7152 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007153 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007154 }
7155
7156 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007157 * Trigger the scrollbars to draw.
7158 * This method differs from awakenScrollBars() only in its default duration.
7159 * initialAwakenScrollBars() will show the scroll bars for longer than
7160 * usual to give the user more of a chance to notice them.
7161 *
7162 * @return true if the animation is played, false otherwise.
7163 */
7164 private boolean initialAwakenScrollBars() {
7165 return mScrollCache != null &&
7166 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7167 }
7168
7169 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007170 * <p>
7171 * Trigger the scrollbars to draw. When invoked this method starts an
7172 * animation to fade the scrollbars out after a fixed delay. If a subclass
7173 * provides animated scrolling, the start delay should equal the duration of
7174 * the scrolling animation.
7175 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007176 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007177 * <p>
7178 * The animation starts only if at least one of the scrollbars is enabled,
7179 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7180 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7181 * this method returns true, and false otherwise. If the animation is
7182 * started, this method calls {@link #invalidate()}; in that case the caller
7183 * should not call {@link #invalidate()}.
7184 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007185 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007186 * <p>
7187 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007188 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007189 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007190 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007191 * @param startDelay the delay, in milliseconds, after which the animation
7192 * should start; when the delay is 0, the animation starts
7193 * immediately
7194 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007195 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007196 * @see #scrollBy(int, int)
7197 * @see #scrollTo(int, int)
7198 * @see #isHorizontalScrollBarEnabled()
7199 * @see #isVerticalScrollBarEnabled()
7200 * @see #setHorizontalScrollBarEnabled(boolean)
7201 * @see #setVerticalScrollBarEnabled(boolean)
7202 */
7203 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007204 return awakenScrollBars(startDelay, true);
7205 }
Joe Malin32736f02011-01-19 16:14:20 -08007206
Mike Cleron290947b2009-09-29 18:34:32 -07007207 /**
7208 * <p>
7209 * Trigger the scrollbars to draw. When invoked this method starts an
7210 * animation to fade the scrollbars out after a fixed delay. If a subclass
7211 * provides animated scrolling, the start delay should equal the duration of
7212 * the scrolling animation.
7213 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007214 *
Mike Cleron290947b2009-09-29 18:34:32 -07007215 * <p>
7216 * The animation starts only if at least one of the scrollbars is enabled,
7217 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7218 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7219 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007220 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007221 * is set to true; in that case the caller
7222 * should not call {@link #invalidate()}.
7223 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007224 *
Mike Cleron290947b2009-09-29 18:34:32 -07007225 * <p>
7226 * This method should be invoked everytime a subclass directly updates the
7227 * scroll parameters.
7228 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007229 *
Mike Cleron290947b2009-09-29 18:34:32 -07007230 * @param startDelay the delay, in milliseconds, after which the animation
7231 * should start; when the delay is 0, the animation starts
7232 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007233 *
Mike Cleron290947b2009-09-29 18:34:32 -07007234 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007235 *
Mike Cleron290947b2009-09-29 18:34:32 -07007236 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007237 *
Mike Cleron290947b2009-09-29 18:34:32 -07007238 * @see #scrollBy(int, int)
7239 * @see #scrollTo(int, int)
7240 * @see #isHorizontalScrollBarEnabled()
7241 * @see #isVerticalScrollBarEnabled()
7242 * @see #setHorizontalScrollBarEnabled(boolean)
7243 * @see #setVerticalScrollBarEnabled(boolean)
7244 */
7245 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007246 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007247
Mike Cleronf116bf82009-09-27 19:14:12 -07007248 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7249 return false;
7250 }
7251
7252 if (scrollCache.scrollBar == null) {
7253 scrollCache.scrollBar = new ScrollBarDrawable();
7254 }
7255
7256 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7257
Mike Cleron290947b2009-09-29 18:34:32 -07007258 if (invalidate) {
7259 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007260 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007261 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007262
7263 if (scrollCache.state == ScrollabilityCache.OFF) {
7264 // FIXME: this is copied from WindowManagerService.
7265 // We should get this value from the system when it
7266 // is possible to do so.
7267 final int KEY_REPEAT_FIRST_DELAY = 750;
7268 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7269 }
7270
7271 // Tell mScrollCache when we should start fading. This may
7272 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007273 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007274 scrollCache.fadeStartTime = fadeStartTime;
7275 scrollCache.state = ScrollabilityCache.ON;
7276
7277 // Schedule our fader to run, unscheduling any old ones first
7278 if (mAttachInfo != null) {
7279 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7280 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7281 }
7282
7283 return true;
7284 }
7285
7286 return false;
7287 }
7288
7289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007290 * Mark the the area defined by dirty as needing to be drawn. If the view is
7291 * visible, {@link #onDraw} will be called at some point in the future.
7292 * This must be called from a UI thread. To call from a non-UI thread, call
7293 * {@link #postInvalidate()}.
7294 *
7295 * WARNING: This method is destructive to dirty.
7296 * @param dirty the rectangle representing the bounds of the dirty region
7297 */
7298 public void invalidate(Rect dirty) {
7299 if (ViewDebug.TRACE_HIERARCHY) {
7300 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7301 }
7302
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007303 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007304 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7305 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007306 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007307 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007308 final ViewParent p = mParent;
7309 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007310 //noinspection PointlessBooleanExpression,ConstantConditions
7311 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7312 if (p != null && ai != null && ai.mHardwareAccelerated) {
7313 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7314 // with a null dirty rect, which tells the ViewRoot to redraw everything
7315 p.invalidateChild(this, null);
7316 return;
7317 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007318 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007319 if (p != null && ai != null) {
7320 final int scrollX = mScrollX;
7321 final int scrollY = mScrollY;
7322 final Rect r = ai.mTmpInvalRect;
7323 r.set(dirty.left - scrollX, dirty.top - scrollY,
7324 dirty.right - scrollX, dirty.bottom - scrollY);
7325 mParent.invalidateChild(this, r);
7326 }
7327 }
7328 }
7329
7330 /**
7331 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7332 * The coordinates of the dirty rect are relative to the view.
7333 * If the view is visible, {@link #onDraw} will be called at some point
7334 * in the future. This must be called from a UI thread. To call
7335 * from a non-UI thread, call {@link #postInvalidate()}.
7336 * @param l the left position of the dirty region
7337 * @param t the top position of the dirty region
7338 * @param r the right position of the dirty region
7339 * @param b the bottom position of the dirty region
7340 */
7341 public void invalidate(int l, int t, int r, int b) {
7342 if (ViewDebug.TRACE_HIERARCHY) {
7343 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7344 }
7345
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007346 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007347 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7348 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007350 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007351 final ViewParent p = mParent;
7352 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007353 //noinspection PointlessBooleanExpression,ConstantConditions
7354 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7355 if (p != null && ai != null && ai.mHardwareAccelerated) {
7356 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7357 // with a null dirty rect, which tells the ViewRoot to redraw everything
7358 p.invalidateChild(this, null);
7359 return;
7360 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 if (p != null && ai != null && l < r && t < b) {
7363 final int scrollX = mScrollX;
7364 final int scrollY = mScrollY;
7365 final Rect tmpr = ai.mTmpInvalRect;
7366 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7367 p.invalidateChild(this, tmpr);
7368 }
7369 }
7370 }
7371
7372 /**
7373 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7374 * be called at some point in the future. This must be called from a
7375 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7376 */
7377 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007378 invalidate(true);
7379 }
Joe Malin32736f02011-01-19 16:14:20 -08007380
Chet Haaseed032702010-10-01 14:05:54 -07007381 /**
7382 * This is where the invalidate() work actually happens. A full invalidate()
7383 * causes the drawing cache to be invalidated, but this function can be called with
7384 * invalidateCache set to false to skip that invalidation step for cases that do not
7385 * need it (for example, a component that remains at the same dimensions with the same
7386 * content).
7387 *
7388 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7389 * well. This is usually true for a full invalidate, but may be set to false if the
7390 * View's contents or dimensions have not changed.
7391 */
Romain Guy849d0a32011-02-01 17:20:48 -08007392 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007393 if (ViewDebug.TRACE_HIERARCHY) {
7394 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7395 }
7396
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007397 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007398 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007399 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7400 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007401 mPrivateFlags &= ~DRAWN;
7402 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007403 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007404 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7405 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007406 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007407 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007408 //noinspection PointlessBooleanExpression,ConstantConditions
7409 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7410 if (p != null && ai != null && ai.mHardwareAccelerated) {
7411 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7412 // with a null dirty rect, which tells the ViewRoot to redraw everything
7413 p.invalidateChild(this, null);
7414 return;
7415 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007416 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007418 if (p != null && ai != null) {
7419 final Rect r = ai.mTmpInvalRect;
7420 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7421 // Don't call invalidate -- we don't want to internally scroll
7422 // our own bounds
7423 p.invalidateChild(this, r);
7424 }
7425 }
7426 }
7427
7428 /**
Romain Guyda489792011-02-03 01:05:15 -08007429 * @hide
7430 */
7431 public void fastInvalidate() {
7432 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7433 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7434 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7435 if (mParent instanceof View) {
7436 ((View) mParent).mPrivateFlags |= INVALIDATED;
7437 }
7438 mPrivateFlags &= ~DRAWN;
7439 mPrivateFlags |= INVALIDATED;
7440 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007441 if (mParent != null && mAttachInfo != null) {
7442 if (mAttachInfo.mHardwareAccelerated) {
7443 mParent.invalidateChild(this, null);
7444 } else {
7445 final Rect r = mAttachInfo.mTmpInvalRect;
7446 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7447 // Don't call invalidate -- we don't want to internally scroll
7448 // our own bounds
7449 mParent.invalidateChild(this, r);
7450 }
Romain Guyda489792011-02-03 01:05:15 -08007451 }
7452 }
7453 }
7454
7455 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007456 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007457 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7458 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007459 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7460 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007461 *
7462 * @hide
7463 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007464 protected void invalidateParentCaches() {
7465 if (mParent instanceof View) {
7466 ((View) mParent).mPrivateFlags |= INVALIDATED;
7467 }
7468 }
Joe Malin32736f02011-01-19 16:14:20 -08007469
Romain Guy0fd89bf2011-01-26 15:41:30 -08007470 /**
7471 * Used to indicate that the parent of this view should be invalidated. This functionality
7472 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7473 * which is necessary when various parent-managed properties of the view change, such as
7474 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7475 * an invalidation event to the parent.
7476 *
7477 * @hide
7478 */
7479 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007480 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007481 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007482 }
7483 }
7484
7485 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007486 * Indicates whether this View is opaque. An opaque View guarantees that it will
7487 * draw all the pixels overlapping its bounds using a fully opaque color.
7488 *
7489 * Subclasses of View should override this method whenever possible to indicate
7490 * whether an instance is opaque. Opaque Views are treated in a special way by
7491 * the View hierarchy, possibly allowing it to perform optimizations during
7492 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007493 *
Romain Guy24443ea2009-05-11 11:56:30 -07007494 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007495 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007496 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007497 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007498 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7499 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007500 }
7501
Adam Powell20232d02010-12-08 21:08:53 -08007502 /**
7503 * @hide
7504 */
7505 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007506 // Opaque if:
7507 // - Has a background
7508 // - Background is opaque
7509 // - Doesn't have scrollbars or scrollbars are inside overlay
7510
7511 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7512 mPrivateFlags |= OPAQUE_BACKGROUND;
7513 } else {
7514 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7515 }
7516
7517 final int flags = mViewFlags;
7518 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7519 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7520 mPrivateFlags |= OPAQUE_SCROLLBARS;
7521 } else {
7522 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7523 }
7524 }
7525
7526 /**
7527 * @hide
7528 */
7529 protected boolean hasOpaqueScrollbars() {
7530 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007531 }
7532
7533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007534 * @return A handler associated with the thread running the View. This
7535 * handler can be used to pump events in the UI events queue.
7536 */
7537 public Handler getHandler() {
7538 if (mAttachInfo != null) {
7539 return mAttachInfo.mHandler;
7540 }
7541 return null;
7542 }
7543
7544 /**
7545 * Causes the Runnable to be added to the message queue.
7546 * The runnable will be run on the user interface thread.
7547 *
7548 * @param action The Runnable that will be executed.
7549 *
7550 * @return Returns true if the Runnable was successfully placed in to the
7551 * message queue. Returns false on failure, usually because the
7552 * looper processing the message queue is exiting.
7553 */
7554 public boolean post(Runnable action) {
7555 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007556 AttachInfo attachInfo = mAttachInfo;
7557 if (attachInfo != null) {
7558 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007559 } else {
7560 // Assume that post will succeed later
7561 ViewRoot.getRunQueue().post(action);
7562 return true;
7563 }
7564
7565 return handler.post(action);
7566 }
7567
7568 /**
7569 * Causes the Runnable to be added to the message queue, to be run
7570 * after the specified amount of time elapses.
7571 * The runnable will be run on the user interface thread.
7572 *
7573 * @param action The Runnable that will be executed.
7574 * @param delayMillis The delay (in milliseconds) until the Runnable
7575 * will be executed.
7576 *
7577 * @return true if the Runnable was successfully placed in to the
7578 * message queue. Returns false on failure, usually because the
7579 * looper processing the message queue is exiting. Note that a
7580 * result of true does not mean the Runnable will be processed --
7581 * if the looper is quit before the delivery time of the message
7582 * occurs then the message will be dropped.
7583 */
7584 public boolean postDelayed(Runnable action, long delayMillis) {
7585 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007586 AttachInfo attachInfo = mAttachInfo;
7587 if (attachInfo != null) {
7588 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007589 } else {
7590 // Assume that post will succeed later
7591 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7592 return true;
7593 }
7594
7595 return handler.postDelayed(action, delayMillis);
7596 }
7597
7598 /**
7599 * Removes the specified Runnable from the message queue.
7600 *
7601 * @param action The Runnable to remove from the message handling queue
7602 *
7603 * @return true if this view could ask the Handler to remove the Runnable,
7604 * false otherwise. When the returned value is true, the Runnable
7605 * may or may not have been actually removed from the message queue
7606 * (for instance, if the Runnable was not in the queue already.)
7607 */
7608 public boolean removeCallbacks(Runnable action) {
7609 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007610 AttachInfo attachInfo = mAttachInfo;
7611 if (attachInfo != null) {
7612 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007613 } else {
7614 // Assume that post will succeed later
7615 ViewRoot.getRunQueue().removeCallbacks(action);
7616 return true;
7617 }
7618
7619 handler.removeCallbacks(action);
7620 return true;
7621 }
7622
7623 /**
7624 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7625 * Use this to invalidate the View from a non-UI thread.
7626 *
7627 * @see #invalidate()
7628 */
7629 public void postInvalidate() {
7630 postInvalidateDelayed(0);
7631 }
7632
7633 /**
7634 * Cause an invalidate of the specified area to happen on a subsequent cycle
7635 * through the event loop. Use this to invalidate the View from a non-UI thread.
7636 *
7637 * @param left The left coordinate of the rectangle to invalidate.
7638 * @param top The top coordinate of the rectangle to invalidate.
7639 * @param right The right coordinate of the rectangle to invalidate.
7640 * @param bottom The bottom coordinate of the rectangle to invalidate.
7641 *
7642 * @see #invalidate(int, int, int, int)
7643 * @see #invalidate(Rect)
7644 */
7645 public void postInvalidate(int left, int top, int right, int bottom) {
7646 postInvalidateDelayed(0, left, top, right, bottom);
7647 }
7648
7649 /**
7650 * Cause an invalidate to happen on a subsequent cycle through the event
7651 * loop. Waits for the specified amount of time.
7652 *
7653 * @param delayMilliseconds the duration in milliseconds to delay the
7654 * invalidation by
7655 */
7656 public void postInvalidateDelayed(long delayMilliseconds) {
7657 // We try only with the AttachInfo because there's no point in invalidating
7658 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007659 AttachInfo attachInfo = mAttachInfo;
7660 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 Message msg = Message.obtain();
7662 msg.what = AttachInfo.INVALIDATE_MSG;
7663 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007664 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007665 }
7666 }
7667
7668 /**
7669 * Cause an invalidate of the specified area to happen on a subsequent cycle
7670 * through the event loop. Waits for the specified amount of time.
7671 *
7672 * @param delayMilliseconds the duration in milliseconds to delay the
7673 * invalidation by
7674 * @param left The left coordinate of the rectangle to invalidate.
7675 * @param top The top coordinate of the rectangle to invalidate.
7676 * @param right The right coordinate of the rectangle to invalidate.
7677 * @param bottom The bottom coordinate of the rectangle to invalidate.
7678 */
7679 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7680 int right, int bottom) {
7681
7682 // We try only with the AttachInfo because there's no point in invalidating
7683 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007684 AttachInfo attachInfo = mAttachInfo;
7685 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007686 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7687 info.target = this;
7688 info.left = left;
7689 info.top = top;
7690 info.right = right;
7691 info.bottom = bottom;
7692
7693 final Message msg = Message.obtain();
7694 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7695 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007696 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007697 }
7698 }
7699
7700 /**
7701 * Called by a parent to request that a child update its values for mScrollX
7702 * and mScrollY if necessary. This will typically be done if the child is
7703 * animating a scroll using a {@link android.widget.Scroller Scroller}
7704 * object.
7705 */
7706 public void computeScroll() {
7707 }
7708
7709 /**
7710 * <p>Indicate whether the horizontal edges are faded when the view is
7711 * scrolled horizontally.</p>
7712 *
7713 * @return true if the horizontal edges should are faded on scroll, false
7714 * otherwise
7715 *
7716 * @see #setHorizontalFadingEdgeEnabled(boolean)
7717 * @attr ref android.R.styleable#View_fadingEdge
7718 */
7719 public boolean isHorizontalFadingEdgeEnabled() {
7720 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7721 }
7722
7723 /**
7724 * <p>Define whether the horizontal edges should be faded when this view
7725 * is scrolled horizontally.</p>
7726 *
7727 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7728 * be faded when the view is scrolled
7729 * horizontally
7730 *
7731 * @see #isHorizontalFadingEdgeEnabled()
7732 * @attr ref android.R.styleable#View_fadingEdge
7733 */
7734 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7735 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7736 if (horizontalFadingEdgeEnabled) {
7737 initScrollCache();
7738 }
7739
7740 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7741 }
7742 }
7743
7744 /**
7745 * <p>Indicate whether the vertical edges are faded when the view is
7746 * scrolled horizontally.</p>
7747 *
7748 * @return true if the vertical edges should are faded on scroll, false
7749 * otherwise
7750 *
7751 * @see #setVerticalFadingEdgeEnabled(boolean)
7752 * @attr ref android.R.styleable#View_fadingEdge
7753 */
7754 public boolean isVerticalFadingEdgeEnabled() {
7755 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7756 }
7757
7758 /**
7759 * <p>Define whether the vertical edges should be faded when this view
7760 * is scrolled vertically.</p>
7761 *
7762 * @param verticalFadingEdgeEnabled true if the vertical edges should
7763 * be faded when the view is scrolled
7764 * vertically
7765 *
7766 * @see #isVerticalFadingEdgeEnabled()
7767 * @attr ref android.R.styleable#View_fadingEdge
7768 */
7769 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7770 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7771 if (verticalFadingEdgeEnabled) {
7772 initScrollCache();
7773 }
7774
7775 mViewFlags ^= FADING_EDGE_VERTICAL;
7776 }
7777 }
7778
7779 /**
7780 * Returns the strength, or intensity, of the top faded edge. The strength is
7781 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7782 * returns 0.0 or 1.0 but no value in between.
7783 *
7784 * Subclasses should override this method to provide a smoother fade transition
7785 * when scrolling occurs.
7786 *
7787 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7788 */
7789 protected float getTopFadingEdgeStrength() {
7790 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7791 }
7792
7793 /**
7794 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7795 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7796 * returns 0.0 or 1.0 but no value in between.
7797 *
7798 * Subclasses should override this method to provide a smoother fade transition
7799 * when scrolling occurs.
7800 *
7801 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7802 */
7803 protected float getBottomFadingEdgeStrength() {
7804 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7805 computeVerticalScrollRange() ? 1.0f : 0.0f;
7806 }
7807
7808 /**
7809 * Returns the strength, or intensity, of the left faded edge. The strength is
7810 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7811 * returns 0.0 or 1.0 but no value in between.
7812 *
7813 * Subclasses should override this method to provide a smoother fade transition
7814 * when scrolling occurs.
7815 *
7816 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7817 */
7818 protected float getLeftFadingEdgeStrength() {
7819 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7820 }
7821
7822 /**
7823 * Returns the strength, or intensity, of the right faded edge. The strength is
7824 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7825 * returns 0.0 or 1.0 but no value in between.
7826 *
7827 * Subclasses should override this method to provide a smoother fade transition
7828 * when scrolling occurs.
7829 *
7830 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7831 */
7832 protected float getRightFadingEdgeStrength() {
7833 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7834 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7835 }
7836
7837 /**
7838 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7839 * scrollbar is not drawn by default.</p>
7840 *
7841 * @return true if the horizontal scrollbar should be painted, false
7842 * otherwise
7843 *
7844 * @see #setHorizontalScrollBarEnabled(boolean)
7845 */
7846 public boolean isHorizontalScrollBarEnabled() {
7847 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7848 }
7849
7850 /**
7851 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7852 * scrollbar is not drawn by default.</p>
7853 *
7854 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7855 * be painted
7856 *
7857 * @see #isHorizontalScrollBarEnabled()
7858 */
7859 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7860 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7861 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007862 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007863 recomputePadding();
7864 }
7865 }
7866
7867 /**
7868 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7869 * scrollbar is not drawn by default.</p>
7870 *
7871 * @return true if the vertical scrollbar should be painted, false
7872 * otherwise
7873 *
7874 * @see #setVerticalScrollBarEnabled(boolean)
7875 */
7876 public boolean isVerticalScrollBarEnabled() {
7877 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7878 }
7879
7880 /**
7881 * <p>Define whether the vertical scrollbar should be drawn or not. The
7882 * scrollbar is not drawn by default.</p>
7883 *
7884 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7885 * be painted
7886 *
7887 * @see #isVerticalScrollBarEnabled()
7888 */
7889 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7890 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7891 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007892 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007893 recomputePadding();
7894 }
7895 }
7896
Adam Powell20232d02010-12-08 21:08:53 -08007897 /**
7898 * @hide
7899 */
7900 protected void recomputePadding() {
7901 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007902 }
Joe Malin32736f02011-01-19 16:14:20 -08007903
Mike Cleronfe81d382009-09-28 14:22:16 -07007904 /**
7905 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007906 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007907 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007908 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007909 */
7910 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7911 initScrollCache();
7912 final ScrollabilityCache scrollabilityCache = mScrollCache;
7913 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007914 if (fadeScrollbars) {
7915 scrollabilityCache.state = ScrollabilityCache.OFF;
7916 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007917 scrollabilityCache.state = ScrollabilityCache.ON;
7918 }
7919 }
Joe Malin32736f02011-01-19 16:14:20 -08007920
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 /**
Joe Malin32736f02011-01-19 16:14:20 -08007922 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007923 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007924 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007925 * @return true if scrollbar fading is enabled
7926 */
7927 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007928 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007929 }
Joe Malin32736f02011-01-19 16:14:20 -08007930
Mike Cleron52f0a642009-09-28 18:21:37 -07007931 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007932 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7933 * inset. When inset, they add to the padding of the view. And the scrollbars
7934 * can be drawn inside the padding area or on the edge of the view. For example,
7935 * if a view has a background drawable and you want to draw the scrollbars
7936 * inside the padding specified by the drawable, you can use
7937 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7938 * appear at the edge of the view, ignoring the padding, then you can use
7939 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7940 * @param style the style of the scrollbars. Should be one of
7941 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7942 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7943 * @see #SCROLLBARS_INSIDE_OVERLAY
7944 * @see #SCROLLBARS_INSIDE_INSET
7945 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7946 * @see #SCROLLBARS_OUTSIDE_INSET
7947 */
7948 public void setScrollBarStyle(int style) {
7949 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7950 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007951 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007952 recomputePadding();
7953 }
7954 }
7955
7956 /**
7957 * <p>Returns the current scrollbar style.</p>
7958 * @return the current scrollbar style
7959 * @see #SCROLLBARS_INSIDE_OVERLAY
7960 * @see #SCROLLBARS_INSIDE_INSET
7961 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7962 * @see #SCROLLBARS_OUTSIDE_INSET
7963 */
7964 public int getScrollBarStyle() {
7965 return mViewFlags & SCROLLBARS_STYLE_MASK;
7966 }
7967
7968 /**
7969 * <p>Compute the horizontal range that the horizontal scrollbar
7970 * represents.</p>
7971 *
7972 * <p>The range is expressed in arbitrary units that must be the same as the
7973 * units used by {@link #computeHorizontalScrollExtent()} and
7974 * {@link #computeHorizontalScrollOffset()}.</p>
7975 *
7976 * <p>The default range is the drawing width of this view.</p>
7977 *
7978 * @return the total horizontal range represented by the horizontal
7979 * scrollbar
7980 *
7981 * @see #computeHorizontalScrollExtent()
7982 * @see #computeHorizontalScrollOffset()
7983 * @see android.widget.ScrollBarDrawable
7984 */
7985 protected int computeHorizontalScrollRange() {
7986 return getWidth();
7987 }
7988
7989 /**
7990 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7991 * within the horizontal range. This value is used to compute the position
7992 * of the thumb within the scrollbar's track.</p>
7993 *
7994 * <p>The range is expressed in arbitrary units that must be the same as the
7995 * units used by {@link #computeHorizontalScrollRange()} and
7996 * {@link #computeHorizontalScrollExtent()}.</p>
7997 *
7998 * <p>The default offset is the scroll offset of this view.</p>
7999 *
8000 * @return the horizontal offset of the scrollbar's thumb
8001 *
8002 * @see #computeHorizontalScrollRange()
8003 * @see #computeHorizontalScrollExtent()
8004 * @see android.widget.ScrollBarDrawable
8005 */
8006 protected int computeHorizontalScrollOffset() {
8007 return mScrollX;
8008 }
8009
8010 /**
8011 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8012 * within the horizontal range. This value is used to compute the length
8013 * of the thumb within the scrollbar's track.</p>
8014 *
8015 * <p>The range is expressed in arbitrary units that must be the same as the
8016 * units used by {@link #computeHorizontalScrollRange()} and
8017 * {@link #computeHorizontalScrollOffset()}.</p>
8018 *
8019 * <p>The default extent is the drawing width of this view.</p>
8020 *
8021 * @return the horizontal extent of the scrollbar's thumb
8022 *
8023 * @see #computeHorizontalScrollRange()
8024 * @see #computeHorizontalScrollOffset()
8025 * @see android.widget.ScrollBarDrawable
8026 */
8027 protected int computeHorizontalScrollExtent() {
8028 return getWidth();
8029 }
8030
8031 /**
8032 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8033 *
8034 * <p>The range is expressed in arbitrary units that must be the same as the
8035 * units used by {@link #computeVerticalScrollExtent()} and
8036 * {@link #computeVerticalScrollOffset()}.</p>
8037 *
8038 * @return the total vertical range represented by the vertical scrollbar
8039 *
8040 * <p>The default range is the drawing height of this view.</p>
8041 *
8042 * @see #computeVerticalScrollExtent()
8043 * @see #computeVerticalScrollOffset()
8044 * @see android.widget.ScrollBarDrawable
8045 */
8046 protected int computeVerticalScrollRange() {
8047 return getHeight();
8048 }
8049
8050 /**
8051 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8052 * within the horizontal range. This value is used to compute the position
8053 * of the thumb within the scrollbar's track.</p>
8054 *
8055 * <p>The range is expressed in arbitrary units that must be the same as the
8056 * units used by {@link #computeVerticalScrollRange()} and
8057 * {@link #computeVerticalScrollExtent()}.</p>
8058 *
8059 * <p>The default offset is the scroll offset of this view.</p>
8060 *
8061 * @return the vertical offset of the scrollbar's thumb
8062 *
8063 * @see #computeVerticalScrollRange()
8064 * @see #computeVerticalScrollExtent()
8065 * @see android.widget.ScrollBarDrawable
8066 */
8067 protected int computeVerticalScrollOffset() {
8068 return mScrollY;
8069 }
8070
8071 /**
8072 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8073 * within the vertical range. This value is used to compute the length
8074 * of the thumb within the scrollbar's track.</p>
8075 *
8076 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008077 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008078 * {@link #computeVerticalScrollOffset()}.</p>
8079 *
8080 * <p>The default extent is the drawing height of this view.</p>
8081 *
8082 * @return the vertical extent of the scrollbar's thumb
8083 *
8084 * @see #computeVerticalScrollRange()
8085 * @see #computeVerticalScrollOffset()
8086 * @see android.widget.ScrollBarDrawable
8087 */
8088 protected int computeVerticalScrollExtent() {
8089 return getHeight();
8090 }
8091
8092 /**
8093 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8094 * scrollbars are painted only if they have been awakened first.</p>
8095 *
8096 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008097 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008098 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008100 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008101 // scrollbars are drawn only when the animation is running
8102 final ScrollabilityCache cache = mScrollCache;
8103 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008104
Mike Cleronf116bf82009-09-27 19:14:12 -07008105 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008106
Mike Cleronf116bf82009-09-27 19:14:12 -07008107 if (state == ScrollabilityCache.OFF) {
8108 return;
8109 }
Joe Malin32736f02011-01-19 16:14:20 -08008110
Mike Cleronf116bf82009-09-27 19:14:12 -07008111 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008112
Mike Cleronf116bf82009-09-27 19:14:12 -07008113 if (state == ScrollabilityCache.FADING) {
8114 // We're fading -- get our fade interpolation
8115 if (cache.interpolatorValues == null) {
8116 cache.interpolatorValues = new float[1];
8117 }
Joe Malin32736f02011-01-19 16:14:20 -08008118
Mike Cleronf116bf82009-09-27 19:14:12 -07008119 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008120
Mike Cleronf116bf82009-09-27 19:14:12 -07008121 // Stops the animation if we're done
8122 if (cache.scrollBarInterpolator.timeToValues(values) ==
8123 Interpolator.Result.FREEZE_END) {
8124 cache.state = ScrollabilityCache.OFF;
8125 } else {
8126 cache.scrollBar.setAlpha(Math.round(values[0]));
8127 }
Joe Malin32736f02011-01-19 16:14:20 -08008128
8129 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008130 // drawing. We only want this when we're fading so that
8131 // we prevent excessive redraws
8132 invalidate = true;
8133 } else {
8134 // We're just on -- but we may have been fading before so
8135 // reset alpha
8136 cache.scrollBar.setAlpha(255);
8137 }
8138
Joe Malin32736f02011-01-19 16:14:20 -08008139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008140 final int viewFlags = mViewFlags;
8141
8142 final boolean drawHorizontalScrollBar =
8143 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8144 final boolean drawVerticalScrollBar =
8145 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8146 && !isVerticalScrollBarHidden();
8147
8148 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8149 final int width = mRight - mLeft;
8150 final int height = mBottom - mTop;
8151
8152 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153
Mike Reede8853fc2009-09-04 14:01:48 -04008154 final int scrollX = mScrollX;
8155 final int scrollY = mScrollY;
8156 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8157
Mike Cleronf116bf82009-09-27 19:14:12 -07008158 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008159
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008160 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008161 int size = scrollBar.getSize(false);
8162 if (size <= 0) {
8163 size = cache.scrollBarSize;
8164 }
8165
Mike Cleronf116bf82009-09-27 19:14:12 -07008166 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008167 computeHorizontalScrollOffset(),
8168 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008169 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008170 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008171 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008172 left = scrollX + (mPaddingLeft & inside);
8173 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8174 bottom = top + size;
8175 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8176 if (invalidate) {
8177 invalidate(left, top, right, bottom);
8178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008179 }
8180
8181 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008182 int size = scrollBar.getSize(true);
8183 if (size <= 0) {
8184 size = cache.scrollBarSize;
8185 }
8186
Mike Reede8853fc2009-09-04 14:01:48 -04008187 scrollBar.setParameters(computeVerticalScrollRange(),
8188 computeVerticalScrollOffset(),
8189 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008190 switch (mVerticalScrollbarPosition) {
8191 default:
8192 case SCROLLBAR_POSITION_DEFAULT:
8193 case SCROLLBAR_POSITION_RIGHT:
8194 left = scrollX + width - size - (mUserPaddingRight & inside);
8195 break;
8196 case SCROLLBAR_POSITION_LEFT:
8197 left = scrollX + (mUserPaddingLeft & inside);
8198 break;
8199 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008200 top = scrollY + (mPaddingTop & inside);
8201 right = left + size;
8202 bottom = scrollY + height - (mUserPaddingBottom & inside);
8203 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8204 if (invalidate) {
8205 invalidate(left, top, right, bottom);
8206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 }
8208 }
8209 }
8210 }
Romain Guy8506ab42009-06-11 17:35:47 -07008211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008213 * 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 -08008214 * FastScroller is visible.
8215 * @return whether to temporarily hide the vertical scrollbar
8216 * @hide
8217 */
8218 protected boolean isVerticalScrollBarHidden() {
8219 return false;
8220 }
8221
8222 /**
8223 * <p>Draw the horizontal scrollbar if
8224 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8225 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008226 * @param canvas the canvas on which to draw the scrollbar
8227 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 *
8229 * @see #isHorizontalScrollBarEnabled()
8230 * @see #computeHorizontalScrollRange()
8231 * @see #computeHorizontalScrollExtent()
8232 * @see #computeHorizontalScrollOffset()
8233 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008234 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008235 */
Romain Guy8fb95422010-08-17 18:38:51 -07008236 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8237 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008238 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008239 scrollBar.draw(canvas);
8240 }
Mike Reede8853fc2009-09-04 14:01:48 -04008241
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008243 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8244 * returns true.</p>
8245 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008246 * @param canvas the canvas on which to draw the scrollbar
8247 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008248 *
8249 * @see #isVerticalScrollBarEnabled()
8250 * @see #computeVerticalScrollRange()
8251 * @see #computeVerticalScrollExtent()
8252 * @see #computeVerticalScrollOffset()
8253 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008254 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 */
Romain Guy8fb95422010-08-17 18:38:51 -07008256 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8257 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008258 scrollBar.setBounds(l, t, r, b);
8259 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008260 }
8261
8262 /**
8263 * Implement this to do your drawing.
8264 *
8265 * @param canvas the canvas on which the background will be drawn
8266 */
8267 protected void onDraw(Canvas canvas) {
8268 }
8269
8270 /*
8271 * Caller is responsible for calling requestLayout if necessary.
8272 * (This allows addViewInLayout to not request a new layout.)
8273 */
8274 void assignParent(ViewParent parent) {
8275 if (mParent == null) {
8276 mParent = parent;
8277 } else if (parent == null) {
8278 mParent = null;
8279 } else {
8280 throw new RuntimeException("view " + this + " being added, but"
8281 + " it already has a parent");
8282 }
8283 }
8284
8285 /**
8286 * This is called when the view is attached to a window. At this point it
8287 * has a Surface and will start drawing. Note that this function is
8288 * guaranteed to be called before {@link #onDraw}, however it may be called
8289 * any time before the first onDraw -- including before or after
8290 * {@link #onMeasure}.
8291 *
8292 * @see #onDetachedFromWindow()
8293 */
8294 protected void onAttachedToWindow() {
8295 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8296 mParent.requestTransparentRegion(this);
8297 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008298 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8299 initialAwakenScrollBars();
8300 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8301 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008302 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008303 }
8304
8305 /**
8306 * This is called when the view is detached from a window. At this point it
8307 * no longer has a surface for drawing.
8308 *
8309 * @see #onAttachedToWindow()
8310 */
8311 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008312 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008313
Romain Guya440b002010-02-24 15:57:54 -08008314 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008315 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008316 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008318 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008319
8320 if (mHardwareLayer != null) {
8321 mHardwareLayer.destroy();
8322 mHardwareLayer = null;
8323 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008324
Chet Haasedaf98e92011-01-10 14:10:36 -08008325 if (mDisplayList != null) {
8326 mDisplayList.invalidate();
8327 }
8328
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008329 if (mAttachInfo != null) {
8330 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8331 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8332 }
8333
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008334 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008335 }
8336
8337 /**
8338 * @return The number of times this view has been attached to a window
8339 */
8340 protected int getWindowAttachCount() {
8341 return mWindowAttachCount;
8342 }
8343
8344 /**
8345 * Retrieve a unique token identifying the window this view is attached to.
8346 * @return Return the window's token for use in
8347 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8348 */
8349 public IBinder getWindowToken() {
8350 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8351 }
8352
8353 /**
8354 * Retrieve a unique token identifying the top-level "real" window of
8355 * the window that this view is attached to. That is, this is like
8356 * {@link #getWindowToken}, except if the window this view in is a panel
8357 * window (attached to another containing window), then the token of
8358 * the containing window is returned instead.
8359 *
8360 * @return Returns the associated window token, either
8361 * {@link #getWindowToken()} or the containing window's token.
8362 */
8363 public IBinder getApplicationWindowToken() {
8364 AttachInfo ai = mAttachInfo;
8365 if (ai != null) {
8366 IBinder appWindowToken = ai.mPanelParentWindowToken;
8367 if (appWindowToken == null) {
8368 appWindowToken = ai.mWindowToken;
8369 }
8370 return appWindowToken;
8371 }
8372 return null;
8373 }
8374
8375 /**
8376 * Retrieve private session object this view hierarchy is using to
8377 * communicate with the window manager.
8378 * @return the session object to communicate with the window manager
8379 */
8380 /*package*/ IWindowSession getWindowSession() {
8381 return mAttachInfo != null ? mAttachInfo.mSession : null;
8382 }
8383
8384 /**
8385 * @param info the {@link android.view.View.AttachInfo} to associated with
8386 * this view
8387 */
8388 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8389 //System.out.println("Attached! " + this);
8390 mAttachInfo = info;
8391 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008392 // We will need to evaluate the drawable state at least once.
8393 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008394 if (mFloatingTreeObserver != null) {
8395 info.mTreeObserver.merge(mFloatingTreeObserver);
8396 mFloatingTreeObserver = null;
8397 }
8398 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8399 mAttachInfo.mScrollContainers.add(this);
8400 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8401 }
8402 performCollectViewAttributes(visibility);
8403 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008404
8405 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8406 mOnAttachStateChangeListeners;
8407 if (listeners != null && listeners.size() > 0) {
8408 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8409 // perform the dispatching. The iterator is a safe guard against listeners that
8410 // could mutate the list by calling the various add/remove methods. This prevents
8411 // the array from being modified while we iterate it.
8412 for (OnAttachStateChangeListener listener : listeners) {
8413 listener.onViewAttachedToWindow(this);
8414 }
8415 }
8416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 int vis = info.mWindowVisibility;
8418 if (vis != GONE) {
8419 onWindowVisibilityChanged(vis);
8420 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008421 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8422 // If nobody has evaluated the drawable state yet, then do it now.
8423 refreshDrawableState();
8424 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008425 }
8426
8427 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 AttachInfo info = mAttachInfo;
8429 if (info != null) {
8430 int vis = info.mWindowVisibility;
8431 if (vis != GONE) {
8432 onWindowVisibilityChanged(GONE);
8433 }
8434 }
8435
8436 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008437
Adam Powell4afd62b2011-02-18 15:02:18 -08008438 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8439 mOnAttachStateChangeListeners;
8440 if (listeners != null && listeners.size() > 0) {
8441 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8442 // perform the dispatching. The iterator is a safe guard against listeners that
8443 // could mutate the list by calling the various add/remove methods. This prevents
8444 // the array from being modified while we iterate it.
8445 for (OnAttachStateChangeListener listener : listeners) {
8446 listener.onViewDetachedFromWindow(this);
8447 }
8448 }
8449
Romain Guy01d5edc2011-01-28 11:28:53 -08008450 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008451 mAttachInfo.mScrollContainers.remove(this);
8452 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8453 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008454
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008455 mAttachInfo = null;
8456 }
8457
8458 /**
8459 * Store this view hierarchy's frozen state into the given container.
8460 *
8461 * @param container The SparseArray in which to save the view's state.
8462 *
8463 * @see #restoreHierarchyState
8464 * @see #dispatchSaveInstanceState
8465 * @see #onSaveInstanceState
8466 */
8467 public void saveHierarchyState(SparseArray<Parcelable> container) {
8468 dispatchSaveInstanceState(container);
8469 }
8470
8471 /**
8472 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8473 * May be overridden to modify how freezing happens to a view's children; for example, some
8474 * views may want to not store state for their children.
8475 *
8476 * @param container The SparseArray in which to save the view's state.
8477 *
8478 * @see #dispatchRestoreInstanceState
8479 * @see #saveHierarchyState
8480 * @see #onSaveInstanceState
8481 */
8482 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8483 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8484 mPrivateFlags &= ~SAVE_STATE_CALLED;
8485 Parcelable state = onSaveInstanceState();
8486 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8487 throw new IllegalStateException(
8488 "Derived class did not call super.onSaveInstanceState()");
8489 }
8490 if (state != null) {
8491 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8492 // + ": " + state);
8493 container.put(mID, state);
8494 }
8495 }
8496 }
8497
8498 /**
8499 * Hook allowing a view to generate a representation of its internal state
8500 * that can later be used to create a new instance with that same state.
8501 * This state should only contain information that is not persistent or can
8502 * not be reconstructed later. For example, you will never store your
8503 * current position on screen because that will be computed again when a
8504 * new instance of the view is placed in its view hierarchy.
8505 * <p>
8506 * Some examples of things you may store here: the current cursor position
8507 * in a text view (but usually not the text itself since that is stored in a
8508 * content provider or other persistent storage), the currently selected
8509 * item in a list view.
8510 *
8511 * @return Returns a Parcelable object containing the view's current dynamic
8512 * state, or null if there is nothing interesting to save. The
8513 * default implementation returns null.
8514 * @see #onRestoreInstanceState
8515 * @see #saveHierarchyState
8516 * @see #dispatchSaveInstanceState
8517 * @see #setSaveEnabled(boolean)
8518 */
8519 protected Parcelable onSaveInstanceState() {
8520 mPrivateFlags |= SAVE_STATE_CALLED;
8521 return BaseSavedState.EMPTY_STATE;
8522 }
8523
8524 /**
8525 * Restore this view hierarchy's frozen state from the given container.
8526 *
8527 * @param container The SparseArray which holds previously frozen states.
8528 *
8529 * @see #saveHierarchyState
8530 * @see #dispatchRestoreInstanceState
8531 * @see #onRestoreInstanceState
8532 */
8533 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8534 dispatchRestoreInstanceState(container);
8535 }
8536
8537 /**
8538 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8539 * children. May be overridden to modify how restoreing happens to a view's children; for
8540 * example, some views may want to not store state for their children.
8541 *
8542 * @param container The SparseArray which holds previously saved state.
8543 *
8544 * @see #dispatchSaveInstanceState
8545 * @see #restoreHierarchyState
8546 * @see #onRestoreInstanceState
8547 */
8548 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8549 if (mID != NO_ID) {
8550 Parcelable state = container.get(mID);
8551 if (state != null) {
8552 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8553 // + ": " + state);
8554 mPrivateFlags &= ~SAVE_STATE_CALLED;
8555 onRestoreInstanceState(state);
8556 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8557 throw new IllegalStateException(
8558 "Derived class did not call super.onRestoreInstanceState()");
8559 }
8560 }
8561 }
8562 }
8563
8564 /**
8565 * Hook allowing a view to re-apply a representation of its internal state that had previously
8566 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8567 * null state.
8568 *
8569 * @param state The frozen state that had previously been returned by
8570 * {@link #onSaveInstanceState}.
8571 *
8572 * @see #onSaveInstanceState
8573 * @see #restoreHierarchyState
8574 * @see #dispatchRestoreInstanceState
8575 */
8576 protected void onRestoreInstanceState(Parcelable state) {
8577 mPrivateFlags |= SAVE_STATE_CALLED;
8578 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008579 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8580 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008581 + "when two views of different type have the same id in the same hierarchy. "
8582 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008583 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008584 }
8585 }
8586
8587 /**
8588 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8589 *
8590 * @return the drawing start time in milliseconds
8591 */
8592 public long getDrawingTime() {
8593 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8594 }
8595
8596 /**
8597 * <p>Enables or disables the duplication of the parent's state into this view. When
8598 * duplication is enabled, this view gets its drawable state from its parent rather
8599 * than from its own internal properties.</p>
8600 *
8601 * <p>Note: in the current implementation, setting this property to true after the
8602 * view was added to a ViewGroup might have no effect at all. This property should
8603 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8604 *
8605 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8606 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008607 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008608 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8609 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 *
8611 * @param enabled True to enable duplication of the parent's drawable state, false
8612 * to disable it.
8613 *
8614 * @see #getDrawableState()
8615 * @see #isDuplicateParentStateEnabled()
8616 */
8617 public void setDuplicateParentStateEnabled(boolean enabled) {
8618 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8619 }
8620
8621 /**
8622 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8623 *
8624 * @return True if this view's drawable state is duplicated from the parent,
8625 * false otherwise
8626 *
8627 * @see #getDrawableState()
8628 * @see #setDuplicateParentStateEnabled(boolean)
8629 */
8630 public boolean isDuplicateParentStateEnabled() {
8631 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8632 }
8633
8634 /**
Romain Guy171c5922011-01-06 10:04:23 -08008635 * <p>Specifies the type of layer backing this view. The layer can be
8636 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8637 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008638 *
Romain Guy171c5922011-01-06 10:04:23 -08008639 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8640 * instance that controls how the layer is composed on screen. The following
8641 * properties of the paint are taken into account when composing the layer:</p>
8642 * <ul>
8643 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8644 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8645 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8646 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008647 *
Romain Guy171c5922011-01-06 10:04:23 -08008648 * <p>If this view has an alpha value set to < 1.0 by calling
8649 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8650 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8651 * equivalent to setting a hardware layer on this view and providing a paint with
8652 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008653 *
Romain Guy171c5922011-01-06 10:04:23 -08008654 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8655 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8656 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008657 *
Romain Guy171c5922011-01-06 10:04:23 -08008658 * @param layerType The ype of layer to use with this view, must be one of
8659 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8660 * {@link #LAYER_TYPE_HARDWARE}
8661 * @param paint The paint used to compose the layer. This argument is optional
8662 * and can be null. It is ignored when the layer type is
8663 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008664 *
8665 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008666 * @see #LAYER_TYPE_NONE
8667 * @see #LAYER_TYPE_SOFTWARE
8668 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008669 * @see #setAlpha(float)
8670 *
Romain Guy171c5922011-01-06 10:04:23 -08008671 * @attr ref android.R.styleable#View_layerType
8672 */
8673 public void setLayerType(int layerType, Paint paint) {
8674 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008675 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008676 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8677 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008678
Romain Guyd6cd5722011-01-17 14:42:41 -08008679 if (layerType == mLayerType) {
8680 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8681 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008682 invalidateParentCaches();
8683 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008684 }
8685 return;
8686 }
Romain Guy171c5922011-01-06 10:04:23 -08008687
8688 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008689 switch (mLayerType) {
8690 case LAYER_TYPE_SOFTWARE:
8691 if (mDrawingCache != null) {
8692 mDrawingCache.recycle();
8693 mDrawingCache = null;
8694 }
Joe Malin32736f02011-01-19 16:14:20 -08008695
Romain Guy6c319ca2011-01-11 14:29:25 -08008696 if (mUnscaledDrawingCache != null) {
8697 mUnscaledDrawingCache.recycle();
8698 mUnscaledDrawingCache = null;
8699 }
8700 break;
8701 case LAYER_TYPE_HARDWARE:
8702 if (mHardwareLayer != null) {
8703 mHardwareLayer.destroy();
8704 mHardwareLayer = null;
8705 }
8706 break;
8707 default:
8708 break;
Romain Guy171c5922011-01-06 10:04:23 -08008709 }
8710
8711 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008712 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8713 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8714 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008715
Romain Guy0fd89bf2011-01-26 15:41:30 -08008716 invalidateParentCaches();
8717 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008718 }
8719
8720 /**
8721 * Indicates what type of layer is currently associated with this view. By default
8722 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8723 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8724 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008725 *
Romain Guy171c5922011-01-06 10:04:23 -08008726 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8727 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008728 *
8729 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008730 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008731 * @see #LAYER_TYPE_NONE
8732 * @see #LAYER_TYPE_SOFTWARE
8733 * @see #LAYER_TYPE_HARDWARE
8734 */
8735 public int getLayerType() {
8736 return mLayerType;
8737 }
Joe Malin32736f02011-01-19 16:14:20 -08008738
Romain Guy6c319ca2011-01-11 14:29:25 -08008739 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008740 * Forces this view's layer to be created and this view to be rendered
8741 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8742 * invoking this method will have no effect.
8743 *
8744 * This method can for instance be used to render a view into its layer before
8745 * starting an animation. If this view is complex, rendering into the layer
8746 * before starting the animation will avoid skipping frames.
8747 *
8748 * @throws IllegalStateException If this view is not attached to a window
8749 *
8750 * @see #setLayerType(int, android.graphics.Paint)
8751 */
8752 public void buildLayer() {
8753 if (mLayerType == LAYER_TYPE_NONE) return;
8754
8755 if (mAttachInfo == null) {
8756 throw new IllegalStateException("This view must be attached to a window first");
8757 }
8758
8759 switch (mLayerType) {
8760 case LAYER_TYPE_HARDWARE:
8761 getHardwareLayer();
8762 break;
8763 case LAYER_TYPE_SOFTWARE:
8764 buildDrawingCache(true);
8765 break;
8766 }
8767 }
8768
8769 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008770 * <p>Returns a hardware layer that can be used to draw this view again
8771 * without executing its draw method.</p>
8772 *
8773 * @return A HardwareLayer ready to render, or null if an error occurred.
8774 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008775 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008776 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8777 return null;
8778 }
8779
8780 final int width = mRight - mLeft;
8781 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008782
Romain Guy6c319ca2011-01-11 14:29:25 -08008783 if (width == 0 || height == 0) {
8784 return null;
8785 }
8786
8787 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8788 if (mHardwareLayer == null) {
8789 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8790 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008791 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008792 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8793 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008794 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008795 }
8796
Romain Guy5e7f7662011-01-24 22:35:56 -08008797 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8798 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8799 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008800 try {
8801 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008802 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008803 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008804
Chet Haase88172fe2011-03-07 17:36:33 -08008805 final int restoreCount = canvas.save();
8806
Romain Guy6c319ca2011-01-11 14:29:25 -08008807 computeScroll();
8808 canvas.translate(-mScrollX, -mScrollY);
8809
Romain Guy6c319ca2011-01-11 14:29:25 -08008810 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008811
Romain Guy6c319ca2011-01-11 14:29:25 -08008812 // Fast path for layouts with no backgrounds
8813 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8814 mPrivateFlags &= ~DIRTY_MASK;
8815 dispatchDraw(canvas);
8816 } else {
8817 draw(canvas);
8818 }
Joe Malin32736f02011-01-19 16:14:20 -08008819
Chet Haase88172fe2011-03-07 17:36:33 -08008820 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008821 } finally {
8822 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008823 mHardwareLayer.end(currentCanvas);
8824 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008825 }
8826 }
8827
8828 return mHardwareLayer;
8829 }
Romain Guy171c5922011-01-06 10:04:23 -08008830
8831 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8833 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8834 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8835 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8836 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8837 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008838 *
Romain Guy171c5922011-01-06 10:04:23 -08008839 * <p>Enabling the drawing cache is similar to
8840 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008841 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8842 * drawing cache has no effect on rendering because the system uses a different mechanism
8843 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8844 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8845 * for information on how to enable software and hardware layers.</p>
8846 *
8847 * <p>This API can be used to manually generate
8848 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8849 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 *
8851 * @param enabled true to enable the drawing cache, false otherwise
8852 *
8853 * @see #isDrawingCacheEnabled()
8854 * @see #getDrawingCache()
8855 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008856 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 */
8858 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008859 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8861 }
8862
8863 /**
8864 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8865 *
8866 * @return true if the drawing cache is enabled
8867 *
8868 * @see #setDrawingCacheEnabled(boolean)
8869 * @see #getDrawingCache()
8870 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008871 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008872 public boolean isDrawingCacheEnabled() {
8873 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8874 }
8875
8876 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008877 * Debugging utility which recursively outputs the dirty state of a view and its
8878 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008879 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008880 * @hide
8881 */
Romain Guy676b1732011-02-14 14:45:33 -08008882 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008883 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8884 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8885 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8886 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8887 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8888 if (clear) {
8889 mPrivateFlags &= clearMask;
8890 }
8891 if (this instanceof ViewGroup) {
8892 ViewGroup parent = (ViewGroup) this;
8893 final int count = parent.getChildCount();
8894 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008895 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008896 child.outputDirtyFlags(indent + " ", clear, clearMask);
8897 }
8898 }
8899 }
8900
8901 /**
8902 * This method is used by ViewGroup to cause its children to restore or recreate their
8903 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8904 * to recreate its own display list, which would happen if it went through the normal
8905 * draw/dispatchDraw mechanisms.
8906 *
8907 * @hide
8908 */
8909 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008910
8911 /**
8912 * A view that is not attached or hardware accelerated cannot create a display list.
8913 * This method checks these conditions and returns the appropriate result.
8914 *
8915 * @return true if view has the ability to create a display list, false otherwise.
8916 *
8917 * @hide
8918 */
8919 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008920 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008921 }
Joe Malin32736f02011-01-19 16:14:20 -08008922
Chet Haasedaf98e92011-01-10 14:10:36 -08008923 /**
Romain Guyb051e892010-09-28 19:09:36 -07008924 * <p>Returns a display list that can be used to draw this view again
8925 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008926 *
Romain Guyb051e892010-09-28 19:09:36 -07008927 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008928 *
8929 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008930 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008931 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008932 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008933 return null;
8934 }
8935
Chet Haasedaf98e92011-01-10 14:10:36 -08008936 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8937 mDisplayList == null || !mDisplayList.isValid() ||
8938 mRecreateDisplayList)) {
8939 // Don't need to recreate the display list, just need to tell our
8940 // children to restore/recreate theirs
8941 if (mDisplayList != null && mDisplayList.isValid() &&
8942 !mRecreateDisplayList) {
8943 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8944 mPrivateFlags &= ~DIRTY_MASK;
8945 dispatchGetDisplayList();
8946
8947 return mDisplayList;
8948 }
8949
8950 // If we got here, we're recreating it. Mark it as such to ensure that
8951 // we copy in child display lists into ours in drawChild()
8952 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008953 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008954 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8955 // If we're creating a new display list, make sure our parent gets invalidated
8956 // since they will need to recreate their display list to account for this
8957 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008958 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008959 }
Romain Guyb051e892010-09-28 19:09:36 -07008960
8961 final HardwareCanvas canvas = mDisplayList.start();
8962 try {
8963 int width = mRight - mLeft;
8964 int height = mBottom - mTop;
8965
8966 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008967 // The dirty rect should always be null for a display list
8968 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008969
Chet Haase88172fe2011-03-07 17:36:33 -08008970 final int restoreCount = canvas.save();
8971
Chet Haasedaf98e92011-01-10 14:10:36 -08008972 computeScroll();
8973 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008974 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008975
Romain Guyb051e892010-09-28 19:09:36 -07008976 // Fast path for layouts with no backgrounds
8977 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8978 mPrivateFlags &= ~DIRTY_MASK;
8979 dispatchDraw(canvas);
8980 } else {
8981 draw(canvas);
8982 }
Joe Malin32736f02011-01-19 16:14:20 -08008983
Chet Haase88172fe2011-03-07 17:36:33 -08008984 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008985 } finally {
8986 canvas.onPostDraw();
8987
8988 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008989 }
Chet Haase77785f92011-01-25 23:22:09 -08008990 } else {
8991 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8992 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008993 }
8994
8995 return mDisplayList;
8996 }
8997
8998 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008999 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009000 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009001 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009002 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009003 * @see #getDrawingCache(boolean)
9004 */
9005 public Bitmap getDrawingCache() {
9006 return getDrawingCache(false);
9007 }
9008
9009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009010 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9011 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9012 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9013 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9014 * request the drawing cache by calling this method and draw it on screen if the
9015 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009016 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009017 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9018 * this method will create a bitmap of the same size as this view. Because this bitmap
9019 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9020 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9021 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9022 * size than the view. This implies that your application must be able to handle this
9023 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009024 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009025 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9026 * the current density of the screen when the application is in compatibility
9027 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009028 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009029 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 * @see #setDrawingCacheEnabled(boolean)
9032 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009033 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 * @see #destroyDrawingCache()
9035 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009036 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009037 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9038 return null;
9039 }
9040 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009041 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009042 }
Romain Guy02890fd2010-08-06 17:58:44 -07009043 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009044 }
9045
9046 /**
9047 * <p>Frees the resources used by the drawing cache. If you call
9048 * {@link #buildDrawingCache()} manually without calling
9049 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9050 * should cleanup the cache with this method afterwards.</p>
9051 *
9052 * @see #setDrawingCacheEnabled(boolean)
9053 * @see #buildDrawingCache()
9054 * @see #getDrawingCache()
9055 */
9056 public void destroyDrawingCache() {
9057 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009058 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059 mDrawingCache = null;
9060 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009061 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009062 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009063 mUnscaledDrawingCache = null;
9064 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009065 }
9066
9067 /**
9068 * Setting a solid background color for the drawing cache's bitmaps will improve
9069 * perfromance and memory usage. Note, though that this should only be used if this
9070 * view will always be drawn on top of a solid color.
9071 *
9072 * @param color The background color to use for the drawing cache's bitmap
9073 *
9074 * @see #setDrawingCacheEnabled(boolean)
9075 * @see #buildDrawingCache()
9076 * @see #getDrawingCache()
9077 */
9078 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009079 if (color != mDrawingCacheBackgroundColor) {
9080 mDrawingCacheBackgroundColor = color;
9081 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009083 }
9084
9085 /**
9086 * @see #setDrawingCacheBackgroundColor(int)
9087 *
9088 * @return The background color to used for the drawing cache's bitmap
9089 */
9090 public int getDrawingCacheBackgroundColor() {
9091 return mDrawingCacheBackgroundColor;
9092 }
9093
9094 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009095 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009096 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009097 * @see #buildDrawingCache(boolean)
9098 */
9099 public void buildDrawingCache() {
9100 buildDrawingCache(false);
9101 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009102
Romain Guyfbd8f692009-06-26 14:51:58 -07009103 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009104 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9105 *
9106 * <p>If you call {@link #buildDrawingCache()} manually without calling
9107 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9108 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009109 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009110 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9111 * this method will create a bitmap of the same size as this view. Because this bitmap
9112 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9113 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9114 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9115 * size than the view. This implies that your application must be able to handle this
9116 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009117 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009118 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9119 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009120 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009121 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 *
9123 * @see #getDrawingCache()
9124 * @see #destroyDrawingCache()
9125 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009126 public void buildDrawingCache(boolean autoScale) {
9127 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009128 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009129 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009130
9131 if (ViewDebug.TRACE_HIERARCHY) {
9132 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009134
Romain Guy8506ab42009-06-11 17:35:47 -07009135 int width = mRight - mLeft;
9136 int height = mBottom - mTop;
9137
9138 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009139 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009140
Romain Guye1123222009-06-29 14:24:56 -07009141 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009142 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9143 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145
9146 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009147 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009148 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149
9150 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009151 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009152 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9154 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009155 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 return;
9157 }
9158
9159 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009160 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161
9162 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 Bitmap.Config quality;
9164 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009165 // Never pick ARGB_4444 because it looks awful
9166 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009167 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9168 case DRAWING_CACHE_QUALITY_AUTO:
9169 quality = Bitmap.Config.ARGB_8888;
9170 break;
9171 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009172 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 break;
9174 case DRAWING_CACHE_QUALITY_HIGH:
9175 quality = Bitmap.Config.ARGB_8888;
9176 break;
9177 default:
9178 quality = Bitmap.Config.ARGB_8888;
9179 break;
9180 }
9181 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009182 // Optimization for translucent windows
9183 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009184 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 }
9186
9187 // Try to cleanup memory
9188 if (bitmap != null) bitmap.recycle();
9189
9190 try {
9191 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009192 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009193 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009194 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009195 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009196 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009197 }
Adam Powell26153a32010-11-08 15:22:27 -08009198 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199 } catch (OutOfMemoryError e) {
9200 // If there is not enough memory to create the bitmap cache, just
9201 // ignore the issue as bitmap caches are not required to draw the
9202 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009203 if (autoScale) {
9204 mDrawingCache = null;
9205 } else {
9206 mUnscaledDrawingCache = null;
9207 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009208 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009209 return;
9210 }
9211
9212 clear = drawingCacheBackgroundColor != 0;
9213 }
9214
9215 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009216 if (attachInfo != null) {
9217 canvas = attachInfo.mCanvas;
9218 if (canvas == null) {
9219 canvas = new Canvas();
9220 }
9221 canvas.setBitmap(bitmap);
9222 // Temporarily clobber the cached Canvas in case one of our children
9223 // is also using a drawing cache. Without this, the children would
9224 // steal the canvas by attaching their own bitmap to it and bad, bad
9225 // thing would happen (invisible views, corrupted drawings, etc.)
9226 attachInfo.mCanvas = null;
9227 } else {
9228 // This case should hopefully never or seldom happen
9229 canvas = new Canvas(bitmap);
9230 }
9231
9232 if (clear) {
9233 bitmap.eraseColor(drawingCacheBackgroundColor);
9234 }
9235
9236 computeScroll();
9237 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009238
Romain Guye1123222009-06-29 14:24:56 -07009239 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009240 final float scale = attachInfo.mApplicationScale;
9241 canvas.scale(scale, scale);
9242 }
Joe Malin32736f02011-01-19 16:14:20 -08009243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009244 canvas.translate(-mScrollX, -mScrollY);
9245
Romain Guy5bcdff42009-05-14 21:27:18 -07009246 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009247 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9248 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009249 mPrivateFlags |= DRAWING_CACHE_VALID;
9250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251
9252 // Fast path for layouts with no backgrounds
9253 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9254 if (ViewDebug.TRACE_HIERARCHY) {
9255 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9256 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009257 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 dispatchDraw(canvas);
9259 } else {
9260 draw(canvas);
9261 }
9262
9263 canvas.restoreToCount(restoreCount);
9264
9265 if (attachInfo != null) {
9266 // Restore the cached Canvas for our siblings
9267 attachInfo.mCanvas = canvas;
9268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 }
9270 }
9271
9272 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009273 * Create a snapshot of the view into a bitmap. We should probably make
9274 * some form of this public, but should think about the API.
9275 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009276 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009277 int width = mRight - mLeft;
9278 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009279
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009280 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009281 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009282 width = (int) ((width * scale) + 0.5f);
9283 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009284
Romain Guy8c11e312009-09-14 15:15:30 -07009285 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009286 if (bitmap == null) {
9287 throw new OutOfMemoryError();
9288 }
9289
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009290 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009291
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009292 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009293 if (attachInfo != null) {
9294 canvas = attachInfo.mCanvas;
9295 if (canvas == null) {
9296 canvas = new Canvas();
9297 }
9298 canvas.setBitmap(bitmap);
9299 // Temporarily clobber the cached Canvas in case one of our children
9300 // is also using a drawing cache. Without this, the children would
9301 // steal the canvas by attaching their own bitmap to it and bad, bad
9302 // things would happen (invisible views, corrupted drawings, etc.)
9303 attachInfo.mCanvas = null;
9304 } else {
9305 // This case should hopefully never or seldom happen
9306 canvas = new Canvas(bitmap);
9307 }
9308
Romain Guy5bcdff42009-05-14 21:27:18 -07009309 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009310 bitmap.eraseColor(backgroundColor);
9311 }
9312
9313 computeScroll();
9314 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009315 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009316 canvas.translate(-mScrollX, -mScrollY);
9317
Romain Guy5bcdff42009-05-14 21:27:18 -07009318 // Temporarily remove the dirty mask
9319 int flags = mPrivateFlags;
9320 mPrivateFlags &= ~DIRTY_MASK;
9321
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009322 // Fast path for layouts with no backgrounds
9323 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9324 dispatchDraw(canvas);
9325 } else {
9326 draw(canvas);
9327 }
9328
Romain Guy5bcdff42009-05-14 21:27:18 -07009329 mPrivateFlags = flags;
9330
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009331 canvas.restoreToCount(restoreCount);
9332
9333 if (attachInfo != null) {
9334 // Restore the cached Canvas for our siblings
9335 attachInfo.mCanvas = canvas;
9336 }
Romain Guy8506ab42009-06-11 17:35:47 -07009337
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009338 return bitmap;
9339 }
9340
9341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009342 * Indicates whether this View is currently in edit mode. A View is usually
9343 * in edit mode when displayed within a developer tool. For instance, if
9344 * this View is being drawn by a visual user interface builder, this method
9345 * should return true.
9346 *
9347 * Subclasses should check the return value of this method to provide
9348 * different behaviors if their normal behavior might interfere with the
9349 * host environment. For instance: the class spawns a thread in its
9350 * constructor, the drawing code relies on device-specific features, etc.
9351 *
9352 * This method is usually checked in the drawing code of custom widgets.
9353 *
9354 * @return True if this View is in edit mode, false otherwise.
9355 */
9356 public boolean isInEditMode() {
9357 return false;
9358 }
9359
9360 /**
9361 * If the View draws content inside its padding and enables fading edges,
9362 * it needs to support padding offsets. Padding offsets are added to the
9363 * fading edges to extend the length of the fade so that it covers pixels
9364 * drawn inside the padding.
9365 *
9366 * Subclasses of this class should override this method if they need
9367 * to draw content inside the padding.
9368 *
9369 * @return True if padding offset must be applied, false otherwise.
9370 *
9371 * @see #getLeftPaddingOffset()
9372 * @see #getRightPaddingOffset()
9373 * @see #getTopPaddingOffset()
9374 * @see #getBottomPaddingOffset()
9375 *
9376 * @since CURRENT
9377 */
9378 protected boolean isPaddingOffsetRequired() {
9379 return false;
9380 }
9381
9382 /**
9383 * Amount by which to extend the left fading region. Called only when
9384 * {@link #isPaddingOffsetRequired()} returns true.
9385 *
9386 * @return The left padding offset in pixels.
9387 *
9388 * @see #isPaddingOffsetRequired()
9389 *
9390 * @since CURRENT
9391 */
9392 protected int getLeftPaddingOffset() {
9393 return 0;
9394 }
9395
9396 /**
9397 * Amount by which to extend the right fading region. Called only when
9398 * {@link #isPaddingOffsetRequired()} returns true.
9399 *
9400 * @return The right padding offset in pixels.
9401 *
9402 * @see #isPaddingOffsetRequired()
9403 *
9404 * @since CURRENT
9405 */
9406 protected int getRightPaddingOffset() {
9407 return 0;
9408 }
9409
9410 /**
9411 * Amount by which to extend the top fading region. Called only when
9412 * {@link #isPaddingOffsetRequired()} returns true.
9413 *
9414 * @return The top padding offset in pixels.
9415 *
9416 * @see #isPaddingOffsetRequired()
9417 *
9418 * @since CURRENT
9419 */
9420 protected int getTopPaddingOffset() {
9421 return 0;
9422 }
9423
9424 /**
9425 * Amount by which to extend the bottom fading region. Called only when
9426 * {@link #isPaddingOffsetRequired()} returns true.
9427 *
9428 * @return The bottom padding offset in pixels.
9429 *
9430 * @see #isPaddingOffsetRequired()
9431 *
9432 * @since CURRENT
9433 */
9434 protected int getBottomPaddingOffset() {
9435 return 0;
9436 }
9437
9438 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009439 * <p>Indicates whether this view is attached to an hardware accelerated
9440 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009441 *
Romain Guy2bffd262010-09-12 17:40:02 -07009442 * <p>Even if this method returns true, it does not mean that every call
9443 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9444 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9445 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9446 * window is hardware accelerated,
9447 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9448 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009449 *
Romain Guy2bffd262010-09-12 17:40:02 -07009450 * @return True if the view is attached to a window and the window is
9451 * hardware accelerated; false in any other case.
9452 */
9453 public boolean isHardwareAccelerated() {
9454 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9455 }
Joe Malin32736f02011-01-19 16:14:20 -08009456
Romain Guy2bffd262010-09-12 17:40:02 -07009457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009458 * Manually render this view (and all of its children) to the given Canvas.
9459 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009460 * called. When implementing a view, implement {@link #onDraw} instead of
9461 * overriding this method. If you do need to override this method, call
9462 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009463 *
9464 * @param canvas The Canvas to which the View is rendered.
9465 */
9466 public void draw(Canvas canvas) {
9467 if (ViewDebug.TRACE_HIERARCHY) {
9468 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9469 }
9470
Romain Guy5bcdff42009-05-14 21:27:18 -07009471 final int privateFlags = mPrivateFlags;
9472 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9473 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9474 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 /*
9477 * Draw traversal performs several drawing steps which must be executed
9478 * in the appropriate order:
9479 *
9480 * 1. Draw the background
9481 * 2. If necessary, save the canvas' layers to prepare for fading
9482 * 3. Draw view's content
9483 * 4. Draw children
9484 * 5. If necessary, draw the fading edges and restore layers
9485 * 6. Draw decorations (scrollbars for instance)
9486 */
9487
9488 // Step 1, draw the background, if needed
9489 int saveCount;
9490
Romain Guy24443ea2009-05-11 11:56:30 -07009491 if (!dirtyOpaque) {
9492 final Drawable background = mBGDrawable;
9493 if (background != null) {
9494 final int scrollX = mScrollX;
9495 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009496
Romain Guy24443ea2009-05-11 11:56:30 -07009497 if (mBackgroundSizeChanged) {
9498 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9499 mBackgroundSizeChanged = false;
9500 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009501
Romain Guy24443ea2009-05-11 11:56:30 -07009502 if ((scrollX | scrollY) == 0) {
9503 background.draw(canvas);
9504 } else {
9505 canvas.translate(scrollX, scrollY);
9506 background.draw(canvas);
9507 canvas.translate(-scrollX, -scrollY);
9508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009509 }
9510 }
9511
9512 // skip step 2 & 5 if possible (common case)
9513 final int viewFlags = mViewFlags;
9514 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9515 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9516 if (!verticalEdges && !horizontalEdges) {
9517 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009518 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009519
9520 // Step 4, draw the children
9521 dispatchDraw(canvas);
9522
9523 // Step 6, draw decorations (scrollbars)
9524 onDrawScrollBars(canvas);
9525
9526 // we're done...
9527 return;
9528 }
9529
9530 /*
9531 * Here we do the full fledged routine...
9532 * (this is an uncommon case where speed matters less,
9533 * this is why we repeat some of the tests that have been
9534 * done above)
9535 */
9536
9537 boolean drawTop = false;
9538 boolean drawBottom = false;
9539 boolean drawLeft = false;
9540 boolean drawRight = false;
9541
9542 float topFadeStrength = 0.0f;
9543 float bottomFadeStrength = 0.0f;
9544 float leftFadeStrength = 0.0f;
9545 float rightFadeStrength = 0.0f;
9546
9547 // Step 2, save the canvas' layers
9548 int paddingLeft = mPaddingLeft;
9549 int paddingTop = mPaddingTop;
9550
9551 final boolean offsetRequired = isPaddingOffsetRequired();
9552 if (offsetRequired) {
9553 paddingLeft += getLeftPaddingOffset();
9554 paddingTop += getTopPaddingOffset();
9555 }
9556
9557 int left = mScrollX + paddingLeft;
9558 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9559 int top = mScrollY + paddingTop;
9560 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9561
9562 if (offsetRequired) {
9563 right += getRightPaddingOffset();
9564 bottom += getBottomPaddingOffset();
9565 }
9566
9567 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009568 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9569 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009570
9571 // clip the fade length if top and bottom fades overlap
9572 // overlapping fades produce odd-looking artifacts
9573 if (verticalEdges && (top + length > bottom - length)) {
9574 length = (bottom - top) / 2;
9575 }
9576
9577 // also clip horizontal fades if necessary
9578 if (horizontalEdges && (left + length > right - length)) {
9579 length = (right - left) / 2;
9580 }
9581
9582 if (verticalEdges) {
9583 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009584 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009585 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009586 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587 }
9588
9589 if (horizontalEdges) {
9590 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009591 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009592 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009593 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009594 }
9595
9596 saveCount = canvas.getSaveCount();
9597
9598 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009599 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009600 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9601
9602 if (drawTop) {
9603 canvas.saveLayer(left, top, right, top + length, null, flags);
9604 }
9605
9606 if (drawBottom) {
9607 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9608 }
9609
9610 if (drawLeft) {
9611 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9612 }
9613
9614 if (drawRight) {
9615 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9616 }
9617 } else {
9618 scrollabilityCache.setFadeColor(solidColor);
9619 }
9620
9621 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009622 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009623
9624 // Step 4, draw the children
9625 dispatchDraw(canvas);
9626
9627 // Step 5, draw the fade effect and restore layers
9628 final Paint p = scrollabilityCache.paint;
9629 final Matrix matrix = scrollabilityCache.matrix;
9630 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009631
9632 if (drawTop) {
9633 matrix.setScale(1, fadeHeight * topFadeStrength);
9634 matrix.postTranslate(left, top);
9635 fade.setLocalMatrix(matrix);
9636 canvas.drawRect(left, top, right, top + length, p);
9637 }
9638
9639 if (drawBottom) {
9640 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9641 matrix.postRotate(180);
9642 matrix.postTranslate(left, bottom);
9643 fade.setLocalMatrix(matrix);
9644 canvas.drawRect(left, bottom - length, right, bottom, p);
9645 }
9646
9647 if (drawLeft) {
9648 matrix.setScale(1, fadeHeight * leftFadeStrength);
9649 matrix.postRotate(-90);
9650 matrix.postTranslate(left, top);
9651 fade.setLocalMatrix(matrix);
9652 canvas.drawRect(left, top, left + length, bottom, p);
9653 }
9654
9655 if (drawRight) {
9656 matrix.setScale(1, fadeHeight * rightFadeStrength);
9657 matrix.postRotate(90);
9658 matrix.postTranslate(right, top);
9659 fade.setLocalMatrix(matrix);
9660 canvas.drawRect(right - length, top, right, bottom, p);
9661 }
9662
9663 canvas.restoreToCount(saveCount);
9664
9665 // Step 6, draw decorations (scrollbars)
9666 onDrawScrollBars(canvas);
9667 }
9668
9669 /**
9670 * Override this if your view is known to always be drawn on top of a solid color background,
9671 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9672 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9673 * should be set to 0xFF.
9674 *
9675 * @see #setVerticalFadingEdgeEnabled
9676 * @see #setHorizontalFadingEdgeEnabled
9677 *
9678 * @return The known solid color background for this view, or 0 if the color may vary
9679 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009680 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009681 public int getSolidColor() {
9682 return 0;
9683 }
9684
9685 /**
9686 * Build a human readable string representation of the specified view flags.
9687 *
9688 * @param flags the view flags to convert to a string
9689 * @return a String representing the supplied flags
9690 */
9691 private static String printFlags(int flags) {
9692 String output = "";
9693 int numFlags = 0;
9694 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9695 output += "TAKES_FOCUS";
9696 numFlags++;
9697 }
9698
9699 switch (flags & VISIBILITY_MASK) {
9700 case INVISIBLE:
9701 if (numFlags > 0) {
9702 output += " ";
9703 }
9704 output += "INVISIBLE";
9705 // USELESS HERE numFlags++;
9706 break;
9707 case GONE:
9708 if (numFlags > 0) {
9709 output += " ";
9710 }
9711 output += "GONE";
9712 // USELESS HERE numFlags++;
9713 break;
9714 default:
9715 break;
9716 }
9717 return output;
9718 }
9719
9720 /**
9721 * Build a human readable string representation of the specified private
9722 * view flags.
9723 *
9724 * @param privateFlags the private view flags to convert to a string
9725 * @return a String representing the supplied flags
9726 */
9727 private static String printPrivateFlags(int privateFlags) {
9728 String output = "";
9729 int numFlags = 0;
9730
9731 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9732 output += "WANTS_FOCUS";
9733 numFlags++;
9734 }
9735
9736 if ((privateFlags & FOCUSED) == FOCUSED) {
9737 if (numFlags > 0) {
9738 output += " ";
9739 }
9740 output += "FOCUSED";
9741 numFlags++;
9742 }
9743
9744 if ((privateFlags & SELECTED) == SELECTED) {
9745 if (numFlags > 0) {
9746 output += " ";
9747 }
9748 output += "SELECTED";
9749 numFlags++;
9750 }
9751
9752 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9753 if (numFlags > 0) {
9754 output += " ";
9755 }
9756 output += "IS_ROOT_NAMESPACE";
9757 numFlags++;
9758 }
9759
9760 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9761 if (numFlags > 0) {
9762 output += " ";
9763 }
9764 output += "HAS_BOUNDS";
9765 numFlags++;
9766 }
9767
9768 if ((privateFlags & DRAWN) == DRAWN) {
9769 if (numFlags > 0) {
9770 output += " ";
9771 }
9772 output += "DRAWN";
9773 // USELESS HERE numFlags++;
9774 }
9775 return output;
9776 }
9777
9778 /**
9779 * <p>Indicates whether or not this view's layout will be requested during
9780 * the next hierarchy layout pass.</p>
9781 *
9782 * @return true if the layout will be forced during next layout pass
9783 */
9784 public boolean isLayoutRequested() {
9785 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9786 }
9787
9788 /**
9789 * Assign a size and position to a view and all of its
9790 * descendants
9791 *
9792 * <p>This is the second phase of the layout mechanism.
9793 * (The first is measuring). In this phase, each parent calls
9794 * layout on all of its children to position them.
9795 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009796 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009797 *
Chet Haase9c087442011-01-12 16:20:16 -08009798 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009799 * Derived classes with children should override
9800 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009801 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 *
9803 * @param l Left position, relative to parent
9804 * @param t Top position, relative to parent
9805 * @param r Right position, relative to parent
9806 * @param b Bottom position, relative to parent
9807 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009808 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009809 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009810 int oldL = mLeft;
9811 int oldT = mTop;
9812 int oldB = mBottom;
9813 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009814 boolean changed = setFrame(l, t, r, b);
9815 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9816 if (ViewDebug.TRACE_HIERARCHY) {
9817 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9818 }
9819
9820 onLayout(changed, l, t, r, b);
9821 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009822
9823 if (mOnLayoutChangeListeners != null) {
9824 ArrayList<OnLayoutChangeListener> listenersCopy =
9825 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9826 int numListeners = listenersCopy.size();
9827 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009828 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009829 }
9830 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009831 }
9832 mPrivateFlags &= ~FORCE_LAYOUT;
9833 }
9834
9835 /**
9836 * Called from layout when this view should
9837 * assign a size and position to each of its children.
9838 *
9839 * Derived classes with children should override
9840 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009841 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009842 * @param changed This is a new size or position for this view
9843 * @param left Left position, relative to parent
9844 * @param top Top position, relative to parent
9845 * @param right Right position, relative to parent
9846 * @param bottom Bottom position, relative to parent
9847 */
9848 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9849 }
9850
9851 /**
9852 * Assign a size and position to this view.
9853 *
9854 * This is called from layout.
9855 *
9856 * @param left Left position, relative to parent
9857 * @param top Top position, relative to parent
9858 * @param right Right position, relative to parent
9859 * @param bottom Bottom position, relative to parent
9860 * @return true if the new size and position are different than the
9861 * previous ones
9862 * {@hide}
9863 */
9864 protected boolean setFrame(int left, int top, int right, int bottom) {
9865 boolean changed = false;
9866
9867 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009868 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009869 + right + "," + bottom + ")");
9870 }
9871
9872 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9873 changed = true;
9874
9875 // Remember our drawn bit
9876 int drawn = mPrivateFlags & DRAWN;
9877
9878 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009879 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009880
9881
9882 int oldWidth = mRight - mLeft;
9883 int oldHeight = mBottom - mTop;
9884
9885 mLeft = left;
9886 mTop = top;
9887 mRight = right;
9888 mBottom = bottom;
9889
9890 mPrivateFlags |= HAS_BOUNDS;
9891
9892 int newWidth = right - left;
9893 int newHeight = bottom - top;
9894
9895 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009896 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9897 // A change in dimension means an auto-centered pivot point changes, too
9898 mMatrixDirty = true;
9899 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9901 }
9902
9903 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9904 // If we are visible, force the DRAWN bit to on so that
9905 // this invalidate will go through (at least to our parent).
9906 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009907 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 // the DRAWN bit.
9909 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009910 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009911 // parent display list may need to be recreated based on a change in the bounds
9912 // of any child
9913 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009914 }
9915
9916 // Reset drawn bit to original value (invalidate turns it off)
9917 mPrivateFlags |= drawn;
9918
9919 mBackgroundSizeChanged = true;
9920 }
9921 return changed;
9922 }
9923
9924 /**
9925 * Finalize inflating a view from XML. This is called as the last phase
9926 * of inflation, after all child views have been added.
9927 *
9928 * <p>Even if the subclass overrides onFinishInflate, they should always be
9929 * sure to call the super method, so that we get called.
9930 */
9931 protected void onFinishInflate() {
9932 }
9933
9934 /**
9935 * Returns the resources associated with this view.
9936 *
9937 * @return Resources object.
9938 */
9939 public Resources getResources() {
9940 return mResources;
9941 }
9942
9943 /**
9944 * Invalidates the specified Drawable.
9945 *
9946 * @param drawable the drawable to invalidate
9947 */
9948 public void invalidateDrawable(Drawable drawable) {
9949 if (verifyDrawable(drawable)) {
9950 final Rect dirty = drawable.getBounds();
9951 final int scrollX = mScrollX;
9952 final int scrollY = mScrollY;
9953
9954 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9955 dirty.right + scrollX, dirty.bottom + scrollY);
9956 }
9957 }
9958
9959 /**
9960 * Schedules an action on a drawable to occur at a specified time.
9961 *
9962 * @param who the recipient of the action
9963 * @param what the action to run on the drawable
9964 * @param when the time at which the action must occur. Uses the
9965 * {@link SystemClock#uptimeMillis} timebase.
9966 */
9967 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9968 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9969 mAttachInfo.mHandler.postAtTime(what, who, when);
9970 }
9971 }
9972
9973 /**
9974 * Cancels a scheduled action on a drawable.
9975 *
9976 * @param who the recipient of the action
9977 * @param what the action to cancel
9978 */
9979 public void unscheduleDrawable(Drawable who, Runnable what) {
9980 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9981 mAttachInfo.mHandler.removeCallbacks(what, who);
9982 }
9983 }
9984
9985 /**
9986 * Unschedule any events associated with the given Drawable. This can be
9987 * used when selecting a new Drawable into a view, so that the previous
9988 * one is completely unscheduled.
9989 *
9990 * @param who The Drawable to unschedule.
9991 *
9992 * @see #drawableStateChanged
9993 */
9994 public void unscheduleDrawable(Drawable who) {
9995 if (mAttachInfo != null) {
9996 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9997 }
9998 }
9999
10000 /**
10001 * If your view subclass is displaying its own Drawable objects, it should
10002 * override this function and return true for any Drawable it is
10003 * displaying. This allows animations for those drawables to be
10004 * scheduled.
10005 *
10006 * <p>Be sure to call through to the super class when overriding this
10007 * function.
10008 *
10009 * @param who The Drawable to verify. Return true if it is one you are
10010 * displaying, else return the result of calling through to the
10011 * super class.
10012 *
10013 * @return boolean If true than the Drawable is being displayed in the
10014 * view; else false and it is not allowed to animate.
10015 *
10016 * @see #unscheduleDrawable
10017 * @see #drawableStateChanged
10018 */
10019 protected boolean verifyDrawable(Drawable who) {
10020 return who == mBGDrawable;
10021 }
10022
10023 /**
10024 * This function is called whenever the state of the view changes in such
10025 * a way that it impacts the state of drawables being shown.
10026 *
10027 * <p>Be sure to call through to the superclass when overriding this
10028 * function.
10029 *
10030 * @see Drawable#setState
10031 */
10032 protected void drawableStateChanged() {
10033 Drawable d = mBGDrawable;
10034 if (d != null && d.isStateful()) {
10035 d.setState(getDrawableState());
10036 }
10037 }
10038
10039 /**
10040 * Call this to force a view to update its drawable state. This will cause
10041 * drawableStateChanged to be called on this view. Views that are interested
10042 * in the new state should call getDrawableState.
10043 *
10044 * @see #drawableStateChanged
10045 * @see #getDrawableState
10046 */
10047 public void refreshDrawableState() {
10048 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10049 drawableStateChanged();
10050
10051 ViewParent parent = mParent;
10052 if (parent != null) {
10053 parent.childDrawableStateChanged(this);
10054 }
10055 }
10056
10057 /**
10058 * Return an array of resource IDs of the drawable states representing the
10059 * current state of the view.
10060 *
10061 * @return The current drawable state
10062 *
10063 * @see Drawable#setState
10064 * @see #drawableStateChanged
10065 * @see #onCreateDrawableState
10066 */
10067 public final int[] getDrawableState() {
10068 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10069 return mDrawableState;
10070 } else {
10071 mDrawableState = onCreateDrawableState(0);
10072 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10073 return mDrawableState;
10074 }
10075 }
10076
10077 /**
10078 * Generate the new {@link android.graphics.drawable.Drawable} state for
10079 * this view. This is called by the view
10080 * system when the cached Drawable state is determined to be invalid. To
10081 * retrieve the current state, you should use {@link #getDrawableState}.
10082 *
10083 * @param extraSpace if non-zero, this is the number of extra entries you
10084 * would like in the returned array in which you can place your own
10085 * states.
10086 *
10087 * @return Returns an array holding the current {@link Drawable} state of
10088 * the view.
10089 *
10090 * @see #mergeDrawableStates
10091 */
10092 protected int[] onCreateDrawableState(int extraSpace) {
10093 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10094 mParent instanceof View) {
10095 return ((View) mParent).onCreateDrawableState(extraSpace);
10096 }
10097
10098 int[] drawableState;
10099
10100 int privateFlags = mPrivateFlags;
10101
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010102 int viewStateIndex = 0;
10103 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10104 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10105 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010106 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010107 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10108 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010109 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
10110 // This is set if HW acceleration is requested, even if the current
10111 // process doesn't allow it. This is just to allow app preview
10112 // windows to better match their app.
10113 viewStateIndex |= VIEW_STATE_ACCELERATED;
10114 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010115 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010116
Christopher Tate3d4bf172011-03-28 16:16:46 -070010117 final int privateFlags2 = mPrivateFlags2;
10118 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10119 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010121 drawableState = VIEW_STATE_SETS[viewStateIndex];
10122
10123 //noinspection ConstantIfStatement
10124 if (false) {
10125 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10126 Log.i("View", toString()
10127 + " pressed=" + ((privateFlags & PRESSED) != 0)
10128 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10129 + " fo=" + hasFocus()
10130 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010131 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010132 + ": " + Arrays.toString(drawableState));
10133 }
10134
10135 if (extraSpace == 0) {
10136 return drawableState;
10137 }
10138
10139 final int[] fullState;
10140 if (drawableState != null) {
10141 fullState = new int[drawableState.length + extraSpace];
10142 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10143 } else {
10144 fullState = new int[extraSpace];
10145 }
10146
10147 return fullState;
10148 }
10149
10150 /**
10151 * Merge your own state values in <var>additionalState</var> into the base
10152 * state values <var>baseState</var> that were returned by
10153 * {@link #onCreateDrawableState}.
10154 *
10155 * @param baseState The base state values returned by
10156 * {@link #onCreateDrawableState}, which will be modified to also hold your
10157 * own additional state values.
10158 *
10159 * @param additionalState The additional state values you would like
10160 * added to <var>baseState</var>; this array is not modified.
10161 *
10162 * @return As a convenience, the <var>baseState</var> array you originally
10163 * passed into the function is returned.
10164 *
10165 * @see #onCreateDrawableState
10166 */
10167 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10168 final int N = baseState.length;
10169 int i = N - 1;
10170 while (i >= 0 && baseState[i] == 0) {
10171 i--;
10172 }
10173 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10174 return baseState;
10175 }
10176
10177 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010178 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10179 * on all Drawable objects associated with this view.
10180 */
10181 public void jumpDrawablesToCurrentState() {
10182 if (mBGDrawable != null) {
10183 mBGDrawable.jumpToCurrentState();
10184 }
10185 }
10186
10187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 * Sets the background color for this view.
10189 * @param color the color of the background
10190 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010191 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010192 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010193 if (mBGDrawable instanceof ColorDrawable) {
10194 ((ColorDrawable) mBGDrawable).setColor(color);
10195 } else {
10196 setBackgroundDrawable(new ColorDrawable(color));
10197 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010198 }
10199
10200 /**
10201 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010202 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010203 * @param resid The identifier of the resource.
10204 * @attr ref android.R.styleable#View_background
10205 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010206 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 public void setBackgroundResource(int resid) {
10208 if (resid != 0 && resid == mBackgroundResource) {
10209 return;
10210 }
10211
10212 Drawable d= null;
10213 if (resid != 0) {
10214 d = mResources.getDrawable(resid);
10215 }
10216 setBackgroundDrawable(d);
10217
10218 mBackgroundResource = resid;
10219 }
10220
10221 /**
10222 * Set the background to a given Drawable, or remove the background. If the
10223 * background has padding, this View's padding is set to the background's
10224 * padding. However, when a background is removed, this View's padding isn't
10225 * touched. If setting the padding is desired, please use
10226 * {@link #setPadding(int, int, int, int)}.
10227 *
10228 * @param d The Drawable to use as the background, or null to remove the
10229 * background
10230 */
10231 public void setBackgroundDrawable(Drawable d) {
10232 boolean requestLayout = false;
10233
10234 mBackgroundResource = 0;
10235
10236 /*
10237 * Regardless of whether we're setting a new background or not, we want
10238 * to clear the previous drawable.
10239 */
10240 if (mBGDrawable != null) {
10241 mBGDrawable.setCallback(null);
10242 unscheduleDrawable(mBGDrawable);
10243 }
10244
10245 if (d != null) {
10246 Rect padding = sThreadLocal.get();
10247 if (padding == null) {
10248 padding = new Rect();
10249 sThreadLocal.set(padding);
10250 }
10251 if (d.getPadding(padding)) {
10252 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10253 }
10254
10255 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10256 // if it has a different minimum size, we should layout again
10257 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10258 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10259 requestLayout = true;
10260 }
10261
10262 d.setCallback(this);
10263 if (d.isStateful()) {
10264 d.setState(getDrawableState());
10265 }
10266 d.setVisible(getVisibility() == VISIBLE, false);
10267 mBGDrawable = d;
10268
10269 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10270 mPrivateFlags &= ~SKIP_DRAW;
10271 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10272 requestLayout = true;
10273 }
10274 } else {
10275 /* Remove the background */
10276 mBGDrawable = null;
10277
10278 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10279 /*
10280 * This view ONLY drew the background before and we're removing
10281 * the background, so now it won't draw anything
10282 * (hence we SKIP_DRAW)
10283 */
10284 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10285 mPrivateFlags |= SKIP_DRAW;
10286 }
10287
10288 /*
10289 * When the background is set, we try to apply its padding to this
10290 * View. When the background is removed, we don't touch this View's
10291 * padding. This is noted in the Javadocs. Hence, we don't need to
10292 * requestLayout(), the invalidate() below is sufficient.
10293 */
10294
10295 // The old background's minimum size could have affected this
10296 // View's layout, so let's requestLayout
10297 requestLayout = true;
10298 }
10299
Romain Guy8f1344f52009-05-15 16:03:59 -070010300 computeOpaqueFlags();
10301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 if (requestLayout) {
10303 requestLayout();
10304 }
10305
10306 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010307 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010308 }
10309
10310 /**
10311 * Gets the background drawable
10312 * @return The drawable used as the background for this view, if any.
10313 */
10314 public Drawable getBackground() {
10315 return mBGDrawable;
10316 }
10317
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318 /**
10319 * Sets the padding. The view may add on the space required to display
10320 * the scrollbars, depending on the style and visibility of the scrollbars.
10321 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10322 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10323 * from the values set in this call.
10324 *
10325 * @attr ref android.R.styleable#View_padding
10326 * @attr ref android.R.styleable#View_paddingBottom
10327 * @attr ref android.R.styleable#View_paddingLeft
10328 * @attr ref android.R.styleable#View_paddingRight
10329 * @attr ref android.R.styleable#View_paddingTop
10330 * @param left the left padding in pixels
10331 * @param top the top padding in pixels
10332 * @param right the right padding in pixels
10333 * @param bottom the bottom padding in pixels
10334 */
10335 public void setPadding(int left, int top, int right, int bottom) {
10336 boolean changed = false;
10337
Adam Powell20232d02010-12-08 21:08:53 -080010338 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 mUserPaddingRight = right;
10340 mUserPaddingBottom = bottom;
10341
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010342 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010343
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010344 // Common case is there are no scroll bars.
10345 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010346 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010347 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10348 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010349 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010350 switch (mVerticalScrollbarPosition) {
10351 case SCROLLBAR_POSITION_DEFAULT:
10352 case SCROLLBAR_POSITION_RIGHT:
10353 right += offset;
10354 break;
10355 case SCROLLBAR_POSITION_LEFT:
10356 left += offset;
10357 break;
10358 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010359 }
Adam Powell20232d02010-12-08 21:08:53 -080010360 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010361 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10362 ? 0 : getHorizontalScrollbarHeight();
10363 }
10364 }
Romain Guy8506ab42009-06-11 17:35:47 -070010365
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010366 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367 changed = true;
10368 mPaddingLeft = left;
10369 }
10370 if (mPaddingTop != top) {
10371 changed = true;
10372 mPaddingTop = top;
10373 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010374 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010376 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010377 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010378 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010380 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010381 }
10382
10383 if (changed) {
10384 requestLayout();
10385 }
10386 }
10387
10388 /**
10389 * Returns the top padding of this view.
10390 *
10391 * @return the top padding in pixels
10392 */
10393 public int getPaddingTop() {
10394 return mPaddingTop;
10395 }
10396
10397 /**
10398 * Returns the bottom padding of this view. If there are inset and enabled
10399 * scrollbars, this value may include the space required to display the
10400 * scrollbars as well.
10401 *
10402 * @return the bottom padding in pixels
10403 */
10404 public int getPaddingBottom() {
10405 return mPaddingBottom;
10406 }
10407
10408 /**
10409 * Returns the left padding of this view. If there are inset and enabled
10410 * scrollbars, this value may include the space required to display the
10411 * scrollbars as well.
10412 *
10413 * @return the left padding in pixels
10414 */
10415 public int getPaddingLeft() {
10416 return mPaddingLeft;
10417 }
10418
10419 /**
10420 * Returns the right padding of this view. If there are inset and enabled
10421 * scrollbars, this value may include the space required to display the
10422 * scrollbars as well.
10423 *
10424 * @return the right padding in pixels
10425 */
10426 public int getPaddingRight() {
10427 return mPaddingRight;
10428 }
10429
10430 /**
10431 * Changes the selection state of this view. A view can be selected or not.
10432 * Note that selection is not the same as focus. Views are typically
10433 * selected in the context of an AdapterView like ListView or GridView;
10434 * the selected view is the view that is highlighted.
10435 *
10436 * @param selected true if the view must be selected, false otherwise
10437 */
10438 public void setSelected(boolean selected) {
10439 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10440 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010441 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010442 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010443 refreshDrawableState();
10444 dispatchSetSelected(selected);
10445 }
10446 }
10447
10448 /**
10449 * Dispatch setSelected to all of this View's children.
10450 *
10451 * @see #setSelected(boolean)
10452 *
10453 * @param selected The new selected state
10454 */
10455 protected void dispatchSetSelected(boolean selected) {
10456 }
10457
10458 /**
10459 * Indicates the selection state of this view.
10460 *
10461 * @return true if the view is selected, false otherwise
10462 */
10463 @ViewDebug.ExportedProperty
10464 public boolean isSelected() {
10465 return (mPrivateFlags & SELECTED) != 0;
10466 }
10467
10468 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010469 * Changes the activated state of this view. A view can be activated or not.
10470 * Note that activation is not the same as selection. Selection is
10471 * a transient property, representing the view (hierarchy) the user is
10472 * currently interacting with. Activation is a longer-term state that the
10473 * user can move views in and out of. For example, in a list view with
10474 * single or multiple selection enabled, the views in the current selection
10475 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10476 * here.) The activated state is propagated down to children of the view it
10477 * is set on.
10478 *
10479 * @param activated true if the view must be activated, false otherwise
10480 */
10481 public void setActivated(boolean activated) {
10482 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10483 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010484 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010485 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010486 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010487 }
10488 }
10489
10490 /**
10491 * Dispatch setActivated to all of this View's children.
10492 *
10493 * @see #setActivated(boolean)
10494 *
10495 * @param activated The new activated state
10496 */
10497 protected void dispatchSetActivated(boolean activated) {
10498 }
10499
10500 /**
10501 * Indicates the activation state of this view.
10502 *
10503 * @return true if the view is activated, false otherwise
10504 */
10505 @ViewDebug.ExportedProperty
10506 public boolean isActivated() {
10507 return (mPrivateFlags & ACTIVATED) != 0;
10508 }
10509
10510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010511 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10512 * observer can be used to get notifications when global events, like
10513 * layout, happen.
10514 *
10515 * The returned ViewTreeObserver observer is not guaranteed to remain
10516 * valid for the lifetime of this View. If the caller of this method keeps
10517 * a long-lived reference to ViewTreeObserver, it should always check for
10518 * the return value of {@link ViewTreeObserver#isAlive()}.
10519 *
10520 * @return The ViewTreeObserver for this view's hierarchy.
10521 */
10522 public ViewTreeObserver getViewTreeObserver() {
10523 if (mAttachInfo != null) {
10524 return mAttachInfo.mTreeObserver;
10525 }
10526 if (mFloatingTreeObserver == null) {
10527 mFloatingTreeObserver = new ViewTreeObserver();
10528 }
10529 return mFloatingTreeObserver;
10530 }
10531
10532 /**
10533 * <p>Finds the topmost view in the current view hierarchy.</p>
10534 *
10535 * @return the topmost view containing this view
10536 */
10537 public View getRootView() {
10538 if (mAttachInfo != null) {
10539 final View v = mAttachInfo.mRootView;
10540 if (v != null) {
10541 return v;
10542 }
10543 }
Romain Guy8506ab42009-06-11 17:35:47 -070010544
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010545 View parent = this;
10546
10547 while (parent.mParent != null && parent.mParent instanceof View) {
10548 parent = (View) parent.mParent;
10549 }
10550
10551 return parent;
10552 }
10553
10554 /**
10555 * <p>Computes the coordinates of this view on the screen. The argument
10556 * must be an array of two integers. After the method returns, the array
10557 * contains the x and y location in that order.</p>
10558 *
10559 * @param location an array of two integers in which to hold the coordinates
10560 */
10561 public void getLocationOnScreen(int[] location) {
10562 getLocationInWindow(location);
10563
10564 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010565 if (info != null) {
10566 location[0] += info.mWindowLeft;
10567 location[1] += info.mWindowTop;
10568 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010569 }
10570
10571 /**
10572 * <p>Computes the coordinates of this view in its window. The argument
10573 * must be an array of two integers. After the method returns, the array
10574 * contains the x and y location in that order.</p>
10575 *
10576 * @param location an array of two integers in which to hold the coordinates
10577 */
10578 public void getLocationInWindow(int[] location) {
10579 if (location == null || location.length < 2) {
10580 throw new IllegalArgumentException("location must be an array of "
10581 + "two integers");
10582 }
10583
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010584 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10585 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010586
10587 ViewParent viewParent = mParent;
10588 while (viewParent instanceof View) {
10589 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010590 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10591 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010592 viewParent = view.mParent;
10593 }
Romain Guy8506ab42009-06-11 17:35:47 -070010594
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010595 if (viewParent instanceof ViewRoot) {
10596 // *cough*
10597 final ViewRoot vr = (ViewRoot)viewParent;
10598 location[1] -= vr.mCurScrollY;
10599 }
10600 }
10601
10602 /**
10603 * {@hide}
10604 * @param id the id of the view to be found
10605 * @return the view of the specified id, null if cannot be found
10606 */
10607 protected View findViewTraversal(int id) {
10608 if (id == mID) {
10609 return this;
10610 }
10611 return null;
10612 }
10613
10614 /**
10615 * {@hide}
10616 * @param tag the tag of the view to be found
10617 * @return the view of specified tag, null if cannot be found
10618 */
10619 protected View findViewWithTagTraversal(Object tag) {
10620 if (tag != null && tag.equals(mTag)) {
10621 return this;
10622 }
10623 return null;
10624 }
10625
10626 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010627 * {@hide}
10628 * @param predicate The predicate to evaluate.
10629 * @return The first view that matches the predicate or null.
10630 */
10631 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10632 if (predicate.apply(this)) {
10633 return this;
10634 }
10635 return null;
10636 }
10637
10638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 * Look for a child view with the given id. If this view has the given
10640 * id, return this view.
10641 *
10642 * @param id The id to search for.
10643 * @return The view that has the given id in the hierarchy or null
10644 */
10645 public final View findViewById(int id) {
10646 if (id < 0) {
10647 return null;
10648 }
10649 return findViewTraversal(id);
10650 }
10651
10652 /**
10653 * Look for a child view with the given tag. If this view has the given
10654 * tag, return this view.
10655 *
10656 * @param tag The tag to search for, using "tag.equals(getTag())".
10657 * @return The View that has the given tag in the hierarchy or null
10658 */
10659 public final View findViewWithTag(Object tag) {
10660 if (tag == null) {
10661 return null;
10662 }
10663 return findViewWithTagTraversal(tag);
10664 }
10665
10666 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010667 * {@hide}
10668 * Look for a child view that matches the specified predicate.
10669 * If this view matches the predicate, return this view.
10670 *
10671 * @param predicate The predicate to evaluate.
10672 * @return The first view that matches the predicate or null.
10673 */
10674 public final View findViewByPredicate(Predicate<View> predicate) {
10675 return findViewByPredicateTraversal(predicate);
10676 }
10677
10678 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010679 * Sets the identifier for this view. The identifier does not have to be
10680 * unique in this view's hierarchy. The identifier should be a positive
10681 * number.
10682 *
10683 * @see #NO_ID
10684 * @see #getId
10685 * @see #findViewById
10686 *
10687 * @param id a number used to identify the view
10688 *
10689 * @attr ref android.R.styleable#View_id
10690 */
10691 public void setId(int id) {
10692 mID = id;
10693 }
10694
10695 /**
10696 * {@hide}
10697 *
10698 * @param isRoot true if the view belongs to the root namespace, false
10699 * otherwise
10700 */
10701 public void setIsRootNamespace(boolean isRoot) {
10702 if (isRoot) {
10703 mPrivateFlags |= IS_ROOT_NAMESPACE;
10704 } else {
10705 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10706 }
10707 }
10708
10709 /**
10710 * {@hide}
10711 *
10712 * @return true if the view belongs to the root namespace, false otherwise
10713 */
10714 public boolean isRootNamespace() {
10715 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10716 }
10717
10718 /**
10719 * Returns this view's identifier.
10720 *
10721 * @return a positive integer used to identify the view or {@link #NO_ID}
10722 * if the view has no ID
10723 *
10724 * @see #setId
10725 * @see #findViewById
10726 * @attr ref android.R.styleable#View_id
10727 */
10728 @ViewDebug.CapturedViewProperty
10729 public int getId() {
10730 return mID;
10731 }
10732
10733 /**
10734 * Returns this view's tag.
10735 *
10736 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010737 *
10738 * @see #setTag(Object)
10739 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740 */
10741 @ViewDebug.ExportedProperty
10742 public Object getTag() {
10743 return mTag;
10744 }
10745
10746 /**
10747 * Sets the tag associated with this view. A tag can be used to mark
10748 * a view in its hierarchy and does not have to be unique within the
10749 * hierarchy. Tags can also be used to store data within a view without
10750 * resorting to another data structure.
10751 *
10752 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010753 *
10754 * @see #getTag()
10755 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010756 */
10757 public void setTag(final Object tag) {
10758 mTag = tag;
10759 }
10760
10761 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010762 * Returns the tag associated with this view and the specified key.
10763 *
10764 * @param key The key identifying the tag
10765 *
10766 * @return the Object stored in this view as a tag
10767 *
10768 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010769 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010770 */
10771 public Object getTag(int key) {
10772 SparseArray<Object> tags = null;
10773 synchronized (sTagsLock) {
10774 if (sTags != null) {
10775 tags = sTags.get(this);
10776 }
10777 }
10778
10779 if (tags != null) return tags.get(key);
10780 return null;
10781 }
10782
10783 /**
10784 * Sets a tag associated with this view and a key. A tag can be used
10785 * to mark a view in its hierarchy and does not have to be unique within
10786 * the hierarchy. Tags can also be used to store data within a view
10787 * without resorting to another data structure.
10788 *
10789 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010790 * application to ensure it is unique (see the <a
10791 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10792 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010793 * the Android framework or not associated with any package will cause
10794 * an {@link IllegalArgumentException} to be thrown.
10795 *
10796 * @param key The key identifying the tag
10797 * @param tag An Object to tag the view with
10798 *
10799 * @throws IllegalArgumentException If they specified key is not valid
10800 *
10801 * @see #setTag(Object)
10802 * @see #getTag(int)
10803 */
10804 public void setTag(int key, final Object tag) {
10805 // If the package id is 0x00 or 0x01, it's either an undefined package
10806 // or a framework id
10807 if ((key >>> 24) < 2) {
10808 throw new IllegalArgumentException("The key must be an application-specific "
10809 + "resource id.");
10810 }
10811
10812 setTagInternal(this, key, tag);
10813 }
10814
10815 /**
10816 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10817 * framework id.
10818 *
10819 * @hide
10820 */
10821 public void setTagInternal(int key, Object tag) {
10822 if ((key >>> 24) != 0x1) {
10823 throw new IllegalArgumentException("The key must be a framework-specific "
10824 + "resource id.");
10825 }
10826
Romain Guy8506ab42009-06-11 17:35:47 -070010827 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010828 }
10829
10830 private static void setTagInternal(View view, int key, Object tag) {
10831 SparseArray<Object> tags = null;
10832 synchronized (sTagsLock) {
10833 if (sTags == null) {
10834 sTags = new WeakHashMap<View, SparseArray<Object>>();
10835 } else {
10836 tags = sTags.get(view);
10837 }
10838 }
10839
10840 if (tags == null) {
10841 tags = new SparseArray<Object>(2);
10842 synchronized (sTagsLock) {
10843 sTags.put(view, tags);
10844 }
10845 }
10846
10847 tags.put(key, tag);
10848 }
10849
10850 /**
Romain Guy13922e02009-05-12 17:56:14 -070010851 * @param consistency The type of consistency. See ViewDebug for more information.
10852 *
10853 * @hide
10854 */
10855 protected boolean dispatchConsistencyCheck(int consistency) {
10856 return onConsistencyCheck(consistency);
10857 }
10858
10859 /**
10860 * Method that subclasses should implement to check their consistency. The type of
10861 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010862 *
Romain Guy13922e02009-05-12 17:56:14 -070010863 * @param consistency The type of consistency. See ViewDebug for more information.
10864 *
10865 * @throws IllegalStateException if the view is in an inconsistent state.
10866 *
10867 * @hide
10868 */
10869 protected boolean onConsistencyCheck(int consistency) {
10870 boolean result = true;
10871
10872 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10873 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10874
10875 if (checkLayout) {
10876 if (getParent() == null) {
10877 result = false;
10878 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10879 "View " + this + " does not have a parent.");
10880 }
10881
10882 if (mAttachInfo == null) {
10883 result = false;
10884 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10885 "View " + this + " is not attached to a window.");
10886 }
10887 }
10888
10889 if (checkDrawing) {
10890 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10891 // from their draw() method
10892
10893 if ((mPrivateFlags & DRAWN) != DRAWN &&
10894 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10895 result = false;
10896 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10897 "View " + this + " was invalidated but its drawing cache is valid.");
10898 }
10899 }
10900
10901 return result;
10902 }
10903
10904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010905 * Prints information about this view in the log output, with the tag
10906 * {@link #VIEW_LOG_TAG}.
10907 *
10908 * @hide
10909 */
10910 public void debug() {
10911 debug(0);
10912 }
10913
10914 /**
10915 * Prints information about this view in the log output, with the tag
10916 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10917 * indentation defined by the <code>depth</code>.
10918 *
10919 * @param depth the indentation level
10920 *
10921 * @hide
10922 */
10923 protected void debug(int depth) {
10924 String output = debugIndent(depth - 1);
10925
10926 output += "+ " + this;
10927 int id = getId();
10928 if (id != -1) {
10929 output += " (id=" + id + ")";
10930 }
10931 Object tag = getTag();
10932 if (tag != null) {
10933 output += " (tag=" + tag + ")";
10934 }
10935 Log.d(VIEW_LOG_TAG, output);
10936
10937 if ((mPrivateFlags & FOCUSED) != 0) {
10938 output = debugIndent(depth) + " FOCUSED";
10939 Log.d(VIEW_LOG_TAG, output);
10940 }
10941
10942 output = debugIndent(depth);
10943 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10944 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10945 + "} ";
10946 Log.d(VIEW_LOG_TAG, output);
10947
10948 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10949 || mPaddingBottom != 0) {
10950 output = debugIndent(depth);
10951 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10952 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10953 Log.d(VIEW_LOG_TAG, output);
10954 }
10955
10956 output = debugIndent(depth);
10957 output += "mMeasureWidth=" + mMeasuredWidth +
10958 " mMeasureHeight=" + mMeasuredHeight;
10959 Log.d(VIEW_LOG_TAG, output);
10960
10961 output = debugIndent(depth);
10962 if (mLayoutParams == null) {
10963 output += "BAD! no layout params";
10964 } else {
10965 output = mLayoutParams.debug(output);
10966 }
10967 Log.d(VIEW_LOG_TAG, output);
10968
10969 output = debugIndent(depth);
10970 output += "flags={";
10971 output += View.printFlags(mViewFlags);
10972 output += "}";
10973 Log.d(VIEW_LOG_TAG, output);
10974
10975 output = debugIndent(depth);
10976 output += "privateFlags={";
10977 output += View.printPrivateFlags(mPrivateFlags);
10978 output += "}";
10979 Log.d(VIEW_LOG_TAG, output);
10980 }
10981
10982 /**
10983 * Creates an string of whitespaces used for indentation.
10984 *
10985 * @param depth the indentation level
10986 * @return a String containing (depth * 2 + 3) * 2 white spaces
10987 *
10988 * @hide
10989 */
10990 protected static String debugIndent(int depth) {
10991 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10992 for (int i = 0; i < (depth * 2) + 3; i++) {
10993 spaces.append(' ').append(' ');
10994 }
10995 return spaces.toString();
10996 }
10997
10998 /**
10999 * <p>Return the offset of the widget's text baseline from the widget's top
11000 * boundary. If this widget does not support baseline alignment, this
11001 * method returns -1. </p>
11002 *
11003 * @return the offset of the baseline within the widget's bounds or -1
11004 * if baseline alignment is not supported
11005 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011006 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011007 public int getBaseline() {
11008 return -1;
11009 }
11010
11011 /**
11012 * Call this when something has changed which has invalidated the
11013 * layout of this view. This will schedule a layout pass of the view
11014 * tree.
11015 */
11016 public void requestLayout() {
11017 if (ViewDebug.TRACE_HIERARCHY) {
11018 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11019 }
11020
11021 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011022 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011023
11024 if (mParent != null && !mParent.isLayoutRequested()) {
11025 mParent.requestLayout();
11026 }
11027 }
11028
11029 /**
11030 * Forces this view to be laid out during the next layout pass.
11031 * This method does not call requestLayout() or forceLayout()
11032 * on the parent.
11033 */
11034 public void forceLayout() {
11035 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011036 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011037 }
11038
11039 /**
11040 * <p>
11041 * This is called to find out how big a view should be. The parent
11042 * supplies constraint information in the width and height parameters.
11043 * </p>
11044 *
11045 * <p>
11046 * The actual mesurement work of a view is performed in
11047 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11048 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11049 * </p>
11050 *
11051 *
11052 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11053 * parent
11054 * @param heightMeasureSpec Vertical space requirements as imposed by the
11055 * parent
11056 *
11057 * @see #onMeasure(int, int)
11058 */
11059 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11060 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11061 widthMeasureSpec != mOldWidthMeasureSpec ||
11062 heightMeasureSpec != mOldHeightMeasureSpec) {
11063
11064 // first clears the measured dimension flag
11065 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11066
11067 if (ViewDebug.TRACE_HIERARCHY) {
11068 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11069 }
11070
11071 // measure ourselves, this should set the measured dimension flag back
11072 onMeasure(widthMeasureSpec, heightMeasureSpec);
11073
11074 // flag not set, setMeasuredDimension() was not invoked, we raise
11075 // an exception to warn the developer
11076 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11077 throw new IllegalStateException("onMeasure() did not set the"
11078 + " measured dimension by calling"
11079 + " setMeasuredDimension()");
11080 }
11081
11082 mPrivateFlags |= LAYOUT_REQUIRED;
11083 }
11084
11085 mOldWidthMeasureSpec = widthMeasureSpec;
11086 mOldHeightMeasureSpec = heightMeasureSpec;
11087 }
11088
11089 /**
11090 * <p>
11091 * Measure the view and its content to determine the measured width and the
11092 * measured height. This method is invoked by {@link #measure(int, int)} and
11093 * should be overriden by subclasses to provide accurate and efficient
11094 * measurement of their contents.
11095 * </p>
11096 *
11097 * <p>
11098 * <strong>CONTRACT:</strong> When overriding this method, you
11099 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11100 * measured width and height of this view. Failure to do so will trigger an
11101 * <code>IllegalStateException</code>, thrown by
11102 * {@link #measure(int, int)}. Calling the superclass'
11103 * {@link #onMeasure(int, int)} is a valid use.
11104 * </p>
11105 *
11106 * <p>
11107 * The base class implementation of measure defaults to the background size,
11108 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11109 * override {@link #onMeasure(int, int)} to provide better measurements of
11110 * their content.
11111 * </p>
11112 *
11113 * <p>
11114 * If this method is overridden, it is the subclass's responsibility to make
11115 * sure the measured height and width are at least the view's minimum height
11116 * and width ({@link #getSuggestedMinimumHeight()} and
11117 * {@link #getSuggestedMinimumWidth()}).
11118 * </p>
11119 *
11120 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11121 * The requirements are encoded with
11122 * {@link android.view.View.MeasureSpec}.
11123 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11124 * The requirements are encoded with
11125 * {@link android.view.View.MeasureSpec}.
11126 *
11127 * @see #getMeasuredWidth()
11128 * @see #getMeasuredHeight()
11129 * @see #setMeasuredDimension(int, int)
11130 * @see #getSuggestedMinimumHeight()
11131 * @see #getSuggestedMinimumWidth()
11132 * @see android.view.View.MeasureSpec#getMode(int)
11133 * @see android.view.View.MeasureSpec#getSize(int)
11134 */
11135 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11136 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11137 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11138 }
11139
11140 /**
11141 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11142 * measured width and measured height. Failing to do so will trigger an
11143 * exception at measurement time.</p>
11144 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011145 * @param measuredWidth The measured width of this view. May be a complex
11146 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11147 * {@link #MEASURED_STATE_TOO_SMALL}.
11148 * @param measuredHeight The measured height of this view. May be a complex
11149 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11150 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011151 */
11152 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11153 mMeasuredWidth = measuredWidth;
11154 mMeasuredHeight = measuredHeight;
11155
11156 mPrivateFlags |= MEASURED_DIMENSION_SET;
11157 }
11158
11159 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011160 * Merge two states as returned by {@link #getMeasuredState()}.
11161 * @param curState The current state as returned from a view or the result
11162 * of combining multiple views.
11163 * @param newState The new view state to combine.
11164 * @return Returns a new integer reflecting the combination of the two
11165 * states.
11166 */
11167 public static int combineMeasuredStates(int curState, int newState) {
11168 return curState | newState;
11169 }
11170
11171 /**
11172 * Version of {@link #resolveSizeAndState(int, int, int)}
11173 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11174 */
11175 public static int resolveSize(int size, int measureSpec) {
11176 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11177 }
11178
11179 /**
11180 * Utility to reconcile a desired size and state, with constraints imposed
11181 * by a MeasureSpec. Will take the desired size, unless a different size
11182 * is imposed by the constraints. The returned value is a compound integer,
11183 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11184 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11185 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011186 *
11187 * @param size How big the view wants to be
11188 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011189 * @return Size information bit mask as defined by
11190 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011191 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011192 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 int result = size;
11194 int specMode = MeasureSpec.getMode(measureSpec);
11195 int specSize = MeasureSpec.getSize(measureSpec);
11196 switch (specMode) {
11197 case MeasureSpec.UNSPECIFIED:
11198 result = size;
11199 break;
11200 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011201 if (specSize < size) {
11202 result = specSize | MEASURED_STATE_TOO_SMALL;
11203 } else {
11204 result = size;
11205 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 break;
11207 case MeasureSpec.EXACTLY:
11208 result = specSize;
11209 break;
11210 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011211 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011212 }
11213
11214 /**
11215 * Utility to return a default size. Uses the supplied size if the
11216 * MeasureSpec imposed no contraints. Will get larger if allowed
11217 * by the MeasureSpec.
11218 *
11219 * @param size Default size for this view
11220 * @param measureSpec Constraints imposed by the parent
11221 * @return The size this view should be.
11222 */
11223 public static int getDefaultSize(int size, int measureSpec) {
11224 int result = size;
11225 int specMode = MeasureSpec.getMode(measureSpec);
11226 int specSize = MeasureSpec.getSize(measureSpec);
11227
11228 switch (specMode) {
11229 case MeasureSpec.UNSPECIFIED:
11230 result = size;
11231 break;
11232 case MeasureSpec.AT_MOST:
11233 case MeasureSpec.EXACTLY:
11234 result = specSize;
11235 break;
11236 }
11237 return result;
11238 }
11239
11240 /**
11241 * Returns the suggested minimum height that the view should use. This
11242 * returns the maximum of the view's minimum height
11243 * and the background's minimum height
11244 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11245 * <p>
11246 * When being used in {@link #onMeasure(int, int)}, the caller should still
11247 * ensure the returned height is within the requirements of the parent.
11248 *
11249 * @return The suggested minimum height of the view.
11250 */
11251 protected int getSuggestedMinimumHeight() {
11252 int suggestedMinHeight = mMinHeight;
11253
11254 if (mBGDrawable != null) {
11255 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11256 if (suggestedMinHeight < bgMinHeight) {
11257 suggestedMinHeight = bgMinHeight;
11258 }
11259 }
11260
11261 return suggestedMinHeight;
11262 }
11263
11264 /**
11265 * Returns the suggested minimum width that the view should use. This
11266 * returns the maximum of the view's minimum width)
11267 * and the background's minimum width
11268 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11269 * <p>
11270 * When being used in {@link #onMeasure(int, int)}, the caller should still
11271 * ensure the returned width is within the requirements of the parent.
11272 *
11273 * @return The suggested minimum width of the view.
11274 */
11275 protected int getSuggestedMinimumWidth() {
11276 int suggestedMinWidth = mMinWidth;
11277
11278 if (mBGDrawable != null) {
11279 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11280 if (suggestedMinWidth < bgMinWidth) {
11281 suggestedMinWidth = bgMinWidth;
11282 }
11283 }
11284
11285 return suggestedMinWidth;
11286 }
11287
11288 /**
11289 * Sets the minimum height of the view. It is not guaranteed the view will
11290 * be able to achieve this minimum height (for example, if its parent layout
11291 * constrains it with less available height).
11292 *
11293 * @param minHeight The minimum height the view will try to be.
11294 */
11295 public void setMinimumHeight(int minHeight) {
11296 mMinHeight = minHeight;
11297 }
11298
11299 /**
11300 * Sets the minimum width of the view. It is not guaranteed the view will
11301 * be able to achieve this minimum width (for example, if its parent layout
11302 * constrains it with less available width).
11303 *
11304 * @param minWidth The minimum width the view will try to be.
11305 */
11306 public void setMinimumWidth(int minWidth) {
11307 mMinWidth = minWidth;
11308 }
11309
11310 /**
11311 * Get the animation currently associated with this view.
11312 *
11313 * @return The animation that is currently playing or
11314 * scheduled to play for this view.
11315 */
11316 public Animation getAnimation() {
11317 return mCurrentAnimation;
11318 }
11319
11320 /**
11321 * Start the specified animation now.
11322 *
11323 * @param animation the animation to start now
11324 */
11325 public void startAnimation(Animation animation) {
11326 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11327 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011328 invalidateParentCaches();
11329 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011330 }
11331
11332 /**
11333 * Cancels any animations for this view.
11334 */
11335 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011336 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011337 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011338 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011339 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011340 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011341 }
11342
11343 /**
11344 * Sets the next animation to play for this view.
11345 * If you want the animation to play immediately, use
11346 * startAnimation. This method provides allows fine-grained
11347 * control over the start time and invalidation, but you
11348 * must make sure that 1) the animation has a start time set, and
11349 * 2) the view will be invalidated when the animation is supposed to
11350 * start.
11351 *
11352 * @param animation The next animation, or null.
11353 */
11354 public void setAnimation(Animation animation) {
11355 mCurrentAnimation = animation;
11356 if (animation != null) {
11357 animation.reset();
11358 }
11359 }
11360
11361 /**
11362 * Invoked by a parent ViewGroup to notify the start of the animation
11363 * currently associated with this view. If you override this method,
11364 * always call super.onAnimationStart();
11365 *
11366 * @see #setAnimation(android.view.animation.Animation)
11367 * @see #getAnimation()
11368 */
11369 protected void onAnimationStart() {
11370 mPrivateFlags |= ANIMATION_STARTED;
11371 }
11372
11373 /**
11374 * Invoked by a parent ViewGroup to notify the end of the animation
11375 * currently associated with this view. If you override this method,
11376 * always call super.onAnimationEnd();
11377 *
11378 * @see #setAnimation(android.view.animation.Animation)
11379 * @see #getAnimation()
11380 */
11381 protected void onAnimationEnd() {
11382 mPrivateFlags &= ~ANIMATION_STARTED;
11383 }
11384
11385 /**
11386 * Invoked if there is a Transform that involves alpha. Subclass that can
11387 * draw themselves with the specified alpha should return true, and then
11388 * respect that alpha when their onDraw() is called. If this returns false
11389 * then the view may be redirected to draw into an offscreen buffer to
11390 * fulfill the request, which will look fine, but may be slower than if the
11391 * subclass handles it internally. The default implementation returns false.
11392 *
11393 * @param alpha The alpha (0..255) to apply to the view's drawing
11394 * @return true if the view can draw with the specified alpha.
11395 */
11396 protected boolean onSetAlpha(int alpha) {
11397 return false;
11398 }
11399
11400 /**
11401 * This is used by the RootView to perform an optimization when
11402 * the view hierarchy contains one or several SurfaceView.
11403 * SurfaceView is always considered transparent, but its children are not,
11404 * therefore all View objects remove themselves from the global transparent
11405 * region (passed as a parameter to this function).
11406 *
11407 * @param region The transparent region for this ViewRoot (window).
11408 *
11409 * @return Returns true if the effective visibility of the view at this
11410 * point is opaque, regardless of the transparent region; returns false
11411 * if it is possible for underlying windows to be seen behind the view.
11412 *
11413 * {@hide}
11414 */
11415 public boolean gatherTransparentRegion(Region region) {
11416 final AttachInfo attachInfo = mAttachInfo;
11417 if (region != null && attachInfo != null) {
11418 final int pflags = mPrivateFlags;
11419 if ((pflags & SKIP_DRAW) == 0) {
11420 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11421 // remove it from the transparent region.
11422 final int[] location = attachInfo.mTransparentLocation;
11423 getLocationInWindow(location);
11424 region.op(location[0], location[1], location[0] + mRight - mLeft,
11425 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11426 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11427 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11428 // exists, so we remove the background drawable's non-transparent
11429 // parts from this transparent region.
11430 applyDrawableToTransparentRegion(mBGDrawable, region);
11431 }
11432 }
11433 return true;
11434 }
11435
11436 /**
11437 * Play a sound effect for this view.
11438 *
11439 * <p>The framework will play sound effects for some built in actions, such as
11440 * clicking, but you may wish to play these effects in your widget,
11441 * for instance, for internal navigation.
11442 *
11443 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11444 * {@link #isSoundEffectsEnabled()} is true.
11445 *
11446 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11447 */
11448 public void playSoundEffect(int soundConstant) {
11449 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11450 return;
11451 }
11452 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11453 }
11454
11455 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011456 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011457 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011458 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 *
11460 * <p>The framework will provide haptic feedback for some built in actions,
11461 * such as long presses, but you may wish to provide feedback for your
11462 * own widget.
11463 *
11464 * <p>The feedback will only be performed if
11465 * {@link #isHapticFeedbackEnabled()} is true.
11466 *
11467 * @param feedbackConstant One of the constants defined in
11468 * {@link HapticFeedbackConstants}
11469 */
11470 public boolean performHapticFeedback(int feedbackConstant) {
11471 return performHapticFeedback(feedbackConstant, 0);
11472 }
11473
11474 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011475 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011476 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011477 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011478 *
11479 * @param feedbackConstant One of the constants defined in
11480 * {@link HapticFeedbackConstants}
11481 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11482 */
11483 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11484 if (mAttachInfo == null) {
11485 return false;
11486 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011487 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011488 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011489 && !isHapticFeedbackEnabled()) {
11490 return false;
11491 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011492 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11493 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011494 }
11495
11496 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011497 * Request that the visibility of the status bar be changed.
11498 */
11499 public void setSystemUiVisibility(int visibility) {
11500 if (visibility != mSystemUiVisibility) {
11501 mSystemUiVisibility = visibility;
11502 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11503 mParent.recomputeViewAttributes(this);
11504 }
11505 }
11506 }
11507
11508 /**
11509 * Returns the status bar visibility that this view has requested.
11510 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011511 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011512 return mSystemUiVisibility;
11513 }
11514
11515 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11516 mOnSystemUiVisibilityChangeListener = l;
11517 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11518 mParent.recomputeViewAttributes(this);
11519 }
11520 }
11521
11522 /**
11523 */
11524 public void dispatchSystemUiVisibilityChanged(int visibility) {
11525 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011526 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011527 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011528 }
11529 }
11530
11531 /**
Joe Malin32736f02011-01-19 16:14:20 -080011532 * Creates an image that the system displays during the drag and drop
11533 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11534 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11535 * appearance as the given View. The default also positions the center of the drag shadow
11536 * directly under the touch point. If no View is provided (the constructor with no parameters
11537 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11538 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11539 * default is an invisible drag shadow.
11540 * <p>
11541 * You are not required to use the View you provide to the constructor as the basis of the
11542 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11543 * anything you want as the drag shadow.
11544 * </p>
11545 * <p>
11546 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11547 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11548 * size and position of the drag shadow. It uses this data to construct a
11549 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11550 * so that your application can draw the shadow image in the Canvas.
11551 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011552 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011553 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011554 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011555
11556 /**
Joe Malin32736f02011-01-19 16:14:20 -080011557 * Constructs a shadow image builder based on a View. By default, the resulting drag
11558 * shadow will have the same appearance and dimensions as the View, with the touch point
11559 * over the center of the View.
11560 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011561 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011562 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011563 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011564 }
11565
Christopher Tate17ed60c2011-01-18 12:50:26 -080011566 /**
11567 * Construct a shadow builder object with no associated View. This
11568 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11569 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11570 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011571 * reference to any View object. If they are not overridden, then the result is an
11572 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011573 */
11574 public DragShadowBuilder() {
11575 mView = new WeakReference<View>(null);
11576 }
11577
11578 /**
11579 * Returns the View object that had been passed to the
11580 * {@link #View.DragShadowBuilder(View)}
11581 * constructor. If that View parameter was {@code null} or if the
11582 * {@link #View.DragShadowBuilder()}
11583 * constructor was used to instantiate the builder object, this method will return
11584 * null.
11585 *
11586 * @return The View object associate with this builder object.
11587 */
Chris Tate6b391282010-10-14 15:48:59 -070011588 final public View getView() {
11589 return mView.get();
11590 }
11591
Christopher Tate2c095f32010-10-04 14:13:40 -070011592 /**
Joe Malin32736f02011-01-19 16:14:20 -080011593 * Provides the metrics for the shadow image. These include the dimensions of
11594 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011595 * be centered under the touch location while dragging.
11596 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011597 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011598 * same as the dimensions of the View itself and centers the shadow under
11599 * the touch point.
11600 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011601 *
Joe Malin32736f02011-01-19 16:14:20 -080011602 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11603 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11604 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11605 * image.
11606 *
11607 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11608 * shadow image that should be underneath the touch point during the drag and drop
11609 * operation. Your application must set {@link android.graphics.Point#x} to the
11610 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011611 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011612 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011613 final View view = mView.get();
11614 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011615 shadowSize.set(view.getWidth(), view.getHeight());
11616 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011617 } else {
11618 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11619 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011620 }
11621
11622 /**
Joe Malin32736f02011-01-19 16:14:20 -080011623 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11624 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011625 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011626 *
Joe Malin32736f02011-01-19 16:14:20 -080011627 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011628 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011629 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011630 final View view = mView.get();
11631 if (view != null) {
11632 view.draw(canvas);
11633 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011634 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011635 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011636 }
11637 }
11638
11639 /**
Joe Malin32736f02011-01-19 16:14:20 -080011640 * Starts a drag and drop operation. When your application calls this method, it passes a
11641 * {@link android.view.View.DragShadowBuilder} object to the system. The
11642 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11643 * to get metrics for the drag shadow, and then calls the object's
11644 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11645 * <p>
11646 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11647 * drag events to all the View objects in your application that are currently visible. It does
11648 * this either by calling the View object's drag listener (an implementation of
11649 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11650 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11651 * Both are passed a {@link android.view.DragEvent} object that has a
11652 * {@link android.view.DragEvent#getAction()} value of
11653 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11654 * </p>
11655 * <p>
11656 * Your application can invoke startDrag() on any attached View object. The View object does not
11657 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11658 * be related to the View the user selected for dragging.
11659 * </p>
11660 * @param data A {@link android.content.ClipData} object pointing to the data to be
11661 * transferred by the drag and drop operation.
11662 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11663 * drag shadow.
11664 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11665 * drop operation. This Object is put into every DragEvent object sent by the system during the
11666 * current drag.
11667 * <p>
11668 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11669 * to the target Views. For example, it can contain flags that differentiate between a
11670 * a copy operation and a move operation.
11671 * </p>
11672 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11673 * so the parameter should be set to 0.
11674 * @return {@code true} if the method completes successfully, or
11675 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11676 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011677 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011678 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011679 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011680 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011681 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011682 }
11683 boolean okay = false;
11684
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011685 Point shadowSize = new Point();
11686 Point shadowTouchPoint = new Point();
11687 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011688
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011689 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11690 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11691 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011692 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011693
Chris Tatea32dcf72010-10-14 12:13:50 -070011694 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011695 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11696 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011697 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011698 Surface surface = new Surface();
11699 try {
11700 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011701 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011702 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011703 + " surface=" + surface);
11704 if (token != null) {
11705 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011706 try {
Chris Tate6b391282010-10-14 15:48:59 -070011707 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011708 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011709 } finally {
11710 surface.unlockCanvasAndPost(canvas);
11711 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011712
Christopher Tate407b4e92010-11-30 17:14:08 -080011713 final ViewRoot root = getViewRoot();
11714
11715 // Cache the local state object for delivery with DragEvents
11716 root.setLocalDragState(myLocalState);
11717
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011718 // repurpose 'shadowSize' for the last touch point
11719 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011720
Christopher Tatea53146c2010-09-07 11:57:52 -070011721 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011722 shadowSize.x, shadowSize.y,
11723 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011724 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011725 }
11726 } catch (Exception e) {
11727 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11728 surface.destroy();
11729 }
11730
11731 return okay;
11732 }
11733
Christopher Tatea53146c2010-09-07 11:57:52 -070011734 /**
Joe Malin32736f02011-01-19 16:14:20 -080011735 * Handles drag events sent by the system following a call to
11736 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11737 *<p>
11738 * When the system calls this method, it passes a
11739 * {@link android.view.DragEvent} object. A call to
11740 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11741 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11742 * operation.
11743 * @param event The {@link android.view.DragEvent} sent by the system.
11744 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11745 * in DragEvent, indicating the type of drag event represented by this object.
11746 * @return {@code true} if the method was successful, otherwise {@code false}.
11747 * <p>
11748 * The method should return {@code true} in response to an action type of
11749 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11750 * operation.
11751 * </p>
11752 * <p>
11753 * The method should also return {@code true} in response to an action type of
11754 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11755 * {@code false} if it didn't.
11756 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011757 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011758 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011759 return false;
11760 }
11761
11762 /**
Joe Malin32736f02011-01-19 16:14:20 -080011763 * Detects if this View is enabled and has a drag event listener.
11764 * If both are true, then it calls the drag event listener with the
11765 * {@link android.view.DragEvent} it received. If the drag event listener returns
11766 * {@code true}, then dispatchDragEvent() returns {@code true}.
11767 * <p>
11768 * For all other cases, the method calls the
11769 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11770 * method and returns its result.
11771 * </p>
11772 * <p>
11773 * This ensures that a drag event is always consumed, even if the View does not have a drag
11774 * event listener. However, if the View has a listener and the listener returns true, then
11775 * onDragEvent() is not called.
11776 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011777 */
11778 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011779 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011780 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11781 && mOnDragListener.onDrag(this, event)) {
11782 return true;
11783 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011784 return onDragEvent(event);
11785 }
11786
Christopher Tate3d4bf172011-03-28 16:16:46 -070011787 boolean canAcceptDrag() {
11788 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11789 }
11790
Christopher Tatea53146c2010-09-07 11:57:52 -070011791 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011792 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11793 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011794 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011795 */
11796 public void onCloseSystemDialogs(String reason) {
11797 }
Joe Malin32736f02011-01-19 16:14:20 -080011798
Dianne Hackbornffa42482009-09-23 22:20:11 -070011799 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011800 * Given a Drawable whose bounds have been set to draw into this view,
11801 * update a Region being computed for {@link #gatherTransparentRegion} so
11802 * that any non-transparent parts of the Drawable are removed from the
11803 * given transparent region.
11804 *
11805 * @param dr The Drawable whose transparency is to be applied to the region.
11806 * @param region A Region holding the current transparency information,
11807 * where any parts of the region that are set are considered to be
11808 * transparent. On return, this region will be modified to have the
11809 * transparency information reduced by the corresponding parts of the
11810 * Drawable that are not transparent.
11811 * {@hide}
11812 */
11813 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11814 if (DBG) {
11815 Log.i("View", "Getting transparent region for: " + this);
11816 }
11817 final Region r = dr.getTransparentRegion();
11818 final Rect db = dr.getBounds();
11819 final AttachInfo attachInfo = mAttachInfo;
11820 if (r != null && attachInfo != null) {
11821 final int w = getRight()-getLeft();
11822 final int h = getBottom()-getTop();
11823 if (db.left > 0) {
11824 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11825 r.op(0, 0, db.left, h, Region.Op.UNION);
11826 }
11827 if (db.right < w) {
11828 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11829 r.op(db.right, 0, w, h, Region.Op.UNION);
11830 }
11831 if (db.top > 0) {
11832 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11833 r.op(0, 0, w, db.top, Region.Op.UNION);
11834 }
11835 if (db.bottom < h) {
11836 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11837 r.op(0, db.bottom, w, h, Region.Op.UNION);
11838 }
11839 final int[] location = attachInfo.mTransparentLocation;
11840 getLocationInWindow(location);
11841 r.translate(location[0], location[1]);
11842 region.op(r, Region.Op.INTERSECT);
11843 } else {
11844 region.op(db, Region.Op.DIFFERENCE);
11845 }
11846 }
11847
Adam Powelle14579b2009-12-16 18:39:52 -080011848 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011849 mHasPerformedLongPress = false;
11850
11851 if (mPendingCheckForLongPress == null) {
11852 mPendingCheckForLongPress = new CheckForLongPress();
11853 }
11854 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011855 postDelayed(mPendingCheckForLongPress,
11856 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857 }
11858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 /**
11860 * Inflate a view from an XML resource. This convenience method wraps the {@link
11861 * LayoutInflater} class, which provides a full range of options for view inflation.
11862 *
11863 * @param context The Context object for your activity or application.
11864 * @param resource The resource ID to inflate
11865 * @param root A view group that will be the parent. Used to properly inflate the
11866 * layout_* parameters.
11867 * @see LayoutInflater
11868 */
11869 public static View inflate(Context context, int resource, ViewGroup root) {
11870 LayoutInflater factory = LayoutInflater.from(context);
11871 return factory.inflate(resource, root);
11872 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 /**
Adam Powell637d3372010-08-25 14:37:03 -070011875 * Scroll the view with standard behavior for scrolling beyond the normal
11876 * content boundaries. Views that call this method should override
11877 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11878 * results of an over-scroll operation.
11879 *
11880 * Views can use this method to handle any touch or fling-based scrolling.
11881 *
11882 * @param deltaX Change in X in pixels
11883 * @param deltaY Change in Y in pixels
11884 * @param scrollX Current X scroll value in pixels before applying deltaX
11885 * @param scrollY Current Y scroll value in pixels before applying deltaY
11886 * @param scrollRangeX Maximum content scroll range along the X axis
11887 * @param scrollRangeY Maximum content scroll range along the Y axis
11888 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11889 * along the X axis.
11890 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11891 * along the Y axis.
11892 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11893 * @return true if scrolling was clamped to an over-scroll boundary along either
11894 * axis, false otherwise.
11895 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011896 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011897 protected boolean overScrollBy(int deltaX, int deltaY,
11898 int scrollX, int scrollY,
11899 int scrollRangeX, int scrollRangeY,
11900 int maxOverScrollX, int maxOverScrollY,
11901 boolean isTouchEvent) {
11902 final int overScrollMode = mOverScrollMode;
11903 final boolean canScrollHorizontal =
11904 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11905 final boolean canScrollVertical =
11906 computeVerticalScrollRange() > computeVerticalScrollExtent();
11907 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11908 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11909 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11910 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11911
11912 int newScrollX = scrollX + deltaX;
11913 if (!overScrollHorizontal) {
11914 maxOverScrollX = 0;
11915 }
11916
11917 int newScrollY = scrollY + deltaY;
11918 if (!overScrollVertical) {
11919 maxOverScrollY = 0;
11920 }
11921
11922 // Clamp values if at the limits and record
11923 final int left = -maxOverScrollX;
11924 final int right = maxOverScrollX + scrollRangeX;
11925 final int top = -maxOverScrollY;
11926 final int bottom = maxOverScrollY + scrollRangeY;
11927
11928 boolean clampedX = false;
11929 if (newScrollX > right) {
11930 newScrollX = right;
11931 clampedX = true;
11932 } else if (newScrollX < left) {
11933 newScrollX = left;
11934 clampedX = true;
11935 }
11936
11937 boolean clampedY = false;
11938 if (newScrollY > bottom) {
11939 newScrollY = bottom;
11940 clampedY = true;
11941 } else if (newScrollY < top) {
11942 newScrollY = top;
11943 clampedY = true;
11944 }
11945
11946 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11947
11948 return clampedX || clampedY;
11949 }
11950
11951 /**
11952 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11953 * respond to the results of an over-scroll operation.
11954 *
11955 * @param scrollX New X scroll value in pixels
11956 * @param scrollY New Y scroll value in pixels
11957 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11958 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11959 */
11960 protected void onOverScrolled(int scrollX, int scrollY,
11961 boolean clampedX, boolean clampedY) {
11962 // Intentionally empty.
11963 }
11964
11965 /**
11966 * Returns the over-scroll mode for this view. The result will be
11967 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11968 * (allow over-scrolling only if the view content is larger than the container),
11969 * or {@link #OVER_SCROLL_NEVER}.
11970 *
11971 * @return This view's over-scroll mode.
11972 */
11973 public int getOverScrollMode() {
11974 return mOverScrollMode;
11975 }
11976
11977 /**
11978 * Set the over-scroll mode for this view. Valid over-scroll modes are
11979 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11980 * (allow over-scrolling only if the view content is larger than the container),
11981 * or {@link #OVER_SCROLL_NEVER}.
11982 *
11983 * Setting the over-scroll mode of a view will have an effect only if the
11984 * view is capable of scrolling.
11985 *
11986 * @param overScrollMode The new over-scroll mode for this view.
11987 */
11988 public void setOverScrollMode(int overScrollMode) {
11989 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11990 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11991 overScrollMode != OVER_SCROLL_NEVER) {
11992 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11993 }
11994 mOverScrollMode = overScrollMode;
11995 }
11996
11997 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011998 * Gets a scale factor that determines the distance the view should scroll
11999 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12000 * @return The vertical scroll scale factor.
12001 * @hide
12002 */
12003 protected float getVerticalScrollFactor() {
12004 if (mVerticalScrollFactor == 0) {
12005 TypedValue outValue = new TypedValue();
12006 if (!mContext.getTheme().resolveAttribute(
12007 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12008 throw new IllegalStateException(
12009 "Expected theme to define listPreferredItemHeight.");
12010 }
12011 mVerticalScrollFactor = outValue.getDimension(
12012 mContext.getResources().getDisplayMetrics());
12013 }
12014 return mVerticalScrollFactor;
12015 }
12016
12017 /**
12018 * Gets a scale factor that determines the distance the view should scroll
12019 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12020 * @return The horizontal scroll scale factor.
12021 * @hide
12022 */
12023 protected float getHorizontalScrollFactor() {
12024 // TODO: Should use something else.
12025 return getVerticalScrollFactor();
12026 }
12027
12028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012029 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12030 * Each MeasureSpec represents a requirement for either the width or the height.
12031 * A MeasureSpec is comprised of a size and a mode. There are three possible
12032 * modes:
12033 * <dl>
12034 * <dt>UNSPECIFIED</dt>
12035 * <dd>
12036 * The parent has not imposed any constraint on the child. It can be whatever size
12037 * it wants.
12038 * </dd>
12039 *
12040 * <dt>EXACTLY</dt>
12041 * <dd>
12042 * The parent has determined an exact size for the child. The child is going to be
12043 * given those bounds regardless of how big it wants to be.
12044 * </dd>
12045 *
12046 * <dt>AT_MOST</dt>
12047 * <dd>
12048 * The child can be as large as it wants up to the specified size.
12049 * </dd>
12050 * </dl>
12051 *
12052 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12053 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12054 */
12055 public static class MeasureSpec {
12056 private static final int MODE_SHIFT = 30;
12057 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12058
12059 /**
12060 * Measure specification mode: The parent has not imposed any constraint
12061 * on the child. It can be whatever size it wants.
12062 */
12063 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12064
12065 /**
12066 * Measure specification mode: The parent has determined an exact size
12067 * for the child. The child is going to be given those bounds regardless
12068 * of how big it wants to be.
12069 */
12070 public static final int EXACTLY = 1 << MODE_SHIFT;
12071
12072 /**
12073 * Measure specification mode: The child can be as large as it wants up
12074 * to the specified size.
12075 */
12076 public static final int AT_MOST = 2 << MODE_SHIFT;
12077
12078 /**
12079 * Creates a measure specification based on the supplied size and mode.
12080 *
12081 * The mode must always be one of the following:
12082 * <ul>
12083 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12084 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12085 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12086 * </ul>
12087 *
12088 * @param size the size of the measure specification
12089 * @param mode the mode of the measure specification
12090 * @return the measure specification based on size and mode
12091 */
12092 public static int makeMeasureSpec(int size, int mode) {
12093 return size + mode;
12094 }
12095
12096 /**
12097 * Extracts the mode from the supplied measure specification.
12098 *
12099 * @param measureSpec the measure specification to extract the mode from
12100 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12101 * {@link android.view.View.MeasureSpec#AT_MOST} or
12102 * {@link android.view.View.MeasureSpec#EXACTLY}
12103 */
12104 public static int getMode(int measureSpec) {
12105 return (measureSpec & MODE_MASK);
12106 }
12107
12108 /**
12109 * Extracts the size from the supplied measure specification.
12110 *
12111 * @param measureSpec the measure specification to extract the size from
12112 * @return the size in pixels defined in the supplied measure specification
12113 */
12114 public static int getSize(int measureSpec) {
12115 return (measureSpec & ~MODE_MASK);
12116 }
12117
12118 /**
12119 * Returns a String representation of the specified measure
12120 * specification.
12121 *
12122 * @param measureSpec the measure specification to convert to a String
12123 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12124 */
12125 public static String toString(int measureSpec) {
12126 int mode = getMode(measureSpec);
12127 int size = getSize(measureSpec);
12128
12129 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12130
12131 if (mode == UNSPECIFIED)
12132 sb.append("UNSPECIFIED ");
12133 else if (mode == EXACTLY)
12134 sb.append("EXACTLY ");
12135 else if (mode == AT_MOST)
12136 sb.append("AT_MOST ");
12137 else
12138 sb.append(mode).append(" ");
12139
12140 sb.append(size);
12141 return sb.toString();
12142 }
12143 }
12144
12145 class CheckForLongPress implements Runnable {
12146
12147 private int mOriginalWindowAttachCount;
12148
12149 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012150 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012151 && mOriginalWindowAttachCount == mWindowAttachCount) {
12152 if (performLongClick()) {
12153 mHasPerformedLongPress = true;
12154 }
12155 }
12156 }
12157
12158 public void rememberWindowAttachCount() {
12159 mOriginalWindowAttachCount = mWindowAttachCount;
12160 }
12161 }
Joe Malin32736f02011-01-19 16:14:20 -080012162
Adam Powelle14579b2009-12-16 18:39:52 -080012163 private final class CheckForTap implements Runnable {
12164 public void run() {
12165 mPrivateFlags &= ~PREPRESSED;
12166 mPrivateFlags |= PRESSED;
12167 refreshDrawableState();
12168 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12169 postCheckForLongClick(ViewConfiguration.getTapTimeout());
12170 }
12171 }
12172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173
Adam Powella35d7682010-03-12 14:48:13 -080012174 private final class PerformClick implements Runnable {
12175 public void run() {
12176 performClick();
12177 }
12178 }
12179
Dianne Hackborn63042d62011-01-26 18:56:29 -080012180 /** @hide */
12181 public void hackTurnOffWindowResizeAnim(boolean off) {
12182 mAttachInfo.mTurnOffWindowResizeAnim = off;
12183 }
Joe Malin32736f02011-01-19 16:14:20 -080012184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012185 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012186 * This method returns a ViewPropertyAnimator object, which can be used to animate
12187 * specific properties on this View.
12188 *
12189 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12190 */
12191 public ViewPropertyAnimator animate() {
12192 if (mAnimator == null) {
12193 mAnimator = new ViewPropertyAnimator(this);
12194 }
12195 return mAnimator;
12196 }
12197
12198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 * Interface definition for a callback to be invoked when a key event is
12200 * dispatched to this view. The callback will be invoked before the key
12201 * event is given to the view.
12202 */
12203 public interface OnKeyListener {
12204 /**
12205 * Called when a key is dispatched to a view. This allows listeners to
12206 * get a chance to respond before the target view.
12207 *
12208 * @param v The view the key has been dispatched to.
12209 * @param keyCode The code for the physical key that was pressed
12210 * @param event The KeyEvent object containing full information about
12211 * the event.
12212 * @return True if the listener has consumed the event, false otherwise.
12213 */
12214 boolean onKey(View v, int keyCode, KeyEvent event);
12215 }
12216
12217 /**
12218 * Interface definition for a callback to be invoked when a touch event is
12219 * dispatched to this view. The callback will be invoked before the touch
12220 * event is given to the view.
12221 */
12222 public interface OnTouchListener {
12223 /**
12224 * Called when a touch event is dispatched to a view. This allows listeners to
12225 * get a chance to respond before the target view.
12226 *
12227 * @param v The view the touch event has been dispatched to.
12228 * @param event The MotionEvent object containing full information about
12229 * the event.
12230 * @return True if the listener has consumed the event, false otherwise.
12231 */
12232 boolean onTouch(View v, MotionEvent event);
12233 }
12234
12235 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012236 * Interface definition for a callback to be invoked when a generic motion event is
12237 * dispatched to this view. The callback will be invoked before the generic motion
12238 * event is given to the view.
12239 */
12240 public interface OnGenericMotionListener {
12241 /**
12242 * Called when a generic motion event is dispatched to a view. This allows listeners to
12243 * get a chance to respond before the target view.
12244 *
12245 * @param v The view the generic motion event has been dispatched to.
12246 * @param event The MotionEvent object containing full information about
12247 * the event.
12248 * @return True if the listener has consumed the event, false otherwise.
12249 */
12250 boolean onGenericMotion(View v, MotionEvent event);
12251 }
12252
12253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 * Interface definition for a callback to be invoked when a view has been clicked and held.
12255 */
12256 public interface OnLongClickListener {
12257 /**
12258 * Called when a view has been clicked and held.
12259 *
12260 * @param v The view that was clicked and held.
12261 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012262 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012263 */
12264 boolean onLongClick(View v);
12265 }
12266
12267 /**
Chris Tate32affef2010-10-18 15:29:21 -070012268 * Interface definition for a callback to be invoked when a drag is being dispatched
12269 * to this view. The callback will be invoked before the hosting view's own
12270 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12271 * onDrag(event) behavior, it should return 'false' from this callback.
12272 */
12273 public interface OnDragListener {
12274 /**
12275 * Called when a drag event is dispatched to a view. This allows listeners
12276 * to get a chance to override base View behavior.
12277 *
Joe Malin32736f02011-01-19 16:14:20 -080012278 * @param v The View that received the drag event.
12279 * @param event The {@link android.view.DragEvent} object for the drag event.
12280 * @return {@code true} if the drag event was handled successfully, or {@code false}
12281 * if the drag event was not handled. Note that {@code false} will trigger the View
12282 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012283 */
12284 boolean onDrag(View v, DragEvent event);
12285 }
12286
12287 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 * Interface definition for a callback to be invoked when the focus state of
12289 * a view changed.
12290 */
12291 public interface OnFocusChangeListener {
12292 /**
12293 * Called when the focus state of a view has changed.
12294 *
12295 * @param v The view whose state has changed.
12296 * @param hasFocus The new focus state of v.
12297 */
12298 void onFocusChange(View v, boolean hasFocus);
12299 }
12300
12301 /**
12302 * Interface definition for a callback to be invoked when a view is clicked.
12303 */
12304 public interface OnClickListener {
12305 /**
12306 * Called when a view has been clicked.
12307 *
12308 * @param v The view that was clicked.
12309 */
12310 void onClick(View v);
12311 }
12312
12313 /**
12314 * Interface definition for a callback to be invoked when the context menu
12315 * for this view is being built.
12316 */
12317 public interface OnCreateContextMenuListener {
12318 /**
12319 * Called when the context menu for this view is being built. It is not
12320 * safe to hold onto the menu after this method returns.
12321 *
12322 * @param menu The context menu that is being built
12323 * @param v The view for which the context menu is being built
12324 * @param menuInfo Extra information about the item for which the
12325 * context menu should be shown. This information will vary
12326 * depending on the class of v.
12327 */
12328 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12329 }
12330
Joe Onorato664644d2011-01-23 17:53:23 -080012331 /**
12332 * Interface definition for a callback to be invoked when the status bar changes
12333 * visibility.
12334 *
12335 * @see #setOnSystemUiVisibilityChangeListener
12336 */
12337 public interface OnSystemUiVisibilityChangeListener {
12338 /**
12339 * Called when the status bar changes visibility because of a call to
12340 * {@link #setSystemUiVisibility}.
12341 *
12342 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12343 */
12344 public void onSystemUiVisibilityChange(int visibility);
12345 }
12346
Adam Powell4afd62b2011-02-18 15:02:18 -080012347 /**
12348 * Interface definition for a callback to be invoked when this view is attached
12349 * or detached from its window.
12350 */
12351 public interface OnAttachStateChangeListener {
12352 /**
12353 * Called when the view is attached to a window.
12354 * @param v The view that was attached
12355 */
12356 public void onViewAttachedToWindow(View v);
12357 /**
12358 * Called when the view is detached from a window.
12359 * @param v The view that was detached
12360 */
12361 public void onViewDetachedFromWindow(View v);
12362 }
12363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012364 private final class UnsetPressedState implements Runnable {
12365 public void run() {
12366 setPressed(false);
12367 }
12368 }
12369
12370 /**
12371 * Base class for derived classes that want to save and restore their own
12372 * state in {@link android.view.View#onSaveInstanceState()}.
12373 */
12374 public static class BaseSavedState extends AbsSavedState {
12375 /**
12376 * Constructor used when reading from a parcel. Reads the state of the superclass.
12377 *
12378 * @param source
12379 */
12380 public BaseSavedState(Parcel source) {
12381 super(source);
12382 }
12383
12384 /**
12385 * Constructor called by derived classes when creating their SavedState objects
12386 *
12387 * @param superState The state of the superclass of this view
12388 */
12389 public BaseSavedState(Parcelable superState) {
12390 super(superState);
12391 }
12392
12393 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12394 new Parcelable.Creator<BaseSavedState>() {
12395 public BaseSavedState createFromParcel(Parcel in) {
12396 return new BaseSavedState(in);
12397 }
12398
12399 public BaseSavedState[] newArray(int size) {
12400 return new BaseSavedState[size];
12401 }
12402 };
12403 }
12404
12405 /**
12406 * A set of information given to a view when it is attached to its parent
12407 * window.
12408 */
12409 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012410 interface Callbacks {
12411 void playSoundEffect(int effectId);
12412 boolean performHapticFeedback(int effectId, boolean always);
12413 }
12414
12415 /**
12416 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12417 * to a Handler. This class contains the target (View) to invalidate and
12418 * the coordinates of the dirty rectangle.
12419 *
12420 * For performance purposes, this class also implements a pool of up to
12421 * POOL_LIMIT objects that get reused. This reduces memory allocations
12422 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 */
Romain Guyd928d682009-03-31 17:52:16 -070012424 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012425 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012426 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12427 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012428 public InvalidateInfo newInstance() {
12429 return new InvalidateInfo();
12430 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012431
Romain Guyd928d682009-03-31 17:52:16 -070012432 public void onAcquired(InvalidateInfo element) {
12433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012434
Romain Guyd928d682009-03-31 17:52:16 -070012435 public void onReleased(InvalidateInfo element) {
12436 }
12437 }, POOL_LIMIT)
12438 );
12439
12440 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012441
12442 View target;
12443
12444 int left;
12445 int top;
12446 int right;
12447 int bottom;
12448
Romain Guyd928d682009-03-31 17:52:16 -070012449 public void setNextPoolable(InvalidateInfo element) {
12450 mNext = element;
12451 }
12452
12453 public InvalidateInfo getNextPoolable() {
12454 return mNext;
12455 }
12456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012457 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012458 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012459 }
12460
12461 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012462 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012463 }
12464 }
12465
12466 final IWindowSession mSession;
12467
12468 final IWindow mWindow;
12469
12470 final IBinder mWindowToken;
12471
12472 final Callbacks mRootCallbacks;
12473
Chet Haasedaf98e92011-01-10 14:10:36 -080012474 Canvas mHardwareCanvas;
12475
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012476 /**
12477 * The top view of the hierarchy.
12478 */
12479 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012480
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012481 IBinder mPanelParentWindowToken;
12482 Surface mSurface;
12483
Romain Guyb051e892010-09-28 19:09:36 -070012484 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012485 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012486 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012487
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012488 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012489 * Scale factor used by the compatibility mode
12490 */
12491 float mApplicationScale;
12492
12493 /**
12494 * Indicates whether the application is in compatibility mode
12495 */
12496 boolean mScalingRequired;
12497
12498 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012499 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12500 */
12501 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012502
Dianne Hackborn63042d62011-01-26 18:56:29 -080012503 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012504 * Left position of this view's window
12505 */
12506 int mWindowLeft;
12507
12508 /**
12509 * Top position of this view's window
12510 */
12511 int mWindowTop;
12512
12513 /**
Adam Powell26153a32010-11-08 15:22:27 -080012514 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012515 */
Adam Powell26153a32010-11-08 15:22:27 -080012516 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012517
12518 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012519 * For windows that are full-screen but using insets to layout inside
12520 * of the screen decorations, these are the current insets for the
12521 * content of the window.
12522 */
12523 final Rect mContentInsets = new Rect();
12524
12525 /**
12526 * For windows that are full-screen but using insets to layout inside
12527 * of the screen decorations, these are the current insets for the
12528 * actual visible parts of the window.
12529 */
12530 final Rect mVisibleInsets = new Rect();
12531
12532 /**
12533 * The internal insets given by this window. This value is
12534 * supplied by the client (through
12535 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12536 * be given to the window manager when changed to be used in laying
12537 * out windows behind it.
12538 */
12539 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12540 = new ViewTreeObserver.InternalInsetsInfo();
12541
12542 /**
12543 * All views in the window's hierarchy that serve as scroll containers,
12544 * used to determine if the window can be resized or must be panned
12545 * to adjust for a soft input area.
12546 */
12547 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12548
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012549 final KeyEvent.DispatcherState mKeyDispatchState
12550 = new KeyEvent.DispatcherState();
12551
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012552 /**
12553 * Indicates whether the view's window currently has the focus.
12554 */
12555 boolean mHasWindowFocus;
12556
12557 /**
12558 * The current visibility of the window.
12559 */
12560 int mWindowVisibility;
12561
12562 /**
12563 * Indicates the time at which drawing started to occur.
12564 */
12565 long mDrawingTime;
12566
12567 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012568 * Indicates whether or not ignoring the DIRTY_MASK flags.
12569 */
12570 boolean mIgnoreDirtyState;
12571
12572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012573 * Indicates whether the view's window is currently in touch mode.
12574 */
12575 boolean mInTouchMode;
12576
12577 /**
12578 * Indicates that ViewRoot should trigger a global layout change
12579 * the next time it performs a traversal
12580 */
12581 boolean mRecomputeGlobalAttributes;
12582
12583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012584 * Set during a traveral if any views want to keep the screen on.
12585 */
12586 boolean mKeepScreenOn;
12587
12588 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012589 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12590 */
12591 int mSystemUiVisibility;
12592
12593 /**
12594 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12595 * attached.
12596 */
12597 boolean mHasSystemUiListeners;
12598
12599 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012600 * Set if the visibility of any views has changed.
12601 */
12602 boolean mViewVisibilityChanged;
12603
12604 /**
12605 * Set to true if a view has been scrolled.
12606 */
12607 boolean mViewScrollChanged;
12608
12609 /**
12610 * Global to the view hierarchy used as a temporary for dealing with
12611 * x/y points in the transparent region computations.
12612 */
12613 final int[] mTransparentLocation = new int[2];
12614
12615 /**
12616 * Global to the view hierarchy used as a temporary for dealing with
12617 * x/y points in the ViewGroup.invalidateChild implementation.
12618 */
12619 final int[] mInvalidateChildLocation = new int[2];
12620
Chet Haasec3aa3612010-06-17 08:50:37 -070012621
12622 /**
12623 * Global to the view hierarchy used as a temporary for dealing with
12624 * x/y location when view is transformed.
12625 */
12626 final float[] mTmpTransformLocation = new float[2];
12627
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012628 /**
12629 * The view tree observer used to dispatch global events like
12630 * layout, pre-draw, touch mode change, etc.
12631 */
12632 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12633
12634 /**
12635 * A Canvas used by the view hierarchy to perform bitmap caching.
12636 */
12637 Canvas mCanvas;
12638
12639 /**
12640 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12641 * handler can be used to pump events in the UI events queue.
12642 */
12643 final Handler mHandler;
12644
12645 /**
12646 * Identifier for messages requesting the view to be invalidated.
12647 * Such messages should be sent to {@link #mHandler}.
12648 */
12649 static final int INVALIDATE_MSG = 0x1;
12650
12651 /**
12652 * Identifier for messages requesting the view to invalidate a region.
12653 * Such messages should be sent to {@link #mHandler}.
12654 */
12655 static final int INVALIDATE_RECT_MSG = 0x2;
12656
12657 /**
12658 * Temporary for use in computing invalidate rectangles while
12659 * calling up the hierarchy.
12660 */
12661 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012662
12663 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012664 * Temporary for use in computing hit areas with transformed views
12665 */
12666 final RectF mTmpTransformRect = new RectF();
12667
12668 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012669 * Temporary list for use in collecting focusable descendents of a view.
12670 */
12671 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12672
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012673 /**
12674 * Creates a new set of attachment information with the specified
12675 * events handler and thread.
12676 *
12677 * @param handler the events handler the view must use
12678 */
12679 AttachInfo(IWindowSession session, IWindow window,
12680 Handler handler, Callbacks effectPlayer) {
12681 mSession = session;
12682 mWindow = window;
12683 mWindowToken = window.asBinder();
12684 mHandler = handler;
12685 mRootCallbacks = effectPlayer;
12686 }
12687 }
12688
12689 /**
12690 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12691 * is supported. This avoids keeping too many unused fields in most
12692 * instances of View.</p>
12693 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012694 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012695
Mike Cleronf116bf82009-09-27 19:14:12 -070012696 /**
12697 * Scrollbars are not visible
12698 */
12699 public static final int OFF = 0;
12700
12701 /**
12702 * Scrollbars are visible
12703 */
12704 public static final int ON = 1;
12705
12706 /**
12707 * Scrollbars are fading away
12708 */
12709 public static final int FADING = 2;
12710
12711 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012713 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012714 public int scrollBarDefaultDelayBeforeFade;
12715 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012716
12717 public int scrollBarSize;
12718 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012719 public float[] interpolatorValues;
12720 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721
12722 public final Paint paint;
12723 public final Matrix matrix;
12724 public Shader shader;
12725
Mike Cleronf116bf82009-09-27 19:14:12 -070012726 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12727
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012728 private static final float[] OPAQUE = { 255 };
12729 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012730
Mike Cleronf116bf82009-09-27 19:14:12 -070012731 /**
12732 * When fading should start. This time moves into the future every time
12733 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12734 */
12735 public long fadeStartTime;
12736
12737
12738 /**
12739 * The current state of the scrollbars: ON, OFF, or FADING
12740 */
12741 public int state = OFF;
12742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012743 private int mLastColor;
12744
Mike Cleronf116bf82009-09-27 19:14:12 -070012745 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012746 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12747 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012748 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12749 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012750
12751 paint = new Paint();
12752 matrix = new Matrix();
12753 // use use a height of 1, and then wack the matrix each time we
12754 // actually use it.
12755 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012757 paint.setShader(shader);
12758 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012759 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012760 }
Romain Guy8506ab42009-06-11 17:35:47 -070012761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012762 public void setFadeColor(int color) {
12763 if (color != 0 && color != mLastColor) {
12764 mLastColor = color;
12765 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012766
Romain Guye55e1a72009-08-27 10:42:26 -070012767 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12768 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012769
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012770 paint.setShader(shader);
12771 // Restore the default transfer mode (src_over)
12772 paint.setXfermode(null);
12773 }
12774 }
Joe Malin32736f02011-01-19 16:14:20 -080012775
Mike Cleronf116bf82009-09-27 19:14:12 -070012776 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012777 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012778 if (now >= fadeStartTime) {
12779
12780 // the animation fades the scrollbars out by changing
12781 // the opacity (alpha) from fully opaque to fully
12782 // transparent
12783 int nextFrame = (int) now;
12784 int framesCount = 0;
12785
12786 Interpolator interpolator = scrollBarInterpolator;
12787
12788 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012789 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012790
12791 // End transparent
12792 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012793 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012794
12795 state = FADING;
12796
12797 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012798 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012799 }
12800 }
12801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012802 }
12803}