blob: e18c5e00237ccbf69f8c9b8a993443d20b7aef50 [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;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Christopher Tatea0374192010-10-05 13:06:41 -070071import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import java.lang.reflect.InvocationTargetException;
73import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.util.ArrayList;
75import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070076import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080077import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078
79/**
80 * <p>
81 * This class represents the basic building block for user interface components. A View
82 * occupies a rectangular area on the screen and is responsible for drawing and
83 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070084 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
86 * are invisible containers that hold other Views (or other ViewGroups) and define
87 * their layout properties.
88 * </p>
89 *
90 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070091 * <p>For an introduction to using this class to develop your
92 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070094 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
103 * </p>
104 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700105 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 * <a name="Using"></a>
107 * <h3>Using Views</h3>
108 * <p>
109 * All of the views in a window are arranged in a single tree. You can add views
110 * either from code or by specifying a tree of views in one or more XML layout
111 * files. There are many specialized subclasses of views that act as controls or
112 * are capable of displaying text, images, or other content.
113 * </p>
114 * <p>
115 * Once you have created a tree of views, there are typically a few types of
116 * common operations you may wish to perform:
117 * <ul>
118 * <li><strong>Set properties:</strong> for example setting the text of a
119 * {@link android.widget.TextView}. The available properties and the methods
120 * that set them will vary among the different subclasses of views. Note that
121 * properties that are known at build time can be set in the XML layout
122 * files.</li>
123 * <li><strong>Set focus:</strong> The framework will handled moving focus in
124 * response to user input. To force focus to a specific view, call
125 * {@link #requestFocus}.</li>
126 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
127 * that will be notified when something interesting happens to the view. For
128 * example, all views will let you set a listener to be notified when the view
129 * gains or loses focus. You can register such a listener using
130 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
131 * specialized listeners. For example, a Button exposes a listener to notify
132 * clients when the button is clicked.</li>
133 * <li><strong>Set visibility:</strong> You can hide or show views using
134 * {@link #setVisibility}.</li>
135 * </ul>
136 * </p>
137 * <p><em>
138 * Note: The Android framework is responsible for measuring, laying out and
139 * drawing views. You should not call methods that perform these actions on
140 * views yourself unless you are actually implementing a
141 * {@link android.view.ViewGroup}.
142 * </em></p>
143 *
144 * <a name="Lifecycle"></a>
145 * <h3>Implementing a Custom View</h3>
146 *
147 * <p>
148 * To implement a custom view, you will usually begin by providing overrides for
149 * some of the standard methods that the framework calls on all views. You do
150 * not need to override all of these methods. In fact, you can start by just
151 * overriding {@link #onDraw(android.graphics.Canvas)}.
152 * <table border="2" width="85%" align="center" cellpadding="5">
153 * <thead>
154 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
155 * </thead>
156 *
157 * <tbody>
158 * <tr>
159 * <td rowspan="2">Creation</td>
160 * <td>Constructors</td>
161 * <td>There is a form of the constructor that are called when the view
162 * is created from code and a form that is called when the view is
163 * inflated from a layout file. The second form should parse and apply
164 * any attributes defined in the layout file.
165 * </td>
166 * </tr>
167 * <tr>
168 * <td><code>{@link #onFinishInflate()}</code></td>
169 * <td>Called after a view and all of its children has been inflated
170 * from XML.</td>
171 * </tr>
172 *
173 * <tr>
174 * <td rowspan="3">Layout</td>
175 * <td><code>{@link #onMeasure}</code></td>
176 * <td>Called to determine the size requirements for this view and all
177 * of its children.
178 * </td>
179 * </tr>
180 * <tr>
181 * <td><code>{@link #onLayout}</code></td>
182 * <td>Called when this view should assign a size and position to all
183 * of its children.
184 * </td>
185 * </tr>
186 * <tr>
187 * <td><code>{@link #onSizeChanged}</code></td>
188 * <td>Called when the size of this view has changed.
189 * </td>
190 * </tr>
191 *
192 * <tr>
193 * <td>Drawing</td>
194 * <td><code>{@link #onDraw}</code></td>
195 * <td>Called when the view should render its content.
196 * </td>
197 * </tr>
198 *
199 * <tr>
200 * <td rowspan="4">Event processing</td>
201 * <td><code>{@link #onKeyDown}</code></td>
202 * <td>Called when a new key event occurs.
203 * </td>
204 * </tr>
205 * <tr>
206 * <td><code>{@link #onKeyUp}</code></td>
207 * <td>Called when a key up event occurs.
208 * </td>
209 * </tr>
210 * <tr>
211 * <td><code>{@link #onTrackballEvent}</code></td>
212 * <td>Called when a trackball motion event occurs.
213 * </td>
214 * </tr>
215 * <tr>
216 * <td><code>{@link #onTouchEvent}</code></td>
217 * <td>Called when a touch screen motion event occurs.
218 * </td>
219 * </tr>
220 *
221 * <tr>
222 * <td rowspan="2">Focus</td>
223 * <td><code>{@link #onFocusChanged}</code></td>
224 * <td>Called when the view gains or loses focus.
225 * </td>
226 * </tr>
227 *
228 * <tr>
229 * <td><code>{@link #onWindowFocusChanged}</code></td>
230 * <td>Called when the window containing the view gains or loses focus.
231 * </td>
232 * </tr>
233 *
234 * <tr>
235 * <td rowspan="3">Attaching</td>
236 * <td><code>{@link #onAttachedToWindow()}</code></td>
237 * <td>Called when the view is attached to a window.
238 * </td>
239 * </tr>
240 *
241 * <tr>
242 * <td><code>{@link #onDetachedFromWindow}</code></td>
243 * <td>Called when the view is detached from its window.
244 * </td>
245 * </tr>
246 *
247 * <tr>
248 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
249 * <td>Called when the visibility of the window containing the view
250 * has changed.
251 * </td>
252 * </tr>
253 * </tbody>
254 *
255 * </table>
256 * </p>
257 *
258 * <a name="IDs"></a>
259 * <h3>IDs</h3>
260 * Views may have an integer id associated with them. These ids are typically
261 * assigned in the layout XML files, and are used to find specific views within
262 * the view tree. A common pattern is to:
263 * <ul>
264 * <li>Define a Button in the layout file and assign it a unique ID.
265 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700266 * &lt;Button
267 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 * android:layout_width="wrap_content"
269 * android:layout_height="wrap_content"
270 * android:text="@string/my_button_text"/&gt;
271 * </pre></li>
272 * <li>From the onCreate method of an Activity, find the Button
273 * <pre class="prettyprint">
274 * Button myButton = (Button) findViewById(R.id.my_button);
275 * </pre></li>
276 * </ul>
277 * <p>
278 * View IDs need not be unique throughout the tree, but it is good practice to
279 * ensure that they are at least unique within the part of the tree you are
280 * searching.
281 * </p>
282 *
283 * <a name="Position"></a>
284 * <h3>Position</h3>
285 * <p>
286 * The geometry of a view is that of a rectangle. A view has a location,
287 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
288 * two dimensions, expressed as a width and a height. The unit for location
289 * and dimensions is the pixel.
290 * </p>
291 *
292 * <p>
293 * It is possible to retrieve the location of a view by invoking the methods
294 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
295 * coordinate of the rectangle representing the view. The latter returns the
296 * top, or Y, coordinate of the rectangle representing the view. These methods
297 * both return the location of the view relative to its parent. For instance,
298 * when getLeft() returns 20, that means the view is located 20 pixels to the
299 * right of the left edge of its direct parent.
300 * </p>
301 *
302 * <p>
303 * In addition, several convenience methods are offered to avoid unnecessary
304 * computations, namely {@link #getRight()} and {@link #getBottom()}.
305 * These methods return the coordinates of the right and bottom edges of the
306 * rectangle representing the view. For instance, calling {@link #getRight()}
307 * is similar to the following computation: <code>getLeft() + getWidth()</code>
308 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
309 * </p>
310 *
311 * <a name="SizePaddingMargins"></a>
312 * <h3>Size, padding and margins</h3>
313 * <p>
314 * The size of a view is expressed with a width and a height. A view actually
315 * possess two pairs of width and height values.
316 * </p>
317 *
318 * <p>
319 * The first pair is known as <em>measured width</em> and
320 * <em>measured height</em>. These dimensions define how big a view wants to be
321 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
322 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
323 * and {@link #getMeasuredHeight()}.
324 * </p>
325 *
326 * <p>
327 * The second pair is simply known as <em>width</em> and <em>height</em>, or
328 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
329 * dimensions define the actual size of the view on screen, at drawing time and
330 * after layout. These values may, but do not have to, be different from the
331 * measured width and height. The width and height can be obtained by calling
332 * {@link #getWidth()} and {@link #getHeight()}.
333 * </p>
334 *
335 * <p>
336 * To measure its dimensions, a view takes into account its padding. The padding
337 * is expressed in pixels for the left, top, right and bottom parts of the view.
338 * Padding can be used to offset the content of the view by a specific amount of
339 * pixels. For instance, a left padding of 2 will push the view's content by
340 * 2 pixels to the right of the left edge. Padding can be set using the
341 * {@link #setPadding(int, int, int, int)} method and queried by calling
342 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
343 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
344 * </p>
345 *
346 * <p>
347 * Even though a view can define a padding, it does not provide any support for
348 * margins. However, view groups provide such a support. Refer to
349 * {@link android.view.ViewGroup} and
350 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
351 * </p>
352 *
353 * <a name="Layout"></a>
354 * <h3>Layout</h3>
355 * <p>
356 * Layout is a two pass process: a measure pass and a layout pass. The measuring
357 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
358 * of the view tree. Each view pushes dimension specifications down the tree
359 * during the recursion. At the end of the measure pass, every view has stored
360 * its measurements. The second pass happens in
361 * {@link #layout(int,int,int,int)} and is also top-down. During
362 * this pass each parent is responsible for positioning all of its children
363 * using the sizes computed in the measure pass.
364 * </p>
365 *
366 * <p>
367 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
368 * {@link #getMeasuredHeight()} values must be set, along with those for all of
369 * that view's descendants. A view's measured width and measured height values
370 * must respect the constraints imposed by the view's parents. This guarantees
371 * that at the end of the measure pass, all parents accept all of their
372 * children's measurements. A parent view may call measure() more than once on
373 * its children. For example, the parent may measure each child once with
374 * unspecified dimensions to find out how big they want to be, then call
375 * measure() on them again with actual numbers if the sum of all the children's
376 * unconstrained sizes is too big or too small.
377 * </p>
378 *
379 * <p>
380 * The measure pass uses two classes to communicate dimensions. The
381 * {@link MeasureSpec} class is used by views to tell their parents how they
382 * want to be measured and positioned. The base LayoutParams class just
383 * describes how big the view wants to be for both width and height. For each
384 * dimension, it can specify one of:
385 * <ul>
386 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800387 * <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 -0800388 * (minus padding)
389 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
390 * enclose its content (plus padding).
391 * </ul>
392 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
393 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
394 * an X and Y value.
395 * </p>
396 *
397 * <p>
398 * MeasureSpecs are used to push requirements down the tree from parent to
399 * child. A MeasureSpec can be in one of three modes:
400 * <ul>
401 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
402 * of a child view. For example, a LinearLayout may call measure() on its child
403 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
404 * tall the child view wants to be given a width of 240 pixels.
405 * <li>EXACTLY: This is used by the parent to impose an exact size on the
406 * child. The child must use this size, and guarantee that all of its
407 * descendants will fit within this size.
408 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
409 * child. The child must gurantee that it and all of its descendants will fit
410 * within this size.
411 * </ul>
412 * </p>
413 *
414 * <p>
415 * To intiate a layout, call {@link #requestLayout}. This method is typically
416 * called by a view on itself when it believes that is can no longer fit within
417 * its current bounds.
418 * </p>
419 *
420 * <a name="Drawing"></a>
421 * <h3>Drawing</h3>
422 * <p>
423 * Drawing is handled by walking the tree and rendering each view that
424 * intersects the the invalid region. Because the tree is traversed in-order,
425 * this means that parents will draw before (i.e., behind) their children, with
426 * siblings drawn in the order they appear in the tree.
427 * If you set a background drawable for a View, then the View will draw it for you
428 * before calling back to its <code>onDraw()</code> method.
429 * </p>
430 *
431 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700432 * 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 -0800433 * </p>
434 *
435 * <p>
436 * To force a view to draw, call {@link #invalidate()}.
437 * </p>
438 *
439 * <a name="EventHandlingThreading"></a>
440 * <h3>Event Handling and Threading</h3>
441 * <p>
442 * The basic cycle of a view is as follows:
443 * <ol>
444 * <li>An event comes in and is dispatched to the appropriate view. The view
445 * handles the event and notifies any listeners.</li>
446 * <li>If in the course of processing the event, the view's bounds may need
447 * to be changed, the view will call {@link #requestLayout()}.</li>
448 * <li>Similarly, if in the course of processing the event the view's appearance
449 * may need to be changed, the view will call {@link #invalidate()}.</li>
450 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
451 * the framework will take care of measuring, laying out, and drawing the tree
452 * as appropriate.</li>
453 * </ol>
454 * </p>
455 *
456 * <p><em>Note: The entire view tree is single threaded. You must always be on
457 * the UI thread when calling any method on any view.</em>
458 * If you are doing work on other threads and want to update the state of a view
459 * from that thread, you should use a {@link Handler}.
460 * </p>
461 *
462 * <a name="FocusHandling"></a>
463 * <h3>Focus Handling</h3>
464 * <p>
465 * The framework will handle routine focus movement in response to user input.
466 * This includes changing the focus as views are removed or hidden, or as new
467 * views become available. Views indicate their willingness to take focus
468 * through the {@link #isFocusable} method. To change whether a view can take
469 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
470 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
471 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
472 * </p>
473 * <p>
474 * Focus movement is based on an algorithm which finds the nearest neighbor in a
475 * given direction. In rare cases, the default algorithm may not match the
476 * intended behavior of the developer. In these situations, you can provide
477 * explicit overrides by using these XML attributes in the layout file:
478 * <pre>
479 * nextFocusDown
480 * nextFocusLeft
481 * nextFocusRight
482 * nextFocusUp
483 * </pre>
484 * </p>
485 *
486 *
487 * <p>
488 * To get a particular view to take focus, call {@link #requestFocus()}.
489 * </p>
490 *
491 * <a name="TouchMode"></a>
492 * <h3>Touch Mode</h3>
493 * <p>
494 * When a user is navigating a user interface via directional keys such as a D-pad, it is
495 * necessary to give focus to actionable items such as buttons so the user can see
496 * what will take input. If the device has touch capabilities, however, and the user
497 * begins interacting with the interface by touching it, it is no longer necessary to
498 * always highlight, or give focus to, a particular view. This motivates a mode
499 * for interaction named 'touch mode'.
500 * </p>
501 * <p>
502 * For a touch capable device, once the user touches the screen, the device
503 * will enter touch mode. From this point onward, only views for which
504 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
505 * Other views that are touchable, like buttons, will not take focus when touched; they will
506 * only fire the on click listeners.
507 * </p>
508 * <p>
509 * Any time a user hits a directional key, such as a D-pad direction, the view device will
510 * exit touch mode, and find a view to take focus, so that the user may resume interacting
511 * with the user interface without touching the screen again.
512 * </p>
513 * <p>
514 * The touch mode state is maintained across {@link android.app.Activity}s. Call
515 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
516 * </p>
517 *
518 * <a name="Scrolling"></a>
519 * <h3>Scrolling</h3>
520 * <p>
521 * The framework provides basic support for views that wish to internally
522 * scroll their content. This includes keeping track of the X and Y scroll
523 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800524 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700525 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526 * </p>
527 *
528 * <a name="Tags"></a>
529 * <h3>Tags</h3>
530 * <p>
531 * Unlike IDs, tags are not used to identify views. Tags are essentially an
532 * extra piece of information that can be associated with a view. They are most
533 * often used as a convenience to store data related to views in the views
534 * themselves rather than by putting them in a separate structure.
535 * </p>
536 *
537 * <a name="Animation"></a>
538 * <h3>Animation</h3>
539 * <p>
540 * You can attach an {@link Animation} object to a view using
541 * {@link #setAnimation(Animation)} or
542 * {@link #startAnimation(Animation)}. The animation can alter the scale,
543 * rotation, translation and alpha of a view over time. If the animation is
544 * attached to a view that has children, the animation will affect the entire
545 * subtree rooted by that node. When an animation is started, the framework will
546 * take care of redrawing the appropriate views until the animation completes.
547 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800548 * <p>
549 * Starting with Android 3.0, the preferred way of animating views is to use the
550 * {@link android.animation} package APIs.
551 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 *
Jeff Brown85a31762010-09-01 17:01:00 -0700553 * <a name="Security"></a>
554 * <h3>Security</h3>
555 * <p>
556 * Sometimes it is essential that an application be able to verify that an action
557 * is being performed with the full knowledge and consent of the user, such as
558 * granting a permission request, making a purchase or clicking on an advertisement.
559 * Unfortunately, a malicious application could try to spoof the user into
560 * performing these actions, unaware, by concealing the intended purpose of the view.
561 * As a remedy, the framework offers a touch filtering mechanism that can be used to
562 * improve the security of views that provide access to sensitive functionality.
563 * </p><p>
564 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800565 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700566 * will discard touches that are received whenever the view's window is obscured by
567 * another visible window. As a result, the view will not receive touches whenever a
568 * toast, dialog or other window appears above the view's window.
569 * </p><p>
570 * For more fine-grained control over security, consider overriding the
571 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
572 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
573 * </p>
574 *
Romain Guy171c5922011-01-06 10:04:23 -0800575 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700576 * @attr ref android.R.styleable#View_background
577 * @attr ref android.R.styleable#View_clickable
578 * @attr ref android.R.styleable#View_contentDescription
579 * @attr ref android.R.styleable#View_drawingCacheQuality
580 * @attr ref android.R.styleable#View_duplicateParentState
581 * @attr ref android.R.styleable#View_id
582 * @attr ref android.R.styleable#View_fadingEdge
583 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700584 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700586 * @attr ref android.R.styleable#View_isScrollContainer
587 * @attr ref android.R.styleable#View_focusable
588 * @attr ref android.R.styleable#View_focusableInTouchMode
589 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
590 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800591 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700592 * @attr ref android.R.styleable#View_longClickable
593 * @attr ref android.R.styleable#View_minHeight
594 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 * @attr ref android.R.styleable#View_nextFocusDown
596 * @attr ref android.R.styleable#View_nextFocusLeft
597 * @attr ref android.R.styleable#View_nextFocusRight
598 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700599 * @attr ref android.R.styleable#View_onClick
600 * @attr ref android.R.styleable#View_padding
601 * @attr ref android.R.styleable#View_paddingBottom
602 * @attr ref android.R.styleable#View_paddingLeft
603 * @attr ref android.R.styleable#View_paddingRight
604 * @attr ref android.R.styleable#View_paddingTop
605 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800606 * @attr ref android.R.styleable#View_rotation
607 * @attr ref android.R.styleable#View_rotationX
608 * @attr ref android.R.styleable#View_rotationY
609 * @attr ref android.R.styleable#View_scaleX
610 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800611 * @attr ref android.R.styleable#View_scrollX
612 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700613 * @attr ref android.R.styleable#View_scrollbarSize
614 * @attr ref android.R.styleable#View_scrollbarStyle
615 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700616 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
617 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
619 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800620 * @attr ref android.R.styleable#View_scrollbarThumbVertical
621 * @attr ref android.R.styleable#View_scrollbarTrackVertical
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700624 * @attr ref android.R.styleable#View_soundEffectsEnabled
625 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800626 * @attr ref android.R.styleable#View_transformPivotX
627 * @attr ref android.R.styleable#View_transformPivotY
628 * @attr ref android.R.styleable#View_translationX
629 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700630 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 *
632 * @see android.view.ViewGroup
633 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700634public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 private static final boolean DBG = false;
636
637 /**
638 * The logging tag used by this class with android.util.Log.
639 */
640 protected static final String VIEW_LOG_TAG = "View";
641
642 /**
643 * Used to mark a View that has no ID.
644 */
645 public static final int NO_ID = -1;
646
647 /**
648 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
649 * calling setFlags.
650 */
651 private static final int NOT_FOCUSABLE = 0x00000000;
652
653 /**
654 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
655 * setFlags.
656 */
657 private static final int FOCUSABLE = 0x00000001;
658
659 /**
660 * Mask for use with setFlags indicating bits used for focus.
661 */
662 private static final int FOCUSABLE_MASK = 0x00000001;
663
664 /**
665 * This view will adjust its padding to fit sytem windows (e.g. status bar)
666 */
667 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
668
669 /**
670 * This view is visible. Use with {@link #setVisibility}.
671 */
672 public static final int VISIBLE = 0x00000000;
673
674 /**
675 * This view is invisible, but it still takes up space for layout purposes.
676 * Use with {@link #setVisibility}.
677 */
678 public static final int INVISIBLE = 0x00000004;
679
680 /**
681 * This view is invisible, and it doesn't take any space for layout
682 * purposes. Use with {@link #setVisibility}.
683 */
684 public static final int GONE = 0x00000008;
685
686 /**
687 * Mask for use with setFlags indicating bits used for visibility.
688 * {@hide}
689 */
690 static final int VISIBILITY_MASK = 0x0000000C;
691
692 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
693
694 /**
695 * This view is enabled. Intrepretation varies by subclass.
696 * Use with ENABLED_MASK when calling setFlags.
697 * {@hide}
698 */
699 static final int ENABLED = 0x00000000;
700
701 /**
702 * This view is disabled. Intrepretation varies by subclass.
703 * Use with ENABLED_MASK when calling setFlags.
704 * {@hide}
705 */
706 static final int DISABLED = 0x00000020;
707
708 /**
709 * Mask for use with setFlags indicating bits used for indicating whether
710 * this view is enabled
711 * {@hide}
712 */
713 static final int ENABLED_MASK = 0x00000020;
714
715 /**
716 * This view won't draw. {@link #onDraw} won't be called and further
717 * optimizations
718 * will be performed. It is okay to have this flag set and a background.
719 * Use with DRAW_MASK when calling setFlags.
720 * {@hide}
721 */
722 static final int WILL_NOT_DRAW = 0x00000080;
723
724 /**
725 * Mask for use with setFlags indicating bits used for indicating whether
726 * this view is will draw
727 * {@hide}
728 */
729 static final int DRAW_MASK = 0x00000080;
730
731 /**
732 * <p>This view doesn't show scrollbars.</p>
733 * {@hide}
734 */
735 static final int SCROLLBARS_NONE = 0x00000000;
736
737 /**
738 * <p>This view shows horizontal scrollbars.</p>
739 * {@hide}
740 */
741 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
742
743 /**
744 * <p>This view shows vertical scrollbars.</p>
745 * {@hide}
746 */
747 static final int SCROLLBARS_VERTICAL = 0x00000200;
748
749 /**
750 * <p>Mask for use with setFlags indicating bits used for indicating which
751 * scrollbars are enabled.</p>
752 * {@hide}
753 */
754 static final int SCROLLBARS_MASK = 0x00000300;
755
Jeff Brown85a31762010-09-01 17:01:00 -0700756 /**
757 * Indicates that the view should filter touches when its window is obscured.
758 * Refer to the class comments for more information about this security feature.
759 * {@hide}
760 */
761 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
762
763 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764
765 /**
766 * <p>This view doesn't show fading edges.</p>
767 * {@hide}
768 */
769 static final int FADING_EDGE_NONE = 0x00000000;
770
771 /**
772 * <p>This view shows horizontal fading edges.</p>
773 * {@hide}
774 */
775 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
776
777 /**
778 * <p>This view shows vertical fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_VERTICAL = 0x00002000;
782
783 /**
784 * <p>Mask for use with setFlags indicating bits used for indicating which
785 * fading edges are enabled.</p>
786 * {@hide}
787 */
788 static final int FADING_EDGE_MASK = 0x00003000;
789
790 /**
791 * <p>Indicates this view can be clicked. When clickable, a View reacts
792 * to clicks by notifying the OnClickListener.<p>
793 * {@hide}
794 */
795 static final int CLICKABLE = 0x00004000;
796
797 /**
798 * <p>Indicates this view is caching its drawing into a bitmap.</p>
799 * {@hide}
800 */
801 static final int DRAWING_CACHE_ENABLED = 0x00008000;
802
803 /**
804 * <p>Indicates that no icicle should be saved for this view.<p>
805 * {@hide}
806 */
807 static final int SAVE_DISABLED = 0x000010000;
808
809 /**
810 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
811 * property.</p>
812 * {@hide}
813 */
814 static final int SAVE_DISABLED_MASK = 0x000010000;
815
816 /**
817 * <p>Indicates that no drawing cache should ever be created for this view.<p>
818 * {@hide}
819 */
820 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
821
822 /**
823 * <p>Indicates this view can take / keep focus when int touch mode.</p>
824 * {@hide}
825 */
826 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
827
828 /**
829 * <p>Enables low quality mode for the drawing cache.</p>
830 */
831 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
832
833 /**
834 * <p>Enables high quality mode for the drawing cache.</p>
835 */
836 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
837
838 /**
839 * <p>Enables automatic quality mode for the drawing cache.</p>
840 */
841 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
842
843 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
844 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
845 };
846
847 /**
848 * <p>Mask for use with setFlags indicating bits used for the cache
849 * quality property.</p>
850 * {@hide}
851 */
852 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
853
854 /**
855 * <p>
856 * Indicates this view can be long clicked. When long clickable, a View
857 * reacts to long clicks by notifying the OnLongClickListener or showing a
858 * context menu.
859 * </p>
860 * {@hide}
861 */
862 static final int LONG_CLICKABLE = 0x00200000;
863
864 /**
865 * <p>Indicates that this view gets its drawable states from its direct parent
866 * and ignores its original internal states.</p>
867 *
868 * @hide
869 */
870 static final int DUPLICATE_PARENT_STATE = 0x00400000;
871
872 /**
873 * The scrollbar style to display the scrollbars inside the content area,
874 * without increasing the padding. The scrollbars will be overlaid with
875 * translucency on the view's content.
876 */
877 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
878
879 /**
880 * The scrollbar style to display the scrollbars inside the padded area,
881 * increasing the padding of the view. The scrollbars will not overlap the
882 * content area of the view.
883 */
884 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
885
886 /**
887 * The scrollbar style to display the scrollbars at the edge of the view,
888 * without increasing the padding. The scrollbars will be overlaid with
889 * translucency.
890 */
891 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
892
893 /**
894 * The scrollbar style to display the scrollbars at the edge of the view,
895 * increasing the padding of the view. The scrollbars will only overlap the
896 * background, if any.
897 */
898 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
899
900 /**
901 * Mask to check if the scrollbar style is overlay or inset.
902 * {@hide}
903 */
904 static final int SCROLLBARS_INSET_MASK = 0x01000000;
905
906 /**
907 * Mask to check if the scrollbar style is inside or outside.
908 * {@hide}
909 */
910 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
911
912 /**
913 * Mask for scrollbar style.
914 * {@hide}
915 */
916 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
917
918 /**
919 * View flag indicating that the screen should remain on while the
920 * window containing this view is visible to the user. This effectively
921 * takes care of automatically setting the WindowManager's
922 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
923 */
924 public static final int KEEP_SCREEN_ON = 0x04000000;
925
926 /**
927 * View flag indicating whether this view should have sound effects enabled
928 * for events such as clicking and touching.
929 */
930 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
931
932 /**
933 * View flag indicating whether this view should have haptic feedback
934 * enabled for events such as long presses.
935 */
936 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
937
938 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700939 * <p>Indicates that the view hierarchy should stop saving state when
940 * it reaches this view. If state saving is initiated immediately at
941 * the view, it will be allowed.
942 * {@hide}
943 */
944 static final int PARENT_SAVE_DISABLED = 0x20000000;
945
946 /**
947 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
948 * {@hide}
949 */
950 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
951
952 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700953 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
954 * should add all focusable Views regardless if they are focusable in touch mode.
955 */
956 public static final int FOCUSABLES_ALL = 0x00000000;
957
958 /**
959 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
960 * should add only Views focusable in touch mode.
961 */
962 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
963
964 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 * Use with {@link #focusSearch}. Move focus to the previous selectable
966 * item.
967 */
968 public static final int FOCUS_BACKWARD = 0x00000001;
969
970 /**
971 * Use with {@link #focusSearch}. Move focus to the next selectable
972 * item.
973 */
974 public static final int FOCUS_FORWARD = 0x00000002;
975
976 /**
977 * Use with {@link #focusSearch}. Move focus to the left.
978 */
979 public static final int FOCUS_LEFT = 0x00000011;
980
981 /**
982 * Use with {@link #focusSearch}. Move focus up.
983 */
984 public static final int FOCUS_UP = 0x00000021;
985
986 /**
987 * Use with {@link #focusSearch}. Move focus to the right.
988 */
989 public static final int FOCUS_RIGHT = 0x00000042;
990
991 /**
992 * Use with {@link #focusSearch}. Move focus down.
993 */
994 public static final int FOCUS_DOWN = 0x00000082;
995
996 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800997 * Bits of {@link #getMeasuredWidthAndState()} and
998 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
999 */
1000 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1001
1002 /**
1003 * Bits of {@link #getMeasuredWidthAndState()} and
1004 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1005 */
1006 public static final int MEASURED_STATE_MASK = 0xff000000;
1007
1008 /**
1009 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1010 * for functions that combine both width and height into a single int,
1011 * such as {@link #getMeasuredState()} and the childState argument of
1012 * {@link #resolveSizeAndState(int, int, int)}.
1013 */
1014 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1015
1016 /**
1017 * Bit of {@link #getMeasuredWidthAndState()} and
1018 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1019 * is smaller that the space the view would like to have.
1020 */
1021 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1022
1023 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 * Base View state sets
1025 */
1026 // Singles
1027 /**
1028 * Indicates the view has no states set. States are used with
1029 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1030 * view depending on its state.
1031 *
1032 * @see android.graphics.drawable.Drawable
1033 * @see #getDrawableState()
1034 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001035 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001036 /**
1037 * Indicates the view is enabled. States are used with
1038 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1039 * view depending on its state.
1040 *
1041 * @see android.graphics.drawable.Drawable
1042 * @see #getDrawableState()
1043 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001044 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 /**
1046 * Indicates the view is focused. States are used with
1047 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1048 * view depending on its state.
1049 *
1050 * @see android.graphics.drawable.Drawable
1051 * @see #getDrawableState()
1052 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001053 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001054 /**
1055 * Indicates the view is selected. States are used with
1056 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1057 * view depending on its state.
1058 *
1059 * @see android.graphics.drawable.Drawable
1060 * @see #getDrawableState()
1061 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001062 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 /**
1064 * Indicates the view is pressed. States are used with
1065 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1066 * view depending on its state.
1067 *
1068 * @see android.graphics.drawable.Drawable
1069 * @see #getDrawableState()
1070 * @hide
1071 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001072 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 /**
1074 * Indicates the view's window has focus. States are used with
1075 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1076 * view depending on its state.
1077 *
1078 * @see android.graphics.drawable.Drawable
1079 * @see #getDrawableState()
1080 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001081 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 // Doubles
1083 /**
1084 * Indicates the view is enabled and has the focus.
1085 *
1086 * @see #ENABLED_STATE_SET
1087 * @see #FOCUSED_STATE_SET
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is enabled and selected.
1092 *
1093 * @see #ENABLED_STATE_SET
1094 * @see #SELECTED_STATE_SET
1095 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001096 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001097 /**
1098 * Indicates the view is enabled and that its window has focus.
1099 *
1100 * @see #ENABLED_STATE_SET
1101 * @see #WINDOW_FOCUSED_STATE_SET
1102 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001103 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 /**
1105 * Indicates the view is focused and selected.
1106 *
1107 * @see #FOCUSED_STATE_SET
1108 * @see #SELECTED_STATE_SET
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view has the focus and that its window has the focus.
1113 *
1114 * @see #FOCUSED_STATE_SET
1115 * @see #WINDOW_FOCUSED_STATE_SET
1116 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001117 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001118 /**
1119 * Indicates the view is selected and that its window has the focus.
1120 *
1121 * @see #SELECTED_STATE_SET
1122 * @see #WINDOW_FOCUSED_STATE_SET
1123 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001124 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001125 // Triples
1126 /**
1127 * Indicates the view is enabled, focused and selected.
1128 *
1129 * @see #ENABLED_STATE_SET
1130 * @see #FOCUSED_STATE_SET
1131 * @see #SELECTED_STATE_SET
1132 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001133 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 /**
1135 * Indicates the view is enabled, focused and its window has the focus.
1136 *
1137 * @see #ENABLED_STATE_SET
1138 * @see #FOCUSED_STATE_SET
1139 * @see #WINDOW_FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is enabled, selected and its window has the focus.
1144 *
1145 * @see #ENABLED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 * @see #WINDOW_FOCUSED_STATE_SET
1148 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001149 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 /**
1151 * Indicates the view is focused, selected and its window has the focus.
1152 *
1153 * @see #FOCUSED_STATE_SET
1154 * @see #SELECTED_STATE_SET
1155 * @see #WINDOW_FOCUSED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view is enabled, focused, selected and its window
1160 * has the focus.
1161 *
1162 * @see #ENABLED_STATE_SET
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 * @see #WINDOW_FOCUSED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view is pressed and its window has the focus.
1170 *
1171 * @see #PRESSED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Indicates the view is pressed and selected.
1177 *
1178 * @see #PRESSED_STATE_SET
1179 * @see #SELECTED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 /**
1183 * Indicates the view is pressed, selected and its window has the focus.
1184 *
1185 * @see #PRESSED_STATE_SET
1186 * @see #SELECTED_STATE_SET
1187 * @see #WINDOW_FOCUSED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is pressed and focused.
1192 *
1193 * @see #PRESSED_STATE_SET
1194 * @see #FOCUSED_STATE_SET
1195 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001196 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 /**
1198 * Indicates the view is pressed, focused and its window has the focus.
1199 *
1200 * @see #PRESSED_STATE_SET
1201 * @see #FOCUSED_STATE_SET
1202 * @see #WINDOW_FOCUSED_STATE_SET
1203 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001204 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 /**
1206 * Indicates the view is pressed, focused and selected.
1207 *
1208 * @see #PRESSED_STATE_SET
1209 * @see #SELECTED_STATE_SET
1210 * @see #FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is pressed, focused, selected and its window has the focus.
1215 *
1216 * @see #PRESSED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and enabled.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #ENABLED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed, enabled and its window has the focus.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #ENABLED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001236 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 /**
1238 * Indicates the view is pressed, enabled and selected.
1239 *
1240 * @see #PRESSED_STATE_SET
1241 * @see #ENABLED_STATE_SET
1242 * @see #SELECTED_STATE_SET
1243 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001244 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 /**
1246 * Indicates the view is pressed, enabled, selected and its window has the
1247 * focus.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #ENABLED_STATE_SET
1251 * @see #SELECTED_STATE_SET
1252 * @see #WINDOW_FOCUSED_STATE_SET
1253 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001254 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 /**
1256 * Indicates the view is pressed, enabled and focused.
1257 *
1258 * @see #PRESSED_STATE_SET
1259 * @see #ENABLED_STATE_SET
1260 * @see #FOCUSED_STATE_SET
1261 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001262 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 /**
1264 * Indicates the view is pressed, enabled, focused and its window has the
1265 * focus.
1266 *
1267 * @see #PRESSED_STATE_SET
1268 * @see #ENABLED_STATE_SET
1269 * @see #FOCUSED_STATE_SET
1270 * @see #WINDOW_FOCUSED_STATE_SET
1271 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001272 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 /**
1274 * Indicates the view is pressed, enabled, focused and selected.
1275 *
1276 * @see #PRESSED_STATE_SET
1277 * @see #ENABLED_STATE_SET
1278 * @see #SELECTED_STATE_SET
1279 * @see #FOCUSED_STATE_SET
1280 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001281 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 /**
1283 * Indicates the view is pressed, enabled, focused, selected and its window
1284 * has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #SELECTED_STATE_SET
1289 * @see #FOCUSED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293
1294 /**
1295 * The order here is very important to {@link #getDrawableState()}
1296 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001297 private static final int[][] VIEW_STATE_SETS;
1298
Romain Guyb051e892010-09-28 19:09:36 -07001299 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1300 static final int VIEW_STATE_SELECTED = 1 << 1;
1301 static final int VIEW_STATE_FOCUSED = 1 << 2;
1302 static final int VIEW_STATE_ENABLED = 1 << 3;
1303 static final int VIEW_STATE_PRESSED = 1 << 4;
1304 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001305 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001306
1307 static final int[] VIEW_STATE_IDS = new int[] {
1308 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1309 R.attr.state_selected, VIEW_STATE_SELECTED,
1310 R.attr.state_focused, VIEW_STATE_FOCUSED,
1311 R.attr.state_enabled, VIEW_STATE_ENABLED,
1312 R.attr.state_pressed, VIEW_STATE_PRESSED,
1313 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001314 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 };
1316
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001317 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1319 throw new IllegalStateException(
1320 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1321 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001322 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001323 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001325 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001327 orderedIds[i * 2] = viewState;
1328 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 }
1330 }
1331 }
Romain Guyb051e892010-09-28 19:09:36 -07001332 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1333 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1334 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 int numBits = Integer.bitCount(i);
1336 int[] set = new int[numBits];
1337 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001338 for (int j = 0; j < orderedIds.length; j += 2) {
1339 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001340 set[pos++] = orderedIds[j];
1341 }
1342 }
1343 VIEW_STATE_SETS[i] = set;
1344 }
1345
1346 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1347 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1348 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1349 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1350 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1351 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1352 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1353 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1354 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1355 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1356 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1357 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1358 | VIEW_STATE_FOCUSED];
1359 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1360 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1361 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1362 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1363 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1364 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1365 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1366 | VIEW_STATE_ENABLED];
1367 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1369 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1370 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1371 | VIEW_STATE_ENABLED];
1372 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1373 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1374 | VIEW_STATE_ENABLED];
1375 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1376 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1377 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1378
1379 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1380 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1381 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1382 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1384 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1385 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1386 | VIEW_STATE_PRESSED];
1387 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1389 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1390 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1391 | VIEW_STATE_PRESSED];
1392 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1393 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1394 | VIEW_STATE_PRESSED];
1395 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1396 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1397 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1398 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1400 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1402 | VIEW_STATE_PRESSED];
1403 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1404 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1405 | VIEW_STATE_PRESSED];
1406 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1408 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1409 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1411 | VIEW_STATE_PRESSED];
1412 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1414 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1415 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1417 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1418 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1421 | VIEW_STATE_PRESSED];
1422 }
1423
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 * Temporary Rect currently for use in setBackground(). This will probably
1426 * be extended in the future to hold our own class with more than just
1427 * a Rect. :)
1428 */
1429 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001430
1431 /**
1432 * Map used to store views' tags.
1433 */
1434 private static WeakHashMap<View, SparseArray<Object>> sTags;
1435
1436 /**
1437 * Lock used to access sTags.
1438 */
1439 private static final Object sTagsLock = new Object();
1440
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001441 /**
1442 * The animation currently associated with this view.
1443 * @hide
1444 */
1445 protected Animation mCurrentAnimation = null;
1446
1447 /**
1448 * Width as measured during measure pass.
1449 * {@hide}
1450 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001451 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001452 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453
1454 /**
1455 * Height as measured during measure pass.
1456 * {@hide}
1457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001458 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001459 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460
1461 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001462 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1463 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1464 * its display list. This flag, used only when hw accelerated, allows us to clear the
1465 * flag while retaining this information until it's needed (at getDisplayList() time and
1466 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1467 *
1468 * {@hide}
1469 */
1470 boolean mRecreateDisplayList = false;
1471
1472 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 * The view's identifier.
1474 * {@hide}
1475 *
1476 * @see #setId(int)
1477 * @see #getId()
1478 */
1479 @ViewDebug.ExportedProperty(resolveId = true)
1480 int mID = NO_ID;
1481
1482 /**
1483 * The view's tag.
1484 * {@hide}
1485 *
1486 * @see #setTag(Object)
1487 * @see #getTag()
1488 */
1489 protected Object mTag;
1490
1491 // for mPrivateFlags:
1492 /** {@hide} */
1493 static final int WANTS_FOCUS = 0x00000001;
1494 /** {@hide} */
1495 static final int FOCUSED = 0x00000002;
1496 /** {@hide} */
1497 static final int SELECTED = 0x00000004;
1498 /** {@hide} */
1499 static final int IS_ROOT_NAMESPACE = 0x00000008;
1500 /** {@hide} */
1501 static final int HAS_BOUNDS = 0x00000010;
1502 /** {@hide} */
1503 static final int DRAWN = 0x00000020;
1504 /**
1505 * When this flag is set, this view is running an animation on behalf of its
1506 * children and should therefore not cancel invalidate requests, even if they
1507 * lie outside of this view's bounds.
1508 *
1509 * {@hide}
1510 */
1511 static final int DRAW_ANIMATION = 0x00000040;
1512 /** {@hide} */
1513 static final int SKIP_DRAW = 0x00000080;
1514 /** {@hide} */
1515 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1516 /** {@hide} */
1517 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1518 /** {@hide} */
1519 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1520 /** {@hide} */
1521 static final int MEASURED_DIMENSION_SET = 0x00000800;
1522 /** {@hide} */
1523 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001524 /** {@hide} */
1525 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526
1527 private static final int PRESSED = 0x00004000;
1528
1529 /** {@hide} */
1530 static final int DRAWING_CACHE_VALID = 0x00008000;
1531 /**
1532 * Flag used to indicate that this view should be drawn once more (and only once
1533 * more) after its animation has completed.
1534 * {@hide}
1535 */
1536 static final int ANIMATION_STARTED = 0x00010000;
1537
1538 private static final int SAVE_STATE_CALLED = 0x00020000;
1539
1540 /**
1541 * Indicates that the View returned true when onSetAlpha() was called and that
1542 * the alpha must be restored.
1543 * {@hide}
1544 */
1545 static final int ALPHA_SET = 0x00040000;
1546
1547 /**
1548 * Set by {@link #setScrollContainer(boolean)}.
1549 */
1550 static final int SCROLL_CONTAINER = 0x00080000;
1551
1552 /**
1553 * Set by {@link #setScrollContainer(boolean)}.
1554 */
1555 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1556
1557 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001558 * View flag indicating whether this view was invalidated (fully or partially.)
1559 *
1560 * @hide
1561 */
1562 static final int DIRTY = 0x00200000;
1563
1564 /**
1565 * View flag indicating whether this view was invalidated by an opaque
1566 * invalidate request.
1567 *
1568 * @hide
1569 */
1570 static final int DIRTY_OPAQUE = 0x00400000;
1571
1572 /**
1573 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1574 *
1575 * @hide
1576 */
1577 static final int DIRTY_MASK = 0x00600000;
1578
1579 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001580 * Indicates whether the background is opaque.
1581 *
1582 * @hide
1583 */
1584 static final int OPAQUE_BACKGROUND = 0x00800000;
1585
1586 /**
1587 * Indicates whether the scrollbars are opaque.
1588 *
1589 * @hide
1590 */
1591 static final int OPAQUE_SCROLLBARS = 0x01000000;
1592
1593 /**
1594 * Indicates whether the view is opaque.
1595 *
1596 * @hide
1597 */
1598 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001599
Adam Powelle14579b2009-12-16 18:39:52 -08001600 /**
1601 * Indicates a prepressed state;
1602 * the short time between ACTION_DOWN and recognizing
1603 * a 'real' press. Prepressed is used to recognize quick taps
1604 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001605 *
Adam Powelle14579b2009-12-16 18:39:52 -08001606 * @hide
1607 */
1608 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001609
Adam Powellc9fbaab2010-02-16 17:16:19 -08001610 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001611 * Indicates whether the view is temporarily detached.
1612 *
1613 * @hide
1614 */
1615 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001616
Adam Powell8568c3a2010-04-19 14:26:11 -07001617 /**
1618 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001619 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001620 * @hide
1621 */
1622 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001623
1624 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001625 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1626 * for transform operations
1627 *
1628 * @hide
1629 */
Adam Powellf37df072010-09-17 16:22:49 -07001630 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001631
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001632 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001633 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001634
Chet Haasefd2b0022010-08-06 13:08:56 -07001635 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001636 * Indicates that this view was specifically invalidated, not just dirtied because some
1637 * child view was invalidated. The flag is used to determine when we need to recreate
1638 * a view's display list (as opposed to just returning a reference to its existing
1639 * display list).
1640 *
1641 * @hide
1642 */
1643 static final int INVALIDATED = 0x80000000;
1644
1645 /**
Adam Powell637d3372010-08-25 14:37:03 -07001646 * Always allow a user to over-scroll this view, provided it is a
1647 * view that can scroll.
1648 *
1649 * @see #getOverScrollMode()
1650 * @see #setOverScrollMode(int)
1651 */
1652 public static final int OVER_SCROLL_ALWAYS = 0;
1653
1654 /**
1655 * Allow a user to over-scroll this view only if the content is large
1656 * enough to meaningfully scroll, provided it is a view that can scroll.
1657 *
1658 * @see #getOverScrollMode()
1659 * @see #setOverScrollMode(int)
1660 */
1661 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1662
1663 /**
1664 * Never allow a user to over-scroll this view.
1665 *
1666 * @see #getOverScrollMode()
1667 * @see #setOverScrollMode(int)
1668 */
1669 public static final int OVER_SCROLL_NEVER = 2;
1670
1671 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001672 * View has requested the status bar to be visible (the default).
1673 *
Joe Malin32736f02011-01-19 16:14:20 -08001674 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001675 */
1676 public static final int STATUS_BAR_VISIBLE = 0;
1677
1678 /**
1679 * View has requested the status bar to be visible (the default).
1680 *
Joe Malin32736f02011-01-19 16:14:20 -08001681 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001682 */
1683 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1684
1685 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001686 * @hide
1687 *
1688 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1689 * out of the public fields to keep the undefined bits out of the developer's way.
1690 *
1691 * Flag to make the status bar not expandable. Unless you also
1692 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1693 */
1694 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1695
1696 /**
1697 * @hide
1698 *
1699 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1700 * out of the public fields to keep the undefined bits out of the developer's way.
1701 *
1702 * Flag to hide notification icons and scrolling ticker text.
1703 */
1704 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1705
1706 /**
1707 * @hide
1708 *
1709 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1710 * out of the public fields to keep the undefined bits out of the developer's way.
1711 *
1712 * Flag to disable incoming notification alerts. This will not block
1713 * icons, but it will block sound, vibrating and other visual or aural notifications.
1714 */
1715 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1716
1717 /**
1718 * @hide
1719 *
1720 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1721 * out of the public fields to keep the undefined bits out of the developer's way.
1722 *
1723 * Flag to hide only the scrolling ticker. Note that
1724 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1725 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1726 */
1727 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1728
1729 /**
1730 * @hide
1731 *
1732 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1733 * out of the public fields to keep the undefined bits out of the developer's way.
1734 *
1735 * Flag to hide the center system info area.
1736 */
1737 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1738
1739 /**
1740 * @hide
1741 *
1742 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1743 * out of the public fields to keep the undefined bits out of the developer's way.
1744 *
1745 * Flag to hide only the navigation buttons. Don't use this
1746 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001747 *
1748 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001749 */
1750 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1751
1752 /**
1753 * @hide
1754 *
1755 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1756 * out of the public fields to keep the undefined bits out of the developer's way.
1757 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001758 * Flag to hide only the back button. Don't use this
1759 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1760 */
1761 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1762
1763 /**
1764 * @hide
1765 *
1766 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1767 * out of the public fields to keep the undefined bits out of the developer's way.
1768 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001769 * Flag to hide only the clock. You might use this if your activity has
1770 * its own clock making the status bar's clock redundant.
1771 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001772 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1773
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001774
1775 /**
1776 * @hide
1777 */
1778 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
1779
1780
1781 /**
Adam Powell637d3372010-08-25 14:37:03 -07001782 * Controls the over-scroll mode for this view.
1783 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1784 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1785 * and {@link #OVER_SCROLL_NEVER}.
1786 */
1787 private int mOverScrollMode;
1788
1789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 * The parent this view is attached to.
1791 * {@hide}
1792 *
1793 * @see #getParent()
1794 */
1795 protected ViewParent mParent;
1796
1797 /**
1798 * {@hide}
1799 */
1800 AttachInfo mAttachInfo;
1801
1802 /**
1803 * {@hide}
1804 */
Romain Guy809a7f62009-05-14 15:44:42 -07001805 @ViewDebug.ExportedProperty(flagMapping = {
1806 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1807 name = "FORCE_LAYOUT"),
1808 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1809 name = "LAYOUT_REQUIRED"),
1810 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001811 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1813 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1815 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1816 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 int mPrivateFlags;
1818
1819 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001820 * This view's request for the visibility of the status bar.
1821 * @hide
1822 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001823 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001824 int mSystemUiVisibility;
1825
1826 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 * Count of how many windows this view has been attached to.
1828 */
1829 int mWindowAttachCount;
1830
1831 /**
1832 * The layout parameters associated with this view and used by the parent
1833 * {@link android.view.ViewGroup} to determine how this view should be
1834 * laid out.
1835 * {@hide}
1836 */
1837 protected ViewGroup.LayoutParams mLayoutParams;
1838
1839 /**
1840 * The view flags hold various views states.
1841 * {@hide}
1842 */
1843 @ViewDebug.ExportedProperty
1844 int mViewFlags;
1845
1846 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001847 * The transform matrix for the View. This transform is calculated internally
1848 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1849 * is used by default. Do *not* use this variable directly; instead call
1850 * getMatrix(), which will automatically recalculate the matrix if necessary
1851 * to get the correct matrix based on the latest rotation and scale properties.
1852 */
1853 private final Matrix mMatrix = new Matrix();
1854
1855 /**
1856 * The transform matrix for the View. This transform is calculated internally
1857 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1858 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001859 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001860 * to get the correct matrix based on the latest rotation and scale properties.
1861 */
1862 private Matrix mInverseMatrix;
1863
1864 /**
1865 * An internal variable that tracks whether we need to recalculate the
1866 * transform matrix, based on whether the rotation or scaleX/Y properties
1867 * have changed since the matrix was last calculated.
1868 */
1869 private boolean mMatrixDirty = false;
1870
1871 /**
1872 * An internal variable that tracks whether we need to recalculate the
1873 * transform matrix, based on whether the rotation or scaleX/Y properties
1874 * have changed since the matrix was last calculated.
1875 */
1876 private boolean mInverseMatrixDirty = true;
1877
1878 /**
1879 * A variable that tracks whether we need to recalculate the
1880 * transform matrix, based on whether the rotation or scaleX/Y properties
1881 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001882 * is only valid after a call to updateMatrix() or to a function that
1883 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001884 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001885 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001886
1887 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001888 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1889 */
1890 private Camera mCamera = null;
1891
1892 /**
1893 * This matrix is used when computing the matrix for 3D rotations.
1894 */
1895 private Matrix matrix3D = null;
1896
1897 /**
1898 * These prev values are used to recalculate a centered pivot point when necessary. The
1899 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1900 * set), so thes values are only used then as well.
1901 */
1902 private int mPrevWidth = -1;
1903 private int mPrevHeight = -1;
1904
Joe Malin32736f02011-01-19 16:14:20 -08001905 private boolean mLastIsOpaque;
1906
Chet Haasefd2b0022010-08-06 13:08:56 -07001907 /**
1908 * Convenience value to check for float values that are close enough to zero to be considered
1909 * zero.
1910 */
Romain Guy2542d192010-08-18 11:47:12 -07001911 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001912
1913 /**
1914 * The degrees rotation around the vertical axis through the pivot point.
1915 */
1916 @ViewDebug.ExportedProperty
1917 private float mRotationY = 0f;
1918
1919 /**
1920 * The degrees rotation around the horizontal axis through the pivot point.
1921 */
1922 @ViewDebug.ExportedProperty
1923 private float mRotationX = 0f;
1924
1925 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001926 * The degrees rotation around the pivot point.
1927 */
1928 @ViewDebug.ExportedProperty
1929 private float mRotation = 0f;
1930
1931 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001932 * The amount of translation of the object away from its left property (post-layout).
1933 */
1934 @ViewDebug.ExportedProperty
1935 private float mTranslationX = 0f;
1936
1937 /**
1938 * The amount of translation of the object away from its top property (post-layout).
1939 */
1940 @ViewDebug.ExportedProperty
1941 private float mTranslationY = 0f;
1942
1943 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001944 * The amount of scale in the x direction around the pivot point. A
1945 * value of 1 means no scaling is applied.
1946 */
1947 @ViewDebug.ExportedProperty
1948 private float mScaleX = 1f;
1949
1950 /**
1951 * The amount of scale in the y direction around the pivot point. A
1952 * value of 1 means no scaling is applied.
1953 */
1954 @ViewDebug.ExportedProperty
1955 private float mScaleY = 1f;
1956
1957 /**
1958 * The amount of scale in the x direction around the pivot point. A
1959 * value of 1 means no scaling is applied.
1960 */
1961 @ViewDebug.ExportedProperty
1962 private float mPivotX = 0f;
1963
1964 /**
1965 * The amount of scale in the y direction around the pivot point. A
1966 * value of 1 means no scaling is applied.
1967 */
1968 @ViewDebug.ExportedProperty
1969 private float mPivotY = 0f;
1970
1971 /**
1972 * The opacity of the View. This is a value from 0 to 1, where 0 means
1973 * completely transparent and 1 means completely opaque.
1974 */
1975 @ViewDebug.ExportedProperty
1976 private float mAlpha = 1f;
1977
1978 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001979 * The distance in pixels from the left edge of this view's parent
1980 * to the left edge of this view.
1981 * {@hide}
1982 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001983 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 protected int mLeft;
1985 /**
1986 * The distance in pixels from the left edge of this view's parent
1987 * to the right edge of this view.
1988 * {@hide}
1989 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001990 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001991 protected int mRight;
1992 /**
1993 * The distance in pixels from the top edge of this view's parent
1994 * to the top edge of this view.
1995 * {@hide}
1996 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001997 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001998 protected int mTop;
1999 /**
2000 * The distance in pixels from the top edge of this view's parent
2001 * to the bottom edge of this view.
2002 * {@hide}
2003 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002004 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002005 protected int mBottom;
2006
2007 /**
2008 * The offset, in pixels, by which the content of this view is scrolled
2009 * horizontally.
2010 * {@hide}
2011 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002012 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 protected int mScrollX;
2014 /**
2015 * The offset, in pixels, by which the content of this view is scrolled
2016 * vertically.
2017 * {@hide}
2018 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002019 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002020 protected int mScrollY;
2021
2022 /**
2023 * The left padding in pixels, that is the distance in pixels between the
2024 * left edge of this view and the left edge of its content.
2025 * {@hide}
2026 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002027 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 protected int mPaddingLeft;
2029 /**
2030 * The right padding in pixels, that is the distance in pixels between the
2031 * right edge of this view and the right edge of its content.
2032 * {@hide}
2033 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002034 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 protected int mPaddingRight;
2036 /**
2037 * The top padding in pixels, that is the distance in pixels between the
2038 * top edge of this view and the top edge of its content.
2039 * {@hide}
2040 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002041 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 protected int mPaddingTop;
2043 /**
2044 * The bottom padding in pixels, that is the distance in pixels between the
2045 * bottom edge of this view and the bottom edge of its content.
2046 * {@hide}
2047 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002048 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 protected int mPaddingBottom;
2050
2051 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002052 * Briefly describes the view and is primarily used for accessibility support.
2053 */
2054 private CharSequence mContentDescription;
2055
2056 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002057 * Cache the paddingRight set by the user to append to the scrollbar's size.
2058 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002059 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 int mUserPaddingRight;
2061
2062 /**
2063 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2064 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002065 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 int mUserPaddingBottom;
2067
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002068 /**
Adam Powell20232d02010-12-08 21:08:53 -08002069 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2070 */
2071 @ViewDebug.ExportedProperty(category = "padding")
2072 int mUserPaddingLeft;
2073
2074 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002075 * @hide
2076 */
2077 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2078 /**
2079 * @hide
2080 */
2081 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082
2083 private Resources mResources = null;
2084
2085 private Drawable mBGDrawable;
2086
2087 private int mBackgroundResource;
2088 private boolean mBackgroundSizeChanged;
2089
2090 /**
2091 * Listener used to dispatch focus change events.
2092 * This field should be made private, so it is hidden from the SDK.
2093 * {@hide}
2094 */
2095 protected OnFocusChangeListener mOnFocusChangeListener;
2096
2097 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002098 * Listeners for layout change events.
2099 */
2100 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2101
2102 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002103 * Listeners for attach events.
2104 */
2105 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2106
2107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 * Listener used to dispatch click events.
2109 * This field should be made private, so it is hidden from the SDK.
2110 * {@hide}
2111 */
2112 protected OnClickListener mOnClickListener;
2113
2114 /**
2115 * Listener used to dispatch long click events.
2116 * This field should be made private, so it is hidden from the SDK.
2117 * {@hide}
2118 */
2119 protected OnLongClickListener mOnLongClickListener;
2120
2121 /**
2122 * Listener used to build the context menu.
2123 * This field should be made private, so it is hidden from the SDK.
2124 * {@hide}
2125 */
2126 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2127
2128 private OnKeyListener mOnKeyListener;
2129
2130 private OnTouchListener mOnTouchListener;
2131
Chris Tate32affef2010-10-18 15:29:21 -07002132 private OnDragListener mOnDragListener;
2133
Joe Onorato664644d2011-01-23 17:53:23 -08002134 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 /**
2137 * The application environment this view lives in.
2138 * This field should be made private, so it is hidden from the SDK.
2139 * {@hide}
2140 */
2141 protected Context mContext;
2142
2143 private ScrollabilityCache mScrollCache;
2144
2145 private int[] mDrawableState = null;
2146
Romain Guy0211a0a2011-02-14 16:34:59 -08002147 /**
2148 * Set to true when drawing cache is enabled and cannot be created.
2149 *
2150 * @hide
2151 */
2152 public boolean mCachingFailed;
2153
Romain Guy02890fd2010-08-06 17:58:44 -07002154 private Bitmap mDrawingCache;
2155 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002156 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002157 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002158
2159 /**
2160 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2161 * the user may specify which view to go to next.
2162 */
2163 private int mNextFocusLeftId = View.NO_ID;
2164
2165 /**
2166 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2167 * the user may specify which view to go to next.
2168 */
2169 private int mNextFocusRightId = View.NO_ID;
2170
2171 /**
2172 * When this view has focus and the next focus is {@link #FOCUS_UP},
2173 * the user may specify which view to go to next.
2174 */
2175 private int mNextFocusUpId = View.NO_ID;
2176
2177 /**
2178 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2179 * the user may specify which view to go to next.
2180 */
2181 private int mNextFocusDownId = View.NO_ID;
2182
Jeff Brown4e6319b2010-12-13 10:36:51 -08002183 /**
2184 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2185 * the user may specify which view to go to next.
2186 */
2187 int mNextFocusForwardId = View.NO_ID;
2188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002189 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002190 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002191 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 private UnsetPressedState mUnsetPressedState;
2194
2195 /**
2196 * Whether the long press's action has been invoked. The tap's action is invoked on the
2197 * up event while a long press is invoked as soon as the long press duration is reached, so
2198 * a long press could be performed before the tap is checked, in which case the tap's action
2199 * should not be invoked.
2200 */
2201 private boolean mHasPerformedLongPress;
2202
2203 /**
2204 * The minimum height of the view. We'll try our best to have the height
2205 * of this view to at least this amount.
2206 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002207 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 private int mMinHeight;
2209
2210 /**
2211 * The minimum width of the view. We'll try our best to have the width
2212 * of this view to at least this amount.
2213 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002214 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 private int mMinWidth;
2216
2217 /**
2218 * The delegate to handle touch events that are physically in this view
2219 * but should be handled by another view.
2220 */
2221 private TouchDelegate mTouchDelegate = null;
2222
2223 /**
2224 * Solid color to use as a background when creating the drawing cache. Enables
2225 * the cache to use 16 bit bitmaps instead of 32 bit.
2226 */
2227 private int mDrawingCacheBackgroundColor = 0;
2228
2229 /**
2230 * Special tree observer used when mAttachInfo is null.
2231 */
2232 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002233
Adam Powelle14579b2009-12-16 18:39:52 -08002234 /**
2235 * Cache the touch slop from the context that created the view.
2236 */
2237 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002240 * Cache drag/drop state
2241 *
2242 */
2243 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002244
2245 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002246 * Flag indicating that a drag can cross window boundaries. When
2247 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2248 * with this flag set, all visible applications will be able to participate
2249 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002250 *
2251 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002252 */
2253 public static final int DRAG_FLAG_GLOBAL = 1;
2254
2255 /**
Adam Powell20232d02010-12-08 21:08:53 -08002256 * Position of the vertical scroll bar.
2257 */
2258 private int mVerticalScrollbarPosition;
2259
2260 /**
2261 * Position the scroll bar at the default position as determined by the system.
2262 */
2263 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2264
2265 /**
2266 * Position the scroll bar along the left edge.
2267 */
2268 public static final int SCROLLBAR_POSITION_LEFT = 1;
2269
2270 /**
2271 * Position the scroll bar along the right edge.
2272 */
2273 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2274
2275 /**
Romain Guy171c5922011-01-06 10:04:23 -08002276 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002277 *
2278 * @see #getLayerType()
2279 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002280 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002281 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002282 */
2283 public static final int LAYER_TYPE_NONE = 0;
2284
2285 /**
2286 * <p>Indicates that the view has a software layer. A software layer is backed
2287 * by a bitmap and causes the view to be rendered using Android's software
2288 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002289 *
Romain Guy171c5922011-01-06 10:04:23 -08002290 * <p>Software layers have various usages:</p>
2291 * <p>When the application is not using hardware acceleration, a software layer
2292 * is useful to apply a specific color filter and/or blending mode and/or
2293 * translucency to a view and all its children.</p>
2294 * <p>When the application is using hardware acceleration, a software layer
2295 * is useful to render drawing primitives not supported by the hardware
2296 * accelerated pipeline. It can also be used to cache a complex view tree
2297 * into a texture and reduce the complexity of drawing operations. For instance,
2298 * when animating a complex view tree with a translation, a software layer can
2299 * be used to render the view tree only once.</p>
2300 * <p>Software layers should be avoided when the affected view tree updates
2301 * often. Every update will require to re-render the software layer, which can
2302 * potentially be slow (particularly when hardware acceleration is turned on
2303 * since the layer will have to be uploaded into a hardware texture after every
2304 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002305 *
2306 * @see #getLayerType()
2307 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002308 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002309 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002310 */
2311 public static final int LAYER_TYPE_SOFTWARE = 1;
2312
2313 /**
2314 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2315 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2316 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2317 * rendering pipeline, but only if hardware acceleration is turned on for the
2318 * view hierarchy. When hardware acceleration is turned off, hardware layers
2319 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002320 *
Romain Guy171c5922011-01-06 10:04:23 -08002321 * <p>A hardware layer is useful to apply a specific color filter and/or
2322 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002323 * <p>A hardware layer can be used to cache a complex view tree into a
2324 * texture and reduce the complexity of drawing operations. For instance,
2325 * when animating a complex view tree with a translation, a hardware layer can
2326 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002327 * <p>A hardware layer can also be used to increase the rendering quality when
2328 * rotation transformations are applied on a view. It can also be used to
2329 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002330 *
2331 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002332 * @see #setLayerType(int, android.graphics.Paint)
2333 * @see #LAYER_TYPE_NONE
2334 * @see #LAYER_TYPE_SOFTWARE
2335 */
2336 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002337
Romain Guy3aaff3a2011-01-12 14:18:47 -08002338 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2339 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2340 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2341 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2342 })
Romain Guy171c5922011-01-06 10:04:23 -08002343 int mLayerType = LAYER_TYPE_NONE;
2344 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002345 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002346
2347 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002348 * Simple constructor to use when creating a view from code.
2349 *
2350 * @param context The Context the view is running in, through which it can
2351 * access the current theme, resources, etc.
2352 */
2353 public View(Context context) {
2354 mContext = context;
2355 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002356 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002357 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002358 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 }
2360
2361 /**
2362 * Constructor that is called when inflating a view from XML. This is called
2363 * when a view is being constructed from an XML file, supplying attributes
2364 * that were specified in the XML file. This version uses a default style of
2365 * 0, so the only attribute values applied are those in the Context's Theme
2366 * and the given AttributeSet.
2367 *
2368 * <p>
2369 * The method onFinishInflate() will be called after all children have been
2370 * added.
2371 *
2372 * @param context The Context the view is running in, through which it can
2373 * access the current theme, resources, etc.
2374 * @param attrs The attributes of the XML tag that is inflating the view.
2375 * @see #View(Context, AttributeSet, int)
2376 */
2377 public View(Context context, AttributeSet attrs) {
2378 this(context, attrs, 0);
2379 }
2380
2381 /**
2382 * Perform inflation from XML and apply a class-specific base style. This
2383 * constructor of View allows subclasses to use their own base style when
2384 * they are inflating. For example, a Button class's constructor would call
2385 * this version of the super class constructor and supply
2386 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2387 * the theme's button style to modify all of the base view attributes (in
2388 * particular its background) as well as the Button class's attributes.
2389 *
2390 * @param context The Context the view is running in, through which it can
2391 * access the current theme, resources, etc.
2392 * @param attrs The attributes of the XML tag that is inflating the view.
2393 * @param defStyle The default style to apply to this view. If 0, no style
2394 * will be applied (beyond what is included in the theme). This may
2395 * either be an attribute resource, whose value will be retrieved
2396 * from the current theme, or an explicit style resource.
2397 * @see #View(Context, AttributeSet)
2398 */
2399 public View(Context context, AttributeSet attrs, int defStyle) {
2400 this(context);
2401
2402 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2403 defStyle, 0);
2404
2405 Drawable background = null;
2406
2407 int leftPadding = -1;
2408 int topPadding = -1;
2409 int rightPadding = -1;
2410 int bottomPadding = -1;
2411
2412 int padding = -1;
2413
2414 int viewFlagValues = 0;
2415 int viewFlagMasks = 0;
2416
2417 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002419 int x = 0;
2420 int y = 0;
2421
Chet Haase73066682010-11-29 15:55:32 -08002422 float tx = 0;
2423 float ty = 0;
2424 float rotation = 0;
2425 float rotationX = 0;
2426 float rotationY = 0;
2427 float sx = 1f;
2428 float sy = 1f;
2429 boolean transformSet = false;
2430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002431 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2432
Adam Powell637d3372010-08-25 14:37:03 -07002433 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 final int N = a.getIndexCount();
2435 for (int i = 0; i < N; i++) {
2436 int attr = a.getIndex(i);
2437 switch (attr) {
2438 case com.android.internal.R.styleable.View_background:
2439 background = a.getDrawable(attr);
2440 break;
2441 case com.android.internal.R.styleable.View_padding:
2442 padding = a.getDimensionPixelSize(attr, -1);
2443 break;
2444 case com.android.internal.R.styleable.View_paddingLeft:
2445 leftPadding = a.getDimensionPixelSize(attr, -1);
2446 break;
2447 case com.android.internal.R.styleable.View_paddingTop:
2448 topPadding = a.getDimensionPixelSize(attr, -1);
2449 break;
2450 case com.android.internal.R.styleable.View_paddingRight:
2451 rightPadding = a.getDimensionPixelSize(attr, -1);
2452 break;
2453 case com.android.internal.R.styleable.View_paddingBottom:
2454 bottomPadding = a.getDimensionPixelSize(attr, -1);
2455 break;
2456 case com.android.internal.R.styleable.View_scrollX:
2457 x = a.getDimensionPixelOffset(attr, 0);
2458 break;
2459 case com.android.internal.R.styleable.View_scrollY:
2460 y = a.getDimensionPixelOffset(attr, 0);
2461 break;
Chet Haase73066682010-11-29 15:55:32 -08002462 case com.android.internal.R.styleable.View_alpha:
2463 setAlpha(a.getFloat(attr, 1f));
2464 break;
2465 case com.android.internal.R.styleable.View_transformPivotX:
2466 setPivotX(a.getDimensionPixelOffset(attr, 0));
2467 break;
2468 case com.android.internal.R.styleable.View_transformPivotY:
2469 setPivotY(a.getDimensionPixelOffset(attr, 0));
2470 break;
2471 case com.android.internal.R.styleable.View_translationX:
2472 tx = a.getDimensionPixelOffset(attr, 0);
2473 transformSet = true;
2474 break;
2475 case com.android.internal.R.styleable.View_translationY:
2476 ty = a.getDimensionPixelOffset(attr, 0);
2477 transformSet = true;
2478 break;
2479 case com.android.internal.R.styleable.View_rotation:
2480 rotation = a.getFloat(attr, 0);
2481 transformSet = true;
2482 break;
2483 case com.android.internal.R.styleable.View_rotationX:
2484 rotationX = a.getFloat(attr, 0);
2485 transformSet = true;
2486 break;
2487 case com.android.internal.R.styleable.View_rotationY:
2488 rotationY = a.getFloat(attr, 0);
2489 transformSet = true;
2490 break;
2491 case com.android.internal.R.styleable.View_scaleX:
2492 sx = a.getFloat(attr, 1f);
2493 transformSet = true;
2494 break;
2495 case com.android.internal.R.styleable.View_scaleY:
2496 sy = a.getFloat(attr, 1f);
2497 transformSet = true;
2498 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002499 case com.android.internal.R.styleable.View_id:
2500 mID = a.getResourceId(attr, NO_ID);
2501 break;
2502 case com.android.internal.R.styleable.View_tag:
2503 mTag = a.getText(attr);
2504 break;
2505 case com.android.internal.R.styleable.View_fitsSystemWindows:
2506 if (a.getBoolean(attr, false)) {
2507 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2508 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2509 }
2510 break;
2511 case com.android.internal.R.styleable.View_focusable:
2512 if (a.getBoolean(attr, false)) {
2513 viewFlagValues |= FOCUSABLE;
2514 viewFlagMasks |= FOCUSABLE_MASK;
2515 }
2516 break;
2517 case com.android.internal.R.styleable.View_focusableInTouchMode:
2518 if (a.getBoolean(attr, false)) {
2519 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2520 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2521 }
2522 break;
2523 case com.android.internal.R.styleable.View_clickable:
2524 if (a.getBoolean(attr, false)) {
2525 viewFlagValues |= CLICKABLE;
2526 viewFlagMasks |= CLICKABLE;
2527 }
2528 break;
2529 case com.android.internal.R.styleable.View_longClickable:
2530 if (a.getBoolean(attr, false)) {
2531 viewFlagValues |= LONG_CLICKABLE;
2532 viewFlagMasks |= LONG_CLICKABLE;
2533 }
2534 break;
2535 case com.android.internal.R.styleable.View_saveEnabled:
2536 if (!a.getBoolean(attr, true)) {
2537 viewFlagValues |= SAVE_DISABLED;
2538 viewFlagMasks |= SAVE_DISABLED_MASK;
2539 }
2540 break;
2541 case com.android.internal.R.styleable.View_duplicateParentState:
2542 if (a.getBoolean(attr, false)) {
2543 viewFlagValues |= DUPLICATE_PARENT_STATE;
2544 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2545 }
2546 break;
2547 case com.android.internal.R.styleable.View_visibility:
2548 final int visibility = a.getInt(attr, 0);
2549 if (visibility != 0) {
2550 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2551 viewFlagMasks |= VISIBILITY_MASK;
2552 }
2553 break;
2554 case com.android.internal.R.styleable.View_drawingCacheQuality:
2555 final int cacheQuality = a.getInt(attr, 0);
2556 if (cacheQuality != 0) {
2557 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2558 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2559 }
2560 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002561 case com.android.internal.R.styleable.View_contentDescription:
2562 mContentDescription = a.getString(attr);
2563 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2565 if (!a.getBoolean(attr, true)) {
2566 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2567 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2568 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002569 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2571 if (!a.getBoolean(attr, true)) {
2572 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2573 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2574 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002575 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002576 case R.styleable.View_scrollbars:
2577 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2578 if (scrollbars != SCROLLBARS_NONE) {
2579 viewFlagValues |= scrollbars;
2580 viewFlagMasks |= SCROLLBARS_MASK;
2581 initializeScrollbars(a);
2582 }
2583 break;
2584 case R.styleable.View_fadingEdge:
2585 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2586 if (fadingEdge != FADING_EDGE_NONE) {
2587 viewFlagValues |= fadingEdge;
2588 viewFlagMasks |= FADING_EDGE_MASK;
2589 initializeFadingEdge(a);
2590 }
2591 break;
2592 case R.styleable.View_scrollbarStyle:
2593 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2594 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2595 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2596 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2597 }
2598 break;
2599 case R.styleable.View_isScrollContainer:
2600 setScrollContainer = true;
2601 if (a.getBoolean(attr, false)) {
2602 setScrollContainer(true);
2603 }
2604 break;
2605 case com.android.internal.R.styleable.View_keepScreenOn:
2606 if (a.getBoolean(attr, false)) {
2607 viewFlagValues |= KEEP_SCREEN_ON;
2608 viewFlagMasks |= KEEP_SCREEN_ON;
2609 }
2610 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002611 case R.styleable.View_filterTouchesWhenObscured:
2612 if (a.getBoolean(attr, false)) {
2613 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2614 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2615 }
2616 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 case R.styleable.View_nextFocusLeft:
2618 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2619 break;
2620 case R.styleable.View_nextFocusRight:
2621 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2622 break;
2623 case R.styleable.View_nextFocusUp:
2624 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2625 break;
2626 case R.styleable.View_nextFocusDown:
2627 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2628 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002629 case R.styleable.View_nextFocusForward:
2630 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2631 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 case R.styleable.View_minWidth:
2633 mMinWidth = a.getDimensionPixelSize(attr, 0);
2634 break;
2635 case R.styleable.View_minHeight:
2636 mMinHeight = a.getDimensionPixelSize(attr, 0);
2637 break;
Romain Guy9a817362009-05-01 10:57:14 -07002638 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002639 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002640 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002641 + "be used within a restricted context");
2642 }
2643
Romain Guy9a817362009-05-01 10:57:14 -07002644 final String handlerName = a.getString(attr);
2645 if (handlerName != null) {
2646 setOnClickListener(new OnClickListener() {
2647 private Method mHandler;
2648
2649 public void onClick(View v) {
2650 if (mHandler == null) {
2651 try {
2652 mHandler = getContext().getClass().getMethod(handlerName,
2653 View.class);
2654 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002655 int id = getId();
2656 String idText = id == NO_ID ? "" : " with id '"
2657 + getContext().getResources().getResourceEntryName(
2658 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002659 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002660 handlerName + "(View) in the activity "
2661 + getContext().getClass() + " for onClick handler"
2662 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002663 }
2664 }
2665
2666 try {
2667 mHandler.invoke(getContext(), View.this);
2668 } catch (IllegalAccessException e) {
2669 throw new IllegalStateException("Could not execute non "
2670 + "public method of the activity", e);
2671 } catch (InvocationTargetException e) {
2672 throw new IllegalStateException("Could not execute "
2673 + "method of the activity", e);
2674 }
2675 }
2676 });
2677 }
2678 break;
Adam Powell637d3372010-08-25 14:37:03 -07002679 case R.styleable.View_overScrollMode:
2680 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2681 break;
Adam Powell20232d02010-12-08 21:08:53 -08002682 case R.styleable.View_verticalScrollbarPosition:
2683 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2684 break;
Romain Guy171c5922011-01-06 10:04:23 -08002685 case R.styleable.View_layerType:
2686 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2687 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002688 }
2689 }
2690
Adam Powell637d3372010-08-25 14:37:03 -07002691 setOverScrollMode(overScrollMode);
2692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 if (background != null) {
2694 setBackgroundDrawable(background);
2695 }
2696
2697 if (padding >= 0) {
2698 leftPadding = padding;
2699 topPadding = padding;
2700 rightPadding = padding;
2701 bottomPadding = padding;
2702 }
2703
2704 // If the user specified the padding (either with android:padding or
2705 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2706 // use the default padding or the padding from the background drawable
2707 // (stored at this point in mPadding*)
2708 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2709 topPadding >= 0 ? topPadding : mPaddingTop,
2710 rightPadding >= 0 ? rightPadding : mPaddingRight,
2711 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2712
2713 if (viewFlagMasks != 0) {
2714 setFlags(viewFlagValues, viewFlagMasks);
2715 }
2716
2717 // Needs to be called after mViewFlags is set
2718 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2719 recomputePadding();
2720 }
2721
2722 if (x != 0 || y != 0) {
2723 scrollTo(x, y);
2724 }
2725
Chet Haase73066682010-11-29 15:55:32 -08002726 if (transformSet) {
2727 setTranslationX(tx);
2728 setTranslationY(ty);
2729 setRotation(rotation);
2730 setRotationX(rotationX);
2731 setRotationY(rotationY);
2732 setScaleX(sx);
2733 setScaleY(sy);
2734 }
2735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2737 setScrollContainer(true);
2738 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002739
2740 computeOpaqueFlags();
2741
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002742 a.recycle();
2743 }
2744
2745 /**
2746 * Non-public constructor for use in testing
2747 */
2748 View() {
2749 }
2750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 /**
2752 * <p>
2753 * Initializes the fading edges from a given set of styled attributes. This
2754 * method should be called by subclasses that need fading edges and when an
2755 * instance of these subclasses is created programmatically rather than
2756 * being inflated from XML. This method is automatically called when the XML
2757 * is inflated.
2758 * </p>
2759 *
2760 * @param a the styled attributes set to initialize the fading edges from
2761 */
2762 protected void initializeFadingEdge(TypedArray a) {
2763 initScrollCache();
2764
2765 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2766 R.styleable.View_fadingEdgeLength,
2767 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2768 }
2769
2770 /**
2771 * Returns the size of the vertical faded edges used to indicate that more
2772 * content in this view is visible.
2773 *
2774 * @return The size in pixels of the vertical faded edge or 0 if vertical
2775 * faded edges are not enabled for this view.
2776 * @attr ref android.R.styleable#View_fadingEdgeLength
2777 */
2778 public int getVerticalFadingEdgeLength() {
2779 if (isVerticalFadingEdgeEnabled()) {
2780 ScrollabilityCache cache = mScrollCache;
2781 if (cache != null) {
2782 return cache.fadingEdgeLength;
2783 }
2784 }
2785 return 0;
2786 }
2787
2788 /**
2789 * Set the size of the faded edge used to indicate that more content in this
2790 * view is available. Will not change whether the fading edge is enabled; use
2791 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2792 * to enable the fading edge for the vertical or horizontal fading edges.
2793 *
2794 * @param length The size in pixels of the faded edge used to indicate that more
2795 * content in this view is visible.
2796 */
2797 public void setFadingEdgeLength(int length) {
2798 initScrollCache();
2799 mScrollCache.fadingEdgeLength = length;
2800 }
2801
2802 /**
2803 * Returns the size of the horizontal faded edges used to indicate that more
2804 * content in this view is visible.
2805 *
2806 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2807 * faded edges are not enabled for this view.
2808 * @attr ref android.R.styleable#View_fadingEdgeLength
2809 */
2810 public int getHorizontalFadingEdgeLength() {
2811 if (isHorizontalFadingEdgeEnabled()) {
2812 ScrollabilityCache cache = mScrollCache;
2813 if (cache != null) {
2814 return cache.fadingEdgeLength;
2815 }
2816 }
2817 return 0;
2818 }
2819
2820 /**
2821 * Returns the width of the vertical scrollbar.
2822 *
2823 * @return The width in pixels of the vertical scrollbar or 0 if there
2824 * is no vertical scrollbar.
2825 */
2826 public int getVerticalScrollbarWidth() {
2827 ScrollabilityCache cache = mScrollCache;
2828 if (cache != null) {
2829 ScrollBarDrawable scrollBar = cache.scrollBar;
2830 if (scrollBar != null) {
2831 int size = scrollBar.getSize(true);
2832 if (size <= 0) {
2833 size = cache.scrollBarSize;
2834 }
2835 return size;
2836 }
2837 return 0;
2838 }
2839 return 0;
2840 }
2841
2842 /**
2843 * Returns the height of the horizontal scrollbar.
2844 *
2845 * @return The height in pixels of the horizontal scrollbar or 0 if
2846 * there is no horizontal scrollbar.
2847 */
2848 protected int getHorizontalScrollbarHeight() {
2849 ScrollabilityCache cache = mScrollCache;
2850 if (cache != null) {
2851 ScrollBarDrawable scrollBar = cache.scrollBar;
2852 if (scrollBar != null) {
2853 int size = scrollBar.getSize(false);
2854 if (size <= 0) {
2855 size = cache.scrollBarSize;
2856 }
2857 return size;
2858 }
2859 return 0;
2860 }
2861 return 0;
2862 }
2863
2864 /**
2865 * <p>
2866 * Initializes the scrollbars from a given set of styled attributes. This
2867 * method should be called by subclasses that need scrollbars and when an
2868 * instance of these subclasses is created programmatically rather than
2869 * being inflated from XML. This method is automatically called when the XML
2870 * is inflated.
2871 * </p>
2872 *
2873 * @param a the styled attributes set to initialize the scrollbars from
2874 */
2875 protected void initializeScrollbars(TypedArray a) {
2876 initScrollCache();
2877
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002879
Mike Cleronf116bf82009-09-27 19:14:12 -07002880 if (scrollabilityCache.scrollBar == null) {
2881 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2882 }
Joe Malin32736f02011-01-19 16:14:20 -08002883
Romain Guy8bda2482010-03-02 11:42:11 -08002884 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002885
Mike Cleronf116bf82009-09-27 19:14:12 -07002886 if (!fadeScrollbars) {
2887 scrollabilityCache.state = ScrollabilityCache.ON;
2888 }
2889 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002890
2891
Mike Cleronf116bf82009-09-27 19:14:12 -07002892 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2893 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2894 .getScrollBarFadeDuration());
2895 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2896 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2897 ViewConfiguration.getScrollDefaultDelay());
2898
Joe Malin32736f02011-01-19 16:14:20 -08002899
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002900 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2901 com.android.internal.R.styleable.View_scrollbarSize,
2902 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2903
2904 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2905 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2906
2907 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2908 if (thumb != null) {
2909 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2910 }
2911
2912 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2913 false);
2914 if (alwaysDraw) {
2915 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2916 }
2917
2918 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2919 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2920
2921 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2922 if (thumb != null) {
2923 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2924 }
2925
2926 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2927 false);
2928 if (alwaysDraw) {
2929 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2930 }
2931
2932 // Re-apply user/background padding so that scrollbar(s) get added
2933 recomputePadding();
2934 }
2935
2936 /**
2937 * <p>
2938 * Initalizes the scrollability cache if necessary.
2939 * </p>
2940 */
2941 private void initScrollCache() {
2942 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002943 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002944 }
2945 }
2946
2947 /**
Adam Powell20232d02010-12-08 21:08:53 -08002948 * Set the position of the vertical scroll bar. Should be one of
2949 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2950 * {@link #SCROLLBAR_POSITION_RIGHT}.
2951 *
2952 * @param position Where the vertical scroll bar should be positioned.
2953 */
2954 public void setVerticalScrollbarPosition(int position) {
2955 if (mVerticalScrollbarPosition != position) {
2956 mVerticalScrollbarPosition = position;
2957 computeOpaqueFlags();
2958 recomputePadding();
2959 }
2960 }
2961
2962 /**
2963 * @return The position where the vertical scroll bar will show, if applicable.
2964 * @see #setVerticalScrollbarPosition(int)
2965 */
2966 public int getVerticalScrollbarPosition() {
2967 return mVerticalScrollbarPosition;
2968 }
2969
2970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002971 * Register a callback to be invoked when focus of this view changed.
2972 *
2973 * @param l The callback that will run.
2974 */
2975 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2976 mOnFocusChangeListener = l;
2977 }
2978
2979 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002980 * Add a listener that will be called when the bounds of the view change due to
2981 * layout processing.
2982 *
2983 * @param listener The listener that will be called when layout bounds change.
2984 */
2985 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2986 if (mOnLayoutChangeListeners == null) {
2987 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2988 }
2989 mOnLayoutChangeListeners.add(listener);
2990 }
2991
2992 /**
2993 * Remove a listener for layout changes.
2994 *
2995 * @param listener The listener for layout bounds change.
2996 */
2997 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
2998 if (mOnLayoutChangeListeners == null) {
2999 return;
3000 }
3001 mOnLayoutChangeListeners.remove(listener);
3002 }
3003
3004 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003005 * Add a listener for attach state changes.
3006 *
3007 * This listener will be called whenever this view is attached or detached
3008 * from a window. Remove the listener using
3009 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3010 *
3011 * @param listener Listener to attach
3012 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3013 */
3014 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3015 if (mOnAttachStateChangeListeners == null) {
3016 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3017 }
3018 mOnAttachStateChangeListeners.add(listener);
3019 }
3020
3021 /**
3022 * Remove a listener for attach state changes. The listener will receive no further
3023 * notification of window attach/detach events.
3024 *
3025 * @param listener Listener to remove
3026 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3027 */
3028 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3029 if (mOnAttachStateChangeListeners == null) {
3030 return;
3031 }
3032 mOnAttachStateChangeListeners.remove(listener);
3033 }
3034
3035 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003036 * Returns the focus-change callback registered for this view.
3037 *
3038 * @return The callback, or null if one is not registered.
3039 */
3040 public OnFocusChangeListener getOnFocusChangeListener() {
3041 return mOnFocusChangeListener;
3042 }
3043
3044 /**
3045 * Register a callback to be invoked when this view is clicked. If this view is not
3046 * clickable, it becomes clickable.
3047 *
3048 * @param l The callback that will run
3049 *
3050 * @see #setClickable(boolean)
3051 */
3052 public void setOnClickListener(OnClickListener l) {
3053 if (!isClickable()) {
3054 setClickable(true);
3055 }
3056 mOnClickListener = l;
3057 }
3058
3059 /**
3060 * Register a callback to be invoked when this view is clicked and held. If this view is not
3061 * long clickable, it becomes long clickable.
3062 *
3063 * @param l The callback that will run
3064 *
3065 * @see #setLongClickable(boolean)
3066 */
3067 public void setOnLongClickListener(OnLongClickListener l) {
3068 if (!isLongClickable()) {
3069 setLongClickable(true);
3070 }
3071 mOnLongClickListener = l;
3072 }
3073
3074 /**
3075 * Register a callback to be invoked when the context menu for this view is
3076 * being built. If this view is not long clickable, it becomes long clickable.
3077 *
3078 * @param l The callback that will run
3079 *
3080 */
3081 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3082 if (!isLongClickable()) {
3083 setLongClickable(true);
3084 }
3085 mOnCreateContextMenuListener = l;
3086 }
3087
3088 /**
3089 * Call this view's OnClickListener, if it is defined.
3090 *
3091 * @return True there was an assigned OnClickListener that was called, false
3092 * otherwise is returned.
3093 */
3094 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003095 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003097 if (mOnClickListener != null) {
3098 playSoundEffect(SoundEffectConstants.CLICK);
3099 mOnClickListener.onClick(this);
3100 return true;
3101 }
3102
3103 return false;
3104 }
3105
3106 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003107 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3108 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003109 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003110 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 */
3112 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003113 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3114
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003115 boolean handled = false;
3116 if (mOnLongClickListener != null) {
3117 handled = mOnLongClickListener.onLongClick(View.this);
3118 }
3119 if (!handled) {
3120 handled = showContextMenu();
3121 }
3122 if (handled) {
3123 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3124 }
3125 return handled;
3126 }
3127
3128 /**
3129 * Bring up the context menu for this view.
3130 *
3131 * @return Whether a context menu was displayed.
3132 */
3133 public boolean showContextMenu() {
3134 return getParent().showContextMenuForChild(this);
3135 }
3136
3137 /**
Adam Powell6e346362010-07-23 10:18:23 -07003138 * Start an action mode.
3139 *
3140 * @param callback Callback that will control the lifecycle of the action mode
3141 * @return The new action mode if it is started, null otherwise
3142 *
3143 * @see ActionMode
3144 */
3145 public ActionMode startActionMode(ActionMode.Callback callback) {
3146 return getParent().startActionModeForChild(this, callback);
3147 }
3148
3149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003150 * Register a callback to be invoked when a key is pressed in this view.
3151 * @param l the key listener to attach to this view
3152 */
3153 public void setOnKeyListener(OnKeyListener l) {
3154 mOnKeyListener = l;
3155 }
3156
3157 /**
3158 * Register a callback to be invoked when a touch event is sent to this view.
3159 * @param l the touch listener to attach to this view
3160 */
3161 public void setOnTouchListener(OnTouchListener l) {
3162 mOnTouchListener = l;
3163 }
3164
3165 /**
Joe Malin32736f02011-01-19 16:14:20 -08003166 * Register a drag event listener callback object for this View. The parameter is
3167 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3168 * View, the system calls the
3169 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3170 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003171 */
3172 public void setOnDragListener(OnDragListener l) {
3173 mOnDragListener = l;
3174 }
3175
3176 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003177 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3178 *
3179 * Note: this does not check whether this {@link View} should get focus, it just
3180 * gives it focus no matter what. It should only be called internally by framework
3181 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3182 *
3183 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
3184 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
3185 * focus moved when requestFocus() is called. It may not always
3186 * apply, in which case use the default View.FOCUS_DOWN.
3187 * @param previouslyFocusedRect The rectangle of the view that had focus
3188 * prior in this View's coordinate system.
3189 */
3190 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3191 if (DBG) {
3192 System.out.println(this + " requestFocus()");
3193 }
3194
3195 if ((mPrivateFlags & FOCUSED) == 0) {
3196 mPrivateFlags |= FOCUSED;
3197
3198 if (mParent != null) {
3199 mParent.requestChildFocus(this, this);
3200 }
3201
3202 onFocusChanged(true, direction, previouslyFocusedRect);
3203 refreshDrawableState();
3204 }
3205 }
3206
3207 /**
3208 * Request that a rectangle of this view be visible on the screen,
3209 * scrolling if necessary just enough.
3210 *
3211 * <p>A View should call this if it maintains some notion of which part
3212 * of its content is interesting. For example, a text editing view
3213 * should call this when its cursor moves.
3214 *
3215 * @param rectangle The rectangle.
3216 * @return Whether any parent scrolled.
3217 */
3218 public boolean requestRectangleOnScreen(Rect rectangle) {
3219 return requestRectangleOnScreen(rectangle, false);
3220 }
3221
3222 /**
3223 * Request that a rectangle of this view be visible on the screen,
3224 * scrolling if necessary just enough.
3225 *
3226 * <p>A View should call this if it maintains some notion of which part
3227 * of its content is interesting. For example, a text editing view
3228 * should call this when its cursor moves.
3229 *
3230 * <p>When <code>immediate</code> is set to true, scrolling will not be
3231 * animated.
3232 *
3233 * @param rectangle The rectangle.
3234 * @param immediate True to forbid animated scrolling, false otherwise
3235 * @return Whether any parent scrolled.
3236 */
3237 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3238 View child = this;
3239 ViewParent parent = mParent;
3240 boolean scrolled = false;
3241 while (parent != null) {
3242 scrolled |= parent.requestChildRectangleOnScreen(child,
3243 rectangle, immediate);
3244
3245 // offset rect so next call has the rectangle in the
3246 // coordinate system of its direct child.
3247 rectangle.offset(child.getLeft(), child.getTop());
3248 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3249
3250 if (!(parent instanceof View)) {
3251 break;
3252 }
Romain Guy8506ab42009-06-11 17:35:47 -07003253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 child = (View) parent;
3255 parent = child.getParent();
3256 }
3257 return scrolled;
3258 }
3259
3260 /**
3261 * Called when this view wants to give up focus. This will cause
3262 * {@link #onFocusChanged} to be called.
3263 */
3264 public void clearFocus() {
3265 if (DBG) {
3266 System.out.println(this + " clearFocus()");
3267 }
3268
3269 if ((mPrivateFlags & FOCUSED) != 0) {
3270 mPrivateFlags &= ~FOCUSED;
3271
3272 if (mParent != null) {
3273 mParent.clearChildFocus(this);
3274 }
3275
3276 onFocusChanged(false, 0, null);
3277 refreshDrawableState();
3278 }
3279 }
3280
3281 /**
3282 * Called to clear the focus of a view that is about to be removed.
3283 * Doesn't call clearChildFocus, which prevents this view from taking
3284 * focus again before it has been removed from the parent
3285 */
3286 void clearFocusForRemoval() {
3287 if ((mPrivateFlags & FOCUSED) != 0) {
3288 mPrivateFlags &= ~FOCUSED;
3289
3290 onFocusChanged(false, 0, null);
3291 refreshDrawableState();
3292 }
3293 }
3294
3295 /**
3296 * Called internally by the view system when a new view is getting focus.
3297 * This is what clears the old focus.
3298 */
3299 void unFocus() {
3300 if (DBG) {
3301 System.out.println(this + " unFocus()");
3302 }
3303
3304 if ((mPrivateFlags & FOCUSED) != 0) {
3305 mPrivateFlags &= ~FOCUSED;
3306
3307 onFocusChanged(false, 0, null);
3308 refreshDrawableState();
3309 }
3310 }
3311
3312 /**
3313 * Returns true if this view has focus iteself, or is the ancestor of the
3314 * view that has focus.
3315 *
3316 * @return True if this view has or contains focus, false otherwise.
3317 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003318 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 public boolean hasFocus() {
3320 return (mPrivateFlags & FOCUSED) != 0;
3321 }
3322
3323 /**
3324 * Returns true if this view is focusable or if it contains a reachable View
3325 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3326 * is a View whose parents do not block descendants focus.
3327 *
3328 * Only {@link #VISIBLE} views are considered focusable.
3329 *
3330 * @return True if the view is focusable or if the view contains a focusable
3331 * View, false otherwise.
3332 *
3333 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3334 */
3335 public boolean hasFocusable() {
3336 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3337 }
3338
3339 /**
3340 * Called by the view system when the focus state of this view changes.
3341 * When the focus change event is caused by directional navigation, direction
3342 * and previouslyFocusedRect provide insight into where the focus is coming from.
3343 * When overriding, be sure to call up through to the super class so that
3344 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003345 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 * @param gainFocus True if the View has focus; false otherwise.
3347 * @param direction The direction focus has moved when requestFocus()
3348 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003349 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3350 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3351 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003352 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3353 * system, of the previously focused view. If applicable, this will be
3354 * passed in as finer grained information about where the focus is coming
3355 * from (in addition to direction). Will be <code>null</code> otherwise.
3356 */
3357 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003358 if (gainFocus) {
3359 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3360 }
3361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003362 InputMethodManager imm = InputMethodManager.peekInstance();
3363 if (!gainFocus) {
3364 if (isPressed()) {
3365 setPressed(false);
3366 }
3367 if (imm != null && mAttachInfo != null
3368 && mAttachInfo.mHasWindowFocus) {
3369 imm.focusOut(this);
3370 }
Romain Guya2431d02009-04-30 16:30:00 -07003371 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003372 } else if (imm != null && mAttachInfo != null
3373 && mAttachInfo.mHasWindowFocus) {
3374 imm.focusIn(this);
3375 }
Romain Guy8506ab42009-06-11 17:35:47 -07003376
Romain Guy0fd89bf2011-01-26 15:41:30 -08003377 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003378 if (mOnFocusChangeListener != null) {
3379 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3380 }
Joe Malin32736f02011-01-19 16:14:20 -08003381
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003382 if (mAttachInfo != null) {
3383 mAttachInfo.mKeyDispatchState.reset(this);
3384 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003385 }
3386
3387 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003388 * {@inheritDoc}
3389 */
3390 public void sendAccessibilityEvent(int eventType) {
3391 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3392 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3393 }
3394 }
3395
3396 /**
3397 * {@inheritDoc}
3398 */
3399 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003400 if (!isShown()) {
3401 return;
3402 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003403 event.setClassName(getClass().getName());
3404 event.setPackageName(getContext().getPackageName());
3405 event.setEnabled(isEnabled());
3406 event.setContentDescription(mContentDescription);
3407
3408 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3409 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3410 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3411 event.setItemCount(focusablesTempList.size());
3412 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3413 focusablesTempList.clear();
3414 }
3415
3416 dispatchPopulateAccessibilityEvent(event);
3417
3418 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3419 }
3420
3421 /**
3422 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3423 * to be populated.
3424 *
3425 * @param event The event.
3426 *
3427 * @return True if the event population was completed.
3428 */
3429 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3430 return false;
3431 }
3432
3433 /**
3434 * Gets the {@link View} description. It briefly describes the view and is
3435 * primarily used for accessibility support. Set this property to enable
3436 * better accessibility support for your application. This is especially
3437 * true for views that do not have textual representation (For example,
3438 * ImageButton).
3439 *
3440 * @return The content descriptiopn.
3441 *
3442 * @attr ref android.R.styleable#View_contentDescription
3443 */
3444 public CharSequence getContentDescription() {
3445 return mContentDescription;
3446 }
3447
3448 /**
3449 * Sets the {@link View} description. It briefly describes the view and is
3450 * primarily used for accessibility support. Set this property to enable
3451 * better accessibility support for your application. This is especially
3452 * true for views that do not have textual representation (For example,
3453 * ImageButton).
3454 *
3455 * @param contentDescription The content description.
3456 *
3457 * @attr ref android.R.styleable#View_contentDescription
3458 */
3459 public void setContentDescription(CharSequence contentDescription) {
3460 mContentDescription = contentDescription;
3461 }
3462
3463 /**
Romain Guya2431d02009-04-30 16:30:00 -07003464 * Invoked whenever this view loses focus, either by losing window focus or by losing
3465 * focus within its window. This method can be used to clear any state tied to the
3466 * focus. For instance, if a button is held pressed with the trackball and the window
3467 * loses focus, this method can be used to cancel the press.
3468 *
3469 * Subclasses of View overriding this method should always call super.onFocusLost().
3470 *
3471 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003472 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003473 *
3474 * @hide pending API council approval
3475 */
3476 protected void onFocusLost() {
3477 resetPressedState();
3478 }
3479
3480 private void resetPressedState() {
3481 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3482 return;
3483 }
3484
3485 if (isPressed()) {
3486 setPressed(false);
3487
3488 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003489 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003490 }
3491 }
3492 }
3493
3494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 * Returns true if this view has focus
3496 *
3497 * @return True if this view has focus, false otherwise.
3498 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003499 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 public boolean isFocused() {
3501 return (mPrivateFlags & FOCUSED) != 0;
3502 }
3503
3504 /**
3505 * Find the view in the hierarchy rooted at this view that currently has
3506 * focus.
3507 *
3508 * @return The view that currently has focus, or null if no focused view can
3509 * be found.
3510 */
3511 public View findFocus() {
3512 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3513 }
3514
3515 /**
3516 * Change whether this view is one of the set of scrollable containers in
3517 * its window. This will be used to determine whether the window can
3518 * resize or must pan when a soft input area is open -- scrollable
3519 * containers allow the window to use resize mode since the container
3520 * will appropriately shrink.
3521 */
3522 public void setScrollContainer(boolean isScrollContainer) {
3523 if (isScrollContainer) {
3524 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3525 mAttachInfo.mScrollContainers.add(this);
3526 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3527 }
3528 mPrivateFlags |= SCROLL_CONTAINER;
3529 } else {
3530 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3531 mAttachInfo.mScrollContainers.remove(this);
3532 }
3533 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3534 }
3535 }
3536
3537 /**
3538 * Returns the quality of the drawing cache.
3539 *
3540 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3541 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3542 *
3543 * @see #setDrawingCacheQuality(int)
3544 * @see #setDrawingCacheEnabled(boolean)
3545 * @see #isDrawingCacheEnabled()
3546 *
3547 * @attr ref android.R.styleable#View_drawingCacheQuality
3548 */
3549 public int getDrawingCacheQuality() {
3550 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3551 }
3552
3553 /**
3554 * Set the drawing cache quality of this view. This value is used only when the
3555 * drawing cache is enabled
3556 *
3557 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3558 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3559 *
3560 * @see #getDrawingCacheQuality()
3561 * @see #setDrawingCacheEnabled(boolean)
3562 * @see #isDrawingCacheEnabled()
3563 *
3564 * @attr ref android.R.styleable#View_drawingCacheQuality
3565 */
3566 public void setDrawingCacheQuality(int quality) {
3567 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3568 }
3569
3570 /**
3571 * Returns whether the screen should remain on, corresponding to the current
3572 * value of {@link #KEEP_SCREEN_ON}.
3573 *
3574 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3575 *
3576 * @see #setKeepScreenOn(boolean)
3577 *
3578 * @attr ref android.R.styleable#View_keepScreenOn
3579 */
3580 public boolean getKeepScreenOn() {
3581 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3582 }
3583
3584 /**
3585 * Controls whether the screen should remain on, modifying the
3586 * value of {@link #KEEP_SCREEN_ON}.
3587 *
3588 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3589 *
3590 * @see #getKeepScreenOn()
3591 *
3592 * @attr ref android.R.styleable#View_keepScreenOn
3593 */
3594 public void setKeepScreenOn(boolean keepScreenOn) {
3595 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3596 }
3597
3598 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003599 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3600 * @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 -08003601 *
3602 * @attr ref android.R.styleable#View_nextFocusLeft
3603 */
3604 public int getNextFocusLeftId() {
3605 return mNextFocusLeftId;
3606 }
3607
3608 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003609 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3610 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3611 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003612 *
3613 * @attr ref android.R.styleable#View_nextFocusLeft
3614 */
3615 public void setNextFocusLeftId(int nextFocusLeftId) {
3616 mNextFocusLeftId = nextFocusLeftId;
3617 }
3618
3619 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003620 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3621 * @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 -08003622 *
3623 * @attr ref android.R.styleable#View_nextFocusRight
3624 */
3625 public int getNextFocusRightId() {
3626 return mNextFocusRightId;
3627 }
3628
3629 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003630 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3631 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3632 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 *
3634 * @attr ref android.R.styleable#View_nextFocusRight
3635 */
3636 public void setNextFocusRightId(int nextFocusRightId) {
3637 mNextFocusRightId = nextFocusRightId;
3638 }
3639
3640 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003641 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3642 * @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 -08003643 *
3644 * @attr ref android.R.styleable#View_nextFocusUp
3645 */
3646 public int getNextFocusUpId() {
3647 return mNextFocusUpId;
3648 }
3649
3650 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003651 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3652 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3653 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003654 *
3655 * @attr ref android.R.styleable#View_nextFocusUp
3656 */
3657 public void setNextFocusUpId(int nextFocusUpId) {
3658 mNextFocusUpId = nextFocusUpId;
3659 }
3660
3661 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003662 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3663 * @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 -08003664 *
3665 * @attr ref android.R.styleable#View_nextFocusDown
3666 */
3667 public int getNextFocusDownId() {
3668 return mNextFocusDownId;
3669 }
3670
3671 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003672 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3673 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3674 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003675 *
3676 * @attr ref android.R.styleable#View_nextFocusDown
3677 */
3678 public void setNextFocusDownId(int nextFocusDownId) {
3679 mNextFocusDownId = nextFocusDownId;
3680 }
3681
3682 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003683 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3684 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3685 *
3686 * @attr ref android.R.styleable#View_nextFocusForward
3687 */
3688 public int getNextFocusForwardId() {
3689 return mNextFocusForwardId;
3690 }
3691
3692 /**
3693 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3694 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3695 * decide automatically.
3696 *
3697 * @attr ref android.R.styleable#View_nextFocusForward
3698 */
3699 public void setNextFocusForwardId(int nextFocusForwardId) {
3700 mNextFocusForwardId = nextFocusForwardId;
3701 }
3702
3703 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003704 * Returns the visibility of this view and all of its ancestors
3705 *
3706 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3707 */
3708 public boolean isShown() {
3709 View current = this;
3710 //noinspection ConstantConditions
3711 do {
3712 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3713 return false;
3714 }
3715 ViewParent parent = current.mParent;
3716 if (parent == null) {
3717 return false; // We are not attached to the view root
3718 }
3719 if (!(parent instanceof View)) {
3720 return true;
3721 }
3722 current = (View) parent;
3723 } while (current != null);
3724
3725 return false;
3726 }
3727
3728 /**
3729 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3730 * is set
3731 *
3732 * @param insets Insets for system windows
3733 *
3734 * @return True if this view applied the insets, false otherwise
3735 */
3736 protected boolean fitSystemWindows(Rect insets) {
3737 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3738 mPaddingLeft = insets.left;
3739 mPaddingTop = insets.top;
3740 mPaddingRight = insets.right;
3741 mPaddingBottom = insets.bottom;
3742 requestLayout();
3743 return true;
3744 }
3745 return false;
3746 }
3747
3748 /**
3749 * Returns the visibility status for this view.
3750 *
3751 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3752 * @attr ref android.R.styleable#View_visibility
3753 */
3754 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003755 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3756 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3757 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003758 })
3759 public int getVisibility() {
3760 return mViewFlags & VISIBILITY_MASK;
3761 }
3762
3763 /**
3764 * Set the enabled state of this view.
3765 *
3766 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3767 * @attr ref android.R.styleable#View_visibility
3768 */
3769 @RemotableViewMethod
3770 public void setVisibility(int visibility) {
3771 setFlags(visibility, VISIBILITY_MASK);
3772 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3773 }
3774
3775 /**
3776 * Returns the enabled status for this view. The interpretation of the
3777 * enabled state varies by subclass.
3778 *
3779 * @return True if this view is enabled, false otherwise.
3780 */
3781 @ViewDebug.ExportedProperty
3782 public boolean isEnabled() {
3783 return (mViewFlags & ENABLED_MASK) == ENABLED;
3784 }
3785
3786 /**
3787 * Set the enabled state of this view. The interpretation of the enabled
3788 * state varies by subclass.
3789 *
3790 * @param enabled True if this view is enabled, false otherwise.
3791 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003792 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003793 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003794 if (enabled == isEnabled()) return;
3795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003796 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3797
3798 /*
3799 * The View most likely has to change its appearance, so refresh
3800 * the drawable state.
3801 */
3802 refreshDrawableState();
3803
3804 // Invalidate too, since the default behavior for views is to be
3805 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003806 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003807 }
3808
3809 /**
3810 * Set whether this view can receive the focus.
3811 *
3812 * Setting this to false will also ensure that this view is not focusable
3813 * in touch mode.
3814 *
3815 * @param focusable If true, this view can receive the focus.
3816 *
3817 * @see #setFocusableInTouchMode(boolean)
3818 * @attr ref android.R.styleable#View_focusable
3819 */
3820 public void setFocusable(boolean focusable) {
3821 if (!focusable) {
3822 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3823 }
3824 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3825 }
3826
3827 /**
3828 * Set whether this view can receive focus while in touch mode.
3829 *
3830 * Setting this to true will also ensure that this view is focusable.
3831 *
3832 * @param focusableInTouchMode If true, this view can receive the focus while
3833 * in touch mode.
3834 *
3835 * @see #setFocusable(boolean)
3836 * @attr ref android.R.styleable#View_focusableInTouchMode
3837 */
3838 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3839 // Focusable in touch mode should always be set before the focusable flag
3840 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3841 // which, in touch mode, will not successfully request focus on this view
3842 // because the focusable in touch mode flag is not set
3843 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3844 if (focusableInTouchMode) {
3845 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3846 }
3847 }
3848
3849 /**
3850 * Set whether this view should have sound effects enabled for events such as
3851 * clicking and touching.
3852 *
3853 * <p>You may wish to disable sound effects for a view if you already play sounds,
3854 * for instance, a dial key that plays dtmf tones.
3855 *
3856 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3857 * @see #isSoundEffectsEnabled()
3858 * @see #playSoundEffect(int)
3859 * @attr ref android.R.styleable#View_soundEffectsEnabled
3860 */
3861 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3862 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3863 }
3864
3865 /**
3866 * @return whether this view should have sound effects enabled for events such as
3867 * clicking and touching.
3868 *
3869 * @see #setSoundEffectsEnabled(boolean)
3870 * @see #playSoundEffect(int)
3871 * @attr ref android.R.styleable#View_soundEffectsEnabled
3872 */
3873 @ViewDebug.ExportedProperty
3874 public boolean isSoundEffectsEnabled() {
3875 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3876 }
3877
3878 /**
3879 * Set whether this view should have haptic feedback for events such as
3880 * long presses.
3881 *
3882 * <p>You may wish to disable haptic feedback if your view already controls
3883 * its own haptic feedback.
3884 *
3885 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3886 * @see #isHapticFeedbackEnabled()
3887 * @see #performHapticFeedback(int)
3888 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3889 */
3890 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3891 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3892 }
3893
3894 /**
3895 * @return whether this view should have haptic feedback enabled for events
3896 * long presses.
3897 *
3898 * @see #setHapticFeedbackEnabled(boolean)
3899 * @see #performHapticFeedback(int)
3900 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3901 */
3902 @ViewDebug.ExportedProperty
3903 public boolean isHapticFeedbackEnabled() {
3904 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3905 }
3906
3907 /**
3908 * If this view doesn't do any drawing on its own, set this flag to
3909 * allow further optimizations. By default, this flag is not set on
3910 * View, but could be set on some View subclasses such as ViewGroup.
3911 *
3912 * Typically, if you override {@link #onDraw} you should clear this flag.
3913 *
3914 * @param willNotDraw whether or not this View draw on its own
3915 */
3916 public void setWillNotDraw(boolean willNotDraw) {
3917 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3918 }
3919
3920 /**
3921 * Returns whether or not this View draws on its own.
3922 *
3923 * @return true if this view has nothing to draw, false otherwise
3924 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003925 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 public boolean willNotDraw() {
3927 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3928 }
3929
3930 /**
3931 * When a View's drawing cache is enabled, drawing is redirected to an
3932 * offscreen bitmap. Some views, like an ImageView, must be able to
3933 * bypass this mechanism if they already draw a single bitmap, to avoid
3934 * unnecessary usage of the memory.
3935 *
3936 * @param willNotCacheDrawing true if this view does not cache its
3937 * drawing, false otherwise
3938 */
3939 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3940 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3941 }
3942
3943 /**
3944 * Returns whether or not this View can cache its drawing or not.
3945 *
3946 * @return true if this view does not cache its drawing, false otherwise
3947 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003948 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003949 public boolean willNotCacheDrawing() {
3950 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3951 }
3952
3953 /**
3954 * Indicates whether this view reacts to click events or not.
3955 *
3956 * @return true if the view is clickable, false otherwise
3957 *
3958 * @see #setClickable(boolean)
3959 * @attr ref android.R.styleable#View_clickable
3960 */
3961 @ViewDebug.ExportedProperty
3962 public boolean isClickable() {
3963 return (mViewFlags & CLICKABLE) == CLICKABLE;
3964 }
3965
3966 /**
3967 * Enables or disables click events for this view. When a view
3968 * is clickable it will change its state to "pressed" on every click.
3969 * Subclasses should set the view clickable to visually react to
3970 * user's clicks.
3971 *
3972 * @param clickable true to make the view clickable, false otherwise
3973 *
3974 * @see #isClickable()
3975 * @attr ref android.R.styleable#View_clickable
3976 */
3977 public void setClickable(boolean clickable) {
3978 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3979 }
3980
3981 /**
3982 * Indicates whether this view reacts to long click events or not.
3983 *
3984 * @return true if the view is long clickable, false otherwise
3985 *
3986 * @see #setLongClickable(boolean)
3987 * @attr ref android.R.styleable#View_longClickable
3988 */
3989 public boolean isLongClickable() {
3990 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3991 }
3992
3993 /**
3994 * Enables or disables long click events for this view. When a view is long
3995 * clickable it reacts to the user holding down the button for a longer
3996 * duration than a tap. This event can either launch the listener or a
3997 * context menu.
3998 *
3999 * @param longClickable true to make the view long clickable, false otherwise
4000 * @see #isLongClickable()
4001 * @attr ref android.R.styleable#View_longClickable
4002 */
4003 public void setLongClickable(boolean longClickable) {
4004 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4005 }
4006
4007 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004008 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009 *
4010 * @see #isClickable()
4011 * @see #setClickable(boolean)
4012 *
4013 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4014 * the View's internal state from a previously set "pressed" state.
4015 */
4016 public void setPressed(boolean pressed) {
4017 if (pressed) {
4018 mPrivateFlags |= PRESSED;
4019 } else {
4020 mPrivateFlags &= ~PRESSED;
4021 }
4022 refreshDrawableState();
4023 dispatchSetPressed(pressed);
4024 }
4025
4026 /**
4027 * Dispatch setPressed to all of this View's children.
4028 *
4029 * @see #setPressed(boolean)
4030 *
4031 * @param pressed The new pressed state
4032 */
4033 protected void dispatchSetPressed(boolean pressed) {
4034 }
4035
4036 /**
4037 * Indicates whether the view is currently in pressed state. Unless
4038 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4039 * the pressed state.
4040 *
4041 * @see #setPressed
4042 * @see #isClickable()
4043 * @see #setClickable(boolean)
4044 *
4045 * @return true if the view is currently pressed, false otherwise
4046 */
4047 public boolean isPressed() {
4048 return (mPrivateFlags & PRESSED) == PRESSED;
4049 }
4050
4051 /**
4052 * Indicates whether this view will save its state (that is,
4053 * whether its {@link #onSaveInstanceState} method will be called).
4054 *
4055 * @return Returns true if the view state saving is enabled, else false.
4056 *
4057 * @see #setSaveEnabled(boolean)
4058 * @attr ref android.R.styleable#View_saveEnabled
4059 */
4060 public boolean isSaveEnabled() {
4061 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4062 }
4063
4064 /**
4065 * Controls whether the saving of this view's state is
4066 * enabled (that is, whether its {@link #onSaveInstanceState} method
4067 * will be called). Note that even if freezing is enabled, the
4068 * view still must have an id assigned to it (via {@link #setId setId()})
4069 * for its state to be saved. This flag can only disable the
4070 * saving of this view; any child views may still have their state saved.
4071 *
4072 * @param enabled Set to false to <em>disable</em> state saving, or true
4073 * (the default) to allow it.
4074 *
4075 * @see #isSaveEnabled()
4076 * @see #setId(int)
4077 * @see #onSaveInstanceState()
4078 * @attr ref android.R.styleable#View_saveEnabled
4079 */
4080 public void setSaveEnabled(boolean enabled) {
4081 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4082 }
4083
Jeff Brown85a31762010-09-01 17:01:00 -07004084 /**
4085 * Gets whether the framework should discard touches when the view's
4086 * window is obscured by another visible window.
4087 * Refer to the {@link View} security documentation for more details.
4088 *
4089 * @return True if touch filtering is enabled.
4090 *
4091 * @see #setFilterTouchesWhenObscured(boolean)
4092 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4093 */
4094 @ViewDebug.ExportedProperty
4095 public boolean getFilterTouchesWhenObscured() {
4096 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4097 }
4098
4099 /**
4100 * Sets whether the framework should discard touches when the view's
4101 * window is obscured by another visible window.
4102 * Refer to the {@link View} security documentation for more details.
4103 *
4104 * @param enabled True if touch filtering should be enabled.
4105 *
4106 * @see #getFilterTouchesWhenObscured
4107 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4108 */
4109 public void setFilterTouchesWhenObscured(boolean enabled) {
4110 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4111 FILTER_TOUCHES_WHEN_OBSCURED);
4112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004113
4114 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004115 * Indicates whether the entire hierarchy under this view will save its
4116 * state when a state saving traversal occurs from its parent. The default
4117 * is true; if false, these views will not be saved unless
4118 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4119 *
4120 * @return Returns true if the view state saving from parent is enabled, else false.
4121 *
4122 * @see #setSaveFromParentEnabled(boolean)
4123 */
4124 public boolean isSaveFromParentEnabled() {
4125 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4126 }
4127
4128 /**
4129 * Controls whether the entire hierarchy under this view will save its
4130 * state when a state saving traversal occurs from its parent. The default
4131 * is true; if false, these views will not be saved unless
4132 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4133 *
4134 * @param enabled Set to false to <em>disable</em> state saving, or true
4135 * (the default) to allow it.
4136 *
4137 * @see #isSaveFromParentEnabled()
4138 * @see #setId(int)
4139 * @see #onSaveInstanceState()
4140 */
4141 public void setSaveFromParentEnabled(boolean enabled) {
4142 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4143 }
4144
4145
4146 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004147 * Returns whether this View is able to take focus.
4148 *
4149 * @return True if this view can take focus, or false otherwise.
4150 * @attr ref android.R.styleable#View_focusable
4151 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004152 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004153 public final boolean isFocusable() {
4154 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4155 }
4156
4157 /**
4158 * When a view is focusable, it may not want to take focus when in touch mode.
4159 * For example, a button would like focus when the user is navigating via a D-pad
4160 * so that the user can click on it, but once the user starts touching the screen,
4161 * the button shouldn't take focus
4162 * @return Whether the view is focusable in touch mode.
4163 * @attr ref android.R.styleable#View_focusableInTouchMode
4164 */
4165 @ViewDebug.ExportedProperty
4166 public final boolean isFocusableInTouchMode() {
4167 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4168 }
4169
4170 /**
4171 * Find the nearest view in the specified direction that can take focus.
4172 * This does not actually give focus to that view.
4173 *
4174 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4175 *
4176 * @return The nearest focusable in the specified direction, or null if none
4177 * can be found.
4178 */
4179 public View focusSearch(int direction) {
4180 if (mParent != null) {
4181 return mParent.focusSearch(this, direction);
4182 } else {
4183 return null;
4184 }
4185 }
4186
4187 /**
4188 * This method is the last chance for the focused view and its ancestors to
4189 * respond to an arrow key. This is called when the focused view did not
4190 * consume the key internally, nor could the view system find a new view in
4191 * the requested direction to give focus to.
4192 *
4193 * @param focused The currently focused view.
4194 * @param direction The direction focus wants to move. One of FOCUS_UP,
4195 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4196 * @return True if the this view consumed this unhandled move.
4197 */
4198 public boolean dispatchUnhandledMove(View focused, int direction) {
4199 return false;
4200 }
4201
4202 /**
4203 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004204 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004205 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004206 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4207 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 * @return The user specified next view, or null if there is none.
4209 */
4210 View findUserSetNextFocus(View root, int direction) {
4211 switch (direction) {
4212 case FOCUS_LEFT:
4213 if (mNextFocusLeftId == View.NO_ID) return null;
4214 return findViewShouldExist(root, mNextFocusLeftId);
4215 case FOCUS_RIGHT:
4216 if (mNextFocusRightId == View.NO_ID) return null;
4217 return findViewShouldExist(root, mNextFocusRightId);
4218 case FOCUS_UP:
4219 if (mNextFocusUpId == View.NO_ID) return null;
4220 return findViewShouldExist(root, mNextFocusUpId);
4221 case FOCUS_DOWN:
4222 if (mNextFocusDownId == View.NO_ID) return null;
4223 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004224 case FOCUS_FORWARD:
4225 if (mNextFocusForwardId == View.NO_ID) return null;
4226 return findViewShouldExist(root, mNextFocusForwardId);
4227 case FOCUS_BACKWARD: {
4228 final int id = mID;
4229 return root.findViewByPredicate(new Predicate<View>() {
4230 @Override
4231 public boolean apply(View t) {
4232 return t.mNextFocusForwardId == id;
4233 }
4234 });
4235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 }
4237 return null;
4238 }
4239
4240 private static View findViewShouldExist(View root, int childViewId) {
4241 View result = root.findViewById(childViewId);
4242 if (result == null) {
4243 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4244 + "by user for id " + childViewId);
4245 }
4246 return result;
4247 }
4248
4249 /**
4250 * Find and return all focusable views that are descendants of this view,
4251 * possibly including this view if it is focusable itself.
4252 *
4253 * @param direction The direction of the focus
4254 * @return A list of focusable views
4255 */
4256 public ArrayList<View> getFocusables(int direction) {
4257 ArrayList<View> result = new ArrayList<View>(24);
4258 addFocusables(result, direction);
4259 return result;
4260 }
4261
4262 /**
4263 * Add any focusable views that are descendants of this view (possibly
4264 * including this view if it is focusable itself) to views. If we are in touch mode,
4265 * only add views that are also focusable in touch mode.
4266 *
4267 * @param views Focusable views found so far
4268 * @param direction The direction of the focus
4269 */
4270 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004271 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4272 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004273
svetoslavganov75986cf2009-05-14 22:28:01 -07004274 /**
4275 * Adds any focusable views that are descendants of this view (possibly
4276 * including this view if it is focusable itself) to views. This method
4277 * adds all focusable views regardless if we are in touch mode or
4278 * only views focusable in touch mode if we are in touch mode depending on
4279 * the focusable mode paramater.
4280 *
4281 * @param views Focusable views found so far or null if all we are interested is
4282 * the number of focusables.
4283 * @param direction The direction of the focus.
4284 * @param focusableMode The type of focusables to be added.
4285 *
4286 * @see #FOCUSABLES_ALL
4287 * @see #FOCUSABLES_TOUCH_MODE
4288 */
4289 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4290 if (!isFocusable()) {
4291 return;
4292 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004293
svetoslavganov75986cf2009-05-14 22:28:01 -07004294 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4295 isInTouchMode() && !isFocusableInTouchMode()) {
4296 return;
4297 }
4298
4299 if (views != null) {
4300 views.add(this);
4301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004302 }
4303
4304 /**
4305 * Find and return all touchable views that are descendants of this view,
4306 * possibly including this view if it is touchable itself.
4307 *
4308 * @return A list of touchable views
4309 */
4310 public ArrayList<View> getTouchables() {
4311 ArrayList<View> result = new ArrayList<View>();
4312 addTouchables(result);
4313 return result;
4314 }
4315
4316 /**
4317 * Add any touchable views that are descendants of this view (possibly
4318 * including this view if it is touchable itself) to views.
4319 *
4320 * @param views Touchable views found so far
4321 */
4322 public void addTouchables(ArrayList<View> views) {
4323 final int viewFlags = mViewFlags;
4324
4325 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4326 && (viewFlags & ENABLED_MASK) == ENABLED) {
4327 views.add(this);
4328 }
4329 }
4330
4331 /**
4332 * Call this to try to give focus to a specific view or to one of its
4333 * descendants.
4334 *
4335 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4336 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4337 * while the device is in touch mode.
4338 *
4339 * See also {@link #focusSearch}, which is what you call to say that you
4340 * have focus, and you want your parent to look for the next one.
4341 *
4342 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4343 * {@link #FOCUS_DOWN} and <code>null</code>.
4344 *
4345 * @return Whether this view or one of its descendants actually took focus.
4346 */
4347 public final boolean requestFocus() {
4348 return requestFocus(View.FOCUS_DOWN);
4349 }
4350
4351
4352 /**
4353 * Call this to try to give focus to a specific view or to one of its
4354 * descendants and give it a hint about what direction focus is heading.
4355 *
4356 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4357 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4358 * while the device is in touch mode.
4359 *
4360 * See also {@link #focusSearch}, which is what you call to say that you
4361 * have focus, and you want your parent to look for the next one.
4362 *
4363 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4364 * <code>null</code> set for the previously focused rectangle.
4365 *
4366 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4367 * @return Whether this view or one of its descendants actually took focus.
4368 */
4369 public final boolean requestFocus(int direction) {
4370 return requestFocus(direction, null);
4371 }
4372
4373 /**
4374 * Call this to try to give focus to a specific view or to one of its descendants
4375 * and give it hints about the direction and a specific rectangle that the focus
4376 * is coming from. The rectangle can help give larger views a finer grained hint
4377 * about where focus is coming from, and therefore, where to show selection, or
4378 * forward focus change internally.
4379 *
4380 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4381 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4382 * while the device is in touch mode.
4383 *
4384 * A View will not take focus if it is not visible.
4385 *
4386 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
4387 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
4388 *
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 /**
Romain Guya5364ee2011-02-24 14:46:04 -08005784 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
5785 * views are drawn) from the camera to this view. The camera's distance
5786 * affects 3D transformations, for instance rotations around the X and Y
5787 * axis. If the rotationX or rotationY properties are changed and this view is
5788 * large (more than half the size of the screen), it is recommended to always
5789 * use a camera distance that's greater than the height (X axis rotation) or
5790 * the width (Y axis rotation) of this view.</p>
5791 *
5792 * <p>The distance of the camera from the view plane can have an affect on the
5793 * perspective distortion of the view when it is rotated around the x or y axis.
5794 * For example, a large distance will result in a large viewing angle, and there
5795 * will not be much perspective distortion of the view as it rotates. A short
5796 * distance may cause much more perspective distortion upon rotation, and can
5797 * also result in some drawing artifacts if the rotated view ends up partially
5798 * behind the camera (which is why the recommendation is to use a distance at
5799 * least as far as the size of the view, if the view is to be rotated.)</p>
5800 *
5801 * <p>The distance is expressed in "depth pixels." The default distance depends
5802 * on the screen density. For instance, on a medium density display, the
5803 * default distance is 1280. On a high density display, the default distance
5804 * is 1920.</p>
5805 *
5806 * <p>If you want to specify a distance that leads to visually consistent
5807 * results across various densities, use the following formula:</p>
5808 * <pre>
5809 * float scale = context.getResources().getDisplayMetrics().density;
5810 * view.setCameraDistance(distance * scale);
5811 * </pre>
5812 *
5813 * <p>The density scale factor of a high density display is 1.5,
5814 * and 1920 = 1280 * 1.5.</p>
5815 *
5816 * @param distance The distance in "depth pixels", if negative the opposite
5817 * value is used
5818 *
5819 * @see #setRotationX(float)
5820 * @see #setRotationY(float)
5821 */
5822 public void setCameraDistance(float distance) {
5823 invalidateParentCaches();
5824 invalidate(false);
5825
5826 final float dpi = mResources.getDisplayMetrics().densityDpi;
5827 if (mCamera == null) {
5828 mCamera = new Camera();
5829 matrix3D = new Matrix();
5830 }
5831
5832 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
5833 mMatrixDirty = true;
5834
5835 invalidate(false);
5836 }
5837
5838 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005839 * The degrees that the view is rotated around the pivot point.
5840 *
Romain Guya5364ee2011-02-24 14:46:04 -08005841 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07005842 * @see #getPivotX()
5843 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005844 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005845 * @return The degrees of rotation.
5846 */
5847 public float getRotation() {
5848 return mRotation;
5849 }
5850
5851 /**
Chet Haase897247b2010-09-09 14:54:47 -07005852 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5853 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005854 *
5855 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005856 *
5857 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07005858 * @see #getPivotX()
5859 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005860 * @see #setRotationX(float)
5861 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08005862 *
5863 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005864 */
5865 public void setRotation(float rotation) {
5866 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005867 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005868 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005869 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005870 mRotation = rotation;
5871 mMatrixDirty = true;
5872 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005873 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005874 }
5875 }
5876
5877 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005878 * The degrees that the view is rotated around the vertical axis through the pivot point.
5879 *
5880 * @see #getPivotX()
5881 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005882 * @see #setRotationY(float)
5883 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005884 * @return The degrees of Y rotation.
5885 */
5886 public float getRotationY() {
5887 return mRotationY;
5888 }
5889
5890 /**
Chet Haase897247b2010-09-09 14:54:47 -07005891 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5892 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5893 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08005894 *
5895 * When rotating large views, it is recommended to adjust the camera distance
5896 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07005897 *
5898 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005899 *
5900 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07005901 * @see #getPivotX()
5902 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005903 * @see #setRotation(float)
5904 * @see #setRotationX(float)
5905 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08005906 *
5907 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005908 */
5909 public void setRotationY(float rotationY) {
5910 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005911 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005912 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005913 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005914 mRotationY = rotationY;
5915 mMatrixDirty = true;
5916 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005917 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005918 }
5919 }
5920
5921 /**
5922 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5923 *
5924 * @see #getPivotX()
5925 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005926 * @see #setRotationX(float)
5927 *
Chet Haasefd2b0022010-08-06 13:08:56 -07005928 * @return The degrees of X rotation.
5929 */
5930 public float getRotationX() {
5931 return mRotationX;
5932 }
5933
5934 /**
Chet Haase897247b2010-09-09 14:54:47 -07005935 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
5936 * Increasing values result in clockwise rotation from the viewpoint of looking down the
5937 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08005938 *
5939 * When rotating large views, it is recommended to adjust the camera distance
5940 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07005941 *
5942 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08005943 *
5944 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07005945 * @see #getPivotX()
5946 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08005947 * @see #setRotation(float)
5948 * @see #setRotationY(float)
5949 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08005950 *
5951 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07005952 */
5953 public void setRotationX(float rotationX) {
5954 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005955 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005956 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005957 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005958 mRotationX = rotationX;
5959 mMatrixDirty = true;
5960 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005961 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005962 }
5963 }
5964
5965 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005966 * The amount that the view is scaled in x around the pivot point, as a proportion of
5967 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
5968 *
Joe Onorato93162322010-09-16 15:42:01 -04005969 * <p>By default, this is 1.0f.
5970 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005971 * @see #getPivotX()
5972 * @see #getPivotY()
5973 * @return The scaling factor.
5974 */
5975 public float getScaleX() {
5976 return mScaleX;
5977 }
5978
5979 /**
5980 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
5981 * the view's unscaled width. A value of 1 means that no scaling is applied.
5982 *
5983 * @param scaleX The scaling factor.
5984 * @see #getPivotX()
5985 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005986 *
5987 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07005988 */
5989 public void setScaleX(float scaleX) {
5990 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005991 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005992 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005993 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005994 mScaleX = scaleX;
5995 mMatrixDirty = true;
5996 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005997 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005998 }
5999 }
6000
6001 /**
6002 * The amount that the view is scaled in y around the pivot point, as a proportion of
6003 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6004 *
Joe Onorato93162322010-09-16 15:42:01 -04006005 * <p>By default, this is 1.0f.
6006 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006007 * @see #getPivotX()
6008 * @see #getPivotY()
6009 * @return The scaling factor.
6010 */
6011 public float getScaleY() {
6012 return mScaleY;
6013 }
6014
6015 /**
6016 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6017 * the view's unscaled width. A value of 1 means that no scaling is applied.
6018 *
6019 * @param scaleY The scaling factor.
6020 * @see #getPivotX()
6021 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006022 *
6023 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006024 */
6025 public void setScaleY(float scaleY) {
6026 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006027 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006028 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006029 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006030 mScaleY = scaleY;
6031 mMatrixDirty = true;
6032 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006033 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006034 }
6035 }
6036
6037 /**
6038 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6039 * and {@link #setScaleX(float) scaled}.
6040 *
6041 * @see #getRotation()
6042 * @see #getScaleX()
6043 * @see #getScaleY()
6044 * @see #getPivotY()
6045 * @return The x location of the pivot point.
6046 */
6047 public float getPivotX() {
6048 return mPivotX;
6049 }
6050
6051 /**
6052 * Sets the x location of the point around which the view is
6053 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006054 * By default, the pivot point is centered on the object.
6055 * Setting this property disables this behavior and causes the view to use only the
6056 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006057 *
6058 * @param pivotX The x location of the pivot point.
6059 * @see #getRotation()
6060 * @see #getScaleX()
6061 * @see #getScaleY()
6062 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006063 *
6064 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006065 */
6066 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006067 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006068 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006069 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006070 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006071 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006072 mPivotX = pivotX;
6073 mMatrixDirty = true;
6074 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006075 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006076 }
6077 }
6078
6079 /**
6080 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6081 * and {@link #setScaleY(float) scaled}.
6082 *
6083 * @see #getRotation()
6084 * @see #getScaleX()
6085 * @see #getScaleY()
6086 * @see #getPivotY()
6087 * @return The y location of the pivot point.
6088 */
6089 public float getPivotY() {
6090 return mPivotY;
6091 }
6092
6093 /**
6094 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006095 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6096 * Setting this property disables this behavior and causes the view to use only the
6097 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006098 *
6099 * @param pivotY The y location of the pivot point.
6100 * @see #getRotation()
6101 * @see #getScaleX()
6102 * @see #getScaleY()
6103 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006104 *
6105 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006106 */
6107 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006108 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006109 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006110 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006111 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006112 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006113 mPivotY = pivotY;
6114 mMatrixDirty = true;
6115 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006116 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006117 }
6118 }
6119
6120 /**
6121 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6122 * completely transparent and 1 means the view is completely opaque.
6123 *
Joe Onorato93162322010-09-16 15:42:01 -04006124 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006125 * @return The opacity of the view.
6126 */
6127 public float getAlpha() {
6128 return mAlpha;
6129 }
6130
6131 /**
Romain Guy171c5922011-01-06 10:04:23 -08006132 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6133 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006134 *
Romain Guy171c5922011-01-06 10:04:23 -08006135 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6136 * responsible for applying the opacity itself. Otherwise, calling this method is
6137 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006138 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006139 *
6140 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006141 *
Joe Malin32736f02011-01-19 16:14:20 -08006142 * @see #setLayerType(int, android.graphics.Paint)
6143 *
Chet Haase73066682010-11-29 15:55:32 -08006144 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006145 */
6146 public void setAlpha(float alpha) {
6147 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006148 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006149 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006150 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006151 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006152 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006153 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006154 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006155 invalidate(false);
6156 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006157 }
6158
6159 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 * Top position of this view relative to its parent.
6161 *
6162 * @return The top of this view, in pixels.
6163 */
6164 @ViewDebug.CapturedViewProperty
6165 public final int getTop() {
6166 return mTop;
6167 }
6168
6169 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006170 * Sets the top position of this view relative to its parent. This method is meant to be called
6171 * by the layout system and should not generally be called otherwise, because the property
6172 * may be changed at any time by the layout.
6173 *
6174 * @param top The top of this view, in pixels.
6175 */
6176 public final void setTop(int top) {
6177 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006178 updateMatrix();
6179 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006180 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006181 int minTop;
6182 int yLoc;
6183 if (top < mTop) {
6184 minTop = top;
6185 yLoc = top - mTop;
6186 } else {
6187 minTop = mTop;
6188 yLoc = 0;
6189 }
Chet Haasee9140a72011-02-16 16:23:29 -08006190 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006191 }
6192 } else {
6193 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006194 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006195 }
6196
Chet Haaseed032702010-10-01 14:05:54 -07006197 int width = mRight - mLeft;
6198 int oldHeight = mBottom - mTop;
6199
Chet Haase21cd1382010-09-01 17:42:29 -07006200 mTop = top;
6201
Chet Haaseed032702010-10-01 14:05:54 -07006202 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6203
Chet Haase21cd1382010-09-01 17:42:29 -07006204 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006205 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6206 // A change in dimension means an auto-centered pivot point changes, too
6207 mMatrixDirty = true;
6208 }
Chet Haase21cd1382010-09-01 17:42:29 -07006209 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006210 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006211 }
Chet Haase55dbb652010-12-21 20:15:08 -08006212 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006213 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006214 }
6215 }
6216
6217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218 * Bottom position of this view relative to its parent.
6219 *
6220 * @return The bottom of this view, in pixels.
6221 */
6222 @ViewDebug.CapturedViewProperty
6223 public final int getBottom() {
6224 return mBottom;
6225 }
6226
6227 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006228 * True if this view has changed since the last time being drawn.
6229 *
6230 * @return The dirty state of this view.
6231 */
6232 public boolean isDirty() {
6233 return (mPrivateFlags & DIRTY_MASK) != 0;
6234 }
6235
6236 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006237 * Sets the bottom position of this view relative to its parent. This method is meant to be
6238 * called by the layout system and should not generally be called otherwise, because the
6239 * property may be changed at any time by the layout.
6240 *
6241 * @param bottom The bottom of this view, in pixels.
6242 */
6243 public final void setBottom(int bottom) {
6244 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006245 updateMatrix();
6246 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006247 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006248 int maxBottom;
6249 if (bottom < mBottom) {
6250 maxBottom = mBottom;
6251 } else {
6252 maxBottom = bottom;
6253 }
Chet Haasee9140a72011-02-16 16:23:29 -08006254 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006255 }
6256 } else {
6257 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006258 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006259 }
6260
Chet Haaseed032702010-10-01 14:05:54 -07006261 int width = mRight - mLeft;
6262 int oldHeight = mBottom - mTop;
6263
Chet Haase21cd1382010-09-01 17:42:29 -07006264 mBottom = bottom;
6265
Chet Haaseed032702010-10-01 14:05:54 -07006266 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6267
Chet Haase21cd1382010-09-01 17:42:29 -07006268 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006269 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6270 // A change in dimension means an auto-centered pivot point changes, too
6271 mMatrixDirty = true;
6272 }
Chet Haase21cd1382010-09-01 17:42:29 -07006273 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006274 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006275 }
Chet Haase55dbb652010-12-21 20:15:08 -08006276 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006277 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006278 }
6279 }
6280
6281 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 * Left position of this view relative to its parent.
6283 *
6284 * @return The left edge of this view, in pixels.
6285 */
6286 @ViewDebug.CapturedViewProperty
6287 public final int getLeft() {
6288 return mLeft;
6289 }
6290
6291 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006292 * Sets the left position of this view relative to its parent. This method is meant to be called
6293 * by the layout system and should not generally be called otherwise, because the property
6294 * may be changed at any time by the layout.
6295 *
6296 * @param left The bottom of this view, in pixels.
6297 */
6298 public final void setLeft(int left) {
6299 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006300 updateMatrix();
6301 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006302 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006303 int minLeft;
6304 int xLoc;
6305 if (left < mLeft) {
6306 minLeft = left;
6307 xLoc = left - mLeft;
6308 } else {
6309 minLeft = mLeft;
6310 xLoc = 0;
6311 }
Chet Haasee9140a72011-02-16 16:23:29 -08006312 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006313 }
6314 } else {
6315 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006316 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006317 }
6318
Chet Haaseed032702010-10-01 14:05:54 -07006319 int oldWidth = mRight - mLeft;
6320 int height = mBottom - mTop;
6321
Chet Haase21cd1382010-09-01 17:42:29 -07006322 mLeft = left;
6323
Chet Haaseed032702010-10-01 14:05:54 -07006324 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6325
Chet Haase21cd1382010-09-01 17:42:29 -07006326 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006327 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6328 // A change in dimension means an auto-centered pivot point changes, too
6329 mMatrixDirty = true;
6330 }
Chet Haase21cd1382010-09-01 17:42:29 -07006331 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006332 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006333 }
Chet Haase55dbb652010-12-21 20:15:08 -08006334 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006335 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006336 }
6337 }
6338
6339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006340 * Right position of this view relative to its parent.
6341 *
6342 * @return The right edge of this view, in pixels.
6343 */
6344 @ViewDebug.CapturedViewProperty
6345 public final int getRight() {
6346 return mRight;
6347 }
6348
6349 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006350 * Sets the right position of this view relative to its parent. This method is meant to be called
6351 * by the layout system and should not generally be called otherwise, because the property
6352 * may be changed at any time by the layout.
6353 *
6354 * @param right The bottom of this view, in pixels.
6355 */
6356 public final void setRight(int right) {
6357 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006358 updateMatrix();
6359 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006360 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006361 int maxRight;
6362 if (right < mRight) {
6363 maxRight = mRight;
6364 } else {
6365 maxRight = right;
6366 }
Chet Haasee9140a72011-02-16 16:23:29 -08006367 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006368 }
6369 } else {
6370 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006371 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006372 }
6373
Chet Haaseed032702010-10-01 14:05:54 -07006374 int oldWidth = mRight - mLeft;
6375 int height = mBottom - mTop;
6376
Chet Haase21cd1382010-09-01 17:42:29 -07006377 mRight = right;
6378
Chet Haaseed032702010-10-01 14:05:54 -07006379 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6380
Chet Haase21cd1382010-09-01 17:42:29 -07006381 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006382 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6383 // A change in dimension means an auto-centered pivot point changes, too
6384 mMatrixDirty = true;
6385 }
Chet Haase21cd1382010-09-01 17:42:29 -07006386 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006387 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006388 }
Chet Haase55dbb652010-12-21 20:15:08 -08006389 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006390 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006391 }
6392 }
6393
6394 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006395 * The visual x position of this view, in pixels. This is equivalent to the
6396 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006397 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006398 *
Chet Haasedf030d22010-07-30 17:22:38 -07006399 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006400 */
Chet Haasedf030d22010-07-30 17:22:38 -07006401 public float getX() {
6402 return mLeft + mTranslationX;
6403 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006404
Chet Haasedf030d22010-07-30 17:22:38 -07006405 /**
6406 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6407 * {@link #setTranslationX(float) translationX} property to be the difference between
6408 * the x value passed in and the current {@link #getLeft() left} property.
6409 *
6410 * @param x The visual x position of this view, in pixels.
6411 */
6412 public void setX(float x) {
6413 setTranslationX(x - mLeft);
6414 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006415
Chet Haasedf030d22010-07-30 17:22:38 -07006416 /**
6417 * The visual y position of this view, in pixels. This is equivalent to the
6418 * {@link #setTranslationY(float) translationY} property plus the current
6419 * {@link #getTop() top} property.
6420 *
6421 * @return The visual y position of this view, in pixels.
6422 */
6423 public float getY() {
6424 return mTop + mTranslationY;
6425 }
6426
6427 /**
6428 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6429 * {@link #setTranslationY(float) translationY} property to be the difference between
6430 * the y value passed in and the current {@link #getTop() top} property.
6431 *
6432 * @param y The visual y position of this view, in pixels.
6433 */
6434 public void setY(float y) {
6435 setTranslationY(y - mTop);
6436 }
6437
6438
6439 /**
6440 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6441 * This position is post-layout, in addition to wherever the object's
6442 * layout placed it.
6443 *
6444 * @return The horizontal position of this view relative to its left position, in pixels.
6445 */
6446 public float getTranslationX() {
6447 return mTranslationX;
6448 }
6449
6450 /**
6451 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6452 * This effectively positions the object post-layout, in addition to wherever the object's
6453 * layout placed it.
6454 *
6455 * @param translationX The horizontal position of this view relative to its left position,
6456 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006457 *
6458 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006459 */
6460 public void setTranslationX(float translationX) {
6461 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006462 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006463 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006464 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006465 mTranslationX = translationX;
6466 mMatrixDirty = true;
6467 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006468 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006469 }
6470 }
6471
6472 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006473 * The horizontal location of this view relative to its {@link #getTop() top} position.
6474 * This position is post-layout, in addition to wherever the object's
6475 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006476 *
Chet Haasedf030d22010-07-30 17:22:38 -07006477 * @return The vertical position of this view relative to its top position,
6478 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006479 */
Chet Haasedf030d22010-07-30 17:22:38 -07006480 public float getTranslationY() {
6481 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006482 }
6483
6484 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006485 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6486 * This effectively positions the object post-layout, in addition to wherever the object's
6487 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006488 *
Chet Haasedf030d22010-07-30 17:22:38 -07006489 * @param translationY The vertical position of this view relative to its top position,
6490 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006491 *
6492 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006493 */
Chet Haasedf030d22010-07-30 17:22:38 -07006494 public void setTranslationY(float translationY) {
6495 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006496 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006497 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006498 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006499 mTranslationY = translationY;
6500 mMatrixDirty = true;
6501 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006502 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006503 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006504 }
6505
6506 /**
Romain Guyda489792011-02-03 01:05:15 -08006507 * @hide
6508 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006509 public void setFastTranslationX(float x) {
6510 mTranslationX = x;
6511 mMatrixDirty = true;
6512 }
6513
6514 /**
6515 * @hide
6516 */
6517 public void setFastTranslationY(float y) {
6518 mTranslationY = y;
6519 mMatrixDirty = true;
6520 }
6521
6522 /**
6523 * @hide
6524 */
Romain Guyda489792011-02-03 01:05:15 -08006525 public void setFastX(float x) {
6526 mTranslationX = x - mLeft;
6527 mMatrixDirty = true;
6528 }
6529
6530 /**
6531 * @hide
6532 */
6533 public void setFastY(float y) {
6534 mTranslationY = y - mTop;
6535 mMatrixDirty = true;
6536 }
6537
6538 /**
6539 * @hide
6540 */
6541 public void setFastScaleX(float x) {
6542 mScaleX = x;
6543 mMatrixDirty = true;
6544 }
6545
6546 /**
6547 * @hide
6548 */
6549 public void setFastScaleY(float y) {
6550 mScaleY = y;
6551 mMatrixDirty = true;
6552 }
6553
6554 /**
6555 * @hide
6556 */
6557 public void setFastAlpha(float alpha) {
6558 mAlpha = alpha;
6559 }
6560
6561 /**
6562 * @hide
6563 */
6564 public void setFastRotationY(float y) {
6565 mRotationY = y;
6566 mMatrixDirty = true;
6567 }
6568
6569 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006570 * Hit rectangle in parent's coordinates
6571 *
6572 * @param outRect The hit rectangle of the view.
6573 */
6574 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006575 updateMatrix();
6576 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006577 outRect.set(mLeft, mTop, mRight, mBottom);
6578 } else {
6579 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006580 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006581 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006582 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6583 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006584 }
6585 }
6586
6587 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006588 * Determines whether the given point, in local coordinates is inside the view.
6589 */
6590 /*package*/ final boolean pointInView(float localX, float localY) {
6591 return localX >= 0 && localX < (mRight - mLeft)
6592 && localY >= 0 && localY < (mBottom - mTop);
6593 }
6594
6595 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006596 * Utility method to determine whether the given point, in local coordinates,
6597 * is inside the view, where the area of the view is expanded by the slop factor.
6598 * This method is called while processing touch-move events to determine if the event
6599 * is still within the view.
6600 */
6601 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006602 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006603 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 }
6605
6606 /**
6607 * When a view has focus and the user navigates away from it, the next view is searched for
6608 * starting from the rectangle filled in by this method.
6609 *
6610 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6611 * view maintains some idea of internal selection, such as a cursor, or a selected row
6612 * or column, you should override this method and fill in a more specific rectangle.
6613 *
6614 * @param r The rectangle to fill in, in this view's coordinates.
6615 */
6616 public void getFocusedRect(Rect r) {
6617 getDrawingRect(r);
6618 }
6619
6620 /**
6621 * If some part of this view is not clipped by any of its parents, then
6622 * return that area in r in global (root) coordinates. To convert r to local
6623 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6624 * -globalOffset.y)) If the view is completely clipped or translated out,
6625 * return false.
6626 *
6627 * @param r If true is returned, r holds the global coordinates of the
6628 * visible portion of this view.
6629 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6630 * between this view and its root. globalOffet may be null.
6631 * @return true if r is non-empty (i.e. part of the view is visible at the
6632 * root level.
6633 */
6634 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6635 int width = mRight - mLeft;
6636 int height = mBottom - mTop;
6637 if (width > 0 && height > 0) {
6638 r.set(0, 0, width, height);
6639 if (globalOffset != null) {
6640 globalOffset.set(-mScrollX, -mScrollY);
6641 }
6642 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6643 }
6644 return false;
6645 }
6646
6647 public final boolean getGlobalVisibleRect(Rect r) {
6648 return getGlobalVisibleRect(r, null);
6649 }
6650
6651 public final boolean getLocalVisibleRect(Rect r) {
6652 Point offset = new Point();
6653 if (getGlobalVisibleRect(r, offset)) {
6654 r.offset(-offset.x, -offset.y); // make r local
6655 return true;
6656 }
6657 return false;
6658 }
6659
6660 /**
6661 * Offset this view's vertical location by the specified number of pixels.
6662 *
6663 * @param offset the number of pixels to offset the view by
6664 */
6665 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006666 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006667 updateMatrix();
6668 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006669 final ViewParent p = mParent;
6670 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006671 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006672 int minTop;
6673 int maxBottom;
6674 int yLoc;
6675 if (offset < 0) {
6676 minTop = mTop + offset;
6677 maxBottom = mBottom;
6678 yLoc = offset;
6679 } else {
6680 minTop = mTop;
6681 maxBottom = mBottom + offset;
6682 yLoc = 0;
6683 }
6684 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6685 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006686 }
6687 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006688 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006689 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006690
Chet Haasec3aa3612010-06-17 08:50:37 -07006691 mTop += offset;
6692 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006693
Chet Haasec3aa3612010-06-17 08:50:37 -07006694 if (!mMatrixIsIdentity) {
6695 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006696 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006697 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006698 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006699 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006700 }
6701
6702 /**
6703 * Offset this view's horizontal location by the specified amount of pixels.
6704 *
6705 * @param offset the numer of pixels to offset the view by
6706 */
6707 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006708 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006709 updateMatrix();
6710 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006711 final ViewParent p = mParent;
6712 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006713 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006714 int minLeft;
6715 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006716 if (offset < 0) {
6717 minLeft = mLeft + offset;
6718 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006719 } else {
6720 minLeft = mLeft;
6721 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006722 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006723 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006724 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006725 }
6726 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006727 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006728 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006729
Chet Haasec3aa3612010-06-17 08:50:37 -07006730 mLeft += offset;
6731 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006732
Chet Haasec3aa3612010-06-17 08:50:37 -07006733 if (!mMatrixIsIdentity) {
6734 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006735 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006736 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006737 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006738 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006739 }
6740
6741 /**
6742 * Get the LayoutParams associated with this view. All views should have
6743 * layout parameters. These supply parameters to the <i>parent</i> of this
6744 * view specifying how it should be arranged. There are many subclasses of
6745 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6746 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006747 *
6748 * This method may return null if this View is not attached to a parent
6749 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6750 * was not invoked successfully. When a View is attached to a parent
6751 * ViewGroup, this method must not return null.
6752 *
6753 * @return The LayoutParams associated with this view, or null if no
6754 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006755 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006756 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 public ViewGroup.LayoutParams getLayoutParams() {
6758 return mLayoutParams;
6759 }
6760
6761 /**
6762 * Set the layout parameters associated with this view. These supply
6763 * parameters to the <i>parent</i> of this view specifying how it should be
6764 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6765 * correspond to the different subclasses of ViewGroup that are responsible
6766 * for arranging their children.
6767 *
Romain Guy01c174b2011-02-22 11:51:06 -08006768 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006769 */
6770 public void setLayoutParams(ViewGroup.LayoutParams params) {
6771 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08006772 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006773 }
6774 mLayoutParams = params;
6775 requestLayout();
6776 }
6777
6778 /**
6779 * Set the scrolled position of your view. This will cause a call to
6780 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6781 * invalidated.
6782 * @param x the x position to scroll to
6783 * @param y the y position to scroll to
6784 */
6785 public void scrollTo(int x, int y) {
6786 if (mScrollX != x || mScrollY != y) {
6787 int oldX = mScrollX;
6788 int oldY = mScrollY;
6789 mScrollX = x;
6790 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006791 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006792 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006793 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006794 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006795 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006796 }
6797 }
6798
6799 /**
6800 * Move the scrolled position of your view. This will cause a call to
6801 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6802 * invalidated.
6803 * @param x the amount of pixels to scroll by horizontally
6804 * @param y the amount of pixels to scroll by vertically
6805 */
6806 public void scrollBy(int x, int y) {
6807 scrollTo(mScrollX + x, mScrollY + y);
6808 }
6809
6810 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006811 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6812 * animation to fade the scrollbars out after a default delay. If a subclass
6813 * provides animated scrolling, the start delay should equal the duration
6814 * of the scrolling animation.</p>
6815 *
6816 * <p>The animation starts only if at least one of the scrollbars is
6817 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6818 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6819 * this method returns true, and false otherwise. If the animation is
6820 * started, this method calls {@link #invalidate()}; in that case the
6821 * caller should not call {@link #invalidate()}.</p>
6822 *
6823 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006824 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006825 *
6826 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6827 * and {@link #scrollTo(int, int)}.</p>
6828 *
6829 * @return true if the animation is played, false otherwise
6830 *
6831 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006832 * @see #scrollBy(int, int)
6833 * @see #scrollTo(int, int)
6834 * @see #isHorizontalScrollBarEnabled()
6835 * @see #isVerticalScrollBarEnabled()
6836 * @see #setHorizontalScrollBarEnabled(boolean)
6837 * @see #setVerticalScrollBarEnabled(boolean)
6838 */
6839 protected boolean awakenScrollBars() {
6840 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006841 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006842 }
6843
6844 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006845 * Trigger the scrollbars to draw.
6846 * This method differs from awakenScrollBars() only in its default duration.
6847 * initialAwakenScrollBars() will show the scroll bars for longer than
6848 * usual to give the user more of a chance to notice them.
6849 *
6850 * @return true if the animation is played, false otherwise.
6851 */
6852 private boolean initialAwakenScrollBars() {
6853 return mScrollCache != null &&
6854 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6855 }
6856
6857 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006858 * <p>
6859 * Trigger the scrollbars to draw. When invoked this method starts an
6860 * animation to fade the scrollbars out after a fixed delay. If a subclass
6861 * provides animated scrolling, the start delay should equal the duration of
6862 * the scrolling animation.
6863 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006864 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006865 * <p>
6866 * The animation starts only if at least one of the scrollbars is enabled,
6867 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6868 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6869 * this method returns true, and false otherwise. If the animation is
6870 * started, this method calls {@link #invalidate()}; in that case the caller
6871 * should not call {@link #invalidate()}.
6872 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006873 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006874 * <p>
6875 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006876 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006877 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006878 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006879 * @param startDelay the delay, in milliseconds, after which the animation
6880 * should start; when the delay is 0, the animation starts
6881 * immediately
6882 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006883 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006884 * @see #scrollBy(int, int)
6885 * @see #scrollTo(int, int)
6886 * @see #isHorizontalScrollBarEnabled()
6887 * @see #isVerticalScrollBarEnabled()
6888 * @see #setHorizontalScrollBarEnabled(boolean)
6889 * @see #setVerticalScrollBarEnabled(boolean)
6890 */
6891 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006892 return awakenScrollBars(startDelay, true);
6893 }
Joe Malin32736f02011-01-19 16:14:20 -08006894
Mike Cleron290947b2009-09-29 18:34:32 -07006895 /**
6896 * <p>
6897 * Trigger the scrollbars to draw. When invoked this method starts an
6898 * animation to fade the scrollbars out after a fixed delay. If a subclass
6899 * provides animated scrolling, the start delay should equal the duration of
6900 * the scrolling animation.
6901 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006902 *
Mike Cleron290947b2009-09-29 18:34:32 -07006903 * <p>
6904 * The animation starts only if at least one of the scrollbars is enabled,
6905 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6906 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6907 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006908 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006909 * is set to true; in that case the caller
6910 * should not call {@link #invalidate()}.
6911 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006912 *
Mike Cleron290947b2009-09-29 18:34:32 -07006913 * <p>
6914 * This method should be invoked everytime a subclass directly updates the
6915 * scroll parameters.
6916 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006917 *
Mike Cleron290947b2009-09-29 18:34:32 -07006918 * @param startDelay the delay, in milliseconds, after which the animation
6919 * should start; when the delay is 0, the animation starts
6920 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08006921 *
Mike Cleron290947b2009-09-29 18:34:32 -07006922 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08006923 *
Mike Cleron290947b2009-09-29 18:34:32 -07006924 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006925 *
Mike Cleron290947b2009-09-29 18:34:32 -07006926 * @see #scrollBy(int, int)
6927 * @see #scrollTo(int, int)
6928 * @see #isHorizontalScrollBarEnabled()
6929 * @see #isVerticalScrollBarEnabled()
6930 * @see #setHorizontalScrollBarEnabled(boolean)
6931 * @see #setVerticalScrollBarEnabled(boolean)
6932 */
6933 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006934 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08006935
Mike Cleronf116bf82009-09-27 19:14:12 -07006936 if (scrollCache == null || !scrollCache.fadeScrollBars) {
6937 return false;
6938 }
6939
6940 if (scrollCache.scrollBar == null) {
6941 scrollCache.scrollBar = new ScrollBarDrawable();
6942 }
6943
6944 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
6945
Mike Cleron290947b2009-09-29 18:34:32 -07006946 if (invalidate) {
6947 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08006948 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07006949 }
Mike Cleronf116bf82009-09-27 19:14:12 -07006950
6951 if (scrollCache.state == ScrollabilityCache.OFF) {
6952 // FIXME: this is copied from WindowManagerService.
6953 // We should get this value from the system when it
6954 // is possible to do so.
6955 final int KEY_REPEAT_FIRST_DELAY = 750;
6956 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
6957 }
6958
6959 // Tell mScrollCache when we should start fading. This may
6960 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07006961 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07006962 scrollCache.fadeStartTime = fadeStartTime;
6963 scrollCache.state = ScrollabilityCache.ON;
6964
6965 // Schedule our fader to run, unscheduling any old ones first
6966 if (mAttachInfo != null) {
6967 mAttachInfo.mHandler.removeCallbacks(scrollCache);
6968 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
6969 }
6970
6971 return true;
6972 }
6973
6974 return false;
6975 }
6976
6977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006978 * Mark the the area defined by dirty as needing to be drawn. If the view is
6979 * visible, {@link #onDraw} will be called at some point in the future.
6980 * This must be called from a UI thread. To call from a non-UI thread, call
6981 * {@link #postInvalidate()}.
6982 *
6983 * WARNING: This method is destructive to dirty.
6984 * @param dirty the rectangle representing the bounds of the dirty region
6985 */
6986 public void invalidate(Rect dirty) {
6987 if (ViewDebug.TRACE_HIERARCHY) {
6988 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6989 }
6990
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006991 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006992 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6993 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006994 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006995 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006996 final ViewParent p = mParent;
6997 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006998 //noinspection PointlessBooleanExpression,ConstantConditions
6999 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7000 if (p != null && ai != null && ai.mHardwareAccelerated) {
7001 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7002 // with a null dirty rect, which tells the ViewRoot to redraw everything
7003 p.invalidateChild(this, null);
7004 return;
7005 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007007 if (p != null && ai != null) {
7008 final int scrollX = mScrollX;
7009 final int scrollY = mScrollY;
7010 final Rect r = ai.mTmpInvalRect;
7011 r.set(dirty.left - scrollX, dirty.top - scrollY,
7012 dirty.right - scrollX, dirty.bottom - scrollY);
7013 mParent.invalidateChild(this, r);
7014 }
7015 }
7016 }
7017
7018 /**
7019 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7020 * The coordinates of the dirty rect are relative to the view.
7021 * If the view is visible, {@link #onDraw} will be called at some point
7022 * in the future. This must be called from a UI thread. To call
7023 * from a non-UI thread, call {@link #postInvalidate()}.
7024 * @param l the left position of the dirty region
7025 * @param t the top position of the dirty region
7026 * @param r the right position of the dirty region
7027 * @param b the bottom position of the dirty region
7028 */
7029 public void invalidate(int l, int t, int r, int b) {
7030 if (ViewDebug.TRACE_HIERARCHY) {
7031 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7032 }
7033
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007034 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007035 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7036 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007037 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007038 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007039 final ViewParent p = mParent;
7040 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007041 //noinspection PointlessBooleanExpression,ConstantConditions
7042 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7043 if (p != null && ai != null && ai.mHardwareAccelerated) {
7044 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7045 // with a null dirty rect, which tells the ViewRoot to redraw everything
7046 p.invalidateChild(this, null);
7047 return;
7048 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007049 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007050 if (p != null && ai != null && l < r && t < b) {
7051 final int scrollX = mScrollX;
7052 final int scrollY = mScrollY;
7053 final Rect tmpr = ai.mTmpInvalRect;
7054 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7055 p.invalidateChild(this, tmpr);
7056 }
7057 }
7058 }
7059
7060 /**
7061 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7062 * be called at some point in the future. This must be called from a
7063 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7064 */
7065 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007066 invalidate(true);
7067 }
Joe Malin32736f02011-01-19 16:14:20 -08007068
Chet Haaseed032702010-10-01 14:05:54 -07007069 /**
7070 * This is where the invalidate() work actually happens. A full invalidate()
7071 * causes the drawing cache to be invalidated, but this function can be called with
7072 * invalidateCache set to false to skip that invalidation step for cases that do not
7073 * need it (for example, a component that remains at the same dimensions with the same
7074 * content).
7075 *
7076 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7077 * well. This is usually true for a full invalidate, but may be set to false if the
7078 * View's contents or dimensions have not changed.
7079 */
Romain Guy849d0a32011-02-01 17:20:48 -08007080 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007081 if (ViewDebug.TRACE_HIERARCHY) {
7082 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7083 }
7084
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007085 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007086 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007087 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7088 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007089 mPrivateFlags &= ~DRAWN;
7090 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007091 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007092 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007094 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007095 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007096 //noinspection PointlessBooleanExpression,ConstantConditions
7097 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7098 if (p != null && ai != null && ai.mHardwareAccelerated) {
7099 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7100 // with a null dirty rect, which tells the ViewRoot to redraw everything
7101 p.invalidateChild(this, null);
7102 return;
7103 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007104 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007105
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007106 if (p != null && ai != null) {
7107 final Rect r = ai.mTmpInvalRect;
7108 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7109 // Don't call invalidate -- we don't want to internally scroll
7110 // our own bounds
7111 p.invalidateChild(this, r);
7112 }
7113 }
7114 }
7115
7116 /**
Romain Guyda489792011-02-03 01:05:15 -08007117 * @hide
7118 */
7119 public void fastInvalidate() {
7120 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7121 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7122 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7123 if (mParent instanceof View) {
7124 ((View) mParent).mPrivateFlags |= INVALIDATED;
7125 }
7126 mPrivateFlags &= ~DRAWN;
7127 mPrivateFlags |= INVALIDATED;
7128 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7129 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7130 mParent.invalidateChild(this, null);
7131 }
7132 }
7133 }
7134
7135 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007136 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007137 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7138 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007139 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7140 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007141 *
7142 * @hide
7143 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007144 protected void invalidateParentCaches() {
7145 if (mParent instanceof View) {
7146 ((View) mParent).mPrivateFlags |= INVALIDATED;
7147 }
7148 }
Joe Malin32736f02011-01-19 16:14:20 -08007149
Romain Guy0fd89bf2011-01-26 15:41:30 -08007150 /**
7151 * Used to indicate that the parent of this view should be invalidated. This functionality
7152 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7153 * which is necessary when various parent-managed properties of the view change, such as
7154 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7155 * an invalidation event to the parent.
7156 *
7157 * @hide
7158 */
7159 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007160 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007161 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007162 }
7163 }
7164
7165 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007166 * Indicates whether this View is opaque. An opaque View guarantees that it will
7167 * draw all the pixels overlapping its bounds using a fully opaque color.
7168 *
7169 * Subclasses of View should override this method whenever possible to indicate
7170 * whether an instance is opaque. Opaque Views are treated in a special way by
7171 * the View hierarchy, possibly allowing it to perform optimizations during
7172 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007173 *
Romain Guy24443ea2009-05-11 11:56:30 -07007174 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007175 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007176 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007177 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007178 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7179 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007180 }
7181
Adam Powell20232d02010-12-08 21:08:53 -08007182 /**
7183 * @hide
7184 */
7185 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007186 // Opaque if:
7187 // - Has a background
7188 // - Background is opaque
7189 // - Doesn't have scrollbars or scrollbars are inside overlay
7190
7191 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7192 mPrivateFlags |= OPAQUE_BACKGROUND;
7193 } else {
7194 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7195 }
7196
7197 final int flags = mViewFlags;
7198 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7199 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7200 mPrivateFlags |= OPAQUE_SCROLLBARS;
7201 } else {
7202 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7203 }
7204 }
7205
7206 /**
7207 * @hide
7208 */
7209 protected boolean hasOpaqueScrollbars() {
7210 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007211 }
7212
7213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007214 * @return A handler associated with the thread running the View. This
7215 * handler can be used to pump events in the UI events queue.
7216 */
7217 public Handler getHandler() {
7218 if (mAttachInfo != null) {
7219 return mAttachInfo.mHandler;
7220 }
7221 return null;
7222 }
7223
7224 /**
7225 * Causes the Runnable to be added to the message queue.
7226 * The runnable will be run on the user interface thread.
7227 *
7228 * @param action The Runnable that will be executed.
7229 *
7230 * @return Returns true if the Runnable was successfully placed in to the
7231 * message queue. Returns false on failure, usually because the
7232 * looper processing the message queue is exiting.
7233 */
7234 public boolean post(Runnable action) {
7235 Handler handler;
7236 if (mAttachInfo != null) {
7237 handler = mAttachInfo.mHandler;
7238 } else {
7239 // Assume that post will succeed later
7240 ViewRoot.getRunQueue().post(action);
7241 return true;
7242 }
7243
7244 return handler.post(action);
7245 }
7246
7247 /**
7248 * Causes the Runnable to be added to the message queue, to be run
7249 * after the specified amount of time elapses.
7250 * The runnable will be run on the user interface thread.
7251 *
7252 * @param action The Runnable that will be executed.
7253 * @param delayMillis The delay (in milliseconds) until the Runnable
7254 * will be executed.
7255 *
7256 * @return true if the Runnable was successfully placed in to the
7257 * message queue. Returns false on failure, usually because the
7258 * looper processing the message queue is exiting. Note that a
7259 * result of true does not mean the Runnable will be processed --
7260 * if the looper is quit before the delivery time of the message
7261 * occurs then the message will be dropped.
7262 */
7263 public boolean postDelayed(Runnable action, long delayMillis) {
7264 Handler handler;
7265 if (mAttachInfo != null) {
7266 handler = mAttachInfo.mHandler;
7267 } else {
7268 // Assume that post will succeed later
7269 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7270 return true;
7271 }
7272
7273 return handler.postDelayed(action, delayMillis);
7274 }
7275
7276 /**
7277 * Removes the specified Runnable from the message queue.
7278 *
7279 * @param action The Runnable to remove from the message handling queue
7280 *
7281 * @return true if this view could ask the Handler to remove the Runnable,
7282 * false otherwise. When the returned value is true, the Runnable
7283 * may or may not have been actually removed from the message queue
7284 * (for instance, if the Runnable was not in the queue already.)
7285 */
7286 public boolean removeCallbacks(Runnable action) {
7287 Handler handler;
7288 if (mAttachInfo != null) {
7289 handler = mAttachInfo.mHandler;
7290 } else {
7291 // Assume that post will succeed later
7292 ViewRoot.getRunQueue().removeCallbacks(action);
7293 return true;
7294 }
7295
7296 handler.removeCallbacks(action);
7297 return true;
7298 }
7299
7300 /**
7301 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7302 * Use this to invalidate the View from a non-UI thread.
7303 *
7304 * @see #invalidate()
7305 */
7306 public void postInvalidate() {
7307 postInvalidateDelayed(0);
7308 }
7309
7310 /**
7311 * Cause an invalidate of the specified area to happen on a subsequent cycle
7312 * through the event loop. Use this to invalidate the View from a non-UI thread.
7313 *
7314 * @param left The left coordinate of the rectangle to invalidate.
7315 * @param top The top coordinate of the rectangle to invalidate.
7316 * @param right The right coordinate of the rectangle to invalidate.
7317 * @param bottom The bottom coordinate of the rectangle to invalidate.
7318 *
7319 * @see #invalidate(int, int, int, int)
7320 * @see #invalidate(Rect)
7321 */
7322 public void postInvalidate(int left, int top, int right, int bottom) {
7323 postInvalidateDelayed(0, left, top, right, bottom);
7324 }
7325
7326 /**
7327 * Cause an invalidate to happen on a subsequent cycle through the event
7328 * loop. Waits for the specified amount of time.
7329 *
7330 * @param delayMilliseconds the duration in milliseconds to delay the
7331 * invalidation by
7332 */
7333 public void postInvalidateDelayed(long delayMilliseconds) {
7334 // We try only with the AttachInfo because there's no point in invalidating
7335 // if we are not attached to our window
7336 if (mAttachInfo != null) {
7337 Message msg = Message.obtain();
7338 msg.what = AttachInfo.INVALIDATE_MSG;
7339 msg.obj = this;
7340 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7341 }
7342 }
7343
7344 /**
7345 * Cause an invalidate of the specified area to happen on a subsequent cycle
7346 * through the event loop. Waits for the specified amount of time.
7347 *
7348 * @param delayMilliseconds the duration in milliseconds to delay the
7349 * invalidation by
7350 * @param left The left coordinate of the rectangle to invalidate.
7351 * @param top The top coordinate of the rectangle to invalidate.
7352 * @param right The right coordinate of the rectangle to invalidate.
7353 * @param bottom The bottom coordinate of the rectangle to invalidate.
7354 */
7355 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7356 int right, int bottom) {
7357
7358 // We try only with the AttachInfo because there's no point in invalidating
7359 // if we are not attached to our window
7360 if (mAttachInfo != null) {
7361 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7362 info.target = this;
7363 info.left = left;
7364 info.top = top;
7365 info.right = right;
7366 info.bottom = bottom;
7367
7368 final Message msg = Message.obtain();
7369 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7370 msg.obj = info;
7371 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7372 }
7373 }
7374
7375 /**
7376 * Called by a parent to request that a child update its values for mScrollX
7377 * and mScrollY if necessary. This will typically be done if the child is
7378 * animating a scroll using a {@link android.widget.Scroller Scroller}
7379 * object.
7380 */
7381 public void computeScroll() {
7382 }
7383
7384 /**
7385 * <p>Indicate whether the horizontal edges are faded when the view is
7386 * scrolled horizontally.</p>
7387 *
7388 * @return true if the horizontal edges should are faded on scroll, false
7389 * otherwise
7390 *
7391 * @see #setHorizontalFadingEdgeEnabled(boolean)
7392 * @attr ref android.R.styleable#View_fadingEdge
7393 */
7394 public boolean isHorizontalFadingEdgeEnabled() {
7395 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7396 }
7397
7398 /**
7399 * <p>Define whether the horizontal edges should be faded when this view
7400 * is scrolled horizontally.</p>
7401 *
7402 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7403 * be faded when the view is scrolled
7404 * horizontally
7405 *
7406 * @see #isHorizontalFadingEdgeEnabled()
7407 * @attr ref android.R.styleable#View_fadingEdge
7408 */
7409 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7410 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7411 if (horizontalFadingEdgeEnabled) {
7412 initScrollCache();
7413 }
7414
7415 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7416 }
7417 }
7418
7419 /**
7420 * <p>Indicate whether the vertical edges are faded when the view is
7421 * scrolled horizontally.</p>
7422 *
7423 * @return true if the vertical edges should are faded on scroll, false
7424 * otherwise
7425 *
7426 * @see #setVerticalFadingEdgeEnabled(boolean)
7427 * @attr ref android.R.styleable#View_fadingEdge
7428 */
7429 public boolean isVerticalFadingEdgeEnabled() {
7430 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7431 }
7432
7433 /**
7434 * <p>Define whether the vertical edges should be faded when this view
7435 * is scrolled vertically.</p>
7436 *
7437 * @param verticalFadingEdgeEnabled true if the vertical edges should
7438 * be faded when the view is scrolled
7439 * vertically
7440 *
7441 * @see #isVerticalFadingEdgeEnabled()
7442 * @attr ref android.R.styleable#View_fadingEdge
7443 */
7444 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7445 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7446 if (verticalFadingEdgeEnabled) {
7447 initScrollCache();
7448 }
7449
7450 mViewFlags ^= FADING_EDGE_VERTICAL;
7451 }
7452 }
7453
7454 /**
7455 * Returns the strength, or intensity, of the top faded edge. The strength is
7456 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7457 * returns 0.0 or 1.0 but no value in between.
7458 *
7459 * Subclasses should override this method to provide a smoother fade transition
7460 * when scrolling occurs.
7461 *
7462 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7463 */
7464 protected float getTopFadingEdgeStrength() {
7465 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7466 }
7467
7468 /**
7469 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7470 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7471 * returns 0.0 or 1.0 but no value in between.
7472 *
7473 * Subclasses should override this method to provide a smoother fade transition
7474 * when scrolling occurs.
7475 *
7476 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7477 */
7478 protected float getBottomFadingEdgeStrength() {
7479 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7480 computeVerticalScrollRange() ? 1.0f : 0.0f;
7481 }
7482
7483 /**
7484 * Returns the strength, or intensity, of the left faded edge. The strength is
7485 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7486 * returns 0.0 or 1.0 but no value in between.
7487 *
7488 * Subclasses should override this method to provide a smoother fade transition
7489 * when scrolling occurs.
7490 *
7491 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7492 */
7493 protected float getLeftFadingEdgeStrength() {
7494 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7495 }
7496
7497 /**
7498 * Returns the strength, or intensity, of the right faded edge. The strength is
7499 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7500 * returns 0.0 or 1.0 but no value in between.
7501 *
7502 * Subclasses should override this method to provide a smoother fade transition
7503 * when scrolling occurs.
7504 *
7505 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7506 */
7507 protected float getRightFadingEdgeStrength() {
7508 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7509 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7510 }
7511
7512 /**
7513 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7514 * scrollbar is not drawn by default.</p>
7515 *
7516 * @return true if the horizontal scrollbar should be painted, false
7517 * otherwise
7518 *
7519 * @see #setHorizontalScrollBarEnabled(boolean)
7520 */
7521 public boolean isHorizontalScrollBarEnabled() {
7522 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7523 }
7524
7525 /**
7526 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7527 * scrollbar is not drawn by default.</p>
7528 *
7529 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7530 * be painted
7531 *
7532 * @see #isHorizontalScrollBarEnabled()
7533 */
7534 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7535 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7536 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007537 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007538 recomputePadding();
7539 }
7540 }
7541
7542 /**
7543 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7544 * scrollbar is not drawn by default.</p>
7545 *
7546 * @return true if the vertical scrollbar should be painted, false
7547 * otherwise
7548 *
7549 * @see #setVerticalScrollBarEnabled(boolean)
7550 */
7551 public boolean isVerticalScrollBarEnabled() {
7552 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7553 }
7554
7555 /**
7556 * <p>Define whether the vertical scrollbar should be drawn or not. The
7557 * scrollbar is not drawn by default.</p>
7558 *
7559 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7560 * be painted
7561 *
7562 * @see #isVerticalScrollBarEnabled()
7563 */
7564 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7565 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7566 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007567 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007568 recomputePadding();
7569 }
7570 }
7571
Adam Powell20232d02010-12-08 21:08:53 -08007572 /**
7573 * @hide
7574 */
7575 protected void recomputePadding() {
7576 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 }
Joe Malin32736f02011-01-19 16:14:20 -08007578
Mike Cleronfe81d382009-09-28 14:22:16 -07007579 /**
7580 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007581 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007582 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007583 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007584 */
7585 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7586 initScrollCache();
7587 final ScrollabilityCache scrollabilityCache = mScrollCache;
7588 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007589 if (fadeScrollbars) {
7590 scrollabilityCache.state = ScrollabilityCache.OFF;
7591 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007592 scrollabilityCache.state = ScrollabilityCache.ON;
7593 }
7594 }
Joe Malin32736f02011-01-19 16:14:20 -08007595
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007596 /**
Joe Malin32736f02011-01-19 16:14:20 -08007597 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007598 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007599 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007600 * @return true if scrollbar fading is enabled
7601 */
7602 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007603 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007604 }
Joe Malin32736f02011-01-19 16:14:20 -08007605
Mike Cleron52f0a642009-09-28 18:21:37 -07007606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7608 * inset. When inset, they add to the padding of the view. And the scrollbars
7609 * can be drawn inside the padding area or on the edge of the view. For example,
7610 * if a view has a background drawable and you want to draw the scrollbars
7611 * inside the padding specified by the drawable, you can use
7612 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7613 * appear at the edge of the view, ignoring the padding, then you can use
7614 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7615 * @param style the style of the scrollbars. Should be one of
7616 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7617 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7618 * @see #SCROLLBARS_INSIDE_OVERLAY
7619 * @see #SCROLLBARS_INSIDE_INSET
7620 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7621 * @see #SCROLLBARS_OUTSIDE_INSET
7622 */
7623 public void setScrollBarStyle(int style) {
7624 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7625 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007626 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007627 recomputePadding();
7628 }
7629 }
7630
7631 /**
7632 * <p>Returns the current scrollbar style.</p>
7633 * @return the current scrollbar style
7634 * @see #SCROLLBARS_INSIDE_OVERLAY
7635 * @see #SCROLLBARS_INSIDE_INSET
7636 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7637 * @see #SCROLLBARS_OUTSIDE_INSET
7638 */
7639 public int getScrollBarStyle() {
7640 return mViewFlags & SCROLLBARS_STYLE_MASK;
7641 }
7642
7643 /**
7644 * <p>Compute the horizontal range that the horizontal scrollbar
7645 * represents.</p>
7646 *
7647 * <p>The range is expressed in arbitrary units that must be the same as the
7648 * units used by {@link #computeHorizontalScrollExtent()} and
7649 * {@link #computeHorizontalScrollOffset()}.</p>
7650 *
7651 * <p>The default range is the drawing width of this view.</p>
7652 *
7653 * @return the total horizontal range represented by the horizontal
7654 * scrollbar
7655 *
7656 * @see #computeHorizontalScrollExtent()
7657 * @see #computeHorizontalScrollOffset()
7658 * @see android.widget.ScrollBarDrawable
7659 */
7660 protected int computeHorizontalScrollRange() {
7661 return getWidth();
7662 }
7663
7664 /**
7665 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7666 * within the horizontal range. This value is used to compute the position
7667 * of the thumb within the scrollbar's track.</p>
7668 *
7669 * <p>The range is expressed in arbitrary units that must be the same as the
7670 * units used by {@link #computeHorizontalScrollRange()} and
7671 * {@link #computeHorizontalScrollExtent()}.</p>
7672 *
7673 * <p>The default offset is the scroll offset of this view.</p>
7674 *
7675 * @return the horizontal offset of the scrollbar's thumb
7676 *
7677 * @see #computeHorizontalScrollRange()
7678 * @see #computeHorizontalScrollExtent()
7679 * @see android.widget.ScrollBarDrawable
7680 */
7681 protected int computeHorizontalScrollOffset() {
7682 return mScrollX;
7683 }
7684
7685 /**
7686 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7687 * within the horizontal range. This value is used to compute the length
7688 * of the thumb within the scrollbar's track.</p>
7689 *
7690 * <p>The range is expressed in arbitrary units that must be the same as the
7691 * units used by {@link #computeHorizontalScrollRange()} and
7692 * {@link #computeHorizontalScrollOffset()}.</p>
7693 *
7694 * <p>The default extent is the drawing width of this view.</p>
7695 *
7696 * @return the horizontal extent of the scrollbar's thumb
7697 *
7698 * @see #computeHorizontalScrollRange()
7699 * @see #computeHorizontalScrollOffset()
7700 * @see android.widget.ScrollBarDrawable
7701 */
7702 protected int computeHorizontalScrollExtent() {
7703 return getWidth();
7704 }
7705
7706 /**
7707 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7708 *
7709 * <p>The range is expressed in arbitrary units that must be the same as the
7710 * units used by {@link #computeVerticalScrollExtent()} and
7711 * {@link #computeVerticalScrollOffset()}.</p>
7712 *
7713 * @return the total vertical range represented by the vertical scrollbar
7714 *
7715 * <p>The default range is the drawing height of this view.</p>
7716 *
7717 * @see #computeVerticalScrollExtent()
7718 * @see #computeVerticalScrollOffset()
7719 * @see android.widget.ScrollBarDrawable
7720 */
7721 protected int computeVerticalScrollRange() {
7722 return getHeight();
7723 }
7724
7725 /**
7726 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7727 * within the horizontal range. This value is used to compute the position
7728 * of the thumb within the scrollbar's track.</p>
7729 *
7730 * <p>The range is expressed in arbitrary units that must be the same as the
7731 * units used by {@link #computeVerticalScrollRange()} and
7732 * {@link #computeVerticalScrollExtent()}.</p>
7733 *
7734 * <p>The default offset is the scroll offset of this view.</p>
7735 *
7736 * @return the vertical offset of the scrollbar's thumb
7737 *
7738 * @see #computeVerticalScrollRange()
7739 * @see #computeVerticalScrollExtent()
7740 * @see android.widget.ScrollBarDrawable
7741 */
7742 protected int computeVerticalScrollOffset() {
7743 return mScrollY;
7744 }
7745
7746 /**
7747 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7748 * within the vertical range. This value is used to compute the length
7749 * of the thumb within the scrollbar's track.</p>
7750 *
7751 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007752 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007753 * {@link #computeVerticalScrollOffset()}.</p>
7754 *
7755 * <p>The default extent is the drawing height of this view.</p>
7756 *
7757 * @return the vertical extent of the scrollbar's thumb
7758 *
7759 * @see #computeVerticalScrollRange()
7760 * @see #computeVerticalScrollOffset()
7761 * @see android.widget.ScrollBarDrawable
7762 */
7763 protected int computeVerticalScrollExtent() {
7764 return getHeight();
7765 }
7766
7767 /**
7768 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7769 * scrollbars are painted only if they have been awakened first.</p>
7770 *
7771 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007772 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007773 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007775 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007776 // scrollbars are drawn only when the animation is running
7777 final ScrollabilityCache cache = mScrollCache;
7778 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007779
Mike Cleronf116bf82009-09-27 19:14:12 -07007780 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007781
Mike Cleronf116bf82009-09-27 19:14:12 -07007782 if (state == ScrollabilityCache.OFF) {
7783 return;
7784 }
Joe Malin32736f02011-01-19 16:14:20 -08007785
Mike Cleronf116bf82009-09-27 19:14:12 -07007786 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007787
Mike Cleronf116bf82009-09-27 19:14:12 -07007788 if (state == ScrollabilityCache.FADING) {
7789 // We're fading -- get our fade interpolation
7790 if (cache.interpolatorValues == null) {
7791 cache.interpolatorValues = new float[1];
7792 }
Joe Malin32736f02011-01-19 16:14:20 -08007793
Mike Cleronf116bf82009-09-27 19:14:12 -07007794 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007795
Mike Cleronf116bf82009-09-27 19:14:12 -07007796 // Stops the animation if we're done
7797 if (cache.scrollBarInterpolator.timeToValues(values) ==
7798 Interpolator.Result.FREEZE_END) {
7799 cache.state = ScrollabilityCache.OFF;
7800 } else {
7801 cache.scrollBar.setAlpha(Math.round(values[0]));
7802 }
Joe Malin32736f02011-01-19 16:14:20 -08007803
7804 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007805 // drawing. We only want this when we're fading so that
7806 // we prevent excessive redraws
7807 invalidate = true;
7808 } else {
7809 // We're just on -- but we may have been fading before so
7810 // reset alpha
7811 cache.scrollBar.setAlpha(255);
7812 }
7813
Joe Malin32736f02011-01-19 16:14:20 -08007814
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007815 final int viewFlags = mViewFlags;
7816
7817 final boolean drawHorizontalScrollBar =
7818 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7819 final boolean drawVerticalScrollBar =
7820 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7821 && !isVerticalScrollBarHidden();
7822
7823 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7824 final int width = mRight - mLeft;
7825 final int height = mBottom - mTop;
7826
7827 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007828
Mike Reede8853fc2009-09-04 14:01:48 -04007829 final int scrollX = mScrollX;
7830 final int scrollY = mScrollY;
7831 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7832
Mike Cleronf116bf82009-09-27 19:14:12 -07007833 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007835 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007836 int size = scrollBar.getSize(false);
7837 if (size <= 0) {
7838 size = cache.scrollBarSize;
7839 }
7840
Mike Cleronf116bf82009-09-27 19:14:12 -07007841 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007842 computeHorizontalScrollOffset(),
7843 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007844 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007845 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007846 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007847 left = scrollX + (mPaddingLeft & inside);
7848 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7849 bottom = top + size;
7850 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7851 if (invalidate) {
7852 invalidate(left, top, right, bottom);
7853 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007854 }
7855
7856 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007857 int size = scrollBar.getSize(true);
7858 if (size <= 0) {
7859 size = cache.scrollBarSize;
7860 }
7861
Mike Reede8853fc2009-09-04 14:01:48 -04007862 scrollBar.setParameters(computeVerticalScrollRange(),
7863 computeVerticalScrollOffset(),
7864 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007865 switch (mVerticalScrollbarPosition) {
7866 default:
7867 case SCROLLBAR_POSITION_DEFAULT:
7868 case SCROLLBAR_POSITION_RIGHT:
7869 left = scrollX + width - size - (mUserPaddingRight & inside);
7870 break;
7871 case SCROLLBAR_POSITION_LEFT:
7872 left = scrollX + (mUserPaddingLeft & inside);
7873 break;
7874 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007875 top = scrollY + (mPaddingTop & inside);
7876 right = left + size;
7877 bottom = scrollY + height - (mUserPaddingBottom & inside);
7878 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7879 if (invalidate) {
7880 invalidate(left, top, right, bottom);
7881 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 }
7883 }
7884 }
7885 }
Romain Guy8506ab42009-06-11 17:35:47 -07007886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007887 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007888 * 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 -08007889 * FastScroller is visible.
7890 * @return whether to temporarily hide the vertical scrollbar
7891 * @hide
7892 */
7893 protected boolean isVerticalScrollBarHidden() {
7894 return false;
7895 }
7896
7897 /**
7898 * <p>Draw the horizontal scrollbar if
7899 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 * @param canvas the canvas on which to draw the scrollbar
7902 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007903 *
7904 * @see #isHorizontalScrollBarEnabled()
7905 * @see #computeHorizontalScrollRange()
7906 * @see #computeHorizontalScrollExtent()
7907 * @see #computeHorizontalScrollOffset()
7908 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07007909 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007910 */
Romain Guy8fb95422010-08-17 18:38:51 -07007911 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
7912 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007913 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007914 scrollBar.draw(canvas);
7915 }
Mike Reede8853fc2009-09-04 14:01:48 -04007916
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007918 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
7919 * returns true.</p>
7920 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007921 * @param canvas the canvas on which to draw the scrollbar
7922 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007923 *
7924 * @see #isVerticalScrollBarEnabled()
7925 * @see #computeVerticalScrollRange()
7926 * @see #computeVerticalScrollExtent()
7927 * @see #computeVerticalScrollOffset()
7928 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04007929 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007930 */
Romain Guy8fb95422010-08-17 18:38:51 -07007931 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
7932 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04007933 scrollBar.setBounds(l, t, r, b);
7934 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007935 }
7936
7937 /**
7938 * Implement this to do your drawing.
7939 *
7940 * @param canvas the canvas on which the background will be drawn
7941 */
7942 protected void onDraw(Canvas canvas) {
7943 }
7944
7945 /*
7946 * Caller is responsible for calling requestLayout if necessary.
7947 * (This allows addViewInLayout to not request a new layout.)
7948 */
7949 void assignParent(ViewParent parent) {
7950 if (mParent == null) {
7951 mParent = parent;
7952 } else if (parent == null) {
7953 mParent = null;
7954 } else {
7955 throw new RuntimeException("view " + this + " being added, but"
7956 + " it already has a parent");
7957 }
7958 }
7959
7960 /**
7961 * This is called when the view is attached to a window. At this point it
7962 * has a Surface and will start drawing. Note that this function is
7963 * guaranteed to be called before {@link #onDraw}, however it may be called
7964 * any time before the first onDraw -- including before or after
7965 * {@link #onMeasure}.
7966 *
7967 * @see #onDetachedFromWindow()
7968 */
7969 protected void onAttachedToWindow() {
7970 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
7971 mParent.requestTransparentRegion(this);
7972 }
Adam Powell8568c3a2010-04-19 14:26:11 -07007973 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
7974 initialAwakenScrollBars();
7975 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
7976 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08007977 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007978 }
7979
7980 /**
7981 * This is called when the view is detached from a window. At this point it
7982 * no longer has a surface for drawing.
7983 *
7984 * @see #onAttachedToWindow()
7985 */
7986 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08007987 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08007988
Romain Guya440b002010-02-24 15:57:54 -08007989 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05007990 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08007991 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08007992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007993 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08007994
7995 if (mHardwareLayer != null) {
7996 mHardwareLayer.destroy();
7997 mHardwareLayer = null;
7998 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007999
Chet Haasedaf98e92011-01-10 14:10:36 -08008000 if (mDisplayList != null) {
8001 mDisplayList.invalidate();
8002 }
8003
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008004 if (mAttachInfo != null) {
8005 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8006 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8007 }
8008
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008009 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008010 }
8011
8012 /**
8013 * @return The number of times this view has been attached to a window
8014 */
8015 protected int getWindowAttachCount() {
8016 return mWindowAttachCount;
8017 }
8018
8019 /**
8020 * Retrieve a unique token identifying the window this view is attached to.
8021 * @return Return the window's token for use in
8022 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8023 */
8024 public IBinder getWindowToken() {
8025 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8026 }
8027
8028 /**
8029 * Retrieve a unique token identifying the top-level "real" window of
8030 * the window that this view is attached to. That is, this is like
8031 * {@link #getWindowToken}, except if the window this view in is a panel
8032 * window (attached to another containing window), then the token of
8033 * the containing window is returned instead.
8034 *
8035 * @return Returns the associated window token, either
8036 * {@link #getWindowToken()} or the containing window's token.
8037 */
8038 public IBinder getApplicationWindowToken() {
8039 AttachInfo ai = mAttachInfo;
8040 if (ai != null) {
8041 IBinder appWindowToken = ai.mPanelParentWindowToken;
8042 if (appWindowToken == null) {
8043 appWindowToken = ai.mWindowToken;
8044 }
8045 return appWindowToken;
8046 }
8047 return null;
8048 }
8049
8050 /**
8051 * Retrieve private session object this view hierarchy is using to
8052 * communicate with the window manager.
8053 * @return the session object to communicate with the window manager
8054 */
8055 /*package*/ IWindowSession getWindowSession() {
8056 return mAttachInfo != null ? mAttachInfo.mSession : null;
8057 }
8058
8059 /**
8060 * @param info the {@link android.view.View.AttachInfo} to associated with
8061 * this view
8062 */
8063 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8064 //System.out.println("Attached! " + this);
8065 mAttachInfo = info;
8066 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008067 // We will need to evaluate the drawable state at least once.
8068 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 if (mFloatingTreeObserver != null) {
8070 info.mTreeObserver.merge(mFloatingTreeObserver);
8071 mFloatingTreeObserver = null;
8072 }
8073 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8074 mAttachInfo.mScrollContainers.add(this);
8075 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8076 }
8077 performCollectViewAttributes(visibility);
8078 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008079
8080 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8081 mOnAttachStateChangeListeners;
8082 if (listeners != null && listeners.size() > 0) {
8083 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8084 // perform the dispatching. The iterator is a safe guard against listeners that
8085 // could mutate the list by calling the various add/remove methods. This prevents
8086 // the array from being modified while we iterate it.
8087 for (OnAttachStateChangeListener listener : listeners) {
8088 listener.onViewAttachedToWindow(this);
8089 }
8090 }
8091
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 int vis = info.mWindowVisibility;
8093 if (vis != GONE) {
8094 onWindowVisibilityChanged(vis);
8095 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008096 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8097 // If nobody has evaluated the drawable state yet, then do it now.
8098 refreshDrawableState();
8099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 }
8101
8102 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008103 AttachInfo info = mAttachInfo;
8104 if (info != null) {
8105 int vis = info.mWindowVisibility;
8106 if (vis != GONE) {
8107 onWindowVisibilityChanged(GONE);
8108 }
8109 }
8110
8111 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008112
Adam Powell4afd62b2011-02-18 15:02:18 -08008113 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8114 mOnAttachStateChangeListeners;
8115 if (listeners != null && listeners.size() > 0) {
8116 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8117 // perform the dispatching. The iterator is a safe guard against listeners that
8118 // could mutate the list by calling the various add/remove methods. This prevents
8119 // the array from being modified while we iterate it.
8120 for (OnAttachStateChangeListener listener : listeners) {
8121 listener.onViewDetachedFromWindow(this);
8122 }
8123 }
8124
Romain Guy01d5edc2011-01-28 11:28:53 -08008125 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008126 mAttachInfo.mScrollContainers.remove(this);
8127 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8128 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 mAttachInfo = null;
8131 }
8132
8133 /**
8134 * Store this view hierarchy's frozen state into the given container.
8135 *
8136 * @param container The SparseArray in which to save the view's state.
8137 *
8138 * @see #restoreHierarchyState
8139 * @see #dispatchSaveInstanceState
8140 * @see #onSaveInstanceState
8141 */
8142 public void saveHierarchyState(SparseArray<Parcelable> container) {
8143 dispatchSaveInstanceState(container);
8144 }
8145
8146 /**
8147 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8148 * May be overridden to modify how freezing happens to a view's children; for example, some
8149 * views may want to not store state for their children.
8150 *
8151 * @param container The SparseArray in which to save the view's state.
8152 *
8153 * @see #dispatchRestoreInstanceState
8154 * @see #saveHierarchyState
8155 * @see #onSaveInstanceState
8156 */
8157 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8158 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8159 mPrivateFlags &= ~SAVE_STATE_CALLED;
8160 Parcelable state = onSaveInstanceState();
8161 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8162 throw new IllegalStateException(
8163 "Derived class did not call super.onSaveInstanceState()");
8164 }
8165 if (state != null) {
8166 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8167 // + ": " + state);
8168 container.put(mID, state);
8169 }
8170 }
8171 }
8172
8173 /**
8174 * Hook allowing a view to generate a representation of its internal state
8175 * that can later be used to create a new instance with that same state.
8176 * This state should only contain information that is not persistent or can
8177 * not be reconstructed later. For example, you will never store your
8178 * current position on screen because that will be computed again when a
8179 * new instance of the view is placed in its view hierarchy.
8180 * <p>
8181 * Some examples of things you may store here: the current cursor position
8182 * in a text view (but usually not the text itself since that is stored in a
8183 * content provider or other persistent storage), the currently selected
8184 * item in a list view.
8185 *
8186 * @return Returns a Parcelable object containing the view's current dynamic
8187 * state, or null if there is nothing interesting to save. The
8188 * default implementation returns null.
8189 * @see #onRestoreInstanceState
8190 * @see #saveHierarchyState
8191 * @see #dispatchSaveInstanceState
8192 * @see #setSaveEnabled(boolean)
8193 */
8194 protected Parcelable onSaveInstanceState() {
8195 mPrivateFlags |= SAVE_STATE_CALLED;
8196 return BaseSavedState.EMPTY_STATE;
8197 }
8198
8199 /**
8200 * Restore this view hierarchy's frozen state from the given container.
8201 *
8202 * @param container The SparseArray which holds previously frozen states.
8203 *
8204 * @see #saveHierarchyState
8205 * @see #dispatchRestoreInstanceState
8206 * @see #onRestoreInstanceState
8207 */
8208 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8209 dispatchRestoreInstanceState(container);
8210 }
8211
8212 /**
8213 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8214 * children. May be overridden to modify how restoreing happens to a view's children; for
8215 * example, some views may want to not store state for their children.
8216 *
8217 * @param container The SparseArray which holds previously saved state.
8218 *
8219 * @see #dispatchSaveInstanceState
8220 * @see #restoreHierarchyState
8221 * @see #onRestoreInstanceState
8222 */
8223 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8224 if (mID != NO_ID) {
8225 Parcelable state = container.get(mID);
8226 if (state != null) {
8227 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8228 // + ": " + state);
8229 mPrivateFlags &= ~SAVE_STATE_CALLED;
8230 onRestoreInstanceState(state);
8231 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8232 throw new IllegalStateException(
8233 "Derived class did not call super.onRestoreInstanceState()");
8234 }
8235 }
8236 }
8237 }
8238
8239 /**
8240 * Hook allowing a view to re-apply a representation of its internal state that had previously
8241 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8242 * null state.
8243 *
8244 * @param state The frozen state that had previously been returned by
8245 * {@link #onSaveInstanceState}.
8246 *
8247 * @see #onSaveInstanceState
8248 * @see #restoreHierarchyState
8249 * @see #dispatchRestoreInstanceState
8250 */
8251 protected void onRestoreInstanceState(Parcelable state) {
8252 mPrivateFlags |= SAVE_STATE_CALLED;
8253 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008254 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8255 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008256 + "when two views of different type have the same id in the same hierarchy. "
8257 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008258 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008259 }
8260 }
8261
8262 /**
8263 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8264 *
8265 * @return the drawing start time in milliseconds
8266 */
8267 public long getDrawingTime() {
8268 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8269 }
8270
8271 /**
8272 * <p>Enables or disables the duplication of the parent's state into this view. When
8273 * duplication is enabled, this view gets its drawable state from its parent rather
8274 * than from its own internal properties.</p>
8275 *
8276 * <p>Note: in the current implementation, setting this property to true after the
8277 * view was added to a ViewGroup might have no effect at all. This property should
8278 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8279 *
8280 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8281 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008282 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008283 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8284 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 *
8286 * @param enabled True to enable duplication of the parent's drawable state, false
8287 * to disable it.
8288 *
8289 * @see #getDrawableState()
8290 * @see #isDuplicateParentStateEnabled()
8291 */
8292 public void setDuplicateParentStateEnabled(boolean enabled) {
8293 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8294 }
8295
8296 /**
8297 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8298 *
8299 * @return True if this view's drawable state is duplicated from the parent,
8300 * false otherwise
8301 *
8302 * @see #getDrawableState()
8303 * @see #setDuplicateParentStateEnabled(boolean)
8304 */
8305 public boolean isDuplicateParentStateEnabled() {
8306 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8307 }
8308
8309 /**
Romain Guy171c5922011-01-06 10:04:23 -08008310 * <p>Specifies the type of layer backing this view. The layer can be
8311 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8312 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008313 *
Romain Guy171c5922011-01-06 10:04:23 -08008314 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8315 * instance that controls how the layer is composed on screen. The following
8316 * properties of the paint are taken into account when composing the layer:</p>
8317 * <ul>
8318 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8319 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8320 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8321 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008322 *
Romain Guy171c5922011-01-06 10:04:23 -08008323 * <p>If this view has an alpha value set to < 1.0 by calling
8324 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8325 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8326 * equivalent to setting a hardware layer on this view and providing a paint with
8327 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008328 *
Romain Guy171c5922011-01-06 10:04:23 -08008329 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8330 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8331 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008332 *
Romain Guy171c5922011-01-06 10:04:23 -08008333 * @param layerType The ype of layer to use with this view, must be one of
8334 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8335 * {@link #LAYER_TYPE_HARDWARE}
8336 * @param paint The paint used to compose the layer. This argument is optional
8337 * and can be null. It is ignored when the layer type is
8338 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008339 *
8340 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008341 * @see #LAYER_TYPE_NONE
8342 * @see #LAYER_TYPE_SOFTWARE
8343 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008344 * @see #setAlpha(float)
8345 *
Romain Guy171c5922011-01-06 10:04:23 -08008346 * @attr ref android.R.styleable#View_layerType
8347 */
8348 public void setLayerType(int layerType, Paint paint) {
8349 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008350 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008351 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8352 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008353
Romain Guyd6cd5722011-01-17 14:42:41 -08008354 if (layerType == mLayerType) {
8355 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8356 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008357 invalidateParentCaches();
8358 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008359 }
8360 return;
8361 }
Romain Guy171c5922011-01-06 10:04:23 -08008362
8363 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008364 switch (mLayerType) {
8365 case LAYER_TYPE_SOFTWARE:
8366 if (mDrawingCache != null) {
8367 mDrawingCache.recycle();
8368 mDrawingCache = null;
8369 }
Joe Malin32736f02011-01-19 16:14:20 -08008370
Romain Guy6c319ca2011-01-11 14:29:25 -08008371 if (mUnscaledDrawingCache != null) {
8372 mUnscaledDrawingCache.recycle();
8373 mUnscaledDrawingCache = null;
8374 }
8375 break;
8376 case LAYER_TYPE_HARDWARE:
8377 if (mHardwareLayer != null) {
8378 mHardwareLayer.destroy();
8379 mHardwareLayer = null;
8380 }
8381 break;
8382 default:
8383 break;
Romain Guy171c5922011-01-06 10:04:23 -08008384 }
8385
8386 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008387 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8388 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8389 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008390
Romain Guy0fd89bf2011-01-26 15:41:30 -08008391 invalidateParentCaches();
8392 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008393 }
8394
8395 /**
8396 * Indicates what type of layer is currently associated with this view. By default
8397 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8398 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8399 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008400 *
Romain Guy171c5922011-01-06 10:04:23 -08008401 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8402 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008403 *
8404 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08008405 * @see #LAYER_TYPE_NONE
8406 * @see #LAYER_TYPE_SOFTWARE
8407 * @see #LAYER_TYPE_HARDWARE
8408 */
8409 public int getLayerType() {
8410 return mLayerType;
8411 }
Joe Malin32736f02011-01-19 16:14:20 -08008412
Romain Guy6c319ca2011-01-11 14:29:25 -08008413 /**
8414 * <p>Returns a hardware layer that can be used to draw this view again
8415 * without executing its draw method.</p>
8416 *
8417 * @return A HardwareLayer ready to render, or null if an error occurred.
8418 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008419 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008420 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8421 return null;
8422 }
8423
8424 final int width = mRight - mLeft;
8425 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008426
Romain Guy6c319ca2011-01-11 14:29:25 -08008427 if (width == 0 || height == 0) {
8428 return null;
8429 }
8430
8431 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8432 if (mHardwareLayer == null) {
8433 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8434 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008435 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008436 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8437 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008438 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008439 }
8440
Romain Guy5e7f7662011-01-24 22:35:56 -08008441 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8442 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8443 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008444 try {
8445 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008446 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008447 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008448
Romain Guy4f09f542011-01-26 22:41:43 -08008449 final int restoreCount = canvas.save();
8450
Romain Guy6c319ca2011-01-11 14:29:25 -08008451 computeScroll();
8452 canvas.translate(-mScrollX, -mScrollY);
8453
Romain Guy6c319ca2011-01-11 14:29:25 -08008454 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008455
Romain Guy6c319ca2011-01-11 14:29:25 -08008456 // Fast path for layouts with no backgrounds
8457 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8458 mPrivateFlags &= ~DIRTY_MASK;
8459 dispatchDraw(canvas);
8460 } else {
8461 draw(canvas);
8462 }
Joe Malin32736f02011-01-19 16:14:20 -08008463
Romain Guy6c319ca2011-01-11 14:29:25 -08008464 canvas.restoreToCount(restoreCount);
8465 } finally {
8466 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008467 mHardwareLayer.end(currentCanvas);
8468 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008469 }
8470 }
8471
8472 return mHardwareLayer;
8473 }
Romain Guy171c5922011-01-06 10:04:23 -08008474
8475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008476 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8477 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8478 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8479 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8480 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8481 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008482 *
Romain Guy171c5922011-01-06 10:04:23 -08008483 * <p>Enabling the drawing cache is similar to
8484 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008485 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8486 * drawing cache has no effect on rendering because the system uses a different mechanism
8487 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8488 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8489 * for information on how to enable software and hardware layers.</p>
8490 *
8491 * <p>This API can be used to manually generate
8492 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8493 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008494 *
8495 * @param enabled true to enable the drawing cache, false otherwise
8496 *
8497 * @see #isDrawingCacheEnabled()
8498 * @see #getDrawingCache()
8499 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008500 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008501 */
8502 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008503 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008504 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8505 }
8506
8507 /**
8508 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8509 *
8510 * @return true if the drawing cache is enabled
8511 *
8512 * @see #setDrawingCacheEnabled(boolean)
8513 * @see #getDrawingCache()
8514 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008515 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 public boolean isDrawingCacheEnabled() {
8517 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8518 }
8519
8520 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008521 * Debugging utility which recursively outputs the dirty state of a view and its
8522 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008523 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008524 * @hide
8525 */
Romain Guy676b1732011-02-14 14:45:33 -08008526 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008527 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8528 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8529 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8530 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8531 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8532 if (clear) {
8533 mPrivateFlags &= clearMask;
8534 }
8535 if (this instanceof ViewGroup) {
8536 ViewGroup parent = (ViewGroup) this;
8537 final int count = parent.getChildCount();
8538 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008539 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008540 child.outputDirtyFlags(indent + " ", clear, clearMask);
8541 }
8542 }
8543 }
8544
8545 /**
8546 * This method is used by ViewGroup to cause its children to restore or recreate their
8547 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8548 * to recreate its own display list, which would happen if it went through the normal
8549 * draw/dispatchDraw mechanisms.
8550 *
8551 * @hide
8552 */
8553 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008554
8555 /**
8556 * A view that is not attached or hardware accelerated cannot create a display list.
8557 * This method checks these conditions and returns the appropriate result.
8558 *
8559 * @return true if view has the ability to create a display list, false otherwise.
8560 *
8561 * @hide
8562 */
8563 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008564 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008565 }
Joe Malin32736f02011-01-19 16:14:20 -08008566
Chet Haasedaf98e92011-01-10 14:10:36 -08008567 /**
Romain Guyb051e892010-09-28 19:09:36 -07008568 * <p>Returns a display list that can be used to draw this view again
8569 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008570 *
Romain Guyb051e892010-09-28 19:09:36 -07008571 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008572 *
8573 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008574 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008575 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008576 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008577 return null;
8578 }
8579
Chet Haasedaf98e92011-01-10 14:10:36 -08008580 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8581 mDisplayList == null || !mDisplayList.isValid() ||
8582 mRecreateDisplayList)) {
8583 // Don't need to recreate the display list, just need to tell our
8584 // children to restore/recreate theirs
8585 if (mDisplayList != null && mDisplayList.isValid() &&
8586 !mRecreateDisplayList) {
8587 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8588 mPrivateFlags &= ~DIRTY_MASK;
8589 dispatchGetDisplayList();
8590
8591 return mDisplayList;
8592 }
8593
8594 // If we got here, we're recreating it. Mark it as such to ensure that
8595 // we copy in child display lists into ours in drawChild()
8596 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008597 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008598 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8599 // If we're creating a new display list, make sure our parent gets invalidated
8600 // since they will need to recreate their display list to account for this
8601 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008602 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008603 }
Romain Guyb051e892010-09-28 19:09:36 -07008604
8605 final HardwareCanvas canvas = mDisplayList.start();
8606 try {
8607 int width = mRight - mLeft;
8608 int height = mBottom - mTop;
8609
8610 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008611 // The dirty rect should always be null for a display list
8612 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008613
8614 final int restoreCount = canvas.save();
8615
Chet Haasedaf98e92011-01-10 14:10:36 -08008616 computeScroll();
8617 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008618 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008619
Romain Guyb051e892010-09-28 19:09:36 -07008620 // Fast path for layouts with no backgrounds
8621 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8622 mPrivateFlags &= ~DIRTY_MASK;
8623 dispatchDraw(canvas);
8624 } else {
8625 draw(canvas);
8626 }
Joe Malin32736f02011-01-19 16:14:20 -08008627
Romain Guyb051e892010-09-28 19:09:36 -07008628 canvas.restoreToCount(restoreCount);
8629 } finally {
8630 canvas.onPostDraw();
8631
8632 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008633 }
Chet Haase77785f92011-01-25 23:22:09 -08008634 } else {
8635 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8636 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008637 }
8638
8639 return mDisplayList;
8640 }
8641
8642 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008643 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008644 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008645 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008646 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008647 * @see #getDrawingCache(boolean)
8648 */
8649 public Bitmap getDrawingCache() {
8650 return getDrawingCache(false);
8651 }
8652
8653 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008654 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8655 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8656 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8657 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8658 * request the drawing cache by calling this method and draw it on screen if the
8659 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008660 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008661 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8662 * this method will create a bitmap of the same size as this view. Because this bitmap
8663 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8664 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8665 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8666 * size than the view. This implies that your application must be able to handle this
8667 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008668 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008669 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8670 * the current density of the screen when the application is in compatibility
8671 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008672 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008673 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008674 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008675 * @see #setDrawingCacheEnabled(boolean)
8676 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008677 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 * @see #destroyDrawingCache()
8679 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008680 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008681 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8682 return null;
8683 }
8684 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008685 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 }
Romain Guy02890fd2010-08-06 17:58:44 -07008687 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 }
8689
8690 /**
8691 * <p>Frees the resources used by the drawing cache. If you call
8692 * {@link #buildDrawingCache()} manually without calling
8693 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8694 * should cleanup the cache with this method afterwards.</p>
8695 *
8696 * @see #setDrawingCacheEnabled(boolean)
8697 * @see #buildDrawingCache()
8698 * @see #getDrawingCache()
8699 */
8700 public void destroyDrawingCache() {
8701 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008702 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008703 mDrawingCache = null;
8704 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008705 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008706 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008707 mUnscaledDrawingCache = null;
8708 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008709 }
8710
8711 /**
8712 * Setting a solid background color for the drawing cache's bitmaps will improve
8713 * perfromance and memory usage. Note, though that this should only be used if this
8714 * view will always be drawn on top of a solid color.
8715 *
8716 * @param color The background color to use for the drawing cache's bitmap
8717 *
8718 * @see #setDrawingCacheEnabled(boolean)
8719 * @see #buildDrawingCache()
8720 * @see #getDrawingCache()
8721 */
8722 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008723 if (color != mDrawingCacheBackgroundColor) {
8724 mDrawingCacheBackgroundColor = color;
8725 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008727 }
8728
8729 /**
8730 * @see #setDrawingCacheBackgroundColor(int)
8731 *
8732 * @return The background color to used for the drawing cache's bitmap
8733 */
8734 public int getDrawingCacheBackgroundColor() {
8735 return mDrawingCacheBackgroundColor;
8736 }
8737
8738 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008739 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008740 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008741 * @see #buildDrawingCache(boolean)
8742 */
8743 public void buildDrawingCache() {
8744 buildDrawingCache(false);
8745 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008746
Romain Guyfbd8f692009-06-26 14:51:58 -07008747 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008748 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8749 *
8750 * <p>If you call {@link #buildDrawingCache()} manually without calling
8751 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8752 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008753 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008754 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8755 * this method will create a bitmap of the same size as this view. Because this bitmap
8756 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8757 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8758 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8759 * size than the view. This implies that your application must be able to handle this
8760 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008761 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008762 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8763 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008764 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008765 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766 *
8767 * @see #getDrawingCache()
8768 * @see #destroyDrawingCache()
8769 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008770 public void buildDrawingCache(boolean autoScale) {
8771 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008772 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008773 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008774
8775 if (ViewDebug.TRACE_HIERARCHY) {
8776 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778
Romain Guy8506ab42009-06-11 17:35:47 -07008779 int width = mRight - mLeft;
8780 int height = mBottom - mTop;
8781
8782 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008783 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008784
Romain Guye1123222009-06-29 14:24:56 -07008785 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008786 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8787 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008788 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008789
8790 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008791 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008792 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793
8794 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008795 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008796 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008797 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8798 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008799 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 return;
8801 }
8802
8803 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008804 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008805
8806 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008807 Bitmap.Config quality;
8808 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008809 // Never pick ARGB_4444 because it looks awful
8810 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008811 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8812 case DRAWING_CACHE_QUALITY_AUTO:
8813 quality = Bitmap.Config.ARGB_8888;
8814 break;
8815 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008816 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008817 break;
8818 case DRAWING_CACHE_QUALITY_HIGH:
8819 quality = Bitmap.Config.ARGB_8888;
8820 break;
8821 default:
8822 quality = Bitmap.Config.ARGB_8888;
8823 break;
8824 }
8825 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008826 // Optimization for translucent windows
8827 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008828 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008829 }
8830
8831 // Try to cleanup memory
8832 if (bitmap != null) bitmap.recycle();
8833
8834 try {
8835 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008836 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008837 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008838 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008839 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008840 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008841 }
Adam Powell26153a32010-11-08 15:22:27 -08008842 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008843 } catch (OutOfMemoryError e) {
8844 // If there is not enough memory to create the bitmap cache, just
8845 // ignore the issue as bitmap caches are not required to draw the
8846 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008847 if (autoScale) {
8848 mDrawingCache = null;
8849 } else {
8850 mUnscaledDrawingCache = null;
8851 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008852 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008853 return;
8854 }
8855
8856 clear = drawingCacheBackgroundColor != 0;
8857 }
8858
8859 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008860 if (attachInfo != null) {
8861 canvas = attachInfo.mCanvas;
8862 if (canvas == null) {
8863 canvas = new Canvas();
8864 }
8865 canvas.setBitmap(bitmap);
8866 // Temporarily clobber the cached Canvas in case one of our children
8867 // is also using a drawing cache. Without this, the children would
8868 // steal the canvas by attaching their own bitmap to it and bad, bad
8869 // thing would happen (invisible views, corrupted drawings, etc.)
8870 attachInfo.mCanvas = null;
8871 } else {
8872 // This case should hopefully never or seldom happen
8873 canvas = new Canvas(bitmap);
8874 }
8875
8876 if (clear) {
8877 bitmap.eraseColor(drawingCacheBackgroundColor);
8878 }
8879
8880 computeScroll();
8881 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08008882
Romain Guye1123222009-06-29 14:24:56 -07008883 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008884 final float scale = attachInfo.mApplicationScale;
8885 canvas.scale(scale, scale);
8886 }
Joe Malin32736f02011-01-19 16:14:20 -08008887
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008888 canvas.translate(-mScrollX, -mScrollY);
8889
Romain Guy5bcdff42009-05-14 21:27:18 -07008890 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08008891 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
8892 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07008893 mPrivateFlags |= DRAWING_CACHE_VALID;
8894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008895
8896 // Fast path for layouts with no backgrounds
8897 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8898 if (ViewDebug.TRACE_HIERARCHY) {
8899 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8900 }
Romain Guy5bcdff42009-05-14 21:27:18 -07008901 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008902 dispatchDraw(canvas);
8903 } else {
8904 draw(canvas);
8905 }
8906
8907 canvas.restoreToCount(restoreCount);
8908
8909 if (attachInfo != null) {
8910 // Restore the cached Canvas for our siblings
8911 attachInfo.mCanvas = canvas;
8912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008913 }
8914 }
8915
8916 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008917 * Create a snapshot of the view into a bitmap. We should probably make
8918 * some form of this public, but should think about the API.
8919 */
Romain Guy223ff5c2010-03-02 17:07:47 -08008920 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008921 int width = mRight - mLeft;
8922 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008923
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008924 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07008925 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008926 width = (int) ((width * scale) + 0.5f);
8927 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08008928
Romain Guy8c11e312009-09-14 15:15:30 -07008929 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008930 if (bitmap == null) {
8931 throw new OutOfMemoryError();
8932 }
8933
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008934 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08008935
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008936 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008937 if (attachInfo != null) {
8938 canvas = attachInfo.mCanvas;
8939 if (canvas == null) {
8940 canvas = new Canvas();
8941 }
8942 canvas.setBitmap(bitmap);
8943 // Temporarily clobber the cached Canvas in case one of our children
8944 // is also using a drawing cache. Without this, the children would
8945 // steal the canvas by attaching their own bitmap to it and bad, bad
8946 // things would happen (invisible views, corrupted drawings, etc.)
8947 attachInfo.mCanvas = null;
8948 } else {
8949 // This case should hopefully never or seldom happen
8950 canvas = new Canvas(bitmap);
8951 }
8952
Romain Guy5bcdff42009-05-14 21:27:18 -07008953 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008954 bitmap.eraseColor(backgroundColor);
8955 }
8956
8957 computeScroll();
8958 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008959 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008960 canvas.translate(-mScrollX, -mScrollY);
8961
Romain Guy5bcdff42009-05-14 21:27:18 -07008962 // Temporarily remove the dirty mask
8963 int flags = mPrivateFlags;
8964 mPrivateFlags &= ~DIRTY_MASK;
8965
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008966 // Fast path for layouts with no backgrounds
8967 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8968 dispatchDraw(canvas);
8969 } else {
8970 draw(canvas);
8971 }
8972
Romain Guy5bcdff42009-05-14 21:27:18 -07008973 mPrivateFlags = flags;
8974
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008975 canvas.restoreToCount(restoreCount);
8976
8977 if (attachInfo != null) {
8978 // Restore the cached Canvas for our siblings
8979 attachInfo.mCanvas = canvas;
8980 }
Romain Guy8506ab42009-06-11 17:35:47 -07008981
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008982 return bitmap;
8983 }
8984
8985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 * Indicates whether this View is currently in edit mode. A View is usually
8987 * in edit mode when displayed within a developer tool. For instance, if
8988 * this View is being drawn by a visual user interface builder, this method
8989 * should return true.
8990 *
8991 * Subclasses should check the return value of this method to provide
8992 * different behaviors if their normal behavior might interfere with the
8993 * host environment. For instance: the class spawns a thread in its
8994 * constructor, the drawing code relies on device-specific features, etc.
8995 *
8996 * This method is usually checked in the drawing code of custom widgets.
8997 *
8998 * @return True if this View is in edit mode, false otherwise.
8999 */
9000 public boolean isInEditMode() {
9001 return false;
9002 }
9003
9004 /**
9005 * If the View draws content inside its padding and enables fading edges,
9006 * it needs to support padding offsets. Padding offsets are added to the
9007 * fading edges to extend the length of the fade so that it covers pixels
9008 * drawn inside the padding.
9009 *
9010 * Subclasses of this class should override this method if they need
9011 * to draw content inside the padding.
9012 *
9013 * @return True if padding offset must be applied, false otherwise.
9014 *
9015 * @see #getLeftPaddingOffset()
9016 * @see #getRightPaddingOffset()
9017 * @see #getTopPaddingOffset()
9018 * @see #getBottomPaddingOffset()
9019 *
9020 * @since CURRENT
9021 */
9022 protected boolean isPaddingOffsetRequired() {
9023 return false;
9024 }
9025
9026 /**
9027 * Amount by which to extend the left fading region. Called only when
9028 * {@link #isPaddingOffsetRequired()} returns true.
9029 *
9030 * @return The left padding offset in pixels.
9031 *
9032 * @see #isPaddingOffsetRequired()
9033 *
9034 * @since CURRENT
9035 */
9036 protected int getLeftPaddingOffset() {
9037 return 0;
9038 }
9039
9040 /**
9041 * Amount by which to extend the right fading region. Called only when
9042 * {@link #isPaddingOffsetRequired()} returns true.
9043 *
9044 * @return The right padding offset in pixels.
9045 *
9046 * @see #isPaddingOffsetRequired()
9047 *
9048 * @since CURRENT
9049 */
9050 protected int getRightPaddingOffset() {
9051 return 0;
9052 }
9053
9054 /**
9055 * Amount by which to extend the top fading region. Called only when
9056 * {@link #isPaddingOffsetRequired()} returns true.
9057 *
9058 * @return The top padding offset in pixels.
9059 *
9060 * @see #isPaddingOffsetRequired()
9061 *
9062 * @since CURRENT
9063 */
9064 protected int getTopPaddingOffset() {
9065 return 0;
9066 }
9067
9068 /**
9069 * Amount by which to extend the bottom fading region. Called only when
9070 * {@link #isPaddingOffsetRequired()} returns true.
9071 *
9072 * @return The bottom padding offset in pixels.
9073 *
9074 * @see #isPaddingOffsetRequired()
9075 *
9076 * @since CURRENT
9077 */
9078 protected int getBottomPaddingOffset() {
9079 return 0;
9080 }
9081
9082 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009083 * <p>Indicates whether this view is attached to an hardware accelerated
9084 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009085 *
Romain Guy2bffd262010-09-12 17:40:02 -07009086 * <p>Even if this method returns true, it does not mean that every call
9087 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9088 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9089 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9090 * window is hardware accelerated,
9091 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9092 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009093 *
Romain Guy2bffd262010-09-12 17:40:02 -07009094 * @return True if the view is attached to a window and the window is
9095 * hardware accelerated; false in any other case.
9096 */
9097 public boolean isHardwareAccelerated() {
9098 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9099 }
Joe Malin32736f02011-01-19 16:14:20 -08009100
Romain Guy2bffd262010-09-12 17:40:02 -07009101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 * Manually render this view (and all of its children) to the given Canvas.
9103 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009104 * called. When implementing a view, implement {@link #onDraw} instead of
9105 * overriding this method. If you do need to override this method, call
9106 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009107 *
9108 * @param canvas The Canvas to which the View is rendered.
9109 */
9110 public void draw(Canvas canvas) {
9111 if (ViewDebug.TRACE_HIERARCHY) {
9112 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9113 }
9114
Romain Guy5bcdff42009-05-14 21:27:18 -07009115 final int privateFlags = mPrivateFlags;
9116 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9117 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9118 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009120 /*
9121 * Draw traversal performs several drawing steps which must be executed
9122 * in the appropriate order:
9123 *
9124 * 1. Draw the background
9125 * 2. If necessary, save the canvas' layers to prepare for fading
9126 * 3. Draw view's content
9127 * 4. Draw children
9128 * 5. If necessary, draw the fading edges and restore layers
9129 * 6. Draw decorations (scrollbars for instance)
9130 */
9131
9132 // Step 1, draw the background, if needed
9133 int saveCount;
9134
Romain Guy24443ea2009-05-11 11:56:30 -07009135 if (!dirtyOpaque) {
9136 final Drawable background = mBGDrawable;
9137 if (background != null) {
9138 final int scrollX = mScrollX;
9139 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009140
Romain Guy24443ea2009-05-11 11:56:30 -07009141 if (mBackgroundSizeChanged) {
9142 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9143 mBackgroundSizeChanged = false;
9144 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145
Romain Guy24443ea2009-05-11 11:56:30 -07009146 if ((scrollX | scrollY) == 0) {
9147 background.draw(canvas);
9148 } else {
9149 canvas.translate(scrollX, scrollY);
9150 background.draw(canvas);
9151 canvas.translate(-scrollX, -scrollY);
9152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009153 }
9154 }
9155
9156 // skip step 2 & 5 if possible (common case)
9157 final int viewFlags = mViewFlags;
9158 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9159 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9160 if (!verticalEdges && !horizontalEdges) {
9161 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009162 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163
9164 // Step 4, draw the children
9165 dispatchDraw(canvas);
9166
9167 // Step 6, draw decorations (scrollbars)
9168 onDrawScrollBars(canvas);
9169
9170 // we're done...
9171 return;
9172 }
9173
9174 /*
9175 * Here we do the full fledged routine...
9176 * (this is an uncommon case where speed matters less,
9177 * this is why we repeat some of the tests that have been
9178 * done above)
9179 */
9180
9181 boolean drawTop = false;
9182 boolean drawBottom = false;
9183 boolean drawLeft = false;
9184 boolean drawRight = false;
9185
9186 float topFadeStrength = 0.0f;
9187 float bottomFadeStrength = 0.0f;
9188 float leftFadeStrength = 0.0f;
9189 float rightFadeStrength = 0.0f;
9190
9191 // Step 2, save the canvas' layers
9192 int paddingLeft = mPaddingLeft;
9193 int paddingTop = mPaddingTop;
9194
9195 final boolean offsetRequired = isPaddingOffsetRequired();
9196 if (offsetRequired) {
9197 paddingLeft += getLeftPaddingOffset();
9198 paddingTop += getTopPaddingOffset();
9199 }
9200
9201 int left = mScrollX + paddingLeft;
9202 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9203 int top = mScrollY + paddingTop;
9204 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9205
9206 if (offsetRequired) {
9207 right += getRightPaddingOffset();
9208 bottom += getBottomPaddingOffset();
9209 }
9210
9211 final ScrollabilityCache scrollabilityCache = mScrollCache;
9212 int length = scrollabilityCache.fadingEdgeLength;
9213
9214 // clip the fade length if top and bottom fades overlap
9215 // overlapping fades produce odd-looking artifacts
9216 if (verticalEdges && (top + length > bottom - length)) {
9217 length = (bottom - top) / 2;
9218 }
9219
9220 // also clip horizontal fades if necessary
9221 if (horizontalEdges && (left + length > right - length)) {
9222 length = (right - left) / 2;
9223 }
9224
9225 if (verticalEdges) {
9226 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009227 drawTop = topFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009228 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009229 drawBottom = bottomFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009230 }
9231
9232 if (horizontalEdges) {
9233 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009234 drawLeft = leftFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009235 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009236 drawRight = rightFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009237 }
9238
9239 saveCount = canvas.getSaveCount();
9240
9241 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009242 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009243 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9244
9245 if (drawTop) {
9246 canvas.saveLayer(left, top, right, top + length, null, flags);
9247 }
9248
9249 if (drawBottom) {
9250 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9251 }
9252
9253 if (drawLeft) {
9254 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9255 }
9256
9257 if (drawRight) {
9258 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9259 }
9260 } else {
9261 scrollabilityCache.setFadeColor(solidColor);
9262 }
9263
9264 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009265 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009266
9267 // Step 4, draw the children
9268 dispatchDraw(canvas);
9269
9270 // Step 5, draw the fade effect and restore layers
9271 final Paint p = scrollabilityCache.paint;
9272 final Matrix matrix = scrollabilityCache.matrix;
9273 final Shader fade = scrollabilityCache.shader;
9274 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9275
9276 if (drawTop) {
9277 matrix.setScale(1, fadeHeight * topFadeStrength);
9278 matrix.postTranslate(left, top);
9279 fade.setLocalMatrix(matrix);
9280 canvas.drawRect(left, top, right, top + length, p);
9281 }
9282
9283 if (drawBottom) {
9284 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9285 matrix.postRotate(180);
9286 matrix.postTranslate(left, bottom);
9287 fade.setLocalMatrix(matrix);
9288 canvas.drawRect(left, bottom - length, right, bottom, p);
9289 }
9290
9291 if (drawLeft) {
9292 matrix.setScale(1, fadeHeight * leftFadeStrength);
9293 matrix.postRotate(-90);
9294 matrix.postTranslate(left, top);
9295 fade.setLocalMatrix(matrix);
9296 canvas.drawRect(left, top, left + length, bottom, p);
9297 }
9298
9299 if (drawRight) {
9300 matrix.setScale(1, fadeHeight * rightFadeStrength);
9301 matrix.postRotate(90);
9302 matrix.postTranslate(right, top);
9303 fade.setLocalMatrix(matrix);
9304 canvas.drawRect(right - length, top, right, bottom, p);
9305 }
9306
9307 canvas.restoreToCount(saveCount);
9308
9309 // Step 6, draw decorations (scrollbars)
9310 onDrawScrollBars(canvas);
9311 }
9312
9313 /**
9314 * Override this if your view is known to always be drawn on top of a solid color background,
9315 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9316 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9317 * should be set to 0xFF.
9318 *
9319 * @see #setVerticalFadingEdgeEnabled
9320 * @see #setHorizontalFadingEdgeEnabled
9321 *
9322 * @return The known solid color background for this view, or 0 if the color may vary
9323 */
9324 public int getSolidColor() {
9325 return 0;
9326 }
9327
9328 /**
9329 * Build a human readable string representation of the specified view flags.
9330 *
9331 * @param flags the view flags to convert to a string
9332 * @return a String representing the supplied flags
9333 */
9334 private static String printFlags(int flags) {
9335 String output = "";
9336 int numFlags = 0;
9337 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9338 output += "TAKES_FOCUS";
9339 numFlags++;
9340 }
9341
9342 switch (flags & VISIBILITY_MASK) {
9343 case INVISIBLE:
9344 if (numFlags > 0) {
9345 output += " ";
9346 }
9347 output += "INVISIBLE";
9348 // USELESS HERE numFlags++;
9349 break;
9350 case GONE:
9351 if (numFlags > 0) {
9352 output += " ";
9353 }
9354 output += "GONE";
9355 // USELESS HERE numFlags++;
9356 break;
9357 default:
9358 break;
9359 }
9360 return output;
9361 }
9362
9363 /**
9364 * Build a human readable string representation of the specified private
9365 * view flags.
9366 *
9367 * @param privateFlags the private view flags to convert to a string
9368 * @return a String representing the supplied flags
9369 */
9370 private static String printPrivateFlags(int privateFlags) {
9371 String output = "";
9372 int numFlags = 0;
9373
9374 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9375 output += "WANTS_FOCUS";
9376 numFlags++;
9377 }
9378
9379 if ((privateFlags & FOCUSED) == FOCUSED) {
9380 if (numFlags > 0) {
9381 output += " ";
9382 }
9383 output += "FOCUSED";
9384 numFlags++;
9385 }
9386
9387 if ((privateFlags & SELECTED) == SELECTED) {
9388 if (numFlags > 0) {
9389 output += " ";
9390 }
9391 output += "SELECTED";
9392 numFlags++;
9393 }
9394
9395 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9396 if (numFlags > 0) {
9397 output += " ";
9398 }
9399 output += "IS_ROOT_NAMESPACE";
9400 numFlags++;
9401 }
9402
9403 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9404 if (numFlags > 0) {
9405 output += " ";
9406 }
9407 output += "HAS_BOUNDS";
9408 numFlags++;
9409 }
9410
9411 if ((privateFlags & DRAWN) == DRAWN) {
9412 if (numFlags > 0) {
9413 output += " ";
9414 }
9415 output += "DRAWN";
9416 // USELESS HERE numFlags++;
9417 }
9418 return output;
9419 }
9420
9421 /**
9422 * <p>Indicates whether or not this view's layout will be requested during
9423 * the next hierarchy layout pass.</p>
9424 *
9425 * @return true if the layout will be forced during next layout pass
9426 */
9427 public boolean isLayoutRequested() {
9428 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9429 }
9430
9431 /**
9432 * Assign a size and position to a view and all of its
9433 * descendants
9434 *
9435 * <p>This is the second phase of the layout mechanism.
9436 * (The first is measuring). In this phase, each parent calls
9437 * layout on all of its children to position them.
9438 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009439 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 *
Chet Haase9c087442011-01-12 16:20:16 -08009441 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009442 * Derived classes with children should override
9443 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009444 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009445 *
9446 * @param l Left position, relative to parent
9447 * @param t Top position, relative to parent
9448 * @param r Right position, relative to parent
9449 * @param b Bottom position, relative to parent
9450 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009451 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009452 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009453 int oldL = mLeft;
9454 int oldT = mTop;
9455 int oldB = mBottom;
9456 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009457 boolean changed = setFrame(l, t, r, b);
9458 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9459 if (ViewDebug.TRACE_HIERARCHY) {
9460 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9461 }
9462
9463 onLayout(changed, l, t, r, b);
9464 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009465
9466 if (mOnLayoutChangeListeners != null) {
9467 ArrayList<OnLayoutChangeListener> listenersCopy =
9468 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9469 int numListeners = listenersCopy.size();
9470 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009471 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009472 }
9473 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009474 }
9475 mPrivateFlags &= ~FORCE_LAYOUT;
9476 }
9477
9478 /**
9479 * Called from layout when this view should
9480 * assign a size and position to each of its children.
9481 *
9482 * Derived classes with children should override
9483 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009484 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009485 * @param changed This is a new size or position for this view
9486 * @param left Left position, relative to parent
9487 * @param top Top position, relative to parent
9488 * @param right Right position, relative to parent
9489 * @param bottom Bottom position, relative to parent
9490 */
9491 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9492 }
9493
9494 /**
9495 * Assign a size and position to this view.
9496 *
9497 * This is called from layout.
9498 *
9499 * @param left Left position, relative to parent
9500 * @param top Top position, relative to parent
9501 * @param right Right position, relative to parent
9502 * @param bottom Bottom position, relative to parent
9503 * @return true if the new size and position are different than the
9504 * previous ones
9505 * {@hide}
9506 */
9507 protected boolean setFrame(int left, int top, int right, int bottom) {
9508 boolean changed = false;
9509
9510 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009511 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 + right + "," + bottom + ")");
9513 }
9514
9515 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9516 changed = true;
9517
9518 // Remember our drawn bit
9519 int drawn = mPrivateFlags & DRAWN;
9520
9521 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009522 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009523
9524
9525 int oldWidth = mRight - mLeft;
9526 int oldHeight = mBottom - mTop;
9527
9528 mLeft = left;
9529 mTop = top;
9530 mRight = right;
9531 mBottom = bottom;
9532
9533 mPrivateFlags |= HAS_BOUNDS;
9534
9535 int newWidth = right - left;
9536 int newHeight = bottom - top;
9537
9538 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009539 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9540 // A change in dimension means an auto-centered pivot point changes, too
9541 mMatrixDirty = true;
9542 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009543 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9544 }
9545
9546 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9547 // If we are visible, force the DRAWN bit to on so that
9548 // this invalidate will go through (at least to our parent).
9549 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009550 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 // the DRAWN bit.
9552 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009553 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009554 // parent display list may need to be recreated based on a change in the bounds
9555 // of any child
9556 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009557 }
9558
9559 // Reset drawn bit to original value (invalidate turns it off)
9560 mPrivateFlags |= drawn;
9561
9562 mBackgroundSizeChanged = true;
9563 }
9564 return changed;
9565 }
9566
9567 /**
9568 * Finalize inflating a view from XML. This is called as the last phase
9569 * of inflation, after all child views have been added.
9570 *
9571 * <p>Even if the subclass overrides onFinishInflate, they should always be
9572 * sure to call the super method, so that we get called.
9573 */
9574 protected void onFinishInflate() {
9575 }
9576
9577 /**
9578 * Returns the resources associated with this view.
9579 *
9580 * @return Resources object.
9581 */
9582 public Resources getResources() {
9583 return mResources;
9584 }
9585
9586 /**
9587 * Invalidates the specified Drawable.
9588 *
9589 * @param drawable the drawable to invalidate
9590 */
9591 public void invalidateDrawable(Drawable drawable) {
9592 if (verifyDrawable(drawable)) {
9593 final Rect dirty = drawable.getBounds();
9594 final int scrollX = mScrollX;
9595 final int scrollY = mScrollY;
9596
9597 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9598 dirty.right + scrollX, dirty.bottom + scrollY);
9599 }
9600 }
9601
9602 /**
9603 * Schedules an action on a drawable to occur at a specified time.
9604 *
9605 * @param who the recipient of the action
9606 * @param what the action to run on the drawable
9607 * @param when the time at which the action must occur. Uses the
9608 * {@link SystemClock#uptimeMillis} timebase.
9609 */
9610 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9611 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9612 mAttachInfo.mHandler.postAtTime(what, who, when);
9613 }
9614 }
9615
9616 /**
9617 * Cancels a scheduled action on a drawable.
9618 *
9619 * @param who the recipient of the action
9620 * @param what the action to cancel
9621 */
9622 public void unscheduleDrawable(Drawable who, Runnable what) {
9623 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9624 mAttachInfo.mHandler.removeCallbacks(what, who);
9625 }
9626 }
9627
9628 /**
9629 * Unschedule any events associated with the given Drawable. This can be
9630 * used when selecting a new Drawable into a view, so that the previous
9631 * one is completely unscheduled.
9632 *
9633 * @param who The Drawable to unschedule.
9634 *
9635 * @see #drawableStateChanged
9636 */
9637 public void unscheduleDrawable(Drawable who) {
9638 if (mAttachInfo != null) {
9639 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9640 }
9641 }
9642
9643 /**
9644 * If your view subclass is displaying its own Drawable objects, it should
9645 * override this function and return true for any Drawable it is
9646 * displaying. This allows animations for those drawables to be
9647 * scheduled.
9648 *
9649 * <p>Be sure to call through to the super class when overriding this
9650 * function.
9651 *
9652 * @param who The Drawable to verify. Return true if it is one you are
9653 * displaying, else return the result of calling through to the
9654 * super class.
9655 *
9656 * @return boolean If true than the Drawable is being displayed in the
9657 * view; else false and it is not allowed to animate.
9658 *
9659 * @see #unscheduleDrawable
9660 * @see #drawableStateChanged
9661 */
9662 protected boolean verifyDrawable(Drawable who) {
9663 return who == mBGDrawable;
9664 }
9665
9666 /**
9667 * This function is called whenever the state of the view changes in such
9668 * a way that it impacts the state of drawables being shown.
9669 *
9670 * <p>Be sure to call through to the superclass when overriding this
9671 * function.
9672 *
9673 * @see Drawable#setState
9674 */
9675 protected void drawableStateChanged() {
9676 Drawable d = mBGDrawable;
9677 if (d != null && d.isStateful()) {
9678 d.setState(getDrawableState());
9679 }
9680 }
9681
9682 /**
9683 * Call this to force a view to update its drawable state. This will cause
9684 * drawableStateChanged to be called on this view. Views that are interested
9685 * in the new state should call getDrawableState.
9686 *
9687 * @see #drawableStateChanged
9688 * @see #getDrawableState
9689 */
9690 public void refreshDrawableState() {
9691 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9692 drawableStateChanged();
9693
9694 ViewParent parent = mParent;
9695 if (parent != null) {
9696 parent.childDrawableStateChanged(this);
9697 }
9698 }
9699
9700 /**
9701 * Return an array of resource IDs of the drawable states representing the
9702 * current state of the view.
9703 *
9704 * @return The current drawable state
9705 *
9706 * @see Drawable#setState
9707 * @see #drawableStateChanged
9708 * @see #onCreateDrawableState
9709 */
9710 public final int[] getDrawableState() {
9711 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9712 return mDrawableState;
9713 } else {
9714 mDrawableState = onCreateDrawableState(0);
9715 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9716 return mDrawableState;
9717 }
9718 }
9719
9720 /**
9721 * Generate the new {@link android.graphics.drawable.Drawable} state for
9722 * this view. This is called by the view
9723 * system when the cached Drawable state is determined to be invalid. To
9724 * retrieve the current state, you should use {@link #getDrawableState}.
9725 *
9726 * @param extraSpace if non-zero, this is the number of extra entries you
9727 * would like in the returned array in which you can place your own
9728 * states.
9729 *
9730 * @return Returns an array holding the current {@link Drawable} state of
9731 * the view.
9732 *
9733 * @see #mergeDrawableStates
9734 */
9735 protected int[] onCreateDrawableState(int extraSpace) {
9736 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9737 mParent instanceof View) {
9738 return ((View) mParent).onCreateDrawableState(extraSpace);
9739 }
9740
9741 int[] drawableState;
9742
9743 int privateFlags = mPrivateFlags;
9744
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009745 int viewStateIndex = 0;
9746 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9747 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9748 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009749 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009750 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9751 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009752 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9753 // This is set if HW acceleration is requested, even if the current
9754 // process doesn't allow it. This is just to allow app preview
9755 // windows to better match their app.
9756 viewStateIndex |= VIEW_STATE_ACCELERATED;
9757 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758
9759 drawableState = VIEW_STATE_SETS[viewStateIndex];
9760
9761 //noinspection ConstantIfStatement
9762 if (false) {
9763 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9764 Log.i("View", toString()
9765 + " pressed=" + ((privateFlags & PRESSED) != 0)
9766 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9767 + " fo=" + hasFocus()
9768 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009769 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009770 + ": " + Arrays.toString(drawableState));
9771 }
9772
9773 if (extraSpace == 0) {
9774 return drawableState;
9775 }
9776
9777 final int[] fullState;
9778 if (drawableState != null) {
9779 fullState = new int[drawableState.length + extraSpace];
9780 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9781 } else {
9782 fullState = new int[extraSpace];
9783 }
9784
9785 return fullState;
9786 }
9787
9788 /**
9789 * Merge your own state values in <var>additionalState</var> into the base
9790 * state values <var>baseState</var> that were returned by
9791 * {@link #onCreateDrawableState}.
9792 *
9793 * @param baseState The base state values returned by
9794 * {@link #onCreateDrawableState}, which will be modified to also hold your
9795 * own additional state values.
9796 *
9797 * @param additionalState The additional state values you would like
9798 * added to <var>baseState</var>; this array is not modified.
9799 *
9800 * @return As a convenience, the <var>baseState</var> array you originally
9801 * passed into the function is returned.
9802 *
9803 * @see #onCreateDrawableState
9804 */
9805 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9806 final int N = baseState.length;
9807 int i = N - 1;
9808 while (i >= 0 && baseState[i] == 0) {
9809 i--;
9810 }
9811 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9812 return baseState;
9813 }
9814
9815 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009816 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9817 * on all Drawable objects associated with this view.
9818 */
9819 public void jumpDrawablesToCurrentState() {
9820 if (mBGDrawable != null) {
9821 mBGDrawable.jumpToCurrentState();
9822 }
9823 }
9824
9825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009826 * Sets the background color for this view.
9827 * @param color the color of the background
9828 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009829 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009830 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009831 if (mBGDrawable instanceof ColorDrawable) {
9832 ((ColorDrawable) mBGDrawable).setColor(color);
9833 } else {
9834 setBackgroundDrawable(new ColorDrawable(color));
9835 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009836 }
9837
9838 /**
9839 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009840 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009841 * @param resid The identifier of the resource.
9842 * @attr ref android.R.styleable#View_background
9843 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009844 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009845 public void setBackgroundResource(int resid) {
9846 if (resid != 0 && resid == mBackgroundResource) {
9847 return;
9848 }
9849
9850 Drawable d= null;
9851 if (resid != 0) {
9852 d = mResources.getDrawable(resid);
9853 }
9854 setBackgroundDrawable(d);
9855
9856 mBackgroundResource = resid;
9857 }
9858
9859 /**
9860 * Set the background to a given Drawable, or remove the background. If the
9861 * background has padding, this View's padding is set to the background's
9862 * padding. However, when a background is removed, this View's padding isn't
9863 * touched. If setting the padding is desired, please use
9864 * {@link #setPadding(int, int, int, int)}.
9865 *
9866 * @param d The Drawable to use as the background, or null to remove the
9867 * background
9868 */
9869 public void setBackgroundDrawable(Drawable d) {
9870 boolean requestLayout = false;
9871
9872 mBackgroundResource = 0;
9873
9874 /*
9875 * Regardless of whether we're setting a new background or not, we want
9876 * to clear the previous drawable.
9877 */
9878 if (mBGDrawable != null) {
9879 mBGDrawable.setCallback(null);
9880 unscheduleDrawable(mBGDrawable);
9881 }
9882
9883 if (d != null) {
9884 Rect padding = sThreadLocal.get();
9885 if (padding == null) {
9886 padding = new Rect();
9887 sThreadLocal.set(padding);
9888 }
9889 if (d.getPadding(padding)) {
9890 setPadding(padding.left, padding.top, padding.right, padding.bottom);
9891 }
9892
9893 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
9894 // if it has a different minimum size, we should layout again
9895 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
9896 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
9897 requestLayout = true;
9898 }
9899
9900 d.setCallback(this);
9901 if (d.isStateful()) {
9902 d.setState(getDrawableState());
9903 }
9904 d.setVisible(getVisibility() == VISIBLE, false);
9905 mBGDrawable = d;
9906
9907 if ((mPrivateFlags & SKIP_DRAW) != 0) {
9908 mPrivateFlags &= ~SKIP_DRAW;
9909 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
9910 requestLayout = true;
9911 }
9912 } else {
9913 /* Remove the background */
9914 mBGDrawable = null;
9915
9916 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
9917 /*
9918 * This view ONLY drew the background before and we're removing
9919 * the background, so now it won't draw anything
9920 * (hence we SKIP_DRAW)
9921 */
9922 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
9923 mPrivateFlags |= SKIP_DRAW;
9924 }
9925
9926 /*
9927 * When the background is set, we try to apply its padding to this
9928 * View. When the background is removed, we don't touch this View's
9929 * padding. This is noted in the Javadocs. Hence, we don't need to
9930 * requestLayout(), the invalidate() below is sufficient.
9931 */
9932
9933 // The old background's minimum size could have affected this
9934 // View's layout, so let's requestLayout
9935 requestLayout = true;
9936 }
9937
Romain Guy8f1344f52009-05-15 16:03:59 -07009938 computeOpaqueFlags();
9939
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009940 if (requestLayout) {
9941 requestLayout();
9942 }
9943
9944 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009945 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 }
9947
9948 /**
9949 * Gets the background drawable
9950 * @return The drawable used as the background for this view, if any.
9951 */
9952 public Drawable getBackground() {
9953 return mBGDrawable;
9954 }
9955
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009956 /**
9957 * Sets the padding. The view may add on the space required to display
9958 * the scrollbars, depending on the style and visibility of the scrollbars.
9959 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
9960 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
9961 * from the values set in this call.
9962 *
9963 * @attr ref android.R.styleable#View_padding
9964 * @attr ref android.R.styleable#View_paddingBottom
9965 * @attr ref android.R.styleable#View_paddingLeft
9966 * @attr ref android.R.styleable#View_paddingRight
9967 * @attr ref android.R.styleable#View_paddingTop
9968 * @param left the left padding in pixels
9969 * @param top the top padding in pixels
9970 * @param right the right padding in pixels
9971 * @param bottom the bottom padding in pixels
9972 */
9973 public void setPadding(int left, int top, int right, int bottom) {
9974 boolean changed = false;
9975
Adam Powell20232d02010-12-08 21:08:53 -08009976 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009977 mUserPaddingRight = right;
9978 mUserPaddingBottom = bottom;
9979
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009980 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07009981
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009982 // Common case is there are no scroll bars.
9983 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009984 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -08009985 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
9986 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009987 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -08009988 switch (mVerticalScrollbarPosition) {
9989 case SCROLLBAR_POSITION_DEFAULT:
9990 case SCROLLBAR_POSITION_RIGHT:
9991 right += offset;
9992 break;
9993 case SCROLLBAR_POSITION_LEFT:
9994 left += offset;
9995 break;
9996 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009997 }
Adam Powell20232d02010-12-08 21:08:53 -08009998 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009999 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10000 ? 0 : getHorizontalScrollbarHeight();
10001 }
10002 }
Romain Guy8506ab42009-06-11 17:35:47 -070010003
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010004 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005 changed = true;
10006 mPaddingLeft = left;
10007 }
10008 if (mPaddingTop != top) {
10009 changed = true;
10010 mPaddingTop = top;
10011 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010012 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010014 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010016 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010017 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010018 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 }
10020
10021 if (changed) {
10022 requestLayout();
10023 }
10024 }
10025
10026 /**
10027 * Returns the top padding of this view.
10028 *
10029 * @return the top padding in pixels
10030 */
10031 public int getPaddingTop() {
10032 return mPaddingTop;
10033 }
10034
10035 /**
10036 * Returns the bottom padding of this view. If there are inset and enabled
10037 * scrollbars, this value may include the space required to display the
10038 * scrollbars as well.
10039 *
10040 * @return the bottom padding in pixels
10041 */
10042 public int getPaddingBottom() {
10043 return mPaddingBottom;
10044 }
10045
10046 /**
10047 * Returns the left padding of this view. If there are inset and enabled
10048 * scrollbars, this value may include the space required to display the
10049 * scrollbars as well.
10050 *
10051 * @return the left padding in pixels
10052 */
10053 public int getPaddingLeft() {
10054 return mPaddingLeft;
10055 }
10056
10057 /**
10058 * Returns the right padding of this view. If there are inset and enabled
10059 * scrollbars, this value may include the space required to display the
10060 * scrollbars as well.
10061 *
10062 * @return the right padding in pixels
10063 */
10064 public int getPaddingRight() {
10065 return mPaddingRight;
10066 }
10067
10068 /**
10069 * Changes the selection state of this view. A view can be selected or not.
10070 * Note that selection is not the same as focus. Views are typically
10071 * selected in the context of an AdapterView like ListView or GridView;
10072 * the selected view is the view that is highlighted.
10073 *
10074 * @param selected true if the view must be selected, false otherwise
10075 */
10076 public void setSelected(boolean selected) {
10077 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10078 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010079 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010080 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010081 refreshDrawableState();
10082 dispatchSetSelected(selected);
10083 }
10084 }
10085
10086 /**
10087 * Dispatch setSelected to all of this View's children.
10088 *
10089 * @see #setSelected(boolean)
10090 *
10091 * @param selected The new selected state
10092 */
10093 protected void dispatchSetSelected(boolean selected) {
10094 }
10095
10096 /**
10097 * Indicates the selection state of this view.
10098 *
10099 * @return true if the view is selected, false otherwise
10100 */
10101 @ViewDebug.ExportedProperty
10102 public boolean isSelected() {
10103 return (mPrivateFlags & SELECTED) != 0;
10104 }
10105
10106 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010107 * Changes the activated state of this view. A view can be activated or not.
10108 * Note that activation is not the same as selection. Selection is
10109 * a transient property, representing the view (hierarchy) the user is
10110 * currently interacting with. Activation is a longer-term state that the
10111 * user can move views in and out of. For example, in a list view with
10112 * single or multiple selection enabled, the views in the current selection
10113 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10114 * here.) The activated state is propagated down to children of the view it
10115 * is set on.
10116 *
10117 * @param activated true if the view must be activated, false otherwise
10118 */
10119 public void setActivated(boolean activated) {
10120 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10121 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010122 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010123 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010124 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010125 }
10126 }
10127
10128 /**
10129 * Dispatch setActivated to all of this View's children.
10130 *
10131 * @see #setActivated(boolean)
10132 *
10133 * @param activated The new activated state
10134 */
10135 protected void dispatchSetActivated(boolean activated) {
10136 }
10137
10138 /**
10139 * Indicates the activation state of this view.
10140 *
10141 * @return true if the view is activated, false otherwise
10142 */
10143 @ViewDebug.ExportedProperty
10144 public boolean isActivated() {
10145 return (mPrivateFlags & ACTIVATED) != 0;
10146 }
10147
10148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010149 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10150 * observer can be used to get notifications when global events, like
10151 * layout, happen.
10152 *
10153 * The returned ViewTreeObserver observer is not guaranteed to remain
10154 * valid for the lifetime of this View. If the caller of this method keeps
10155 * a long-lived reference to ViewTreeObserver, it should always check for
10156 * the return value of {@link ViewTreeObserver#isAlive()}.
10157 *
10158 * @return The ViewTreeObserver for this view's hierarchy.
10159 */
10160 public ViewTreeObserver getViewTreeObserver() {
10161 if (mAttachInfo != null) {
10162 return mAttachInfo.mTreeObserver;
10163 }
10164 if (mFloatingTreeObserver == null) {
10165 mFloatingTreeObserver = new ViewTreeObserver();
10166 }
10167 return mFloatingTreeObserver;
10168 }
10169
10170 /**
10171 * <p>Finds the topmost view in the current view hierarchy.</p>
10172 *
10173 * @return the topmost view containing this view
10174 */
10175 public View getRootView() {
10176 if (mAttachInfo != null) {
10177 final View v = mAttachInfo.mRootView;
10178 if (v != null) {
10179 return v;
10180 }
10181 }
Romain Guy8506ab42009-06-11 17:35:47 -070010182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010183 View parent = this;
10184
10185 while (parent.mParent != null && parent.mParent instanceof View) {
10186 parent = (View) parent.mParent;
10187 }
10188
10189 return parent;
10190 }
10191
10192 /**
10193 * <p>Computes the coordinates of this view on the screen. The argument
10194 * must be an array of two integers. After the method returns, the array
10195 * contains the x and y location in that order.</p>
10196 *
10197 * @param location an array of two integers in which to hold the coordinates
10198 */
10199 public void getLocationOnScreen(int[] location) {
10200 getLocationInWindow(location);
10201
10202 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010203 if (info != null) {
10204 location[0] += info.mWindowLeft;
10205 location[1] += info.mWindowTop;
10206 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010207 }
10208
10209 /**
10210 * <p>Computes the coordinates of this view in its window. The argument
10211 * must be an array of two integers. After the method returns, the array
10212 * contains the x and y location in that order.</p>
10213 *
10214 * @param location an array of two integers in which to hold the coordinates
10215 */
10216 public void getLocationInWindow(int[] location) {
10217 if (location == null || location.length < 2) {
10218 throw new IllegalArgumentException("location must be an array of "
10219 + "two integers");
10220 }
10221
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010222 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10223 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224
10225 ViewParent viewParent = mParent;
10226 while (viewParent instanceof View) {
10227 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010228 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10229 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010230 viewParent = view.mParent;
10231 }
Romain Guy8506ab42009-06-11 17:35:47 -070010232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010233 if (viewParent instanceof ViewRoot) {
10234 // *cough*
10235 final ViewRoot vr = (ViewRoot)viewParent;
10236 location[1] -= vr.mCurScrollY;
10237 }
10238 }
10239
10240 /**
10241 * {@hide}
10242 * @param id the id of the view to be found
10243 * @return the view of the specified id, null if cannot be found
10244 */
10245 protected View findViewTraversal(int id) {
10246 if (id == mID) {
10247 return this;
10248 }
10249 return null;
10250 }
10251
10252 /**
10253 * {@hide}
10254 * @param tag the tag of the view to be found
10255 * @return the view of specified tag, null if cannot be found
10256 */
10257 protected View findViewWithTagTraversal(Object tag) {
10258 if (tag != null && tag.equals(mTag)) {
10259 return this;
10260 }
10261 return null;
10262 }
10263
10264 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010265 * {@hide}
10266 * @param predicate The predicate to evaluate.
10267 * @return The first view that matches the predicate or null.
10268 */
10269 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10270 if (predicate.apply(this)) {
10271 return this;
10272 }
10273 return null;
10274 }
10275
10276 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010277 * Look for a child view with the given id. If this view has the given
10278 * id, return this view.
10279 *
10280 * @param id The id to search for.
10281 * @return The view that has the given id in the hierarchy or null
10282 */
10283 public final View findViewById(int id) {
10284 if (id < 0) {
10285 return null;
10286 }
10287 return findViewTraversal(id);
10288 }
10289
10290 /**
10291 * Look for a child view with the given tag. If this view has the given
10292 * tag, return this view.
10293 *
10294 * @param tag The tag to search for, using "tag.equals(getTag())".
10295 * @return The View that has the given tag in the hierarchy or null
10296 */
10297 public final View findViewWithTag(Object tag) {
10298 if (tag == null) {
10299 return null;
10300 }
10301 return findViewWithTagTraversal(tag);
10302 }
10303
10304 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010305 * {@hide}
10306 * Look for a child view that matches the specified predicate.
10307 * If this view matches the predicate, return this view.
10308 *
10309 * @param predicate The predicate to evaluate.
10310 * @return The first view that matches the predicate or null.
10311 */
10312 public final View findViewByPredicate(Predicate<View> predicate) {
10313 return findViewByPredicateTraversal(predicate);
10314 }
10315
10316 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010317 * Sets the identifier for this view. The identifier does not have to be
10318 * unique in this view's hierarchy. The identifier should be a positive
10319 * number.
10320 *
10321 * @see #NO_ID
10322 * @see #getId
10323 * @see #findViewById
10324 *
10325 * @param id a number used to identify the view
10326 *
10327 * @attr ref android.R.styleable#View_id
10328 */
10329 public void setId(int id) {
10330 mID = id;
10331 }
10332
10333 /**
10334 * {@hide}
10335 *
10336 * @param isRoot true if the view belongs to the root namespace, false
10337 * otherwise
10338 */
10339 public void setIsRootNamespace(boolean isRoot) {
10340 if (isRoot) {
10341 mPrivateFlags |= IS_ROOT_NAMESPACE;
10342 } else {
10343 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10344 }
10345 }
10346
10347 /**
10348 * {@hide}
10349 *
10350 * @return true if the view belongs to the root namespace, false otherwise
10351 */
10352 public boolean isRootNamespace() {
10353 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10354 }
10355
10356 /**
10357 * Returns this view's identifier.
10358 *
10359 * @return a positive integer used to identify the view or {@link #NO_ID}
10360 * if the view has no ID
10361 *
10362 * @see #setId
10363 * @see #findViewById
10364 * @attr ref android.R.styleable#View_id
10365 */
10366 @ViewDebug.CapturedViewProperty
10367 public int getId() {
10368 return mID;
10369 }
10370
10371 /**
10372 * Returns this view's tag.
10373 *
10374 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010375 *
10376 * @see #setTag(Object)
10377 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010378 */
10379 @ViewDebug.ExportedProperty
10380 public Object getTag() {
10381 return mTag;
10382 }
10383
10384 /**
10385 * Sets the tag associated with this view. A tag can be used to mark
10386 * a view in its hierarchy and does not have to be unique within the
10387 * hierarchy. Tags can also be used to store data within a view without
10388 * resorting to another data structure.
10389 *
10390 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010391 *
10392 * @see #getTag()
10393 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 */
10395 public void setTag(final Object tag) {
10396 mTag = tag;
10397 }
10398
10399 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010400 * Returns the tag associated with this view and the specified key.
10401 *
10402 * @param key The key identifying the tag
10403 *
10404 * @return the Object stored in this view as a tag
10405 *
10406 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010407 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010408 */
10409 public Object getTag(int key) {
10410 SparseArray<Object> tags = null;
10411 synchronized (sTagsLock) {
10412 if (sTags != null) {
10413 tags = sTags.get(this);
10414 }
10415 }
10416
10417 if (tags != null) return tags.get(key);
10418 return null;
10419 }
10420
10421 /**
10422 * Sets a tag associated with this view and a key. A tag can be used
10423 * to mark a view in its hierarchy and does not have to be unique within
10424 * the hierarchy. Tags can also be used to store data within a view
10425 * without resorting to another data structure.
10426 *
10427 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010428 * application to ensure it is unique (see the <a
10429 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10430 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010431 * the Android framework or not associated with any package will cause
10432 * an {@link IllegalArgumentException} to be thrown.
10433 *
10434 * @param key The key identifying the tag
10435 * @param tag An Object to tag the view with
10436 *
10437 * @throws IllegalArgumentException If they specified key is not valid
10438 *
10439 * @see #setTag(Object)
10440 * @see #getTag(int)
10441 */
10442 public void setTag(int key, final Object tag) {
10443 // If the package id is 0x00 or 0x01, it's either an undefined package
10444 // or a framework id
10445 if ((key >>> 24) < 2) {
10446 throw new IllegalArgumentException("The key must be an application-specific "
10447 + "resource id.");
10448 }
10449
10450 setTagInternal(this, key, tag);
10451 }
10452
10453 /**
10454 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10455 * framework id.
10456 *
10457 * @hide
10458 */
10459 public void setTagInternal(int key, Object tag) {
10460 if ((key >>> 24) != 0x1) {
10461 throw new IllegalArgumentException("The key must be a framework-specific "
10462 + "resource id.");
10463 }
10464
Romain Guy8506ab42009-06-11 17:35:47 -070010465 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010466 }
10467
10468 private static void setTagInternal(View view, int key, Object tag) {
10469 SparseArray<Object> tags = null;
10470 synchronized (sTagsLock) {
10471 if (sTags == null) {
10472 sTags = new WeakHashMap<View, SparseArray<Object>>();
10473 } else {
10474 tags = sTags.get(view);
10475 }
10476 }
10477
10478 if (tags == null) {
10479 tags = new SparseArray<Object>(2);
10480 synchronized (sTagsLock) {
10481 sTags.put(view, tags);
10482 }
10483 }
10484
10485 tags.put(key, tag);
10486 }
10487
10488 /**
Romain Guy13922e02009-05-12 17:56:14 -070010489 * @param consistency The type of consistency. See ViewDebug for more information.
10490 *
10491 * @hide
10492 */
10493 protected boolean dispatchConsistencyCheck(int consistency) {
10494 return onConsistencyCheck(consistency);
10495 }
10496
10497 /**
10498 * Method that subclasses should implement to check their consistency. The type of
10499 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010500 *
Romain Guy13922e02009-05-12 17:56:14 -070010501 * @param consistency The type of consistency. See ViewDebug for more information.
10502 *
10503 * @throws IllegalStateException if the view is in an inconsistent state.
10504 *
10505 * @hide
10506 */
10507 protected boolean onConsistencyCheck(int consistency) {
10508 boolean result = true;
10509
10510 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10511 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10512
10513 if (checkLayout) {
10514 if (getParent() == null) {
10515 result = false;
10516 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10517 "View " + this + " does not have a parent.");
10518 }
10519
10520 if (mAttachInfo == null) {
10521 result = false;
10522 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10523 "View " + this + " is not attached to a window.");
10524 }
10525 }
10526
10527 if (checkDrawing) {
10528 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10529 // from their draw() method
10530
10531 if ((mPrivateFlags & DRAWN) != DRAWN &&
10532 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10533 result = false;
10534 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10535 "View " + this + " was invalidated but its drawing cache is valid.");
10536 }
10537 }
10538
10539 return result;
10540 }
10541
10542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 * Prints information about this view in the log output, with the tag
10544 * {@link #VIEW_LOG_TAG}.
10545 *
10546 * @hide
10547 */
10548 public void debug() {
10549 debug(0);
10550 }
10551
10552 /**
10553 * Prints information about this view in the log output, with the tag
10554 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10555 * indentation defined by the <code>depth</code>.
10556 *
10557 * @param depth the indentation level
10558 *
10559 * @hide
10560 */
10561 protected void debug(int depth) {
10562 String output = debugIndent(depth - 1);
10563
10564 output += "+ " + this;
10565 int id = getId();
10566 if (id != -1) {
10567 output += " (id=" + id + ")";
10568 }
10569 Object tag = getTag();
10570 if (tag != null) {
10571 output += " (tag=" + tag + ")";
10572 }
10573 Log.d(VIEW_LOG_TAG, output);
10574
10575 if ((mPrivateFlags & FOCUSED) != 0) {
10576 output = debugIndent(depth) + " FOCUSED";
10577 Log.d(VIEW_LOG_TAG, output);
10578 }
10579
10580 output = debugIndent(depth);
10581 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10582 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10583 + "} ";
10584 Log.d(VIEW_LOG_TAG, output);
10585
10586 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10587 || mPaddingBottom != 0) {
10588 output = debugIndent(depth);
10589 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10590 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10591 Log.d(VIEW_LOG_TAG, output);
10592 }
10593
10594 output = debugIndent(depth);
10595 output += "mMeasureWidth=" + mMeasuredWidth +
10596 " mMeasureHeight=" + mMeasuredHeight;
10597 Log.d(VIEW_LOG_TAG, output);
10598
10599 output = debugIndent(depth);
10600 if (mLayoutParams == null) {
10601 output += "BAD! no layout params";
10602 } else {
10603 output = mLayoutParams.debug(output);
10604 }
10605 Log.d(VIEW_LOG_TAG, output);
10606
10607 output = debugIndent(depth);
10608 output += "flags={";
10609 output += View.printFlags(mViewFlags);
10610 output += "}";
10611 Log.d(VIEW_LOG_TAG, output);
10612
10613 output = debugIndent(depth);
10614 output += "privateFlags={";
10615 output += View.printPrivateFlags(mPrivateFlags);
10616 output += "}";
10617 Log.d(VIEW_LOG_TAG, output);
10618 }
10619
10620 /**
10621 * Creates an string of whitespaces used for indentation.
10622 *
10623 * @param depth the indentation level
10624 * @return a String containing (depth * 2 + 3) * 2 white spaces
10625 *
10626 * @hide
10627 */
10628 protected static String debugIndent(int depth) {
10629 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10630 for (int i = 0; i < (depth * 2) + 3; i++) {
10631 spaces.append(' ').append(' ');
10632 }
10633 return spaces.toString();
10634 }
10635
10636 /**
10637 * <p>Return the offset of the widget's text baseline from the widget's top
10638 * boundary. If this widget does not support baseline alignment, this
10639 * method returns -1. </p>
10640 *
10641 * @return the offset of the baseline within the widget's bounds or -1
10642 * if baseline alignment is not supported
10643 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010644 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010645 public int getBaseline() {
10646 return -1;
10647 }
10648
10649 /**
10650 * Call this when something has changed which has invalidated the
10651 * layout of this view. This will schedule a layout pass of the view
10652 * tree.
10653 */
10654 public void requestLayout() {
10655 if (ViewDebug.TRACE_HIERARCHY) {
10656 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10657 }
10658
10659 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010660 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010661
10662 if (mParent != null && !mParent.isLayoutRequested()) {
10663 mParent.requestLayout();
10664 }
10665 }
10666
10667 /**
10668 * Forces this view to be laid out during the next layout pass.
10669 * This method does not call requestLayout() or forceLayout()
10670 * on the parent.
10671 */
10672 public void forceLayout() {
10673 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010674 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675 }
10676
10677 /**
10678 * <p>
10679 * This is called to find out how big a view should be. The parent
10680 * supplies constraint information in the width and height parameters.
10681 * </p>
10682 *
10683 * <p>
10684 * The actual mesurement work of a view is performed in
10685 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10686 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10687 * </p>
10688 *
10689 *
10690 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10691 * parent
10692 * @param heightMeasureSpec Vertical space requirements as imposed by the
10693 * parent
10694 *
10695 * @see #onMeasure(int, int)
10696 */
10697 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10698 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10699 widthMeasureSpec != mOldWidthMeasureSpec ||
10700 heightMeasureSpec != mOldHeightMeasureSpec) {
10701
10702 // first clears the measured dimension flag
10703 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10704
10705 if (ViewDebug.TRACE_HIERARCHY) {
10706 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10707 }
10708
10709 // measure ourselves, this should set the measured dimension flag back
10710 onMeasure(widthMeasureSpec, heightMeasureSpec);
10711
10712 // flag not set, setMeasuredDimension() was not invoked, we raise
10713 // an exception to warn the developer
10714 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10715 throw new IllegalStateException("onMeasure() did not set the"
10716 + " measured dimension by calling"
10717 + " setMeasuredDimension()");
10718 }
10719
10720 mPrivateFlags |= LAYOUT_REQUIRED;
10721 }
10722
10723 mOldWidthMeasureSpec = widthMeasureSpec;
10724 mOldHeightMeasureSpec = heightMeasureSpec;
10725 }
10726
10727 /**
10728 * <p>
10729 * Measure the view and its content to determine the measured width and the
10730 * measured height. This method is invoked by {@link #measure(int, int)} and
10731 * should be overriden by subclasses to provide accurate and efficient
10732 * measurement of their contents.
10733 * </p>
10734 *
10735 * <p>
10736 * <strong>CONTRACT:</strong> When overriding this method, you
10737 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10738 * measured width and height of this view. Failure to do so will trigger an
10739 * <code>IllegalStateException</code>, thrown by
10740 * {@link #measure(int, int)}. Calling the superclass'
10741 * {@link #onMeasure(int, int)} is a valid use.
10742 * </p>
10743 *
10744 * <p>
10745 * The base class implementation of measure defaults to the background size,
10746 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10747 * override {@link #onMeasure(int, int)} to provide better measurements of
10748 * their content.
10749 * </p>
10750 *
10751 * <p>
10752 * If this method is overridden, it is the subclass's responsibility to make
10753 * sure the measured height and width are at least the view's minimum height
10754 * and width ({@link #getSuggestedMinimumHeight()} and
10755 * {@link #getSuggestedMinimumWidth()}).
10756 * </p>
10757 *
10758 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10759 * The requirements are encoded with
10760 * {@link android.view.View.MeasureSpec}.
10761 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10762 * The requirements are encoded with
10763 * {@link android.view.View.MeasureSpec}.
10764 *
10765 * @see #getMeasuredWidth()
10766 * @see #getMeasuredHeight()
10767 * @see #setMeasuredDimension(int, int)
10768 * @see #getSuggestedMinimumHeight()
10769 * @see #getSuggestedMinimumWidth()
10770 * @see android.view.View.MeasureSpec#getMode(int)
10771 * @see android.view.View.MeasureSpec#getSize(int)
10772 */
10773 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10774 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10775 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10776 }
10777
10778 /**
10779 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10780 * measured width and measured height. Failing to do so will trigger an
10781 * exception at measurement time.</p>
10782 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010783 * @param measuredWidth The measured width of this view. May be a complex
10784 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10785 * {@link #MEASURED_STATE_TOO_SMALL}.
10786 * @param measuredHeight The measured height of this view. May be a complex
10787 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10788 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010789 */
10790 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10791 mMeasuredWidth = measuredWidth;
10792 mMeasuredHeight = measuredHeight;
10793
10794 mPrivateFlags |= MEASURED_DIMENSION_SET;
10795 }
10796
10797 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010798 * Merge two states as returned by {@link #getMeasuredState()}.
10799 * @param curState The current state as returned from a view or the result
10800 * of combining multiple views.
10801 * @param newState The new view state to combine.
10802 * @return Returns a new integer reflecting the combination of the two
10803 * states.
10804 */
10805 public static int combineMeasuredStates(int curState, int newState) {
10806 return curState | newState;
10807 }
10808
10809 /**
10810 * Version of {@link #resolveSizeAndState(int, int, int)}
10811 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10812 */
10813 public static int resolveSize(int size, int measureSpec) {
10814 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10815 }
10816
10817 /**
10818 * Utility to reconcile a desired size and state, with constraints imposed
10819 * by a MeasureSpec. Will take the desired size, unless a different size
10820 * is imposed by the constraints. The returned value is a compound integer,
10821 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10822 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10823 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010824 *
10825 * @param size How big the view wants to be
10826 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010827 * @return Size information bit mask as defined by
10828 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010829 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010830 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010831 int result = size;
10832 int specMode = MeasureSpec.getMode(measureSpec);
10833 int specSize = MeasureSpec.getSize(measureSpec);
10834 switch (specMode) {
10835 case MeasureSpec.UNSPECIFIED:
10836 result = size;
10837 break;
10838 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010839 if (specSize < size) {
10840 result = specSize | MEASURED_STATE_TOO_SMALL;
10841 } else {
10842 result = size;
10843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010844 break;
10845 case MeasureSpec.EXACTLY:
10846 result = specSize;
10847 break;
10848 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010849 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 }
10851
10852 /**
10853 * Utility to return a default size. Uses the supplied size if the
10854 * MeasureSpec imposed no contraints. Will get larger if allowed
10855 * by the MeasureSpec.
10856 *
10857 * @param size Default size for this view
10858 * @param measureSpec Constraints imposed by the parent
10859 * @return The size this view should be.
10860 */
10861 public static int getDefaultSize(int size, int measureSpec) {
10862 int result = size;
10863 int specMode = MeasureSpec.getMode(measureSpec);
10864 int specSize = MeasureSpec.getSize(measureSpec);
10865
10866 switch (specMode) {
10867 case MeasureSpec.UNSPECIFIED:
10868 result = size;
10869 break;
10870 case MeasureSpec.AT_MOST:
10871 case MeasureSpec.EXACTLY:
10872 result = specSize;
10873 break;
10874 }
10875 return result;
10876 }
10877
10878 /**
10879 * Returns the suggested minimum height that the view should use. This
10880 * returns the maximum of the view's minimum height
10881 * and the background's minimum height
10882 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
10883 * <p>
10884 * When being used in {@link #onMeasure(int, int)}, the caller should still
10885 * ensure the returned height is within the requirements of the parent.
10886 *
10887 * @return The suggested minimum height of the view.
10888 */
10889 protected int getSuggestedMinimumHeight() {
10890 int suggestedMinHeight = mMinHeight;
10891
10892 if (mBGDrawable != null) {
10893 final int bgMinHeight = mBGDrawable.getMinimumHeight();
10894 if (suggestedMinHeight < bgMinHeight) {
10895 suggestedMinHeight = bgMinHeight;
10896 }
10897 }
10898
10899 return suggestedMinHeight;
10900 }
10901
10902 /**
10903 * Returns the suggested minimum width that the view should use. This
10904 * returns the maximum of the view's minimum width)
10905 * and the background's minimum width
10906 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
10907 * <p>
10908 * When being used in {@link #onMeasure(int, int)}, the caller should still
10909 * ensure the returned width is within the requirements of the parent.
10910 *
10911 * @return The suggested minimum width of the view.
10912 */
10913 protected int getSuggestedMinimumWidth() {
10914 int suggestedMinWidth = mMinWidth;
10915
10916 if (mBGDrawable != null) {
10917 final int bgMinWidth = mBGDrawable.getMinimumWidth();
10918 if (suggestedMinWidth < bgMinWidth) {
10919 suggestedMinWidth = bgMinWidth;
10920 }
10921 }
10922
10923 return suggestedMinWidth;
10924 }
10925
10926 /**
10927 * Sets the minimum height of the view. It is not guaranteed the view will
10928 * be able to achieve this minimum height (for example, if its parent layout
10929 * constrains it with less available height).
10930 *
10931 * @param minHeight The minimum height the view will try to be.
10932 */
10933 public void setMinimumHeight(int minHeight) {
10934 mMinHeight = minHeight;
10935 }
10936
10937 /**
10938 * Sets the minimum width of the view. It is not guaranteed the view will
10939 * be able to achieve this minimum width (for example, if its parent layout
10940 * constrains it with less available width).
10941 *
10942 * @param minWidth The minimum width the view will try to be.
10943 */
10944 public void setMinimumWidth(int minWidth) {
10945 mMinWidth = minWidth;
10946 }
10947
10948 /**
10949 * Get the animation currently associated with this view.
10950 *
10951 * @return The animation that is currently playing or
10952 * scheduled to play for this view.
10953 */
10954 public Animation getAnimation() {
10955 return mCurrentAnimation;
10956 }
10957
10958 /**
10959 * Start the specified animation now.
10960 *
10961 * @param animation the animation to start now
10962 */
10963 public void startAnimation(Animation animation) {
10964 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
10965 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010966 invalidateParentCaches();
10967 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 }
10969
10970 /**
10971 * Cancels any animations for this view.
10972 */
10973 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080010974 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080010975 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080010976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010977 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010978 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010979 }
10980
10981 /**
10982 * Sets the next animation to play for this view.
10983 * If you want the animation to play immediately, use
10984 * startAnimation. This method provides allows fine-grained
10985 * control over the start time and invalidation, but you
10986 * must make sure that 1) the animation has a start time set, and
10987 * 2) the view will be invalidated when the animation is supposed to
10988 * start.
10989 *
10990 * @param animation The next animation, or null.
10991 */
10992 public void setAnimation(Animation animation) {
10993 mCurrentAnimation = animation;
10994 if (animation != null) {
10995 animation.reset();
10996 }
10997 }
10998
10999 /**
11000 * Invoked by a parent ViewGroup to notify the start of the animation
11001 * currently associated with this view. If you override this method,
11002 * always call super.onAnimationStart();
11003 *
11004 * @see #setAnimation(android.view.animation.Animation)
11005 * @see #getAnimation()
11006 */
11007 protected void onAnimationStart() {
11008 mPrivateFlags |= ANIMATION_STARTED;
11009 }
11010
11011 /**
11012 * Invoked by a parent ViewGroup to notify the end of the animation
11013 * currently associated with this view. If you override this method,
11014 * always call super.onAnimationEnd();
11015 *
11016 * @see #setAnimation(android.view.animation.Animation)
11017 * @see #getAnimation()
11018 */
11019 protected void onAnimationEnd() {
11020 mPrivateFlags &= ~ANIMATION_STARTED;
11021 }
11022
11023 /**
11024 * Invoked if there is a Transform that involves alpha. Subclass that can
11025 * draw themselves with the specified alpha should return true, and then
11026 * respect that alpha when their onDraw() is called. If this returns false
11027 * then the view may be redirected to draw into an offscreen buffer to
11028 * fulfill the request, which will look fine, but may be slower than if the
11029 * subclass handles it internally. The default implementation returns false.
11030 *
11031 * @param alpha The alpha (0..255) to apply to the view's drawing
11032 * @return true if the view can draw with the specified alpha.
11033 */
11034 protected boolean onSetAlpha(int alpha) {
11035 return false;
11036 }
11037
11038 /**
11039 * This is used by the RootView to perform an optimization when
11040 * the view hierarchy contains one or several SurfaceView.
11041 * SurfaceView is always considered transparent, but its children are not,
11042 * therefore all View objects remove themselves from the global transparent
11043 * region (passed as a parameter to this function).
11044 *
11045 * @param region The transparent region for this ViewRoot (window).
11046 *
11047 * @return Returns true if the effective visibility of the view at this
11048 * point is opaque, regardless of the transparent region; returns false
11049 * if it is possible for underlying windows to be seen behind the view.
11050 *
11051 * {@hide}
11052 */
11053 public boolean gatherTransparentRegion(Region region) {
11054 final AttachInfo attachInfo = mAttachInfo;
11055 if (region != null && attachInfo != null) {
11056 final int pflags = mPrivateFlags;
11057 if ((pflags & SKIP_DRAW) == 0) {
11058 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11059 // remove it from the transparent region.
11060 final int[] location = attachInfo.mTransparentLocation;
11061 getLocationInWindow(location);
11062 region.op(location[0], location[1], location[0] + mRight - mLeft,
11063 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11064 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11065 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11066 // exists, so we remove the background drawable's non-transparent
11067 // parts from this transparent region.
11068 applyDrawableToTransparentRegion(mBGDrawable, region);
11069 }
11070 }
11071 return true;
11072 }
11073
11074 /**
11075 * Play a sound effect for this view.
11076 *
11077 * <p>The framework will play sound effects for some built in actions, such as
11078 * clicking, but you may wish to play these effects in your widget,
11079 * for instance, for internal navigation.
11080 *
11081 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11082 * {@link #isSoundEffectsEnabled()} is true.
11083 *
11084 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11085 */
11086 public void playSoundEffect(int soundConstant) {
11087 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11088 return;
11089 }
11090 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11091 }
11092
11093 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011094 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011095 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011096 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 *
11098 * <p>The framework will provide haptic feedback for some built in actions,
11099 * such as long presses, but you may wish to provide feedback for your
11100 * own widget.
11101 *
11102 * <p>The feedback will only be performed if
11103 * {@link #isHapticFeedbackEnabled()} is true.
11104 *
11105 * @param feedbackConstant One of the constants defined in
11106 * {@link HapticFeedbackConstants}
11107 */
11108 public boolean performHapticFeedback(int feedbackConstant) {
11109 return performHapticFeedback(feedbackConstant, 0);
11110 }
11111
11112 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011113 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011114 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011115 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011116 *
11117 * @param feedbackConstant One of the constants defined in
11118 * {@link HapticFeedbackConstants}
11119 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11120 */
11121 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11122 if (mAttachInfo == null) {
11123 return false;
11124 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011125 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011126 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011127 && !isHapticFeedbackEnabled()) {
11128 return false;
11129 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011130 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11131 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 }
11133
11134 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011135 * Request that the visibility of the status bar be changed.
11136 */
11137 public void setSystemUiVisibility(int visibility) {
11138 if (visibility != mSystemUiVisibility) {
11139 mSystemUiVisibility = visibility;
11140 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11141 mParent.recomputeViewAttributes(this);
11142 }
11143 }
11144 }
11145
11146 /**
11147 * Returns the status bar visibility that this view has requested.
11148 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011149 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011150 return mSystemUiVisibility;
11151 }
11152
11153 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11154 mOnSystemUiVisibilityChangeListener = l;
11155 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11156 mParent.recomputeViewAttributes(this);
11157 }
11158 }
11159
11160 /**
11161 */
11162 public void dispatchSystemUiVisibilityChanged(int visibility) {
11163 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011164 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011165 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011166 }
11167 }
11168
11169 /**
Joe Malin32736f02011-01-19 16:14:20 -080011170 * Creates an image that the system displays during the drag and drop
11171 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11172 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11173 * appearance as the given View. The default also positions the center of the drag shadow
11174 * directly under the touch point. If no View is provided (the constructor with no parameters
11175 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11176 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11177 * default is an invisible drag shadow.
11178 * <p>
11179 * You are not required to use the View you provide to the constructor as the basis of the
11180 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11181 * anything you want as the drag shadow.
11182 * </p>
11183 * <p>
11184 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11185 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11186 * size and position of the drag shadow. It uses this data to construct a
11187 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11188 * so that your application can draw the shadow image in the Canvas.
11189 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011190 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011191 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011192 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011193
11194 /**
Joe Malin32736f02011-01-19 16:14:20 -080011195 * Constructs a shadow image builder based on a View. By default, the resulting drag
11196 * shadow will have the same appearance and dimensions as the View, with the touch point
11197 * over the center of the View.
11198 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011199 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011200 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011201 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011202 }
11203
Christopher Tate17ed60c2011-01-18 12:50:26 -080011204 /**
11205 * Construct a shadow builder object with no associated View. This
11206 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11207 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11208 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011209 * reference to any View object. If they are not overridden, then the result is an
11210 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011211 */
11212 public DragShadowBuilder() {
11213 mView = new WeakReference<View>(null);
11214 }
11215
11216 /**
11217 * Returns the View object that had been passed to the
11218 * {@link #View.DragShadowBuilder(View)}
11219 * constructor. If that View parameter was {@code null} or if the
11220 * {@link #View.DragShadowBuilder()}
11221 * constructor was used to instantiate the builder object, this method will return
11222 * null.
11223 *
11224 * @return The View object associate with this builder object.
11225 */
Chris Tate6b391282010-10-14 15:48:59 -070011226 final public View getView() {
11227 return mView.get();
11228 }
11229
Christopher Tate2c095f32010-10-04 14:13:40 -070011230 /**
Joe Malin32736f02011-01-19 16:14:20 -080011231 * Provides the metrics for the shadow image. These include the dimensions of
11232 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011233 * be centered under the touch location while dragging.
11234 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011235 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011236 * same as the dimensions of the View itself and centers the shadow under
11237 * the touch point.
11238 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011239 *
Joe Malin32736f02011-01-19 16:14:20 -080011240 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11241 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11242 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11243 * image.
11244 *
11245 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11246 * shadow image that should be underneath the touch point during the drag and drop
11247 * operation. Your application must set {@link android.graphics.Point#x} to the
11248 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011249 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011250 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011251 final View view = mView.get();
11252 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011253 shadowSize.set(view.getWidth(), view.getHeight());
11254 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011255 } else {
11256 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11257 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011258 }
11259
11260 /**
Joe Malin32736f02011-01-19 16:14:20 -080011261 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11262 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011263 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011264 *
Joe Malin32736f02011-01-19 16:14:20 -080011265 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011266 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011267 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011268 final View view = mView.get();
11269 if (view != null) {
11270 view.draw(canvas);
11271 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011272 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011273 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011274 }
11275 }
11276
11277 /**
Joe Malin32736f02011-01-19 16:14:20 -080011278 * Starts a drag and drop operation. When your application calls this method, it passes a
11279 * {@link android.view.View.DragShadowBuilder} object to the system. The
11280 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11281 * to get metrics for the drag shadow, and then calls the object's
11282 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11283 * <p>
11284 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11285 * drag events to all the View objects in your application that are currently visible. It does
11286 * this either by calling the View object's drag listener (an implementation of
11287 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11288 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11289 * Both are passed a {@link android.view.DragEvent} object that has a
11290 * {@link android.view.DragEvent#getAction()} value of
11291 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11292 * </p>
11293 * <p>
11294 * Your application can invoke startDrag() on any attached View object. The View object does not
11295 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11296 * be related to the View the user selected for dragging.
11297 * </p>
11298 * @param data A {@link android.content.ClipData} object pointing to the data to be
11299 * transferred by the drag and drop operation.
11300 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11301 * drag shadow.
11302 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11303 * drop operation. This Object is put into every DragEvent object sent by the system during the
11304 * current drag.
11305 * <p>
11306 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11307 * to the target Views. For example, it can contain flags that differentiate between a
11308 * a copy operation and a move operation.
11309 * </p>
11310 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11311 * so the parameter should be set to 0.
11312 * @return {@code true} if the method completes successfully, or
11313 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11314 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011315 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011316 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011317 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011318 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011319 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011320 }
11321 boolean okay = false;
11322
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011323 Point shadowSize = new Point();
11324 Point shadowTouchPoint = new Point();
11325 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011326
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011327 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11328 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11329 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011330 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011331
Chris Tatea32dcf72010-10-14 12:13:50 -070011332 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011333 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11334 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011335 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011336 Surface surface = new Surface();
11337 try {
11338 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011339 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011340 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011341 + " surface=" + surface);
11342 if (token != null) {
11343 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011344 try {
Chris Tate6b391282010-10-14 15:48:59 -070011345 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011346 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011347 } finally {
11348 surface.unlockCanvasAndPost(canvas);
11349 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011350
Christopher Tate407b4e92010-11-30 17:14:08 -080011351 final ViewRoot root = getViewRoot();
11352
11353 // Cache the local state object for delivery with DragEvents
11354 root.setLocalDragState(myLocalState);
11355
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011356 // repurpose 'shadowSize' for the last touch point
11357 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011358
Christopher Tatea53146c2010-09-07 11:57:52 -070011359 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011360 shadowSize.x, shadowSize.y,
11361 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011362 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011363 }
11364 } catch (Exception e) {
11365 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11366 surface.destroy();
11367 }
11368
11369 return okay;
11370 }
11371
Christopher Tatea53146c2010-09-07 11:57:52 -070011372 /**
Joe Malin32736f02011-01-19 16:14:20 -080011373 * Handles drag events sent by the system following a call to
11374 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11375 *<p>
11376 * When the system calls this method, it passes a
11377 * {@link android.view.DragEvent} object. A call to
11378 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11379 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11380 * operation.
11381 * @param event The {@link android.view.DragEvent} sent by the system.
11382 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11383 * in DragEvent, indicating the type of drag event represented by this object.
11384 * @return {@code true} if the method was successful, otherwise {@code false}.
11385 * <p>
11386 * The method should return {@code true} in response to an action type of
11387 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11388 * operation.
11389 * </p>
11390 * <p>
11391 * The method should also return {@code true} in response to an action type of
11392 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11393 * {@code false} if it didn't.
11394 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011395 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011396 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011397 return false;
11398 }
11399
11400 /**
Joe Malin32736f02011-01-19 16:14:20 -080011401 * Detects if this View is enabled and has a drag event listener.
11402 * If both are true, then it calls the drag event listener with the
11403 * {@link android.view.DragEvent} it received. If the drag event listener returns
11404 * {@code true}, then dispatchDragEvent() returns {@code true}.
11405 * <p>
11406 * For all other cases, the method calls the
11407 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11408 * method and returns its result.
11409 * </p>
11410 * <p>
11411 * This ensures that a drag event is always consumed, even if the View does not have a drag
11412 * event listener. However, if the View has a listener and the listener returns true, then
11413 * onDragEvent() is not called.
11414 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011415 */
11416 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011417 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011418 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11419 && mOnDragListener.onDrag(this, event)) {
11420 return true;
11421 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011422 return onDragEvent(event);
11423 }
11424
11425 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011426 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11427 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011428 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011429 */
11430 public void onCloseSystemDialogs(String reason) {
11431 }
Joe Malin32736f02011-01-19 16:14:20 -080011432
Dianne Hackbornffa42482009-09-23 22:20:11 -070011433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011434 * Given a Drawable whose bounds have been set to draw into this view,
11435 * update a Region being computed for {@link #gatherTransparentRegion} so
11436 * that any non-transparent parts of the Drawable are removed from the
11437 * given transparent region.
11438 *
11439 * @param dr The Drawable whose transparency is to be applied to the region.
11440 * @param region A Region holding the current transparency information,
11441 * where any parts of the region that are set are considered to be
11442 * transparent. On return, this region will be modified to have the
11443 * transparency information reduced by the corresponding parts of the
11444 * Drawable that are not transparent.
11445 * {@hide}
11446 */
11447 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11448 if (DBG) {
11449 Log.i("View", "Getting transparent region for: " + this);
11450 }
11451 final Region r = dr.getTransparentRegion();
11452 final Rect db = dr.getBounds();
11453 final AttachInfo attachInfo = mAttachInfo;
11454 if (r != null && attachInfo != null) {
11455 final int w = getRight()-getLeft();
11456 final int h = getBottom()-getTop();
11457 if (db.left > 0) {
11458 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11459 r.op(0, 0, db.left, h, Region.Op.UNION);
11460 }
11461 if (db.right < w) {
11462 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11463 r.op(db.right, 0, w, h, Region.Op.UNION);
11464 }
11465 if (db.top > 0) {
11466 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11467 r.op(0, 0, w, db.top, Region.Op.UNION);
11468 }
11469 if (db.bottom < h) {
11470 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11471 r.op(0, db.bottom, w, h, Region.Op.UNION);
11472 }
11473 final int[] location = attachInfo.mTransparentLocation;
11474 getLocationInWindow(location);
11475 r.translate(location[0], location[1]);
11476 region.op(r, Region.Op.INTERSECT);
11477 } else {
11478 region.op(db, Region.Op.DIFFERENCE);
11479 }
11480 }
11481
Adam Powelle14579b2009-12-16 18:39:52 -080011482 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011483 mHasPerformedLongPress = false;
11484
11485 if (mPendingCheckForLongPress == null) {
11486 mPendingCheckForLongPress = new CheckForLongPress();
11487 }
11488 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011489 postDelayed(mPendingCheckForLongPress,
11490 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011491 }
11492
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 /**
11494 * Inflate a view from an XML resource. This convenience method wraps the {@link
11495 * LayoutInflater} class, which provides a full range of options for view inflation.
11496 *
11497 * @param context The Context object for your activity or application.
11498 * @param resource The resource ID to inflate
11499 * @param root A view group that will be the parent. Used to properly inflate the
11500 * layout_* parameters.
11501 * @see LayoutInflater
11502 */
11503 public static View inflate(Context context, int resource, ViewGroup root) {
11504 LayoutInflater factory = LayoutInflater.from(context);
11505 return factory.inflate(resource, root);
11506 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 /**
Adam Powell637d3372010-08-25 14:37:03 -070011509 * Scroll the view with standard behavior for scrolling beyond the normal
11510 * content boundaries. Views that call this method should override
11511 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11512 * results of an over-scroll operation.
11513 *
11514 * Views can use this method to handle any touch or fling-based scrolling.
11515 *
11516 * @param deltaX Change in X in pixels
11517 * @param deltaY Change in Y in pixels
11518 * @param scrollX Current X scroll value in pixels before applying deltaX
11519 * @param scrollY Current Y scroll value in pixels before applying deltaY
11520 * @param scrollRangeX Maximum content scroll range along the X axis
11521 * @param scrollRangeY Maximum content scroll range along the Y axis
11522 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11523 * along the X axis.
11524 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11525 * along the Y axis.
11526 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11527 * @return true if scrolling was clamped to an over-scroll boundary along either
11528 * axis, false otherwise.
11529 */
11530 protected boolean overScrollBy(int deltaX, int deltaY,
11531 int scrollX, int scrollY,
11532 int scrollRangeX, int scrollRangeY,
11533 int maxOverScrollX, int maxOverScrollY,
11534 boolean isTouchEvent) {
11535 final int overScrollMode = mOverScrollMode;
11536 final boolean canScrollHorizontal =
11537 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11538 final boolean canScrollVertical =
11539 computeVerticalScrollRange() > computeVerticalScrollExtent();
11540 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11541 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11542 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11543 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11544
11545 int newScrollX = scrollX + deltaX;
11546 if (!overScrollHorizontal) {
11547 maxOverScrollX = 0;
11548 }
11549
11550 int newScrollY = scrollY + deltaY;
11551 if (!overScrollVertical) {
11552 maxOverScrollY = 0;
11553 }
11554
11555 // Clamp values if at the limits and record
11556 final int left = -maxOverScrollX;
11557 final int right = maxOverScrollX + scrollRangeX;
11558 final int top = -maxOverScrollY;
11559 final int bottom = maxOverScrollY + scrollRangeY;
11560
11561 boolean clampedX = false;
11562 if (newScrollX > right) {
11563 newScrollX = right;
11564 clampedX = true;
11565 } else if (newScrollX < left) {
11566 newScrollX = left;
11567 clampedX = true;
11568 }
11569
11570 boolean clampedY = false;
11571 if (newScrollY > bottom) {
11572 newScrollY = bottom;
11573 clampedY = true;
11574 } else if (newScrollY < top) {
11575 newScrollY = top;
11576 clampedY = true;
11577 }
11578
11579 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11580
11581 return clampedX || clampedY;
11582 }
11583
11584 /**
11585 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11586 * respond to the results of an over-scroll operation.
11587 *
11588 * @param scrollX New X scroll value in pixels
11589 * @param scrollY New Y scroll value in pixels
11590 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11591 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11592 */
11593 protected void onOverScrolled(int scrollX, int scrollY,
11594 boolean clampedX, boolean clampedY) {
11595 // Intentionally empty.
11596 }
11597
11598 /**
11599 * Returns the over-scroll mode for this view. The result will be
11600 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11601 * (allow over-scrolling only if the view content is larger than the container),
11602 * or {@link #OVER_SCROLL_NEVER}.
11603 *
11604 * @return This view's over-scroll mode.
11605 */
11606 public int getOverScrollMode() {
11607 return mOverScrollMode;
11608 }
11609
11610 /**
11611 * Set the over-scroll mode for this view. Valid over-scroll modes are
11612 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11613 * (allow over-scrolling only if the view content is larger than the container),
11614 * or {@link #OVER_SCROLL_NEVER}.
11615 *
11616 * Setting the over-scroll mode of a view will have an effect only if the
11617 * view is capable of scrolling.
11618 *
11619 * @param overScrollMode The new over-scroll mode for this view.
11620 */
11621 public void setOverScrollMode(int overScrollMode) {
11622 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11623 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11624 overScrollMode != OVER_SCROLL_NEVER) {
11625 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11626 }
11627 mOverScrollMode = overScrollMode;
11628 }
11629
11630 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011631 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11632 * Each MeasureSpec represents a requirement for either the width or the height.
11633 * A MeasureSpec is comprised of a size and a mode. There are three possible
11634 * modes:
11635 * <dl>
11636 * <dt>UNSPECIFIED</dt>
11637 * <dd>
11638 * The parent has not imposed any constraint on the child. It can be whatever size
11639 * it wants.
11640 * </dd>
11641 *
11642 * <dt>EXACTLY</dt>
11643 * <dd>
11644 * The parent has determined an exact size for the child. The child is going to be
11645 * given those bounds regardless of how big it wants to be.
11646 * </dd>
11647 *
11648 * <dt>AT_MOST</dt>
11649 * <dd>
11650 * The child can be as large as it wants up to the specified size.
11651 * </dd>
11652 * </dl>
11653 *
11654 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11655 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11656 */
11657 public static class MeasureSpec {
11658 private static final int MODE_SHIFT = 30;
11659 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11660
11661 /**
11662 * Measure specification mode: The parent has not imposed any constraint
11663 * on the child. It can be whatever size it wants.
11664 */
11665 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11666
11667 /**
11668 * Measure specification mode: The parent has determined an exact size
11669 * for the child. The child is going to be given those bounds regardless
11670 * of how big it wants to be.
11671 */
11672 public static final int EXACTLY = 1 << MODE_SHIFT;
11673
11674 /**
11675 * Measure specification mode: The child can be as large as it wants up
11676 * to the specified size.
11677 */
11678 public static final int AT_MOST = 2 << MODE_SHIFT;
11679
11680 /**
11681 * Creates a measure specification based on the supplied size and mode.
11682 *
11683 * The mode must always be one of the following:
11684 * <ul>
11685 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11686 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11687 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11688 * </ul>
11689 *
11690 * @param size the size of the measure specification
11691 * @param mode the mode of the measure specification
11692 * @return the measure specification based on size and mode
11693 */
11694 public static int makeMeasureSpec(int size, int mode) {
11695 return size + mode;
11696 }
11697
11698 /**
11699 * Extracts the mode from the supplied measure specification.
11700 *
11701 * @param measureSpec the measure specification to extract the mode from
11702 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11703 * {@link android.view.View.MeasureSpec#AT_MOST} or
11704 * {@link android.view.View.MeasureSpec#EXACTLY}
11705 */
11706 public static int getMode(int measureSpec) {
11707 return (measureSpec & MODE_MASK);
11708 }
11709
11710 /**
11711 * Extracts the size from the supplied measure specification.
11712 *
11713 * @param measureSpec the measure specification to extract the size from
11714 * @return the size in pixels defined in the supplied measure specification
11715 */
11716 public static int getSize(int measureSpec) {
11717 return (measureSpec & ~MODE_MASK);
11718 }
11719
11720 /**
11721 * Returns a String representation of the specified measure
11722 * specification.
11723 *
11724 * @param measureSpec the measure specification to convert to a String
11725 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11726 */
11727 public static String toString(int measureSpec) {
11728 int mode = getMode(measureSpec);
11729 int size = getSize(measureSpec);
11730
11731 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11732
11733 if (mode == UNSPECIFIED)
11734 sb.append("UNSPECIFIED ");
11735 else if (mode == EXACTLY)
11736 sb.append("EXACTLY ");
11737 else if (mode == AT_MOST)
11738 sb.append("AT_MOST ");
11739 else
11740 sb.append(mode).append(" ");
11741
11742 sb.append(size);
11743 return sb.toString();
11744 }
11745 }
11746
11747 class CheckForLongPress implements Runnable {
11748
11749 private int mOriginalWindowAttachCount;
11750
11751 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011752 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011753 && mOriginalWindowAttachCount == mWindowAttachCount) {
11754 if (performLongClick()) {
11755 mHasPerformedLongPress = true;
11756 }
11757 }
11758 }
11759
11760 public void rememberWindowAttachCount() {
11761 mOriginalWindowAttachCount = mWindowAttachCount;
11762 }
11763 }
Joe Malin32736f02011-01-19 16:14:20 -080011764
Adam Powelle14579b2009-12-16 18:39:52 -080011765 private final class CheckForTap implements Runnable {
11766 public void run() {
11767 mPrivateFlags &= ~PREPRESSED;
11768 mPrivateFlags |= PRESSED;
11769 refreshDrawableState();
11770 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11771 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11772 }
11773 }
11774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011775
Adam Powella35d7682010-03-12 14:48:13 -080011776 private final class PerformClick implements Runnable {
11777 public void run() {
11778 performClick();
11779 }
11780 }
11781
Dianne Hackborn63042d62011-01-26 18:56:29 -080011782 /** @hide */
11783 public void hackTurnOffWindowResizeAnim(boolean off) {
11784 mAttachInfo.mTurnOffWindowResizeAnim = off;
11785 }
Joe Malin32736f02011-01-19 16:14:20 -080011786
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011787 /**
11788 * Interface definition for a callback to be invoked when a key event is
11789 * dispatched to this view. The callback will be invoked before the key
11790 * event is given to the view.
11791 */
11792 public interface OnKeyListener {
11793 /**
11794 * Called when a key is dispatched to a view. This allows listeners to
11795 * get a chance to respond before the target view.
11796 *
11797 * @param v The view the key has been dispatched to.
11798 * @param keyCode The code for the physical key that was pressed
11799 * @param event The KeyEvent object containing full information about
11800 * the event.
11801 * @return True if the listener has consumed the event, false otherwise.
11802 */
11803 boolean onKey(View v, int keyCode, KeyEvent event);
11804 }
11805
11806 /**
11807 * Interface definition for a callback to be invoked when a touch event is
11808 * dispatched to this view. The callback will be invoked before the touch
11809 * event is given to the view.
11810 */
11811 public interface OnTouchListener {
11812 /**
11813 * Called when a touch event is dispatched to a view. This allows listeners to
11814 * get a chance to respond before the target view.
11815 *
11816 * @param v The view the touch event has been dispatched to.
11817 * @param event The MotionEvent object containing full information about
11818 * the event.
11819 * @return True if the listener has consumed the event, false otherwise.
11820 */
11821 boolean onTouch(View v, MotionEvent event);
11822 }
11823
11824 /**
11825 * Interface definition for a callback to be invoked when a view has been clicked and held.
11826 */
11827 public interface OnLongClickListener {
11828 /**
11829 * Called when a view has been clicked and held.
11830 *
11831 * @param v The view that was clicked and held.
11832 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080011833 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011834 */
11835 boolean onLongClick(View v);
11836 }
11837
11838 /**
Chris Tate32affef2010-10-18 15:29:21 -070011839 * Interface definition for a callback to be invoked when a drag is being dispatched
11840 * to this view. The callback will be invoked before the hosting view's own
11841 * onDrag(event) method. If the listener wants to fall back to the hosting view's
11842 * onDrag(event) behavior, it should return 'false' from this callback.
11843 */
11844 public interface OnDragListener {
11845 /**
11846 * Called when a drag event is dispatched to a view. This allows listeners
11847 * to get a chance to override base View behavior.
11848 *
Joe Malin32736f02011-01-19 16:14:20 -080011849 * @param v The View that received the drag event.
11850 * @param event The {@link android.view.DragEvent} object for the drag event.
11851 * @return {@code true} if the drag event was handled successfully, or {@code false}
11852 * if the drag event was not handled. Note that {@code false} will trigger the View
11853 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070011854 */
11855 boolean onDrag(View v, DragEvent event);
11856 }
11857
11858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859 * Interface definition for a callback to be invoked when the focus state of
11860 * a view changed.
11861 */
11862 public interface OnFocusChangeListener {
11863 /**
11864 * Called when the focus state of a view has changed.
11865 *
11866 * @param v The view whose state has changed.
11867 * @param hasFocus The new focus state of v.
11868 */
11869 void onFocusChange(View v, boolean hasFocus);
11870 }
11871
11872 /**
11873 * Interface definition for a callback to be invoked when a view is clicked.
11874 */
11875 public interface OnClickListener {
11876 /**
11877 * Called when a view has been clicked.
11878 *
11879 * @param v The view that was clicked.
11880 */
11881 void onClick(View v);
11882 }
11883
11884 /**
11885 * Interface definition for a callback to be invoked when the context menu
11886 * for this view is being built.
11887 */
11888 public interface OnCreateContextMenuListener {
11889 /**
11890 * Called when the context menu for this view is being built. It is not
11891 * safe to hold onto the menu after this method returns.
11892 *
11893 * @param menu The context menu that is being built
11894 * @param v The view for which the context menu is being built
11895 * @param menuInfo Extra information about the item for which the
11896 * context menu should be shown. This information will vary
11897 * depending on the class of v.
11898 */
11899 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
11900 }
11901
Joe Onorato664644d2011-01-23 17:53:23 -080011902 /**
11903 * Interface definition for a callback to be invoked when the status bar changes
11904 * visibility.
11905 *
11906 * @see #setOnSystemUiVisibilityChangeListener
11907 */
11908 public interface OnSystemUiVisibilityChangeListener {
11909 /**
11910 * Called when the status bar changes visibility because of a call to
11911 * {@link #setSystemUiVisibility}.
11912 *
11913 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
11914 */
11915 public void onSystemUiVisibilityChange(int visibility);
11916 }
11917
Adam Powell4afd62b2011-02-18 15:02:18 -080011918 /**
11919 * Interface definition for a callback to be invoked when this view is attached
11920 * or detached from its window.
11921 */
11922 public interface OnAttachStateChangeListener {
11923 /**
11924 * Called when the view is attached to a window.
11925 * @param v The view that was attached
11926 */
11927 public void onViewAttachedToWindow(View v);
11928 /**
11929 * Called when the view is detached from a window.
11930 * @param v The view that was detached
11931 */
11932 public void onViewDetachedFromWindow(View v);
11933 }
11934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 private final class UnsetPressedState implements Runnable {
11936 public void run() {
11937 setPressed(false);
11938 }
11939 }
11940
11941 /**
11942 * Base class for derived classes that want to save and restore their own
11943 * state in {@link android.view.View#onSaveInstanceState()}.
11944 */
11945 public static class BaseSavedState extends AbsSavedState {
11946 /**
11947 * Constructor used when reading from a parcel. Reads the state of the superclass.
11948 *
11949 * @param source
11950 */
11951 public BaseSavedState(Parcel source) {
11952 super(source);
11953 }
11954
11955 /**
11956 * Constructor called by derived classes when creating their SavedState objects
11957 *
11958 * @param superState The state of the superclass of this view
11959 */
11960 public BaseSavedState(Parcelable superState) {
11961 super(superState);
11962 }
11963
11964 public static final Parcelable.Creator<BaseSavedState> CREATOR =
11965 new Parcelable.Creator<BaseSavedState>() {
11966 public BaseSavedState createFromParcel(Parcel in) {
11967 return new BaseSavedState(in);
11968 }
11969
11970 public BaseSavedState[] newArray(int size) {
11971 return new BaseSavedState[size];
11972 }
11973 };
11974 }
11975
11976 /**
11977 * A set of information given to a view when it is attached to its parent
11978 * window.
11979 */
11980 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011981 interface Callbacks {
11982 void playSoundEffect(int effectId);
11983 boolean performHapticFeedback(int effectId, boolean always);
11984 }
11985
11986 /**
11987 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
11988 * to a Handler. This class contains the target (View) to invalidate and
11989 * the coordinates of the dirty rectangle.
11990 *
11991 * For performance purposes, this class also implements a pool of up to
11992 * POOL_LIMIT objects that get reused. This reduces memory allocations
11993 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011994 */
Romain Guyd928d682009-03-31 17:52:16 -070011995 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011996 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070011997 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
11998 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070011999 public InvalidateInfo newInstance() {
12000 return new InvalidateInfo();
12001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012002
Romain Guyd928d682009-03-31 17:52:16 -070012003 public void onAcquired(InvalidateInfo element) {
12004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012005
Romain Guyd928d682009-03-31 17:52:16 -070012006 public void onReleased(InvalidateInfo element) {
12007 }
12008 }, POOL_LIMIT)
12009 );
12010
12011 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012012
12013 View target;
12014
12015 int left;
12016 int top;
12017 int right;
12018 int bottom;
12019
Romain Guyd928d682009-03-31 17:52:16 -070012020 public void setNextPoolable(InvalidateInfo element) {
12021 mNext = element;
12022 }
12023
12024 public InvalidateInfo getNextPoolable() {
12025 return mNext;
12026 }
12027
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012028 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012029 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012030 }
12031
12032 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012033 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012034 }
12035 }
12036
12037 final IWindowSession mSession;
12038
12039 final IWindow mWindow;
12040
12041 final IBinder mWindowToken;
12042
12043 final Callbacks mRootCallbacks;
12044
Chet Haasedaf98e92011-01-10 14:10:36 -080012045 Canvas mHardwareCanvas;
12046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012047 /**
12048 * The top view of the hierarchy.
12049 */
12050 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012051
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012052 IBinder mPanelParentWindowToken;
12053 Surface mSurface;
12054
Romain Guyb051e892010-09-28 19:09:36 -070012055 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012056 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012057 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012058
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012059 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012060 * Scale factor used by the compatibility mode
12061 */
12062 float mApplicationScale;
12063
12064 /**
12065 * Indicates whether the application is in compatibility mode
12066 */
12067 boolean mScalingRequired;
12068
12069 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012070 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12071 */
12072 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012073
Dianne Hackborn63042d62011-01-26 18:56:29 -080012074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012075 * Left position of this view's window
12076 */
12077 int mWindowLeft;
12078
12079 /**
12080 * Top position of this view's window
12081 */
12082 int mWindowTop;
12083
12084 /**
Adam Powell26153a32010-11-08 15:22:27 -080012085 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012086 */
Adam Powell26153a32010-11-08 15:22:27 -080012087 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012088
12089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012090 * For windows that are full-screen but using insets to layout inside
12091 * of the screen decorations, these are the current insets for the
12092 * content of the window.
12093 */
12094 final Rect mContentInsets = new Rect();
12095
12096 /**
12097 * For windows that are full-screen but using insets to layout inside
12098 * of the screen decorations, these are the current insets for the
12099 * actual visible parts of the window.
12100 */
12101 final Rect mVisibleInsets = new Rect();
12102
12103 /**
12104 * The internal insets given by this window. This value is
12105 * supplied by the client (through
12106 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12107 * be given to the window manager when changed to be used in laying
12108 * out windows behind it.
12109 */
12110 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12111 = new ViewTreeObserver.InternalInsetsInfo();
12112
12113 /**
12114 * All views in the window's hierarchy that serve as scroll containers,
12115 * used to determine if the window can be resized or must be panned
12116 * to adjust for a soft input area.
12117 */
12118 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12119
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012120 final KeyEvent.DispatcherState mKeyDispatchState
12121 = new KeyEvent.DispatcherState();
12122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012123 /**
12124 * Indicates whether the view's window currently has the focus.
12125 */
12126 boolean mHasWindowFocus;
12127
12128 /**
12129 * The current visibility of the window.
12130 */
12131 int mWindowVisibility;
12132
12133 /**
12134 * Indicates the time at which drawing started to occur.
12135 */
12136 long mDrawingTime;
12137
12138 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012139 * Indicates whether or not ignoring the DIRTY_MASK flags.
12140 */
12141 boolean mIgnoreDirtyState;
12142
12143 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012144 * Indicates whether the view's window is currently in touch mode.
12145 */
12146 boolean mInTouchMode;
12147
12148 /**
12149 * Indicates that ViewRoot should trigger a global layout change
12150 * the next time it performs a traversal
12151 */
12152 boolean mRecomputeGlobalAttributes;
12153
12154 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012155 * Set during a traveral if any views want to keep the screen on.
12156 */
12157 boolean mKeepScreenOn;
12158
12159 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012160 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12161 */
12162 int mSystemUiVisibility;
12163
12164 /**
12165 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12166 * attached.
12167 */
12168 boolean mHasSystemUiListeners;
12169
12170 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 * Set if the visibility of any views has changed.
12172 */
12173 boolean mViewVisibilityChanged;
12174
12175 /**
12176 * Set to true if a view has been scrolled.
12177 */
12178 boolean mViewScrollChanged;
12179
12180 /**
12181 * Global to the view hierarchy used as a temporary for dealing with
12182 * x/y points in the transparent region computations.
12183 */
12184 final int[] mTransparentLocation = new int[2];
12185
12186 /**
12187 * Global to the view hierarchy used as a temporary for dealing with
12188 * x/y points in the ViewGroup.invalidateChild implementation.
12189 */
12190 final int[] mInvalidateChildLocation = new int[2];
12191
Chet Haasec3aa3612010-06-17 08:50:37 -070012192
12193 /**
12194 * Global to the view hierarchy used as a temporary for dealing with
12195 * x/y location when view is transformed.
12196 */
12197 final float[] mTmpTransformLocation = new float[2];
12198
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012199 /**
12200 * The view tree observer used to dispatch global events like
12201 * layout, pre-draw, touch mode change, etc.
12202 */
12203 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12204
12205 /**
12206 * A Canvas used by the view hierarchy to perform bitmap caching.
12207 */
12208 Canvas mCanvas;
12209
12210 /**
12211 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12212 * handler can be used to pump events in the UI events queue.
12213 */
12214 final Handler mHandler;
12215
12216 /**
12217 * Identifier for messages requesting the view to be invalidated.
12218 * Such messages should be sent to {@link #mHandler}.
12219 */
12220 static final int INVALIDATE_MSG = 0x1;
12221
12222 /**
12223 * Identifier for messages requesting the view to invalidate a region.
12224 * Such messages should be sent to {@link #mHandler}.
12225 */
12226 static final int INVALIDATE_RECT_MSG = 0x2;
12227
12228 /**
12229 * Temporary for use in computing invalidate rectangles while
12230 * calling up the hierarchy.
12231 */
12232 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012233
12234 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012235 * Temporary for use in computing hit areas with transformed views
12236 */
12237 final RectF mTmpTransformRect = new RectF();
12238
12239 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012240 * Temporary list for use in collecting focusable descendents of a view.
12241 */
12242 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012244 /**
12245 * Creates a new set of attachment information with the specified
12246 * events handler and thread.
12247 *
12248 * @param handler the events handler the view must use
12249 */
12250 AttachInfo(IWindowSession session, IWindow window,
12251 Handler handler, Callbacks effectPlayer) {
12252 mSession = session;
12253 mWindow = window;
12254 mWindowToken = window.asBinder();
12255 mHandler = handler;
12256 mRootCallbacks = effectPlayer;
12257 }
12258 }
12259
12260 /**
12261 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12262 * is supported. This avoids keeping too many unused fields in most
12263 * instances of View.</p>
12264 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012265 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012266
Mike Cleronf116bf82009-09-27 19:14:12 -070012267 /**
12268 * Scrollbars are not visible
12269 */
12270 public static final int OFF = 0;
12271
12272 /**
12273 * Scrollbars are visible
12274 */
12275 public static final int ON = 1;
12276
12277 /**
12278 * Scrollbars are fading away
12279 */
12280 public static final int FADING = 2;
12281
12282 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012284 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012285 public int scrollBarDefaultDelayBeforeFade;
12286 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012287
12288 public int scrollBarSize;
12289 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012290 public float[] interpolatorValues;
12291 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012292
12293 public final Paint paint;
12294 public final Matrix matrix;
12295 public Shader shader;
12296
Mike Cleronf116bf82009-09-27 19:14:12 -070012297 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12298
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012299 private static final float[] OPAQUE = { 255 };
12300 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012301
Mike Cleronf116bf82009-09-27 19:14:12 -070012302 /**
12303 * When fading should start. This time moves into the future every time
12304 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12305 */
12306 public long fadeStartTime;
12307
12308
12309 /**
12310 * The current state of the scrollbars: ON, OFF, or FADING
12311 */
12312 public int state = OFF;
12313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 private int mLastColor;
12315
Mike Cleronf116bf82009-09-27 19:14:12 -070012316 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012317 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12318 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012319 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12320 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012321
12322 paint = new Paint();
12323 matrix = new Matrix();
12324 // use use a height of 1, and then wack the matrix each time we
12325 // actually use it.
12326 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012328 paint.setShader(shader);
12329 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012330 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012331 }
Romain Guy8506ab42009-06-11 17:35:47 -070012332
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012333 public void setFadeColor(int color) {
12334 if (color != 0 && color != mLastColor) {
12335 mLastColor = color;
12336 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012337
Romain Guye55e1a72009-08-27 10:42:26 -070012338 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12339 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012340
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012341 paint.setShader(shader);
12342 // Restore the default transfer mode (src_over)
12343 paint.setXfermode(null);
12344 }
12345 }
Joe Malin32736f02011-01-19 16:14:20 -080012346
Mike Cleronf116bf82009-09-27 19:14:12 -070012347 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012348 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012349 if (now >= fadeStartTime) {
12350
12351 // the animation fades the scrollbars out by changing
12352 // the opacity (alpha) from fully opaque to fully
12353 // transparent
12354 int nextFrame = (int) now;
12355 int framesCount = 0;
12356
12357 Interpolator interpolator = scrollBarInterpolator;
12358
12359 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012360 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012361
12362 // End transparent
12363 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012364 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012365
12366 state = FADING;
12367
12368 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012369 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012370 }
12371 }
12372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012373 }
12374}