blob: ffd6de2bfd96a14734ddd539026c4a3cd3abea5d [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080061import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001307
1308 static final int[] VIEW_STATE_IDS = new int[] {
1309 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1310 R.attr.state_selected, VIEW_STATE_SELECTED,
1311 R.attr.state_focused, VIEW_STATE_FOCUSED,
1312 R.attr.state_enabled, VIEW_STATE_ENABLED,
1313 R.attr.state_pressed, VIEW_STATE_PRESSED,
1314 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001315 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 };
1317
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001319 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1320 throw new IllegalStateException(
1321 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1322 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001326 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001327 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001328 orderedIds[i * 2] = viewState;
1329 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 }
1331 }
1332 }
Romain Guyb051e892010-09-28 19:09:36 -07001333 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1334 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1335 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 int numBits = Integer.bitCount(i);
1337 int[] set = new int[numBits];
1338 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001339 for (int j = 0; j < orderedIds.length; j += 2) {
1340 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001341 set[pos++] = orderedIds[j];
1342 }
1343 }
1344 VIEW_STATE_SETS[i] = set;
1345 }
1346
1347 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1348 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1349 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1350 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1351 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1352 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1353 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1357 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1358 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1359 | VIEW_STATE_FOCUSED];
1360 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1361 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1365 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1366 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1367 | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1370 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1371 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1372 | VIEW_STATE_ENABLED];
1373 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1374 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1375 | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1378 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1379
1380 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1381 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1385 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1386 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1387 | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1390 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1391 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1392 | VIEW_STATE_PRESSED];
1393 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1395 | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1398 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1401 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1402 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1403 | VIEW_STATE_PRESSED];
1404 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1406 | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1409 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1418 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1422 | VIEW_STATE_PRESSED];
1423 }
1424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001426 * Temporary Rect currently for use in setBackground(). This will probably
1427 * be extended in the future to hold our own class with more than just
1428 * a Rect. :)
1429 */
1430 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001431
1432 /**
1433 * Map used to store views' tags.
1434 */
1435 private static WeakHashMap<View, SparseArray<Object>> sTags;
1436
1437 /**
1438 * Lock used to access sTags.
1439 */
1440 private static final Object sTagsLock = new Object();
1441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 /**
1443 * The animation currently associated with this view.
1444 * @hide
1445 */
1446 protected Animation mCurrentAnimation = null;
1447
1448 /**
1449 * Width as measured during measure pass.
1450 * {@hide}
1451 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001452 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001453 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001454
1455 /**
1456 * Height as measured during measure pass.
1457 * {@hide}
1458 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001459 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001460 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001461
1462 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001463 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1464 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1465 * its display list. This flag, used only when hw accelerated, allows us to clear the
1466 * flag while retaining this information until it's needed (at getDisplayList() time and
1467 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1468 *
1469 * {@hide}
1470 */
1471 boolean mRecreateDisplayList = false;
1472
1473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 * The view's identifier.
1475 * {@hide}
1476 *
1477 * @see #setId(int)
1478 * @see #getId()
1479 */
1480 @ViewDebug.ExportedProperty(resolveId = true)
1481 int mID = NO_ID;
1482
1483 /**
1484 * The view's tag.
1485 * {@hide}
1486 *
1487 * @see #setTag(Object)
1488 * @see #getTag()
1489 */
1490 protected Object mTag;
1491
1492 // for mPrivateFlags:
1493 /** {@hide} */
1494 static final int WANTS_FOCUS = 0x00000001;
1495 /** {@hide} */
1496 static final int FOCUSED = 0x00000002;
1497 /** {@hide} */
1498 static final int SELECTED = 0x00000004;
1499 /** {@hide} */
1500 static final int IS_ROOT_NAMESPACE = 0x00000008;
1501 /** {@hide} */
1502 static final int HAS_BOUNDS = 0x00000010;
1503 /** {@hide} */
1504 static final int DRAWN = 0x00000020;
1505 /**
1506 * When this flag is set, this view is running an animation on behalf of its
1507 * children and should therefore not cancel invalidate requests, even if they
1508 * lie outside of this view's bounds.
1509 *
1510 * {@hide}
1511 */
1512 static final int DRAW_ANIMATION = 0x00000040;
1513 /** {@hide} */
1514 static final int SKIP_DRAW = 0x00000080;
1515 /** {@hide} */
1516 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1517 /** {@hide} */
1518 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1519 /** {@hide} */
1520 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1521 /** {@hide} */
1522 static final int MEASURED_DIMENSION_SET = 0x00000800;
1523 /** {@hide} */
1524 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001525 /** {@hide} */
1526 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 private static final int PRESSED = 0x00004000;
1529
1530 /** {@hide} */
1531 static final int DRAWING_CACHE_VALID = 0x00008000;
1532 /**
1533 * Flag used to indicate that this view should be drawn once more (and only once
1534 * more) after its animation has completed.
1535 * {@hide}
1536 */
1537 static final int ANIMATION_STARTED = 0x00010000;
1538
1539 private static final int SAVE_STATE_CALLED = 0x00020000;
1540
1541 /**
1542 * Indicates that the View returned true when onSetAlpha() was called and that
1543 * the alpha must be restored.
1544 * {@hide}
1545 */
1546 static final int ALPHA_SET = 0x00040000;
1547
1548 /**
1549 * Set by {@link #setScrollContainer(boolean)}.
1550 */
1551 static final int SCROLL_CONTAINER = 0x00080000;
1552
1553 /**
1554 * Set by {@link #setScrollContainer(boolean)}.
1555 */
1556 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1557
1558 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001559 * View flag indicating whether this view was invalidated (fully or partially.)
1560 *
1561 * @hide
1562 */
1563 static final int DIRTY = 0x00200000;
1564
1565 /**
1566 * View flag indicating whether this view was invalidated by an opaque
1567 * invalidate request.
1568 *
1569 * @hide
1570 */
1571 static final int DIRTY_OPAQUE = 0x00400000;
1572
1573 /**
1574 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1575 *
1576 * @hide
1577 */
1578 static final int DIRTY_MASK = 0x00600000;
1579
1580 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001581 * Indicates whether the background is opaque.
1582 *
1583 * @hide
1584 */
1585 static final int OPAQUE_BACKGROUND = 0x00800000;
1586
1587 /**
1588 * Indicates whether the scrollbars are opaque.
1589 *
1590 * @hide
1591 */
1592 static final int OPAQUE_SCROLLBARS = 0x01000000;
1593
1594 /**
1595 * Indicates whether the view is opaque.
1596 *
1597 * @hide
1598 */
1599 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001600
Adam Powelle14579b2009-12-16 18:39:52 -08001601 /**
1602 * Indicates a prepressed state;
1603 * the short time between ACTION_DOWN and recognizing
1604 * a 'real' press. Prepressed is used to recognize quick taps
1605 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001606 *
Adam Powelle14579b2009-12-16 18:39:52 -08001607 * @hide
1608 */
1609 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001610
Adam Powellc9fbaab2010-02-16 17:16:19 -08001611 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001612 * Indicates whether the view is temporarily detached.
1613 *
1614 * @hide
1615 */
1616 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001617
Adam Powell8568c3a2010-04-19 14:26:11 -07001618 /**
1619 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001620 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001621 * @hide
1622 */
1623 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001624
1625 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001626 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1627 * for transform operations
1628 *
1629 * @hide
1630 */
Adam Powellf37df072010-09-17 16:22:49 -07001631 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001632
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001633 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001634 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001635
Chet Haasefd2b0022010-08-06 13:08:56 -07001636 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001637 * Indicates that this view was specifically invalidated, not just dirtied because some
1638 * child view was invalidated. The flag is used to determine when we need to recreate
1639 * a view's display list (as opposed to just returning a reference to its existing
1640 * display list).
1641 *
1642 * @hide
1643 */
1644 static final int INVALIDATED = 0x80000000;
1645
1646 /**
Adam Powell637d3372010-08-25 14:37:03 -07001647 * Always allow a user to over-scroll this view, provided it is a
1648 * view that can scroll.
1649 *
1650 * @see #getOverScrollMode()
1651 * @see #setOverScrollMode(int)
1652 */
1653 public static final int OVER_SCROLL_ALWAYS = 0;
1654
1655 /**
1656 * Allow a user to over-scroll this view only if the content is large
1657 * enough to meaningfully scroll, provided it is a view that can scroll.
1658 *
1659 * @see #getOverScrollMode()
1660 * @see #setOverScrollMode(int)
1661 */
1662 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1663
1664 /**
1665 * Never allow a user to over-scroll this view.
1666 *
1667 * @see #getOverScrollMode()
1668 * @see #setOverScrollMode(int)
1669 */
1670 public static final int OVER_SCROLL_NEVER = 2;
1671
1672 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001673 * View has requested the status bar to be visible (the default).
1674 *
Joe Malin32736f02011-01-19 16:14:20 -08001675 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001676 */
1677 public static final int STATUS_BAR_VISIBLE = 0;
1678
1679 /**
1680 * View has requested the status bar to be visible (the default).
1681 *
Joe Malin32736f02011-01-19 16:14:20 -08001682 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001683 */
1684 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1685
1686 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001687 * @hide
1688 *
1689 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1690 * out of the public fields to keep the undefined bits out of the developer's way.
1691 *
1692 * Flag to make the status bar not expandable. Unless you also
1693 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1694 */
1695 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1696
1697 /**
1698 * @hide
1699 *
1700 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1701 * out of the public fields to keep the undefined bits out of the developer's way.
1702 *
1703 * Flag to hide notification icons and scrolling ticker text.
1704 */
1705 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1706
1707 /**
1708 * @hide
1709 *
1710 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1711 * out of the public fields to keep the undefined bits out of the developer's way.
1712 *
1713 * Flag to disable incoming notification alerts. This will not block
1714 * icons, but it will block sound, vibrating and other visual or aural notifications.
1715 */
1716 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1717
1718 /**
1719 * @hide
1720 *
1721 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1722 * out of the public fields to keep the undefined bits out of the developer's way.
1723 *
1724 * Flag to hide only the scrolling ticker. Note that
1725 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1726 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1727 */
1728 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1729
1730 /**
1731 * @hide
1732 *
1733 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1734 * out of the public fields to keep the undefined bits out of the developer's way.
1735 *
1736 * Flag to hide the center system info area.
1737 */
1738 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1739
1740 /**
1741 * @hide
1742 *
1743 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1744 * out of the public fields to keep the undefined bits out of the developer's way.
1745 *
1746 * Flag to hide only the navigation buttons. Don't use this
1747 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001748 *
1749 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001750 */
1751 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1752
1753 /**
1754 * @hide
1755 *
1756 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1757 * out of the public fields to keep the undefined bits out of the developer's way.
1758 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001759 * Flag to hide only the back button. Don't use this
1760 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1761 */
1762 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1763
1764 /**
1765 * @hide
1766 *
1767 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1768 * out of the public fields to keep the undefined bits out of the developer's way.
1769 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001770 * Flag to hide only the clock. You might use this if your activity has
1771 * its own clock making the status bar's clock redundant.
1772 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001773 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1774
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001775 /**
1776 * @hide
1777 */
1778 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001779
1780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Controls the over-scroll mode for this view.
1782 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1783 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1784 * and {@link #OVER_SCROLL_NEVER}.
1785 */
1786 private int mOverScrollMode;
1787
1788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * The parent this view is attached to.
1790 * {@hide}
1791 *
1792 * @see #getParent()
1793 */
1794 protected ViewParent mParent;
1795
1796 /**
1797 * {@hide}
1798 */
1799 AttachInfo mAttachInfo;
1800
1801 /**
1802 * {@hide}
1803 */
Romain Guy809a7f62009-05-14 15:44:42 -07001804 @ViewDebug.ExportedProperty(flagMapping = {
1805 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1806 name = "FORCE_LAYOUT"),
1807 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1808 name = "LAYOUT_REQUIRED"),
1809 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001810 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001811 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1813 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1815 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int mPrivateFlags;
1817
1818 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001819 * This view's request for the visibility of the status bar.
1820 * @hide
1821 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001822 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001823 int mSystemUiVisibility;
1824
1825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 * Count of how many windows this view has been attached to.
1827 */
1828 int mWindowAttachCount;
1829
1830 /**
1831 * The layout parameters associated with this view and used by the parent
1832 * {@link android.view.ViewGroup} to determine how this view should be
1833 * laid out.
1834 * {@hide}
1835 */
1836 protected ViewGroup.LayoutParams mLayoutParams;
1837
1838 /**
1839 * The view flags hold various views states.
1840 * {@hide}
1841 */
1842 @ViewDebug.ExportedProperty
1843 int mViewFlags;
1844
1845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001846 * The transform matrix for the View. This transform is calculated internally
1847 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1848 * is used by default. Do *not* use this variable directly; instead call
1849 * getMatrix(), which will automatically recalculate the matrix if necessary
1850 * to get the correct matrix based on the latest rotation and scale properties.
1851 */
1852 private final Matrix mMatrix = new Matrix();
1853
1854 /**
1855 * The transform matrix for the View. This transform is calculated internally
1856 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1857 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001858 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001859 * to get the correct matrix based on the latest rotation and scale properties.
1860 */
1861 private Matrix mInverseMatrix;
1862
1863 /**
1864 * An internal variable that tracks whether we need to recalculate the
1865 * transform matrix, based on whether the rotation or scaleX/Y properties
1866 * have changed since the matrix was last calculated.
1867 */
1868 private boolean mMatrixDirty = false;
1869
1870 /**
1871 * An internal variable that tracks whether we need to recalculate the
1872 * transform matrix, based on whether the rotation or scaleX/Y properties
1873 * have changed since the matrix was last calculated.
1874 */
1875 private boolean mInverseMatrixDirty = true;
1876
1877 /**
1878 * A variable that tracks whether we need to recalculate the
1879 * transform matrix, based on whether the rotation or scaleX/Y properties
1880 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001881 * is only valid after a call to updateMatrix() or to a function that
1882 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001883 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001884 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001885
1886 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001887 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1888 */
1889 private Camera mCamera = null;
1890
1891 /**
1892 * This matrix is used when computing the matrix for 3D rotations.
1893 */
1894 private Matrix matrix3D = null;
1895
1896 /**
1897 * These prev values are used to recalculate a centered pivot point when necessary. The
1898 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1899 * set), so thes values are only used then as well.
1900 */
1901 private int mPrevWidth = -1;
1902 private int mPrevHeight = -1;
1903
Joe Malin32736f02011-01-19 16:14:20 -08001904 private boolean mLastIsOpaque;
1905
Chet Haasefd2b0022010-08-06 13:08:56 -07001906 /**
1907 * Convenience value to check for float values that are close enough to zero to be considered
1908 * zero.
1909 */
Romain Guy2542d192010-08-18 11:47:12 -07001910 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001911
1912 /**
1913 * The degrees rotation around the vertical axis through the pivot point.
1914 */
1915 @ViewDebug.ExportedProperty
1916 private float mRotationY = 0f;
1917
1918 /**
1919 * The degrees rotation around the horizontal axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
1922 private float mRotationX = 0f;
1923
1924 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001925 * The degrees rotation around the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
1928 private float mRotation = 0f;
1929
1930 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001931 * The amount of translation of the object away from its left property (post-layout).
1932 */
1933 @ViewDebug.ExportedProperty
1934 private float mTranslationX = 0f;
1935
1936 /**
1937 * The amount of translation of the object away from its top property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
1940 private float mTranslationY = 0f;
1941
1942 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001943 * The amount of scale in the x direction around the pivot point. A
1944 * value of 1 means no scaling is applied.
1945 */
1946 @ViewDebug.ExportedProperty
1947 private float mScaleX = 1f;
1948
1949 /**
1950 * The amount of scale in the y direction around the pivot point. A
1951 * value of 1 means no scaling is applied.
1952 */
1953 @ViewDebug.ExportedProperty
1954 private float mScaleY = 1f;
1955
1956 /**
1957 * The amount of scale in the x direction around the pivot point. A
1958 * value of 1 means no scaling is applied.
1959 */
1960 @ViewDebug.ExportedProperty
1961 private float mPivotX = 0f;
1962
1963 /**
1964 * The amount of scale in the y direction around the pivot point. A
1965 * value of 1 means no scaling is applied.
1966 */
1967 @ViewDebug.ExportedProperty
1968 private float mPivotY = 0f;
1969
1970 /**
1971 * The opacity of the View. This is a value from 0 to 1, where 0 means
1972 * completely transparent and 1 means completely opaque.
1973 */
1974 @ViewDebug.ExportedProperty
1975 private float mAlpha = 1f;
1976
1977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * The distance in pixels from the left edge of this view's parent
1979 * to the left edge of this view.
1980 * {@hide}
1981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 protected int mLeft;
1984 /**
1985 * The distance in pixels from the left edge of this view's parent
1986 * to the right edge of this view.
1987 * {@hide}
1988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 protected int mRight;
1991 /**
1992 * The distance in pixels from the top edge of this view's parent
1993 * to the top edge of this view.
1994 * {@hide}
1995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001996 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected int mTop;
1998 /**
1999 * The distance in pixels from the top edge of this view's parent
2000 * to the bottom edge of this view.
2001 * {@hide}
2002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002003 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 protected int mBottom;
2005
2006 /**
2007 * The offset, in pixels, by which the content of this view is scrolled
2008 * horizontally.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mScrollX;
2013 /**
2014 * The offset, in pixels, by which the content of this view is scrolled
2015 * vertically.
2016 * {@hide}
2017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002018 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 protected int mScrollY;
2020
2021 /**
2022 * The left padding in pixels, that is the distance in pixels between the
2023 * left edge of this view and the left edge of its content.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mPaddingLeft;
2028 /**
2029 * The right padding in pixels, that is the distance in pixels between the
2030 * right edge of this view and the right edge of its content.
2031 * {@hide}
2032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002033 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 protected int mPaddingRight;
2035 /**
2036 * The top padding in pixels, that is the distance in pixels between the
2037 * top edge of this view and the top edge of its content.
2038 * {@hide}
2039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002040 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 protected int mPaddingTop;
2042 /**
2043 * The bottom padding in pixels, that is the distance in pixels between the
2044 * bottom edge of this view and the bottom edge of its content.
2045 * {@hide}
2046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002047 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 protected int mPaddingBottom;
2049
2050 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002051 * Briefly describes the view and is primarily used for accessibility support.
2052 */
2053 private CharSequence mContentDescription;
2054
2055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * Cache the paddingRight set by the user to append to the scrollbar's size.
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int mUserPaddingRight;
2060
2061 /**
2062 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingBottom;
2066
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002067 /**
Adam Powell20232d02010-12-08 21:08:53 -08002068 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2069 */
2070 @ViewDebug.ExportedProperty(category = "padding")
2071 int mUserPaddingLeft;
2072
2073 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002074 * @hide
2075 */
2076 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2077 /**
2078 * @hide
2079 */
2080 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 private Resources mResources = null;
2083
2084 private Drawable mBGDrawable;
2085
2086 private int mBackgroundResource;
2087 private boolean mBackgroundSizeChanged;
2088
2089 /**
2090 * Listener used to dispatch focus change events.
2091 * This field should be made private, so it is hidden from the SDK.
2092 * {@hide}
2093 */
2094 protected OnFocusChangeListener mOnFocusChangeListener;
2095
2096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002097 * Listeners for layout change events.
2098 */
2099 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2100
2101 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002102 * Listeners for attach events.
2103 */
2104 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2105
2106 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 * Listener used to dispatch click events.
2108 * This field should be made private, so it is hidden from the SDK.
2109 * {@hide}
2110 */
2111 protected OnClickListener mOnClickListener;
2112
2113 /**
2114 * Listener used to dispatch long click events.
2115 * This field should be made private, so it is hidden from the SDK.
2116 * {@hide}
2117 */
2118 protected OnLongClickListener mOnLongClickListener;
2119
2120 /**
2121 * Listener used to build the context menu.
2122 * This field should be made private, so it is hidden from the SDK.
2123 * {@hide}
2124 */
2125 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2126
2127 private OnKeyListener mOnKeyListener;
2128
2129 private OnTouchListener mOnTouchListener;
2130
Chris Tate32affef2010-10-18 15:29:21 -07002131 private OnDragListener mOnDragListener;
2132
Joe Onorato664644d2011-01-23 17:53:23 -08002133 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002135 /**
2136 * The application environment this view lives in.
2137 * This field should be made private, so it is hidden from the SDK.
2138 * {@hide}
2139 */
2140 protected Context mContext;
2141
2142 private ScrollabilityCache mScrollCache;
2143
2144 private int[] mDrawableState = null;
2145
Romain Guy0211a0a2011-02-14 16:34:59 -08002146 /**
2147 * Set to true when drawing cache is enabled and cannot be created.
2148 *
2149 * @hide
2150 */
2151 public boolean mCachingFailed;
2152
Romain Guy02890fd2010-08-06 17:58:44 -07002153 private Bitmap mDrawingCache;
2154 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002155 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002156 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002157
2158 /**
2159 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2160 * the user may specify which view to go to next.
2161 */
2162 private int mNextFocusLeftId = View.NO_ID;
2163
2164 /**
2165 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2166 * the user may specify which view to go to next.
2167 */
2168 private int mNextFocusRightId = View.NO_ID;
2169
2170 /**
2171 * When this view has focus and the next focus is {@link #FOCUS_UP},
2172 * the user may specify which view to go to next.
2173 */
2174 private int mNextFocusUpId = View.NO_ID;
2175
2176 /**
2177 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2178 * the user may specify which view to go to next.
2179 */
2180 private int mNextFocusDownId = View.NO_ID;
2181
Jeff Brown4e6319b2010-12-13 10:36:51 -08002182 /**
2183 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2184 * the user may specify which view to go to next.
2185 */
2186 int mNextFocusForwardId = View.NO_ID;
2187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002188 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002189 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002190 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002191
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 private UnsetPressedState mUnsetPressedState;
2193
2194 /**
2195 * Whether the long press's action has been invoked. The tap's action is invoked on the
2196 * up event while a long press is invoked as soon as the long press duration is reached, so
2197 * a long press could be performed before the tap is checked, in which case the tap's action
2198 * should not be invoked.
2199 */
2200 private boolean mHasPerformedLongPress;
2201
2202 /**
2203 * The minimum height of the view. We'll try our best to have the height
2204 * of this view to at least this amount.
2205 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002206 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 private int mMinHeight;
2208
2209 /**
2210 * The minimum width of the view. We'll try our best to have the width
2211 * of this view to at least this amount.
2212 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002213 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 private int mMinWidth;
2215
2216 /**
2217 * The delegate to handle touch events that are physically in this view
2218 * but should be handled by another view.
2219 */
2220 private TouchDelegate mTouchDelegate = null;
2221
2222 /**
2223 * Solid color to use as a background when creating the drawing cache. Enables
2224 * the cache to use 16 bit bitmaps instead of 32 bit.
2225 */
2226 private int mDrawingCacheBackgroundColor = 0;
2227
2228 /**
2229 * Special tree observer used when mAttachInfo is null.
2230 */
2231 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002232
Adam Powelle14579b2009-12-16 18:39:52 -08002233 /**
2234 * Cache the touch slop from the context that created the view.
2235 */
2236 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002237
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002239 * Cache drag/drop state
2240 *
2241 */
2242 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002243
2244 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002245 * Flag indicating that a drag can cross window boundaries. When
2246 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2247 * with this flag set, all visible applications will be able to participate
2248 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002249 *
2250 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002251 */
2252 public static final int DRAG_FLAG_GLOBAL = 1;
2253
2254 /**
Adam Powell20232d02010-12-08 21:08:53 -08002255 * Position of the vertical scroll bar.
2256 */
2257 private int mVerticalScrollbarPosition;
2258
2259 /**
2260 * Position the scroll bar at the default position as determined by the system.
2261 */
2262 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2263
2264 /**
2265 * Position the scroll bar along the left edge.
2266 */
2267 public static final int SCROLLBAR_POSITION_LEFT = 1;
2268
2269 /**
2270 * Position the scroll bar along the right edge.
2271 */
2272 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2273
2274 /**
Romain Guy171c5922011-01-06 10:04:23 -08002275 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002276 *
2277 * @see #getLayerType()
2278 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002279 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002280 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002281 */
2282 public static final int LAYER_TYPE_NONE = 0;
2283
2284 /**
2285 * <p>Indicates that the view has a software layer. A software layer is backed
2286 * by a bitmap and causes the view to be rendered using Android's software
2287 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002288 *
Romain Guy171c5922011-01-06 10:04:23 -08002289 * <p>Software layers have various usages:</p>
2290 * <p>When the application is not using hardware acceleration, a software layer
2291 * is useful to apply a specific color filter and/or blending mode and/or
2292 * translucency to a view and all its children.</p>
2293 * <p>When the application is using hardware acceleration, a software layer
2294 * is useful to render drawing primitives not supported by the hardware
2295 * accelerated pipeline. It can also be used to cache a complex view tree
2296 * into a texture and reduce the complexity of drawing operations. For instance,
2297 * when animating a complex view tree with a translation, a software layer can
2298 * be used to render the view tree only once.</p>
2299 * <p>Software layers should be avoided when the affected view tree updates
2300 * often. Every update will require to re-render the software layer, which can
2301 * potentially be slow (particularly when hardware acceleration is turned on
2302 * since the layer will have to be uploaded into a hardware texture after every
2303 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002304 *
2305 * @see #getLayerType()
2306 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002307 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002308 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002309 */
2310 public static final int LAYER_TYPE_SOFTWARE = 1;
2311
2312 /**
2313 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2314 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2315 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2316 * rendering pipeline, but only if hardware acceleration is turned on for the
2317 * view hierarchy. When hardware acceleration is turned off, hardware layers
2318 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002319 *
Romain Guy171c5922011-01-06 10:04:23 -08002320 * <p>A hardware layer is useful to apply a specific color filter and/or
2321 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002322 * <p>A hardware layer can be used to cache a complex view tree into a
2323 * texture and reduce the complexity of drawing operations. For instance,
2324 * when animating a complex view tree with a translation, a hardware layer can
2325 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002326 * <p>A hardware layer can also be used to increase the rendering quality when
2327 * rotation transformations are applied on a view. It can also be used to
2328 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002329 *
2330 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002331 * @see #setLayerType(int, android.graphics.Paint)
2332 * @see #LAYER_TYPE_NONE
2333 * @see #LAYER_TYPE_SOFTWARE
2334 */
2335 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002336
Romain Guy3aaff3a2011-01-12 14:18:47 -08002337 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2338 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2339 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2340 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2341 })
Romain Guy171c5922011-01-06 10:04:23 -08002342 int mLayerType = LAYER_TYPE_NONE;
2343 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002344 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002345
2346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347 * Simple constructor to use when creating a view from code.
2348 *
2349 * @param context The Context the view is running in, through which it can
2350 * access the current theme, resources, etc.
2351 */
2352 public View(Context context) {
2353 mContext = context;
2354 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002355 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002356 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002357 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 }
2359
2360 /**
2361 * Constructor that is called when inflating a view from XML. This is called
2362 * when a view is being constructed from an XML file, supplying attributes
2363 * that were specified in the XML file. This version uses a default style of
2364 * 0, so the only attribute values applied are those in the Context's Theme
2365 * and the given AttributeSet.
2366 *
2367 * <p>
2368 * The method onFinishInflate() will be called after all children have been
2369 * added.
2370 *
2371 * @param context The Context the view is running in, through which it can
2372 * access the current theme, resources, etc.
2373 * @param attrs The attributes of the XML tag that is inflating the view.
2374 * @see #View(Context, AttributeSet, int)
2375 */
2376 public View(Context context, AttributeSet attrs) {
2377 this(context, attrs, 0);
2378 }
2379
2380 /**
2381 * Perform inflation from XML and apply a class-specific base style. This
2382 * constructor of View allows subclasses to use their own base style when
2383 * they are inflating. For example, a Button class's constructor would call
2384 * this version of the super class constructor and supply
2385 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2386 * the theme's button style to modify all of the base view attributes (in
2387 * particular its background) as well as the Button class's attributes.
2388 *
2389 * @param context The Context the view is running in, through which it can
2390 * access the current theme, resources, etc.
2391 * @param attrs The attributes of the XML tag that is inflating the view.
2392 * @param defStyle The default style to apply to this view. If 0, no style
2393 * will be applied (beyond what is included in the theme). This may
2394 * either be an attribute resource, whose value will be retrieved
2395 * from the current theme, or an explicit style resource.
2396 * @see #View(Context, AttributeSet)
2397 */
2398 public View(Context context, AttributeSet attrs, int defStyle) {
2399 this(context);
2400
2401 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2402 defStyle, 0);
2403
2404 Drawable background = null;
2405
2406 int leftPadding = -1;
2407 int topPadding = -1;
2408 int rightPadding = -1;
2409 int bottomPadding = -1;
2410
2411 int padding = -1;
2412
2413 int viewFlagValues = 0;
2414 int viewFlagMasks = 0;
2415
2416 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002417
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002418 int x = 0;
2419 int y = 0;
2420
Chet Haase73066682010-11-29 15:55:32 -08002421 float tx = 0;
2422 float ty = 0;
2423 float rotation = 0;
2424 float rotationX = 0;
2425 float rotationY = 0;
2426 float sx = 1f;
2427 float sy = 1f;
2428 boolean transformSet = false;
2429
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2431
Adam Powell637d3372010-08-25 14:37:03 -07002432 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002433 final int N = a.getIndexCount();
2434 for (int i = 0; i < N; i++) {
2435 int attr = a.getIndex(i);
2436 switch (attr) {
2437 case com.android.internal.R.styleable.View_background:
2438 background = a.getDrawable(attr);
2439 break;
2440 case com.android.internal.R.styleable.View_padding:
2441 padding = a.getDimensionPixelSize(attr, -1);
2442 break;
2443 case com.android.internal.R.styleable.View_paddingLeft:
2444 leftPadding = a.getDimensionPixelSize(attr, -1);
2445 break;
2446 case com.android.internal.R.styleable.View_paddingTop:
2447 topPadding = a.getDimensionPixelSize(attr, -1);
2448 break;
2449 case com.android.internal.R.styleable.View_paddingRight:
2450 rightPadding = a.getDimensionPixelSize(attr, -1);
2451 break;
2452 case com.android.internal.R.styleable.View_paddingBottom:
2453 bottomPadding = a.getDimensionPixelSize(attr, -1);
2454 break;
2455 case com.android.internal.R.styleable.View_scrollX:
2456 x = a.getDimensionPixelOffset(attr, 0);
2457 break;
2458 case com.android.internal.R.styleable.View_scrollY:
2459 y = a.getDimensionPixelOffset(attr, 0);
2460 break;
Chet Haase73066682010-11-29 15:55:32 -08002461 case com.android.internal.R.styleable.View_alpha:
2462 setAlpha(a.getFloat(attr, 1f));
2463 break;
2464 case com.android.internal.R.styleable.View_transformPivotX:
2465 setPivotX(a.getDimensionPixelOffset(attr, 0));
2466 break;
2467 case com.android.internal.R.styleable.View_transformPivotY:
2468 setPivotY(a.getDimensionPixelOffset(attr, 0));
2469 break;
2470 case com.android.internal.R.styleable.View_translationX:
2471 tx = a.getDimensionPixelOffset(attr, 0);
2472 transformSet = true;
2473 break;
2474 case com.android.internal.R.styleable.View_translationY:
2475 ty = a.getDimensionPixelOffset(attr, 0);
2476 transformSet = true;
2477 break;
2478 case com.android.internal.R.styleable.View_rotation:
2479 rotation = a.getFloat(attr, 0);
2480 transformSet = true;
2481 break;
2482 case com.android.internal.R.styleable.View_rotationX:
2483 rotationX = a.getFloat(attr, 0);
2484 transformSet = true;
2485 break;
2486 case com.android.internal.R.styleable.View_rotationY:
2487 rotationY = a.getFloat(attr, 0);
2488 transformSet = true;
2489 break;
2490 case com.android.internal.R.styleable.View_scaleX:
2491 sx = a.getFloat(attr, 1f);
2492 transformSet = true;
2493 break;
2494 case com.android.internal.R.styleable.View_scaleY:
2495 sy = a.getFloat(attr, 1f);
2496 transformSet = true;
2497 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002498 case com.android.internal.R.styleable.View_id:
2499 mID = a.getResourceId(attr, NO_ID);
2500 break;
2501 case com.android.internal.R.styleable.View_tag:
2502 mTag = a.getText(attr);
2503 break;
2504 case com.android.internal.R.styleable.View_fitsSystemWindows:
2505 if (a.getBoolean(attr, false)) {
2506 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2507 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2508 }
2509 break;
2510 case com.android.internal.R.styleable.View_focusable:
2511 if (a.getBoolean(attr, false)) {
2512 viewFlagValues |= FOCUSABLE;
2513 viewFlagMasks |= FOCUSABLE_MASK;
2514 }
2515 break;
2516 case com.android.internal.R.styleable.View_focusableInTouchMode:
2517 if (a.getBoolean(attr, false)) {
2518 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2519 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2520 }
2521 break;
2522 case com.android.internal.R.styleable.View_clickable:
2523 if (a.getBoolean(attr, false)) {
2524 viewFlagValues |= CLICKABLE;
2525 viewFlagMasks |= CLICKABLE;
2526 }
2527 break;
2528 case com.android.internal.R.styleable.View_longClickable:
2529 if (a.getBoolean(attr, false)) {
2530 viewFlagValues |= LONG_CLICKABLE;
2531 viewFlagMasks |= LONG_CLICKABLE;
2532 }
2533 break;
2534 case com.android.internal.R.styleable.View_saveEnabled:
2535 if (!a.getBoolean(attr, true)) {
2536 viewFlagValues |= SAVE_DISABLED;
2537 viewFlagMasks |= SAVE_DISABLED_MASK;
2538 }
2539 break;
2540 case com.android.internal.R.styleable.View_duplicateParentState:
2541 if (a.getBoolean(attr, false)) {
2542 viewFlagValues |= DUPLICATE_PARENT_STATE;
2543 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2544 }
2545 break;
2546 case com.android.internal.R.styleable.View_visibility:
2547 final int visibility = a.getInt(attr, 0);
2548 if (visibility != 0) {
2549 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2550 viewFlagMasks |= VISIBILITY_MASK;
2551 }
2552 break;
2553 case com.android.internal.R.styleable.View_drawingCacheQuality:
2554 final int cacheQuality = a.getInt(attr, 0);
2555 if (cacheQuality != 0) {
2556 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2557 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2558 }
2559 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002560 case com.android.internal.R.styleable.View_contentDescription:
2561 mContentDescription = a.getString(attr);
2562 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002563 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2564 if (!a.getBoolean(attr, true)) {
2565 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2566 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2567 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002568 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002569 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2570 if (!a.getBoolean(attr, true)) {
2571 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2572 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2573 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002574 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002575 case R.styleable.View_scrollbars:
2576 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2577 if (scrollbars != SCROLLBARS_NONE) {
2578 viewFlagValues |= scrollbars;
2579 viewFlagMasks |= SCROLLBARS_MASK;
2580 initializeScrollbars(a);
2581 }
2582 break;
2583 case R.styleable.View_fadingEdge:
2584 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2585 if (fadingEdge != FADING_EDGE_NONE) {
2586 viewFlagValues |= fadingEdge;
2587 viewFlagMasks |= FADING_EDGE_MASK;
2588 initializeFadingEdge(a);
2589 }
2590 break;
2591 case R.styleable.View_scrollbarStyle:
2592 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2593 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2594 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2595 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2596 }
2597 break;
2598 case R.styleable.View_isScrollContainer:
2599 setScrollContainer = true;
2600 if (a.getBoolean(attr, false)) {
2601 setScrollContainer(true);
2602 }
2603 break;
2604 case com.android.internal.R.styleable.View_keepScreenOn:
2605 if (a.getBoolean(attr, false)) {
2606 viewFlagValues |= KEEP_SCREEN_ON;
2607 viewFlagMasks |= KEEP_SCREEN_ON;
2608 }
2609 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002610 case R.styleable.View_filterTouchesWhenObscured:
2611 if (a.getBoolean(attr, false)) {
2612 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2613 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2614 }
2615 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 case R.styleable.View_nextFocusLeft:
2617 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2618 break;
2619 case R.styleable.View_nextFocusRight:
2620 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2621 break;
2622 case R.styleable.View_nextFocusUp:
2623 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2624 break;
2625 case R.styleable.View_nextFocusDown:
2626 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2627 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002628 case R.styleable.View_nextFocusForward:
2629 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2630 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002631 case R.styleable.View_minWidth:
2632 mMinWidth = a.getDimensionPixelSize(attr, 0);
2633 break;
2634 case R.styleable.View_minHeight:
2635 mMinHeight = a.getDimensionPixelSize(attr, 0);
2636 break;
Romain Guy9a817362009-05-01 10:57:14 -07002637 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002638 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002639 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002640 + "be used within a restricted context");
2641 }
2642
Romain Guy9a817362009-05-01 10:57:14 -07002643 final String handlerName = a.getString(attr);
2644 if (handlerName != null) {
2645 setOnClickListener(new OnClickListener() {
2646 private Method mHandler;
2647
2648 public void onClick(View v) {
2649 if (mHandler == null) {
2650 try {
2651 mHandler = getContext().getClass().getMethod(handlerName,
2652 View.class);
2653 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002654 int id = getId();
2655 String idText = id == NO_ID ? "" : " with id '"
2656 + getContext().getResources().getResourceEntryName(
2657 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002658 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002659 handlerName + "(View) in the activity "
2660 + getContext().getClass() + " for onClick handler"
2661 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002662 }
2663 }
2664
2665 try {
2666 mHandler.invoke(getContext(), View.this);
2667 } catch (IllegalAccessException e) {
2668 throw new IllegalStateException("Could not execute non "
2669 + "public method of the activity", e);
2670 } catch (InvocationTargetException e) {
2671 throw new IllegalStateException("Could not execute "
2672 + "method of the activity", e);
2673 }
2674 }
2675 });
2676 }
2677 break;
Adam Powell637d3372010-08-25 14:37:03 -07002678 case R.styleable.View_overScrollMode:
2679 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2680 break;
Adam Powell20232d02010-12-08 21:08:53 -08002681 case R.styleable.View_verticalScrollbarPosition:
2682 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2683 break;
Romain Guy171c5922011-01-06 10:04:23 -08002684 case R.styleable.View_layerType:
2685 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2686 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 }
2688 }
2689
Adam Powell637d3372010-08-25 14:37:03 -07002690 setOverScrollMode(overScrollMode);
2691
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002692 if (background != null) {
2693 setBackgroundDrawable(background);
2694 }
2695
2696 if (padding >= 0) {
2697 leftPadding = padding;
2698 topPadding = padding;
2699 rightPadding = padding;
2700 bottomPadding = padding;
2701 }
2702
2703 // If the user specified the padding (either with android:padding or
2704 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2705 // use the default padding or the padding from the background drawable
2706 // (stored at this point in mPadding*)
2707 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2708 topPadding >= 0 ? topPadding : mPaddingTop,
2709 rightPadding >= 0 ? rightPadding : mPaddingRight,
2710 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2711
2712 if (viewFlagMasks != 0) {
2713 setFlags(viewFlagValues, viewFlagMasks);
2714 }
2715
2716 // Needs to be called after mViewFlags is set
2717 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2718 recomputePadding();
2719 }
2720
2721 if (x != 0 || y != 0) {
2722 scrollTo(x, y);
2723 }
2724
Chet Haase73066682010-11-29 15:55:32 -08002725 if (transformSet) {
2726 setTranslationX(tx);
2727 setTranslationY(ty);
2728 setRotation(rotation);
2729 setRotationX(rotationX);
2730 setRotationY(rotationY);
2731 setScaleX(sx);
2732 setScaleY(sy);
2733 }
2734
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2736 setScrollContainer(true);
2737 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002738
2739 computeOpaqueFlags();
2740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 a.recycle();
2742 }
2743
2744 /**
2745 * Non-public constructor for use in testing
2746 */
2747 View() {
2748 }
2749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002750 /**
2751 * <p>
2752 * Initializes the fading edges from a given set of styled attributes. This
2753 * method should be called by subclasses that need fading edges and when an
2754 * instance of these subclasses is created programmatically rather than
2755 * being inflated from XML. This method is automatically called when the XML
2756 * is inflated.
2757 * </p>
2758 *
2759 * @param a the styled attributes set to initialize the fading edges from
2760 */
2761 protected void initializeFadingEdge(TypedArray a) {
2762 initScrollCache();
2763
2764 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2765 R.styleable.View_fadingEdgeLength,
2766 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2767 }
2768
2769 /**
2770 * Returns the size of the vertical faded edges used to indicate that more
2771 * content in this view is visible.
2772 *
2773 * @return The size in pixels of the vertical faded edge or 0 if vertical
2774 * faded edges are not enabled for this view.
2775 * @attr ref android.R.styleable#View_fadingEdgeLength
2776 */
2777 public int getVerticalFadingEdgeLength() {
2778 if (isVerticalFadingEdgeEnabled()) {
2779 ScrollabilityCache cache = mScrollCache;
2780 if (cache != null) {
2781 return cache.fadingEdgeLength;
2782 }
2783 }
2784 return 0;
2785 }
2786
2787 /**
2788 * Set the size of the faded edge used to indicate that more content in this
2789 * view is available. Will not change whether the fading edge is enabled; use
2790 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2791 * to enable the fading edge for the vertical or horizontal fading edges.
2792 *
2793 * @param length The size in pixels of the faded edge used to indicate that more
2794 * content in this view is visible.
2795 */
2796 public void setFadingEdgeLength(int length) {
2797 initScrollCache();
2798 mScrollCache.fadingEdgeLength = length;
2799 }
2800
2801 /**
2802 * Returns the size of the horizontal faded edges used to indicate that more
2803 * content in this view is visible.
2804 *
2805 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2806 * faded edges are not enabled for this view.
2807 * @attr ref android.R.styleable#View_fadingEdgeLength
2808 */
2809 public int getHorizontalFadingEdgeLength() {
2810 if (isHorizontalFadingEdgeEnabled()) {
2811 ScrollabilityCache cache = mScrollCache;
2812 if (cache != null) {
2813 return cache.fadingEdgeLength;
2814 }
2815 }
2816 return 0;
2817 }
2818
2819 /**
2820 * Returns the width of the vertical scrollbar.
2821 *
2822 * @return The width in pixels of the vertical scrollbar or 0 if there
2823 * is no vertical scrollbar.
2824 */
2825 public int getVerticalScrollbarWidth() {
2826 ScrollabilityCache cache = mScrollCache;
2827 if (cache != null) {
2828 ScrollBarDrawable scrollBar = cache.scrollBar;
2829 if (scrollBar != null) {
2830 int size = scrollBar.getSize(true);
2831 if (size <= 0) {
2832 size = cache.scrollBarSize;
2833 }
2834 return size;
2835 }
2836 return 0;
2837 }
2838 return 0;
2839 }
2840
2841 /**
2842 * Returns the height of the horizontal scrollbar.
2843 *
2844 * @return The height in pixels of the horizontal scrollbar or 0 if
2845 * there is no horizontal scrollbar.
2846 */
2847 protected int getHorizontalScrollbarHeight() {
2848 ScrollabilityCache cache = mScrollCache;
2849 if (cache != null) {
2850 ScrollBarDrawable scrollBar = cache.scrollBar;
2851 if (scrollBar != null) {
2852 int size = scrollBar.getSize(false);
2853 if (size <= 0) {
2854 size = cache.scrollBarSize;
2855 }
2856 return size;
2857 }
2858 return 0;
2859 }
2860 return 0;
2861 }
2862
2863 /**
2864 * <p>
2865 * Initializes the scrollbars from a given set of styled attributes. This
2866 * method should be called by subclasses that need scrollbars and when an
2867 * instance of these subclasses is created programmatically rather than
2868 * being inflated from XML. This method is automatically called when the XML
2869 * is inflated.
2870 * </p>
2871 *
2872 * @param a the styled attributes set to initialize the scrollbars from
2873 */
2874 protected void initializeScrollbars(TypedArray a) {
2875 initScrollCache();
2876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002878
Mike Cleronf116bf82009-09-27 19:14:12 -07002879 if (scrollabilityCache.scrollBar == null) {
2880 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2881 }
Joe Malin32736f02011-01-19 16:14:20 -08002882
Romain Guy8bda2482010-03-02 11:42:11 -08002883 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884
Mike Cleronf116bf82009-09-27 19:14:12 -07002885 if (!fadeScrollbars) {
2886 scrollabilityCache.state = ScrollabilityCache.ON;
2887 }
2888 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002889
2890
Mike Cleronf116bf82009-09-27 19:14:12 -07002891 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2892 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2893 .getScrollBarFadeDuration());
2894 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2895 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2896 ViewConfiguration.getScrollDefaultDelay());
2897
Joe Malin32736f02011-01-19 16:14:20 -08002898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002899 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2900 com.android.internal.R.styleable.View_scrollbarSize,
2901 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2902
2903 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2904 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2905
2906 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2907 if (thumb != null) {
2908 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2909 }
2910
2911 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2912 false);
2913 if (alwaysDraw) {
2914 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2915 }
2916
2917 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2918 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2919
2920 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2921 if (thumb != null) {
2922 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2923 }
2924
2925 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2926 false);
2927 if (alwaysDraw) {
2928 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2929 }
2930
2931 // Re-apply user/background padding so that scrollbar(s) get added
2932 recomputePadding();
2933 }
2934
2935 /**
2936 * <p>
2937 * Initalizes the scrollability cache if necessary.
2938 * </p>
2939 */
2940 private void initScrollCache() {
2941 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002942 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002943 }
2944 }
2945
2946 /**
Adam Powell20232d02010-12-08 21:08:53 -08002947 * Set the position of the vertical scroll bar. Should be one of
2948 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2949 * {@link #SCROLLBAR_POSITION_RIGHT}.
2950 *
2951 * @param position Where the vertical scroll bar should be positioned.
2952 */
2953 public void setVerticalScrollbarPosition(int position) {
2954 if (mVerticalScrollbarPosition != position) {
2955 mVerticalScrollbarPosition = position;
2956 computeOpaqueFlags();
2957 recomputePadding();
2958 }
2959 }
2960
2961 /**
2962 * @return The position where the vertical scroll bar will show, if applicable.
2963 * @see #setVerticalScrollbarPosition(int)
2964 */
2965 public int getVerticalScrollbarPosition() {
2966 return mVerticalScrollbarPosition;
2967 }
2968
2969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002970 * Register a callback to be invoked when focus of this view changed.
2971 *
2972 * @param l The callback that will run.
2973 */
2974 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2975 mOnFocusChangeListener = l;
2976 }
2977
2978 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002979 * Add a listener that will be called when the bounds of the view change due to
2980 * layout processing.
2981 *
2982 * @param listener The listener that will be called when layout bounds change.
2983 */
2984 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2985 if (mOnLayoutChangeListeners == null) {
2986 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2987 }
2988 mOnLayoutChangeListeners.add(listener);
2989 }
2990
2991 /**
2992 * Remove a listener for layout changes.
2993 *
2994 * @param listener The listener for layout bounds change.
2995 */
2996 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
2997 if (mOnLayoutChangeListeners == null) {
2998 return;
2999 }
3000 mOnLayoutChangeListeners.remove(listener);
3001 }
3002
3003 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003004 * Add a listener for attach state changes.
3005 *
3006 * This listener will be called whenever this view is attached or detached
3007 * from a window. Remove the listener using
3008 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3009 *
3010 * @param listener Listener to attach
3011 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3012 */
3013 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3014 if (mOnAttachStateChangeListeners == null) {
3015 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3016 }
3017 mOnAttachStateChangeListeners.add(listener);
3018 }
3019
3020 /**
3021 * Remove a listener for attach state changes. The listener will receive no further
3022 * notification of window attach/detach events.
3023 *
3024 * @param listener Listener to remove
3025 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3026 */
3027 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3028 if (mOnAttachStateChangeListeners == null) {
3029 return;
3030 }
3031 mOnAttachStateChangeListeners.remove(listener);
3032 }
3033
3034 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 * Returns the focus-change callback registered for this view.
3036 *
3037 * @return The callback, or null if one is not registered.
3038 */
3039 public OnFocusChangeListener getOnFocusChangeListener() {
3040 return mOnFocusChangeListener;
3041 }
3042
3043 /**
3044 * Register a callback to be invoked when this view is clicked. If this view is not
3045 * clickable, it becomes clickable.
3046 *
3047 * @param l The callback that will run
3048 *
3049 * @see #setClickable(boolean)
3050 */
3051 public void setOnClickListener(OnClickListener l) {
3052 if (!isClickable()) {
3053 setClickable(true);
3054 }
3055 mOnClickListener = l;
3056 }
3057
3058 /**
3059 * Register a callback to be invoked when this view is clicked and held. If this view is not
3060 * long clickable, it becomes long clickable.
3061 *
3062 * @param l The callback that will run
3063 *
3064 * @see #setLongClickable(boolean)
3065 */
3066 public void setOnLongClickListener(OnLongClickListener l) {
3067 if (!isLongClickable()) {
3068 setLongClickable(true);
3069 }
3070 mOnLongClickListener = l;
3071 }
3072
3073 /**
3074 * Register a callback to be invoked when the context menu for this view is
3075 * being built. If this view is not long clickable, it becomes long clickable.
3076 *
3077 * @param l The callback that will run
3078 *
3079 */
3080 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3081 if (!isLongClickable()) {
3082 setLongClickable(true);
3083 }
3084 mOnCreateContextMenuListener = l;
3085 }
3086
3087 /**
3088 * Call this view's OnClickListener, if it is defined.
3089 *
3090 * @return True there was an assigned OnClickListener that was called, false
3091 * otherwise is returned.
3092 */
3093 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003094 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003096 if (mOnClickListener != null) {
3097 playSoundEffect(SoundEffectConstants.CLICK);
3098 mOnClickListener.onClick(this);
3099 return true;
3100 }
3101
3102 return false;
3103 }
3104
3105 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003106 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3107 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003108 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003109 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003110 */
3111 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003112 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003114 boolean handled = false;
3115 if (mOnLongClickListener != null) {
3116 handled = mOnLongClickListener.onLongClick(View.this);
3117 }
3118 if (!handled) {
3119 handled = showContextMenu();
3120 }
3121 if (handled) {
3122 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3123 }
3124 return handled;
3125 }
3126
3127 /**
3128 * Bring up the context menu for this view.
3129 *
3130 * @return Whether a context menu was displayed.
3131 */
3132 public boolean showContextMenu() {
3133 return getParent().showContextMenuForChild(this);
3134 }
3135
3136 /**
Adam Powell6e346362010-07-23 10:18:23 -07003137 * Start an action mode.
3138 *
3139 * @param callback Callback that will control the lifecycle of the action mode
3140 * @return The new action mode if it is started, null otherwise
3141 *
3142 * @see ActionMode
3143 */
3144 public ActionMode startActionMode(ActionMode.Callback callback) {
3145 return getParent().startActionModeForChild(this, callback);
3146 }
3147
3148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003149 * Register a callback to be invoked when a key is pressed in this view.
3150 * @param l the key listener to attach to this view
3151 */
3152 public void setOnKeyListener(OnKeyListener l) {
3153 mOnKeyListener = l;
3154 }
3155
3156 /**
3157 * Register a callback to be invoked when a touch event is sent to this view.
3158 * @param l the touch listener to attach to this view
3159 */
3160 public void setOnTouchListener(OnTouchListener l) {
3161 mOnTouchListener = l;
3162 }
3163
3164 /**
Joe Malin32736f02011-01-19 16:14:20 -08003165 * Register a drag event listener callback object for this View. The parameter is
3166 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3167 * View, the system calls the
3168 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3169 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003170 */
3171 public void setOnDragListener(OnDragListener l) {
3172 mOnDragListener = l;
3173 }
3174
3175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003176 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3177 *
3178 * Note: this does not check whether this {@link View} should get focus, it just
3179 * gives it focus no matter what. It should only be called internally by framework
3180 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3181 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003182 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3183 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003184 * focus moved when requestFocus() is called. It may not always
3185 * apply, in which case use the default View.FOCUS_DOWN.
3186 * @param previouslyFocusedRect The rectangle of the view that had focus
3187 * prior in this View's coordinate system.
3188 */
3189 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3190 if (DBG) {
3191 System.out.println(this + " requestFocus()");
3192 }
3193
3194 if ((mPrivateFlags & FOCUSED) == 0) {
3195 mPrivateFlags |= FOCUSED;
3196
3197 if (mParent != null) {
3198 mParent.requestChildFocus(this, this);
3199 }
3200
3201 onFocusChanged(true, direction, previouslyFocusedRect);
3202 refreshDrawableState();
3203 }
3204 }
3205
3206 /**
3207 * Request that a rectangle of this view be visible on the screen,
3208 * scrolling if necessary just enough.
3209 *
3210 * <p>A View should call this if it maintains some notion of which part
3211 * of its content is interesting. For example, a text editing view
3212 * should call this when its cursor moves.
3213 *
3214 * @param rectangle The rectangle.
3215 * @return Whether any parent scrolled.
3216 */
3217 public boolean requestRectangleOnScreen(Rect rectangle) {
3218 return requestRectangleOnScreen(rectangle, false);
3219 }
3220
3221 /**
3222 * Request that a rectangle of this view be visible on the screen,
3223 * scrolling if necessary just enough.
3224 *
3225 * <p>A View should call this if it maintains some notion of which part
3226 * of its content is interesting. For example, a text editing view
3227 * should call this when its cursor moves.
3228 *
3229 * <p>When <code>immediate</code> is set to true, scrolling will not be
3230 * animated.
3231 *
3232 * @param rectangle The rectangle.
3233 * @param immediate True to forbid animated scrolling, false otherwise
3234 * @return Whether any parent scrolled.
3235 */
3236 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3237 View child = this;
3238 ViewParent parent = mParent;
3239 boolean scrolled = false;
3240 while (parent != null) {
3241 scrolled |= parent.requestChildRectangleOnScreen(child,
3242 rectangle, immediate);
3243
3244 // offset rect so next call has the rectangle in the
3245 // coordinate system of its direct child.
3246 rectangle.offset(child.getLeft(), child.getTop());
3247 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3248
3249 if (!(parent instanceof View)) {
3250 break;
3251 }
Romain Guy8506ab42009-06-11 17:35:47 -07003252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 child = (View) parent;
3254 parent = child.getParent();
3255 }
3256 return scrolled;
3257 }
3258
3259 /**
3260 * Called when this view wants to give up focus. This will cause
3261 * {@link #onFocusChanged} to be called.
3262 */
3263 public void clearFocus() {
3264 if (DBG) {
3265 System.out.println(this + " clearFocus()");
3266 }
3267
3268 if ((mPrivateFlags & FOCUSED) != 0) {
3269 mPrivateFlags &= ~FOCUSED;
3270
3271 if (mParent != null) {
3272 mParent.clearChildFocus(this);
3273 }
3274
3275 onFocusChanged(false, 0, null);
3276 refreshDrawableState();
3277 }
3278 }
3279
3280 /**
3281 * Called to clear the focus of a view that is about to be removed.
3282 * Doesn't call clearChildFocus, which prevents this view from taking
3283 * focus again before it has been removed from the parent
3284 */
3285 void clearFocusForRemoval() {
3286 if ((mPrivateFlags & FOCUSED) != 0) {
3287 mPrivateFlags &= ~FOCUSED;
3288
3289 onFocusChanged(false, 0, null);
3290 refreshDrawableState();
3291 }
3292 }
3293
3294 /**
3295 * Called internally by the view system when a new view is getting focus.
3296 * This is what clears the old focus.
3297 */
3298 void unFocus() {
3299 if (DBG) {
3300 System.out.println(this + " unFocus()");
3301 }
3302
3303 if ((mPrivateFlags & FOCUSED) != 0) {
3304 mPrivateFlags &= ~FOCUSED;
3305
3306 onFocusChanged(false, 0, null);
3307 refreshDrawableState();
3308 }
3309 }
3310
3311 /**
3312 * Returns true if this view has focus iteself, or is the ancestor of the
3313 * view that has focus.
3314 *
3315 * @return True if this view has or contains focus, false otherwise.
3316 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003317 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003318 public boolean hasFocus() {
3319 return (mPrivateFlags & FOCUSED) != 0;
3320 }
3321
3322 /**
3323 * Returns true if this view is focusable or if it contains a reachable View
3324 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3325 * is a View whose parents do not block descendants focus.
3326 *
3327 * Only {@link #VISIBLE} views are considered focusable.
3328 *
3329 * @return True if the view is focusable or if the view contains a focusable
3330 * View, false otherwise.
3331 *
3332 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3333 */
3334 public boolean hasFocusable() {
3335 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3336 }
3337
3338 /**
3339 * Called by the view system when the focus state of this view changes.
3340 * When the focus change event is caused by directional navigation, direction
3341 * and previouslyFocusedRect provide insight into where the focus is coming from.
3342 * When overriding, be sure to call up through to the super class so that
3343 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003344 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003345 * @param gainFocus True if the View has focus; false otherwise.
3346 * @param direction The direction focus has moved when requestFocus()
3347 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003348 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3349 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3350 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003351 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3352 * system, of the previously focused view. If applicable, this will be
3353 * passed in as finer grained information about where the focus is coming
3354 * from (in addition to direction). Will be <code>null</code> otherwise.
3355 */
3356 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003357 if (gainFocus) {
3358 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3359 }
3360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003361 InputMethodManager imm = InputMethodManager.peekInstance();
3362 if (!gainFocus) {
3363 if (isPressed()) {
3364 setPressed(false);
3365 }
3366 if (imm != null && mAttachInfo != null
3367 && mAttachInfo.mHasWindowFocus) {
3368 imm.focusOut(this);
3369 }
Romain Guya2431d02009-04-30 16:30:00 -07003370 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003371 } else if (imm != null && mAttachInfo != null
3372 && mAttachInfo.mHasWindowFocus) {
3373 imm.focusIn(this);
3374 }
Romain Guy8506ab42009-06-11 17:35:47 -07003375
Romain Guy0fd89bf2011-01-26 15:41:30 -08003376 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003377 if (mOnFocusChangeListener != null) {
3378 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3379 }
Joe Malin32736f02011-01-19 16:14:20 -08003380
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003381 if (mAttachInfo != null) {
3382 mAttachInfo.mKeyDispatchState.reset(this);
3383 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003384 }
3385
3386 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003387 * {@inheritDoc}
3388 */
3389 public void sendAccessibilityEvent(int eventType) {
3390 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3391 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3392 }
3393 }
3394
3395 /**
3396 * {@inheritDoc}
3397 */
3398 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003399 if (!isShown()) {
3400 return;
3401 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003402 event.setClassName(getClass().getName());
3403 event.setPackageName(getContext().getPackageName());
3404 event.setEnabled(isEnabled());
3405 event.setContentDescription(mContentDescription);
3406
3407 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3408 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3409 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3410 event.setItemCount(focusablesTempList.size());
3411 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3412 focusablesTempList.clear();
3413 }
3414
3415 dispatchPopulateAccessibilityEvent(event);
3416
3417 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3418 }
3419
3420 /**
3421 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3422 * to be populated.
3423 *
3424 * @param event The event.
3425 *
3426 * @return True if the event population was completed.
3427 */
3428 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3429 return false;
3430 }
3431
3432 /**
3433 * Gets the {@link View} description. It briefly describes the view and is
3434 * primarily used for accessibility support. Set this property to enable
3435 * better accessibility support for your application. This is especially
3436 * true for views that do not have textual representation (For example,
3437 * ImageButton).
3438 *
3439 * @return The content descriptiopn.
3440 *
3441 * @attr ref android.R.styleable#View_contentDescription
3442 */
3443 public CharSequence getContentDescription() {
3444 return mContentDescription;
3445 }
3446
3447 /**
3448 * Sets the {@link View} description. It briefly describes the view and is
3449 * primarily used for accessibility support. Set this property to enable
3450 * better accessibility support for your application. This is especially
3451 * true for views that do not have textual representation (For example,
3452 * ImageButton).
3453 *
3454 * @param contentDescription The content description.
3455 *
3456 * @attr ref android.R.styleable#View_contentDescription
3457 */
3458 public void setContentDescription(CharSequence contentDescription) {
3459 mContentDescription = contentDescription;
3460 }
3461
3462 /**
Romain Guya2431d02009-04-30 16:30:00 -07003463 * Invoked whenever this view loses focus, either by losing window focus or by losing
3464 * focus within its window. This method can be used to clear any state tied to the
3465 * focus. For instance, if a button is held pressed with the trackball and the window
3466 * loses focus, this method can be used to cancel the press.
3467 *
3468 * Subclasses of View overriding this method should always call super.onFocusLost().
3469 *
3470 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003471 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003472 *
3473 * @hide pending API council approval
3474 */
3475 protected void onFocusLost() {
3476 resetPressedState();
3477 }
3478
3479 private void resetPressedState() {
3480 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3481 return;
3482 }
3483
3484 if (isPressed()) {
3485 setPressed(false);
3486
3487 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003488 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003489 }
3490 }
3491 }
3492
3493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003494 * Returns true if this view has focus
3495 *
3496 * @return True if this view has focus, false otherwise.
3497 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003498 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003499 public boolean isFocused() {
3500 return (mPrivateFlags & FOCUSED) != 0;
3501 }
3502
3503 /**
3504 * Find the view in the hierarchy rooted at this view that currently has
3505 * focus.
3506 *
3507 * @return The view that currently has focus, or null if no focused view can
3508 * be found.
3509 */
3510 public View findFocus() {
3511 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3512 }
3513
3514 /**
3515 * Change whether this view is one of the set of scrollable containers in
3516 * its window. This will be used to determine whether the window can
3517 * resize or must pan when a soft input area is open -- scrollable
3518 * containers allow the window to use resize mode since the container
3519 * will appropriately shrink.
3520 */
3521 public void setScrollContainer(boolean isScrollContainer) {
3522 if (isScrollContainer) {
3523 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3524 mAttachInfo.mScrollContainers.add(this);
3525 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3526 }
3527 mPrivateFlags |= SCROLL_CONTAINER;
3528 } else {
3529 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3530 mAttachInfo.mScrollContainers.remove(this);
3531 }
3532 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3533 }
3534 }
3535
3536 /**
3537 * Returns the quality of the drawing cache.
3538 *
3539 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3540 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3541 *
3542 * @see #setDrawingCacheQuality(int)
3543 * @see #setDrawingCacheEnabled(boolean)
3544 * @see #isDrawingCacheEnabled()
3545 *
3546 * @attr ref android.R.styleable#View_drawingCacheQuality
3547 */
3548 public int getDrawingCacheQuality() {
3549 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3550 }
3551
3552 /**
3553 * Set the drawing cache quality of this view. This value is used only when the
3554 * drawing cache is enabled
3555 *
3556 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3557 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3558 *
3559 * @see #getDrawingCacheQuality()
3560 * @see #setDrawingCacheEnabled(boolean)
3561 * @see #isDrawingCacheEnabled()
3562 *
3563 * @attr ref android.R.styleable#View_drawingCacheQuality
3564 */
3565 public void setDrawingCacheQuality(int quality) {
3566 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3567 }
3568
3569 /**
3570 * Returns whether the screen should remain on, corresponding to the current
3571 * value of {@link #KEEP_SCREEN_ON}.
3572 *
3573 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3574 *
3575 * @see #setKeepScreenOn(boolean)
3576 *
3577 * @attr ref android.R.styleable#View_keepScreenOn
3578 */
3579 public boolean getKeepScreenOn() {
3580 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3581 }
3582
3583 /**
3584 * Controls whether the screen should remain on, modifying the
3585 * value of {@link #KEEP_SCREEN_ON}.
3586 *
3587 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3588 *
3589 * @see #getKeepScreenOn()
3590 *
3591 * @attr ref android.R.styleable#View_keepScreenOn
3592 */
3593 public void setKeepScreenOn(boolean keepScreenOn) {
3594 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3595 }
3596
3597 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003598 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3599 * @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 -08003600 *
3601 * @attr ref android.R.styleable#View_nextFocusLeft
3602 */
3603 public int getNextFocusLeftId() {
3604 return mNextFocusLeftId;
3605 }
3606
3607 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003608 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3609 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3610 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003611 *
3612 * @attr ref android.R.styleable#View_nextFocusLeft
3613 */
3614 public void setNextFocusLeftId(int nextFocusLeftId) {
3615 mNextFocusLeftId = nextFocusLeftId;
3616 }
3617
3618 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003619 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3620 * @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 -08003621 *
3622 * @attr ref android.R.styleable#View_nextFocusRight
3623 */
3624 public int getNextFocusRightId() {
3625 return mNextFocusRightId;
3626 }
3627
3628 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003629 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3630 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3631 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003632 *
3633 * @attr ref android.R.styleable#View_nextFocusRight
3634 */
3635 public void setNextFocusRightId(int nextFocusRightId) {
3636 mNextFocusRightId = nextFocusRightId;
3637 }
3638
3639 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003640 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3641 * @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 -08003642 *
3643 * @attr ref android.R.styleable#View_nextFocusUp
3644 */
3645 public int getNextFocusUpId() {
3646 return mNextFocusUpId;
3647 }
3648
3649 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003650 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3651 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3652 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003653 *
3654 * @attr ref android.R.styleable#View_nextFocusUp
3655 */
3656 public void setNextFocusUpId(int nextFocusUpId) {
3657 mNextFocusUpId = nextFocusUpId;
3658 }
3659
3660 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003661 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3662 * @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 -08003663 *
3664 * @attr ref android.R.styleable#View_nextFocusDown
3665 */
3666 public int getNextFocusDownId() {
3667 return mNextFocusDownId;
3668 }
3669
3670 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003671 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3672 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3673 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003674 *
3675 * @attr ref android.R.styleable#View_nextFocusDown
3676 */
3677 public void setNextFocusDownId(int nextFocusDownId) {
3678 mNextFocusDownId = nextFocusDownId;
3679 }
3680
3681 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003682 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3683 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3684 *
3685 * @attr ref android.R.styleable#View_nextFocusForward
3686 */
3687 public int getNextFocusForwardId() {
3688 return mNextFocusForwardId;
3689 }
3690
3691 /**
3692 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3693 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3694 * decide automatically.
3695 *
3696 * @attr ref android.R.styleable#View_nextFocusForward
3697 */
3698 public void setNextFocusForwardId(int nextFocusForwardId) {
3699 mNextFocusForwardId = nextFocusForwardId;
3700 }
3701
3702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 * Returns the visibility of this view and all of its ancestors
3704 *
3705 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3706 */
3707 public boolean isShown() {
3708 View current = this;
3709 //noinspection ConstantConditions
3710 do {
3711 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3712 return false;
3713 }
3714 ViewParent parent = current.mParent;
3715 if (parent == null) {
3716 return false; // We are not attached to the view root
3717 }
3718 if (!(parent instanceof View)) {
3719 return true;
3720 }
3721 current = (View) parent;
3722 } while (current != null);
3723
3724 return false;
3725 }
3726
3727 /**
3728 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3729 * is set
3730 *
3731 * @param insets Insets for system windows
3732 *
3733 * @return True if this view applied the insets, false otherwise
3734 */
3735 protected boolean fitSystemWindows(Rect insets) {
3736 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3737 mPaddingLeft = insets.left;
3738 mPaddingTop = insets.top;
3739 mPaddingRight = insets.right;
3740 mPaddingBottom = insets.bottom;
3741 requestLayout();
3742 return true;
3743 }
3744 return false;
3745 }
3746
3747 /**
3748 * Returns the visibility status for this view.
3749 *
3750 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3751 * @attr ref android.R.styleable#View_visibility
3752 */
3753 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003754 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3755 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3756 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 })
3758 public int getVisibility() {
3759 return mViewFlags & VISIBILITY_MASK;
3760 }
3761
3762 /**
3763 * Set the enabled state of this view.
3764 *
3765 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3766 * @attr ref android.R.styleable#View_visibility
3767 */
3768 @RemotableViewMethod
3769 public void setVisibility(int visibility) {
3770 setFlags(visibility, VISIBILITY_MASK);
3771 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3772 }
3773
3774 /**
3775 * Returns the enabled status for this view. The interpretation of the
3776 * enabled state varies by subclass.
3777 *
3778 * @return True if this view is enabled, false otherwise.
3779 */
3780 @ViewDebug.ExportedProperty
3781 public boolean isEnabled() {
3782 return (mViewFlags & ENABLED_MASK) == ENABLED;
3783 }
3784
3785 /**
3786 * Set the enabled state of this view. The interpretation of the enabled
3787 * state varies by subclass.
3788 *
3789 * @param enabled True if this view is enabled, false otherwise.
3790 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003791 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003792 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003793 if (enabled == isEnabled()) return;
3794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003795 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3796
3797 /*
3798 * The View most likely has to change its appearance, so refresh
3799 * the drawable state.
3800 */
3801 refreshDrawableState();
3802
3803 // Invalidate too, since the default behavior for views is to be
3804 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003805 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003806 }
3807
3808 /**
3809 * Set whether this view can receive the focus.
3810 *
3811 * Setting this to false will also ensure that this view is not focusable
3812 * in touch mode.
3813 *
3814 * @param focusable If true, this view can receive the focus.
3815 *
3816 * @see #setFocusableInTouchMode(boolean)
3817 * @attr ref android.R.styleable#View_focusable
3818 */
3819 public void setFocusable(boolean focusable) {
3820 if (!focusable) {
3821 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3822 }
3823 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3824 }
3825
3826 /**
3827 * Set whether this view can receive focus while in touch mode.
3828 *
3829 * Setting this to true will also ensure that this view is focusable.
3830 *
3831 * @param focusableInTouchMode If true, this view can receive the focus while
3832 * in touch mode.
3833 *
3834 * @see #setFocusable(boolean)
3835 * @attr ref android.R.styleable#View_focusableInTouchMode
3836 */
3837 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3838 // Focusable in touch mode should always be set before the focusable flag
3839 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3840 // which, in touch mode, will not successfully request focus on this view
3841 // because the focusable in touch mode flag is not set
3842 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3843 if (focusableInTouchMode) {
3844 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3845 }
3846 }
3847
3848 /**
3849 * Set whether this view should have sound effects enabled for events such as
3850 * clicking and touching.
3851 *
3852 * <p>You may wish to disable sound effects for a view if you already play sounds,
3853 * for instance, a dial key that plays dtmf tones.
3854 *
3855 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3856 * @see #isSoundEffectsEnabled()
3857 * @see #playSoundEffect(int)
3858 * @attr ref android.R.styleable#View_soundEffectsEnabled
3859 */
3860 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3861 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3862 }
3863
3864 /**
3865 * @return whether this view should have sound effects enabled for events such as
3866 * clicking and touching.
3867 *
3868 * @see #setSoundEffectsEnabled(boolean)
3869 * @see #playSoundEffect(int)
3870 * @attr ref android.R.styleable#View_soundEffectsEnabled
3871 */
3872 @ViewDebug.ExportedProperty
3873 public boolean isSoundEffectsEnabled() {
3874 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3875 }
3876
3877 /**
3878 * Set whether this view should have haptic feedback for events such as
3879 * long presses.
3880 *
3881 * <p>You may wish to disable haptic feedback if your view already controls
3882 * its own haptic feedback.
3883 *
3884 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3885 * @see #isHapticFeedbackEnabled()
3886 * @see #performHapticFeedback(int)
3887 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3888 */
3889 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3890 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3891 }
3892
3893 /**
3894 * @return whether this view should have haptic feedback enabled for events
3895 * long presses.
3896 *
3897 * @see #setHapticFeedbackEnabled(boolean)
3898 * @see #performHapticFeedback(int)
3899 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3900 */
3901 @ViewDebug.ExportedProperty
3902 public boolean isHapticFeedbackEnabled() {
3903 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3904 }
3905
3906 /**
3907 * If this view doesn't do any drawing on its own, set this flag to
3908 * allow further optimizations. By default, this flag is not set on
3909 * View, but could be set on some View subclasses such as ViewGroup.
3910 *
3911 * Typically, if you override {@link #onDraw} you should clear this flag.
3912 *
3913 * @param willNotDraw whether or not this View draw on its own
3914 */
3915 public void setWillNotDraw(boolean willNotDraw) {
3916 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3917 }
3918
3919 /**
3920 * Returns whether or not this View draws on its own.
3921 *
3922 * @return true if this view has nothing to draw, false otherwise
3923 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003924 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003925 public boolean willNotDraw() {
3926 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3927 }
3928
3929 /**
3930 * When a View's drawing cache is enabled, drawing is redirected to an
3931 * offscreen bitmap. Some views, like an ImageView, must be able to
3932 * bypass this mechanism if they already draw a single bitmap, to avoid
3933 * unnecessary usage of the memory.
3934 *
3935 * @param willNotCacheDrawing true if this view does not cache its
3936 * drawing, false otherwise
3937 */
3938 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3939 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3940 }
3941
3942 /**
3943 * Returns whether or not this View can cache its drawing or not.
3944 *
3945 * @return true if this view does not cache its drawing, false otherwise
3946 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003947 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003948 public boolean willNotCacheDrawing() {
3949 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3950 }
3951
3952 /**
3953 * Indicates whether this view reacts to click events or not.
3954 *
3955 * @return true if the view is clickable, false otherwise
3956 *
3957 * @see #setClickable(boolean)
3958 * @attr ref android.R.styleable#View_clickable
3959 */
3960 @ViewDebug.ExportedProperty
3961 public boolean isClickable() {
3962 return (mViewFlags & CLICKABLE) == CLICKABLE;
3963 }
3964
3965 /**
3966 * Enables or disables click events for this view. When a view
3967 * is clickable it will change its state to "pressed" on every click.
3968 * Subclasses should set the view clickable to visually react to
3969 * user's clicks.
3970 *
3971 * @param clickable true to make the view clickable, false otherwise
3972 *
3973 * @see #isClickable()
3974 * @attr ref android.R.styleable#View_clickable
3975 */
3976 public void setClickable(boolean clickable) {
3977 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3978 }
3979
3980 /**
3981 * Indicates whether this view reacts to long click events or not.
3982 *
3983 * @return true if the view is long clickable, false otherwise
3984 *
3985 * @see #setLongClickable(boolean)
3986 * @attr ref android.R.styleable#View_longClickable
3987 */
3988 public boolean isLongClickable() {
3989 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3990 }
3991
3992 /**
3993 * Enables or disables long click events for this view. When a view is long
3994 * clickable it reacts to the user holding down the button for a longer
3995 * duration than a tap. This event can either launch the listener or a
3996 * context menu.
3997 *
3998 * @param longClickable true to make the view long clickable, false otherwise
3999 * @see #isLongClickable()
4000 * @attr ref android.R.styleable#View_longClickable
4001 */
4002 public void setLongClickable(boolean longClickable) {
4003 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4004 }
4005
4006 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004007 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004008 *
4009 * @see #isClickable()
4010 * @see #setClickable(boolean)
4011 *
4012 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4013 * the View's internal state from a previously set "pressed" state.
4014 */
4015 public void setPressed(boolean pressed) {
4016 if (pressed) {
4017 mPrivateFlags |= PRESSED;
4018 } else {
4019 mPrivateFlags &= ~PRESSED;
4020 }
4021 refreshDrawableState();
4022 dispatchSetPressed(pressed);
4023 }
4024
4025 /**
4026 * Dispatch setPressed to all of this View's children.
4027 *
4028 * @see #setPressed(boolean)
4029 *
4030 * @param pressed The new pressed state
4031 */
4032 protected void dispatchSetPressed(boolean pressed) {
4033 }
4034
4035 /**
4036 * Indicates whether the view is currently in pressed state. Unless
4037 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4038 * the pressed state.
4039 *
4040 * @see #setPressed
4041 * @see #isClickable()
4042 * @see #setClickable(boolean)
4043 *
4044 * @return true if the view is currently pressed, false otherwise
4045 */
4046 public boolean isPressed() {
4047 return (mPrivateFlags & PRESSED) == PRESSED;
4048 }
4049
4050 /**
4051 * Indicates whether this view will save its state (that is,
4052 * whether its {@link #onSaveInstanceState} method will be called).
4053 *
4054 * @return Returns true if the view state saving is enabled, else false.
4055 *
4056 * @see #setSaveEnabled(boolean)
4057 * @attr ref android.R.styleable#View_saveEnabled
4058 */
4059 public boolean isSaveEnabled() {
4060 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4061 }
4062
4063 /**
4064 * Controls whether the saving of this view's state is
4065 * enabled (that is, whether its {@link #onSaveInstanceState} method
4066 * will be called). Note that even if freezing is enabled, the
4067 * view still must have an id assigned to it (via {@link #setId setId()})
4068 * for its state to be saved. This flag can only disable the
4069 * saving of this view; any child views may still have their state saved.
4070 *
4071 * @param enabled Set to false to <em>disable</em> state saving, or true
4072 * (the default) to allow it.
4073 *
4074 * @see #isSaveEnabled()
4075 * @see #setId(int)
4076 * @see #onSaveInstanceState()
4077 * @attr ref android.R.styleable#View_saveEnabled
4078 */
4079 public void setSaveEnabled(boolean enabled) {
4080 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4081 }
4082
Jeff Brown85a31762010-09-01 17:01:00 -07004083 /**
4084 * Gets whether the framework should discard touches when the view's
4085 * window is obscured by another visible window.
4086 * Refer to the {@link View} security documentation for more details.
4087 *
4088 * @return True if touch filtering is enabled.
4089 *
4090 * @see #setFilterTouchesWhenObscured(boolean)
4091 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4092 */
4093 @ViewDebug.ExportedProperty
4094 public boolean getFilterTouchesWhenObscured() {
4095 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4096 }
4097
4098 /**
4099 * Sets whether the framework should discard touches when the view's
4100 * window is obscured by another visible window.
4101 * Refer to the {@link View} security documentation for more details.
4102 *
4103 * @param enabled True if touch filtering should be enabled.
4104 *
4105 * @see #getFilterTouchesWhenObscured
4106 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4107 */
4108 public void setFilterTouchesWhenObscured(boolean enabled) {
4109 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4110 FILTER_TOUCHES_WHEN_OBSCURED);
4111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004112
4113 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004114 * Indicates whether the entire hierarchy under this view will save its
4115 * state when a state saving traversal occurs from its parent. The default
4116 * is true; if false, these views will not be saved unless
4117 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4118 *
4119 * @return Returns true if the view state saving from parent is enabled, else false.
4120 *
4121 * @see #setSaveFromParentEnabled(boolean)
4122 */
4123 public boolean isSaveFromParentEnabled() {
4124 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4125 }
4126
4127 /**
4128 * Controls whether the entire hierarchy under this view will save its
4129 * state when a state saving traversal occurs from its parent. The default
4130 * is true; if false, these views will not be saved unless
4131 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4132 *
4133 * @param enabled Set to false to <em>disable</em> state saving, or true
4134 * (the default) to allow it.
4135 *
4136 * @see #isSaveFromParentEnabled()
4137 * @see #setId(int)
4138 * @see #onSaveInstanceState()
4139 */
4140 public void setSaveFromParentEnabled(boolean enabled) {
4141 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4142 }
4143
4144
4145 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004146 * Returns whether this View is able to take focus.
4147 *
4148 * @return True if this view can take focus, or false otherwise.
4149 * @attr ref android.R.styleable#View_focusable
4150 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004151 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004152 public final boolean isFocusable() {
4153 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4154 }
4155
4156 /**
4157 * When a view is focusable, it may not want to take focus when in touch mode.
4158 * For example, a button would like focus when the user is navigating via a D-pad
4159 * so that the user can click on it, but once the user starts touching the screen,
4160 * the button shouldn't take focus
4161 * @return Whether the view is focusable in touch mode.
4162 * @attr ref android.R.styleable#View_focusableInTouchMode
4163 */
4164 @ViewDebug.ExportedProperty
4165 public final boolean isFocusableInTouchMode() {
4166 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4167 }
4168
4169 /**
4170 * Find the nearest view in the specified direction that can take focus.
4171 * This does not actually give focus to that view.
4172 *
4173 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4174 *
4175 * @return The nearest focusable in the specified direction, or null if none
4176 * can be found.
4177 */
4178 public View focusSearch(int direction) {
4179 if (mParent != null) {
4180 return mParent.focusSearch(this, direction);
4181 } else {
4182 return null;
4183 }
4184 }
4185
4186 /**
4187 * This method is the last chance for the focused view and its ancestors to
4188 * respond to an arrow key. This is called when the focused view did not
4189 * consume the key internally, nor could the view system find a new view in
4190 * the requested direction to give focus to.
4191 *
4192 * @param focused The currently focused view.
4193 * @param direction The direction focus wants to move. One of FOCUS_UP,
4194 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4195 * @return True if the this view consumed this unhandled move.
4196 */
4197 public boolean dispatchUnhandledMove(View focused, int direction) {
4198 return false;
4199 }
4200
4201 /**
4202 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004203 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004204 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004205 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4206 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004207 * @return The user specified next view, or null if there is none.
4208 */
4209 View findUserSetNextFocus(View root, int direction) {
4210 switch (direction) {
4211 case FOCUS_LEFT:
4212 if (mNextFocusLeftId == View.NO_ID) return null;
4213 return findViewShouldExist(root, mNextFocusLeftId);
4214 case FOCUS_RIGHT:
4215 if (mNextFocusRightId == View.NO_ID) return null;
4216 return findViewShouldExist(root, mNextFocusRightId);
4217 case FOCUS_UP:
4218 if (mNextFocusUpId == View.NO_ID) return null;
4219 return findViewShouldExist(root, mNextFocusUpId);
4220 case FOCUS_DOWN:
4221 if (mNextFocusDownId == View.NO_ID) return null;
4222 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004223 case FOCUS_FORWARD:
4224 if (mNextFocusForwardId == View.NO_ID) return null;
4225 return findViewShouldExist(root, mNextFocusForwardId);
4226 case FOCUS_BACKWARD: {
4227 final int id = mID;
4228 return root.findViewByPredicate(new Predicate<View>() {
4229 @Override
4230 public boolean apply(View t) {
4231 return t.mNextFocusForwardId == id;
4232 }
4233 });
4234 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004235 }
4236 return null;
4237 }
4238
4239 private static View findViewShouldExist(View root, int childViewId) {
4240 View result = root.findViewById(childViewId);
4241 if (result == null) {
4242 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4243 + "by user for id " + childViewId);
4244 }
4245 return result;
4246 }
4247
4248 /**
4249 * Find and return all focusable views that are descendants of this view,
4250 * possibly including this view if it is focusable itself.
4251 *
4252 * @param direction The direction of the focus
4253 * @return A list of focusable views
4254 */
4255 public ArrayList<View> getFocusables(int direction) {
4256 ArrayList<View> result = new ArrayList<View>(24);
4257 addFocusables(result, direction);
4258 return result;
4259 }
4260
4261 /**
4262 * Add any focusable views that are descendants of this view (possibly
4263 * including this view if it is focusable itself) to views. If we are in touch mode,
4264 * only add views that are also focusable in touch mode.
4265 *
4266 * @param views Focusable views found so far
4267 * @param direction The direction of the focus
4268 */
4269 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004270 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4271 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004272
svetoslavganov75986cf2009-05-14 22:28:01 -07004273 /**
4274 * Adds any focusable views that are descendants of this view (possibly
4275 * including this view if it is focusable itself) to views. This method
4276 * adds all focusable views regardless if we are in touch mode or
4277 * only views focusable in touch mode if we are in touch mode depending on
4278 * the focusable mode paramater.
4279 *
4280 * @param views Focusable views found so far or null if all we are interested is
4281 * the number of focusables.
4282 * @param direction The direction of the focus.
4283 * @param focusableMode The type of focusables to be added.
4284 *
4285 * @see #FOCUSABLES_ALL
4286 * @see #FOCUSABLES_TOUCH_MODE
4287 */
4288 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4289 if (!isFocusable()) {
4290 return;
4291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004292
svetoslavganov75986cf2009-05-14 22:28:01 -07004293 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4294 isInTouchMode() && !isFocusableInTouchMode()) {
4295 return;
4296 }
4297
4298 if (views != null) {
4299 views.add(this);
4300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 }
4302
4303 /**
4304 * Find and return all touchable views that are descendants of this view,
4305 * possibly including this view if it is touchable itself.
4306 *
4307 * @return A list of touchable views
4308 */
4309 public ArrayList<View> getTouchables() {
4310 ArrayList<View> result = new ArrayList<View>();
4311 addTouchables(result);
4312 return result;
4313 }
4314
4315 /**
4316 * Add any touchable views that are descendants of this view (possibly
4317 * including this view if it is touchable itself) to views.
4318 *
4319 * @param views Touchable views found so far
4320 */
4321 public void addTouchables(ArrayList<View> views) {
4322 final int viewFlags = mViewFlags;
4323
4324 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4325 && (viewFlags & ENABLED_MASK) == ENABLED) {
4326 views.add(this);
4327 }
4328 }
4329
4330 /**
4331 * Call this to try to give focus to a specific view or to one of its
4332 * descendants.
4333 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004334 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4335 * false), or if it is focusable and it is not focusable in touch mode
4336 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004337 *
4338 * See also {@link #focusSearch}, which is what you call to say that you
4339 * have focus, and you want your parent to look for the next one.
4340 *
4341 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4342 * {@link #FOCUS_DOWN} and <code>null</code>.
4343 *
4344 * @return Whether this view or one of its descendants actually took focus.
4345 */
4346 public final boolean requestFocus() {
4347 return requestFocus(View.FOCUS_DOWN);
4348 }
4349
4350
4351 /**
4352 * Call this to try to give focus to a specific view or to one of its
4353 * descendants and give it a hint about what direction focus is heading.
4354 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004355 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4356 * false), or if it is focusable and it is not focusable in touch mode
4357 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 *
4359 * See also {@link #focusSearch}, which is what you call to say that you
4360 * have focus, and you want your parent to look for the next one.
4361 *
4362 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4363 * <code>null</code> set for the previously focused rectangle.
4364 *
4365 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4366 * @return Whether this view or one of its descendants actually took focus.
4367 */
4368 public final boolean requestFocus(int direction) {
4369 return requestFocus(direction, null);
4370 }
4371
4372 /**
4373 * Call this to try to give focus to a specific view or to one of its descendants
4374 * and give it hints about the direction and a specific rectangle that the focus
4375 * is coming from. The rectangle can help give larger views a finer grained hint
4376 * about where focus is coming from, and therefore, where to show selection, or
4377 * forward focus change internally.
4378 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004379 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4380 * false), or if it is focusable and it is not focusable in touch mode
4381 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004382 *
4383 * A View will not take focus if it is not visible.
4384 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004385 * A View will not take focus if one of its parents has
4386 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4387 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004388 *
4389 * See also {@link #focusSearch}, which is what you call to say that you
4390 * have focus, and you want your parent to look for the next one.
4391 *
4392 * You may wish to override this method if your custom {@link View} has an internal
4393 * {@link View} that it wishes to forward the request to.
4394 *
4395 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4396 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4397 * to give a finer grained hint about where focus is coming from. May be null
4398 * if there is no hint.
4399 * @return Whether this view or one of its descendants actually took focus.
4400 */
4401 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4402 // need to be focusable
4403 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4404 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4405 return false;
4406 }
4407
4408 // need to be focusable in touch mode if in touch mode
4409 if (isInTouchMode() &&
4410 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4411 return false;
4412 }
4413
4414 // need to not have any parents blocking us
4415 if (hasAncestorThatBlocksDescendantFocus()) {
4416 return false;
4417 }
4418
4419 handleFocusGainInternal(direction, previouslyFocusedRect);
4420 return true;
4421 }
4422
Christopher Tate2c095f32010-10-04 14:13:40 -07004423 /** Gets the ViewRoot, or null if not attached. */
4424 /*package*/ ViewRoot getViewRoot() {
4425 View root = getRootView();
4426 return root != null ? (ViewRoot)root.getParent() : null;
4427 }
4428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004429 /**
4430 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4431 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4432 * touch mode to request focus when they are touched.
4433 *
4434 * @return Whether this view or one of its descendants actually took focus.
4435 *
4436 * @see #isInTouchMode()
4437 *
4438 */
4439 public final boolean requestFocusFromTouch() {
4440 // Leave touch mode if we need to
4441 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004442 ViewRoot viewRoot = getViewRoot();
4443 if (viewRoot != null) {
4444 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004445 }
4446 }
4447 return requestFocus(View.FOCUS_DOWN);
4448 }
4449
4450 /**
4451 * @return Whether any ancestor of this view blocks descendant focus.
4452 */
4453 private boolean hasAncestorThatBlocksDescendantFocus() {
4454 ViewParent ancestor = mParent;
4455 while (ancestor instanceof ViewGroup) {
4456 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4457 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4458 return true;
4459 } else {
4460 ancestor = vgAncestor.getParent();
4461 }
4462 }
4463 return false;
4464 }
4465
4466 /**
Romain Guya440b002010-02-24 15:57:54 -08004467 * @hide
4468 */
4469 public void dispatchStartTemporaryDetach() {
4470 onStartTemporaryDetach();
4471 }
4472
4473 /**
4474 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004475 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4476 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004477 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004478 */
4479 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004480 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004481 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004482 }
4483
4484 /**
4485 * @hide
4486 */
4487 public void dispatchFinishTemporaryDetach() {
4488 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 }
Romain Guy8506ab42009-06-11 17:35:47 -07004490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004491 /**
4492 * Called after {@link #onStartTemporaryDetach} when the container is done
4493 * changing the view.
4494 */
4495 public void onFinishTemporaryDetach() {
4496 }
Romain Guy8506ab42009-06-11 17:35:47 -07004497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004498 /**
4499 * capture information of this view for later analysis: developement only
4500 * check dynamic switch to make sure we only dump view
4501 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4502 */
4503 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004504 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004505 return;
4506 }
4507 ViewDebug.dumpCapturedView(subTag, v);
4508 }
4509
4510 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004511 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4512 * for this view's window. Returns null if the view is not currently attached
4513 * to the window. Normally you will not need to use this directly, but
4514 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4515 */
4516 public KeyEvent.DispatcherState getKeyDispatcherState() {
4517 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4518 }
Joe Malin32736f02011-01-19 16:14:20 -08004519
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 * Dispatch a key event before it is processed by any input method
4522 * associated with the view hierarchy. This can be used to intercept
4523 * key events in special situations before the IME consumes them; a
4524 * typical example would be handling the BACK key to update the application's
4525 * UI instead of allowing the IME to see it and close itself.
4526 *
4527 * @param event The key event to be dispatched.
4528 * @return True if the event was handled, false otherwise.
4529 */
4530 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4531 return onKeyPreIme(event.getKeyCode(), event);
4532 }
4533
4534 /**
4535 * Dispatch a key event to the next view on the focus path. This path runs
4536 * from the top of the view tree down to the currently focused view. If this
4537 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4538 * the next node down the focus path. This method also fires any key
4539 * listeners.
4540 *
4541 * @param event The key event to be dispatched.
4542 * @return True if the event was handled, false otherwise.
4543 */
4544 public boolean dispatchKeyEvent(KeyEvent event) {
4545 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546
Romain Guyf607bdc2010-09-10 19:20:06 -07004547 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 if (android.util.Config.LOGV) {
4549 captureViewInfo("captureViewKeyEvent", this);
4550 }
4551
Romain Guyf607bdc2010-09-10 19:20:06 -07004552 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004553 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4554 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4555 return true;
4556 }
4557
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004558 return event.dispatch(this, mAttachInfo != null
4559 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004560 }
4561
4562 /**
4563 * Dispatches a key shortcut event.
4564 *
4565 * @param event The key event to be dispatched.
4566 * @return True if the event was handled by the view, false otherwise.
4567 */
4568 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4569 return onKeyShortcut(event.getKeyCode(), event);
4570 }
4571
4572 /**
4573 * Pass the touch screen motion event down to the target view, or this
4574 * view if it is the target.
4575 *
4576 * @param event The motion event to be dispatched.
4577 * @return True if the event was handled by the view, false otherwise.
4578 */
4579 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004580 if (!onFilterTouchEventForSecurity(event)) {
4581 return false;
4582 }
4583
Romain Guyf607bdc2010-09-10 19:20:06 -07004584 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4586 mOnTouchListener.onTouch(this, event)) {
4587 return true;
4588 }
4589 return onTouchEvent(event);
4590 }
4591
4592 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004593 * Filter the touch event to apply security policies.
4594 *
4595 * @param event The motion event to be filtered.
4596 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004597 *
Jeff Brown85a31762010-09-01 17:01:00 -07004598 * @see #getFilterTouchesWhenObscured
4599 */
4600 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004601 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004602 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4603 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4604 // Window is obscured, drop this touch.
4605 return false;
4606 }
4607 return true;
4608 }
4609
4610 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004611 * Pass a trackball motion event down to the focused view.
4612 *
4613 * @param event The motion event to be dispatched.
4614 * @return True if the event was handled by the view, false otherwise.
4615 */
4616 public boolean dispatchTrackballEvent(MotionEvent event) {
4617 //Log.i("view", "view=" + this + ", " + event.toString());
4618 return onTrackballEvent(event);
4619 }
4620
4621 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08004622 * Pass a generic motion event down to the focused view.
4623 *
4624 * @param event The motion event to be dispatched.
4625 * @return True if the event was handled by the view, false otherwise.
4626 */
4627 public boolean dispatchGenericMotionEvent(MotionEvent event) {
4628 return onGenericMotionEvent(event);
4629 }
4630
4631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004632 * Called when the window containing this view gains or loses window focus.
4633 * ViewGroups should override to route to their children.
4634 *
4635 * @param hasFocus True if the window containing this view now has focus,
4636 * false otherwise.
4637 */
4638 public void dispatchWindowFocusChanged(boolean hasFocus) {
4639 onWindowFocusChanged(hasFocus);
4640 }
4641
4642 /**
4643 * Called when the window containing this view gains or loses focus. Note
4644 * that this is separate from view focus: to receive key events, both
4645 * your view and its window must have focus. If a window is displayed
4646 * on top of yours that takes input focus, then your own window will lose
4647 * focus but the view focus will remain unchanged.
4648 *
4649 * @param hasWindowFocus True if the window containing this view now has
4650 * focus, false otherwise.
4651 */
4652 public void onWindowFocusChanged(boolean hasWindowFocus) {
4653 InputMethodManager imm = InputMethodManager.peekInstance();
4654 if (!hasWindowFocus) {
4655 if (isPressed()) {
4656 setPressed(false);
4657 }
4658 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4659 imm.focusOut(this);
4660 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004661 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004662 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004663 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004664 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4665 imm.focusIn(this);
4666 }
4667 refreshDrawableState();
4668 }
4669
4670 /**
4671 * Returns true if this view is in a window that currently has window focus.
4672 * Note that this is not the same as the view itself having focus.
4673 *
4674 * @return True if this view is in a window that currently has window focus.
4675 */
4676 public boolean hasWindowFocus() {
4677 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4678 }
4679
4680 /**
Adam Powell326d8082009-12-09 15:10:07 -08004681 * Dispatch a view visibility change down the view hierarchy.
4682 * ViewGroups should override to route to their children.
4683 * @param changedView The view whose visibility changed. Could be 'this' or
4684 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004685 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4686 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004687 */
4688 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4689 onVisibilityChanged(changedView, visibility);
4690 }
4691
4692 /**
4693 * Called when the visibility of the view or an ancestor of the view is changed.
4694 * @param changedView The view whose visibility changed. Could be 'this' or
4695 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004696 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4697 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004698 */
4699 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004700 if (visibility == VISIBLE) {
4701 if (mAttachInfo != null) {
4702 initialAwakenScrollBars();
4703 } else {
4704 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4705 }
4706 }
Adam Powell326d8082009-12-09 15:10:07 -08004707 }
4708
4709 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004710 * Dispatch a hint about whether this view is displayed. For instance, when
4711 * a View moves out of the screen, it might receives a display hint indicating
4712 * the view is not displayed. Applications should not <em>rely</em> on this hint
4713 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004714 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004715 * @param hint A hint about whether or not this view is displayed:
4716 * {@link #VISIBLE} or {@link #INVISIBLE}.
4717 */
4718 public void dispatchDisplayHint(int hint) {
4719 onDisplayHint(hint);
4720 }
4721
4722 /**
4723 * Gives this view a hint about whether is displayed or not. For instance, when
4724 * a View moves out of the screen, it might receives a display hint indicating
4725 * the view is not displayed. Applications should not <em>rely</em> on this hint
4726 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004727 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004728 * @param hint A hint about whether or not this view is displayed:
4729 * {@link #VISIBLE} or {@link #INVISIBLE}.
4730 */
4731 protected void onDisplayHint(int hint) {
4732 }
4733
4734 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004735 * Dispatch a window visibility change down the view hierarchy.
4736 * ViewGroups should override to route to their children.
4737 *
4738 * @param visibility The new visibility of the window.
4739 *
4740 * @see #onWindowVisibilityChanged
4741 */
4742 public void dispatchWindowVisibilityChanged(int visibility) {
4743 onWindowVisibilityChanged(visibility);
4744 }
4745
4746 /**
4747 * Called when the window containing has change its visibility
4748 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4749 * that this tells you whether or not your window is being made visible
4750 * to the window manager; this does <em>not</em> tell you whether or not
4751 * your window is obscured by other windows on the screen, even if it
4752 * is itself visible.
4753 *
4754 * @param visibility The new visibility of the window.
4755 */
4756 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004757 if (visibility == VISIBLE) {
4758 initialAwakenScrollBars();
4759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004760 }
4761
4762 /**
4763 * Returns the current visibility of the window this view is attached to
4764 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4765 *
4766 * @return Returns the current visibility of the view's window.
4767 */
4768 public int getWindowVisibility() {
4769 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4770 }
4771
4772 /**
4773 * Retrieve the overall visible display size in which the window this view is
4774 * attached to has been positioned in. This takes into account screen
4775 * decorations above the window, for both cases where the window itself
4776 * is being position inside of them or the window is being placed under
4777 * then and covered insets are used for the window to position its content
4778 * inside. In effect, this tells you the available area where content can
4779 * be placed and remain visible to users.
4780 *
4781 * <p>This function requires an IPC back to the window manager to retrieve
4782 * the requested information, so should not be used in performance critical
4783 * code like drawing.
4784 *
4785 * @param outRect Filled in with the visible display frame. If the view
4786 * is not attached to a window, this is simply the raw display size.
4787 */
4788 public void getWindowVisibleDisplayFrame(Rect outRect) {
4789 if (mAttachInfo != null) {
4790 try {
4791 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4792 } catch (RemoteException e) {
4793 return;
4794 }
4795 // XXX This is really broken, and probably all needs to be done
4796 // in the window manager, and we need to know more about whether
4797 // we want the area behind or in front of the IME.
4798 final Rect insets = mAttachInfo.mVisibleInsets;
4799 outRect.left += insets.left;
4800 outRect.top += insets.top;
4801 outRect.right -= insets.right;
4802 outRect.bottom -= insets.bottom;
4803 return;
4804 }
4805 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4806 outRect.set(0, 0, d.getWidth(), d.getHeight());
4807 }
4808
4809 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004810 * Dispatch a notification about a resource configuration change down
4811 * the view hierarchy.
4812 * ViewGroups should override to route to their children.
4813 *
4814 * @param newConfig The new resource configuration.
4815 *
4816 * @see #onConfigurationChanged
4817 */
4818 public void dispatchConfigurationChanged(Configuration newConfig) {
4819 onConfigurationChanged(newConfig);
4820 }
4821
4822 /**
4823 * Called when the current configuration of the resources being used
4824 * by the application have changed. You can use this to decide when
4825 * to reload resources that can changed based on orientation and other
4826 * configuration characterstics. You only need to use this if you are
4827 * not relying on the normal {@link android.app.Activity} mechanism of
4828 * recreating the activity instance upon a configuration change.
4829 *
4830 * @param newConfig The new resource configuration.
4831 */
4832 protected void onConfigurationChanged(Configuration newConfig) {
4833 }
4834
4835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004836 * Private function to aggregate all per-view attributes in to the view
4837 * root.
4838 */
4839 void dispatchCollectViewAttributes(int visibility) {
4840 performCollectViewAttributes(visibility);
4841 }
4842
4843 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004844 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004845 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4846 mAttachInfo.mKeepScreenOn = true;
4847 }
4848 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4849 if (mOnSystemUiVisibilityChangeListener != null) {
4850 mAttachInfo.mHasSystemUiListeners = true;
4851 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004852 }
4853 }
4854
4855 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004856 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004857 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004858 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4859 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004860 ai.mRecomputeGlobalAttributes = true;
4861 }
4862 }
4863 }
4864
4865 /**
4866 * Returns whether the device is currently in touch mode. Touch mode is entered
4867 * once the user begins interacting with the device by touch, and affects various
4868 * things like whether focus is always visible to the user.
4869 *
4870 * @return Whether the device is in touch mode.
4871 */
4872 @ViewDebug.ExportedProperty
4873 public boolean isInTouchMode() {
4874 if (mAttachInfo != null) {
4875 return mAttachInfo.mInTouchMode;
4876 } else {
4877 return ViewRoot.isInTouchMode();
4878 }
4879 }
4880
4881 /**
4882 * Returns the context the view is running in, through which it can
4883 * access the current theme, resources, etc.
4884 *
4885 * @return The view's Context.
4886 */
4887 @ViewDebug.CapturedViewProperty
4888 public final Context getContext() {
4889 return mContext;
4890 }
4891
4892 /**
4893 * Handle a key event before it is processed by any input method
4894 * associated with the view hierarchy. This can be used to intercept
4895 * key events in special situations before the IME consumes them; a
4896 * typical example would be handling the BACK key to update the application's
4897 * UI instead of allowing the IME to see it and close itself.
4898 *
4899 * @param keyCode The value in event.getKeyCode().
4900 * @param event Description of the key event.
4901 * @return If you handled the event, return true. If you want to allow the
4902 * event to be handled by the next receiver, return false.
4903 */
4904 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4905 return false;
4906 }
4907
4908 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004909 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
4910 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4912 * is released, if the view is enabled and clickable.
4913 *
4914 * @param keyCode A key code that represents the button pressed, from
4915 * {@link android.view.KeyEvent}.
4916 * @param event The KeyEvent object that defines the button action.
4917 */
4918 public boolean onKeyDown(int keyCode, KeyEvent event) {
4919 boolean result = false;
4920
4921 switch (keyCode) {
4922 case KeyEvent.KEYCODE_DPAD_CENTER:
4923 case KeyEvent.KEYCODE_ENTER: {
4924 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4925 return true;
4926 }
4927 // Long clickable items don't necessarily have to be clickable
4928 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4929 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4930 (event.getRepeatCount() == 0)) {
4931 setPressed(true);
4932 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004933 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004934 }
4935 return true;
4936 }
4937 break;
4938 }
4939 }
4940 return result;
4941 }
4942
4943 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004944 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4945 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4946 * the event).
4947 */
4948 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
4949 return false;
4950 }
4951
4952 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004953 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
4954 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004955 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
4956 * {@link KeyEvent#KEYCODE_ENTER} is released.
4957 *
4958 * @param keyCode A key code that represents the button pressed, from
4959 * {@link android.view.KeyEvent}.
4960 * @param event The KeyEvent object that defines the button action.
4961 */
4962 public boolean onKeyUp(int keyCode, KeyEvent event) {
4963 boolean result = false;
4964
4965 switch (keyCode) {
4966 case KeyEvent.KEYCODE_DPAD_CENTER:
4967 case KeyEvent.KEYCODE_ENTER: {
4968 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4969 return true;
4970 }
4971 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
4972 setPressed(false);
4973
4974 if (!mHasPerformedLongPress) {
4975 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05004976 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977
4978 result = performClick();
4979 }
4980 }
4981 break;
4982 }
4983 }
4984 return result;
4985 }
4986
4987 /**
4988 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4989 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
4990 * the event).
4991 *
4992 * @param keyCode A key code that represents the button pressed, from
4993 * {@link android.view.KeyEvent}.
4994 * @param repeatCount The number of times the action was made.
4995 * @param event The KeyEvent object that defines the button action.
4996 */
4997 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4998 return false;
4999 }
5000
5001 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005002 * Called on the focused view when a key shortcut event is not handled.
5003 * Override this method to implement local key shortcuts for the View.
5004 * Key shortcuts can also be implemented by setting the
5005 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 *
5007 * @param keyCode The value in event.getKeyCode().
5008 * @param event Description of the key event.
5009 * @return If you handled the event, return true. If you want to allow the
5010 * event to be handled by the next receiver, return false.
5011 */
5012 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5013 return false;
5014 }
5015
5016 /**
5017 * Check whether the called view is a text editor, in which case it
5018 * would make sense to automatically display a soft input window for
5019 * it. Subclasses should override this if they implement
5020 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005021 * a call on that method would return a non-null InputConnection, and
5022 * they are really a first-class editor that the user would normally
5023 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005024 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005025 * <p>The default implementation always returns false. This does
5026 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5027 * will not be called or the user can not otherwise perform edits on your
5028 * view; it is just a hint to the system that this is not the primary
5029 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005030 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 * @return Returns true if this view is a text editor, else false.
5032 */
5033 public boolean onCheckIsTextEditor() {
5034 return false;
5035 }
Romain Guy8506ab42009-06-11 17:35:47 -07005036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 /**
5038 * Create a new InputConnection for an InputMethod to interact
5039 * with the view. The default implementation returns null, since it doesn't
5040 * support input methods. You can override this to implement such support.
5041 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005042 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 * <p>When implementing this, you probably also want to implement
5044 * {@link #onCheckIsTextEditor()} to indicate you will return a
5045 * non-null InputConnection.
5046 *
5047 * @param outAttrs Fill in with attribute information about the connection.
5048 */
5049 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5050 return null;
5051 }
5052
5053 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005054 * Called by the {@link android.view.inputmethod.InputMethodManager}
5055 * when a view who is not the current
5056 * input connection target is trying to make a call on the manager. The
5057 * default implementation returns false; you can override this to return
5058 * true for certain views if you are performing InputConnection proxying
5059 * to them.
5060 * @param view The View that is making the InputMethodManager call.
5061 * @return Return true to allow the call, false to reject.
5062 */
5063 public boolean checkInputConnectionProxy(View view) {
5064 return false;
5065 }
Romain Guy8506ab42009-06-11 17:35:47 -07005066
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005068 * Show the context menu for this view. It is not safe to hold on to the
5069 * menu after returning from this method.
5070 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005071 * You should normally not overload this method. Overload
5072 * {@link #onCreateContextMenu(ContextMenu)} or define an
5073 * {@link OnCreateContextMenuListener} to add items to the context menu.
5074 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005075 * @param menu The context menu to populate
5076 */
5077 public void createContextMenu(ContextMenu menu) {
5078 ContextMenuInfo menuInfo = getContextMenuInfo();
5079
5080 // Sets the current menu info so all items added to menu will have
5081 // my extra info set.
5082 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5083
5084 onCreateContextMenu(menu);
5085 if (mOnCreateContextMenuListener != null) {
5086 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5087 }
5088
5089 // Clear the extra information so subsequent items that aren't mine don't
5090 // have my extra info.
5091 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5092
5093 if (mParent != null) {
5094 mParent.createContextMenu(menu);
5095 }
5096 }
5097
5098 /**
5099 * Views should implement this if they have extra information to associate
5100 * with the context menu. The return result is supplied as a parameter to
5101 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5102 * callback.
5103 *
5104 * @return Extra information about the item for which the context menu
5105 * should be shown. This information will vary across different
5106 * subclasses of View.
5107 */
5108 protected ContextMenuInfo getContextMenuInfo() {
5109 return null;
5110 }
5111
5112 /**
5113 * Views should implement this if the view itself is going to add items to
5114 * the context menu.
5115 *
5116 * @param menu the context menu to populate
5117 */
5118 protected void onCreateContextMenu(ContextMenu menu) {
5119 }
5120
5121 /**
5122 * Implement this method to handle trackball motion events. The
5123 * <em>relative</em> movement of the trackball since the last event
5124 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5125 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5126 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5127 * they will often be fractional values, representing the more fine-grained
5128 * movement information available from a trackball).
5129 *
5130 * @param event The motion event.
5131 * @return True if the event was handled, false otherwise.
5132 */
5133 public boolean onTrackballEvent(MotionEvent event) {
5134 return false;
5135 }
5136
5137 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005138 * Implement this method to handle generic motion events.
5139 * <p>
5140 * Generic motion events are dispatched to the focused view to describe
5141 * the motions of input devices such as joysticks. The
5142 * {@link MotionEvent#getSource() source} of the motion event specifies
5143 * the class of input that was received. Implementations of this method
5144 * must examine the bits in the source before processing the event.
5145 * The following code example shows how this is done.
5146 * </p>
5147 * <code>
5148 * public boolean onGenericMotionEvent(MotionEvent event) {
5149 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
5150 * float x = event.getX();
5151 * float y = event.getY();
5152 * // process the joystick motion
5153 * return true;
5154 * }
5155 * return super.onGenericMotionEvent(event);
5156 * }
5157 * </code>
5158 *
5159 * @param event The generic motion event being processed.
5160 *
5161 * @return Return true if you have consumed the event, false if you haven't.
5162 * The default implementation always returns false.
5163 */
5164 public boolean onGenericMotionEvent(MotionEvent event) {
5165 return false;
5166 }
5167
5168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005169 * Implement this method to handle touch screen motion events.
5170 *
5171 * @param event The motion event.
5172 * @return True if the event was handled, false otherwise.
5173 */
5174 public boolean onTouchEvent(MotionEvent event) {
5175 final int viewFlags = mViewFlags;
5176
5177 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5178 // A disabled view that is clickable still consumes the touch
5179 // events, it just doesn't respond to them.
5180 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5181 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5182 }
5183
5184 if (mTouchDelegate != null) {
5185 if (mTouchDelegate.onTouchEvent(event)) {
5186 return true;
5187 }
5188 }
5189
5190 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5191 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5192 switch (event.getAction()) {
5193 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005194 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5195 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005196 // take focus if we don't have it already and we should in
5197 // touch mode.
5198 boolean focusTaken = false;
5199 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5200 focusTaken = requestFocus();
5201 }
5202
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005203 if (prepressed) {
5204 // The button is being released before we actually
5205 // showed it as pressed. Make it show the pressed
5206 // state now (before scheduling the click) to ensure
5207 // the user sees it.
5208 mPrivateFlags |= PRESSED;
5209 refreshDrawableState();
5210 }
Joe Malin32736f02011-01-19 16:14:20 -08005211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005212 if (!mHasPerformedLongPress) {
5213 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005214 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215
5216 // Only perform take click actions if we were in the pressed state
5217 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005218 // Use a Runnable and post this rather than calling
5219 // performClick directly. This lets other visual state
5220 // of the view update before click actions start.
5221 if (mPerformClick == null) {
5222 mPerformClick = new PerformClick();
5223 }
5224 if (!post(mPerformClick)) {
5225 performClick();
5226 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 }
5228 }
5229
5230 if (mUnsetPressedState == null) {
5231 mUnsetPressedState = new UnsetPressedState();
5232 }
5233
Adam Powelle14579b2009-12-16 18:39:52 -08005234 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005235 postDelayed(mUnsetPressedState,
5236 ViewConfiguration.getPressedStateDuration());
5237 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005238 // If the post failed, unpress right now
5239 mUnsetPressedState.run();
5240 }
Adam Powelle14579b2009-12-16 18:39:52 -08005241 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005242 }
5243 break;
5244
5245 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005246 if (mPendingCheckForTap == null) {
5247 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005248 }
Adam Powelle14579b2009-12-16 18:39:52 -08005249 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005250 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005251 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005252 break;
5253
5254 case MotionEvent.ACTION_CANCEL:
5255 mPrivateFlags &= ~PRESSED;
5256 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005257 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 break;
5259
5260 case MotionEvent.ACTION_MOVE:
5261 final int x = (int) event.getX();
5262 final int y = (int) event.getY();
5263
5264 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005265 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005266 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005267 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005268 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005269 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005270 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005271
5272 // Need to switch from pressed to not pressed
5273 mPrivateFlags &= ~PRESSED;
5274 refreshDrawableState();
5275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005276 }
5277 break;
5278 }
5279 return true;
5280 }
5281
5282 return false;
5283 }
5284
5285 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005286 * Remove the longpress detection timer.
5287 */
5288 private void removeLongPressCallback() {
5289 if (mPendingCheckForLongPress != null) {
5290 removeCallbacks(mPendingCheckForLongPress);
5291 }
5292 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005293
5294 /**
5295 * Remove the pending click action
5296 */
5297 private void removePerformClickCallback() {
5298 if (mPerformClick != null) {
5299 removeCallbacks(mPerformClick);
5300 }
5301 }
5302
Adam Powelle14579b2009-12-16 18:39:52 -08005303 /**
Romain Guya440b002010-02-24 15:57:54 -08005304 * Remove the prepress detection timer.
5305 */
5306 private void removeUnsetPressCallback() {
5307 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5308 setPressed(false);
5309 removeCallbacks(mUnsetPressedState);
5310 }
5311 }
5312
5313 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005314 * Remove the tap detection timer.
5315 */
5316 private void removeTapCallback() {
5317 if (mPendingCheckForTap != null) {
5318 mPrivateFlags &= ~PREPRESSED;
5319 removeCallbacks(mPendingCheckForTap);
5320 }
5321 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005322
5323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005324 * Cancels a pending long press. Your subclass can use this if you
5325 * want the context menu to come up if the user presses and holds
5326 * at the same place, but you don't want it to come up if they press
5327 * and then move around enough to cause scrolling.
5328 */
5329 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005330 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005331
5332 /*
5333 * The prepressed state handled by the tap callback is a display
5334 * construct, but the tap callback will post a long press callback
5335 * less its own timeout. Remove it here.
5336 */
5337 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005338 }
5339
5340 /**
5341 * Sets the TouchDelegate for this View.
5342 */
5343 public void setTouchDelegate(TouchDelegate delegate) {
5344 mTouchDelegate = delegate;
5345 }
5346
5347 /**
5348 * Gets the TouchDelegate for this View.
5349 */
5350 public TouchDelegate getTouchDelegate() {
5351 return mTouchDelegate;
5352 }
5353
5354 /**
5355 * Set flags controlling behavior of this view.
5356 *
5357 * @param flags Constant indicating the value which should be set
5358 * @param mask Constant indicating the bit range that should be changed
5359 */
5360 void setFlags(int flags, int mask) {
5361 int old = mViewFlags;
5362 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5363
5364 int changed = mViewFlags ^ old;
5365 if (changed == 0) {
5366 return;
5367 }
5368 int privateFlags = mPrivateFlags;
5369
5370 /* Check if the FOCUSABLE bit has changed */
5371 if (((changed & FOCUSABLE_MASK) != 0) &&
5372 ((privateFlags & HAS_BOUNDS) !=0)) {
5373 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5374 && ((privateFlags & FOCUSED) != 0)) {
5375 /* Give up focus if we are no longer focusable */
5376 clearFocus();
5377 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5378 && ((privateFlags & FOCUSED) == 0)) {
5379 /*
5380 * Tell the view system that we are now available to take focus
5381 * if no one else already has it.
5382 */
5383 if (mParent != null) mParent.focusableViewAvailable(this);
5384 }
5385 }
5386
5387 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5388 if ((changed & VISIBILITY_MASK) != 0) {
5389 /*
5390 * If this view is becoming visible, set the DRAWN flag so that
5391 * the next invalidate() will not be skipped.
5392 */
5393 mPrivateFlags |= DRAWN;
5394
5395 needGlobalAttributesUpdate(true);
5396
5397 // a view becoming visible is worth notifying the parent
5398 // about in case nothing has focus. even if this specific view
5399 // isn't focusable, it may contain something that is, so let
5400 // the root view try to give this focus if nothing else does.
5401 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5402 mParent.focusableViewAvailable(this);
5403 }
5404 }
5405 }
5406
5407 /* Check if the GONE bit has changed */
5408 if ((changed & GONE) != 0) {
5409 needGlobalAttributesUpdate(false);
5410 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005411 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005412
Romain Guyecd80ee2009-12-03 17:13:02 -08005413 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5414 if (hasFocus()) clearFocus();
5415 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005416 }
5417 if (mAttachInfo != null) {
5418 mAttachInfo.mViewVisibilityChanged = true;
5419 }
5420 }
5421
5422 /* Check if the VISIBLE bit has changed */
5423 if ((changed & INVISIBLE) != 0) {
5424 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005425 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005426
5427 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5428 // root view becoming invisible shouldn't clear focus
5429 if (getRootView() != this) {
5430 clearFocus();
5431 }
5432 }
5433 if (mAttachInfo != null) {
5434 mAttachInfo.mViewVisibilityChanged = true;
5435 }
5436 }
5437
Adam Powell326d8082009-12-09 15:10:07 -08005438 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005439 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005440 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5441 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005442 }
Adam Powell326d8082009-12-09 15:10:07 -08005443 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5444 }
5445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5447 destroyDrawingCache();
5448 }
5449
5450 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5451 destroyDrawingCache();
5452 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005453 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005454 }
5455
5456 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5457 destroyDrawingCache();
5458 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5459 }
5460
5461 if ((changed & DRAW_MASK) != 0) {
5462 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5463 if (mBGDrawable != null) {
5464 mPrivateFlags &= ~SKIP_DRAW;
5465 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5466 } else {
5467 mPrivateFlags |= SKIP_DRAW;
5468 }
5469 } else {
5470 mPrivateFlags &= ~SKIP_DRAW;
5471 }
5472 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005473 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005474 }
5475
5476 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005477 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005478 mParent.recomputeViewAttributes(this);
5479 }
5480 }
5481 }
5482
5483 /**
5484 * Change the view's z order in the tree, so it's on top of other sibling
5485 * views
5486 */
5487 public void bringToFront() {
5488 if (mParent != null) {
5489 mParent.bringChildToFront(this);
5490 }
5491 }
5492
5493 /**
5494 * This is called in response to an internal scroll in this view (i.e., the
5495 * view scrolled its own contents). This is typically as a result of
5496 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5497 * called.
5498 *
5499 * @param l Current horizontal scroll origin.
5500 * @param t Current vertical scroll origin.
5501 * @param oldl Previous horizontal scroll origin.
5502 * @param oldt Previous vertical scroll origin.
5503 */
5504 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5505 mBackgroundSizeChanged = true;
5506
5507 final AttachInfo ai = mAttachInfo;
5508 if (ai != null) {
5509 ai.mViewScrollChanged = true;
5510 }
5511 }
5512
5513 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005514 * Interface definition for a callback to be invoked when the layout bounds of a view
5515 * changes due to layout processing.
5516 */
5517 public interface OnLayoutChangeListener {
5518 /**
5519 * Called when the focus state of a view has changed.
5520 *
5521 * @param v The view whose state has changed.
5522 * @param left The new value of the view's left property.
5523 * @param top The new value of the view's top property.
5524 * @param right The new value of the view's right property.
5525 * @param bottom The new value of the view's bottom property.
5526 * @param oldLeft The previous value of the view's left property.
5527 * @param oldTop The previous value of the view's top property.
5528 * @param oldRight The previous value of the view's right property.
5529 * @param oldBottom The previous value of the view's bottom property.
5530 */
5531 void onLayoutChange(View v, int left, int top, int right, int bottom,
5532 int oldLeft, int oldTop, int oldRight, int oldBottom);
5533 }
5534
5535 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 * This is called during layout when the size of this view has changed. If
5537 * you were just added to the view hierarchy, you're called with the old
5538 * values of 0.
5539 *
5540 * @param w Current width of this view.
5541 * @param h Current height of this view.
5542 * @param oldw Old width of this view.
5543 * @param oldh Old height of this view.
5544 */
5545 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5546 }
5547
5548 /**
5549 * Called by draw to draw the child views. This may be overridden
5550 * by derived classes to gain control just before its children are drawn
5551 * (but after its own view has been drawn).
5552 * @param canvas the canvas on which to draw the view
5553 */
5554 protected void dispatchDraw(Canvas canvas) {
5555 }
5556
5557 /**
5558 * Gets the parent of this view. Note that the parent is a
5559 * ViewParent and not necessarily a View.
5560 *
5561 * @return Parent of this view.
5562 */
5563 public final ViewParent getParent() {
5564 return mParent;
5565 }
5566
5567 /**
5568 * Return the scrolled left position of this view. This is the left edge of
5569 * the displayed part of your view. You do not need to draw any pixels
5570 * farther left, since those are outside of the frame of your view on
5571 * screen.
5572 *
5573 * @return The left edge of the displayed part of your view, in pixels.
5574 */
5575 public final int getScrollX() {
5576 return mScrollX;
5577 }
5578
5579 /**
5580 * Return the scrolled top position of this view. This is the top edge of
5581 * the displayed part of your view. You do not need to draw any pixels above
5582 * it, since those are outside of the frame of your view on screen.
5583 *
5584 * @return The top edge of the displayed part of your view, in pixels.
5585 */
5586 public final int getScrollY() {
5587 return mScrollY;
5588 }
5589
5590 /**
5591 * Return the width of the your view.
5592 *
5593 * @return The width of your view, in pixels.
5594 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005595 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 public final int getWidth() {
5597 return mRight - mLeft;
5598 }
5599
5600 /**
5601 * Return the height of your view.
5602 *
5603 * @return The height of your view, in pixels.
5604 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005605 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 public final int getHeight() {
5607 return mBottom - mTop;
5608 }
5609
5610 /**
5611 * Return the visible drawing bounds of your view. Fills in the output
5612 * rectangle with the values from getScrollX(), getScrollY(),
5613 * getWidth(), and getHeight().
5614 *
5615 * @param outRect The (scrolled) drawing bounds of the view.
5616 */
5617 public void getDrawingRect(Rect outRect) {
5618 outRect.left = mScrollX;
5619 outRect.top = mScrollY;
5620 outRect.right = mScrollX + (mRight - mLeft);
5621 outRect.bottom = mScrollY + (mBottom - mTop);
5622 }
5623
5624 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005625 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5626 * raw width component (that is the result is masked by
5627 * {@link #MEASURED_SIZE_MASK}).
5628 *
5629 * @return The raw measured width of this view.
5630 */
5631 public final int getMeasuredWidth() {
5632 return mMeasuredWidth & MEASURED_SIZE_MASK;
5633 }
5634
5635 /**
5636 * Return the full width measurement information for this view as computed
5637 * by the most recent call to {@link #measure}. This result is a bit mask
5638 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 * This should be used during measurement and layout calculations only. Use
5640 * {@link #getWidth()} to see how wide a view is after layout.
5641 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005642 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005643 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005644 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005645 return mMeasuredWidth;
5646 }
5647
5648 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005649 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5650 * raw width component (that is the result is masked by
5651 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005653 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 */
5655 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005656 return mMeasuredHeight & MEASURED_SIZE_MASK;
5657 }
5658
5659 /**
5660 * Return the full height measurement information for this view as computed
5661 * by the most recent call to {@link #measure}. This result is a bit mask
5662 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5663 * This should be used during measurement and layout calculations only. Use
5664 * {@link #getHeight()} to see how wide a view is after layout.
5665 *
5666 * @return The measured width of this view as a bit mask.
5667 */
5668 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005669 return mMeasuredHeight;
5670 }
5671
5672 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005673 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5674 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5675 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5676 * and the height component is at the shifted bits
5677 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5678 */
5679 public final int getMeasuredState() {
5680 return (mMeasuredWidth&MEASURED_STATE_MASK)
5681 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5682 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5683 }
5684
5685 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005686 * The transform matrix of this view, which is calculated based on the current
5687 * roation, scale, and pivot properties.
5688 *
5689 * @see #getRotation()
5690 * @see #getScaleX()
5691 * @see #getScaleY()
5692 * @see #getPivotX()
5693 * @see #getPivotY()
5694 * @return The current transform matrix for the view
5695 */
5696 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005697 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005698 return mMatrix;
5699 }
5700
5701 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005702 * Utility function to determine if the value is far enough away from zero to be
5703 * considered non-zero.
5704 * @param value A floating point value to check for zero-ness
5705 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5706 */
5707 private static boolean nonzero(float value) {
5708 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5709 }
5710
5711 /**
Jeff Brown86671742010-09-30 20:00:15 -07005712 * Returns true if the transform matrix is the identity matrix.
5713 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005714 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005715 * @return True if the transform matrix is the identity matrix, false otherwise.
5716 */
Jeff Brown86671742010-09-30 20:00:15 -07005717 final boolean hasIdentityMatrix() {
5718 updateMatrix();
5719 return mMatrixIsIdentity;
5720 }
5721
5722 /**
5723 * Recomputes the transform matrix if necessary.
5724 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005725 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005726 if (mMatrixDirty) {
5727 // transform-related properties have changed since the last time someone
5728 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005729
5730 // Figure out if we need to update the pivot point
5731 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005732 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005733 mPrevWidth = mRight - mLeft;
5734 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005735 mPivotX = mPrevWidth / 2f;
5736 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005737 }
5738 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005739 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005740 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5741 mMatrix.setTranslate(mTranslationX, mTranslationY);
5742 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5743 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5744 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005745 if (mCamera == null) {
5746 mCamera = new Camera();
5747 matrix3D = new Matrix();
5748 }
5749 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005750 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08005751 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005752 mCamera.getMatrix(matrix3D);
5753 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005754 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005755 mMatrix.postConcat(matrix3D);
5756 mCamera.restore();
5757 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005758 mMatrixDirty = false;
5759 mMatrixIsIdentity = mMatrix.isIdentity();
5760 mInverseMatrixDirty = true;
5761 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005762 }
5763
5764 /**
5765 * Utility method to retrieve the inverse of the current mMatrix property.
5766 * We cache the matrix to avoid recalculating it when transform properties
5767 * have not changed.
5768 *
5769 * @return The inverse of the current matrix of this view.
5770 */
Jeff Brown86671742010-09-30 20:00:15 -07005771 final Matrix getInverseMatrix() {
5772 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005773 if (mInverseMatrixDirty) {
5774 if (mInverseMatrix == null) {
5775 mInverseMatrix = new Matrix();
5776 }
5777 mMatrix.invert(mInverseMatrix);
5778 mInverseMatrixDirty = false;
5779 }
5780 return mInverseMatrix;
5781 }
5782
5783 /**
5784 * The degrees that the view is rotated around the pivot point.
5785 *
5786 * @see #getPivotX()
5787 * @see #getPivotY()
5788 * @return The degrees of rotation.
5789 */
5790 public float getRotation() {
5791 return mRotation;
5792 }
5793
5794 /**
Chet Haase897247b2010-09-09 14:54:47 -07005795 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5796 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005797 *
5798 * @param rotation The degrees of rotation.
5799 * @see #getPivotX()
5800 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005801 *
5802 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005803 */
5804 public void setRotation(float rotation) {
5805 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005806 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005807 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005808 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005809 mRotation = rotation;
5810 mMatrixDirty = true;
5811 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005812 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005813 }
5814 }
5815
5816 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005817 * The degrees that the view is rotated around the vertical axis through the pivot point.
5818 *
5819 * @see #getPivotX()
5820 * @see #getPivotY()
5821 * @return The degrees of Y rotation.
5822 */
5823 public float getRotationY() {
5824 return mRotationY;
5825 }
5826
5827 /**
Chet Haase897247b2010-09-09 14:54:47 -07005828 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5829 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5830 * down the y axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005831 *
5832 * @param rotationY The degrees of Y rotation.
5833 * @see #getPivotX()
5834 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005835 *
5836 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005837 */
5838 public void setRotationY(float rotationY) {
5839 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005840 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005841 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005842 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005843 mRotationY = rotationY;
5844 mMatrixDirty = true;
5845 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005846 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005847 }
5848 }
5849
5850 /**
5851 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5852 *
5853 * @see #getPivotX()
5854 * @see #getPivotY()
5855 * @return The degrees of X rotation.
5856 */
5857 public float getRotationX() {
5858 return mRotationX;
5859 }
5860
5861 /**
Chet Haase897247b2010-09-09 14:54:47 -07005862 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
5863 * Increasing values result in clockwise rotation from the viewpoint of looking down the
5864 * x axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005865 *
5866 * @param rotationX The degrees of X rotation.
5867 * @see #getPivotX()
5868 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005869 *
5870 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07005871 */
5872 public void setRotationX(float rotationX) {
5873 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005874 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005875 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005876 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005877 mRotationX = rotationX;
5878 mMatrixDirty = true;
5879 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005880 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005881 }
5882 }
5883
5884 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005885 * The amount that the view is scaled in x around the pivot point, as a proportion of
5886 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
5887 *
Joe Onorato93162322010-09-16 15:42:01 -04005888 * <p>By default, this is 1.0f.
5889 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005890 * @see #getPivotX()
5891 * @see #getPivotY()
5892 * @return The scaling factor.
5893 */
5894 public float getScaleX() {
5895 return mScaleX;
5896 }
5897
5898 /**
5899 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
5900 * the view's unscaled width. A value of 1 means that no scaling is applied.
5901 *
5902 * @param scaleX The scaling factor.
5903 * @see #getPivotX()
5904 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005905 *
5906 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07005907 */
5908 public void setScaleX(float scaleX) {
5909 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005910 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005911 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005912 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005913 mScaleX = scaleX;
5914 mMatrixDirty = true;
5915 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005916 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005917 }
5918 }
5919
5920 /**
5921 * The amount that the view is scaled in y around the pivot point, as a proportion of
5922 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
5923 *
Joe Onorato93162322010-09-16 15:42:01 -04005924 * <p>By default, this is 1.0f.
5925 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005926 * @see #getPivotX()
5927 * @see #getPivotY()
5928 * @return The scaling factor.
5929 */
5930 public float getScaleY() {
5931 return mScaleY;
5932 }
5933
5934 /**
5935 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
5936 * the view's unscaled width. A value of 1 means that no scaling is applied.
5937 *
5938 * @param scaleY The scaling factor.
5939 * @see #getPivotX()
5940 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005941 *
5942 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07005943 */
5944 public void setScaleY(float scaleY) {
5945 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005946 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005947 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005948 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005949 mScaleY = scaleY;
5950 mMatrixDirty = true;
5951 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005952 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005953 }
5954 }
5955
5956 /**
5957 * The x location of the point around which the view is {@link #setRotation(float) rotated}
5958 * and {@link #setScaleX(float) scaled}.
5959 *
5960 * @see #getRotation()
5961 * @see #getScaleX()
5962 * @see #getScaleY()
5963 * @see #getPivotY()
5964 * @return The x location of the pivot point.
5965 */
5966 public float getPivotX() {
5967 return mPivotX;
5968 }
5969
5970 /**
5971 * Sets the x location of the point around which the view is
5972 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07005973 * By default, the pivot point is centered on the object.
5974 * Setting this property disables this behavior and causes the view to use only the
5975 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07005976 *
5977 * @param pivotX The x location of the pivot point.
5978 * @see #getRotation()
5979 * @see #getScaleX()
5980 * @see #getScaleY()
5981 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005982 *
5983 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07005984 */
5985 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005986 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07005987 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005988 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005989 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005990 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005991 mPivotX = pivotX;
5992 mMatrixDirty = true;
5993 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005994 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005995 }
5996 }
5997
5998 /**
5999 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6000 * and {@link #setScaleY(float) scaled}.
6001 *
6002 * @see #getRotation()
6003 * @see #getScaleX()
6004 * @see #getScaleY()
6005 * @see #getPivotY()
6006 * @return The y location of the pivot point.
6007 */
6008 public float getPivotY() {
6009 return mPivotY;
6010 }
6011
6012 /**
6013 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006014 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6015 * Setting this property disables this behavior and causes the view to use only the
6016 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006017 *
6018 * @param pivotY The y location of the pivot point.
6019 * @see #getRotation()
6020 * @see #getScaleX()
6021 * @see #getScaleY()
6022 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006023 *
6024 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006025 */
6026 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006027 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006028 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006029 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006030 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006031 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006032 mPivotY = pivotY;
6033 mMatrixDirty = true;
6034 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006035 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006036 }
6037 }
6038
6039 /**
6040 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6041 * completely transparent and 1 means the view is completely opaque.
6042 *
Joe Onorato93162322010-09-16 15:42:01 -04006043 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006044 * @return The opacity of the view.
6045 */
6046 public float getAlpha() {
6047 return mAlpha;
6048 }
6049
6050 /**
Romain Guy171c5922011-01-06 10:04:23 -08006051 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6052 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006053 *
Romain Guy171c5922011-01-06 10:04:23 -08006054 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6055 * responsible for applying the opacity itself. Otherwise, calling this method is
6056 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006057 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006058 *
6059 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006060 *
Joe Malin32736f02011-01-19 16:14:20 -08006061 * @see #setLayerType(int, android.graphics.Paint)
6062 *
Chet Haase73066682010-11-29 15:55:32 -08006063 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006064 */
6065 public void setAlpha(float alpha) {
6066 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006067 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006068 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006069 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006070 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006071 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006072 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006073 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006074 invalidate(false);
6075 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006076 }
6077
6078 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006079 * Top position of this view relative to its parent.
6080 *
6081 * @return The top of this view, in pixels.
6082 */
6083 @ViewDebug.CapturedViewProperty
6084 public final int getTop() {
6085 return mTop;
6086 }
6087
6088 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006089 * Sets the top position of this view relative to its parent. This method is meant to be called
6090 * by the layout system and should not generally be called otherwise, because the property
6091 * may be changed at any time by the layout.
6092 *
6093 * @param top The top of this view, in pixels.
6094 */
6095 public final void setTop(int top) {
6096 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006097 updateMatrix();
6098 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006099 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006100 int minTop;
6101 int yLoc;
6102 if (top < mTop) {
6103 minTop = top;
6104 yLoc = top - mTop;
6105 } else {
6106 minTop = mTop;
6107 yLoc = 0;
6108 }
Chet Haasee9140a72011-02-16 16:23:29 -08006109 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006110 }
6111 } else {
6112 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006113 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006114 }
6115
Chet Haaseed032702010-10-01 14:05:54 -07006116 int width = mRight - mLeft;
6117 int oldHeight = mBottom - mTop;
6118
Chet Haase21cd1382010-09-01 17:42:29 -07006119 mTop = top;
6120
Chet Haaseed032702010-10-01 14:05:54 -07006121 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6122
Chet Haase21cd1382010-09-01 17:42:29 -07006123 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006124 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6125 // A change in dimension means an auto-centered pivot point changes, too
6126 mMatrixDirty = true;
6127 }
Chet Haase21cd1382010-09-01 17:42:29 -07006128 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006129 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006130 }
Chet Haase55dbb652010-12-21 20:15:08 -08006131 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006132 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006133 }
6134 }
6135
6136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137 * Bottom position of this view relative to its parent.
6138 *
6139 * @return The bottom of this view, in pixels.
6140 */
6141 @ViewDebug.CapturedViewProperty
6142 public final int getBottom() {
6143 return mBottom;
6144 }
6145
6146 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006147 * True if this view has changed since the last time being drawn.
6148 *
6149 * @return The dirty state of this view.
6150 */
6151 public boolean isDirty() {
6152 return (mPrivateFlags & DIRTY_MASK) != 0;
6153 }
6154
6155 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006156 * Sets the bottom position of this view relative to its parent. This method is meant to be
6157 * called by the layout system and should not generally be called otherwise, because the
6158 * property may be changed at any time by the layout.
6159 *
6160 * @param bottom The bottom of this view, in pixels.
6161 */
6162 public final void setBottom(int bottom) {
6163 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006164 updateMatrix();
6165 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006166 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006167 int maxBottom;
6168 if (bottom < mBottom) {
6169 maxBottom = mBottom;
6170 } else {
6171 maxBottom = bottom;
6172 }
Chet Haasee9140a72011-02-16 16:23:29 -08006173 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006174 }
6175 } else {
6176 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006177 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006178 }
6179
Chet Haaseed032702010-10-01 14:05:54 -07006180 int width = mRight - mLeft;
6181 int oldHeight = mBottom - mTop;
6182
Chet Haase21cd1382010-09-01 17:42:29 -07006183 mBottom = bottom;
6184
Chet Haaseed032702010-10-01 14:05:54 -07006185 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6186
Chet Haase21cd1382010-09-01 17:42:29 -07006187 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006188 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6189 // A change in dimension means an auto-centered pivot point changes, too
6190 mMatrixDirty = true;
6191 }
Chet Haase21cd1382010-09-01 17:42:29 -07006192 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006193 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006194 }
Chet Haase55dbb652010-12-21 20:15:08 -08006195 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006196 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006197 }
6198 }
6199
6200 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 * Left position of this view relative to its parent.
6202 *
6203 * @return The left edge of this view, in pixels.
6204 */
6205 @ViewDebug.CapturedViewProperty
6206 public final int getLeft() {
6207 return mLeft;
6208 }
6209
6210 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006211 * Sets the left position of this view relative to its parent. This method is meant to be called
6212 * by the layout system and should not generally be called otherwise, because the property
6213 * may be changed at any time by the layout.
6214 *
6215 * @param left The bottom of this view, in pixels.
6216 */
6217 public final void setLeft(int left) {
6218 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006219 updateMatrix();
6220 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006221 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006222 int minLeft;
6223 int xLoc;
6224 if (left < mLeft) {
6225 minLeft = left;
6226 xLoc = left - mLeft;
6227 } else {
6228 minLeft = mLeft;
6229 xLoc = 0;
6230 }
Chet Haasee9140a72011-02-16 16:23:29 -08006231 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006232 }
6233 } else {
6234 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006235 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006236 }
6237
Chet Haaseed032702010-10-01 14:05:54 -07006238 int oldWidth = mRight - mLeft;
6239 int height = mBottom - mTop;
6240
Chet Haase21cd1382010-09-01 17:42:29 -07006241 mLeft = left;
6242
Chet Haaseed032702010-10-01 14:05:54 -07006243 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6244
Chet Haase21cd1382010-09-01 17:42:29 -07006245 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006246 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6247 // A change in dimension means an auto-centered pivot point changes, too
6248 mMatrixDirty = true;
6249 }
Chet Haase21cd1382010-09-01 17:42:29 -07006250 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006251 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006252 }
Chet Haase55dbb652010-12-21 20:15:08 -08006253 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006254 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006255 }
6256 }
6257
6258 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006259 * Right position of this view relative to its parent.
6260 *
6261 * @return The right edge of this view, in pixels.
6262 */
6263 @ViewDebug.CapturedViewProperty
6264 public final int getRight() {
6265 return mRight;
6266 }
6267
6268 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006269 * Sets the right position of this view relative to its parent. This method is meant to be called
6270 * by the layout system and should not generally be called otherwise, because the property
6271 * may be changed at any time by the layout.
6272 *
6273 * @param right The bottom of this view, in pixels.
6274 */
6275 public final void setRight(int right) {
6276 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006277 updateMatrix();
6278 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006279 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006280 int maxRight;
6281 if (right < mRight) {
6282 maxRight = mRight;
6283 } else {
6284 maxRight = right;
6285 }
Chet Haasee9140a72011-02-16 16:23:29 -08006286 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006287 }
6288 } else {
6289 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006290 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006291 }
6292
Chet Haaseed032702010-10-01 14:05:54 -07006293 int oldWidth = mRight - mLeft;
6294 int height = mBottom - mTop;
6295
Chet Haase21cd1382010-09-01 17:42:29 -07006296 mRight = right;
6297
Chet Haaseed032702010-10-01 14:05:54 -07006298 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6299
Chet Haase21cd1382010-09-01 17:42:29 -07006300 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006301 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6302 // A change in dimension means an auto-centered pivot point changes, too
6303 mMatrixDirty = true;
6304 }
Chet Haase21cd1382010-09-01 17:42:29 -07006305 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006306 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006307 }
Chet Haase55dbb652010-12-21 20:15:08 -08006308 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006309 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006310 }
6311 }
6312
6313 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006314 * The visual x position of this view, in pixels. This is equivalent to the
6315 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006316 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006317 *
Chet Haasedf030d22010-07-30 17:22:38 -07006318 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006319 */
Chet Haasedf030d22010-07-30 17:22:38 -07006320 public float getX() {
6321 return mLeft + mTranslationX;
6322 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006323
Chet Haasedf030d22010-07-30 17:22:38 -07006324 /**
6325 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6326 * {@link #setTranslationX(float) translationX} property to be the difference between
6327 * the x value passed in and the current {@link #getLeft() left} property.
6328 *
6329 * @param x The visual x position of this view, in pixels.
6330 */
6331 public void setX(float x) {
6332 setTranslationX(x - mLeft);
6333 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006334
Chet Haasedf030d22010-07-30 17:22:38 -07006335 /**
6336 * The visual y position of this view, in pixels. This is equivalent to the
6337 * {@link #setTranslationY(float) translationY} property plus the current
6338 * {@link #getTop() top} property.
6339 *
6340 * @return The visual y position of this view, in pixels.
6341 */
6342 public float getY() {
6343 return mTop + mTranslationY;
6344 }
6345
6346 /**
6347 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6348 * {@link #setTranslationY(float) translationY} property to be the difference between
6349 * the y value passed in and the current {@link #getTop() top} property.
6350 *
6351 * @param y The visual y position of this view, in pixels.
6352 */
6353 public void setY(float y) {
6354 setTranslationY(y - mTop);
6355 }
6356
6357
6358 /**
6359 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6360 * This position is post-layout, in addition to wherever the object's
6361 * layout placed it.
6362 *
6363 * @return The horizontal position of this view relative to its left position, in pixels.
6364 */
6365 public float getTranslationX() {
6366 return mTranslationX;
6367 }
6368
6369 /**
6370 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6371 * This effectively positions the object post-layout, in addition to wherever the object's
6372 * layout placed it.
6373 *
6374 * @param translationX The horizontal position of this view relative to its left position,
6375 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006376 *
6377 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006378 */
6379 public void setTranslationX(float translationX) {
6380 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006381 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006382 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006383 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006384 mTranslationX = translationX;
6385 mMatrixDirty = true;
6386 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006387 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006388 }
6389 }
6390
6391 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006392 * The horizontal location of this view relative to its {@link #getTop() top} position.
6393 * This position is post-layout, in addition to wherever the object's
6394 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006395 *
Chet Haasedf030d22010-07-30 17:22:38 -07006396 * @return The vertical position of this view relative to its top position,
6397 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006398 */
Chet Haasedf030d22010-07-30 17:22:38 -07006399 public float getTranslationY() {
6400 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006401 }
6402
6403 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006404 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6405 * This effectively positions the object post-layout, in addition to wherever the object's
6406 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006407 *
Chet Haasedf030d22010-07-30 17:22:38 -07006408 * @param translationY The vertical position of this view relative to its top position,
6409 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006410 *
6411 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006412 */
Chet Haasedf030d22010-07-30 17:22:38 -07006413 public void setTranslationY(float translationY) {
6414 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006415 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006416 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006417 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006418 mTranslationY = translationY;
6419 mMatrixDirty = true;
6420 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006421 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006422 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006423 }
6424
6425 /**
Romain Guyda489792011-02-03 01:05:15 -08006426 * @hide
6427 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006428 public void setFastTranslationX(float x) {
6429 mTranslationX = x;
6430 mMatrixDirty = true;
6431 }
6432
6433 /**
6434 * @hide
6435 */
6436 public void setFastTranslationY(float y) {
6437 mTranslationY = y;
6438 mMatrixDirty = true;
6439 }
6440
6441 /**
6442 * @hide
6443 */
Romain Guyda489792011-02-03 01:05:15 -08006444 public void setFastX(float x) {
6445 mTranslationX = x - mLeft;
6446 mMatrixDirty = true;
6447 }
6448
6449 /**
6450 * @hide
6451 */
6452 public void setFastY(float y) {
6453 mTranslationY = y - mTop;
6454 mMatrixDirty = true;
6455 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006456
Romain Guyda489792011-02-03 01:05:15 -08006457 /**
6458 * @hide
6459 */
6460 public void setFastScaleX(float x) {
6461 mScaleX = x;
6462 mMatrixDirty = true;
6463 }
6464
6465 /**
6466 * @hide
6467 */
6468 public void setFastScaleY(float y) {
6469 mScaleY = y;
6470 mMatrixDirty = true;
6471 }
6472
6473 /**
6474 * @hide
6475 */
6476 public void setFastAlpha(float alpha) {
6477 mAlpha = alpha;
6478 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006479
Romain Guyda489792011-02-03 01:05:15 -08006480 /**
6481 * @hide
6482 */
6483 public void setFastRotationY(float y) {
6484 mRotationY = y;
6485 mMatrixDirty = true;
6486 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006487
Romain Guyda489792011-02-03 01:05:15 -08006488 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006489 * Hit rectangle in parent's coordinates
6490 *
6491 * @param outRect The hit rectangle of the view.
6492 */
6493 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006494 updateMatrix();
6495 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006496 outRect.set(mLeft, mTop, mRight, mBottom);
6497 } else {
6498 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006499 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006500 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006501 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6502 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006503 }
6504 }
6505
6506 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006507 * Determines whether the given point, in local coordinates is inside the view.
6508 */
6509 /*package*/ final boolean pointInView(float localX, float localY) {
6510 return localX >= 0 && localX < (mRight - mLeft)
6511 && localY >= 0 && localY < (mBottom - mTop);
6512 }
6513
6514 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006515 * Utility method to determine whether the given point, in local coordinates,
6516 * is inside the view, where the area of the view is expanded by the slop factor.
6517 * This method is called while processing touch-move events to determine if the event
6518 * is still within the view.
6519 */
6520 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006521 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006522 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006523 }
6524
6525 /**
6526 * When a view has focus and the user navigates away from it, the next view is searched for
6527 * starting from the rectangle filled in by this method.
6528 *
6529 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6530 * view maintains some idea of internal selection, such as a cursor, or a selected row
6531 * or column, you should override this method and fill in a more specific rectangle.
6532 *
6533 * @param r The rectangle to fill in, in this view's coordinates.
6534 */
6535 public void getFocusedRect(Rect r) {
6536 getDrawingRect(r);
6537 }
6538
6539 /**
6540 * If some part of this view is not clipped by any of its parents, then
6541 * return that area in r in global (root) coordinates. To convert r to local
6542 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6543 * -globalOffset.y)) If the view is completely clipped or translated out,
6544 * return false.
6545 *
6546 * @param r If true is returned, r holds the global coordinates of the
6547 * visible portion of this view.
6548 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6549 * between this view and its root. globalOffet may be null.
6550 * @return true if r is non-empty (i.e. part of the view is visible at the
6551 * root level.
6552 */
6553 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6554 int width = mRight - mLeft;
6555 int height = mBottom - mTop;
6556 if (width > 0 && height > 0) {
6557 r.set(0, 0, width, height);
6558 if (globalOffset != null) {
6559 globalOffset.set(-mScrollX, -mScrollY);
6560 }
6561 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6562 }
6563 return false;
6564 }
6565
6566 public final boolean getGlobalVisibleRect(Rect r) {
6567 return getGlobalVisibleRect(r, null);
6568 }
6569
6570 public final boolean getLocalVisibleRect(Rect r) {
6571 Point offset = new Point();
6572 if (getGlobalVisibleRect(r, offset)) {
6573 r.offset(-offset.x, -offset.y); // make r local
6574 return true;
6575 }
6576 return false;
6577 }
6578
6579 /**
6580 * Offset this view's vertical location by the specified number of pixels.
6581 *
6582 * @param offset the number of pixels to offset the view by
6583 */
6584 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006585 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006586 updateMatrix();
6587 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006588 final ViewParent p = mParent;
6589 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006591 int minTop;
6592 int maxBottom;
6593 int yLoc;
6594 if (offset < 0) {
6595 minTop = mTop + offset;
6596 maxBottom = mBottom;
6597 yLoc = offset;
6598 } else {
6599 minTop = mTop;
6600 maxBottom = mBottom + offset;
6601 yLoc = 0;
6602 }
6603 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6604 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006605 }
6606 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006607 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006608 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006609
Chet Haasec3aa3612010-06-17 08:50:37 -07006610 mTop += offset;
6611 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006612
Chet Haasec3aa3612010-06-17 08:50:37 -07006613 if (!mMatrixIsIdentity) {
6614 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006615 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006616 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006617 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006619 }
6620
6621 /**
6622 * Offset this view's horizontal location by the specified amount of pixels.
6623 *
6624 * @param offset the numer of pixels to offset the view by
6625 */
6626 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006627 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006628 updateMatrix();
6629 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006630 final ViewParent p = mParent;
6631 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006632 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006633 int minLeft;
6634 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006635 if (offset < 0) {
6636 minLeft = mLeft + offset;
6637 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006638 } else {
6639 minLeft = mLeft;
6640 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006641 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006642 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006643 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006644 }
6645 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006646 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006647 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006648
Chet Haasec3aa3612010-06-17 08:50:37 -07006649 mLeft += offset;
6650 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006651
Chet Haasec3aa3612010-06-17 08:50:37 -07006652 if (!mMatrixIsIdentity) {
6653 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006654 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006655 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006656 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006657 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658 }
6659
6660 /**
6661 * Get the LayoutParams associated with this view. All views should have
6662 * layout parameters. These supply parameters to the <i>parent</i> of this
6663 * view specifying how it should be arranged. There are many subclasses of
6664 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6665 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006666 *
6667 * This method may return null if this View is not attached to a parent
6668 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6669 * was not invoked successfully. When a View is attached to a parent
6670 * ViewGroup, this method must not return null.
6671 *
6672 * @return The LayoutParams associated with this view, or null if no
6673 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006675 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006676 public ViewGroup.LayoutParams getLayoutParams() {
6677 return mLayoutParams;
6678 }
6679
6680 /**
6681 * Set the layout parameters associated with this view. These supply
6682 * parameters to the <i>parent</i> of this view specifying how it should be
6683 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6684 * correspond to the different subclasses of ViewGroup that are responsible
6685 * for arranging their children.
6686 *
Romain Guy01c174b2011-02-22 11:51:06 -08006687 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 */
6689 public void setLayoutParams(ViewGroup.LayoutParams params) {
6690 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08006691 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693 mLayoutParams = params;
6694 requestLayout();
6695 }
6696
6697 /**
6698 * Set the scrolled position of your view. This will cause a call to
6699 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6700 * invalidated.
6701 * @param x the x position to scroll to
6702 * @param y the y position to scroll to
6703 */
6704 public void scrollTo(int x, int y) {
6705 if (mScrollX != x || mScrollY != y) {
6706 int oldX = mScrollX;
6707 int oldY = mScrollY;
6708 mScrollX = x;
6709 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006710 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006711 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006712 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006713 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006715 }
6716 }
6717
6718 /**
6719 * Move the scrolled position of your view. This will cause a call to
6720 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6721 * invalidated.
6722 * @param x the amount of pixels to scroll by horizontally
6723 * @param y the amount of pixels to scroll by vertically
6724 */
6725 public void scrollBy(int x, int y) {
6726 scrollTo(mScrollX + x, mScrollY + y);
6727 }
6728
6729 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006730 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6731 * animation to fade the scrollbars out after a default delay. If a subclass
6732 * provides animated scrolling, the start delay should equal the duration
6733 * of the scrolling animation.</p>
6734 *
6735 * <p>The animation starts only if at least one of the scrollbars is
6736 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6737 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6738 * this method returns true, and false otherwise. If the animation is
6739 * started, this method calls {@link #invalidate()}; in that case the
6740 * caller should not call {@link #invalidate()}.</p>
6741 *
6742 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006743 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006744 *
6745 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6746 * and {@link #scrollTo(int, int)}.</p>
6747 *
6748 * @return true if the animation is played, false otherwise
6749 *
6750 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006751 * @see #scrollBy(int, int)
6752 * @see #scrollTo(int, int)
6753 * @see #isHorizontalScrollBarEnabled()
6754 * @see #isVerticalScrollBarEnabled()
6755 * @see #setHorizontalScrollBarEnabled(boolean)
6756 * @see #setVerticalScrollBarEnabled(boolean)
6757 */
6758 protected boolean awakenScrollBars() {
6759 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006760 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006761 }
6762
6763 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006764 * Trigger the scrollbars to draw.
6765 * This method differs from awakenScrollBars() only in its default duration.
6766 * initialAwakenScrollBars() will show the scroll bars for longer than
6767 * usual to give the user more of a chance to notice them.
6768 *
6769 * @return true if the animation is played, false otherwise.
6770 */
6771 private boolean initialAwakenScrollBars() {
6772 return mScrollCache != null &&
6773 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6774 }
6775
6776 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006777 * <p>
6778 * Trigger the scrollbars to draw. When invoked this method starts an
6779 * animation to fade the scrollbars out after a fixed delay. If a subclass
6780 * provides animated scrolling, the start delay should equal the duration of
6781 * the scrolling animation.
6782 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006783 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006784 * <p>
6785 * The animation starts only if at least one of the scrollbars is enabled,
6786 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6787 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6788 * this method returns true, and false otherwise. If the animation is
6789 * started, this method calls {@link #invalidate()}; in that case the caller
6790 * should not call {@link #invalidate()}.
6791 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006792 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006793 * <p>
6794 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006795 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006796 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006797 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006798 * @param startDelay the delay, in milliseconds, after which the animation
6799 * should start; when the delay is 0, the animation starts
6800 * immediately
6801 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006802 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006803 * @see #scrollBy(int, int)
6804 * @see #scrollTo(int, int)
6805 * @see #isHorizontalScrollBarEnabled()
6806 * @see #isVerticalScrollBarEnabled()
6807 * @see #setHorizontalScrollBarEnabled(boolean)
6808 * @see #setVerticalScrollBarEnabled(boolean)
6809 */
6810 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006811 return awakenScrollBars(startDelay, true);
6812 }
Joe Malin32736f02011-01-19 16:14:20 -08006813
Mike Cleron290947b2009-09-29 18:34:32 -07006814 /**
6815 * <p>
6816 * Trigger the scrollbars to draw. When invoked this method starts an
6817 * animation to fade the scrollbars out after a fixed delay. If a subclass
6818 * provides animated scrolling, the start delay should equal the duration of
6819 * the scrolling animation.
6820 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006821 *
Mike Cleron290947b2009-09-29 18:34:32 -07006822 * <p>
6823 * The animation starts only if at least one of the scrollbars is enabled,
6824 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6825 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6826 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006827 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006828 * is set to true; in that case the caller
6829 * should not call {@link #invalidate()}.
6830 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006831 *
Mike Cleron290947b2009-09-29 18:34:32 -07006832 * <p>
6833 * This method should be invoked everytime a subclass directly updates the
6834 * scroll parameters.
6835 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006836 *
Mike Cleron290947b2009-09-29 18:34:32 -07006837 * @param startDelay the delay, in milliseconds, after which the animation
6838 * should start; when the delay is 0, the animation starts
6839 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08006840 *
Mike Cleron290947b2009-09-29 18:34:32 -07006841 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08006842 *
Mike Cleron290947b2009-09-29 18:34:32 -07006843 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006844 *
Mike Cleron290947b2009-09-29 18:34:32 -07006845 * @see #scrollBy(int, int)
6846 * @see #scrollTo(int, int)
6847 * @see #isHorizontalScrollBarEnabled()
6848 * @see #isVerticalScrollBarEnabled()
6849 * @see #setHorizontalScrollBarEnabled(boolean)
6850 * @see #setVerticalScrollBarEnabled(boolean)
6851 */
6852 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006853 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08006854
Mike Cleronf116bf82009-09-27 19:14:12 -07006855 if (scrollCache == null || !scrollCache.fadeScrollBars) {
6856 return false;
6857 }
6858
6859 if (scrollCache.scrollBar == null) {
6860 scrollCache.scrollBar = new ScrollBarDrawable();
6861 }
6862
6863 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
6864
Mike Cleron290947b2009-09-29 18:34:32 -07006865 if (invalidate) {
6866 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08006867 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07006868 }
Mike Cleronf116bf82009-09-27 19:14:12 -07006869
6870 if (scrollCache.state == ScrollabilityCache.OFF) {
6871 // FIXME: this is copied from WindowManagerService.
6872 // We should get this value from the system when it
6873 // is possible to do so.
6874 final int KEY_REPEAT_FIRST_DELAY = 750;
6875 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
6876 }
6877
6878 // Tell mScrollCache when we should start fading. This may
6879 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07006880 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07006881 scrollCache.fadeStartTime = fadeStartTime;
6882 scrollCache.state = ScrollabilityCache.ON;
6883
6884 // Schedule our fader to run, unscheduling any old ones first
6885 if (mAttachInfo != null) {
6886 mAttachInfo.mHandler.removeCallbacks(scrollCache);
6887 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
6888 }
6889
6890 return true;
6891 }
6892
6893 return false;
6894 }
6895
6896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006897 * Mark the the area defined by dirty as needing to be drawn. If the view is
6898 * visible, {@link #onDraw} will be called at some point in the future.
6899 * This must be called from a UI thread. To call from a non-UI thread, call
6900 * {@link #postInvalidate()}.
6901 *
6902 * WARNING: This method is destructive to dirty.
6903 * @param dirty the rectangle representing the bounds of the dirty region
6904 */
6905 public void invalidate(Rect dirty) {
6906 if (ViewDebug.TRACE_HIERARCHY) {
6907 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6908 }
6909
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006910 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006911 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6912 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006913 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006914 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006915 final ViewParent p = mParent;
6916 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006917 //noinspection PointlessBooleanExpression,ConstantConditions
6918 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6919 if (p != null && ai != null && ai.mHardwareAccelerated) {
6920 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6921 // with a null dirty rect, which tells the ViewRoot to redraw everything
6922 p.invalidateChild(this, null);
6923 return;
6924 }
Romain Guyaf636eb2010-12-09 17:47:21 -08006925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 if (p != null && ai != null) {
6927 final int scrollX = mScrollX;
6928 final int scrollY = mScrollY;
6929 final Rect r = ai.mTmpInvalRect;
6930 r.set(dirty.left - scrollX, dirty.top - scrollY,
6931 dirty.right - scrollX, dirty.bottom - scrollY);
6932 mParent.invalidateChild(this, r);
6933 }
6934 }
6935 }
6936
6937 /**
6938 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
6939 * The coordinates of the dirty rect are relative to the view.
6940 * If the view is visible, {@link #onDraw} will be called at some point
6941 * in the future. This must be called from a UI thread. To call
6942 * from a non-UI thread, call {@link #postInvalidate()}.
6943 * @param l the left position of the dirty region
6944 * @param t the top position of the dirty region
6945 * @param r the right position of the dirty region
6946 * @param b the bottom position of the dirty region
6947 */
6948 public void invalidate(int l, int t, int r, int b) {
6949 if (ViewDebug.TRACE_HIERARCHY) {
6950 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6951 }
6952
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006953 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006954 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6955 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006956 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006957 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 final ViewParent p = mParent;
6959 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006960 //noinspection PointlessBooleanExpression,ConstantConditions
6961 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6962 if (p != null && ai != null && ai.mHardwareAccelerated) {
6963 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6964 // with a null dirty rect, which tells the ViewRoot to redraw everything
6965 p.invalidateChild(this, null);
6966 return;
6967 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08006968 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006969 if (p != null && ai != null && l < r && t < b) {
6970 final int scrollX = mScrollX;
6971 final int scrollY = mScrollY;
6972 final Rect tmpr = ai.mTmpInvalRect;
6973 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
6974 p.invalidateChild(this, tmpr);
6975 }
6976 }
6977 }
6978
6979 /**
6980 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
6981 * be called at some point in the future. This must be called from a
6982 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
6983 */
6984 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07006985 invalidate(true);
6986 }
Joe Malin32736f02011-01-19 16:14:20 -08006987
Chet Haaseed032702010-10-01 14:05:54 -07006988 /**
6989 * This is where the invalidate() work actually happens. A full invalidate()
6990 * causes the drawing cache to be invalidated, but this function can be called with
6991 * invalidateCache set to false to skip that invalidation step for cases that do not
6992 * need it (for example, a component that remains at the same dimensions with the same
6993 * content).
6994 *
6995 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
6996 * well. This is usually true for a full invalidate, but may be set to false if the
6997 * View's contents or dimensions have not changed.
6998 */
Romain Guy849d0a32011-02-01 17:20:48 -08006999 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007000 if (ViewDebug.TRACE_HIERARCHY) {
7001 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7002 }
7003
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007004 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007005 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007006 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7007 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007008 mPrivateFlags &= ~DRAWN;
7009 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007010 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007011 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007013 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007014 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007015 //noinspection PointlessBooleanExpression,ConstantConditions
7016 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7017 if (p != null && ai != null && ai.mHardwareAccelerated) {
7018 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7019 // with a null dirty rect, which tells the ViewRoot to redraw everything
7020 p.invalidateChild(this, null);
7021 return;
7022 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007023 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007024
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007025 if (p != null && ai != null) {
7026 final Rect r = ai.mTmpInvalRect;
7027 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7028 // Don't call invalidate -- we don't want to internally scroll
7029 // our own bounds
7030 p.invalidateChild(this, r);
7031 }
7032 }
7033 }
7034
7035 /**
Romain Guyda489792011-02-03 01:05:15 -08007036 * @hide
7037 */
7038 public void fastInvalidate() {
7039 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7040 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7041 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7042 if (mParent instanceof View) {
7043 ((View) mParent).mPrivateFlags |= INVALIDATED;
7044 }
7045 mPrivateFlags &= ~DRAWN;
7046 mPrivateFlags |= INVALIDATED;
7047 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7048 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7049 mParent.invalidateChild(this, null);
7050 }
7051 }
7052 }
7053
7054 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007055 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007056 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7057 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007058 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7059 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007060 *
7061 * @hide
7062 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007063 protected void invalidateParentCaches() {
7064 if (mParent instanceof View) {
7065 ((View) mParent).mPrivateFlags |= INVALIDATED;
7066 }
7067 }
Joe Malin32736f02011-01-19 16:14:20 -08007068
Romain Guy0fd89bf2011-01-26 15:41:30 -08007069 /**
7070 * Used to indicate that the parent of this view should be invalidated. This functionality
7071 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7072 * which is necessary when various parent-managed properties of the view change, such as
7073 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7074 * an invalidation event to the parent.
7075 *
7076 * @hide
7077 */
7078 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007079 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007080 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007081 }
7082 }
7083
7084 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007085 * Indicates whether this View is opaque. An opaque View guarantees that it will
7086 * draw all the pixels overlapping its bounds using a fully opaque color.
7087 *
7088 * Subclasses of View should override this method whenever possible to indicate
7089 * whether an instance is opaque. Opaque Views are treated in a special way by
7090 * the View hierarchy, possibly allowing it to perform optimizations during
7091 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007092 *
Romain Guy24443ea2009-05-11 11:56:30 -07007093 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007095 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007096 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007097 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7098 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007099 }
7100
Adam Powell20232d02010-12-08 21:08:53 -08007101 /**
7102 * @hide
7103 */
7104 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007105 // Opaque if:
7106 // - Has a background
7107 // - Background is opaque
7108 // - Doesn't have scrollbars or scrollbars are inside overlay
7109
7110 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7111 mPrivateFlags |= OPAQUE_BACKGROUND;
7112 } else {
7113 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7114 }
7115
7116 final int flags = mViewFlags;
7117 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7118 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7119 mPrivateFlags |= OPAQUE_SCROLLBARS;
7120 } else {
7121 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7122 }
7123 }
7124
7125 /**
7126 * @hide
7127 */
7128 protected boolean hasOpaqueScrollbars() {
7129 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007130 }
7131
7132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007133 * @return A handler associated with the thread running the View. This
7134 * handler can be used to pump events in the UI events queue.
7135 */
7136 public Handler getHandler() {
7137 if (mAttachInfo != null) {
7138 return mAttachInfo.mHandler;
7139 }
7140 return null;
7141 }
7142
7143 /**
7144 * Causes the Runnable to be added to the message queue.
7145 * The runnable will be run on the user interface thread.
7146 *
7147 * @param action The Runnable that will be executed.
7148 *
7149 * @return Returns true if the Runnable was successfully placed in to the
7150 * message queue. Returns false on failure, usually because the
7151 * looper processing the message queue is exiting.
7152 */
7153 public boolean post(Runnable action) {
7154 Handler handler;
7155 if (mAttachInfo != null) {
7156 handler = mAttachInfo.mHandler;
7157 } else {
7158 // Assume that post will succeed later
7159 ViewRoot.getRunQueue().post(action);
7160 return true;
7161 }
7162
7163 return handler.post(action);
7164 }
7165
7166 /**
7167 * Causes the Runnable to be added to the message queue, to be run
7168 * after the specified amount of time elapses.
7169 * The runnable will be run on the user interface thread.
7170 *
7171 * @param action The Runnable that will be executed.
7172 * @param delayMillis The delay (in milliseconds) until the Runnable
7173 * will be executed.
7174 *
7175 * @return true if the Runnable was successfully placed in to the
7176 * message queue. Returns false on failure, usually because the
7177 * looper processing the message queue is exiting. Note that a
7178 * result of true does not mean the Runnable will be processed --
7179 * if the looper is quit before the delivery time of the message
7180 * occurs then the message will be dropped.
7181 */
7182 public boolean postDelayed(Runnable action, long delayMillis) {
7183 Handler handler;
7184 if (mAttachInfo != null) {
7185 handler = mAttachInfo.mHandler;
7186 } else {
7187 // Assume that post will succeed later
7188 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7189 return true;
7190 }
7191
7192 return handler.postDelayed(action, delayMillis);
7193 }
7194
7195 /**
7196 * Removes the specified Runnable from the message queue.
7197 *
7198 * @param action The Runnable to remove from the message handling queue
7199 *
7200 * @return true if this view could ask the Handler to remove the Runnable,
7201 * false otherwise. When the returned value is true, the Runnable
7202 * may or may not have been actually removed from the message queue
7203 * (for instance, if the Runnable was not in the queue already.)
7204 */
7205 public boolean removeCallbacks(Runnable action) {
7206 Handler handler;
7207 if (mAttachInfo != null) {
7208 handler = mAttachInfo.mHandler;
7209 } else {
7210 // Assume that post will succeed later
7211 ViewRoot.getRunQueue().removeCallbacks(action);
7212 return true;
7213 }
7214
7215 handler.removeCallbacks(action);
7216 return true;
7217 }
7218
7219 /**
7220 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7221 * Use this to invalidate the View from a non-UI thread.
7222 *
7223 * @see #invalidate()
7224 */
7225 public void postInvalidate() {
7226 postInvalidateDelayed(0);
7227 }
7228
7229 /**
7230 * Cause an invalidate of the specified area to happen on a subsequent cycle
7231 * through the event loop. Use this to invalidate the View from a non-UI thread.
7232 *
7233 * @param left The left coordinate of the rectangle to invalidate.
7234 * @param top The top coordinate of the rectangle to invalidate.
7235 * @param right The right coordinate of the rectangle to invalidate.
7236 * @param bottom The bottom coordinate of the rectangle to invalidate.
7237 *
7238 * @see #invalidate(int, int, int, int)
7239 * @see #invalidate(Rect)
7240 */
7241 public void postInvalidate(int left, int top, int right, int bottom) {
7242 postInvalidateDelayed(0, left, top, right, bottom);
7243 }
7244
7245 /**
7246 * Cause an invalidate to happen on a subsequent cycle through the event
7247 * loop. Waits for the specified amount of time.
7248 *
7249 * @param delayMilliseconds the duration in milliseconds to delay the
7250 * invalidation by
7251 */
7252 public void postInvalidateDelayed(long delayMilliseconds) {
7253 // We try only with the AttachInfo because there's no point in invalidating
7254 // if we are not attached to our window
7255 if (mAttachInfo != null) {
7256 Message msg = Message.obtain();
7257 msg.what = AttachInfo.INVALIDATE_MSG;
7258 msg.obj = this;
7259 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7260 }
7261 }
7262
7263 /**
7264 * Cause an invalidate of the specified area to happen on a subsequent cycle
7265 * through the event loop. Waits for the specified amount of time.
7266 *
7267 * @param delayMilliseconds the duration in milliseconds to delay the
7268 * invalidation by
7269 * @param left The left coordinate of the rectangle to invalidate.
7270 * @param top The top coordinate of the rectangle to invalidate.
7271 * @param right The right coordinate of the rectangle to invalidate.
7272 * @param bottom The bottom coordinate of the rectangle to invalidate.
7273 */
7274 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7275 int right, int bottom) {
7276
7277 // We try only with the AttachInfo because there's no point in invalidating
7278 // if we are not attached to our window
7279 if (mAttachInfo != null) {
7280 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7281 info.target = this;
7282 info.left = left;
7283 info.top = top;
7284 info.right = right;
7285 info.bottom = bottom;
7286
7287 final Message msg = Message.obtain();
7288 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7289 msg.obj = info;
7290 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7291 }
7292 }
7293
7294 /**
7295 * Called by a parent to request that a child update its values for mScrollX
7296 * and mScrollY if necessary. This will typically be done if the child is
7297 * animating a scroll using a {@link android.widget.Scroller Scroller}
7298 * object.
7299 */
7300 public void computeScroll() {
7301 }
7302
7303 /**
7304 * <p>Indicate whether the horizontal edges are faded when the view is
7305 * scrolled horizontally.</p>
7306 *
7307 * @return true if the horizontal edges should are faded on scroll, false
7308 * otherwise
7309 *
7310 * @see #setHorizontalFadingEdgeEnabled(boolean)
7311 * @attr ref android.R.styleable#View_fadingEdge
7312 */
7313 public boolean isHorizontalFadingEdgeEnabled() {
7314 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7315 }
7316
7317 /**
7318 * <p>Define whether the horizontal edges should be faded when this view
7319 * is scrolled horizontally.</p>
7320 *
7321 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7322 * be faded when the view is scrolled
7323 * horizontally
7324 *
7325 * @see #isHorizontalFadingEdgeEnabled()
7326 * @attr ref android.R.styleable#View_fadingEdge
7327 */
7328 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7329 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7330 if (horizontalFadingEdgeEnabled) {
7331 initScrollCache();
7332 }
7333
7334 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7335 }
7336 }
7337
7338 /**
7339 * <p>Indicate whether the vertical edges are faded when the view is
7340 * scrolled horizontally.</p>
7341 *
7342 * @return true if the vertical edges should are faded on scroll, false
7343 * otherwise
7344 *
7345 * @see #setVerticalFadingEdgeEnabled(boolean)
7346 * @attr ref android.R.styleable#View_fadingEdge
7347 */
7348 public boolean isVerticalFadingEdgeEnabled() {
7349 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7350 }
7351
7352 /**
7353 * <p>Define whether the vertical edges should be faded when this view
7354 * is scrolled vertically.</p>
7355 *
7356 * @param verticalFadingEdgeEnabled true if the vertical edges should
7357 * be faded when the view is scrolled
7358 * vertically
7359 *
7360 * @see #isVerticalFadingEdgeEnabled()
7361 * @attr ref android.R.styleable#View_fadingEdge
7362 */
7363 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7364 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7365 if (verticalFadingEdgeEnabled) {
7366 initScrollCache();
7367 }
7368
7369 mViewFlags ^= FADING_EDGE_VERTICAL;
7370 }
7371 }
7372
7373 /**
7374 * Returns the strength, or intensity, of the top faded edge. The strength is
7375 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7376 * returns 0.0 or 1.0 but no value in between.
7377 *
7378 * Subclasses should override this method to provide a smoother fade transition
7379 * when scrolling occurs.
7380 *
7381 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7382 */
7383 protected float getTopFadingEdgeStrength() {
7384 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7385 }
7386
7387 /**
7388 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7389 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7390 * returns 0.0 or 1.0 but no value in between.
7391 *
7392 * Subclasses should override this method to provide a smoother fade transition
7393 * when scrolling occurs.
7394 *
7395 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7396 */
7397 protected float getBottomFadingEdgeStrength() {
7398 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7399 computeVerticalScrollRange() ? 1.0f : 0.0f;
7400 }
7401
7402 /**
7403 * Returns the strength, or intensity, of the left faded edge. The strength is
7404 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7405 * returns 0.0 or 1.0 but no value in between.
7406 *
7407 * Subclasses should override this method to provide a smoother fade transition
7408 * when scrolling occurs.
7409 *
7410 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7411 */
7412 protected float getLeftFadingEdgeStrength() {
7413 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7414 }
7415
7416 /**
7417 * Returns the strength, or intensity, of the right faded edge. The strength is
7418 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7419 * returns 0.0 or 1.0 but no value in between.
7420 *
7421 * Subclasses should override this method to provide a smoother fade transition
7422 * when scrolling occurs.
7423 *
7424 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7425 */
7426 protected float getRightFadingEdgeStrength() {
7427 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7428 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7429 }
7430
7431 /**
7432 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7433 * scrollbar is not drawn by default.</p>
7434 *
7435 * @return true if the horizontal scrollbar should be painted, false
7436 * otherwise
7437 *
7438 * @see #setHorizontalScrollBarEnabled(boolean)
7439 */
7440 public boolean isHorizontalScrollBarEnabled() {
7441 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7442 }
7443
7444 /**
7445 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7446 * scrollbar is not drawn by default.</p>
7447 *
7448 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7449 * be painted
7450 *
7451 * @see #isHorizontalScrollBarEnabled()
7452 */
7453 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7454 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7455 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007456 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007457 recomputePadding();
7458 }
7459 }
7460
7461 /**
7462 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7463 * scrollbar is not drawn by default.</p>
7464 *
7465 * @return true if the vertical scrollbar should be painted, false
7466 * otherwise
7467 *
7468 * @see #setVerticalScrollBarEnabled(boolean)
7469 */
7470 public boolean isVerticalScrollBarEnabled() {
7471 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7472 }
7473
7474 /**
7475 * <p>Define whether the vertical scrollbar should be drawn or not. The
7476 * scrollbar is not drawn by default.</p>
7477 *
7478 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7479 * be painted
7480 *
7481 * @see #isVerticalScrollBarEnabled()
7482 */
7483 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7484 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7485 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007486 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 recomputePadding();
7488 }
7489 }
7490
Adam Powell20232d02010-12-08 21:08:53 -08007491 /**
7492 * @hide
7493 */
7494 protected void recomputePadding() {
7495 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007496 }
Joe Malin32736f02011-01-19 16:14:20 -08007497
Mike Cleronfe81d382009-09-28 14:22:16 -07007498 /**
7499 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007500 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007501 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007502 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007503 */
7504 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7505 initScrollCache();
7506 final ScrollabilityCache scrollabilityCache = mScrollCache;
7507 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007508 if (fadeScrollbars) {
7509 scrollabilityCache.state = ScrollabilityCache.OFF;
7510 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007511 scrollabilityCache.state = ScrollabilityCache.ON;
7512 }
7513 }
Joe Malin32736f02011-01-19 16:14:20 -08007514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007515 /**
Joe Malin32736f02011-01-19 16:14:20 -08007516 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007517 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007518 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007519 * @return true if scrollbar fading is enabled
7520 */
7521 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007522 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007523 }
Joe Malin32736f02011-01-19 16:14:20 -08007524
Mike Cleron52f0a642009-09-28 18:21:37 -07007525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007526 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7527 * inset. When inset, they add to the padding of the view. And the scrollbars
7528 * can be drawn inside the padding area or on the edge of the view. For example,
7529 * if a view has a background drawable and you want to draw the scrollbars
7530 * inside the padding specified by the drawable, you can use
7531 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7532 * appear at the edge of the view, ignoring the padding, then you can use
7533 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7534 * @param style the style of the scrollbars. Should be one of
7535 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7536 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7537 * @see #SCROLLBARS_INSIDE_OVERLAY
7538 * @see #SCROLLBARS_INSIDE_INSET
7539 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7540 * @see #SCROLLBARS_OUTSIDE_INSET
7541 */
7542 public void setScrollBarStyle(int style) {
7543 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7544 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007545 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007546 recomputePadding();
7547 }
7548 }
7549
7550 /**
7551 * <p>Returns the current scrollbar style.</p>
7552 * @return the current scrollbar style
7553 * @see #SCROLLBARS_INSIDE_OVERLAY
7554 * @see #SCROLLBARS_INSIDE_INSET
7555 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7556 * @see #SCROLLBARS_OUTSIDE_INSET
7557 */
7558 public int getScrollBarStyle() {
7559 return mViewFlags & SCROLLBARS_STYLE_MASK;
7560 }
7561
7562 /**
7563 * <p>Compute the horizontal range that the horizontal scrollbar
7564 * represents.</p>
7565 *
7566 * <p>The range is expressed in arbitrary units that must be the same as the
7567 * units used by {@link #computeHorizontalScrollExtent()} and
7568 * {@link #computeHorizontalScrollOffset()}.</p>
7569 *
7570 * <p>The default range is the drawing width of this view.</p>
7571 *
7572 * @return the total horizontal range represented by the horizontal
7573 * scrollbar
7574 *
7575 * @see #computeHorizontalScrollExtent()
7576 * @see #computeHorizontalScrollOffset()
7577 * @see android.widget.ScrollBarDrawable
7578 */
7579 protected int computeHorizontalScrollRange() {
7580 return getWidth();
7581 }
7582
7583 /**
7584 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7585 * within the horizontal range. This value is used to compute the position
7586 * of the thumb within the scrollbar's track.</p>
7587 *
7588 * <p>The range is expressed in arbitrary units that must be the same as the
7589 * units used by {@link #computeHorizontalScrollRange()} and
7590 * {@link #computeHorizontalScrollExtent()}.</p>
7591 *
7592 * <p>The default offset is the scroll offset of this view.</p>
7593 *
7594 * @return the horizontal offset of the scrollbar's thumb
7595 *
7596 * @see #computeHorizontalScrollRange()
7597 * @see #computeHorizontalScrollExtent()
7598 * @see android.widget.ScrollBarDrawable
7599 */
7600 protected int computeHorizontalScrollOffset() {
7601 return mScrollX;
7602 }
7603
7604 /**
7605 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7606 * within the horizontal range. This value is used to compute the length
7607 * of the thumb within the scrollbar's track.</p>
7608 *
7609 * <p>The range is expressed in arbitrary units that must be the same as the
7610 * units used by {@link #computeHorizontalScrollRange()} and
7611 * {@link #computeHorizontalScrollOffset()}.</p>
7612 *
7613 * <p>The default extent is the drawing width of this view.</p>
7614 *
7615 * @return the horizontal extent of the scrollbar's thumb
7616 *
7617 * @see #computeHorizontalScrollRange()
7618 * @see #computeHorizontalScrollOffset()
7619 * @see android.widget.ScrollBarDrawable
7620 */
7621 protected int computeHorizontalScrollExtent() {
7622 return getWidth();
7623 }
7624
7625 /**
7626 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7627 *
7628 * <p>The range is expressed in arbitrary units that must be the same as the
7629 * units used by {@link #computeVerticalScrollExtent()} and
7630 * {@link #computeVerticalScrollOffset()}.</p>
7631 *
7632 * @return the total vertical range represented by the vertical scrollbar
7633 *
7634 * <p>The default range is the drawing height of this view.</p>
7635 *
7636 * @see #computeVerticalScrollExtent()
7637 * @see #computeVerticalScrollOffset()
7638 * @see android.widget.ScrollBarDrawable
7639 */
7640 protected int computeVerticalScrollRange() {
7641 return getHeight();
7642 }
7643
7644 /**
7645 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7646 * within the horizontal range. This value is used to compute the position
7647 * of the thumb within the scrollbar's track.</p>
7648 *
7649 * <p>The range is expressed in arbitrary units that must be the same as the
7650 * units used by {@link #computeVerticalScrollRange()} and
7651 * {@link #computeVerticalScrollExtent()}.</p>
7652 *
7653 * <p>The default offset is the scroll offset of this view.</p>
7654 *
7655 * @return the vertical offset of the scrollbar's thumb
7656 *
7657 * @see #computeVerticalScrollRange()
7658 * @see #computeVerticalScrollExtent()
7659 * @see android.widget.ScrollBarDrawable
7660 */
7661 protected int computeVerticalScrollOffset() {
7662 return mScrollY;
7663 }
7664
7665 /**
7666 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7667 * within the vertical range. This value is used to compute the length
7668 * of the thumb within the scrollbar's track.</p>
7669 *
7670 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007671 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 * {@link #computeVerticalScrollOffset()}.</p>
7673 *
7674 * <p>The default extent is the drawing height of this view.</p>
7675 *
7676 * @return the vertical extent of the scrollbar's thumb
7677 *
7678 * @see #computeVerticalScrollRange()
7679 * @see #computeVerticalScrollOffset()
7680 * @see android.widget.ScrollBarDrawable
7681 */
7682 protected int computeVerticalScrollExtent() {
7683 return getHeight();
7684 }
7685
7686 /**
7687 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7688 * scrollbars are painted only if they have been awakened first.</p>
7689 *
7690 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007691 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007692 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007693 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007694 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007695 // scrollbars are drawn only when the animation is running
7696 final ScrollabilityCache cache = mScrollCache;
7697 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007698
Mike Cleronf116bf82009-09-27 19:14:12 -07007699 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007700
Mike Cleronf116bf82009-09-27 19:14:12 -07007701 if (state == ScrollabilityCache.OFF) {
7702 return;
7703 }
Joe Malin32736f02011-01-19 16:14:20 -08007704
Mike Cleronf116bf82009-09-27 19:14:12 -07007705 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007706
Mike Cleronf116bf82009-09-27 19:14:12 -07007707 if (state == ScrollabilityCache.FADING) {
7708 // We're fading -- get our fade interpolation
7709 if (cache.interpolatorValues == null) {
7710 cache.interpolatorValues = new float[1];
7711 }
Joe Malin32736f02011-01-19 16:14:20 -08007712
Mike Cleronf116bf82009-09-27 19:14:12 -07007713 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007714
Mike Cleronf116bf82009-09-27 19:14:12 -07007715 // Stops the animation if we're done
7716 if (cache.scrollBarInterpolator.timeToValues(values) ==
7717 Interpolator.Result.FREEZE_END) {
7718 cache.state = ScrollabilityCache.OFF;
7719 } else {
7720 cache.scrollBar.setAlpha(Math.round(values[0]));
7721 }
Joe Malin32736f02011-01-19 16:14:20 -08007722
7723 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007724 // drawing. We only want this when we're fading so that
7725 // we prevent excessive redraws
7726 invalidate = true;
7727 } else {
7728 // We're just on -- but we may have been fading before so
7729 // reset alpha
7730 cache.scrollBar.setAlpha(255);
7731 }
7732
Joe Malin32736f02011-01-19 16:14:20 -08007733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007734 final int viewFlags = mViewFlags;
7735
7736 final boolean drawHorizontalScrollBar =
7737 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7738 final boolean drawVerticalScrollBar =
7739 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7740 && !isVerticalScrollBarHidden();
7741
7742 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7743 final int width = mRight - mLeft;
7744 final int height = mBottom - mTop;
7745
7746 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007747
Mike Reede8853fc2009-09-04 14:01:48 -04007748 final int scrollX = mScrollX;
7749 final int scrollY = mScrollY;
7750 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7751
Mike Cleronf116bf82009-09-27 19:14:12 -07007752 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007753
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007754 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007755 int size = scrollBar.getSize(false);
7756 if (size <= 0) {
7757 size = cache.scrollBarSize;
7758 }
7759
Mike Cleronf116bf82009-09-27 19:14:12 -07007760 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007761 computeHorizontalScrollOffset(),
7762 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007763 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007764 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007765 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007766 left = scrollX + (mPaddingLeft & inside);
7767 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7768 bottom = top + size;
7769 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7770 if (invalidate) {
7771 invalidate(left, top, right, bottom);
7772 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 }
7774
7775 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007776 int size = scrollBar.getSize(true);
7777 if (size <= 0) {
7778 size = cache.scrollBarSize;
7779 }
7780
Mike Reede8853fc2009-09-04 14:01:48 -04007781 scrollBar.setParameters(computeVerticalScrollRange(),
7782 computeVerticalScrollOffset(),
7783 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007784 switch (mVerticalScrollbarPosition) {
7785 default:
7786 case SCROLLBAR_POSITION_DEFAULT:
7787 case SCROLLBAR_POSITION_RIGHT:
7788 left = scrollX + width - size - (mUserPaddingRight & inside);
7789 break;
7790 case SCROLLBAR_POSITION_LEFT:
7791 left = scrollX + (mUserPaddingLeft & inside);
7792 break;
7793 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007794 top = scrollY + (mPaddingTop & inside);
7795 right = left + size;
7796 bottom = scrollY + height - (mUserPaddingBottom & inside);
7797 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7798 if (invalidate) {
7799 invalidate(left, top, right, bottom);
7800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007801 }
7802 }
7803 }
7804 }
Romain Guy8506ab42009-06-11 17:35:47 -07007805
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007807 * 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 -08007808 * FastScroller is visible.
7809 * @return whether to temporarily hide the vertical scrollbar
7810 * @hide
7811 */
7812 protected boolean isVerticalScrollBarHidden() {
7813 return false;
7814 }
7815
7816 /**
7817 * <p>Draw the horizontal scrollbar if
7818 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7819 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007820 * @param canvas the canvas on which to draw the scrollbar
7821 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 *
7823 * @see #isHorizontalScrollBarEnabled()
7824 * @see #computeHorizontalScrollRange()
7825 * @see #computeHorizontalScrollExtent()
7826 * @see #computeHorizontalScrollOffset()
7827 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07007828 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007829 */
Romain Guy8fb95422010-08-17 18:38:51 -07007830 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
7831 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007832 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007833 scrollBar.draw(canvas);
7834 }
Mike Reede8853fc2009-09-04 14:01:48 -04007835
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007836 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007837 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
7838 * returns true.</p>
7839 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007840 * @param canvas the canvas on which to draw the scrollbar
7841 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 *
7843 * @see #isVerticalScrollBarEnabled()
7844 * @see #computeVerticalScrollRange()
7845 * @see #computeVerticalScrollExtent()
7846 * @see #computeVerticalScrollOffset()
7847 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04007848 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007849 */
Romain Guy8fb95422010-08-17 18:38:51 -07007850 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
7851 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04007852 scrollBar.setBounds(l, t, r, b);
7853 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 }
7855
7856 /**
7857 * Implement this to do your drawing.
7858 *
7859 * @param canvas the canvas on which the background will be drawn
7860 */
7861 protected void onDraw(Canvas canvas) {
7862 }
7863
7864 /*
7865 * Caller is responsible for calling requestLayout if necessary.
7866 * (This allows addViewInLayout to not request a new layout.)
7867 */
7868 void assignParent(ViewParent parent) {
7869 if (mParent == null) {
7870 mParent = parent;
7871 } else if (parent == null) {
7872 mParent = null;
7873 } else {
7874 throw new RuntimeException("view " + this + " being added, but"
7875 + " it already has a parent");
7876 }
7877 }
7878
7879 /**
7880 * This is called when the view is attached to a window. At this point it
7881 * has a Surface and will start drawing. Note that this function is
7882 * guaranteed to be called before {@link #onDraw}, however it may be called
7883 * any time before the first onDraw -- including before or after
7884 * {@link #onMeasure}.
7885 *
7886 * @see #onDetachedFromWindow()
7887 */
7888 protected void onAttachedToWindow() {
7889 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
7890 mParent.requestTransparentRegion(this);
7891 }
Adam Powell8568c3a2010-04-19 14:26:11 -07007892 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
7893 initialAwakenScrollBars();
7894 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
7895 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08007896 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 }
7898
7899 /**
7900 * This is called when the view is detached from a window. At this point it
7901 * no longer has a surface for drawing.
7902 *
7903 * @see #onAttachedToWindow()
7904 */
7905 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08007906 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08007907
Romain Guya440b002010-02-24 15:57:54 -08007908 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05007909 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08007910 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08007911
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007912 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08007913
7914 if (mHardwareLayer != null) {
7915 mHardwareLayer.destroy();
7916 mHardwareLayer = null;
7917 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007918
Chet Haasedaf98e92011-01-10 14:10:36 -08007919 if (mDisplayList != null) {
7920 mDisplayList.invalidate();
7921 }
7922
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007923 if (mAttachInfo != null) {
7924 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
7925 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
7926 }
7927
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08007928 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007929 }
7930
7931 /**
7932 * @return The number of times this view has been attached to a window
7933 */
7934 protected int getWindowAttachCount() {
7935 return mWindowAttachCount;
7936 }
7937
7938 /**
7939 * Retrieve a unique token identifying the window this view is attached to.
7940 * @return Return the window's token for use in
7941 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
7942 */
7943 public IBinder getWindowToken() {
7944 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
7945 }
7946
7947 /**
7948 * Retrieve a unique token identifying the top-level "real" window of
7949 * the window that this view is attached to. That is, this is like
7950 * {@link #getWindowToken}, except if the window this view in is a panel
7951 * window (attached to another containing window), then the token of
7952 * the containing window is returned instead.
7953 *
7954 * @return Returns the associated window token, either
7955 * {@link #getWindowToken()} or the containing window's token.
7956 */
7957 public IBinder getApplicationWindowToken() {
7958 AttachInfo ai = mAttachInfo;
7959 if (ai != null) {
7960 IBinder appWindowToken = ai.mPanelParentWindowToken;
7961 if (appWindowToken == null) {
7962 appWindowToken = ai.mWindowToken;
7963 }
7964 return appWindowToken;
7965 }
7966 return null;
7967 }
7968
7969 /**
7970 * Retrieve private session object this view hierarchy is using to
7971 * communicate with the window manager.
7972 * @return the session object to communicate with the window manager
7973 */
7974 /*package*/ IWindowSession getWindowSession() {
7975 return mAttachInfo != null ? mAttachInfo.mSession : null;
7976 }
7977
7978 /**
7979 * @param info the {@link android.view.View.AttachInfo} to associated with
7980 * this view
7981 */
7982 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
7983 //System.out.println("Attached! " + this);
7984 mAttachInfo = info;
7985 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08007986 // We will need to evaluate the drawable state at least once.
7987 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 if (mFloatingTreeObserver != null) {
7989 info.mTreeObserver.merge(mFloatingTreeObserver);
7990 mFloatingTreeObserver = null;
7991 }
7992 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
7993 mAttachInfo.mScrollContainers.add(this);
7994 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
7995 }
7996 performCollectViewAttributes(visibility);
7997 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08007998
7999 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8000 mOnAttachStateChangeListeners;
8001 if (listeners != null && listeners.size() > 0) {
8002 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8003 // perform the dispatching. The iterator is a safe guard against listeners that
8004 // could mutate the list by calling the various add/remove methods. This prevents
8005 // the array from being modified while we iterate it.
8006 for (OnAttachStateChangeListener listener : listeners) {
8007 listener.onViewAttachedToWindow(this);
8008 }
8009 }
8010
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 int vis = info.mWindowVisibility;
8012 if (vis != GONE) {
8013 onWindowVisibilityChanged(vis);
8014 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008015 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8016 // If nobody has evaluated the drawable state yet, then do it now.
8017 refreshDrawableState();
8018 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008019 }
8020
8021 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008022 AttachInfo info = mAttachInfo;
8023 if (info != null) {
8024 int vis = info.mWindowVisibility;
8025 if (vis != GONE) {
8026 onWindowVisibilityChanged(GONE);
8027 }
8028 }
8029
8030 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008031
Adam Powell4afd62b2011-02-18 15:02:18 -08008032 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8033 mOnAttachStateChangeListeners;
8034 if (listeners != null && listeners.size() > 0) {
8035 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8036 // perform the dispatching. The iterator is a safe guard against listeners that
8037 // could mutate the list by calling the various add/remove methods. This prevents
8038 // the array from being modified while we iterate it.
8039 for (OnAttachStateChangeListener listener : listeners) {
8040 listener.onViewDetachedFromWindow(this);
8041 }
8042 }
8043
Romain Guy01d5edc2011-01-28 11:28:53 -08008044 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008045 mAttachInfo.mScrollContainers.remove(this);
8046 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8047 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008048
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049 mAttachInfo = null;
8050 }
8051
8052 /**
8053 * Store this view hierarchy's frozen state into the given container.
8054 *
8055 * @param container The SparseArray in which to save the view's state.
8056 *
8057 * @see #restoreHierarchyState
8058 * @see #dispatchSaveInstanceState
8059 * @see #onSaveInstanceState
8060 */
8061 public void saveHierarchyState(SparseArray<Parcelable> container) {
8062 dispatchSaveInstanceState(container);
8063 }
8064
8065 /**
8066 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8067 * May be overridden to modify how freezing happens to a view's children; for example, some
8068 * views may want to not store state for their children.
8069 *
8070 * @param container The SparseArray in which to save the view's state.
8071 *
8072 * @see #dispatchRestoreInstanceState
8073 * @see #saveHierarchyState
8074 * @see #onSaveInstanceState
8075 */
8076 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8077 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8078 mPrivateFlags &= ~SAVE_STATE_CALLED;
8079 Parcelable state = onSaveInstanceState();
8080 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8081 throw new IllegalStateException(
8082 "Derived class did not call super.onSaveInstanceState()");
8083 }
8084 if (state != null) {
8085 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8086 // + ": " + state);
8087 container.put(mID, state);
8088 }
8089 }
8090 }
8091
8092 /**
8093 * Hook allowing a view to generate a representation of its internal state
8094 * that can later be used to create a new instance with that same state.
8095 * This state should only contain information that is not persistent or can
8096 * not be reconstructed later. For example, you will never store your
8097 * current position on screen because that will be computed again when a
8098 * new instance of the view is placed in its view hierarchy.
8099 * <p>
8100 * Some examples of things you may store here: the current cursor position
8101 * in a text view (but usually not the text itself since that is stored in a
8102 * content provider or other persistent storage), the currently selected
8103 * item in a list view.
8104 *
8105 * @return Returns a Parcelable object containing the view's current dynamic
8106 * state, or null if there is nothing interesting to save. The
8107 * default implementation returns null.
8108 * @see #onRestoreInstanceState
8109 * @see #saveHierarchyState
8110 * @see #dispatchSaveInstanceState
8111 * @see #setSaveEnabled(boolean)
8112 */
8113 protected Parcelable onSaveInstanceState() {
8114 mPrivateFlags |= SAVE_STATE_CALLED;
8115 return BaseSavedState.EMPTY_STATE;
8116 }
8117
8118 /**
8119 * Restore this view hierarchy's frozen state from the given container.
8120 *
8121 * @param container The SparseArray which holds previously frozen states.
8122 *
8123 * @see #saveHierarchyState
8124 * @see #dispatchRestoreInstanceState
8125 * @see #onRestoreInstanceState
8126 */
8127 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8128 dispatchRestoreInstanceState(container);
8129 }
8130
8131 /**
8132 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8133 * children. May be overridden to modify how restoreing happens to a view's children; for
8134 * example, some views may want to not store state for their children.
8135 *
8136 * @param container The SparseArray which holds previously saved state.
8137 *
8138 * @see #dispatchSaveInstanceState
8139 * @see #restoreHierarchyState
8140 * @see #onRestoreInstanceState
8141 */
8142 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8143 if (mID != NO_ID) {
8144 Parcelable state = container.get(mID);
8145 if (state != null) {
8146 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8147 // + ": " + state);
8148 mPrivateFlags &= ~SAVE_STATE_CALLED;
8149 onRestoreInstanceState(state);
8150 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8151 throw new IllegalStateException(
8152 "Derived class did not call super.onRestoreInstanceState()");
8153 }
8154 }
8155 }
8156 }
8157
8158 /**
8159 * Hook allowing a view to re-apply a representation of its internal state that had previously
8160 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8161 * null state.
8162 *
8163 * @param state The frozen state that had previously been returned by
8164 * {@link #onSaveInstanceState}.
8165 *
8166 * @see #onSaveInstanceState
8167 * @see #restoreHierarchyState
8168 * @see #dispatchRestoreInstanceState
8169 */
8170 protected void onRestoreInstanceState(Parcelable state) {
8171 mPrivateFlags |= SAVE_STATE_CALLED;
8172 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008173 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8174 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008175 + "when two views of different type have the same id in the same hierarchy. "
8176 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008177 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008178 }
8179 }
8180
8181 /**
8182 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8183 *
8184 * @return the drawing start time in milliseconds
8185 */
8186 public long getDrawingTime() {
8187 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8188 }
8189
8190 /**
8191 * <p>Enables or disables the duplication of the parent's state into this view. When
8192 * duplication is enabled, this view gets its drawable state from its parent rather
8193 * than from its own internal properties.</p>
8194 *
8195 * <p>Note: in the current implementation, setting this property to true after the
8196 * view was added to a ViewGroup might have no effect at all. This property should
8197 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8198 *
8199 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8200 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008201 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008202 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8203 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008204 *
8205 * @param enabled True to enable duplication of the parent's drawable state, false
8206 * to disable it.
8207 *
8208 * @see #getDrawableState()
8209 * @see #isDuplicateParentStateEnabled()
8210 */
8211 public void setDuplicateParentStateEnabled(boolean enabled) {
8212 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8213 }
8214
8215 /**
8216 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8217 *
8218 * @return True if this view's drawable state is duplicated from the parent,
8219 * false otherwise
8220 *
8221 * @see #getDrawableState()
8222 * @see #setDuplicateParentStateEnabled(boolean)
8223 */
8224 public boolean isDuplicateParentStateEnabled() {
8225 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8226 }
8227
8228 /**
Romain Guy171c5922011-01-06 10:04:23 -08008229 * <p>Specifies the type of layer backing this view. The layer can be
8230 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8231 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008232 *
Romain Guy171c5922011-01-06 10:04:23 -08008233 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8234 * instance that controls how the layer is composed on screen. The following
8235 * properties of the paint are taken into account when composing the layer:</p>
8236 * <ul>
8237 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8238 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8239 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8240 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008241 *
Romain Guy171c5922011-01-06 10:04:23 -08008242 * <p>If this view has an alpha value set to < 1.0 by calling
8243 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8244 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8245 * equivalent to setting a hardware layer on this view and providing a paint with
8246 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008247 *
Romain Guy171c5922011-01-06 10:04:23 -08008248 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8249 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8250 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008251 *
Romain Guy171c5922011-01-06 10:04:23 -08008252 * @param layerType The ype of layer to use with this view, must be one of
8253 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8254 * {@link #LAYER_TYPE_HARDWARE}
8255 * @param paint The paint used to compose the layer. This argument is optional
8256 * and can be null. It is ignored when the layer type is
8257 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008258 *
8259 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008260 * @see #LAYER_TYPE_NONE
8261 * @see #LAYER_TYPE_SOFTWARE
8262 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008263 * @see #setAlpha(float)
8264 *
Romain Guy171c5922011-01-06 10:04:23 -08008265 * @attr ref android.R.styleable#View_layerType
8266 */
8267 public void setLayerType(int layerType, Paint paint) {
8268 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008269 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008270 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8271 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008272
Romain Guyd6cd5722011-01-17 14:42:41 -08008273 if (layerType == mLayerType) {
8274 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8275 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008276 invalidateParentCaches();
8277 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008278 }
8279 return;
8280 }
Romain Guy171c5922011-01-06 10:04:23 -08008281
8282 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008283 switch (mLayerType) {
8284 case LAYER_TYPE_SOFTWARE:
8285 if (mDrawingCache != null) {
8286 mDrawingCache.recycle();
8287 mDrawingCache = null;
8288 }
Joe Malin32736f02011-01-19 16:14:20 -08008289
Romain Guy6c319ca2011-01-11 14:29:25 -08008290 if (mUnscaledDrawingCache != null) {
8291 mUnscaledDrawingCache.recycle();
8292 mUnscaledDrawingCache = null;
8293 }
8294 break;
8295 case LAYER_TYPE_HARDWARE:
8296 if (mHardwareLayer != null) {
8297 mHardwareLayer.destroy();
8298 mHardwareLayer = null;
8299 }
8300 break;
8301 default:
8302 break;
Romain Guy171c5922011-01-06 10:04:23 -08008303 }
8304
8305 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008306 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8307 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8308 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008309
Romain Guy0fd89bf2011-01-26 15:41:30 -08008310 invalidateParentCaches();
8311 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008312 }
8313
8314 /**
8315 * Indicates what type of layer is currently associated with this view. By default
8316 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8317 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8318 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008319 *
Romain Guy171c5922011-01-06 10:04:23 -08008320 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8321 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008322 *
8323 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08008324 * @see #LAYER_TYPE_NONE
8325 * @see #LAYER_TYPE_SOFTWARE
8326 * @see #LAYER_TYPE_HARDWARE
8327 */
8328 public int getLayerType() {
8329 return mLayerType;
8330 }
Joe Malin32736f02011-01-19 16:14:20 -08008331
Romain Guy6c319ca2011-01-11 14:29:25 -08008332 /**
8333 * <p>Returns a hardware layer that can be used to draw this view again
8334 * without executing its draw method.</p>
8335 *
8336 * @return A HardwareLayer ready to render, or null if an error occurred.
8337 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008338 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008339 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8340 return null;
8341 }
8342
8343 final int width = mRight - mLeft;
8344 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008345
Romain Guy6c319ca2011-01-11 14:29:25 -08008346 if (width == 0 || height == 0) {
8347 return null;
8348 }
8349
8350 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8351 if (mHardwareLayer == null) {
8352 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8353 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008354 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008355 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8356 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008357 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008358 }
8359
Romain Guy5e7f7662011-01-24 22:35:56 -08008360 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8361 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8362 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008363 try {
8364 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008365 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008366 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008367
Romain Guy4f09f542011-01-26 22:41:43 -08008368 final int restoreCount = canvas.save();
8369
Romain Guy6c319ca2011-01-11 14:29:25 -08008370 computeScroll();
8371 canvas.translate(-mScrollX, -mScrollY);
8372
Romain Guy6c319ca2011-01-11 14:29:25 -08008373 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008374
Romain Guy6c319ca2011-01-11 14:29:25 -08008375 // Fast path for layouts with no backgrounds
8376 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8377 mPrivateFlags &= ~DIRTY_MASK;
8378 dispatchDraw(canvas);
8379 } else {
8380 draw(canvas);
8381 }
Joe Malin32736f02011-01-19 16:14:20 -08008382
Romain Guy6c319ca2011-01-11 14:29:25 -08008383 canvas.restoreToCount(restoreCount);
8384 } finally {
8385 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008386 mHardwareLayer.end(currentCanvas);
8387 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008388 }
8389 }
8390
8391 return mHardwareLayer;
8392 }
Romain Guy171c5922011-01-06 10:04:23 -08008393
8394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008395 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8396 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8397 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8398 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8399 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8400 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008401 *
Romain Guy171c5922011-01-06 10:04:23 -08008402 * <p>Enabling the drawing cache is similar to
8403 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008404 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8405 * drawing cache has no effect on rendering because the system uses a different mechanism
8406 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8407 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8408 * for information on how to enable software and hardware layers.</p>
8409 *
8410 * <p>This API can be used to manually generate
8411 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8412 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008413 *
8414 * @param enabled true to enable the drawing cache, false otherwise
8415 *
8416 * @see #isDrawingCacheEnabled()
8417 * @see #getDrawingCache()
8418 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008419 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 */
8421 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008422 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008423 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8424 }
8425
8426 /**
8427 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8428 *
8429 * @return true if the drawing cache is enabled
8430 *
8431 * @see #setDrawingCacheEnabled(boolean)
8432 * @see #getDrawingCache()
8433 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008434 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 public boolean isDrawingCacheEnabled() {
8436 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8437 }
8438
8439 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008440 * Debugging utility which recursively outputs the dirty state of a view and its
8441 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008442 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008443 * @hide
8444 */
Romain Guy676b1732011-02-14 14:45:33 -08008445 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008446 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8447 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8448 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8449 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8450 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8451 if (clear) {
8452 mPrivateFlags &= clearMask;
8453 }
8454 if (this instanceof ViewGroup) {
8455 ViewGroup parent = (ViewGroup) this;
8456 final int count = parent.getChildCount();
8457 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008458 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008459 child.outputDirtyFlags(indent + " ", clear, clearMask);
8460 }
8461 }
8462 }
8463
8464 /**
8465 * This method is used by ViewGroup to cause its children to restore or recreate their
8466 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8467 * to recreate its own display list, which would happen if it went through the normal
8468 * draw/dispatchDraw mechanisms.
8469 *
8470 * @hide
8471 */
8472 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008473
8474 /**
8475 * A view that is not attached or hardware accelerated cannot create a display list.
8476 * This method checks these conditions and returns the appropriate result.
8477 *
8478 * @return true if view has the ability to create a display list, false otherwise.
8479 *
8480 * @hide
8481 */
8482 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008483 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008484 }
Joe Malin32736f02011-01-19 16:14:20 -08008485
Chet Haasedaf98e92011-01-10 14:10:36 -08008486 /**
Romain Guyb051e892010-09-28 19:09:36 -07008487 * <p>Returns a display list that can be used to draw this view again
8488 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008489 *
Romain Guyb051e892010-09-28 19:09:36 -07008490 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008491 *
8492 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008493 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008494 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008495 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008496 return null;
8497 }
8498
Chet Haasedaf98e92011-01-10 14:10:36 -08008499 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8500 mDisplayList == null || !mDisplayList.isValid() ||
8501 mRecreateDisplayList)) {
8502 // Don't need to recreate the display list, just need to tell our
8503 // children to restore/recreate theirs
8504 if (mDisplayList != null && mDisplayList.isValid() &&
8505 !mRecreateDisplayList) {
8506 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8507 mPrivateFlags &= ~DIRTY_MASK;
8508 dispatchGetDisplayList();
8509
8510 return mDisplayList;
8511 }
8512
8513 // If we got here, we're recreating it. Mark it as such to ensure that
8514 // we copy in child display lists into ours in drawChild()
8515 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008516 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008517 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8518 // If we're creating a new display list, make sure our parent gets invalidated
8519 // since they will need to recreate their display list to account for this
8520 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008521 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008522 }
Romain Guyb051e892010-09-28 19:09:36 -07008523
8524 final HardwareCanvas canvas = mDisplayList.start();
8525 try {
8526 int width = mRight - mLeft;
8527 int height = mBottom - mTop;
8528
8529 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008530 // The dirty rect should always be null for a display list
8531 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008532
8533 final int restoreCount = canvas.save();
8534
Chet Haasedaf98e92011-01-10 14:10:36 -08008535 computeScroll();
8536 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008537 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008538
Romain Guyb051e892010-09-28 19:09:36 -07008539 // Fast path for layouts with no backgrounds
8540 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8541 mPrivateFlags &= ~DIRTY_MASK;
8542 dispatchDraw(canvas);
8543 } else {
8544 draw(canvas);
8545 }
Joe Malin32736f02011-01-19 16:14:20 -08008546
Romain Guyb051e892010-09-28 19:09:36 -07008547 canvas.restoreToCount(restoreCount);
8548 } finally {
8549 canvas.onPostDraw();
8550
8551 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008552 }
Chet Haase77785f92011-01-25 23:22:09 -08008553 } else {
8554 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8555 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008556 }
8557
8558 return mDisplayList;
8559 }
8560
8561 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008562 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008563 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008564 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008565 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008566 * @see #getDrawingCache(boolean)
8567 */
8568 public Bitmap getDrawingCache() {
8569 return getDrawingCache(false);
8570 }
8571
8572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8574 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8575 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8576 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8577 * request the drawing cache by calling this method and draw it on screen if the
8578 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008579 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008580 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8581 * this method will create a bitmap of the same size as this view. Because this bitmap
8582 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8583 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8584 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8585 * size than the view. This implies that your application must be able to handle this
8586 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008587 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008588 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8589 * the current density of the screen when the application is in compatibility
8590 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008591 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008592 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008593 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008594 * @see #setDrawingCacheEnabled(boolean)
8595 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008596 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008597 * @see #destroyDrawingCache()
8598 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008599 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008600 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8601 return null;
8602 }
8603 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008604 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008605 }
Romain Guy02890fd2010-08-06 17:58:44 -07008606 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008607 }
8608
8609 /**
8610 * <p>Frees the resources used by the drawing cache. If you call
8611 * {@link #buildDrawingCache()} manually without calling
8612 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8613 * should cleanup the cache with this method afterwards.</p>
8614 *
8615 * @see #setDrawingCacheEnabled(boolean)
8616 * @see #buildDrawingCache()
8617 * @see #getDrawingCache()
8618 */
8619 public void destroyDrawingCache() {
8620 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008621 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008622 mDrawingCache = null;
8623 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008624 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008625 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008626 mUnscaledDrawingCache = null;
8627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 }
8629
8630 /**
8631 * Setting a solid background color for the drawing cache's bitmaps will improve
8632 * perfromance and memory usage. Note, though that this should only be used if this
8633 * view will always be drawn on top of a solid color.
8634 *
8635 * @param color The background color to use for the drawing cache's bitmap
8636 *
8637 * @see #setDrawingCacheEnabled(boolean)
8638 * @see #buildDrawingCache()
8639 * @see #getDrawingCache()
8640 */
8641 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008642 if (color != mDrawingCacheBackgroundColor) {
8643 mDrawingCacheBackgroundColor = color;
8644 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008646 }
8647
8648 /**
8649 * @see #setDrawingCacheBackgroundColor(int)
8650 *
8651 * @return The background color to used for the drawing cache's bitmap
8652 */
8653 public int getDrawingCacheBackgroundColor() {
8654 return mDrawingCacheBackgroundColor;
8655 }
8656
8657 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008658 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008659 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008660 * @see #buildDrawingCache(boolean)
8661 */
8662 public void buildDrawingCache() {
8663 buildDrawingCache(false);
8664 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08008665
Romain Guyfbd8f692009-06-26 14:51:58 -07008666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8668 *
8669 * <p>If you call {@link #buildDrawingCache()} manually without calling
8670 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8671 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008672 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008673 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8674 * this method will create a bitmap of the same size as this view. Because this bitmap
8675 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8676 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8677 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8678 * size than the view. This implies that your application must be able to handle this
8679 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008680 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008681 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8682 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008683 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008684 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008685 *
8686 * @see #getDrawingCache()
8687 * @see #destroyDrawingCache()
8688 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008689 public void buildDrawingCache(boolean autoScale) {
8690 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008691 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008692 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008693
8694 if (ViewDebug.TRACE_HIERARCHY) {
8695 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008697
Romain Guy8506ab42009-06-11 17:35:47 -07008698 int width = mRight - mLeft;
8699 int height = mBottom - mTop;
8700
8701 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008702 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008703
Romain Guye1123222009-06-29 14:24:56 -07008704 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008705 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8706 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008708
8709 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008710 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008711 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712
8713 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008714 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008715 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008716 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8717 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008718 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008719 return;
8720 }
8721
8722 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008723 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724
8725 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008726 Bitmap.Config quality;
8727 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008728 // Never pick ARGB_4444 because it looks awful
8729 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008730 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8731 case DRAWING_CACHE_QUALITY_AUTO:
8732 quality = Bitmap.Config.ARGB_8888;
8733 break;
8734 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008735 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008736 break;
8737 case DRAWING_CACHE_QUALITY_HIGH:
8738 quality = Bitmap.Config.ARGB_8888;
8739 break;
8740 default:
8741 quality = Bitmap.Config.ARGB_8888;
8742 break;
8743 }
8744 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008745 // Optimization for translucent windows
8746 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008747 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 }
8749
8750 // Try to cleanup memory
8751 if (bitmap != null) bitmap.recycle();
8752
8753 try {
8754 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008755 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008756 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008757 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008758 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008759 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008760 }
Adam Powell26153a32010-11-08 15:22:27 -08008761 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762 } catch (OutOfMemoryError e) {
8763 // If there is not enough memory to create the bitmap cache, just
8764 // ignore the issue as bitmap caches are not required to draw the
8765 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008766 if (autoScale) {
8767 mDrawingCache = null;
8768 } else {
8769 mUnscaledDrawingCache = null;
8770 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008771 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008772 return;
8773 }
8774
8775 clear = drawingCacheBackgroundColor != 0;
8776 }
8777
8778 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008779 if (attachInfo != null) {
8780 canvas = attachInfo.mCanvas;
8781 if (canvas == null) {
8782 canvas = new Canvas();
8783 }
8784 canvas.setBitmap(bitmap);
8785 // Temporarily clobber the cached Canvas in case one of our children
8786 // is also using a drawing cache. Without this, the children would
8787 // steal the canvas by attaching their own bitmap to it and bad, bad
8788 // thing would happen (invisible views, corrupted drawings, etc.)
8789 attachInfo.mCanvas = null;
8790 } else {
8791 // This case should hopefully never or seldom happen
8792 canvas = new Canvas(bitmap);
8793 }
8794
8795 if (clear) {
8796 bitmap.eraseColor(drawingCacheBackgroundColor);
8797 }
8798
8799 computeScroll();
8800 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08008801
Romain Guye1123222009-06-29 14:24:56 -07008802 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008803 final float scale = attachInfo.mApplicationScale;
8804 canvas.scale(scale, scale);
8805 }
Joe Malin32736f02011-01-19 16:14:20 -08008806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 canvas.translate(-mScrollX, -mScrollY);
8808
Romain Guy5bcdff42009-05-14 21:27:18 -07008809 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08008810 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
8811 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07008812 mPrivateFlags |= DRAWING_CACHE_VALID;
8813 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814
8815 // Fast path for layouts with no backgrounds
8816 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8817 if (ViewDebug.TRACE_HIERARCHY) {
8818 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8819 }
Romain Guy5bcdff42009-05-14 21:27:18 -07008820 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 dispatchDraw(canvas);
8822 } else {
8823 draw(canvas);
8824 }
8825
8826 canvas.restoreToCount(restoreCount);
8827
8828 if (attachInfo != null) {
8829 // Restore the cached Canvas for our siblings
8830 attachInfo.mCanvas = canvas;
8831 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008832 }
8833 }
8834
8835 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008836 * Create a snapshot of the view into a bitmap. We should probably make
8837 * some form of this public, but should think about the API.
8838 */
Romain Guy223ff5c2010-03-02 17:07:47 -08008839 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008840 int width = mRight - mLeft;
8841 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008842
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008843 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07008844 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008845 width = (int) ((width * scale) + 0.5f);
8846 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08008847
Romain Guy8c11e312009-09-14 15:15:30 -07008848 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008849 if (bitmap == null) {
8850 throw new OutOfMemoryError();
8851 }
8852
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008853 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08008854
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008855 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008856 if (attachInfo != null) {
8857 canvas = attachInfo.mCanvas;
8858 if (canvas == null) {
8859 canvas = new Canvas();
8860 }
8861 canvas.setBitmap(bitmap);
8862 // Temporarily clobber the cached Canvas in case one of our children
8863 // is also using a drawing cache. Without this, the children would
8864 // steal the canvas by attaching their own bitmap to it and bad, bad
8865 // things would happen (invisible views, corrupted drawings, etc.)
8866 attachInfo.mCanvas = null;
8867 } else {
8868 // This case should hopefully never or seldom happen
8869 canvas = new Canvas(bitmap);
8870 }
8871
Romain Guy5bcdff42009-05-14 21:27:18 -07008872 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008873 bitmap.eraseColor(backgroundColor);
8874 }
8875
8876 computeScroll();
8877 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008878 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008879 canvas.translate(-mScrollX, -mScrollY);
8880
Romain Guy5bcdff42009-05-14 21:27:18 -07008881 // Temporarily remove the dirty mask
8882 int flags = mPrivateFlags;
8883 mPrivateFlags &= ~DIRTY_MASK;
8884
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008885 // Fast path for layouts with no backgrounds
8886 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8887 dispatchDraw(canvas);
8888 } else {
8889 draw(canvas);
8890 }
8891
Romain Guy5bcdff42009-05-14 21:27:18 -07008892 mPrivateFlags = flags;
8893
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008894 canvas.restoreToCount(restoreCount);
8895
8896 if (attachInfo != null) {
8897 // Restore the cached Canvas for our siblings
8898 attachInfo.mCanvas = canvas;
8899 }
Romain Guy8506ab42009-06-11 17:35:47 -07008900
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008901 return bitmap;
8902 }
8903
8904 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008905 * Indicates whether this View is currently in edit mode. A View is usually
8906 * in edit mode when displayed within a developer tool. For instance, if
8907 * this View is being drawn by a visual user interface builder, this method
8908 * should return true.
8909 *
8910 * Subclasses should check the return value of this method to provide
8911 * different behaviors if their normal behavior might interfere with the
8912 * host environment. For instance: the class spawns a thread in its
8913 * constructor, the drawing code relies on device-specific features, etc.
8914 *
8915 * This method is usually checked in the drawing code of custom widgets.
8916 *
8917 * @return True if this View is in edit mode, false otherwise.
8918 */
8919 public boolean isInEditMode() {
8920 return false;
8921 }
8922
8923 /**
8924 * If the View draws content inside its padding and enables fading edges,
8925 * it needs to support padding offsets. Padding offsets are added to the
8926 * fading edges to extend the length of the fade so that it covers pixels
8927 * drawn inside the padding.
8928 *
8929 * Subclasses of this class should override this method if they need
8930 * to draw content inside the padding.
8931 *
8932 * @return True if padding offset must be applied, false otherwise.
8933 *
8934 * @see #getLeftPaddingOffset()
8935 * @see #getRightPaddingOffset()
8936 * @see #getTopPaddingOffset()
8937 * @see #getBottomPaddingOffset()
8938 *
8939 * @since CURRENT
8940 */
8941 protected boolean isPaddingOffsetRequired() {
8942 return false;
8943 }
8944
8945 /**
8946 * Amount by which to extend the left fading region. Called only when
8947 * {@link #isPaddingOffsetRequired()} returns true.
8948 *
8949 * @return The left padding offset in pixels.
8950 *
8951 * @see #isPaddingOffsetRequired()
8952 *
8953 * @since CURRENT
8954 */
8955 protected int getLeftPaddingOffset() {
8956 return 0;
8957 }
8958
8959 /**
8960 * Amount by which to extend the right fading region. Called only when
8961 * {@link #isPaddingOffsetRequired()} returns true.
8962 *
8963 * @return The right padding offset in pixels.
8964 *
8965 * @see #isPaddingOffsetRequired()
8966 *
8967 * @since CURRENT
8968 */
8969 protected int getRightPaddingOffset() {
8970 return 0;
8971 }
8972
8973 /**
8974 * Amount by which to extend the top fading region. Called only when
8975 * {@link #isPaddingOffsetRequired()} returns true.
8976 *
8977 * @return The top padding offset in pixels.
8978 *
8979 * @see #isPaddingOffsetRequired()
8980 *
8981 * @since CURRENT
8982 */
8983 protected int getTopPaddingOffset() {
8984 return 0;
8985 }
8986
8987 /**
8988 * Amount by which to extend the bottom fading region. Called only when
8989 * {@link #isPaddingOffsetRequired()} returns true.
8990 *
8991 * @return The bottom padding offset in pixels.
8992 *
8993 * @see #isPaddingOffsetRequired()
8994 *
8995 * @since CURRENT
8996 */
8997 protected int getBottomPaddingOffset() {
8998 return 0;
8999 }
9000
9001 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009002 * <p>Indicates whether this view is attached to an hardware accelerated
9003 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009004 *
Romain Guy2bffd262010-09-12 17:40:02 -07009005 * <p>Even if this method returns true, it does not mean that every call
9006 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9007 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9008 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9009 * window is hardware accelerated,
9010 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9011 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009012 *
Romain Guy2bffd262010-09-12 17:40:02 -07009013 * @return True if the view is attached to a window and the window is
9014 * hardware accelerated; false in any other case.
9015 */
9016 public boolean isHardwareAccelerated() {
9017 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9018 }
Joe Malin32736f02011-01-19 16:14:20 -08009019
Romain Guy2bffd262010-09-12 17:40:02 -07009020 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009021 * Manually render this view (and all of its children) to the given Canvas.
9022 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009023 * called. When implementing a view, implement {@link #onDraw} instead of
9024 * overriding this method. If you do need to override this method, call
9025 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009026 *
9027 * @param canvas The Canvas to which the View is rendered.
9028 */
9029 public void draw(Canvas canvas) {
9030 if (ViewDebug.TRACE_HIERARCHY) {
9031 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9032 }
9033
Romain Guy5bcdff42009-05-14 21:27:18 -07009034 final int privateFlags = mPrivateFlags;
9035 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9036 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9037 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009039 /*
9040 * Draw traversal performs several drawing steps which must be executed
9041 * in the appropriate order:
9042 *
9043 * 1. Draw the background
9044 * 2. If necessary, save the canvas' layers to prepare for fading
9045 * 3. Draw view's content
9046 * 4. Draw children
9047 * 5. If necessary, draw the fading edges and restore layers
9048 * 6. Draw decorations (scrollbars for instance)
9049 */
9050
9051 // Step 1, draw the background, if needed
9052 int saveCount;
9053
Romain Guy24443ea2009-05-11 11:56:30 -07009054 if (!dirtyOpaque) {
9055 final Drawable background = mBGDrawable;
9056 if (background != null) {
9057 final int scrollX = mScrollX;
9058 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009059
Romain Guy24443ea2009-05-11 11:56:30 -07009060 if (mBackgroundSizeChanged) {
9061 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9062 mBackgroundSizeChanged = false;
9063 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064
Romain Guy24443ea2009-05-11 11:56:30 -07009065 if ((scrollX | scrollY) == 0) {
9066 background.draw(canvas);
9067 } else {
9068 canvas.translate(scrollX, scrollY);
9069 background.draw(canvas);
9070 canvas.translate(-scrollX, -scrollY);
9071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 }
9073 }
9074
9075 // skip step 2 & 5 if possible (common case)
9076 final int viewFlags = mViewFlags;
9077 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9078 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9079 if (!verticalEdges && !horizontalEdges) {
9080 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009081 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082
9083 // Step 4, draw the children
9084 dispatchDraw(canvas);
9085
9086 // Step 6, draw decorations (scrollbars)
9087 onDrawScrollBars(canvas);
9088
9089 // we're done...
9090 return;
9091 }
9092
9093 /*
9094 * Here we do the full fledged routine...
9095 * (this is an uncommon case where speed matters less,
9096 * this is why we repeat some of the tests that have been
9097 * done above)
9098 */
9099
9100 boolean drawTop = false;
9101 boolean drawBottom = false;
9102 boolean drawLeft = false;
9103 boolean drawRight = false;
9104
9105 float topFadeStrength = 0.0f;
9106 float bottomFadeStrength = 0.0f;
9107 float leftFadeStrength = 0.0f;
9108 float rightFadeStrength = 0.0f;
9109
9110 // Step 2, save the canvas' layers
9111 int paddingLeft = mPaddingLeft;
9112 int paddingTop = mPaddingTop;
9113
9114 final boolean offsetRequired = isPaddingOffsetRequired();
9115 if (offsetRequired) {
9116 paddingLeft += getLeftPaddingOffset();
9117 paddingTop += getTopPaddingOffset();
9118 }
9119
9120 int left = mScrollX + paddingLeft;
9121 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9122 int top = mScrollY + paddingTop;
9123 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9124
9125 if (offsetRequired) {
9126 right += getRightPaddingOffset();
9127 bottom += getBottomPaddingOffset();
9128 }
9129
9130 final ScrollabilityCache scrollabilityCache = mScrollCache;
9131 int length = scrollabilityCache.fadingEdgeLength;
9132
9133 // clip the fade length if top and bottom fades overlap
9134 // overlapping fades produce odd-looking artifacts
9135 if (verticalEdges && (top + length > bottom - length)) {
9136 length = (bottom - top) / 2;
9137 }
9138
9139 // also clip horizontal fades if necessary
9140 if (horizontalEdges && (left + length > right - length)) {
9141 length = (right - left) / 2;
9142 }
9143
9144 if (verticalEdges) {
9145 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009146 drawTop = topFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009147 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009148 drawBottom = bottomFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 }
9150
9151 if (horizontalEdges) {
9152 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009153 drawLeft = leftFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009154 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009155 drawRight = rightFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 }
9157
9158 saveCount = canvas.getSaveCount();
9159
9160 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009161 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009162 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9163
9164 if (drawTop) {
9165 canvas.saveLayer(left, top, right, top + length, null, flags);
9166 }
9167
9168 if (drawBottom) {
9169 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9170 }
9171
9172 if (drawLeft) {
9173 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9174 }
9175
9176 if (drawRight) {
9177 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9178 }
9179 } else {
9180 scrollabilityCache.setFadeColor(solidColor);
9181 }
9182
9183 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009184 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185
9186 // Step 4, draw the children
9187 dispatchDraw(canvas);
9188
9189 // Step 5, draw the fade effect and restore layers
9190 final Paint p = scrollabilityCache.paint;
9191 final Matrix matrix = scrollabilityCache.matrix;
9192 final Shader fade = scrollabilityCache.shader;
9193 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9194
9195 if (drawTop) {
9196 matrix.setScale(1, fadeHeight * topFadeStrength);
9197 matrix.postTranslate(left, top);
9198 fade.setLocalMatrix(matrix);
9199 canvas.drawRect(left, top, right, top + length, p);
9200 }
9201
9202 if (drawBottom) {
9203 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9204 matrix.postRotate(180);
9205 matrix.postTranslate(left, bottom);
9206 fade.setLocalMatrix(matrix);
9207 canvas.drawRect(left, bottom - length, right, bottom, p);
9208 }
9209
9210 if (drawLeft) {
9211 matrix.setScale(1, fadeHeight * leftFadeStrength);
9212 matrix.postRotate(-90);
9213 matrix.postTranslate(left, top);
9214 fade.setLocalMatrix(matrix);
9215 canvas.drawRect(left, top, left + length, bottom, p);
9216 }
9217
9218 if (drawRight) {
9219 matrix.setScale(1, fadeHeight * rightFadeStrength);
9220 matrix.postRotate(90);
9221 matrix.postTranslate(right, top);
9222 fade.setLocalMatrix(matrix);
9223 canvas.drawRect(right - length, top, right, bottom, p);
9224 }
9225
9226 canvas.restoreToCount(saveCount);
9227
9228 // Step 6, draw decorations (scrollbars)
9229 onDrawScrollBars(canvas);
9230 }
9231
9232 /**
9233 * Override this if your view is known to always be drawn on top of a solid color background,
9234 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9235 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9236 * should be set to 0xFF.
9237 *
9238 * @see #setVerticalFadingEdgeEnabled
9239 * @see #setHorizontalFadingEdgeEnabled
9240 *
9241 * @return The known solid color background for this view, or 0 if the color may vary
9242 */
9243 public int getSolidColor() {
9244 return 0;
9245 }
9246
9247 /**
9248 * Build a human readable string representation of the specified view flags.
9249 *
9250 * @param flags the view flags to convert to a string
9251 * @return a String representing the supplied flags
9252 */
9253 private static String printFlags(int flags) {
9254 String output = "";
9255 int numFlags = 0;
9256 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9257 output += "TAKES_FOCUS";
9258 numFlags++;
9259 }
9260
9261 switch (flags & VISIBILITY_MASK) {
9262 case INVISIBLE:
9263 if (numFlags > 0) {
9264 output += " ";
9265 }
9266 output += "INVISIBLE";
9267 // USELESS HERE numFlags++;
9268 break;
9269 case GONE:
9270 if (numFlags > 0) {
9271 output += " ";
9272 }
9273 output += "GONE";
9274 // USELESS HERE numFlags++;
9275 break;
9276 default:
9277 break;
9278 }
9279 return output;
9280 }
9281
9282 /**
9283 * Build a human readable string representation of the specified private
9284 * view flags.
9285 *
9286 * @param privateFlags the private view flags to convert to a string
9287 * @return a String representing the supplied flags
9288 */
9289 private static String printPrivateFlags(int privateFlags) {
9290 String output = "";
9291 int numFlags = 0;
9292
9293 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9294 output += "WANTS_FOCUS";
9295 numFlags++;
9296 }
9297
9298 if ((privateFlags & FOCUSED) == FOCUSED) {
9299 if (numFlags > 0) {
9300 output += " ";
9301 }
9302 output += "FOCUSED";
9303 numFlags++;
9304 }
9305
9306 if ((privateFlags & SELECTED) == SELECTED) {
9307 if (numFlags > 0) {
9308 output += " ";
9309 }
9310 output += "SELECTED";
9311 numFlags++;
9312 }
9313
9314 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9315 if (numFlags > 0) {
9316 output += " ";
9317 }
9318 output += "IS_ROOT_NAMESPACE";
9319 numFlags++;
9320 }
9321
9322 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9323 if (numFlags > 0) {
9324 output += " ";
9325 }
9326 output += "HAS_BOUNDS";
9327 numFlags++;
9328 }
9329
9330 if ((privateFlags & DRAWN) == DRAWN) {
9331 if (numFlags > 0) {
9332 output += " ";
9333 }
9334 output += "DRAWN";
9335 // USELESS HERE numFlags++;
9336 }
9337 return output;
9338 }
9339
9340 /**
9341 * <p>Indicates whether or not this view's layout will be requested during
9342 * the next hierarchy layout pass.</p>
9343 *
9344 * @return true if the layout will be forced during next layout pass
9345 */
9346 public boolean isLayoutRequested() {
9347 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9348 }
9349
9350 /**
9351 * Assign a size and position to a view and all of its
9352 * descendants
9353 *
9354 * <p>This is the second phase of the layout mechanism.
9355 * (The first is measuring). In this phase, each parent calls
9356 * layout on all of its children to position them.
9357 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009358 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009359 *
Chet Haase9c087442011-01-12 16:20:16 -08009360 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009361 * Derived classes with children should override
9362 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009363 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009364 *
9365 * @param l Left position, relative to parent
9366 * @param t Top position, relative to parent
9367 * @param r Right position, relative to parent
9368 * @param b Bottom position, relative to parent
9369 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009370 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009371 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009372 int oldL = mLeft;
9373 int oldT = mTop;
9374 int oldB = mBottom;
9375 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 boolean changed = setFrame(l, t, r, b);
9377 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9378 if (ViewDebug.TRACE_HIERARCHY) {
9379 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9380 }
9381
9382 onLayout(changed, l, t, r, b);
9383 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009384
9385 if (mOnLayoutChangeListeners != null) {
9386 ArrayList<OnLayoutChangeListener> listenersCopy =
9387 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9388 int numListeners = listenersCopy.size();
9389 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009390 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009391 }
9392 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009393 }
9394 mPrivateFlags &= ~FORCE_LAYOUT;
9395 }
9396
9397 /**
9398 * Called from layout when this view should
9399 * assign a size and position to each of its children.
9400 *
9401 * Derived classes with children should override
9402 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009403 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009404 * @param changed This is a new size or position for this view
9405 * @param left Left position, relative to parent
9406 * @param top Top position, relative to parent
9407 * @param right Right position, relative to parent
9408 * @param bottom Bottom position, relative to parent
9409 */
9410 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9411 }
9412
9413 /**
9414 * Assign a size and position to this view.
9415 *
9416 * This is called from layout.
9417 *
9418 * @param left Left position, relative to parent
9419 * @param top Top position, relative to parent
9420 * @param right Right position, relative to parent
9421 * @param bottom Bottom position, relative to parent
9422 * @return true if the new size and position are different than the
9423 * previous ones
9424 * {@hide}
9425 */
9426 protected boolean setFrame(int left, int top, int right, int bottom) {
9427 boolean changed = false;
9428
9429 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009430 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009431 + right + "," + bottom + ")");
9432 }
9433
9434 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9435 changed = true;
9436
9437 // Remember our drawn bit
9438 int drawn = mPrivateFlags & DRAWN;
9439
9440 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009441 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442
9443
9444 int oldWidth = mRight - mLeft;
9445 int oldHeight = mBottom - mTop;
9446
9447 mLeft = left;
9448 mTop = top;
9449 mRight = right;
9450 mBottom = bottom;
9451
9452 mPrivateFlags |= HAS_BOUNDS;
9453
9454 int newWidth = right - left;
9455 int newHeight = bottom - top;
9456
9457 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009458 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9459 // A change in dimension means an auto-centered pivot point changes, too
9460 mMatrixDirty = true;
9461 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009462 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9463 }
9464
9465 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9466 // If we are visible, force the DRAWN bit to on so that
9467 // this invalidate will go through (at least to our parent).
9468 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009469 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 // the DRAWN bit.
9471 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009472 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009473 // parent display list may need to be recreated based on a change in the bounds
9474 // of any child
9475 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 }
9477
9478 // Reset drawn bit to original value (invalidate turns it off)
9479 mPrivateFlags |= drawn;
9480
9481 mBackgroundSizeChanged = true;
9482 }
9483 return changed;
9484 }
9485
9486 /**
9487 * Finalize inflating a view from XML. This is called as the last phase
9488 * of inflation, after all child views have been added.
9489 *
9490 * <p>Even if the subclass overrides onFinishInflate, they should always be
9491 * sure to call the super method, so that we get called.
9492 */
9493 protected void onFinishInflate() {
9494 }
9495
9496 /**
9497 * Returns the resources associated with this view.
9498 *
9499 * @return Resources object.
9500 */
9501 public Resources getResources() {
9502 return mResources;
9503 }
9504
9505 /**
9506 * Invalidates the specified Drawable.
9507 *
9508 * @param drawable the drawable to invalidate
9509 */
9510 public void invalidateDrawable(Drawable drawable) {
9511 if (verifyDrawable(drawable)) {
9512 final Rect dirty = drawable.getBounds();
9513 final int scrollX = mScrollX;
9514 final int scrollY = mScrollY;
9515
9516 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9517 dirty.right + scrollX, dirty.bottom + scrollY);
9518 }
9519 }
9520
9521 /**
9522 * Schedules an action on a drawable to occur at a specified time.
9523 *
9524 * @param who the recipient of the action
9525 * @param what the action to run on the drawable
9526 * @param when the time at which the action must occur. Uses the
9527 * {@link SystemClock#uptimeMillis} timebase.
9528 */
9529 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9530 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9531 mAttachInfo.mHandler.postAtTime(what, who, when);
9532 }
9533 }
9534
9535 /**
9536 * Cancels a scheduled action on a drawable.
9537 *
9538 * @param who the recipient of the action
9539 * @param what the action to cancel
9540 */
9541 public void unscheduleDrawable(Drawable who, Runnable what) {
9542 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9543 mAttachInfo.mHandler.removeCallbacks(what, who);
9544 }
9545 }
9546
9547 /**
9548 * Unschedule any events associated with the given Drawable. This can be
9549 * used when selecting a new Drawable into a view, so that the previous
9550 * one is completely unscheduled.
9551 *
9552 * @param who The Drawable to unschedule.
9553 *
9554 * @see #drawableStateChanged
9555 */
9556 public void unscheduleDrawable(Drawable who) {
9557 if (mAttachInfo != null) {
9558 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9559 }
9560 }
9561
9562 /**
9563 * If your view subclass is displaying its own Drawable objects, it should
9564 * override this function and return true for any Drawable it is
9565 * displaying. This allows animations for those drawables to be
9566 * scheduled.
9567 *
9568 * <p>Be sure to call through to the super class when overriding this
9569 * function.
9570 *
9571 * @param who The Drawable to verify. Return true if it is one you are
9572 * displaying, else return the result of calling through to the
9573 * super class.
9574 *
9575 * @return boolean If true than the Drawable is being displayed in the
9576 * view; else false and it is not allowed to animate.
9577 *
9578 * @see #unscheduleDrawable
9579 * @see #drawableStateChanged
9580 */
9581 protected boolean verifyDrawable(Drawable who) {
9582 return who == mBGDrawable;
9583 }
9584
9585 /**
9586 * This function is called whenever the state of the view changes in such
9587 * a way that it impacts the state of drawables being shown.
9588 *
9589 * <p>Be sure to call through to the superclass when overriding this
9590 * function.
9591 *
9592 * @see Drawable#setState
9593 */
9594 protected void drawableStateChanged() {
9595 Drawable d = mBGDrawable;
9596 if (d != null && d.isStateful()) {
9597 d.setState(getDrawableState());
9598 }
9599 }
9600
9601 /**
9602 * Call this to force a view to update its drawable state. This will cause
9603 * drawableStateChanged to be called on this view. Views that are interested
9604 * in the new state should call getDrawableState.
9605 *
9606 * @see #drawableStateChanged
9607 * @see #getDrawableState
9608 */
9609 public void refreshDrawableState() {
9610 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9611 drawableStateChanged();
9612
9613 ViewParent parent = mParent;
9614 if (parent != null) {
9615 parent.childDrawableStateChanged(this);
9616 }
9617 }
9618
9619 /**
9620 * Return an array of resource IDs of the drawable states representing the
9621 * current state of the view.
9622 *
9623 * @return The current drawable state
9624 *
9625 * @see Drawable#setState
9626 * @see #drawableStateChanged
9627 * @see #onCreateDrawableState
9628 */
9629 public final int[] getDrawableState() {
9630 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9631 return mDrawableState;
9632 } else {
9633 mDrawableState = onCreateDrawableState(0);
9634 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9635 return mDrawableState;
9636 }
9637 }
9638
9639 /**
9640 * Generate the new {@link android.graphics.drawable.Drawable} state for
9641 * this view. This is called by the view
9642 * system when the cached Drawable state is determined to be invalid. To
9643 * retrieve the current state, you should use {@link #getDrawableState}.
9644 *
9645 * @param extraSpace if non-zero, this is the number of extra entries you
9646 * would like in the returned array in which you can place your own
9647 * states.
9648 *
9649 * @return Returns an array holding the current {@link Drawable} state of
9650 * the view.
9651 *
9652 * @see #mergeDrawableStates
9653 */
9654 protected int[] onCreateDrawableState(int extraSpace) {
9655 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9656 mParent instanceof View) {
9657 return ((View) mParent).onCreateDrawableState(extraSpace);
9658 }
9659
9660 int[] drawableState;
9661
9662 int privateFlags = mPrivateFlags;
9663
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009664 int viewStateIndex = 0;
9665 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9666 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9667 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009668 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009669 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9670 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009671 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9672 // This is set if HW acceleration is requested, even if the current
9673 // process doesn't allow it. This is just to allow app preview
9674 // windows to better match their app.
9675 viewStateIndex |= VIEW_STATE_ACCELERATED;
9676 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677
9678 drawableState = VIEW_STATE_SETS[viewStateIndex];
9679
9680 //noinspection ConstantIfStatement
9681 if (false) {
9682 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9683 Log.i("View", toString()
9684 + " pressed=" + ((privateFlags & PRESSED) != 0)
9685 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9686 + " fo=" + hasFocus()
9687 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009688 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 + ": " + Arrays.toString(drawableState));
9690 }
9691
9692 if (extraSpace == 0) {
9693 return drawableState;
9694 }
9695
9696 final int[] fullState;
9697 if (drawableState != null) {
9698 fullState = new int[drawableState.length + extraSpace];
9699 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9700 } else {
9701 fullState = new int[extraSpace];
9702 }
9703
9704 return fullState;
9705 }
9706
9707 /**
9708 * Merge your own state values in <var>additionalState</var> into the base
9709 * state values <var>baseState</var> that were returned by
9710 * {@link #onCreateDrawableState}.
9711 *
9712 * @param baseState The base state values returned by
9713 * {@link #onCreateDrawableState}, which will be modified to also hold your
9714 * own additional state values.
9715 *
9716 * @param additionalState The additional state values you would like
9717 * added to <var>baseState</var>; this array is not modified.
9718 *
9719 * @return As a convenience, the <var>baseState</var> array you originally
9720 * passed into the function is returned.
9721 *
9722 * @see #onCreateDrawableState
9723 */
9724 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9725 final int N = baseState.length;
9726 int i = N - 1;
9727 while (i >= 0 && baseState[i] == 0) {
9728 i--;
9729 }
9730 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9731 return baseState;
9732 }
9733
9734 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009735 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9736 * on all Drawable objects associated with this view.
9737 */
9738 public void jumpDrawablesToCurrentState() {
9739 if (mBGDrawable != null) {
9740 mBGDrawable.jumpToCurrentState();
9741 }
9742 }
9743
9744 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009745 * Sets the background color for this view.
9746 * @param color the color of the background
9747 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009748 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009749 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009750 if (mBGDrawable instanceof ColorDrawable) {
9751 ((ColorDrawable) mBGDrawable).setColor(color);
9752 } else {
9753 setBackgroundDrawable(new ColorDrawable(color));
9754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009755 }
9756
9757 /**
9758 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009759 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009760 * @param resid The identifier of the resource.
9761 * @attr ref android.R.styleable#View_background
9762 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009763 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009764 public void setBackgroundResource(int resid) {
9765 if (resid != 0 && resid == mBackgroundResource) {
9766 return;
9767 }
9768
9769 Drawable d= null;
9770 if (resid != 0) {
9771 d = mResources.getDrawable(resid);
9772 }
9773 setBackgroundDrawable(d);
9774
9775 mBackgroundResource = resid;
9776 }
9777
9778 /**
9779 * Set the background to a given Drawable, or remove the background. If the
9780 * background has padding, this View's padding is set to the background's
9781 * padding. However, when a background is removed, this View's padding isn't
9782 * touched. If setting the padding is desired, please use
9783 * {@link #setPadding(int, int, int, int)}.
9784 *
9785 * @param d The Drawable to use as the background, or null to remove the
9786 * background
9787 */
9788 public void setBackgroundDrawable(Drawable d) {
9789 boolean requestLayout = false;
9790
9791 mBackgroundResource = 0;
9792
9793 /*
9794 * Regardless of whether we're setting a new background or not, we want
9795 * to clear the previous drawable.
9796 */
9797 if (mBGDrawable != null) {
9798 mBGDrawable.setCallback(null);
9799 unscheduleDrawable(mBGDrawable);
9800 }
9801
9802 if (d != null) {
9803 Rect padding = sThreadLocal.get();
9804 if (padding == null) {
9805 padding = new Rect();
9806 sThreadLocal.set(padding);
9807 }
9808 if (d.getPadding(padding)) {
9809 setPadding(padding.left, padding.top, padding.right, padding.bottom);
9810 }
9811
9812 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
9813 // if it has a different minimum size, we should layout again
9814 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
9815 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
9816 requestLayout = true;
9817 }
9818
9819 d.setCallback(this);
9820 if (d.isStateful()) {
9821 d.setState(getDrawableState());
9822 }
9823 d.setVisible(getVisibility() == VISIBLE, false);
9824 mBGDrawable = d;
9825
9826 if ((mPrivateFlags & SKIP_DRAW) != 0) {
9827 mPrivateFlags &= ~SKIP_DRAW;
9828 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
9829 requestLayout = true;
9830 }
9831 } else {
9832 /* Remove the background */
9833 mBGDrawable = null;
9834
9835 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
9836 /*
9837 * This view ONLY drew the background before and we're removing
9838 * the background, so now it won't draw anything
9839 * (hence we SKIP_DRAW)
9840 */
9841 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
9842 mPrivateFlags |= SKIP_DRAW;
9843 }
9844
9845 /*
9846 * When the background is set, we try to apply its padding to this
9847 * View. When the background is removed, we don't touch this View's
9848 * padding. This is noted in the Javadocs. Hence, we don't need to
9849 * requestLayout(), the invalidate() below is sufficient.
9850 */
9851
9852 // The old background's minimum size could have affected this
9853 // View's layout, so let's requestLayout
9854 requestLayout = true;
9855 }
9856
Romain Guy8f1344f52009-05-15 16:03:59 -07009857 computeOpaqueFlags();
9858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009859 if (requestLayout) {
9860 requestLayout();
9861 }
9862
9863 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009864 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009865 }
9866
9867 /**
9868 * Gets the background drawable
9869 * @return The drawable used as the background for this view, if any.
9870 */
9871 public Drawable getBackground() {
9872 return mBGDrawable;
9873 }
9874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 /**
9876 * Sets the padding. The view may add on the space required to display
9877 * the scrollbars, depending on the style and visibility of the scrollbars.
9878 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
9879 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
9880 * from the values set in this call.
9881 *
9882 * @attr ref android.R.styleable#View_padding
9883 * @attr ref android.R.styleable#View_paddingBottom
9884 * @attr ref android.R.styleable#View_paddingLeft
9885 * @attr ref android.R.styleable#View_paddingRight
9886 * @attr ref android.R.styleable#View_paddingTop
9887 * @param left the left padding in pixels
9888 * @param top the top padding in pixels
9889 * @param right the right padding in pixels
9890 * @param bottom the bottom padding in pixels
9891 */
9892 public void setPadding(int left, int top, int right, int bottom) {
9893 boolean changed = false;
9894
Adam Powell20232d02010-12-08 21:08:53 -08009895 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009896 mUserPaddingRight = right;
9897 mUserPaddingBottom = bottom;
9898
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009899 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07009900
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009901 // Common case is there are no scroll bars.
9902 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009903 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -08009904 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
9905 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009906 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -08009907 switch (mVerticalScrollbarPosition) {
9908 case SCROLLBAR_POSITION_DEFAULT:
9909 case SCROLLBAR_POSITION_RIGHT:
9910 right += offset;
9911 break;
9912 case SCROLLBAR_POSITION_LEFT:
9913 left += offset;
9914 break;
9915 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009916 }
Adam Powell20232d02010-12-08 21:08:53 -08009917 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009918 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
9919 ? 0 : getHorizontalScrollbarHeight();
9920 }
9921 }
Romain Guy8506ab42009-06-11 17:35:47 -07009922
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009923 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009924 changed = true;
9925 mPaddingLeft = left;
9926 }
9927 if (mPaddingTop != top) {
9928 changed = true;
9929 mPaddingTop = top;
9930 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009931 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009932 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009933 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009935 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009936 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009937 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009938 }
9939
9940 if (changed) {
9941 requestLayout();
9942 }
9943 }
9944
9945 /**
9946 * Returns the top padding of this view.
9947 *
9948 * @return the top padding in pixels
9949 */
9950 public int getPaddingTop() {
9951 return mPaddingTop;
9952 }
9953
9954 /**
9955 * Returns the bottom padding of this view. If there are inset and enabled
9956 * scrollbars, this value may include the space required to display the
9957 * scrollbars as well.
9958 *
9959 * @return the bottom padding in pixels
9960 */
9961 public int getPaddingBottom() {
9962 return mPaddingBottom;
9963 }
9964
9965 /**
9966 * Returns the left padding of this view. If there are inset and enabled
9967 * scrollbars, this value may include the space required to display the
9968 * scrollbars as well.
9969 *
9970 * @return the left padding in pixels
9971 */
9972 public int getPaddingLeft() {
9973 return mPaddingLeft;
9974 }
9975
9976 /**
9977 * Returns the right padding of this view. If there are inset and enabled
9978 * scrollbars, this value may include the space required to display the
9979 * scrollbars as well.
9980 *
9981 * @return the right padding in pixels
9982 */
9983 public int getPaddingRight() {
9984 return mPaddingRight;
9985 }
9986
9987 /**
9988 * Changes the selection state of this view. A view can be selected or not.
9989 * Note that selection is not the same as focus. Views are typically
9990 * selected in the context of an AdapterView like ListView or GridView;
9991 * the selected view is the view that is highlighted.
9992 *
9993 * @param selected true if the view must be selected, false otherwise
9994 */
9995 public void setSelected(boolean selected) {
9996 if (((mPrivateFlags & SELECTED) != 0) != selected) {
9997 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -07009998 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -08009999 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000 refreshDrawableState();
10001 dispatchSetSelected(selected);
10002 }
10003 }
10004
10005 /**
10006 * Dispatch setSelected to all of this View's children.
10007 *
10008 * @see #setSelected(boolean)
10009 *
10010 * @param selected The new selected state
10011 */
10012 protected void dispatchSetSelected(boolean selected) {
10013 }
10014
10015 /**
10016 * Indicates the selection state of this view.
10017 *
10018 * @return true if the view is selected, false otherwise
10019 */
10020 @ViewDebug.ExportedProperty
10021 public boolean isSelected() {
10022 return (mPrivateFlags & SELECTED) != 0;
10023 }
10024
10025 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010026 * Changes the activated state of this view. A view can be activated or not.
10027 * Note that activation is not the same as selection. Selection is
10028 * a transient property, representing the view (hierarchy) the user is
10029 * currently interacting with. Activation is a longer-term state that the
10030 * user can move views in and out of. For example, in a list view with
10031 * single or multiple selection enabled, the views in the current selection
10032 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10033 * here.) The activated state is propagated down to children of the view it
10034 * is set on.
10035 *
10036 * @param activated true if the view must be activated, false otherwise
10037 */
10038 public void setActivated(boolean activated) {
10039 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10040 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010041 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010042 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010043 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010044 }
10045 }
10046
10047 /**
10048 * Dispatch setActivated to all of this View's children.
10049 *
10050 * @see #setActivated(boolean)
10051 *
10052 * @param activated The new activated state
10053 */
10054 protected void dispatchSetActivated(boolean activated) {
10055 }
10056
10057 /**
10058 * Indicates the activation state of this view.
10059 *
10060 * @return true if the view is activated, false otherwise
10061 */
10062 @ViewDebug.ExportedProperty
10063 public boolean isActivated() {
10064 return (mPrivateFlags & ACTIVATED) != 0;
10065 }
10066
10067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010068 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10069 * observer can be used to get notifications when global events, like
10070 * layout, happen.
10071 *
10072 * The returned ViewTreeObserver observer is not guaranteed to remain
10073 * valid for the lifetime of this View. If the caller of this method keeps
10074 * a long-lived reference to ViewTreeObserver, it should always check for
10075 * the return value of {@link ViewTreeObserver#isAlive()}.
10076 *
10077 * @return The ViewTreeObserver for this view's hierarchy.
10078 */
10079 public ViewTreeObserver getViewTreeObserver() {
10080 if (mAttachInfo != null) {
10081 return mAttachInfo.mTreeObserver;
10082 }
10083 if (mFloatingTreeObserver == null) {
10084 mFloatingTreeObserver = new ViewTreeObserver();
10085 }
10086 return mFloatingTreeObserver;
10087 }
10088
10089 /**
10090 * <p>Finds the topmost view in the current view hierarchy.</p>
10091 *
10092 * @return the topmost view containing this view
10093 */
10094 public View getRootView() {
10095 if (mAttachInfo != null) {
10096 final View v = mAttachInfo.mRootView;
10097 if (v != null) {
10098 return v;
10099 }
10100 }
Romain Guy8506ab42009-06-11 17:35:47 -070010101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010102 View parent = this;
10103
10104 while (parent.mParent != null && parent.mParent instanceof View) {
10105 parent = (View) parent.mParent;
10106 }
10107
10108 return parent;
10109 }
10110
10111 /**
10112 * <p>Computes the coordinates of this view on the screen. The argument
10113 * must be an array of two integers. After the method returns, the array
10114 * contains the x and y location in that order.</p>
10115 *
10116 * @param location an array of two integers in which to hold the coordinates
10117 */
10118 public void getLocationOnScreen(int[] location) {
10119 getLocationInWindow(location);
10120
10121 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010122 if (info != null) {
10123 location[0] += info.mWindowLeft;
10124 location[1] += info.mWindowTop;
10125 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010126 }
10127
10128 /**
10129 * <p>Computes the coordinates of this view in its window. The argument
10130 * must be an array of two integers. After the method returns, the array
10131 * contains the x and y location in that order.</p>
10132 *
10133 * @param location an array of two integers in which to hold the coordinates
10134 */
10135 public void getLocationInWindow(int[] location) {
10136 if (location == null || location.length < 2) {
10137 throw new IllegalArgumentException("location must be an array of "
10138 + "two integers");
10139 }
10140
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010141 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10142 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010143
10144 ViewParent viewParent = mParent;
10145 while (viewParent instanceof View) {
10146 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010147 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10148 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 viewParent = view.mParent;
10150 }
Romain Guy8506ab42009-06-11 17:35:47 -070010151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 if (viewParent instanceof ViewRoot) {
10153 // *cough*
10154 final ViewRoot vr = (ViewRoot)viewParent;
10155 location[1] -= vr.mCurScrollY;
10156 }
10157 }
10158
10159 /**
10160 * {@hide}
10161 * @param id the id of the view to be found
10162 * @return the view of the specified id, null if cannot be found
10163 */
10164 protected View findViewTraversal(int id) {
10165 if (id == mID) {
10166 return this;
10167 }
10168 return null;
10169 }
10170
10171 /**
10172 * {@hide}
10173 * @param tag the tag of the view to be found
10174 * @return the view of specified tag, null if cannot be found
10175 */
10176 protected View findViewWithTagTraversal(Object tag) {
10177 if (tag != null && tag.equals(mTag)) {
10178 return this;
10179 }
10180 return null;
10181 }
10182
10183 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010184 * {@hide}
10185 * @param predicate The predicate to evaluate.
10186 * @return The first view that matches the predicate or null.
10187 */
10188 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10189 if (predicate.apply(this)) {
10190 return this;
10191 }
10192 return null;
10193 }
10194
10195 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010196 * Look for a child view with the given id. If this view has the given
10197 * id, return this view.
10198 *
10199 * @param id The id to search for.
10200 * @return The view that has the given id in the hierarchy or null
10201 */
10202 public final View findViewById(int id) {
10203 if (id < 0) {
10204 return null;
10205 }
10206 return findViewTraversal(id);
10207 }
10208
10209 /**
10210 * Look for a child view with the given tag. If this view has the given
10211 * tag, return this view.
10212 *
10213 * @param tag The tag to search for, using "tag.equals(getTag())".
10214 * @return The View that has the given tag in the hierarchy or null
10215 */
10216 public final View findViewWithTag(Object tag) {
10217 if (tag == null) {
10218 return null;
10219 }
10220 return findViewWithTagTraversal(tag);
10221 }
10222
10223 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010224 * {@hide}
10225 * Look for a child view that matches the specified predicate.
10226 * If this view matches the predicate, return this view.
10227 *
10228 * @param predicate The predicate to evaluate.
10229 * @return The first view that matches the predicate or null.
10230 */
10231 public final View findViewByPredicate(Predicate<View> predicate) {
10232 return findViewByPredicateTraversal(predicate);
10233 }
10234
10235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010236 * Sets the identifier for this view. The identifier does not have to be
10237 * unique in this view's hierarchy. The identifier should be a positive
10238 * number.
10239 *
10240 * @see #NO_ID
10241 * @see #getId
10242 * @see #findViewById
10243 *
10244 * @param id a number used to identify the view
10245 *
10246 * @attr ref android.R.styleable#View_id
10247 */
10248 public void setId(int id) {
10249 mID = id;
10250 }
10251
10252 /**
10253 * {@hide}
10254 *
10255 * @param isRoot true if the view belongs to the root namespace, false
10256 * otherwise
10257 */
10258 public void setIsRootNamespace(boolean isRoot) {
10259 if (isRoot) {
10260 mPrivateFlags |= IS_ROOT_NAMESPACE;
10261 } else {
10262 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10263 }
10264 }
10265
10266 /**
10267 * {@hide}
10268 *
10269 * @return true if the view belongs to the root namespace, false otherwise
10270 */
10271 public boolean isRootNamespace() {
10272 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10273 }
10274
10275 /**
10276 * Returns this view's identifier.
10277 *
10278 * @return a positive integer used to identify the view or {@link #NO_ID}
10279 * if the view has no ID
10280 *
10281 * @see #setId
10282 * @see #findViewById
10283 * @attr ref android.R.styleable#View_id
10284 */
10285 @ViewDebug.CapturedViewProperty
10286 public int getId() {
10287 return mID;
10288 }
10289
10290 /**
10291 * Returns this view's tag.
10292 *
10293 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010294 *
10295 * @see #setTag(Object)
10296 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297 */
10298 @ViewDebug.ExportedProperty
10299 public Object getTag() {
10300 return mTag;
10301 }
10302
10303 /**
10304 * Sets the tag associated with this view. A tag can be used to mark
10305 * a view in its hierarchy and does not have to be unique within the
10306 * hierarchy. Tags can also be used to store data within a view without
10307 * resorting to another data structure.
10308 *
10309 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010310 *
10311 * @see #getTag()
10312 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010313 */
10314 public void setTag(final Object tag) {
10315 mTag = tag;
10316 }
10317
10318 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010319 * Returns the tag associated with this view and the specified key.
10320 *
10321 * @param key The key identifying the tag
10322 *
10323 * @return the Object stored in this view as a tag
10324 *
10325 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010326 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010327 */
10328 public Object getTag(int key) {
10329 SparseArray<Object> tags = null;
10330 synchronized (sTagsLock) {
10331 if (sTags != null) {
10332 tags = sTags.get(this);
10333 }
10334 }
10335
10336 if (tags != null) return tags.get(key);
10337 return null;
10338 }
10339
10340 /**
10341 * Sets a tag associated with this view and a key. A tag can be used
10342 * to mark a view in its hierarchy and does not have to be unique within
10343 * the hierarchy. Tags can also be used to store data within a view
10344 * without resorting to another data structure.
10345 *
10346 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010347 * application to ensure it is unique (see the <a
10348 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10349 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010350 * the Android framework or not associated with any package will cause
10351 * an {@link IllegalArgumentException} to be thrown.
10352 *
10353 * @param key The key identifying the tag
10354 * @param tag An Object to tag the view with
10355 *
10356 * @throws IllegalArgumentException If they specified key is not valid
10357 *
10358 * @see #setTag(Object)
10359 * @see #getTag(int)
10360 */
10361 public void setTag(int key, final Object tag) {
10362 // If the package id is 0x00 or 0x01, it's either an undefined package
10363 // or a framework id
10364 if ((key >>> 24) < 2) {
10365 throw new IllegalArgumentException("The key must be an application-specific "
10366 + "resource id.");
10367 }
10368
10369 setTagInternal(this, key, tag);
10370 }
10371
10372 /**
10373 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10374 * framework id.
10375 *
10376 * @hide
10377 */
10378 public void setTagInternal(int key, Object tag) {
10379 if ((key >>> 24) != 0x1) {
10380 throw new IllegalArgumentException("The key must be a framework-specific "
10381 + "resource id.");
10382 }
10383
Romain Guy8506ab42009-06-11 17:35:47 -070010384 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010385 }
10386
10387 private static void setTagInternal(View view, int key, Object tag) {
10388 SparseArray<Object> tags = null;
10389 synchronized (sTagsLock) {
10390 if (sTags == null) {
10391 sTags = new WeakHashMap<View, SparseArray<Object>>();
10392 } else {
10393 tags = sTags.get(view);
10394 }
10395 }
10396
10397 if (tags == null) {
10398 tags = new SparseArray<Object>(2);
10399 synchronized (sTagsLock) {
10400 sTags.put(view, tags);
10401 }
10402 }
10403
10404 tags.put(key, tag);
10405 }
10406
10407 /**
Romain Guy13922e02009-05-12 17:56:14 -070010408 * @param consistency The type of consistency. See ViewDebug for more information.
10409 *
10410 * @hide
10411 */
10412 protected boolean dispatchConsistencyCheck(int consistency) {
10413 return onConsistencyCheck(consistency);
10414 }
10415
10416 /**
10417 * Method that subclasses should implement to check their consistency. The type of
10418 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010419 *
Romain Guy13922e02009-05-12 17:56:14 -070010420 * @param consistency The type of consistency. See ViewDebug for more information.
10421 *
10422 * @throws IllegalStateException if the view is in an inconsistent state.
10423 *
10424 * @hide
10425 */
10426 protected boolean onConsistencyCheck(int consistency) {
10427 boolean result = true;
10428
10429 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10430 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10431
10432 if (checkLayout) {
10433 if (getParent() == null) {
10434 result = false;
10435 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10436 "View " + this + " does not have a parent.");
10437 }
10438
10439 if (mAttachInfo == null) {
10440 result = false;
10441 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10442 "View " + this + " is not attached to a window.");
10443 }
10444 }
10445
10446 if (checkDrawing) {
10447 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10448 // from their draw() method
10449
10450 if ((mPrivateFlags & DRAWN) != DRAWN &&
10451 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10452 result = false;
10453 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10454 "View " + this + " was invalidated but its drawing cache is valid.");
10455 }
10456 }
10457
10458 return result;
10459 }
10460
10461 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010462 * Prints information about this view in the log output, with the tag
10463 * {@link #VIEW_LOG_TAG}.
10464 *
10465 * @hide
10466 */
10467 public void debug() {
10468 debug(0);
10469 }
10470
10471 /**
10472 * Prints information about this view in the log output, with the tag
10473 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10474 * indentation defined by the <code>depth</code>.
10475 *
10476 * @param depth the indentation level
10477 *
10478 * @hide
10479 */
10480 protected void debug(int depth) {
10481 String output = debugIndent(depth - 1);
10482
10483 output += "+ " + this;
10484 int id = getId();
10485 if (id != -1) {
10486 output += " (id=" + id + ")";
10487 }
10488 Object tag = getTag();
10489 if (tag != null) {
10490 output += " (tag=" + tag + ")";
10491 }
10492 Log.d(VIEW_LOG_TAG, output);
10493
10494 if ((mPrivateFlags & FOCUSED) != 0) {
10495 output = debugIndent(depth) + " FOCUSED";
10496 Log.d(VIEW_LOG_TAG, output);
10497 }
10498
10499 output = debugIndent(depth);
10500 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10501 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10502 + "} ";
10503 Log.d(VIEW_LOG_TAG, output);
10504
10505 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10506 || mPaddingBottom != 0) {
10507 output = debugIndent(depth);
10508 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10509 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10510 Log.d(VIEW_LOG_TAG, output);
10511 }
10512
10513 output = debugIndent(depth);
10514 output += "mMeasureWidth=" + mMeasuredWidth +
10515 " mMeasureHeight=" + mMeasuredHeight;
10516 Log.d(VIEW_LOG_TAG, output);
10517
10518 output = debugIndent(depth);
10519 if (mLayoutParams == null) {
10520 output += "BAD! no layout params";
10521 } else {
10522 output = mLayoutParams.debug(output);
10523 }
10524 Log.d(VIEW_LOG_TAG, output);
10525
10526 output = debugIndent(depth);
10527 output += "flags={";
10528 output += View.printFlags(mViewFlags);
10529 output += "}";
10530 Log.d(VIEW_LOG_TAG, output);
10531
10532 output = debugIndent(depth);
10533 output += "privateFlags={";
10534 output += View.printPrivateFlags(mPrivateFlags);
10535 output += "}";
10536 Log.d(VIEW_LOG_TAG, output);
10537 }
10538
10539 /**
10540 * Creates an string of whitespaces used for indentation.
10541 *
10542 * @param depth the indentation level
10543 * @return a String containing (depth * 2 + 3) * 2 white spaces
10544 *
10545 * @hide
10546 */
10547 protected static String debugIndent(int depth) {
10548 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10549 for (int i = 0; i < (depth * 2) + 3; i++) {
10550 spaces.append(' ').append(' ');
10551 }
10552 return spaces.toString();
10553 }
10554
10555 /**
10556 * <p>Return the offset of the widget's text baseline from the widget's top
10557 * boundary. If this widget does not support baseline alignment, this
10558 * method returns -1. </p>
10559 *
10560 * @return the offset of the baseline within the widget's bounds or -1
10561 * if baseline alignment is not supported
10562 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010563 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010564 public int getBaseline() {
10565 return -1;
10566 }
10567
10568 /**
10569 * Call this when something has changed which has invalidated the
10570 * layout of this view. This will schedule a layout pass of the view
10571 * tree.
10572 */
10573 public void requestLayout() {
10574 if (ViewDebug.TRACE_HIERARCHY) {
10575 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10576 }
10577
10578 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010579 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010580
10581 if (mParent != null && !mParent.isLayoutRequested()) {
10582 mParent.requestLayout();
10583 }
10584 }
10585
10586 /**
10587 * Forces this view to be laid out during the next layout pass.
10588 * This method does not call requestLayout() or forceLayout()
10589 * on the parent.
10590 */
10591 public void forceLayout() {
10592 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010593 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010594 }
10595
10596 /**
10597 * <p>
10598 * This is called to find out how big a view should be. The parent
10599 * supplies constraint information in the width and height parameters.
10600 * </p>
10601 *
10602 * <p>
10603 * The actual mesurement work of a view is performed in
10604 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10605 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10606 * </p>
10607 *
10608 *
10609 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10610 * parent
10611 * @param heightMeasureSpec Vertical space requirements as imposed by the
10612 * parent
10613 *
10614 * @see #onMeasure(int, int)
10615 */
10616 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10617 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10618 widthMeasureSpec != mOldWidthMeasureSpec ||
10619 heightMeasureSpec != mOldHeightMeasureSpec) {
10620
10621 // first clears the measured dimension flag
10622 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10623
10624 if (ViewDebug.TRACE_HIERARCHY) {
10625 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10626 }
10627
10628 // measure ourselves, this should set the measured dimension flag back
10629 onMeasure(widthMeasureSpec, heightMeasureSpec);
10630
10631 // flag not set, setMeasuredDimension() was not invoked, we raise
10632 // an exception to warn the developer
10633 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10634 throw new IllegalStateException("onMeasure() did not set the"
10635 + " measured dimension by calling"
10636 + " setMeasuredDimension()");
10637 }
10638
10639 mPrivateFlags |= LAYOUT_REQUIRED;
10640 }
10641
10642 mOldWidthMeasureSpec = widthMeasureSpec;
10643 mOldHeightMeasureSpec = heightMeasureSpec;
10644 }
10645
10646 /**
10647 * <p>
10648 * Measure the view and its content to determine the measured width and the
10649 * measured height. This method is invoked by {@link #measure(int, int)} and
10650 * should be overriden by subclasses to provide accurate and efficient
10651 * measurement of their contents.
10652 * </p>
10653 *
10654 * <p>
10655 * <strong>CONTRACT:</strong> When overriding this method, you
10656 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10657 * measured width and height of this view. Failure to do so will trigger an
10658 * <code>IllegalStateException</code>, thrown by
10659 * {@link #measure(int, int)}. Calling the superclass'
10660 * {@link #onMeasure(int, int)} is a valid use.
10661 * </p>
10662 *
10663 * <p>
10664 * The base class implementation of measure defaults to the background size,
10665 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10666 * override {@link #onMeasure(int, int)} to provide better measurements of
10667 * their content.
10668 * </p>
10669 *
10670 * <p>
10671 * If this method is overridden, it is the subclass's responsibility to make
10672 * sure the measured height and width are at least the view's minimum height
10673 * and width ({@link #getSuggestedMinimumHeight()} and
10674 * {@link #getSuggestedMinimumWidth()}).
10675 * </p>
10676 *
10677 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10678 * The requirements are encoded with
10679 * {@link android.view.View.MeasureSpec}.
10680 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10681 * The requirements are encoded with
10682 * {@link android.view.View.MeasureSpec}.
10683 *
10684 * @see #getMeasuredWidth()
10685 * @see #getMeasuredHeight()
10686 * @see #setMeasuredDimension(int, int)
10687 * @see #getSuggestedMinimumHeight()
10688 * @see #getSuggestedMinimumWidth()
10689 * @see android.view.View.MeasureSpec#getMode(int)
10690 * @see android.view.View.MeasureSpec#getSize(int)
10691 */
10692 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10693 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10694 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10695 }
10696
10697 /**
10698 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10699 * measured width and measured height. Failing to do so will trigger an
10700 * exception at measurement time.</p>
10701 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010702 * @param measuredWidth The measured width of this view. May be a complex
10703 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10704 * {@link #MEASURED_STATE_TOO_SMALL}.
10705 * @param measuredHeight The measured height of this view. May be a complex
10706 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10707 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 */
10709 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10710 mMeasuredWidth = measuredWidth;
10711 mMeasuredHeight = measuredHeight;
10712
10713 mPrivateFlags |= MEASURED_DIMENSION_SET;
10714 }
10715
10716 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010717 * Merge two states as returned by {@link #getMeasuredState()}.
10718 * @param curState The current state as returned from a view or the result
10719 * of combining multiple views.
10720 * @param newState The new view state to combine.
10721 * @return Returns a new integer reflecting the combination of the two
10722 * states.
10723 */
10724 public static int combineMeasuredStates(int curState, int newState) {
10725 return curState | newState;
10726 }
10727
10728 /**
10729 * Version of {@link #resolveSizeAndState(int, int, int)}
10730 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10731 */
10732 public static int resolveSize(int size, int measureSpec) {
10733 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10734 }
10735
10736 /**
10737 * Utility to reconcile a desired size and state, with constraints imposed
10738 * by a MeasureSpec. Will take the desired size, unless a different size
10739 * is imposed by the constraints. The returned value is a compound integer,
10740 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10741 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10742 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010743 *
10744 * @param size How big the view wants to be
10745 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010746 * @return Size information bit mask as defined by
10747 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010748 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010749 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010750 int result = size;
10751 int specMode = MeasureSpec.getMode(measureSpec);
10752 int specSize = MeasureSpec.getSize(measureSpec);
10753 switch (specMode) {
10754 case MeasureSpec.UNSPECIFIED:
10755 result = size;
10756 break;
10757 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010758 if (specSize < size) {
10759 result = specSize | MEASURED_STATE_TOO_SMALL;
10760 } else {
10761 result = size;
10762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010763 break;
10764 case MeasureSpec.EXACTLY:
10765 result = specSize;
10766 break;
10767 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010768 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010769 }
10770
10771 /**
10772 * Utility to return a default size. Uses the supplied size if the
10773 * MeasureSpec imposed no contraints. Will get larger if allowed
10774 * by the MeasureSpec.
10775 *
10776 * @param size Default size for this view
10777 * @param measureSpec Constraints imposed by the parent
10778 * @return The size this view should be.
10779 */
10780 public static int getDefaultSize(int size, int measureSpec) {
10781 int result = size;
10782 int specMode = MeasureSpec.getMode(measureSpec);
10783 int specSize = MeasureSpec.getSize(measureSpec);
10784
10785 switch (specMode) {
10786 case MeasureSpec.UNSPECIFIED:
10787 result = size;
10788 break;
10789 case MeasureSpec.AT_MOST:
10790 case MeasureSpec.EXACTLY:
10791 result = specSize;
10792 break;
10793 }
10794 return result;
10795 }
10796
10797 /**
10798 * Returns the suggested minimum height that the view should use. This
10799 * returns the maximum of the view's minimum height
10800 * and the background's minimum height
10801 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
10802 * <p>
10803 * When being used in {@link #onMeasure(int, int)}, the caller should still
10804 * ensure the returned height is within the requirements of the parent.
10805 *
10806 * @return The suggested minimum height of the view.
10807 */
10808 protected int getSuggestedMinimumHeight() {
10809 int suggestedMinHeight = mMinHeight;
10810
10811 if (mBGDrawable != null) {
10812 final int bgMinHeight = mBGDrawable.getMinimumHeight();
10813 if (suggestedMinHeight < bgMinHeight) {
10814 suggestedMinHeight = bgMinHeight;
10815 }
10816 }
10817
10818 return suggestedMinHeight;
10819 }
10820
10821 /**
10822 * Returns the suggested minimum width that the view should use. This
10823 * returns the maximum of the view's minimum width)
10824 * and the background's minimum width
10825 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
10826 * <p>
10827 * When being used in {@link #onMeasure(int, int)}, the caller should still
10828 * ensure the returned width is within the requirements of the parent.
10829 *
10830 * @return The suggested minimum width of the view.
10831 */
10832 protected int getSuggestedMinimumWidth() {
10833 int suggestedMinWidth = mMinWidth;
10834
10835 if (mBGDrawable != null) {
10836 final int bgMinWidth = mBGDrawable.getMinimumWidth();
10837 if (suggestedMinWidth < bgMinWidth) {
10838 suggestedMinWidth = bgMinWidth;
10839 }
10840 }
10841
10842 return suggestedMinWidth;
10843 }
10844
10845 /**
10846 * Sets the minimum height of the view. It is not guaranteed the view will
10847 * be able to achieve this minimum height (for example, if its parent layout
10848 * constrains it with less available height).
10849 *
10850 * @param minHeight The minimum height the view will try to be.
10851 */
10852 public void setMinimumHeight(int minHeight) {
10853 mMinHeight = minHeight;
10854 }
10855
10856 /**
10857 * Sets the minimum width of the view. It is not guaranteed the view will
10858 * be able to achieve this minimum width (for example, if its parent layout
10859 * constrains it with less available width).
10860 *
10861 * @param minWidth The minimum width the view will try to be.
10862 */
10863 public void setMinimumWidth(int minWidth) {
10864 mMinWidth = minWidth;
10865 }
10866
10867 /**
10868 * Get the animation currently associated with this view.
10869 *
10870 * @return The animation that is currently playing or
10871 * scheduled to play for this view.
10872 */
10873 public Animation getAnimation() {
10874 return mCurrentAnimation;
10875 }
10876
10877 /**
10878 * Start the specified animation now.
10879 *
10880 * @param animation the animation to start now
10881 */
10882 public void startAnimation(Animation animation) {
10883 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
10884 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010885 invalidateParentCaches();
10886 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010887 }
10888
10889 /**
10890 * Cancels any animations for this view.
10891 */
10892 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080010893 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080010894 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080010895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010896 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010897 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010898 }
10899
10900 /**
10901 * Sets the next animation to play for this view.
10902 * If you want the animation to play immediately, use
10903 * startAnimation. This method provides allows fine-grained
10904 * control over the start time and invalidation, but you
10905 * must make sure that 1) the animation has a start time set, and
10906 * 2) the view will be invalidated when the animation is supposed to
10907 * start.
10908 *
10909 * @param animation The next animation, or null.
10910 */
10911 public void setAnimation(Animation animation) {
10912 mCurrentAnimation = animation;
10913 if (animation != null) {
10914 animation.reset();
10915 }
10916 }
10917
10918 /**
10919 * Invoked by a parent ViewGroup to notify the start of the animation
10920 * currently associated with this view. If you override this method,
10921 * always call super.onAnimationStart();
10922 *
10923 * @see #setAnimation(android.view.animation.Animation)
10924 * @see #getAnimation()
10925 */
10926 protected void onAnimationStart() {
10927 mPrivateFlags |= ANIMATION_STARTED;
10928 }
10929
10930 /**
10931 * Invoked by a parent ViewGroup to notify the end of the animation
10932 * currently associated with this view. If you override this method,
10933 * always call super.onAnimationEnd();
10934 *
10935 * @see #setAnimation(android.view.animation.Animation)
10936 * @see #getAnimation()
10937 */
10938 protected void onAnimationEnd() {
10939 mPrivateFlags &= ~ANIMATION_STARTED;
10940 }
10941
10942 /**
10943 * Invoked if there is a Transform that involves alpha. Subclass that can
10944 * draw themselves with the specified alpha should return true, and then
10945 * respect that alpha when their onDraw() is called. If this returns false
10946 * then the view may be redirected to draw into an offscreen buffer to
10947 * fulfill the request, which will look fine, but may be slower than if the
10948 * subclass handles it internally. The default implementation returns false.
10949 *
10950 * @param alpha The alpha (0..255) to apply to the view's drawing
10951 * @return true if the view can draw with the specified alpha.
10952 */
10953 protected boolean onSetAlpha(int alpha) {
10954 return false;
10955 }
10956
10957 /**
10958 * This is used by the RootView to perform an optimization when
10959 * the view hierarchy contains one or several SurfaceView.
10960 * SurfaceView is always considered transparent, but its children are not,
10961 * therefore all View objects remove themselves from the global transparent
10962 * region (passed as a parameter to this function).
10963 *
10964 * @param region The transparent region for this ViewRoot (window).
10965 *
10966 * @return Returns true if the effective visibility of the view at this
10967 * point is opaque, regardless of the transparent region; returns false
10968 * if it is possible for underlying windows to be seen behind the view.
10969 *
10970 * {@hide}
10971 */
10972 public boolean gatherTransparentRegion(Region region) {
10973 final AttachInfo attachInfo = mAttachInfo;
10974 if (region != null && attachInfo != null) {
10975 final int pflags = mPrivateFlags;
10976 if ((pflags & SKIP_DRAW) == 0) {
10977 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
10978 // remove it from the transparent region.
10979 final int[] location = attachInfo.mTransparentLocation;
10980 getLocationInWindow(location);
10981 region.op(location[0], location[1], location[0] + mRight - mLeft,
10982 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
10983 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
10984 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
10985 // exists, so we remove the background drawable's non-transparent
10986 // parts from this transparent region.
10987 applyDrawableToTransparentRegion(mBGDrawable, region);
10988 }
10989 }
10990 return true;
10991 }
10992
10993 /**
10994 * Play a sound effect for this view.
10995 *
10996 * <p>The framework will play sound effects for some built in actions, such as
10997 * clicking, but you may wish to play these effects in your widget,
10998 * for instance, for internal navigation.
10999 *
11000 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11001 * {@link #isSoundEffectsEnabled()} is true.
11002 *
11003 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11004 */
11005 public void playSoundEffect(int soundConstant) {
11006 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11007 return;
11008 }
11009 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11010 }
11011
11012 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011013 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011014 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011015 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011016 *
11017 * <p>The framework will provide haptic feedback for some built in actions,
11018 * such as long presses, but you may wish to provide feedback for your
11019 * own widget.
11020 *
11021 * <p>The feedback will only be performed if
11022 * {@link #isHapticFeedbackEnabled()} is true.
11023 *
11024 * @param feedbackConstant One of the constants defined in
11025 * {@link HapticFeedbackConstants}
11026 */
11027 public boolean performHapticFeedback(int feedbackConstant) {
11028 return performHapticFeedback(feedbackConstant, 0);
11029 }
11030
11031 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011032 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011033 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011034 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 *
11036 * @param feedbackConstant One of the constants defined in
11037 * {@link HapticFeedbackConstants}
11038 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11039 */
11040 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11041 if (mAttachInfo == null) {
11042 return false;
11043 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011044 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011045 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011046 && !isHapticFeedbackEnabled()) {
11047 return false;
11048 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011049 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11050 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011051 }
11052
11053 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011054 * Request that the visibility of the status bar be changed.
11055 */
11056 public void setSystemUiVisibility(int visibility) {
11057 if (visibility != mSystemUiVisibility) {
11058 mSystemUiVisibility = visibility;
11059 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11060 mParent.recomputeViewAttributes(this);
11061 }
11062 }
11063 }
11064
11065 /**
11066 * Returns the status bar visibility that this view has requested.
11067 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011068 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011069 return mSystemUiVisibility;
11070 }
11071
11072 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11073 mOnSystemUiVisibilityChangeListener = l;
11074 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11075 mParent.recomputeViewAttributes(this);
11076 }
11077 }
11078
11079 /**
11080 */
11081 public void dispatchSystemUiVisibilityChanged(int visibility) {
11082 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011083 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011084 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011085 }
11086 }
11087
11088 /**
Joe Malin32736f02011-01-19 16:14:20 -080011089 * Creates an image that the system displays during the drag and drop
11090 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11091 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11092 * appearance as the given View. The default also positions the center of the drag shadow
11093 * directly under the touch point. If no View is provided (the constructor with no parameters
11094 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11095 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11096 * default is an invisible drag shadow.
11097 * <p>
11098 * You are not required to use the View you provide to the constructor as the basis of the
11099 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11100 * anything you want as the drag shadow.
11101 * </p>
11102 * <p>
11103 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11104 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11105 * size and position of the drag shadow. It uses this data to construct a
11106 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11107 * so that your application can draw the shadow image in the Canvas.
11108 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011109 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011110 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011111 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011112
11113 /**
Joe Malin32736f02011-01-19 16:14:20 -080011114 * Constructs a shadow image builder based on a View. By default, the resulting drag
11115 * shadow will have the same appearance and dimensions as the View, with the touch point
11116 * over the center of the View.
11117 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011118 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011119 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011120 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011121 }
11122
Christopher Tate17ed60c2011-01-18 12:50:26 -080011123 /**
11124 * Construct a shadow builder object with no associated View. This
11125 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11126 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11127 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011128 * reference to any View object. If they are not overridden, then the result is an
11129 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011130 */
11131 public DragShadowBuilder() {
11132 mView = new WeakReference<View>(null);
11133 }
11134
11135 /**
11136 * Returns the View object that had been passed to the
11137 * {@link #View.DragShadowBuilder(View)}
11138 * constructor. If that View parameter was {@code null} or if the
11139 * {@link #View.DragShadowBuilder()}
11140 * constructor was used to instantiate the builder object, this method will return
11141 * null.
11142 *
11143 * @return The View object associate with this builder object.
11144 */
Chris Tate6b391282010-10-14 15:48:59 -070011145 final public View getView() {
11146 return mView.get();
11147 }
11148
Christopher Tate2c095f32010-10-04 14:13:40 -070011149 /**
Joe Malin32736f02011-01-19 16:14:20 -080011150 * Provides the metrics for the shadow image. These include the dimensions of
11151 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011152 * be centered under the touch location while dragging.
11153 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011154 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011155 * same as the dimensions of the View itself and centers the shadow under
11156 * the touch point.
11157 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011158 *
Joe Malin32736f02011-01-19 16:14:20 -080011159 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11160 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11161 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11162 * image.
11163 *
11164 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11165 * shadow image that should be underneath the touch point during the drag and drop
11166 * operation. Your application must set {@link android.graphics.Point#x} to the
11167 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011168 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011169 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011170 final View view = mView.get();
11171 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011172 shadowSize.set(view.getWidth(), view.getHeight());
11173 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011174 } else {
11175 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11176 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011177 }
11178
11179 /**
Joe Malin32736f02011-01-19 16:14:20 -080011180 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11181 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011182 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011183 *
Joe Malin32736f02011-01-19 16:14:20 -080011184 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011185 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011186 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011187 final View view = mView.get();
11188 if (view != null) {
11189 view.draw(canvas);
11190 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011191 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011192 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011193 }
11194 }
11195
11196 /**
Joe Malin32736f02011-01-19 16:14:20 -080011197 * Starts a drag and drop operation. When your application calls this method, it passes a
11198 * {@link android.view.View.DragShadowBuilder} object to the system. The
11199 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11200 * to get metrics for the drag shadow, and then calls the object's
11201 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11202 * <p>
11203 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11204 * drag events to all the View objects in your application that are currently visible. It does
11205 * this either by calling the View object's drag listener (an implementation of
11206 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11207 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11208 * Both are passed a {@link android.view.DragEvent} object that has a
11209 * {@link android.view.DragEvent#getAction()} value of
11210 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11211 * </p>
11212 * <p>
11213 * Your application can invoke startDrag() on any attached View object. The View object does not
11214 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11215 * be related to the View the user selected for dragging.
11216 * </p>
11217 * @param data A {@link android.content.ClipData} object pointing to the data to be
11218 * transferred by the drag and drop operation.
11219 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11220 * drag shadow.
11221 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11222 * drop operation. This Object is put into every DragEvent object sent by the system during the
11223 * current drag.
11224 * <p>
11225 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11226 * to the target Views. For example, it can contain flags that differentiate between a
11227 * a copy operation and a move operation.
11228 * </p>
11229 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11230 * so the parameter should be set to 0.
11231 * @return {@code true} if the method completes successfully, or
11232 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11233 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011234 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011235 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011236 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011237 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011238 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011239 }
11240 boolean okay = false;
11241
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011242 Point shadowSize = new Point();
11243 Point shadowTouchPoint = new Point();
11244 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011245
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011246 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11247 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11248 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011249 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011250
Chris Tatea32dcf72010-10-14 12:13:50 -070011251 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011252 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11253 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011254 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011255 Surface surface = new Surface();
11256 try {
11257 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011258 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011259 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011260 + " surface=" + surface);
11261 if (token != null) {
11262 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011263 try {
Chris Tate6b391282010-10-14 15:48:59 -070011264 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011265 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011266 } finally {
11267 surface.unlockCanvasAndPost(canvas);
11268 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011269
Christopher Tate407b4e92010-11-30 17:14:08 -080011270 final ViewRoot root = getViewRoot();
11271
11272 // Cache the local state object for delivery with DragEvents
11273 root.setLocalDragState(myLocalState);
11274
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011275 // repurpose 'shadowSize' for the last touch point
11276 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011277
Christopher Tatea53146c2010-09-07 11:57:52 -070011278 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011279 shadowSize.x, shadowSize.y,
11280 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011281 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011282 }
11283 } catch (Exception e) {
11284 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11285 surface.destroy();
11286 }
11287
11288 return okay;
11289 }
11290
Christopher Tatea53146c2010-09-07 11:57:52 -070011291 /**
Joe Malin32736f02011-01-19 16:14:20 -080011292 * Handles drag events sent by the system following a call to
11293 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11294 *<p>
11295 * When the system calls this method, it passes a
11296 * {@link android.view.DragEvent} object. A call to
11297 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11298 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11299 * operation.
11300 * @param event The {@link android.view.DragEvent} sent by the system.
11301 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11302 * in DragEvent, indicating the type of drag event represented by this object.
11303 * @return {@code true} if the method was successful, otherwise {@code false}.
11304 * <p>
11305 * The method should return {@code true} in response to an action type of
11306 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11307 * operation.
11308 * </p>
11309 * <p>
11310 * The method should also return {@code true} in response to an action type of
11311 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11312 * {@code false} if it didn't.
11313 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011314 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011315 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011316 return false;
11317 }
11318
11319 /**
Joe Malin32736f02011-01-19 16:14:20 -080011320 * Detects if this View is enabled and has a drag event listener.
11321 * If both are true, then it calls the drag event listener with the
11322 * {@link android.view.DragEvent} it received. If the drag event listener returns
11323 * {@code true}, then dispatchDragEvent() returns {@code true}.
11324 * <p>
11325 * For all other cases, the method calls the
11326 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11327 * method and returns its result.
11328 * </p>
11329 * <p>
11330 * This ensures that a drag event is always consumed, even if the View does not have a drag
11331 * event listener. However, if the View has a listener and the listener returns true, then
11332 * onDragEvent() is not called.
11333 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011334 */
11335 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011336 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011337 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11338 && mOnDragListener.onDrag(this, event)) {
11339 return true;
11340 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011341 return onDragEvent(event);
11342 }
11343
11344 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011345 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11346 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011347 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011348 */
11349 public void onCloseSystemDialogs(String reason) {
11350 }
Joe Malin32736f02011-01-19 16:14:20 -080011351
Dianne Hackbornffa42482009-09-23 22:20:11 -070011352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353 * Given a Drawable whose bounds have been set to draw into this view,
11354 * update a Region being computed for {@link #gatherTransparentRegion} so
11355 * that any non-transparent parts of the Drawable are removed from the
11356 * given transparent region.
11357 *
11358 * @param dr The Drawable whose transparency is to be applied to the region.
11359 * @param region A Region holding the current transparency information,
11360 * where any parts of the region that are set are considered to be
11361 * transparent. On return, this region will be modified to have the
11362 * transparency information reduced by the corresponding parts of the
11363 * Drawable that are not transparent.
11364 * {@hide}
11365 */
11366 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11367 if (DBG) {
11368 Log.i("View", "Getting transparent region for: " + this);
11369 }
11370 final Region r = dr.getTransparentRegion();
11371 final Rect db = dr.getBounds();
11372 final AttachInfo attachInfo = mAttachInfo;
11373 if (r != null && attachInfo != null) {
11374 final int w = getRight()-getLeft();
11375 final int h = getBottom()-getTop();
11376 if (db.left > 0) {
11377 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11378 r.op(0, 0, db.left, h, Region.Op.UNION);
11379 }
11380 if (db.right < w) {
11381 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11382 r.op(db.right, 0, w, h, Region.Op.UNION);
11383 }
11384 if (db.top > 0) {
11385 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11386 r.op(0, 0, w, db.top, Region.Op.UNION);
11387 }
11388 if (db.bottom < h) {
11389 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11390 r.op(0, db.bottom, w, h, Region.Op.UNION);
11391 }
11392 final int[] location = attachInfo.mTransparentLocation;
11393 getLocationInWindow(location);
11394 r.translate(location[0], location[1]);
11395 region.op(r, Region.Op.INTERSECT);
11396 } else {
11397 region.op(db, Region.Op.DIFFERENCE);
11398 }
11399 }
11400
Adam Powelle14579b2009-12-16 18:39:52 -080011401 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011402 mHasPerformedLongPress = false;
11403
11404 if (mPendingCheckForLongPress == null) {
11405 mPendingCheckForLongPress = new CheckForLongPress();
11406 }
11407 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011408 postDelayed(mPendingCheckForLongPress,
11409 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011410 }
11411
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011412 /**
11413 * Inflate a view from an XML resource. This convenience method wraps the {@link
11414 * LayoutInflater} class, which provides a full range of options for view inflation.
11415 *
11416 * @param context The Context object for your activity or application.
11417 * @param resource The resource ID to inflate
11418 * @param root A view group that will be the parent. Used to properly inflate the
11419 * layout_* parameters.
11420 * @see LayoutInflater
11421 */
11422 public static View inflate(Context context, int resource, ViewGroup root) {
11423 LayoutInflater factory = LayoutInflater.from(context);
11424 return factory.inflate(resource, root);
11425 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011427 /**
Adam Powell637d3372010-08-25 14:37:03 -070011428 * Scroll the view with standard behavior for scrolling beyond the normal
11429 * content boundaries. Views that call this method should override
11430 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11431 * results of an over-scroll operation.
11432 *
11433 * Views can use this method to handle any touch or fling-based scrolling.
11434 *
11435 * @param deltaX Change in X in pixels
11436 * @param deltaY Change in Y in pixels
11437 * @param scrollX Current X scroll value in pixels before applying deltaX
11438 * @param scrollY Current Y scroll value in pixels before applying deltaY
11439 * @param scrollRangeX Maximum content scroll range along the X axis
11440 * @param scrollRangeY Maximum content scroll range along the Y axis
11441 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11442 * along the X axis.
11443 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11444 * along the Y axis.
11445 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11446 * @return true if scrolling was clamped to an over-scroll boundary along either
11447 * axis, false otherwise.
11448 */
11449 protected boolean overScrollBy(int deltaX, int deltaY,
11450 int scrollX, int scrollY,
11451 int scrollRangeX, int scrollRangeY,
11452 int maxOverScrollX, int maxOverScrollY,
11453 boolean isTouchEvent) {
11454 final int overScrollMode = mOverScrollMode;
11455 final boolean canScrollHorizontal =
11456 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11457 final boolean canScrollVertical =
11458 computeVerticalScrollRange() > computeVerticalScrollExtent();
11459 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11460 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11461 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11462 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11463
11464 int newScrollX = scrollX + deltaX;
11465 if (!overScrollHorizontal) {
11466 maxOverScrollX = 0;
11467 }
11468
11469 int newScrollY = scrollY + deltaY;
11470 if (!overScrollVertical) {
11471 maxOverScrollY = 0;
11472 }
11473
11474 // Clamp values if at the limits and record
11475 final int left = -maxOverScrollX;
11476 final int right = maxOverScrollX + scrollRangeX;
11477 final int top = -maxOverScrollY;
11478 final int bottom = maxOverScrollY + scrollRangeY;
11479
11480 boolean clampedX = false;
11481 if (newScrollX > right) {
11482 newScrollX = right;
11483 clampedX = true;
11484 } else if (newScrollX < left) {
11485 newScrollX = left;
11486 clampedX = true;
11487 }
11488
11489 boolean clampedY = false;
11490 if (newScrollY > bottom) {
11491 newScrollY = bottom;
11492 clampedY = true;
11493 } else if (newScrollY < top) {
11494 newScrollY = top;
11495 clampedY = true;
11496 }
11497
11498 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11499
11500 return clampedX || clampedY;
11501 }
11502
11503 /**
11504 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11505 * respond to the results of an over-scroll operation.
11506 *
11507 * @param scrollX New X scroll value in pixels
11508 * @param scrollY New Y scroll value in pixels
11509 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11510 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11511 */
11512 protected void onOverScrolled(int scrollX, int scrollY,
11513 boolean clampedX, boolean clampedY) {
11514 // Intentionally empty.
11515 }
11516
11517 /**
11518 * Returns the over-scroll mode for this view. The result will be
11519 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11520 * (allow over-scrolling only if the view content is larger than the container),
11521 * or {@link #OVER_SCROLL_NEVER}.
11522 *
11523 * @return This view's over-scroll mode.
11524 */
11525 public int getOverScrollMode() {
11526 return mOverScrollMode;
11527 }
11528
11529 /**
11530 * Set the over-scroll mode for this view. Valid over-scroll modes are
11531 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11532 * (allow over-scrolling only if the view content is larger than the container),
11533 * or {@link #OVER_SCROLL_NEVER}.
11534 *
11535 * Setting the over-scroll mode of a view will have an effect only if the
11536 * view is capable of scrolling.
11537 *
11538 * @param overScrollMode The new over-scroll mode for this view.
11539 */
11540 public void setOverScrollMode(int overScrollMode) {
11541 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11542 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11543 overScrollMode != OVER_SCROLL_NEVER) {
11544 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11545 }
11546 mOverScrollMode = overScrollMode;
11547 }
11548
11549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011550 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11551 * Each MeasureSpec represents a requirement for either the width or the height.
11552 * A MeasureSpec is comprised of a size and a mode. There are three possible
11553 * modes:
11554 * <dl>
11555 * <dt>UNSPECIFIED</dt>
11556 * <dd>
11557 * The parent has not imposed any constraint on the child. It can be whatever size
11558 * it wants.
11559 * </dd>
11560 *
11561 * <dt>EXACTLY</dt>
11562 * <dd>
11563 * The parent has determined an exact size for the child. The child is going to be
11564 * given those bounds regardless of how big it wants to be.
11565 * </dd>
11566 *
11567 * <dt>AT_MOST</dt>
11568 * <dd>
11569 * The child can be as large as it wants up to the specified size.
11570 * </dd>
11571 * </dl>
11572 *
11573 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11574 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11575 */
11576 public static class MeasureSpec {
11577 private static final int MODE_SHIFT = 30;
11578 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11579
11580 /**
11581 * Measure specification mode: The parent has not imposed any constraint
11582 * on the child. It can be whatever size it wants.
11583 */
11584 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11585
11586 /**
11587 * Measure specification mode: The parent has determined an exact size
11588 * for the child. The child is going to be given those bounds regardless
11589 * of how big it wants to be.
11590 */
11591 public static final int EXACTLY = 1 << MODE_SHIFT;
11592
11593 /**
11594 * Measure specification mode: The child can be as large as it wants up
11595 * to the specified size.
11596 */
11597 public static final int AT_MOST = 2 << MODE_SHIFT;
11598
11599 /**
11600 * Creates a measure specification based on the supplied size and mode.
11601 *
11602 * The mode must always be one of the following:
11603 * <ul>
11604 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11605 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11606 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11607 * </ul>
11608 *
11609 * @param size the size of the measure specification
11610 * @param mode the mode of the measure specification
11611 * @return the measure specification based on size and mode
11612 */
11613 public static int makeMeasureSpec(int size, int mode) {
11614 return size + mode;
11615 }
11616
11617 /**
11618 * Extracts the mode from the supplied measure specification.
11619 *
11620 * @param measureSpec the measure specification to extract the mode from
11621 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11622 * {@link android.view.View.MeasureSpec#AT_MOST} or
11623 * {@link android.view.View.MeasureSpec#EXACTLY}
11624 */
11625 public static int getMode(int measureSpec) {
11626 return (measureSpec & MODE_MASK);
11627 }
11628
11629 /**
11630 * Extracts the size from the supplied measure specification.
11631 *
11632 * @param measureSpec the measure specification to extract the size from
11633 * @return the size in pixels defined in the supplied measure specification
11634 */
11635 public static int getSize(int measureSpec) {
11636 return (measureSpec & ~MODE_MASK);
11637 }
11638
11639 /**
11640 * Returns a String representation of the specified measure
11641 * specification.
11642 *
11643 * @param measureSpec the measure specification to convert to a String
11644 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11645 */
11646 public static String toString(int measureSpec) {
11647 int mode = getMode(measureSpec);
11648 int size = getSize(measureSpec);
11649
11650 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11651
11652 if (mode == UNSPECIFIED)
11653 sb.append("UNSPECIFIED ");
11654 else if (mode == EXACTLY)
11655 sb.append("EXACTLY ");
11656 else if (mode == AT_MOST)
11657 sb.append("AT_MOST ");
11658 else
11659 sb.append(mode).append(" ");
11660
11661 sb.append(size);
11662 return sb.toString();
11663 }
11664 }
11665
11666 class CheckForLongPress implements Runnable {
11667
11668 private int mOriginalWindowAttachCount;
11669
11670 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011671 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011672 && mOriginalWindowAttachCount == mWindowAttachCount) {
11673 if (performLongClick()) {
11674 mHasPerformedLongPress = true;
11675 }
11676 }
11677 }
11678
11679 public void rememberWindowAttachCount() {
11680 mOriginalWindowAttachCount = mWindowAttachCount;
11681 }
11682 }
Joe Malin32736f02011-01-19 16:14:20 -080011683
Adam Powelle14579b2009-12-16 18:39:52 -080011684 private final class CheckForTap implements Runnable {
11685 public void run() {
11686 mPrivateFlags &= ~PREPRESSED;
11687 mPrivateFlags |= PRESSED;
11688 refreshDrawableState();
11689 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11690 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11691 }
11692 }
11693 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011694
Adam Powella35d7682010-03-12 14:48:13 -080011695 private final class PerformClick implements Runnable {
11696 public void run() {
11697 performClick();
11698 }
11699 }
11700
Dianne Hackborn63042d62011-01-26 18:56:29 -080011701 /** @hide */
11702 public void hackTurnOffWindowResizeAnim(boolean off) {
11703 mAttachInfo.mTurnOffWindowResizeAnim = off;
11704 }
Joe Malin32736f02011-01-19 16:14:20 -080011705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011706 /**
11707 * Interface definition for a callback to be invoked when a key event is
11708 * dispatched to this view. The callback will be invoked before the key
11709 * event is given to the view.
11710 */
11711 public interface OnKeyListener {
11712 /**
11713 * Called when a key is dispatched to a view. This allows listeners to
11714 * get a chance to respond before the target view.
11715 *
11716 * @param v The view the key has been dispatched to.
11717 * @param keyCode The code for the physical key that was pressed
11718 * @param event The KeyEvent object containing full information about
11719 * the event.
11720 * @return True if the listener has consumed the event, false otherwise.
11721 */
11722 boolean onKey(View v, int keyCode, KeyEvent event);
11723 }
11724
11725 /**
11726 * Interface definition for a callback to be invoked when a touch event is
11727 * dispatched to this view. The callback will be invoked before the touch
11728 * event is given to the view.
11729 */
11730 public interface OnTouchListener {
11731 /**
11732 * Called when a touch event is dispatched to a view. This allows listeners to
11733 * get a chance to respond before the target view.
11734 *
11735 * @param v The view the touch event has been dispatched to.
11736 * @param event The MotionEvent object containing full information about
11737 * the event.
11738 * @return True if the listener has consumed the event, false otherwise.
11739 */
11740 boolean onTouch(View v, MotionEvent event);
11741 }
11742
11743 /**
11744 * Interface definition for a callback to be invoked when a view has been clicked and held.
11745 */
11746 public interface OnLongClickListener {
11747 /**
11748 * Called when a view has been clicked and held.
11749 *
11750 * @param v The view that was clicked and held.
11751 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080011752 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 */
11754 boolean onLongClick(View v);
11755 }
11756
11757 /**
Chris Tate32affef2010-10-18 15:29:21 -070011758 * Interface definition for a callback to be invoked when a drag is being dispatched
11759 * to this view. The callback will be invoked before the hosting view's own
11760 * onDrag(event) method. If the listener wants to fall back to the hosting view's
11761 * onDrag(event) behavior, it should return 'false' from this callback.
11762 */
11763 public interface OnDragListener {
11764 /**
11765 * Called when a drag event is dispatched to a view. This allows listeners
11766 * to get a chance to override base View behavior.
11767 *
Joe Malin32736f02011-01-19 16:14:20 -080011768 * @param v The View that received the drag event.
11769 * @param event The {@link android.view.DragEvent} object for the drag event.
11770 * @return {@code true} if the drag event was handled successfully, or {@code false}
11771 * if the drag event was not handled. Note that {@code false} will trigger the View
11772 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070011773 */
11774 boolean onDrag(View v, DragEvent event);
11775 }
11776
11777 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011778 * Interface definition for a callback to be invoked when the focus state of
11779 * a view changed.
11780 */
11781 public interface OnFocusChangeListener {
11782 /**
11783 * Called when the focus state of a view has changed.
11784 *
11785 * @param v The view whose state has changed.
11786 * @param hasFocus The new focus state of v.
11787 */
11788 void onFocusChange(View v, boolean hasFocus);
11789 }
11790
11791 /**
11792 * Interface definition for a callback to be invoked when a view is clicked.
11793 */
11794 public interface OnClickListener {
11795 /**
11796 * Called when a view has been clicked.
11797 *
11798 * @param v The view that was clicked.
11799 */
11800 void onClick(View v);
11801 }
11802
11803 /**
11804 * Interface definition for a callback to be invoked when the context menu
11805 * for this view is being built.
11806 */
11807 public interface OnCreateContextMenuListener {
11808 /**
11809 * Called when the context menu for this view is being built. It is not
11810 * safe to hold onto the menu after this method returns.
11811 *
11812 * @param menu The context menu that is being built
11813 * @param v The view for which the context menu is being built
11814 * @param menuInfo Extra information about the item for which the
11815 * context menu should be shown. This information will vary
11816 * depending on the class of v.
11817 */
11818 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
11819 }
11820
Joe Onorato664644d2011-01-23 17:53:23 -080011821 /**
11822 * Interface definition for a callback to be invoked when the status bar changes
11823 * visibility.
11824 *
11825 * @see #setOnSystemUiVisibilityChangeListener
11826 */
11827 public interface OnSystemUiVisibilityChangeListener {
11828 /**
11829 * Called when the status bar changes visibility because of a call to
11830 * {@link #setSystemUiVisibility}.
11831 *
11832 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
11833 */
11834 public void onSystemUiVisibilityChange(int visibility);
11835 }
11836
Adam Powell4afd62b2011-02-18 15:02:18 -080011837 /**
11838 * Interface definition for a callback to be invoked when this view is attached
11839 * or detached from its window.
11840 */
11841 public interface OnAttachStateChangeListener {
11842 /**
11843 * Called when the view is attached to a window.
11844 * @param v The view that was attached
11845 */
11846 public void onViewAttachedToWindow(View v);
11847 /**
11848 * Called when the view is detached from a window.
11849 * @param v The view that was detached
11850 */
11851 public void onViewDetachedFromWindow(View v);
11852 }
11853
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011854 private final class UnsetPressedState implements Runnable {
11855 public void run() {
11856 setPressed(false);
11857 }
11858 }
11859
11860 /**
11861 * Base class for derived classes that want to save and restore their own
11862 * state in {@link android.view.View#onSaveInstanceState()}.
11863 */
11864 public static class BaseSavedState extends AbsSavedState {
11865 /**
11866 * Constructor used when reading from a parcel. Reads the state of the superclass.
11867 *
11868 * @param source
11869 */
11870 public BaseSavedState(Parcel source) {
11871 super(source);
11872 }
11873
11874 /**
11875 * Constructor called by derived classes when creating their SavedState objects
11876 *
11877 * @param superState The state of the superclass of this view
11878 */
11879 public BaseSavedState(Parcelable superState) {
11880 super(superState);
11881 }
11882
11883 public static final Parcelable.Creator<BaseSavedState> CREATOR =
11884 new Parcelable.Creator<BaseSavedState>() {
11885 public BaseSavedState createFromParcel(Parcel in) {
11886 return new BaseSavedState(in);
11887 }
11888
11889 public BaseSavedState[] newArray(int size) {
11890 return new BaseSavedState[size];
11891 }
11892 };
11893 }
11894
11895 /**
11896 * A set of information given to a view when it is attached to its parent
11897 * window.
11898 */
11899 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011900 interface Callbacks {
11901 void playSoundEffect(int effectId);
11902 boolean performHapticFeedback(int effectId, boolean always);
11903 }
11904
11905 /**
11906 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
11907 * to a Handler. This class contains the target (View) to invalidate and
11908 * the coordinates of the dirty rectangle.
11909 *
11910 * For performance purposes, this class also implements a pool of up to
11911 * POOL_LIMIT objects that get reused. This reduces memory allocations
11912 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 */
Romain Guyd928d682009-03-31 17:52:16 -070011914 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011915 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070011916 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
11917 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070011918 public InvalidateInfo newInstance() {
11919 return new InvalidateInfo();
11920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011921
Romain Guyd928d682009-03-31 17:52:16 -070011922 public void onAcquired(InvalidateInfo element) {
11923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011924
Romain Guyd928d682009-03-31 17:52:16 -070011925 public void onReleased(InvalidateInfo element) {
11926 }
11927 }, POOL_LIMIT)
11928 );
11929
11930 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011931
11932 View target;
11933
11934 int left;
11935 int top;
11936 int right;
11937 int bottom;
11938
Romain Guyd928d682009-03-31 17:52:16 -070011939 public void setNextPoolable(InvalidateInfo element) {
11940 mNext = element;
11941 }
11942
11943 public InvalidateInfo getNextPoolable() {
11944 return mNext;
11945 }
11946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011947 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070011948 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011949 }
11950
11951 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070011952 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011953 }
11954 }
11955
11956 final IWindowSession mSession;
11957
11958 final IWindow mWindow;
11959
11960 final IBinder mWindowToken;
11961
11962 final Callbacks mRootCallbacks;
11963
Chet Haasedaf98e92011-01-10 14:10:36 -080011964 Canvas mHardwareCanvas;
11965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011966 /**
11967 * The top view of the hierarchy.
11968 */
11969 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070011970
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011971 IBinder mPanelParentWindowToken;
11972 Surface mSurface;
11973
Romain Guyb051e892010-09-28 19:09:36 -070011974 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011975 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070011976 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080011977
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011978 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011979 * Scale factor used by the compatibility mode
11980 */
11981 float mApplicationScale;
11982
11983 /**
11984 * Indicates whether the application is in compatibility mode
11985 */
11986 boolean mScalingRequired;
11987
11988 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080011989 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
11990 */
11991 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080011992
Dianne Hackborn63042d62011-01-26 18:56:29 -080011993 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994 * Left position of this view's window
11995 */
11996 int mWindowLeft;
11997
11998 /**
11999 * Top position of this view's window
12000 */
12001 int mWindowTop;
12002
12003 /**
Adam Powell26153a32010-11-08 15:22:27 -080012004 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012005 */
Adam Powell26153a32010-11-08 15:22:27 -080012006 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012007
12008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012009 * For windows that are full-screen but using insets to layout inside
12010 * of the screen decorations, these are the current insets for the
12011 * content of the window.
12012 */
12013 final Rect mContentInsets = new Rect();
12014
12015 /**
12016 * For windows that are full-screen but using insets to layout inside
12017 * of the screen decorations, these are the current insets for the
12018 * actual visible parts of the window.
12019 */
12020 final Rect mVisibleInsets = new Rect();
12021
12022 /**
12023 * The internal insets given by this window. This value is
12024 * supplied by the client (through
12025 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12026 * be given to the window manager when changed to be used in laying
12027 * out windows behind it.
12028 */
12029 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12030 = new ViewTreeObserver.InternalInsetsInfo();
12031
12032 /**
12033 * All views in the window's hierarchy that serve as scroll containers,
12034 * used to determine if the window can be resized or must be panned
12035 * to adjust for a soft input area.
12036 */
12037 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12038
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012039 final KeyEvent.DispatcherState mKeyDispatchState
12040 = new KeyEvent.DispatcherState();
12041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012042 /**
12043 * Indicates whether the view's window currently has the focus.
12044 */
12045 boolean mHasWindowFocus;
12046
12047 /**
12048 * The current visibility of the window.
12049 */
12050 int mWindowVisibility;
12051
12052 /**
12053 * Indicates the time at which drawing started to occur.
12054 */
12055 long mDrawingTime;
12056
12057 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012058 * Indicates whether or not ignoring the DIRTY_MASK flags.
12059 */
12060 boolean mIgnoreDirtyState;
12061
12062 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012063 * Indicates whether the view's window is currently in touch mode.
12064 */
12065 boolean mInTouchMode;
12066
12067 /**
12068 * Indicates that ViewRoot should trigger a global layout change
12069 * the next time it performs a traversal
12070 */
12071 boolean mRecomputeGlobalAttributes;
12072
12073 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012074 * Set during a traveral if any views want to keep the screen on.
12075 */
12076 boolean mKeepScreenOn;
12077
12078 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012079 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12080 */
12081 int mSystemUiVisibility;
12082
12083 /**
12084 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12085 * attached.
12086 */
12087 boolean mHasSystemUiListeners;
12088
12089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090 * Set if the visibility of any views has changed.
12091 */
12092 boolean mViewVisibilityChanged;
12093
12094 /**
12095 * Set to true if a view has been scrolled.
12096 */
12097 boolean mViewScrollChanged;
12098
12099 /**
12100 * Global to the view hierarchy used as a temporary for dealing with
12101 * x/y points in the transparent region computations.
12102 */
12103 final int[] mTransparentLocation = new int[2];
12104
12105 /**
12106 * Global to the view hierarchy used as a temporary for dealing with
12107 * x/y points in the ViewGroup.invalidateChild implementation.
12108 */
12109 final int[] mInvalidateChildLocation = new int[2];
12110
Chet Haasec3aa3612010-06-17 08:50:37 -070012111
12112 /**
12113 * Global to the view hierarchy used as a temporary for dealing with
12114 * x/y location when view is transformed.
12115 */
12116 final float[] mTmpTransformLocation = new float[2];
12117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118 /**
12119 * The view tree observer used to dispatch global events like
12120 * layout, pre-draw, touch mode change, etc.
12121 */
12122 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12123
12124 /**
12125 * A Canvas used by the view hierarchy to perform bitmap caching.
12126 */
12127 Canvas mCanvas;
12128
12129 /**
12130 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12131 * handler can be used to pump events in the UI events queue.
12132 */
12133 final Handler mHandler;
12134
12135 /**
12136 * Identifier for messages requesting the view to be invalidated.
12137 * Such messages should be sent to {@link #mHandler}.
12138 */
12139 static final int INVALIDATE_MSG = 0x1;
12140
12141 /**
12142 * Identifier for messages requesting the view to invalidate a region.
12143 * Such messages should be sent to {@link #mHandler}.
12144 */
12145 static final int INVALIDATE_RECT_MSG = 0x2;
12146
12147 /**
12148 * Temporary for use in computing invalidate rectangles while
12149 * calling up the hierarchy.
12150 */
12151 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012152
12153 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012154 * Temporary for use in computing hit areas with transformed views
12155 */
12156 final RectF mTmpTransformRect = new RectF();
12157
12158 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012159 * Temporary list for use in collecting focusable descendents of a view.
12160 */
12161 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 /**
12164 * Creates a new set of attachment information with the specified
12165 * events handler and thread.
12166 *
12167 * @param handler the events handler the view must use
12168 */
12169 AttachInfo(IWindowSession session, IWindow window,
12170 Handler handler, Callbacks effectPlayer) {
12171 mSession = session;
12172 mWindow = window;
12173 mWindowToken = window.asBinder();
12174 mHandler = handler;
12175 mRootCallbacks = effectPlayer;
12176 }
12177 }
12178
12179 /**
12180 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12181 * is supported. This avoids keeping too many unused fields in most
12182 * instances of View.</p>
12183 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012184 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012185
Mike Cleronf116bf82009-09-27 19:14:12 -070012186 /**
12187 * Scrollbars are not visible
12188 */
12189 public static final int OFF = 0;
12190
12191 /**
12192 * Scrollbars are visible
12193 */
12194 public static final int ON = 1;
12195
12196 /**
12197 * Scrollbars are fading away
12198 */
12199 public static final int FADING = 2;
12200
12201 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012203 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012204 public int scrollBarDefaultDelayBeforeFade;
12205 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012206
12207 public int scrollBarSize;
12208 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012209 public float[] interpolatorValues;
12210 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012211
12212 public final Paint paint;
12213 public final Matrix matrix;
12214 public Shader shader;
12215
Mike Cleronf116bf82009-09-27 19:14:12 -070012216 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12217
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012218 private static final float[] OPAQUE = { 255 };
12219 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012220
Mike Cleronf116bf82009-09-27 19:14:12 -070012221 /**
12222 * When fading should start. This time moves into the future every time
12223 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12224 */
12225 public long fadeStartTime;
12226
12227
12228 /**
12229 * The current state of the scrollbars: ON, OFF, or FADING
12230 */
12231 public int state = OFF;
12232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012233 private int mLastColor;
12234
Mike Cleronf116bf82009-09-27 19:14:12 -070012235 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012236 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12237 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012238 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12239 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240
12241 paint = new Paint();
12242 matrix = new Matrix();
12243 // use use a height of 1, and then wack the matrix each time we
12244 // actually use it.
12245 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012247 paint.setShader(shader);
12248 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012249 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012250 }
Romain Guy8506ab42009-06-11 17:35:47 -070012251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012252 public void setFadeColor(int color) {
12253 if (color != 0 && color != mLastColor) {
12254 mLastColor = color;
12255 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012256
Romain Guye55e1a72009-08-27 10:42:26 -070012257 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12258 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012259
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012260 paint.setShader(shader);
12261 // Restore the default transfer mode (src_over)
12262 paint.setXfermode(null);
12263 }
12264 }
Joe Malin32736f02011-01-19 16:14:20 -080012265
Mike Cleronf116bf82009-09-27 19:14:12 -070012266 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012267 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012268 if (now >= fadeStartTime) {
12269
12270 // the animation fades the scrollbars out by changing
12271 // the opacity (alpha) from fully opaque to fully
12272 // transparent
12273 int nextFrame = (int) now;
12274 int framesCount = 0;
12275
12276 Interpolator interpolator = scrollBarInterpolator;
12277
12278 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012279 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012280
12281 // End transparent
12282 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012283 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012284
12285 state = FADING;
12286
12287 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012288 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012289 }
12290 }
12291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292 }
12293}