blob: aa9e00aefdf169fd5220c0906f606494c0adcaaf [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78/**
79 * <p>
80 * This class represents the basic building block for user interface components. A View
81 * occupies a rectangular area on the screen and is responsible for drawing and
82 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070083 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
85 * are invisible containers that hold other Views (or other ViewGroups) and define
86 * their layout properties.
87 * </p>
88 *
89 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070090 * <p>For an introduction to using this class to develop your
91 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070093 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
95 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
102 * </p>
103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
129 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
130 * specialized listeners. For example, a Button exposes a listener to notify
131 * clients when the button is clicked.</li>
132 * <li><strong>Set visibility:</strong> You can hide or show views using
133 * {@link #setVisibility}.</li>
134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
174 * <td><code>{@link #onMeasure}</code></td>
175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
180 * <td><code>{@link #onLayout}</code></td>
181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
186 * <td><code>{@link #onSizeChanged}</code></td>
187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
193 * <td><code>{@link #onDraw}</code></td>
194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
200 * <td><code>{@link #onKeyDown}</code></td>
201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
205 * <td><code>{@link #onKeyUp}</code></td>
206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
210 * <td><code>{@link #onTrackballEvent}</code></td>
211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
215 * <td><code>{@link #onTouchEvent}</code></td>
216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
222 * <td><code>{@link #onFocusChanged}</code></td>
223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td><code>{@link #onWindowFocusChanged}</code></td>
229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
340 * {@link #setPadding(int, int, int, int)} method and queried by calling
341 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <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 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
423 * intersects the the invalid region. Because the tree is traversed in-order,
424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * 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 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
570 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
571 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
581 * @attr ref android.R.styleable#View_fadingEdge
582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
604 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800605 * @attr ref android.R.styleable#View_rotation
606 * @attr ref android.R.styleable#View_rotationX
607 * @attr ref android.R.styleable#View_rotationY
608 * @attr ref android.R.styleable#View_scaleX
609 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_scrollX
611 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_scrollbarSize
613 * @attr ref android.R.styleable#View_scrollbarStyle
614 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700615 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
616 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
618 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarThumbVertical
620 * @attr ref android.R.styleable#View_scrollbarTrackVertical
621 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700623 * @attr ref android.R.styleable#View_soundEffectsEnabled
624 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800625 * @attr ref android.R.styleable#View_transformPivotX
626 * @attr ref android.R.styleable#View_transformPivotY
627 * @attr ref android.R.styleable#View_translationX
628 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 *
631 * @see android.view.ViewGroup
632 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700633public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private static final boolean DBG = false;
635
636 /**
637 * The logging tag used by this class with android.util.Log.
638 */
639 protected static final String VIEW_LOG_TAG = "View";
640
641 /**
642 * Used to mark a View that has no ID.
643 */
644 public static final int NO_ID = -1;
645
646 /**
647 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
648 * calling setFlags.
649 */
650 private static final int NOT_FOCUSABLE = 0x00000000;
651
652 /**
653 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
654 * setFlags.
655 */
656 private static final int FOCUSABLE = 0x00000001;
657
658 /**
659 * Mask for use with setFlags indicating bits used for focus.
660 */
661 private static final int FOCUSABLE_MASK = 0x00000001;
662
663 /**
664 * This view will adjust its padding to fit sytem windows (e.g. status bar)
665 */
666 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
667
668 /**
669 * This view is visible. Use with {@link #setVisibility}.
670 */
671 public static final int VISIBLE = 0x00000000;
672
673 /**
674 * This view is invisible, but it still takes up space for layout purposes.
675 * Use with {@link #setVisibility}.
676 */
677 public static final int INVISIBLE = 0x00000004;
678
679 /**
680 * This view is invisible, and it doesn't take any space for layout
681 * purposes. Use with {@link #setVisibility}.
682 */
683 public static final int GONE = 0x00000008;
684
685 /**
686 * Mask for use with setFlags indicating bits used for visibility.
687 * {@hide}
688 */
689 static final int VISIBILITY_MASK = 0x0000000C;
690
691 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
692
693 /**
694 * This view is enabled. Intrepretation varies by subclass.
695 * Use with ENABLED_MASK when calling setFlags.
696 * {@hide}
697 */
698 static final int ENABLED = 0x00000000;
699
700 /**
701 * This view is disabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int DISABLED = 0x00000020;
706
707 /**
708 * Mask for use with setFlags indicating bits used for indicating whether
709 * this view is enabled
710 * {@hide}
711 */
712 static final int ENABLED_MASK = 0x00000020;
713
714 /**
715 * This view won't draw. {@link #onDraw} won't be called and further
716 * optimizations
717 * will be performed. It is okay to have this flag set and a background.
718 * Use with DRAW_MASK when calling setFlags.
719 * {@hide}
720 */
721 static final int WILL_NOT_DRAW = 0x00000080;
722
723 /**
724 * Mask for use with setFlags indicating bits used for indicating whether
725 * this view is will draw
726 * {@hide}
727 */
728 static final int DRAW_MASK = 0x00000080;
729
730 /**
731 * <p>This view doesn't show scrollbars.</p>
732 * {@hide}
733 */
734 static final int SCROLLBARS_NONE = 0x00000000;
735
736 /**
737 * <p>This view shows horizontal scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
741
742 /**
743 * <p>This view shows vertical scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_VERTICAL = 0x00000200;
747
748 /**
749 * <p>Mask for use with setFlags indicating bits used for indicating which
750 * scrollbars are enabled.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_MASK = 0x00000300;
754
Jeff Brown85a31762010-09-01 17:01:00 -0700755 /**
756 * Indicates that the view should filter touches when its window is obscured.
757 * Refer to the class comments for more information about this security feature.
758 * {@hide}
759 */
760 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
761
762 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763
764 /**
765 * <p>This view doesn't show fading edges.</p>
766 * {@hide}
767 */
768 static final int FADING_EDGE_NONE = 0x00000000;
769
770 /**
771 * <p>This view shows horizontal fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
775
776 /**
777 * <p>This view shows vertical fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_VERTICAL = 0x00002000;
781
782 /**
783 * <p>Mask for use with setFlags indicating bits used for indicating which
784 * fading edges are enabled.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_MASK = 0x00003000;
788
789 /**
790 * <p>Indicates this view can be clicked. When clickable, a View reacts
791 * to clicks by notifying the OnClickListener.<p>
792 * {@hide}
793 */
794 static final int CLICKABLE = 0x00004000;
795
796 /**
797 * <p>Indicates this view is caching its drawing into a bitmap.</p>
798 * {@hide}
799 */
800 static final int DRAWING_CACHE_ENABLED = 0x00008000;
801
802 /**
803 * <p>Indicates that no icicle should be saved for this view.<p>
804 * {@hide}
805 */
806 static final int SAVE_DISABLED = 0x000010000;
807
808 /**
809 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
810 * property.</p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED_MASK = 0x000010000;
814
815 /**
816 * <p>Indicates that no drawing cache should ever be created for this view.<p>
817 * {@hide}
818 */
819 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
820
821 /**
822 * <p>Indicates this view can take / keep focus when int touch mode.</p>
823 * {@hide}
824 */
825 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
826
827 /**
828 * <p>Enables low quality mode for the drawing cache.</p>
829 */
830 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
831
832 /**
833 * <p>Enables high quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
836
837 /**
838 * <p>Enables automatic quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
841
842 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
843 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
844 };
845
846 /**
847 * <p>Mask for use with setFlags indicating bits used for the cache
848 * quality property.</p>
849 * {@hide}
850 */
851 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
852
853 /**
854 * <p>
855 * Indicates this view can be long clicked. When long clickable, a View
856 * reacts to long clicks by notifying the OnLongClickListener or showing a
857 * context menu.
858 * </p>
859 * {@hide}
860 */
861 static final int LONG_CLICKABLE = 0x00200000;
862
863 /**
864 * <p>Indicates that this view gets its drawable states from its direct parent
865 * and ignores its original internal states.</p>
866 *
867 * @hide
868 */
869 static final int DUPLICATE_PARENT_STATE = 0x00400000;
870
871 /**
872 * The scrollbar style to display the scrollbars inside the content area,
873 * without increasing the padding. The scrollbars will be overlaid with
874 * translucency on the view's content.
875 */
876 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the padded area,
880 * increasing the padding of the view. The scrollbars will not overlap the
881 * content area of the view.
882 */
883 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
884
885 /**
886 * The scrollbar style to display the scrollbars at the edge of the view,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency.
889 */
890 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * increasing the padding of the view. The scrollbars will only overlap the
895 * background, if any.
896 */
897 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
898
899 /**
900 * Mask to check if the scrollbar style is overlay or inset.
901 * {@hide}
902 */
903 static final int SCROLLBARS_INSET_MASK = 0x01000000;
904
905 /**
906 * Mask to check if the scrollbar style is inside or outside.
907 * {@hide}
908 */
909 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
910
911 /**
912 * Mask for scrollbar style.
913 * {@hide}
914 */
915 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
916
917 /**
918 * View flag indicating that the screen should remain on while the
919 * window containing this view is visible to the user. This effectively
920 * takes care of automatically setting the WindowManager's
921 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
922 */
923 public static final int KEEP_SCREEN_ON = 0x04000000;
924
925 /**
926 * View flag indicating whether this view should have sound effects enabled
927 * for events such as clicking and touching.
928 */
929 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
930
931 /**
932 * View flag indicating whether this view should have haptic feedback
933 * enabled for events such as long presses.
934 */
935 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
936
937 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700938 * <p>Indicates that the view hierarchy should stop saving state when
939 * it reaches this view. If state saving is initiated immediately at
940 * the view, it will be allowed.
941 * {@hide}
942 */
943 static final int PARENT_SAVE_DISABLED = 0x20000000;
944
945 /**
946 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
950
951 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700952 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
953 * should add all focusable Views regardless if they are focusable in touch mode.
954 */
955 public static final int FOCUSABLES_ALL = 0x00000000;
956
957 /**
958 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
959 * should add only Views focusable in touch mode.
960 */
961 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
962
963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * Use with {@link #focusSearch}. Move focus to the previous selectable
965 * item.
966 */
967 public static final int FOCUS_BACKWARD = 0x00000001;
968
969 /**
970 * Use with {@link #focusSearch}. Move focus to the next selectable
971 * item.
972 */
973 public static final int FOCUS_FORWARD = 0x00000002;
974
975 /**
976 * Use with {@link #focusSearch}. Move focus to the left.
977 */
978 public static final int FOCUS_LEFT = 0x00000011;
979
980 /**
981 * Use with {@link #focusSearch}. Move focus up.
982 */
983 public static final int FOCUS_UP = 0x00000021;
984
985 /**
986 * Use with {@link #focusSearch}. Move focus to the right.
987 */
988 public static final int FOCUS_RIGHT = 0x00000042;
989
990 /**
991 * Use with {@link #focusSearch}. Move focus down.
992 */
993 public static final int FOCUS_DOWN = 0x00000082;
994
995 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800996 * Bits of {@link #getMeasuredWidthAndState()} and
997 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
998 */
999 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1000
1001 /**
1002 * Bits of {@link #getMeasuredWidthAndState()} and
1003 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1004 */
1005 public static final int MEASURED_STATE_MASK = 0xff000000;
1006
1007 /**
1008 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1009 * for functions that combine both width and height into a single int,
1010 * such as {@link #getMeasuredState()} and the childState argument of
1011 * {@link #resolveSizeAndState(int, int, int)}.
1012 */
1013 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1014
1015 /**
1016 * Bit of {@link #getMeasuredWidthAndState()} and
1017 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1018 * is smaller that the space the view would like to have.
1019 */
1020 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * Base View state sets
1024 */
1025 // Singles
1026 /**
1027 * Indicates the view has no states set. States are used with
1028 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1029 * view depending on its state.
1030 *
1031 * @see android.graphics.drawable.Drawable
1032 * @see #getDrawableState()
1033 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001034 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
1036 * Indicates the view is enabled. States are used with
1037 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1038 * view depending on its state.
1039 *
1040 * @see android.graphics.drawable.Drawable
1041 * @see #getDrawableState()
1042 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001043 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 /**
1045 * Indicates the view is focused. States are used with
1046 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1047 * view depending on its state.
1048 *
1049 * @see android.graphics.drawable.Drawable
1050 * @see #getDrawableState()
1051 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001052 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 /**
1054 * Indicates the view is selected. States are used with
1055 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1056 * view depending on its state.
1057 *
1058 * @see android.graphics.drawable.Drawable
1059 * @see #getDrawableState()
1060 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001061 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 /**
1063 * Indicates the view is pressed. States are used with
1064 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1065 * view depending on its state.
1066 *
1067 * @see android.graphics.drawable.Drawable
1068 * @see #getDrawableState()
1069 * @hide
1070 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001071 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /**
1073 * Indicates the view's window has focus. States are used with
1074 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1075 * view depending on its state.
1076 *
1077 * @see android.graphics.drawable.Drawable
1078 * @see #getDrawableState()
1079 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001080 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 // Doubles
1082 /**
1083 * Indicates the view is enabled and has the focus.
1084 *
1085 * @see #ENABLED_STATE_SET
1086 * @see #FOCUSED_STATE_SET
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled and selected.
1091 *
1092 * @see #ENABLED_STATE_SET
1093 * @see #SELECTED_STATE_SET
1094 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001095 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 /**
1097 * Indicates the view is enabled and that its window has focus.
1098 *
1099 * @see #ENABLED_STATE_SET
1100 * @see #WINDOW_FOCUSED_STATE_SET
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is focused and selected.
1105 *
1106 * @see #FOCUSED_STATE_SET
1107 * @see #SELECTED_STATE_SET
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view has the focus and that its window has the focus.
1112 *
1113 * @see #FOCUSED_STATE_SET
1114 * @see #WINDOW_FOCUSED_STATE_SET
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is selected and that its window has the focus.
1119 *
1120 * @see #SELECTED_STATE_SET
1121 * @see #WINDOW_FOCUSED_STATE_SET
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // Triples
1125 /**
1126 * Indicates the view is enabled, focused and selected.
1127 *
1128 * @see #ENABLED_STATE_SET
1129 * @see #FOCUSED_STATE_SET
1130 * @see #SELECTED_STATE_SET
1131 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001132 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 /**
1134 * Indicates the view is enabled, focused and its window has the focus.
1135 *
1136 * @see #ENABLED_STATE_SET
1137 * @see #FOCUSED_STATE_SET
1138 * @see #WINDOW_FOCUSED_STATE_SET
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is enabled, selected and its window has the focus.
1143 *
1144 * @see #ENABLED_STATE_SET
1145 * @see #SELECTED_STATE_SET
1146 * @see #WINDOW_FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is focused, selected and its window has the focus.
1151 *
1152 * @see #FOCUSED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is enabled, focused, selected and its window
1159 * has the focus.
1160 *
1161 * @see #ENABLED_STATE_SET
1162 * @see #FOCUSED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 * @see #WINDOW_FOCUSED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is pressed and its window has the focus.
1169 *
1170 * @see #PRESSED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is pressed and selected.
1176 *
1177 * @see #PRESSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is pressed, selected and its window has the focus.
1183 *
1184 * @see #PRESSED_STATE_SET
1185 * @see #SELECTED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is pressed and focused.
1191 *
1192 * @see #PRESSED_STATE_SET
1193 * @see #FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is pressed, focused and its window has the focus.
1198 *
1199 * @see #PRESSED_STATE_SET
1200 * @see #FOCUSED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is pressed, focused and selected.
1206 *
1207 * @see #PRESSED_STATE_SET
1208 * @see #SELECTED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is pressed, focused, selected and its window has the focus.
1214 *
1215 * @see #PRESSED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and enabled.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #ENABLED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed, enabled and its window has the focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #ENABLED_STATE_SET
1233 * @see #WINDOW_FOCUSED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed, enabled and selected.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #ENABLED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed, enabled, selected and its window has the
1246 * focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #ENABLED_STATE_SET
1250 * @see #SELECTED_STATE_SET
1251 * @see #WINDOW_FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, enabled and focused.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #ENABLED_STATE_SET
1259 * @see #FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, enabled, focused and its window has the
1264 * focus.
1265 *
1266 * @see #PRESSED_STATE_SET
1267 * @see #ENABLED_STATE_SET
1268 * @see #FOCUSED_STATE_SET
1269 * @see #WINDOW_FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed, enabled, focused and selected.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #ENABLED_STATE_SET
1277 * @see #SELECTED_STATE_SET
1278 * @see #FOCUSED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled, focused, selected and its window
1283 * has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292
1293 /**
1294 * The order here is very important to {@link #getDrawableState()}
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 private static final int[][] VIEW_STATE_SETS;
1297
Romain Guyb051e892010-09-28 19:09:36 -07001298 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1299 static final int VIEW_STATE_SELECTED = 1 << 1;
1300 static final int VIEW_STATE_FOCUSED = 1 << 2;
1301 static final int VIEW_STATE_ENABLED = 1 << 3;
1302 static final int VIEW_STATE_PRESSED = 1 << 4;
1303 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001304 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305
1306 static final int[] VIEW_STATE_IDS = new int[] {
1307 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1308 R.attr.state_selected, VIEW_STATE_SELECTED,
1309 R.attr.state_focused, VIEW_STATE_FOCUSED,
1310 R.attr.state_enabled, VIEW_STATE_ENABLED,
1311 R.attr.state_pressed, VIEW_STATE_PRESSED,
1312 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001313 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 };
1315
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001316 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001317 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1318 throw new IllegalStateException(
1319 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1320 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001322 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001326 orderedIds[i * 2] = viewState;
1327 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 }
1329 }
1330 }
Romain Guyb051e892010-09-28 19:09:36 -07001331 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1332 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1333 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 int numBits = Integer.bitCount(i);
1335 int[] set = new int[numBits];
1336 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001337 for (int j = 0; j < orderedIds.length; j += 2) {
1338 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001339 set[pos++] = orderedIds[j];
1340 }
1341 }
1342 VIEW_STATE_SETS[i] = set;
1343 }
1344
1345 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1346 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1347 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1348 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1349 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1350 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1351 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1352 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1353 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1357 | VIEW_STATE_FOCUSED];
1358 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1359 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1361 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1365 | VIEW_STATE_ENABLED];
1366 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1367 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1370 | VIEW_STATE_ENABLED];
1371 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1373 | VIEW_STATE_ENABLED];
1374 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1376 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1377
1378 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1379 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1381 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1385 | VIEW_STATE_PRESSED];
1386 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1390 | VIEW_STATE_PRESSED];
1391 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1393 | VIEW_STATE_PRESSED];
1394 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1396 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1397 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1398 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1401 | VIEW_STATE_PRESSED];
1402 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1404 | VIEW_STATE_PRESSED];
1405 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1407 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1408 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1410 | VIEW_STATE_PRESSED];
1411 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1413 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1414 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1416 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1417 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1420 | VIEW_STATE_PRESSED];
1421 }
1422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * Temporary Rect currently for use in setBackground(). This will probably
1425 * be extended in the future to hold our own class with more than just
1426 * a Rect. :)
1427 */
1428 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001429
1430 /**
1431 * Map used to store views' tags.
1432 */
1433 private static WeakHashMap<View, SparseArray<Object>> sTags;
1434
1435 /**
1436 * Lock used to access sTags.
1437 */
1438 private static final Object sTagsLock = new Object();
1439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 /**
1441 * The animation currently associated with this view.
1442 * @hide
1443 */
1444 protected Animation mCurrentAnimation = null;
1445
1446 /**
1447 * Width as measured during measure pass.
1448 * {@hide}
1449 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001450 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001451 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452
1453 /**
1454 * Height as measured during measure pass.
1455 * {@hide}
1456 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001457 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001458 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
1460 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001461 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1462 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1463 * its display list. This flag, used only when hw accelerated, allows us to clear the
1464 * flag while retaining this information until it's needed (at getDisplayList() time and
1465 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1466 *
1467 * {@hide}
1468 */
1469 boolean mRecreateDisplayList = false;
1470
1471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 * The view's identifier.
1473 * {@hide}
1474 *
1475 * @see #setId(int)
1476 * @see #getId()
1477 */
1478 @ViewDebug.ExportedProperty(resolveId = true)
1479 int mID = NO_ID;
1480
1481 /**
1482 * The view's tag.
1483 * {@hide}
1484 *
1485 * @see #setTag(Object)
1486 * @see #getTag()
1487 */
1488 protected Object mTag;
1489
1490 // for mPrivateFlags:
1491 /** {@hide} */
1492 static final int WANTS_FOCUS = 0x00000001;
1493 /** {@hide} */
1494 static final int FOCUSED = 0x00000002;
1495 /** {@hide} */
1496 static final int SELECTED = 0x00000004;
1497 /** {@hide} */
1498 static final int IS_ROOT_NAMESPACE = 0x00000008;
1499 /** {@hide} */
1500 static final int HAS_BOUNDS = 0x00000010;
1501 /** {@hide} */
1502 static final int DRAWN = 0x00000020;
1503 /**
1504 * When this flag is set, this view is running an animation on behalf of its
1505 * children and should therefore not cancel invalidate requests, even if they
1506 * lie outside of this view's bounds.
1507 *
1508 * {@hide}
1509 */
1510 static final int DRAW_ANIMATION = 0x00000040;
1511 /** {@hide} */
1512 static final int SKIP_DRAW = 0x00000080;
1513 /** {@hide} */
1514 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1515 /** {@hide} */
1516 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1517 /** {@hide} */
1518 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1519 /** {@hide} */
1520 static final int MEASURED_DIMENSION_SET = 0x00000800;
1521 /** {@hide} */
1522 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001523 /** {@hide} */
1524 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 private static final int PRESSED = 0x00004000;
1527
1528 /** {@hide} */
1529 static final int DRAWING_CACHE_VALID = 0x00008000;
1530 /**
1531 * Flag used to indicate that this view should be drawn once more (and only once
1532 * more) after its animation has completed.
1533 * {@hide}
1534 */
1535 static final int ANIMATION_STARTED = 0x00010000;
1536
1537 private static final int SAVE_STATE_CALLED = 0x00020000;
1538
1539 /**
1540 * Indicates that the View returned true when onSetAlpha() was called and that
1541 * the alpha must be restored.
1542 * {@hide}
1543 */
1544 static final int ALPHA_SET = 0x00040000;
1545
1546 /**
1547 * Set by {@link #setScrollContainer(boolean)}.
1548 */
1549 static final int SCROLL_CONTAINER = 0x00080000;
1550
1551 /**
1552 * Set by {@link #setScrollContainer(boolean)}.
1553 */
1554 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1555
1556 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001557 * View flag indicating whether this view was invalidated (fully or partially.)
1558 *
1559 * @hide
1560 */
1561 static final int DIRTY = 0x00200000;
1562
1563 /**
1564 * View flag indicating whether this view was invalidated by an opaque
1565 * invalidate request.
1566 *
1567 * @hide
1568 */
1569 static final int DIRTY_OPAQUE = 0x00400000;
1570
1571 /**
1572 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1573 *
1574 * @hide
1575 */
1576 static final int DIRTY_MASK = 0x00600000;
1577
1578 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001579 * Indicates whether the background is opaque.
1580 *
1581 * @hide
1582 */
1583 static final int OPAQUE_BACKGROUND = 0x00800000;
1584
1585 /**
1586 * Indicates whether the scrollbars are opaque.
1587 *
1588 * @hide
1589 */
1590 static final int OPAQUE_SCROLLBARS = 0x01000000;
1591
1592 /**
1593 * Indicates whether the view is opaque.
1594 *
1595 * @hide
1596 */
1597 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001598
Adam Powelle14579b2009-12-16 18:39:52 -08001599 /**
1600 * Indicates a prepressed state;
1601 * the short time between ACTION_DOWN and recognizing
1602 * a 'real' press. Prepressed is used to recognize quick taps
1603 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001604 *
Adam Powelle14579b2009-12-16 18:39:52 -08001605 * @hide
1606 */
1607 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001608
Adam Powellc9fbaab2010-02-16 17:16:19 -08001609 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001610 * Indicates whether the view is temporarily detached.
1611 *
1612 * @hide
1613 */
1614 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001615
Adam Powell8568c3a2010-04-19 14:26:11 -07001616 /**
1617 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001618 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001619 * @hide
1620 */
1621 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001622
1623 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001624 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1625 * for transform operations
1626 *
1627 * @hide
1628 */
Adam Powellf37df072010-09-17 16:22:49 -07001629 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001630
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001631 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001632 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001633
Chet Haasefd2b0022010-08-06 13:08:56 -07001634 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001635 * Indicates that this view was specifically invalidated, not just dirtied because some
1636 * child view was invalidated. The flag is used to determine when we need to recreate
1637 * a view's display list (as opposed to just returning a reference to its existing
1638 * display list).
1639 *
1640 * @hide
1641 */
1642 static final int INVALIDATED = 0x80000000;
1643
1644 /**
Adam Powell637d3372010-08-25 14:37:03 -07001645 * Always allow a user to over-scroll this view, provided it is a
1646 * view that can scroll.
1647 *
1648 * @see #getOverScrollMode()
1649 * @see #setOverScrollMode(int)
1650 */
1651 public static final int OVER_SCROLL_ALWAYS = 0;
1652
1653 /**
1654 * Allow a user to over-scroll this view only if the content is large
1655 * enough to meaningfully scroll, provided it is a view that can scroll.
1656 *
1657 * @see #getOverScrollMode()
1658 * @see #setOverScrollMode(int)
1659 */
1660 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1661
1662 /**
1663 * Never allow a user to over-scroll this view.
1664 *
1665 * @see #getOverScrollMode()
1666 * @see #setOverScrollMode(int)
1667 */
1668 public static final int OVER_SCROLL_NEVER = 2;
1669
1670 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001671 * View has requested the status bar to be visible (the default).
1672 *
Joe Malin32736f02011-01-19 16:14:20 -08001673 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001674 */
1675 public static final int STATUS_BAR_VISIBLE = 0;
1676
1677 /**
1678 * View has requested the status bar to be visible (the default).
1679 *
Joe Malin32736f02011-01-19 16:14:20 -08001680 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001681 */
1682 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1683
1684 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001685 * @hide
1686 *
1687 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1688 * out of the public fields to keep the undefined bits out of the developer's way.
1689 *
1690 * Flag to make the status bar not expandable. Unless you also
1691 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1692 */
1693 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1694
1695 /**
1696 * @hide
1697 *
1698 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1699 * out of the public fields to keep the undefined bits out of the developer's way.
1700 *
1701 * Flag to hide notification icons and scrolling ticker text.
1702 */
1703 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1704
1705 /**
1706 * @hide
1707 *
1708 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1709 * out of the public fields to keep the undefined bits out of the developer's way.
1710 *
1711 * Flag to disable incoming notification alerts. This will not block
1712 * icons, but it will block sound, vibrating and other visual or aural notifications.
1713 */
1714 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1715
1716 /**
1717 * @hide
1718 *
1719 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1720 * out of the public fields to keep the undefined bits out of the developer's way.
1721 *
1722 * Flag to hide only the scrolling ticker. Note that
1723 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1724 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1725 */
1726 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1727
1728 /**
1729 * @hide
1730 *
1731 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1732 * out of the public fields to keep the undefined bits out of the developer's way.
1733 *
1734 * Flag to hide the center system info area.
1735 */
1736 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1737
1738 /**
1739 * @hide
1740 *
1741 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1742 * out of the public fields to keep the undefined bits out of the developer's way.
1743 *
1744 * Flag to hide only the navigation buttons. Don't use this
1745 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001746 *
1747 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001748 */
1749 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1750
1751 /**
1752 * @hide
1753 *
1754 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1755 * out of the public fields to keep the undefined bits out of the developer's way.
1756 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001757 * Flag to hide only the back button. Don't use this
1758 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1759 */
1760 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1761
1762 /**
1763 * @hide
1764 *
1765 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1766 * out of the public fields to keep the undefined bits out of the developer's way.
1767 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001768 * Flag to hide only the clock. You might use this if your activity has
1769 * its own clock making the status bar's clock redundant.
1770 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001771 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1772
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001773
1774 /**
1775 * @hide
1776 */
1777 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
1778
1779
1780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Controls the over-scroll mode for this view.
1782 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1783 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1784 * and {@link #OVER_SCROLL_NEVER}.
1785 */
1786 private int mOverScrollMode;
1787
1788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * The parent this view is attached to.
1790 * {@hide}
1791 *
1792 * @see #getParent()
1793 */
1794 protected ViewParent mParent;
1795
1796 /**
1797 * {@hide}
1798 */
1799 AttachInfo mAttachInfo;
1800
1801 /**
1802 * {@hide}
1803 */
Romain Guy809a7f62009-05-14 15:44:42 -07001804 @ViewDebug.ExportedProperty(flagMapping = {
1805 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1806 name = "FORCE_LAYOUT"),
1807 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1808 name = "LAYOUT_REQUIRED"),
1809 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001810 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001811 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1813 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1815 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int mPrivateFlags;
1817
1818 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001819 * This view's request for the visibility of the status bar.
1820 * @hide
1821 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001822 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001823 int mSystemUiVisibility;
1824
1825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 * Count of how many windows this view has been attached to.
1827 */
1828 int mWindowAttachCount;
1829
1830 /**
1831 * The layout parameters associated with this view and used by the parent
1832 * {@link android.view.ViewGroup} to determine how this view should be
1833 * laid out.
1834 * {@hide}
1835 */
1836 protected ViewGroup.LayoutParams mLayoutParams;
1837
1838 /**
1839 * The view flags hold various views states.
1840 * {@hide}
1841 */
1842 @ViewDebug.ExportedProperty
1843 int mViewFlags;
1844
1845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001846 * The transform matrix for the View. This transform is calculated internally
1847 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1848 * is used by default. Do *not* use this variable directly; instead call
1849 * getMatrix(), which will automatically recalculate the matrix if necessary
1850 * to get the correct matrix based on the latest rotation and scale properties.
1851 */
1852 private final Matrix mMatrix = new Matrix();
1853
1854 /**
1855 * The transform matrix for the View. This transform is calculated internally
1856 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1857 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001858 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001859 * to get the correct matrix based on the latest rotation and scale properties.
1860 */
1861 private Matrix mInverseMatrix;
1862
1863 /**
1864 * An internal variable that tracks whether we need to recalculate the
1865 * transform matrix, based on whether the rotation or scaleX/Y properties
1866 * have changed since the matrix was last calculated.
1867 */
1868 private boolean mMatrixDirty = false;
1869
1870 /**
1871 * An internal variable that tracks whether we need to recalculate the
1872 * transform matrix, based on whether the rotation or scaleX/Y properties
1873 * have changed since the matrix was last calculated.
1874 */
1875 private boolean mInverseMatrixDirty = true;
1876
1877 /**
1878 * A variable that tracks whether we need to recalculate the
1879 * transform matrix, based on whether the rotation or scaleX/Y properties
1880 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001881 * is only valid after a call to updateMatrix() or to a function that
1882 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001883 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001884 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001885
1886 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001887 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1888 */
1889 private Camera mCamera = null;
1890
1891 /**
1892 * This matrix is used when computing the matrix for 3D rotations.
1893 */
1894 private Matrix matrix3D = null;
1895
1896 /**
1897 * These prev values are used to recalculate a centered pivot point when necessary. The
1898 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1899 * set), so thes values are only used then as well.
1900 */
1901 private int mPrevWidth = -1;
1902 private int mPrevHeight = -1;
1903
Joe Malin32736f02011-01-19 16:14:20 -08001904 private boolean mLastIsOpaque;
1905
Chet Haasefd2b0022010-08-06 13:08:56 -07001906 /**
1907 * Convenience value to check for float values that are close enough to zero to be considered
1908 * zero.
1909 */
Romain Guy2542d192010-08-18 11:47:12 -07001910 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001911
1912 /**
1913 * The degrees rotation around the vertical axis through the pivot point.
1914 */
1915 @ViewDebug.ExportedProperty
1916 private float mRotationY = 0f;
1917
1918 /**
1919 * The degrees rotation around the horizontal axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
1922 private float mRotationX = 0f;
1923
1924 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001925 * The degrees rotation around the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
1928 private float mRotation = 0f;
1929
1930 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001931 * The amount of translation of the object away from its left property (post-layout).
1932 */
1933 @ViewDebug.ExportedProperty
1934 private float mTranslationX = 0f;
1935
1936 /**
1937 * The amount of translation of the object away from its top property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
1940 private float mTranslationY = 0f;
1941
1942 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001943 * The amount of scale in the x direction around the pivot point. A
1944 * value of 1 means no scaling is applied.
1945 */
1946 @ViewDebug.ExportedProperty
1947 private float mScaleX = 1f;
1948
1949 /**
1950 * The amount of scale in the y direction around the pivot point. A
1951 * value of 1 means no scaling is applied.
1952 */
1953 @ViewDebug.ExportedProperty
1954 private float mScaleY = 1f;
1955
1956 /**
1957 * The amount of scale in the x direction around the pivot point. A
1958 * value of 1 means no scaling is applied.
1959 */
1960 @ViewDebug.ExportedProperty
1961 private float mPivotX = 0f;
1962
1963 /**
1964 * The amount of scale in the y direction around the pivot point. A
1965 * value of 1 means no scaling is applied.
1966 */
1967 @ViewDebug.ExportedProperty
1968 private float mPivotY = 0f;
1969
1970 /**
1971 * The opacity of the View. This is a value from 0 to 1, where 0 means
1972 * completely transparent and 1 means completely opaque.
1973 */
1974 @ViewDebug.ExportedProperty
1975 private float mAlpha = 1f;
1976
1977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * The distance in pixels from the left edge of this view's parent
1979 * to the left edge of this view.
1980 * {@hide}
1981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 protected int mLeft;
1984 /**
1985 * The distance in pixels from the left edge of this view's parent
1986 * to the right edge of this view.
1987 * {@hide}
1988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 protected int mRight;
1991 /**
1992 * The distance in pixels from the top edge of this view's parent
1993 * to the top edge of this view.
1994 * {@hide}
1995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001996 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected int mTop;
1998 /**
1999 * The distance in pixels from the top edge of this view's parent
2000 * to the bottom edge of this view.
2001 * {@hide}
2002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002003 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 protected int mBottom;
2005
2006 /**
2007 * The offset, in pixels, by which the content of this view is scrolled
2008 * horizontally.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mScrollX;
2013 /**
2014 * The offset, in pixels, by which the content of this view is scrolled
2015 * vertically.
2016 * {@hide}
2017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002018 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 protected int mScrollY;
2020
2021 /**
2022 * The left padding in pixels, that is the distance in pixels between the
2023 * left edge of this view and the left edge of its content.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mPaddingLeft;
2028 /**
2029 * The right padding in pixels, that is the distance in pixels between the
2030 * right edge of this view and the right edge of its content.
2031 * {@hide}
2032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002033 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 protected int mPaddingRight;
2035 /**
2036 * The top padding in pixels, that is the distance in pixels between the
2037 * top edge of this view and the top edge of its content.
2038 * {@hide}
2039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002040 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 protected int mPaddingTop;
2042 /**
2043 * The bottom padding in pixels, that is the distance in pixels between the
2044 * bottom edge of this view and the bottom edge of its content.
2045 * {@hide}
2046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002047 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 protected int mPaddingBottom;
2049
2050 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002051 * Briefly describes the view and is primarily used for accessibility support.
2052 */
2053 private CharSequence mContentDescription;
2054
2055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * Cache the paddingRight set by the user to append to the scrollbar's size.
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int mUserPaddingRight;
2060
2061 /**
2062 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingBottom;
2066
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002067 /**
Adam Powell20232d02010-12-08 21:08:53 -08002068 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2069 */
2070 @ViewDebug.ExportedProperty(category = "padding")
2071 int mUserPaddingLeft;
2072
2073 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002074 * @hide
2075 */
2076 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2077 /**
2078 * @hide
2079 */
2080 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 private Resources mResources = null;
2083
2084 private Drawable mBGDrawable;
2085
2086 private int mBackgroundResource;
2087 private boolean mBackgroundSizeChanged;
2088
2089 /**
2090 * Listener used to dispatch focus change events.
2091 * This field should be made private, so it is hidden from the SDK.
2092 * {@hide}
2093 */
2094 protected OnFocusChangeListener mOnFocusChangeListener;
2095
2096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002097 * Listeners for layout change events.
2098 */
2099 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2100
2101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 * Listener used to dispatch click events.
2103 * This field should be made private, so it is hidden from the SDK.
2104 * {@hide}
2105 */
2106 protected OnClickListener mOnClickListener;
2107
2108 /**
2109 * Listener used to dispatch long click events.
2110 * This field should be made private, so it is hidden from the SDK.
2111 * {@hide}
2112 */
2113 protected OnLongClickListener mOnLongClickListener;
2114
2115 /**
2116 * Listener used to build the context menu.
2117 * This field should be made private, so it is hidden from the SDK.
2118 * {@hide}
2119 */
2120 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2121
2122 private OnKeyListener mOnKeyListener;
2123
2124 private OnTouchListener mOnTouchListener;
2125
Chris Tate32affef2010-10-18 15:29:21 -07002126 private OnDragListener mOnDragListener;
2127
Joe Onorato664644d2011-01-23 17:53:23 -08002128 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 /**
2131 * The application environment this view lives in.
2132 * This field should be made private, so it is hidden from the SDK.
2133 * {@hide}
2134 */
2135 protected Context mContext;
2136
2137 private ScrollabilityCache mScrollCache;
2138
2139 private int[] mDrawableState = null;
2140
Romain Guy0211a0a2011-02-14 16:34:59 -08002141 /**
2142 * Set to true when drawing cache is enabled and cannot be created.
2143 *
2144 * @hide
2145 */
2146 public boolean mCachingFailed;
2147
Romain Guy02890fd2010-08-06 17:58:44 -07002148 private Bitmap mDrawingCache;
2149 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002150 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002151 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152
2153 /**
2154 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2155 * the user may specify which view to go to next.
2156 */
2157 private int mNextFocusLeftId = View.NO_ID;
2158
2159 /**
2160 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2161 * the user may specify which view to go to next.
2162 */
2163 private int mNextFocusRightId = View.NO_ID;
2164
2165 /**
2166 * When this view has focus and the next focus is {@link #FOCUS_UP},
2167 * the user may specify which view to go to next.
2168 */
2169 private int mNextFocusUpId = View.NO_ID;
2170
2171 /**
2172 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2173 * the user may specify which view to go to next.
2174 */
2175 private int mNextFocusDownId = View.NO_ID;
2176
Jeff Brown4e6319b2010-12-13 10:36:51 -08002177 /**
2178 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2179 * the user may specify which view to go to next.
2180 */
2181 int mNextFocusForwardId = View.NO_ID;
2182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002184 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002185 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 private UnsetPressedState mUnsetPressedState;
2188
2189 /**
2190 * Whether the long press's action has been invoked. The tap's action is invoked on the
2191 * up event while a long press is invoked as soon as the long press duration is reached, so
2192 * a long press could be performed before the tap is checked, in which case the tap's action
2193 * should not be invoked.
2194 */
2195 private boolean mHasPerformedLongPress;
2196
2197 /**
2198 * The minimum height of the view. We'll try our best to have the height
2199 * of this view to at least this amount.
2200 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002201 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 private int mMinHeight;
2203
2204 /**
2205 * The minimum width of the view. We'll try our best to have the width
2206 * of this view to at least this amount.
2207 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002208 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 private int mMinWidth;
2210
2211 /**
2212 * The delegate to handle touch events that are physically in this view
2213 * but should be handled by another view.
2214 */
2215 private TouchDelegate mTouchDelegate = null;
2216
2217 /**
2218 * Solid color to use as a background when creating the drawing cache. Enables
2219 * the cache to use 16 bit bitmaps instead of 32 bit.
2220 */
2221 private int mDrawingCacheBackgroundColor = 0;
2222
2223 /**
2224 * Special tree observer used when mAttachInfo is null.
2225 */
2226 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002227
Adam Powelle14579b2009-12-16 18:39:52 -08002228 /**
2229 * Cache the touch slop from the context that created the view.
2230 */
2231 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002234 * Cache drag/drop state
2235 *
2236 */
2237 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002238
2239 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002240 * Flag indicating that a drag can cross window boundaries. When
2241 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2242 * with this flag set, all visible applications will be able to participate
2243 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002244 *
2245 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002246 */
2247 public static final int DRAG_FLAG_GLOBAL = 1;
2248
2249 /**
Adam Powell20232d02010-12-08 21:08:53 -08002250 * Position of the vertical scroll bar.
2251 */
2252 private int mVerticalScrollbarPosition;
2253
2254 /**
2255 * Position the scroll bar at the default position as determined by the system.
2256 */
2257 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2258
2259 /**
2260 * Position the scroll bar along the left edge.
2261 */
2262 public static final int SCROLLBAR_POSITION_LEFT = 1;
2263
2264 /**
2265 * Position the scroll bar along the right edge.
2266 */
2267 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2268
2269 /**
Romain Guy171c5922011-01-06 10:04:23 -08002270 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002271 *
2272 * @see #getLayerType()
2273 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002274 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002275 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002276 */
2277 public static final int LAYER_TYPE_NONE = 0;
2278
2279 /**
2280 * <p>Indicates that the view has a software layer. A software layer is backed
2281 * by a bitmap and causes the view to be rendered using Android's software
2282 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002283 *
Romain Guy171c5922011-01-06 10:04:23 -08002284 * <p>Software layers have various usages:</p>
2285 * <p>When the application is not using hardware acceleration, a software layer
2286 * is useful to apply a specific color filter and/or blending mode and/or
2287 * translucency to a view and all its children.</p>
2288 * <p>When the application is using hardware acceleration, a software layer
2289 * is useful to render drawing primitives not supported by the hardware
2290 * accelerated pipeline. It can also be used to cache a complex view tree
2291 * into a texture and reduce the complexity of drawing operations. For instance,
2292 * when animating a complex view tree with a translation, a software layer can
2293 * be used to render the view tree only once.</p>
2294 * <p>Software layers should be avoided when the affected view tree updates
2295 * often. Every update will require to re-render the software layer, which can
2296 * potentially be slow (particularly when hardware acceleration is turned on
2297 * since the layer will have to be uploaded into a hardware texture after every
2298 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002299 *
2300 * @see #getLayerType()
2301 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002302 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002303 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002304 */
2305 public static final int LAYER_TYPE_SOFTWARE = 1;
2306
2307 /**
2308 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2309 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2310 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2311 * rendering pipeline, but only if hardware acceleration is turned on for the
2312 * view hierarchy. When hardware acceleration is turned off, hardware layers
2313 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002314 *
Romain Guy171c5922011-01-06 10:04:23 -08002315 * <p>A hardware layer is useful to apply a specific color filter and/or
2316 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002317 * <p>A hardware layer can be used to cache a complex view tree into a
2318 * texture and reduce the complexity of drawing operations. For instance,
2319 * when animating a complex view tree with a translation, a hardware layer can
2320 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002321 * <p>A hardware layer can also be used to increase the rendering quality when
2322 * rotation transformations are applied on a view. It can also be used to
2323 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002324 *
2325 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002326 * @see #setLayerType(int, android.graphics.Paint)
2327 * @see #LAYER_TYPE_NONE
2328 * @see #LAYER_TYPE_SOFTWARE
2329 */
2330 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002331
Romain Guy3aaff3a2011-01-12 14:18:47 -08002332 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2333 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2334 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2335 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2336 })
Romain Guy171c5922011-01-06 10:04:23 -08002337 int mLayerType = LAYER_TYPE_NONE;
2338 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002339 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002340
2341 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 * Simple constructor to use when creating a view from code.
2343 *
2344 * @param context The Context the view is running in, through which it can
2345 * access the current theme, resources, etc.
2346 */
2347 public View(Context context) {
2348 mContext = context;
2349 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002350 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002351 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002352 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 }
2354
2355 /**
2356 * Constructor that is called when inflating a view from XML. This is called
2357 * when a view is being constructed from an XML file, supplying attributes
2358 * that were specified in the XML file. This version uses a default style of
2359 * 0, so the only attribute values applied are those in the Context's Theme
2360 * and the given AttributeSet.
2361 *
2362 * <p>
2363 * The method onFinishInflate() will be called after all children have been
2364 * added.
2365 *
2366 * @param context The Context the view is running in, through which it can
2367 * access the current theme, resources, etc.
2368 * @param attrs The attributes of the XML tag that is inflating the view.
2369 * @see #View(Context, AttributeSet, int)
2370 */
2371 public View(Context context, AttributeSet attrs) {
2372 this(context, attrs, 0);
2373 }
2374
2375 /**
2376 * Perform inflation from XML and apply a class-specific base style. This
2377 * constructor of View allows subclasses to use their own base style when
2378 * they are inflating. For example, a Button class's constructor would call
2379 * this version of the super class constructor and supply
2380 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2381 * the theme's button style to modify all of the base view attributes (in
2382 * particular its background) as well as the Button class's attributes.
2383 *
2384 * @param context The Context the view is running in, through which it can
2385 * access the current theme, resources, etc.
2386 * @param attrs The attributes of the XML tag that is inflating the view.
2387 * @param defStyle The default style to apply to this view. If 0, no style
2388 * will be applied (beyond what is included in the theme). This may
2389 * either be an attribute resource, whose value will be retrieved
2390 * from the current theme, or an explicit style resource.
2391 * @see #View(Context, AttributeSet)
2392 */
2393 public View(Context context, AttributeSet attrs, int defStyle) {
2394 this(context);
2395
2396 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2397 defStyle, 0);
2398
2399 Drawable background = null;
2400
2401 int leftPadding = -1;
2402 int topPadding = -1;
2403 int rightPadding = -1;
2404 int bottomPadding = -1;
2405
2406 int padding = -1;
2407
2408 int viewFlagValues = 0;
2409 int viewFlagMasks = 0;
2410
2411 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002412
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002413 int x = 0;
2414 int y = 0;
2415
Chet Haase73066682010-11-29 15:55:32 -08002416 float tx = 0;
2417 float ty = 0;
2418 float rotation = 0;
2419 float rotationX = 0;
2420 float rotationY = 0;
2421 float sx = 1f;
2422 float sy = 1f;
2423 boolean transformSet = false;
2424
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2426
Adam Powell637d3372010-08-25 14:37:03 -07002427 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 final int N = a.getIndexCount();
2429 for (int i = 0; i < N; i++) {
2430 int attr = a.getIndex(i);
2431 switch (attr) {
2432 case com.android.internal.R.styleable.View_background:
2433 background = a.getDrawable(attr);
2434 break;
2435 case com.android.internal.R.styleable.View_padding:
2436 padding = a.getDimensionPixelSize(attr, -1);
2437 break;
2438 case com.android.internal.R.styleable.View_paddingLeft:
2439 leftPadding = a.getDimensionPixelSize(attr, -1);
2440 break;
2441 case com.android.internal.R.styleable.View_paddingTop:
2442 topPadding = a.getDimensionPixelSize(attr, -1);
2443 break;
2444 case com.android.internal.R.styleable.View_paddingRight:
2445 rightPadding = a.getDimensionPixelSize(attr, -1);
2446 break;
2447 case com.android.internal.R.styleable.View_paddingBottom:
2448 bottomPadding = a.getDimensionPixelSize(attr, -1);
2449 break;
2450 case com.android.internal.R.styleable.View_scrollX:
2451 x = a.getDimensionPixelOffset(attr, 0);
2452 break;
2453 case com.android.internal.R.styleable.View_scrollY:
2454 y = a.getDimensionPixelOffset(attr, 0);
2455 break;
Chet Haase73066682010-11-29 15:55:32 -08002456 case com.android.internal.R.styleable.View_alpha:
2457 setAlpha(a.getFloat(attr, 1f));
2458 break;
2459 case com.android.internal.R.styleable.View_transformPivotX:
2460 setPivotX(a.getDimensionPixelOffset(attr, 0));
2461 break;
2462 case com.android.internal.R.styleable.View_transformPivotY:
2463 setPivotY(a.getDimensionPixelOffset(attr, 0));
2464 break;
2465 case com.android.internal.R.styleable.View_translationX:
2466 tx = a.getDimensionPixelOffset(attr, 0);
2467 transformSet = true;
2468 break;
2469 case com.android.internal.R.styleable.View_translationY:
2470 ty = a.getDimensionPixelOffset(attr, 0);
2471 transformSet = true;
2472 break;
2473 case com.android.internal.R.styleable.View_rotation:
2474 rotation = a.getFloat(attr, 0);
2475 transformSet = true;
2476 break;
2477 case com.android.internal.R.styleable.View_rotationX:
2478 rotationX = a.getFloat(attr, 0);
2479 transformSet = true;
2480 break;
2481 case com.android.internal.R.styleable.View_rotationY:
2482 rotationY = a.getFloat(attr, 0);
2483 transformSet = true;
2484 break;
2485 case com.android.internal.R.styleable.View_scaleX:
2486 sx = a.getFloat(attr, 1f);
2487 transformSet = true;
2488 break;
2489 case com.android.internal.R.styleable.View_scaleY:
2490 sy = a.getFloat(attr, 1f);
2491 transformSet = true;
2492 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002493 case com.android.internal.R.styleable.View_id:
2494 mID = a.getResourceId(attr, NO_ID);
2495 break;
2496 case com.android.internal.R.styleable.View_tag:
2497 mTag = a.getText(attr);
2498 break;
2499 case com.android.internal.R.styleable.View_fitsSystemWindows:
2500 if (a.getBoolean(attr, false)) {
2501 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2502 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2503 }
2504 break;
2505 case com.android.internal.R.styleable.View_focusable:
2506 if (a.getBoolean(attr, false)) {
2507 viewFlagValues |= FOCUSABLE;
2508 viewFlagMasks |= FOCUSABLE_MASK;
2509 }
2510 break;
2511 case com.android.internal.R.styleable.View_focusableInTouchMode:
2512 if (a.getBoolean(attr, false)) {
2513 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2514 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2515 }
2516 break;
2517 case com.android.internal.R.styleable.View_clickable:
2518 if (a.getBoolean(attr, false)) {
2519 viewFlagValues |= CLICKABLE;
2520 viewFlagMasks |= CLICKABLE;
2521 }
2522 break;
2523 case com.android.internal.R.styleable.View_longClickable:
2524 if (a.getBoolean(attr, false)) {
2525 viewFlagValues |= LONG_CLICKABLE;
2526 viewFlagMasks |= LONG_CLICKABLE;
2527 }
2528 break;
2529 case com.android.internal.R.styleable.View_saveEnabled:
2530 if (!a.getBoolean(attr, true)) {
2531 viewFlagValues |= SAVE_DISABLED;
2532 viewFlagMasks |= SAVE_DISABLED_MASK;
2533 }
2534 break;
2535 case com.android.internal.R.styleable.View_duplicateParentState:
2536 if (a.getBoolean(attr, false)) {
2537 viewFlagValues |= DUPLICATE_PARENT_STATE;
2538 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2539 }
2540 break;
2541 case com.android.internal.R.styleable.View_visibility:
2542 final int visibility = a.getInt(attr, 0);
2543 if (visibility != 0) {
2544 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2545 viewFlagMasks |= VISIBILITY_MASK;
2546 }
2547 break;
2548 case com.android.internal.R.styleable.View_drawingCacheQuality:
2549 final int cacheQuality = a.getInt(attr, 0);
2550 if (cacheQuality != 0) {
2551 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2552 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2553 }
2554 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002555 case com.android.internal.R.styleable.View_contentDescription:
2556 mContentDescription = a.getString(attr);
2557 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002558 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2559 if (!a.getBoolean(attr, true)) {
2560 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2561 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2562 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002563 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002564 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2565 if (!a.getBoolean(attr, true)) {
2566 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2567 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2568 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002569 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002570 case R.styleable.View_scrollbars:
2571 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2572 if (scrollbars != SCROLLBARS_NONE) {
2573 viewFlagValues |= scrollbars;
2574 viewFlagMasks |= SCROLLBARS_MASK;
2575 initializeScrollbars(a);
2576 }
2577 break;
2578 case R.styleable.View_fadingEdge:
2579 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2580 if (fadingEdge != FADING_EDGE_NONE) {
2581 viewFlagValues |= fadingEdge;
2582 viewFlagMasks |= FADING_EDGE_MASK;
2583 initializeFadingEdge(a);
2584 }
2585 break;
2586 case R.styleable.View_scrollbarStyle:
2587 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2588 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2589 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2590 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2591 }
2592 break;
2593 case R.styleable.View_isScrollContainer:
2594 setScrollContainer = true;
2595 if (a.getBoolean(attr, false)) {
2596 setScrollContainer(true);
2597 }
2598 break;
2599 case com.android.internal.R.styleable.View_keepScreenOn:
2600 if (a.getBoolean(attr, false)) {
2601 viewFlagValues |= KEEP_SCREEN_ON;
2602 viewFlagMasks |= KEEP_SCREEN_ON;
2603 }
2604 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002605 case R.styleable.View_filterTouchesWhenObscured:
2606 if (a.getBoolean(attr, false)) {
2607 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2608 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2609 }
2610 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 case R.styleable.View_nextFocusLeft:
2612 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2613 break;
2614 case R.styleable.View_nextFocusRight:
2615 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2616 break;
2617 case R.styleable.View_nextFocusUp:
2618 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2619 break;
2620 case R.styleable.View_nextFocusDown:
2621 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2622 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002623 case R.styleable.View_nextFocusForward:
2624 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2625 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002626 case R.styleable.View_minWidth:
2627 mMinWidth = a.getDimensionPixelSize(attr, 0);
2628 break;
2629 case R.styleable.View_minHeight:
2630 mMinHeight = a.getDimensionPixelSize(attr, 0);
2631 break;
Romain Guy9a817362009-05-01 10:57:14 -07002632 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002633 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002634 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002635 + "be used within a restricted context");
2636 }
2637
Romain Guy9a817362009-05-01 10:57:14 -07002638 final String handlerName = a.getString(attr);
2639 if (handlerName != null) {
2640 setOnClickListener(new OnClickListener() {
2641 private Method mHandler;
2642
2643 public void onClick(View v) {
2644 if (mHandler == null) {
2645 try {
2646 mHandler = getContext().getClass().getMethod(handlerName,
2647 View.class);
2648 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002649 int id = getId();
2650 String idText = id == NO_ID ? "" : " with id '"
2651 + getContext().getResources().getResourceEntryName(
2652 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002653 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002654 handlerName + "(View) in the activity "
2655 + getContext().getClass() + " for onClick handler"
2656 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002657 }
2658 }
2659
2660 try {
2661 mHandler.invoke(getContext(), View.this);
2662 } catch (IllegalAccessException e) {
2663 throw new IllegalStateException("Could not execute non "
2664 + "public method of the activity", e);
2665 } catch (InvocationTargetException e) {
2666 throw new IllegalStateException("Could not execute "
2667 + "method of the activity", e);
2668 }
2669 }
2670 });
2671 }
2672 break;
Adam Powell637d3372010-08-25 14:37:03 -07002673 case R.styleable.View_overScrollMode:
2674 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2675 break;
Adam Powell20232d02010-12-08 21:08:53 -08002676 case R.styleable.View_verticalScrollbarPosition:
2677 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2678 break;
Romain Guy171c5922011-01-06 10:04:23 -08002679 case R.styleable.View_layerType:
2680 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2681 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002682 }
2683 }
2684
Adam Powell637d3372010-08-25 14:37:03 -07002685 setOverScrollMode(overScrollMode);
2686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 if (background != null) {
2688 setBackgroundDrawable(background);
2689 }
2690
2691 if (padding >= 0) {
2692 leftPadding = padding;
2693 topPadding = padding;
2694 rightPadding = padding;
2695 bottomPadding = padding;
2696 }
2697
2698 // If the user specified the padding (either with android:padding or
2699 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2700 // use the default padding or the padding from the background drawable
2701 // (stored at this point in mPadding*)
2702 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2703 topPadding >= 0 ? topPadding : mPaddingTop,
2704 rightPadding >= 0 ? rightPadding : mPaddingRight,
2705 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2706
2707 if (viewFlagMasks != 0) {
2708 setFlags(viewFlagValues, viewFlagMasks);
2709 }
2710
2711 // Needs to be called after mViewFlags is set
2712 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2713 recomputePadding();
2714 }
2715
2716 if (x != 0 || y != 0) {
2717 scrollTo(x, y);
2718 }
2719
Chet Haase73066682010-11-29 15:55:32 -08002720 if (transformSet) {
2721 setTranslationX(tx);
2722 setTranslationY(ty);
2723 setRotation(rotation);
2724 setRotationX(rotationX);
2725 setRotationY(rotationY);
2726 setScaleX(sx);
2727 setScaleY(sy);
2728 }
2729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002730 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2731 setScrollContainer(true);
2732 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002733
2734 computeOpaqueFlags();
2735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002736 a.recycle();
2737 }
2738
2739 /**
2740 * Non-public constructor for use in testing
2741 */
2742 View() {
2743 }
2744
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002745 /**
2746 * <p>
2747 * Initializes the fading edges from a given set of styled attributes. This
2748 * method should be called by subclasses that need fading edges and when an
2749 * instance of these subclasses is created programmatically rather than
2750 * being inflated from XML. This method is automatically called when the XML
2751 * is inflated.
2752 * </p>
2753 *
2754 * @param a the styled attributes set to initialize the fading edges from
2755 */
2756 protected void initializeFadingEdge(TypedArray a) {
2757 initScrollCache();
2758
2759 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2760 R.styleable.View_fadingEdgeLength,
2761 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2762 }
2763
2764 /**
2765 * Returns the size of the vertical faded edges used to indicate that more
2766 * content in this view is visible.
2767 *
2768 * @return The size in pixels of the vertical faded edge or 0 if vertical
2769 * faded edges are not enabled for this view.
2770 * @attr ref android.R.styleable#View_fadingEdgeLength
2771 */
2772 public int getVerticalFadingEdgeLength() {
2773 if (isVerticalFadingEdgeEnabled()) {
2774 ScrollabilityCache cache = mScrollCache;
2775 if (cache != null) {
2776 return cache.fadingEdgeLength;
2777 }
2778 }
2779 return 0;
2780 }
2781
2782 /**
2783 * Set the size of the faded edge used to indicate that more content in this
2784 * view is available. Will not change whether the fading edge is enabled; use
2785 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2786 * to enable the fading edge for the vertical or horizontal fading edges.
2787 *
2788 * @param length The size in pixels of the faded edge used to indicate that more
2789 * content in this view is visible.
2790 */
2791 public void setFadingEdgeLength(int length) {
2792 initScrollCache();
2793 mScrollCache.fadingEdgeLength = length;
2794 }
2795
2796 /**
2797 * Returns the size of the horizontal faded edges used to indicate that more
2798 * content in this view is visible.
2799 *
2800 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2801 * faded edges are not enabled for this view.
2802 * @attr ref android.R.styleable#View_fadingEdgeLength
2803 */
2804 public int getHorizontalFadingEdgeLength() {
2805 if (isHorizontalFadingEdgeEnabled()) {
2806 ScrollabilityCache cache = mScrollCache;
2807 if (cache != null) {
2808 return cache.fadingEdgeLength;
2809 }
2810 }
2811 return 0;
2812 }
2813
2814 /**
2815 * Returns the width of the vertical scrollbar.
2816 *
2817 * @return The width in pixels of the vertical scrollbar or 0 if there
2818 * is no vertical scrollbar.
2819 */
2820 public int getVerticalScrollbarWidth() {
2821 ScrollabilityCache cache = mScrollCache;
2822 if (cache != null) {
2823 ScrollBarDrawable scrollBar = cache.scrollBar;
2824 if (scrollBar != null) {
2825 int size = scrollBar.getSize(true);
2826 if (size <= 0) {
2827 size = cache.scrollBarSize;
2828 }
2829 return size;
2830 }
2831 return 0;
2832 }
2833 return 0;
2834 }
2835
2836 /**
2837 * Returns the height of the horizontal scrollbar.
2838 *
2839 * @return The height in pixels of the horizontal scrollbar or 0 if
2840 * there is no horizontal scrollbar.
2841 */
2842 protected int getHorizontalScrollbarHeight() {
2843 ScrollabilityCache cache = mScrollCache;
2844 if (cache != null) {
2845 ScrollBarDrawable scrollBar = cache.scrollBar;
2846 if (scrollBar != null) {
2847 int size = scrollBar.getSize(false);
2848 if (size <= 0) {
2849 size = cache.scrollBarSize;
2850 }
2851 return size;
2852 }
2853 return 0;
2854 }
2855 return 0;
2856 }
2857
2858 /**
2859 * <p>
2860 * Initializes the scrollbars from a given set of styled attributes. This
2861 * method should be called by subclasses that need scrollbars and when an
2862 * instance of these subclasses is created programmatically rather than
2863 * being inflated from XML. This method is automatically called when the XML
2864 * is inflated.
2865 * </p>
2866 *
2867 * @param a the styled attributes set to initialize the scrollbars from
2868 */
2869 protected void initializeScrollbars(TypedArray a) {
2870 initScrollCache();
2871
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002873
Mike Cleronf116bf82009-09-27 19:14:12 -07002874 if (scrollabilityCache.scrollBar == null) {
2875 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2876 }
Joe Malin32736f02011-01-19 16:14:20 -08002877
Romain Guy8bda2482010-03-02 11:42:11 -08002878 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002879
Mike Cleronf116bf82009-09-27 19:14:12 -07002880 if (!fadeScrollbars) {
2881 scrollabilityCache.state = ScrollabilityCache.ON;
2882 }
2883 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002884
2885
Mike Cleronf116bf82009-09-27 19:14:12 -07002886 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2887 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2888 .getScrollBarFadeDuration());
2889 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2890 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2891 ViewConfiguration.getScrollDefaultDelay());
2892
Joe Malin32736f02011-01-19 16:14:20 -08002893
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002894 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2895 com.android.internal.R.styleable.View_scrollbarSize,
2896 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2897
2898 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2899 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2900
2901 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2902 if (thumb != null) {
2903 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2904 }
2905
2906 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2907 false);
2908 if (alwaysDraw) {
2909 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2910 }
2911
2912 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2913 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2914
2915 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2916 if (thumb != null) {
2917 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2918 }
2919
2920 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2921 false);
2922 if (alwaysDraw) {
2923 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2924 }
2925
2926 // Re-apply user/background padding so that scrollbar(s) get added
2927 recomputePadding();
2928 }
2929
2930 /**
2931 * <p>
2932 * Initalizes the scrollability cache if necessary.
2933 * </p>
2934 */
2935 private void initScrollCache() {
2936 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002937 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002938 }
2939 }
2940
2941 /**
Adam Powell20232d02010-12-08 21:08:53 -08002942 * Set the position of the vertical scroll bar. Should be one of
2943 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2944 * {@link #SCROLLBAR_POSITION_RIGHT}.
2945 *
2946 * @param position Where the vertical scroll bar should be positioned.
2947 */
2948 public void setVerticalScrollbarPosition(int position) {
2949 if (mVerticalScrollbarPosition != position) {
2950 mVerticalScrollbarPosition = position;
2951 computeOpaqueFlags();
2952 recomputePadding();
2953 }
2954 }
2955
2956 /**
2957 * @return The position where the vertical scroll bar will show, if applicable.
2958 * @see #setVerticalScrollbarPosition(int)
2959 */
2960 public int getVerticalScrollbarPosition() {
2961 return mVerticalScrollbarPosition;
2962 }
2963
2964 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002965 * Register a callback to be invoked when focus of this view changed.
2966 *
2967 * @param l The callback that will run.
2968 */
2969 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2970 mOnFocusChangeListener = l;
2971 }
2972
2973 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002974 * Add a listener that will be called when the bounds of the view change due to
2975 * layout processing.
2976 *
2977 * @param listener The listener that will be called when layout bounds change.
2978 */
2979 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2980 if (mOnLayoutChangeListeners == null) {
2981 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2982 }
2983 mOnLayoutChangeListeners.add(listener);
2984 }
2985
2986 /**
2987 * Remove a listener for layout changes.
2988 *
2989 * @param listener The listener for layout bounds change.
2990 */
2991 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
2992 if (mOnLayoutChangeListeners == null) {
2993 return;
2994 }
2995 mOnLayoutChangeListeners.remove(listener);
2996 }
2997
2998 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 * Returns the focus-change callback registered for this view.
3000 *
3001 * @return The callback, or null if one is not registered.
3002 */
3003 public OnFocusChangeListener getOnFocusChangeListener() {
3004 return mOnFocusChangeListener;
3005 }
3006
3007 /**
3008 * Register a callback to be invoked when this view is clicked. If this view is not
3009 * clickable, it becomes clickable.
3010 *
3011 * @param l The callback that will run
3012 *
3013 * @see #setClickable(boolean)
3014 */
3015 public void setOnClickListener(OnClickListener l) {
3016 if (!isClickable()) {
3017 setClickable(true);
3018 }
3019 mOnClickListener = l;
3020 }
3021
3022 /**
3023 * Register a callback to be invoked when this view is clicked and held. If this view is not
3024 * long clickable, it becomes long clickable.
3025 *
3026 * @param l The callback that will run
3027 *
3028 * @see #setLongClickable(boolean)
3029 */
3030 public void setOnLongClickListener(OnLongClickListener l) {
3031 if (!isLongClickable()) {
3032 setLongClickable(true);
3033 }
3034 mOnLongClickListener = l;
3035 }
3036
3037 /**
3038 * Register a callback to be invoked when the context menu for this view is
3039 * being built. If this view is not long clickable, it becomes long clickable.
3040 *
3041 * @param l The callback that will run
3042 *
3043 */
3044 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3045 if (!isLongClickable()) {
3046 setLongClickable(true);
3047 }
3048 mOnCreateContextMenuListener = l;
3049 }
3050
3051 /**
3052 * Call this view's OnClickListener, if it is defined.
3053 *
3054 * @return True there was an assigned OnClickListener that was called, false
3055 * otherwise is returned.
3056 */
3057 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003058 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 if (mOnClickListener != null) {
3061 playSoundEffect(SoundEffectConstants.CLICK);
3062 mOnClickListener.onClick(this);
3063 return true;
3064 }
3065
3066 return false;
3067 }
3068
3069 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003070 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3071 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003073 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003074 */
3075 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003076 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3077
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003078 boolean handled = false;
3079 if (mOnLongClickListener != null) {
3080 handled = mOnLongClickListener.onLongClick(View.this);
3081 }
3082 if (!handled) {
3083 handled = showContextMenu();
3084 }
3085 if (handled) {
3086 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3087 }
3088 return handled;
3089 }
3090
3091 /**
3092 * Bring up the context menu for this view.
3093 *
3094 * @return Whether a context menu was displayed.
3095 */
3096 public boolean showContextMenu() {
3097 return getParent().showContextMenuForChild(this);
3098 }
3099
3100 /**
Adam Powell6e346362010-07-23 10:18:23 -07003101 * Start an action mode.
3102 *
3103 * @param callback Callback that will control the lifecycle of the action mode
3104 * @return The new action mode if it is started, null otherwise
3105 *
3106 * @see ActionMode
3107 */
3108 public ActionMode startActionMode(ActionMode.Callback callback) {
3109 return getParent().startActionModeForChild(this, callback);
3110 }
3111
3112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003113 * Register a callback to be invoked when a key is pressed in this view.
3114 * @param l the key listener to attach to this view
3115 */
3116 public void setOnKeyListener(OnKeyListener l) {
3117 mOnKeyListener = l;
3118 }
3119
3120 /**
3121 * Register a callback to be invoked when a touch event is sent to this view.
3122 * @param l the touch listener to attach to this view
3123 */
3124 public void setOnTouchListener(OnTouchListener l) {
3125 mOnTouchListener = l;
3126 }
3127
3128 /**
Joe Malin32736f02011-01-19 16:14:20 -08003129 * Register a drag event listener callback object for this View. The parameter is
3130 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3131 * View, the system calls the
3132 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3133 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003134 */
3135 public void setOnDragListener(OnDragListener l) {
3136 mOnDragListener = l;
3137 }
3138
3139 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003140 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3141 *
3142 * Note: this does not check whether this {@link View} should get focus, it just
3143 * gives it focus no matter what. It should only be called internally by framework
3144 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3145 *
3146 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
3147 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
3148 * focus moved when requestFocus() is called. It may not always
3149 * apply, in which case use the default View.FOCUS_DOWN.
3150 * @param previouslyFocusedRect The rectangle of the view that had focus
3151 * prior in this View's coordinate system.
3152 */
3153 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3154 if (DBG) {
3155 System.out.println(this + " requestFocus()");
3156 }
3157
3158 if ((mPrivateFlags & FOCUSED) == 0) {
3159 mPrivateFlags |= FOCUSED;
3160
3161 if (mParent != null) {
3162 mParent.requestChildFocus(this, this);
3163 }
3164
3165 onFocusChanged(true, direction, previouslyFocusedRect);
3166 refreshDrawableState();
3167 }
3168 }
3169
3170 /**
3171 * Request that a rectangle of this view be visible on the screen,
3172 * scrolling if necessary just enough.
3173 *
3174 * <p>A View should call this if it maintains some notion of which part
3175 * of its content is interesting. For example, a text editing view
3176 * should call this when its cursor moves.
3177 *
3178 * @param rectangle The rectangle.
3179 * @return Whether any parent scrolled.
3180 */
3181 public boolean requestRectangleOnScreen(Rect rectangle) {
3182 return requestRectangleOnScreen(rectangle, false);
3183 }
3184
3185 /**
3186 * Request that a rectangle of this view be visible on the screen,
3187 * scrolling if necessary just enough.
3188 *
3189 * <p>A View should call this if it maintains some notion of which part
3190 * of its content is interesting. For example, a text editing view
3191 * should call this when its cursor moves.
3192 *
3193 * <p>When <code>immediate</code> is set to true, scrolling will not be
3194 * animated.
3195 *
3196 * @param rectangle The rectangle.
3197 * @param immediate True to forbid animated scrolling, false otherwise
3198 * @return Whether any parent scrolled.
3199 */
3200 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3201 View child = this;
3202 ViewParent parent = mParent;
3203 boolean scrolled = false;
3204 while (parent != null) {
3205 scrolled |= parent.requestChildRectangleOnScreen(child,
3206 rectangle, immediate);
3207
3208 // offset rect so next call has the rectangle in the
3209 // coordinate system of its direct child.
3210 rectangle.offset(child.getLeft(), child.getTop());
3211 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3212
3213 if (!(parent instanceof View)) {
3214 break;
3215 }
Romain Guy8506ab42009-06-11 17:35:47 -07003216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 child = (View) parent;
3218 parent = child.getParent();
3219 }
3220 return scrolled;
3221 }
3222
3223 /**
3224 * Called when this view wants to give up focus. This will cause
3225 * {@link #onFocusChanged} to be called.
3226 */
3227 public void clearFocus() {
3228 if (DBG) {
3229 System.out.println(this + " clearFocus()");
3230 }
3231
3232 if ((mPrivateFlags & FOCUSED) != 0) {
3233 mPrivateFlags &= ~FOCUSED;
3234
3235 if (mParent != null) {
3236 mParent.clearChildFocus(this);
3237 }
3238
3239 onFocusChanged(false, 0, null);
3240 refreshDrawableState();
3241 }
3242 }
3243
3244 /**
3245 * Called to clear the focus of a view that is about to be removed.
3246 * Doesn't call clearChildFocus, which prevents this view from taking
3247 * focus again before it has been removed from the parent
3248 */
3249 void clearFocusForRemoval() {
3250 if ((mPrivateFlags & FOCUSED) != 0) {
3251 mPrivateFlags &= ~FOCUSED;
3252
3253 onFocusChanged(false, 0, null);
3254 refreshDrawableState();
3255 }
3256 }
3257
3258 /**
3259 * Called internally by the view system when a new view is getting focus.
3260 * This is what clears the old focus.
3261 */
3262 void unFocus() {
3263 if (DBG) {
3264 System.out.println(this + " unFocus()");
3265 }
3266
3267 if ((mPrivateFlags & FOCUSED) != 0) {
3268 mPrivateFlags &= ~FOCUSED;
3269
3270 onFocusChanged(false, 0, null);
3271 refreshDrawableState();
3272 }
3273 }
3274
3275 /**
3276 * Returns true if this view has focus iteself, or is the ancestor of the
3277 * view that has focus.
3278 *
3279 * @return True if this view has or contains focus, false otherwise.
3280 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003281 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003282 public boolean hasFocus() {
3283 return (mPrivateFlags & FOCUSED) != 0;
3284 }
3285
3286 /**
3287 * Returns true if this view is focusable or if it contains a reachable View
3288 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3289 * is a View whose parents do not block descendants focus.
3290 *
3291 * Only {@link #VISIBLE} views are considered focusable.
3292 *
3293 * @return True if the view is focusable or if the view contains a focusable
3294 * View, false otherwise.
3295 *
3296 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3297 */
3298 public boolean hasFocusable() {
3299 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3300 }
3301
3302 /**
3303 * Called by the view system when the focus state of this view changes.
3304 * When the focus change event is caused by directional navigation, direction
3305 * and previouslyFocusedRect provide insight into where the focus is coming from.
3306 * When overriding, be sure to call up through to the super class so that
3307 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 * @param gainFocus True if the View has focus; false otherwise.
3310 * @param direction The direction focus has moved when requestFocus()
3311 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003312 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3313 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3314 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003315 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3316 * system, of the previously focused view. If applicable, this will be
3317 * passed in as finer grained information about where the focus is coming
3318 * from (in addition to direction). Will be <code>null</code> otherwise.
3319 */
3320 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003321 if (gainFocus) {
3322 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3323 }
3324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 InputMethodManager imm = InputMethodManager.peekInstance();
3326 if (!gainFocus) {
3327 if (isPressed()) {
3328 setPressed(false);
3329 }
3330 if (imm != null && mAttachInfo != null
3331 && mAttachInfo.mHasWindowFocus) {
3332 imm.focusOut(this);
3333 }
Romain Guya2431d02009-04-30 16:30:00 -07003334 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003335 } else if (imm != null && mAttachInfo != null
3336 && mAttachInfo.mHasWindowFocus) {
3337 imm.focusIn(this);
3338 }
Romain Guy8506ab42009-06-11 17:35:47 -07003339
Romain Guy0fd89bf2011-01-26 15:41:30 -08003340 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003341 if (mOnFocusChangeListener != null) {
3342 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3343 }
Joe Malin32736f02011-01-19 16:14:20 -08003344
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003345 if (mAttachInfo != null) {
3346 mAttachInfo.mKeyDispatchState.reset(this);
3347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003348 }
3349
3350 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003351 * {@inheritDoc}
3352 */
3353 public void sendAccessibilityEvent(int eventType) {
3354 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3355 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3356 }
3357 }
3358
3359 /**
3360 * {@inheritDoc}
3361 */
3362 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003363 if (!isShown()) {
3364 return;
3365 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003366 event.setClassName(getClass().getName());
3367 event.setPackageName(getContext().getPackageName());
3368 event.setEnabled(isEnabled());
3369 event.setContentDescription(mContentDescription);
3370
3371 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3372 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3373 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3374 event.setItemCount(focusablesTempList.size());
3375 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3376 focusablesTempList.clear();
3377 }
3378
3379 dispatchPopulateAccessibilityEvent(event);
3380
3381 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3382 }
3383
3384 /**
3385 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3386 * to be populated.
3387 *
3388 * @param event The event.
3389 *
3390 * @return True if the event population was completed.
3391 */
3392 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3393 return false;
3394 }
3395
3396 /**
3397 * Gets the {@link View} description. It briefly describes the view and is
3398 * primarily used for accessibility support. Set this property to enable
3399 * better accessibility support for your application. This is especially
3400 * true for views that do not have textual representation (For example,
3401 * ImageButton).
3402 *
3403 * @return The content descriptiopn.
3404 *
3405 * @attr ref android.R.styleable#View_contentDescription
3406 */
3407 public CharSequence getContentDescription() {
3408 return mContentDescription;
3409 }
3410
3411 /**
3412 * Sets the {@link View} description. It briefly describes the view and is
3413 * primarily used for accessibility support. Set this property to enable
3414 * better accessibility support for your application. This is especially
3415 * true for views that do not have textual representation (For example,
3416 * ImageButton).
3417 *
3418 * @param contentDescription The content description.
3419 *
3420 * @attr ref android.R.styleable#View_contentDescription
3421 */
3422 public void setContentDescription(CharSequence contentDescription) {
3423 mContentDescription = contentDescription;
3424 }
3425
3426 /**
Romain Guya2431d02009-04-30 16:30:00 -07003427 * Invoked whenever this view loses focus, either by losing window focus or by losing
3428 * focus within its window. This method can be used to clear any state tied to the
3429 * focus. For instance, if a button is held pressed with the trackball and the window
3430 * loses focus, this method can be used to cancel the press.
3431 *
3432 * Subclasses of View overriding this method should always call super.onFocusLost().
3433 *
3434 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003435 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003436 *
3437 * @hide pending API council approval
3438 */
3439 protected void onFocusLost() {
3440 resetPressedState();
3441 }
3442
3443 private void resetPressedState() {
3444 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3445 return;
3446 }
3447
3448 if (isPressed()) {
3449 setPressed(false);
3450
3451 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003452 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003453 }
3454 }
3455 }
3456
3457 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 * Returns true if this view has focus
3459 *
3460 * @return True if this view has focus, false otherwise.
3461 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003462 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003463 public boolean isFocused() {
3464 return (mPrivateFlags & FOCUSED) != 0;
3465 }
3466
3467 /**
3468 * Find the view in the hierarchy rooted at this view that currently has
3469 * focus.
3470 *
3471 * @return The view that currently has focus, or null if no focused view can
3472 * be found.
3473 */
3474 public View findFocus() {
3475 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3476 }
3477
3478 /**
3479 * Change whether this view is one of the set of scrollable containers in
3480 * its window. This will be used to determine whether the window can
3481 * resize or must pan when a soft input area is open -- scrollable
3482 * containers allow the window to use resize mode since the container
3483 * will appropriately shrink.
3484 */
3485 public void setScrollContainer(boolean isScrollContainer) {
3486 if (isScrollContainer) {
3487 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3488 mAttachInfo.mScrollContainers.add(this);
3489 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3490 }
3491 mPrivateFlags |= SCROLL_CONTAINER;
3492 } else {
3493 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3494 mAttachInfo.mScrollContainers.remove(this);
3495 }
3496 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3497 }
3498 }
3499
3500 /**
3501 * Returns the quality of the drawing cache.
3502 *
3503 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3504 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3505 *
3506 * @see #setDrawingCacheQuality(int)
3507 * @see #setDrawingCacheEnabled(boolean)
3508 * @see #isDrawingCacheEnabled()
3509 *
3510 * @attr ref android.R.styleable#View_drawingCacheQuality
3511 */
3512 public int getDrawingCacheQuality() {
3513 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3514 }
3515
3516 /**
3517 * Set the drawing cache quality of this view. This value is used only when the
3518 * drawing cache is enabled
3519 *
3520 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3521 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3522 *
3523 * @see #getDrawingCacheQuality()
3524 * @see #setDrawingCacheEnabled(boolean)
3525 * @see #isDrawingCacheEnabled()
3526 *
3527 * @attr ref android.R.styleable#View_drawingCacheQuality
3528 */
3529 public void setDrawingCacheQuality(int quality) {
3530 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3531 }
3532
3533 /**
3534 * Returns whether the screen should remain on, corresponding to the current
3535 * value of {@link #KEEP_SCREEN_ON}.
3536 *
3537 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3538 *
3539 * @see #setKeepScreenOn(boolean)
3540 *
3541 * @attr ref android.R.styleable#View_keepScreenOn
3542 */
3543 public boolean getKeepScreenOn() {
3544 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3545 }
3546
3547 /**
3548 * Controls whether the screen should remain on, modifying the
3549 * value of {@link #KEEP_SCREEN_ON}.
3550 *
3551 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3552 *
3553 * @see #getKeepScreenOn()
3554 *
3555 * @attr ref android.R.styleable#View_keepScreenOn
3556 */
3557 public void setKeepScreenOn(boolean keepScreenOn) {
3558 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3559 }
3560
3561 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003562 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3563 * @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 -08003564 *
3565 * @attr ref android.R.styleable#View_nextFocusLeft
3566 */
3567 public int getNextFocusLeftId() {
3568 return mNextFocusLeftId;
3569 }
3570
3571 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003572 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3573 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3574 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003575 *
3576 * @attr ref android.R.styleable#View_nextFocusLeft
3577 */
3578 public void setNextFocusLeftId(int nextFocusLeftId) {
3579 mNextFocusLeftId = nextFocusLeftId;
3580 }
3581
3582 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003583 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3584 * @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 -08003585 *
3586 * @attr ref android.R.styleable#View_nextFocusRight
3587 */
3588 public int getNextFocusRightId() {
3589 return mNextFocusRightId;
3590 }
3591
3592 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003593 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3594 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3595 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003596 *
3597 * @attr ref android.R.styleable#View_nextFocusRight
3598 */
3599 public void setNextFocusRightId(int nextFocusRightId) {
3600 mNextFocusRightId = nextFocusRightId;
3601 }
3602
3603 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003604 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3605 * @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 -08003606 *
3607 * @attr ref android.R.styleable#View_nextFocusUp
3608 */
3609 public int getNextFocusUpId() {
3610 return mNextFocusUpId;
3611 }
3612
3613 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003614 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3615 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3616 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 *
3618 * @attr ref android.R.styleable#View_nextFocusUp
3619 */
3620 public void setNextFocusUpId(int nextFocusUpId) {
3621 mNextFocusUpId = nextFocusUpId;
3622 }
3623
3624 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003625 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3626 * @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 -08003627 *
3628 * @attr ref android.R.styleable#View_nextFocusDown
3629 */
3630 public int getNextFocusDownId() {
3631 return mNextFocusDownId;
3632 }
3633
3634 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003635 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3636 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3637 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 *
3639 * @attr ref android.R.styleable#View_nextFocusDown
3640 */
3641 public void setNextFocusDownId(int nextFocusDownId) {
3642 mNextFocusDownId = nextFocusDownId;
3643 }
3644
3645 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003646 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3647 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3648 *
3649 * @attr ref android.R.styleable#View_nextFocusForward
3650 */
3651 public int getNextFocusForwardId() {
3652 return mNextFocusForwardId;
3653 }
3654
3655 /**
3656 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3657 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3658 * decide automatically.
3659 *
3660 * @attr ref android.R.styleable#View_nextFocusForward
3661 */
3662 public void setNextFocusForwardId(int nextFocusForwardId) {
3663 mNextFocusForwardId = nextFocusForwardId;
3664 }
3665
3666 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 * Returns the visibility of this view and all of its ancestors
3668 *
3669 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3670 */
3671 public boolean isShown() {
3672 View current = this;
3673 //noinspection ConstantConditions
3674 do {
3675 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3676 return false;
3677 }
3678 ViewParent parent = current.mParent;
3679 if (parent == null) {
3680 return false; // We are not attached to the view root
3681 }
3682 if (!(parent instanceof View)) {
3683 return true;
3684 }
3685 current = (View) parent;
3686 } while (current != null);
3687
3688 return false;
3689 }
3690
3691 /**
3692 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3693 * is set
3694 *
3695 * @param insets Insets for system windows
3696 *
3697 * @return True if this view applied the insets, false otherwise
3698 */
3699 protected boolean fitSystemWindows(Rect insets) {
3700 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3701 mPaddingLeft = insets.left;
3702 mPaddingTop = insets.top;
3703 mPaddingRight = insets.right;
3704 mPaddingBottom = insets.bottom;
3705 requestLayout();
3706 return true;
3707 }
3708 return false;
3709 }
3710
3711 /**
3712 * Returns the visibility status for this view.
3713 *
3714 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3715 * @attr ref android.R.styleable#View_visibility
3716 */
3717 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003718 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3719 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3720 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003721 })
3722 public int getVisibility() {
3723 return mViewFlags & VISIBILITY_MASK;
3724 }
3725
3726 /**
3727 * Set the enabled state of this view.
3728 *
3729 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3730 * @attr ref android.R.styleable#View_visibility
3731 */
3732 @RemotableViewMethod
3733 public void setVisibility(int visibility) {
3734 setFlags(visibility, VISIBILITY_MASK);
3735 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3736 }
3737
3738 /**
3739 * Returns the enabled status for this view. The interpretation of the
3740 * enabled state varies by subclass.
3741 *
3742 * @return True if this view is enabled, false otherwise.
3743 */
3744 @ViewDebug.ExportedProperty
3745 public boolean isEnabled() {
3746 return (mViewFlags & ENABLED_MASK) == ENABLED;
3747 }
3748
3749 /**
3750 * Set the enabled state of this view. The interpretation of the enabled
3751 * state varies by subclass.
3752 *
3753 * @param enabled True if this view is enabled, false otherwise.
3754 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003755 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003756 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003757 if (enabled == isEnabled()) return;
3758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3760
3761 /*
3762 * The View most likely has to change its appearance, so refresh
3763 * the drawable state.
3764 */
3765 refreshDrawableState();
3766
3767 // Invalidate too, since the default behavior for views is to be
3768 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003769 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003770 }
3771
3772 /**
3773 * Set whether this view can receive the focus.
3774 *
3775 * Setting this to false will also ensure that this view is not focusable
3776 * in touch mode.
3777 *
3778 * @param focusable If true, this view can receive the focus.
3779 *
3780 * @see #setFocusableInTouchMode(boolean)
3781 * @attr ref android.R.styleable#View_focusable
3782 */
3783 public void setFocusable(boolean focusable) {
3784 if (!focusable) {
3785 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3786 }
3787 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3788 }
3789
3790 /**
3791 * Set whether this view can receive focus while in touch mode.
3792 *
3793 * Setting this to true will also ensure that this view is focusable.
3794 *
3795 * @param focusableInTouchMode If true, this view can receive the focus while
3796 * in touch mode.
3797 *
3798 * @see #setFocusable(boolean)
3799 * @attr ref android.R.styleable#View_focusableInTouchMode
3800 */
3801 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3802 // Focusable in touch mode should always be set before the focusable flag
3803 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3804 // which, in touch mode, will not successfully request focus on this view
3805 // because the focusable in touch mode flag is not set
3806 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3807 if (focusableInTouchMode) {
3808 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3809 }
3810 }
3811
3812 /**
3813 * Set whether this view should have sound effects enabled for events such as
3814 * clicking and touching.
3815 *
3816 * <p>You may wish to disable sound effects for a view if you already play sounds,
3817 * for instance, a dial key that plays dtmf tones.
3818 *
3819 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3820 * @see #isSoundEffectsEnabled()
3821 * @see #playSoundEffect(int)
3822 * @attr ref android.R.styleable#View_soundEffectsEnabled
3823 */
3824 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3825 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3826 }
3827
3828 /**
3829 * @return whether this view should have sound effects enabled for events such as
3830 * clicking and touching.
3831 *
3832 * @see #setSoundEffectsEnabled(boolean)
3833 * @see #playSoundEffect(int)
3834 * @attr ref android.R.styleable#View_soundEffectsEnabled
3835 */
3836 @ViewDebug.ExportedProperty
3837 public boolean isSoundEffectsEnabled() {
3838 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3839 }
3840
3841 /**
3842 * Set whether this view should have haptic feedback for events such as
3843 * long presses.
3844 *
3845 * <p>You may wish to disable haptic feedback if your view already controls
3846 * its own haptic feedback.
3847 *
3848 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3849 * @see #isHapticFeedbackEnabled()
3850 * @see #performHapticFeedback(int)
3851 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3852 */
3853 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3854 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3855 }
3856
3857 /**
3858 * @return whether this view should have haptic feedback enabled for events
3859 * long presses.
3860 *
3861 * @see #setHapticFeedbackEnabled(boolean)
3862 * @see #performHapticFeedback(int)
3863 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3864 */
3865 @ViewDebug.ExportedProperty
3866 public boolean isHapticFeedbackEnabled() {
3867 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3868 }
3869
3870 /**
3871 * If this view doesn't do any drawing on its own, set this flag to
3872 * allow further optimizations. By default, this flag is not set on
3873 * View, but could be set on some View subclasses such as ViewGroup.
3874 *
3875 * Typically, if you override {@link #onDraw} you should clear this flag.
3876 *
3877 * @param willNotDraw whether or not this View draw on its own
3878 */
3879 public void setWillNotDraw(boolean willNotDraw) {
3880 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3881 }
3882
3883 /**
3884 * Returns whether or not this View draws on its own.
3885 *
3886 * @return true if this view has nothing to draw, false otherwise
3887 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003888 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003889 public boolean willNotDraw() {
3890 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3891 }
3892
3893 /**
3894 * When a View's drawing cache is enabled, drawing is redirected to an
3895 * offscreen bitmap. Some views, like an ImageView, must be able to
3896 * bypass this mechanism if they already draw a single bitmap, to avoid
3897 * unnecessary usage of the memory.
3898 *
3899 * @param willNotCacheDrawing true if this view does not cache its
3900 * drawing, false otherwise
3901 */
3902 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3903 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3904 }
3905
3906 /**
3907 * Returns whether or not this View can cache its drawing or not.
3908 *
3909 * @return true if this view does not cache its drawing, false otherwise
3910 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003911 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 public boolean willNotCacheDrawing() {
3913 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3914 }
3915
3916 /**
3917 * Indicates whether this view reacts to click events or not.
3918 *
3919 * @return true if the view is clickable, false otherwise
3920 *
3921 * @see #setClickable(boolean)
3922 * @attr ref android.R.styleable#View_clickable
3923 */
3924 @ViewDebug.ExportedProperty
3925 public boolean isClickable() {
3926 return (mViewFlags & CLICKABLE) == CLICKABLE;
3927 }
3928
3929 /**
3930 * Enables or disables click events for this view. When a view
3931 * is clickable it will change its state to "pressed" on every click.
3932 * Subclasses should set the view clickable to visually react to
3933 * user's clicks.
3934 *
3935 * @param clickable true to make the view clickable, false otherwise
3936 *
3937 * @see #isClickable()
3938 * @attr ref android.R.styleable#View_clickable
3939 */
3940 public void setClickable(boolean clickable) {
3941 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3942 }
3943
3944 /**
3945 * Indicates whether this view reacts to long click events or not.
3946 *
3947 * @return true if the view is long clickable, false otherwise
3948 *
3949 * @see #setLongClickable(boolean)
3950 * @attr ref android.R.styleable#View_longClickable
3951 */
3952 public boolean isLongClickable() {
3953 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3954 }
3955
3956 /**
3957 * Enables or disables long click events for this view. When a view is long
3958 * clickable it reacts to the user holding down the button for a longer
3959 * duration than a tap. This event can either launch the listener or a
3960 * context menu.
3961 *
3962 * @param longClickable true to make the view long clickable, false otherwise
3963 * @see #isLongClickable()
3964 * @attr ref android.R.styleable#View_longClickable
3965 */
3966 public void setLongClickable(boolean longClickable) {
3967 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
3968 }
3969
3970 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07003971 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003972 *
3973 * @see #isClickable()
3974 * @see #setClickable(boolean)
3975 *
3976 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
3977 * the View's internal state from a previously set "pressed" state.
3978 */
3979 public void setPressed(boolean pressed) {
3980 if (pressed) {
3981 mPrivateFlags |= PRESSED;
3982 } else {
3983 mPrivateFlags &= ~PRESSED;
3984 }
3985 refreshDrawableState();
3986 dispatchSetPressed(pressed);
3987 }
3988
3989 /**
3990 * Dispatch setPressed to all of this View's children.
3991 *
3992 * @see #setPressed(boolean)
3993 *
3994 * @param pressed The new pressed state
3995 */
3996 protected void dispatchSetPressed(boolean pressed) {
3997 }
3998
3999 /**
4000 * Indicates whether the view is currently in pressed state. Unless
4001 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4002 * the pressed state.
4003 *
4004 * @see #setPressed
4005 * @see #isClickable()
4006 * @see #setClickable(boolean)
4007 *
4008 * @return true if the view is currently pressed, false otherwise
4009 */
4010 public boolean isPressed() {
4011 return (mPrivateFlags & PRESSED) == PRESSED;
4012 }
4013
4014 /**
4015 * Indicates whether this view will save its state (that is,
4016 * whether its {@link #onSaveInstanceState} method will be called).
4017 *
4018 * @return Returns true if the view state saving is enabled, else false.
4019 *
4020 * @see #setSaveEnabled(boolean)
4021 * @attr ref android.R.styleable#View_saveEnabled
4022 */
4023 public boolean isSaveEnabled() {
4024 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4025 }
4026
4027 /**
4028 * Controls whether the saving of this view's state is
4029 * enabled (that is, whether its {@link #onSaveInstanceState} method
4030 * will be called). Note that even if freezing is enabled, the
4031 * view still must have an id assigned to it (via {@link #setId setId()})
4032 * for its state to be saved. This flag can only disable the
4033 * saving of this view; any child views may still have their state saved.
4034 *
4035 * @param enabled Set to false to <em>disable</em> state saving, or true
4036 * (the default) to allow it.
4037 *
4038 * @see #isSaveEnabled()
4039 * @see #setId(int)
4040 * @see #onSaveInstanceState()
4041 * @attr ref android.R.styleable#View_saveEnabled
4042 */
4043 public void setSaveEnabled(boolean enabled) {
4044 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4045 }
4046
Jeff Brown85a31762010-09-01 17:01:00 -07004047 /**
4048 * Gets whether the framework should discard touches when the view's
4049 * window is obscured by another visible window.
4050 * Refer to the {@link View} security documentation for more details.
4051 *
4052 * @return True if touch filtering is enabled.
4053 *
4054 * @see #setFilterTouchesWhenObscured(boolean)
4055 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4056 */
4057 @ViewDebug.ExportedProperty
4058 public boolean getFilterTouchesWhenObscured() {
4059 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4060 }
4061
4062 /**
4063 * Sets whether the framework should discard touches when the view's
4064 * window is obscured by another visible window.
4065 * Refer to the {@link View} security documentation for more details.
4066 *
4067 * @param enabled True if touch filtering should be enabled.
4068 *
4069 * @see #getFilterTouchesWhenObscured
4070 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4071 */
4072 public void setFilterTouchesWhenObscured(boolean enabled) {
4073 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4074 FILTER_TOUCHES_WHEN_OBSCURED);
4075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004076
4077 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004078 * Indicates whether the entire hierarchy under this view will save its
4079 * state when a state saving traversal occurs from its parent. The default
4080 * is true; if false, these views will not be saved unless
4081 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4082 *
4083 * @return Returns true if the view state saving from parent is enabled, else false.
4084 *
4085 * @see #setSaveFromParentEnabled(boolean)
4086 */
4087 public boolean isSaveFromParentEnabled() {
4088 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4089 }
4090
4091 /**
4092 * Controls whether the entire hierarchy under this view will save its
4093 * state when a state saving traversal occurs from its parent. The default
4094 * is true; if false, these views will not be saved unless
4095 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4096 *
4097 * @param enabled Set to false to <em>disable</em> state saving, or true
4098 * (the default) to allow it.
4099 *
4100 * @see #isSaveFromParentEnabled()
4101 * @see #setId(int)
4102 * @see #onSaveInstanceState()
4103 */
4104 public void setSaveFromParentEnabled(boolean enabled) {
4105 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4106 }
4107
4108
4109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004110 * Returns whether this View is able to take focus.
4111 *
4112 * @return True if this view can take focus, or false otherwise.
4113 * @attr ref android.R.styleable#View_focusable
4114 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004115 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004116 public final boolean isFocusable() {
4117 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4118 }
4119
4120 /**
4121 * When a view is focusable, it may not want to take focus when in touch mode.
4122 * For example, a button would like focus when the user is navigating via a D-pad
4123 * so that the user can click on it, but once the user starts touching the screen,
4124 * the button shouldn't take focus
4125 * @return Whether the view is focusable in touch mode.
4126 * @attr ref android.R.styleable#View_focusableInTouchMode
4127 */
4128 @ViewDebug.ExportedProperty
4129 public final boolean isFocusableInTouchMode() {
4130 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4131 }
4132
4133 /**
4134 * Find the nearest view in the specified direction that can take focus.
4135 * This does not actually give focus to that view.
4136 *
4137 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4138 *
4139 * @return The nearest focusable in the specified direction, or null if none
4140 * can be found.
4141 */
4142 public View focusSearch(int direction) {
4143 if (mParent != null) {
4144 return mParent.focusSearch(this, direction);
4145 } else {
4146 return null;
4147 }
4148 }
4149
4150 /**
4151 * This method is the last chance for the focused view and its ancestors to
4152 * respond to an arrow key. This is called when the focused view did not
4153 * consume the key internally, nor could the view system find a new view in
4154 * the requested direction to give focus to.
4155 *
4156 * @param focused The currently focused view.
4157 * @param direction The direction focus wants to move. One of FOCUS_UP,
4158 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4159 * @return True if the this view consumed this unhandled move.
4160 */
4161 public boolean dispatchUnhandledMove(View focused, int direction) {
4162 return false;
4163 }
4164
4165 /**
4166 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004167 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004169 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4170 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004171 * @return The user specified next view, or null if there is none.
4172 */
4173 View findUserSetNextFocus(View root, int direction) {
4174 switch (direction) {
4175 case FOCUS_LEFT:
4176 if (mNextFocusLeftId == View.NO_ID) return null;
4177 return findViewShouldExist(root, mNextFocusLeftId);
4178 case FOCUS_RIGHT:
4179 if (mNextFocusRightId == View.NO_ID) return null;
4180 return findViewShouldExist(root, mNextFocusRightId);
4181 case FOCUS_UP:
4182 if (mNextFocusUpId == View.NO_ID) return null;
4183 return findViewShouldExist(root, mNextFocusUpId);
4184 case FOCUS_DOWN:
4185 if (mNextFocusDownId == View.NO_ID) return null;
4186 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004187 case FOCUS_FORWARD:
4188 if (mNextFocusForwardId == View.NO_ID) return null;
4189 return findViewShouldExist(root, mNextFocusForwardId);
4190 case FOCUS_BACKWARD: {
4191 final int id = mID;
4192 return root.findViewByPredicate(new Predicate<View>() {
4193 @Override
4194 public boolean apply(View t) {
4195 return t.mNextFocusForwardId == id;
4196 }
4197 });
4198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004199 }
4200 return null;
4201 }
4202
4203 private static View findViewShouldExist(View root, int childViewId) {
4204 View result = root.findViewById(childViewId);
4205 if (result == null) {
4206 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4207 + "by user for id " + childViewId);
4208 }
4209 return result;
4210 }
4211
4212 /**
4213 * Find and return all focusable views that are descendants of this view,
4214 * possibly including this view if it is focusable itself.
4215 *
4216 * @param direction The direction of the focus
4217 * @return A list of focusable views
4218 */
4219 public ArrayList<View> getFocusables(int direction) {
4220 ArrayList<View> result = new ArrayList<View>(24);
4221 addFocusables(result, direction);
4222 return result;
4223 }
4224
4225 /**
4226 * Add any focusable views that are descendants of this view (possibly
4227 * including this view if it is focusable itself) to views. If we are in touch mode,
4228 * only add views that are also focusable in touch mode.
4229 *
4230 * @param views Focusable views found so far
4231 * @param direction The direction of the focus
4232 */
4233 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004234 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236
svetoslavganov75986cf2009-05-14 22:28:01 -07004237 /**
4238 * Adds any focusable views that are descendants of this view (possibly
4239 * including this view if it is focusable itself) to views. This method
4240 * adds all focusable views regardless if we are in touch mode or
4241 * only views focusable in touch mode if we are in touch mode depending on
4242 * the focusable mode paramater.
4243 *
4244 * @param views Focusable views found so far or null if all we are interested is
4245 * the number of focusables.
4246 * @param direction The direction of the focus.
4247 * @param focusableMode The type of focusables to be added.
4248 *
4249 * @see #FOCUSABLES_ALL
4250 * @see #FOCUSABLES_TOUCH_MODE
4251 */
4252 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4253 if (!isFocusable()) {
4254 return;
4255 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004256
svetoslavganov75986cf2009-05-14 22:28:01 -07004257 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4258 isInTouchMode() && !isFocusableInTouchMode()) {
4259 return;
4260 }
4261
4262 if (views != null) {
4263 views.add(this);
4264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004265 }
4266
4267 /**
4268 * Find and return all touchable views that are descendants of this view,
4269 * possibly including this view if it is touchable itself.
4270 *
4271 * @return A list of touchable views
4272 */
4273 public ArrayList<View> getTouchables() {
4274 ArrayList<View> result = new ArrayList<View>();
4275 addTouchables(result);
4276 return result;
4277 }
4278
4279 /**
4280 * Add any touchable views that are descendants of this view (possibly
4281 * including this view if it is touchable itself) to views.
4282 *
4283 * @param views Touchable views found so far
4284 */
4285 public void addTouchables(ArrayList<View> views) {
4286 final int viewFlags = mViewFlags;
4287
4288 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4289 && (viewFlags & ENABLED_MASK) == ENABLED) {
4290 views.add(this);
4291 }
4292 }
4293
4294 /**
4295 * Call this to try to give focus to a specific view or to one of its
4296 * descendants.
4297 *
4298 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4299 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4300 * while the device is in touch mode.
4301 *
4302 * See also {@link #focusSearch}, which is what you call to say that you
4303 * have focus, and you want your parent to look for the next one.
4304 *
4305 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4306 * {@link #FOCUS_DOWN} and <code>null</code>.
4307 *
4308 * @return Whether this view or one of its descendants actually took focus.
4309 */
4310 public final boolean requestFocus() {
4311 return requestFocus(View.FOCUS_DOWN);
4312 }
4313
4314
4315 /**
4316 * Call this to try to give focus to a specific view or to one of its
4317 * descendants and give it a hint about what direction focus is heading.
4318 *
4319 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4320 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4321 * while the device is in touch mode.
4322 *
4323 * See also {@link #focusSearch}, which is what you call to say that you
4324 * have focus, and you want your parent to look for the next one.
4325 *
4326 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4327 * <code>null</code> set for the previously focused rectangle.
4328 *
4329 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4330 * @return Whether this view or one of its descendants actually took focus.
4331 */
4332 public final boolean requestFocus(int direction) {
4333 return requestFocus(direction, null);
4334 }
4335
4336 /**
4337 * Call this to try to give focus to a specific view or to one of its descendants
4338 * and give it hints about the direction and a specific rectangle that the focus
4339 * is coming from. The rectangle can help give larger views a finer grained hint
4340 * about where focus is coming from, and therefore, where to show selection, or
4341 * forward focus change internally.
4342 *
4343 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4344 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4345 * while the device is in touch mode.
4346 *
4347 * A View will not take focus if it is not visible.
4348 *
4349 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
4350 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
4351 *
4352 * See also {@link #focusSearch}, which is what you call to say that you
4353 * have focus, and you want your parent to look for the next one.
4354 *
4355 * You may wish to override this method if your custom {@link View} has an internal
4356 * {@link View} that it wishes to forward the request to.
4357 *
4358 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4359 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4360 * to give a finer grained hint about where focus is coming from. May be null
4361 * if there is no hint.
4362 * @return Whether this view or one of its descendants actually took focus.
4363 */
4364 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4365 // need to be focusable
4366 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4367 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4368 return false;
4369 }
4370
4371 // need to be focusable in touch mode if in touch mode
4372 if (isInTouchMode() &&
4373 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4374 return false;
4375 }
4376
4377 // need to not have any parents blocking us
4378 if (hasAncestorThatBlocksDescendantFocus()) {
4379 return false;
4380 }
4381
4382 handleFocusGainInternal(direction, previouslyFocusedRect);
4383 return true;
4384 }
4385
Christopher Tate2c095f32010-10-04 14:13:40 -07004386 /** Gets the ViewRoot, or null if not attached. */
4387 /*package*/ ViewRoot getViewRoot() {
4388 View root = getRootView();
4389 return root != null ? (ViewRoot)root.getParent() : null;
4390 }
4391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004392 /**
4393 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4394 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4395 * touch mode to request focus when they are touched.
4396 *
4397 * @return Whether this view or one of its descendants actually took focus.
4398 *
4399 * @see #isInTouchMode()
4400 *
4401 */
4402 public final boolean requestFocusFromTouch() {
4403 // Leave touch mode if we need to
4404 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004405 ViewRoot viewRoot = getViewRoot();
4406 if (viewRoot != null) {
4407 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409 }
4410 return requestFocus(View.FOCUS_DOWN);
4411 }
4412
4413 /**
4414 * @return Whether any ancestor of this view blocks descendant focus.
4415 */
4416 private boolean hasAncestorThatBlocksDescendantFocus() {
4417 ViewParent ancestor = mParent;
4418 while (ancestor instanceof ViewGroup) {
4419 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4420 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4421 return true;
4422 } else {
4423 ancestor = vgAncestor.getParent();
4424 }
4425 }
4426 return false;
4427 }
4428
4429 /**
Romain Guya440b002010-02-24 15:57:54 -08004430 * @hide
4431 */
4432 public void dispatchStartTemporaryDetach() {
4433 onStartTemporaryDetach();
4434 }
4435
4436 /**
4437 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4439 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004440 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004441 */
4442 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004443 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004444 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004445 }
4446
4447 /**
4448 * @hide
4449 */
4450 public void dispatchFinishTemporaryDetach() {
4451 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 }
Romain Guy8506ab42009-06-11 17:35:47 -07004453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004454 /**
4455 * Called after {@link #onStartTemporaryDetach} when the container is done
4456 * changing the view.
4457 */
4458 public void onFinishTemporaryDetach() {
4459 }
Romain Guy8506ab42009-06-11 17:35:47 -07004460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004461 /**
4462 * capture information of this view for later analysis: developement only
4463 * check dynamic switch to make sure we only dump view
4464 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4465 */
4466 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004467 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004468 return;
4469 }
4470 ViewDebug.dumpCapturedView(subTag, v);
4471 }
4472
4473 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004474 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4475 * for this view's window. Returns null if the view is not currently attached
4476 * to the window. Normally you will not need to use this directly, but
4477 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4478 */
4479 public KeyEvent.DispatcherState getKeyDispatcherState() {
4480 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4481 }
Joe Malin32736f02011-01-19 16:14:20 -08004482
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004483 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004484 * Dispatch a key event before it is processed by any input method
4485 * associated with the view hierarchy. This can be used to intercept
4486 * key events in special situations before the IME consumes them; a
4487 * typical example would be handling the BACK key to update the application's
4488 * UI instead of allowing the IME to see it and close itself.
4489 *
4490 * @param event The key event to be dispatched.
4491 * @return True if the event was handled, false otherwise.
4492 */
4493 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4494 return onKeyPreIme(event.getKeyCode(), event);
4495 }
4496
4497 /**
4498 * Dispatch a key event to the next view on the focus path. This path runs
4499 * from the top of the view tree down to the currently focused view. If this
4500 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4501 * the next node down the focus path. This method also fires any key
4502 * listeners.
4503 *
4504 * @param event The key event to be dispatched.
4505 * @return True if the event was handled, false otherwise.
4506 */
4507 public boolean dispatchKeyEvent(KeyEvent event) {
4508 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509
Romain Guyf607bdc2010-09-10 19:20:06 -07004510 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004511 if (android.util.Config.LOGV) {
4512 captureViewInfo("captureViewKeyEvent", this);
4513 }
4514
Romain Guyf607bdc2010-09-10 19:20:06 -07004515 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004516 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4517 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4518 return true;
4519 }
4520
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004521 return event.dispatch(this, mAttachInfo != null
4522 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523 }
4524
4525 /**
4526 * Dispatches a key shortcut event.
4527 *
4528 * @param event The key event to be dispatched.
4529 * @return True if the event was handled by the view, false otherwise.
4530 */
4531 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4532 return onKeyShortcut(event.getKeyCode(), event);
4533 }
4534
4535 /**
4536 * Pass the touch screen motion event down to the target view, or this
4537 * view if it is the target.
4538 *
4539 * @param event The motion event to be dispatched.
4540 * @return True if the event was handled by the view, false otherwise.
4541 */
4542 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004543 if (!onFilterTouchEventForSecurity(event)) {
4544 return false;
4545 }
4546
Romain Guyf607bdc2010-09-10 19:20:06 -07004547 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004548 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4549 mOnTouchListener.onTouch(this, event)) {
4550 return true;
4551 }
4552 return onTouchEvent(event);
4553 }
4554
4555 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004556 * Filter the touch event to apply security policies.
4557 *
4558 * @param event The motion event to be filtered.
4559 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004560 *
Jeff Brown85a31762010-09-01 17:01:00 -07004561 * @see #getFilterTouchesWhenObscured
4562 */
4563 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004564 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004565 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4566 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4567 // Window is obscured, drop this touch.
4568 return false;
4569 }
4570 return true;
4571 }
4572
4573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004574 * Pass a trackball motion event down to the focused view.
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 dispatchTrackballEvent(MotionEvent event) {
4580 //Log.i("view", "view=" + this + ", " + event.toString());
4581 return onTrackballEvent(event);
4582 }
4583
4584 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08004585 * Pass a generic motion event down to the focused view.
4586 *
4587 * @param event The motion event to be dispatched.
4588 * @return True if the event was handled by the view, false otherwise.
4589 */
4590 public boolean dispatchGenericMotionEvent(MotionEvent event) {
4591 return onGenericMotionEvent(event);
4592 }
4593
4594 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004595 * Called when the window containing this view gains or loses window focus.
4596 * ViewGroups should override to route to their children.
4597 *
4598 * @param hasFocus True if the window containing this view now has focus,
4599 * false otherwise.
4600 */
4601 public void dispatchWindowFocusChanged(boolean hasFocus) {
4602 onWindowFocusChanged(hasFocus);
4603 }
4604
4605 /**
4606 * Called when the window containing this view gains or loses focus. Note
4607 * that this is separate from view focus: to receive key events, both
4608 * your view and its window must have focus. If a window is displayed
4609 * on top of yours that takes input focus, then your own window will lose
4610 * focus but the view focus will remain unchanged.
4611 *
4612 * @param hasWindowFocus True if the window containing this view now has
4613 * focus, false otherwise.
4614 */
4615 public void onWindowFocusChanged(boolean hasWindowFocus) {
4616 InputMethodManager imm = InputMethodManager.peekInstance();
4617 if (!hasWindowFocus) {
4618 if (isPressed()) {
4619 setPressed(false);
4620 }
4621 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4622 imm.focusOut(this);
4623 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004624 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004625 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004626 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004627 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4628 imm.focusIn(this);
4629 }
4630 refreshDrawableState();
4631 }
4632
4633 /**
4634 * Returns true if this view is in a window that currently has window focus.
4635 * Note that this is not the same as the view itself having focus.
4636 *
4637 * @return True if this view is in a window that currently has window focus.
4638 */
4639 public boolean hasWindowFocus() {
4640 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4641 }
4642
4643 /**
Adam Powell326d8082009-12-09 15:10:07 -08004644 * Dispatch a view visibility change down the view hierarchy.
4645 * ViewGroups should override to route to their children.
4646 * @param changedView The view whose visibility changed. Could be 'this' or
4647 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004648 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4649 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004650 */
4651 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4652 onVisibilityChanged(changedView, visibility);
4653 }
4654
4655 /**
4656 * Called when the visibility of the view or an ancestor of the view is changed.
4657 * @param changedView The view whose visibility changed. Could be 'this' or
4658 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004659 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4660 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004661 */
4662 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004663 if (visibility == VISIBLE) {
4664 if (mAttachInfo != null) {
4665 initialAwakenScrollBars();
4666 } else {
4667 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4668 }
4669 }
Adam Powell326d8082009-12-09 15:10:07 -08004670 }
4671
4672 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004673 * Dispatch a hint about whether this view is displayed. For instance, when
4674 * a View moves out of the screen, it might receives a display hint indicating
4675 * the view is not displayed. Applications should not <em>rely</em> on this hint
4676 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004677 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004678 * @param hint A hint about whether or not this view is displayed:
4679 * {@link #VISIBLE} or {@link #INVISIBLE}.
4680 */
4681 public void dispatchDisplayHint(int hint) {
4682 onDisplayHint(hint);
4683 }
4684
4685 /**
4686 * Gives this view a hint about whether is displayed or not. For instance, when
4687 * a View moves out of the screen, it might receives a display hint indicating
4688 * the view is not displayed. Applications should not <em>rely</em> on this hint
4689 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004690 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004691 * @param hint A hint about whether or not this view is displayed:
4692 * {@link #VISIBLE} or {@link #INVISIBLE}.
4693 */
4694 protected void onDisplayHint(int hint) {
4695 }
4696
4697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 * Dispatch a window visibility change down the view hierarchy.
4699 * ViewGroups should override to route to their children.
4700 *
4701 * @param visibility The new visibility of the window.
4702 *
4703 * @see #onWindowVisibilityChanged
4704 */
4705 public void dispatchWindowVisibilityChanged(int visibility) {
4706 onWindowVisibilityChanged(visibility);
4707 }
4708
4709 /**
4710 * Called when the window containing has change its visibility
4711 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4712 * that this tells you whether or not your window is being made visible
4713 * to the window manager; this does <em>not</em> tell you whether or not
4714 * your window is obscured by other windows on the screen, even if it
4715 * is itself visible.
4716 *
4717 * @param visibility The new visibility of the window.
4718 */
4719 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004720 if (visibility == VISIBLE) {
4721 initialAwakenScrollBars();
4722 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004723 }
4724
4725 /**
4726 * Returns the current visibility of the window this view is attached to
4727 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4728 *
4729 * @return Returns the current visibility of the view's window.
4730 */
4731 public int getWindowVisibility() {
4732 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4733 }
4734
4735 /**
4736 * Retrieve the overall visible display size in which the window this view is
4737 * attached to has been positioned in. This takes into account screen
4738 * decorations above the window, for both cases where the window itself
4739 * is being position inside of them or the window is being placed under
4740 * then and covered insets are used for the window to position its content
4741 * inside. In effect, this tells you the available area where content can
4742 * be placed and remain visible to users.
4743 *
4744 * <p>This function requires an IPC back to the window manager to retrieve
4745 * the requested information, so should not be used in performance critical
4746 * code like drawing.
4747 *
4748 * @param outRect Filled in with the visible display frame. If the view
4749 * is not attached to a window, this is simply the raw display size.
4750 */
4751 public void getWindowVisibleDisplayFrame(Rect outRect) {
4752 if (mAttachInfo != null) {
4753 try {
4754 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4755 } catch (RemoteException e) {
4756 return;
4757 }
4758 // XXX This is really broken, and probably all needs to be done
4759 // in the window manager, and we need to know more about whether
4760 // we want the area behind or in front of the IME.
4761 final Rect insets = mAttachInfo.mVisibleInsets;
4762 outRect.left += insets.left;
4763 outRect.top += insets.top;
4764 outRect.right -= insets.right;
4765 outRect.bottom -= insets.bottom;
4766 return;
4767 }
4768 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4769 outRect.set(0, 0, d.getWidth(), d.getHeight());
4770 }
4771
4772 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004773 * Dispatch a notification about a resource configuration change down
4774 * the view hierarchy.
4775 * ViewGroups should override to route to their children.
4776 *
4777 * @param newConfig The new resource configuration.
4778 *
4779 * @see #onConfigurationChanged
4780 */
4781 public void dispatchConfigurationChanged(Configuration newConfig) {
4782 onConfigurationChanged(newConfig);
4783 }
4784
4785 /**
4786 * Called when the current configuration of the resources being used
4787 * by the application have changed. You can use this to decide when
4788 * to reload resources that can changed based on orientation and other
4789 * configuration characterstics. You only need to use this if you are
4790 * not relying on the normal {@link android.app.Activity} mechanism of
4791 * recreating the activity instance upon a configuration change.
4792 *
4793 * @param newConfig The new resource configuration.
4794 */
4795 protected void onConfigurationChanged(Configuration newConfig) {
4796 }
4797
4798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004799 * Private function to aggregate all per-view attributes in to the view
4800 * root.
4801 */
4802 void dispatchCollectViewAttributes(int visibility) {
4803 performCollectViewAttributes(visibility);
4804 }
4805
4806 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004807 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004808 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4809 mAttachInfo.mKeepScreenOn = true;
4810 }
4811 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4812 if (mOnSystemUiVisibilityChangeListener != null) {
4813 mAttachInfo.mHasSystemUiListeners = true;
4814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004815 }
4816 }
4817
4818 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004819 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004820 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004821 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4822 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004823 ai.mRecomputeGlobalAttributes = true;
4824 }
4825 }
4826 }
4827
4828 /**
4829 * Returns whether the device is currently in touch mode. Touch mode is entered
4830 * once the user begins interacting with the device by touch, and affects various
4831 * things like whether focus is always visible to the user.
4832 *
4833 * @return Whether the device is in touch mode.
4834 */
4835 @ViewDebug.ExportedProperty
4836 public boolean isInTouchMode() {
4837 if (mAttachInfo != null) {
4838 return mAttachInfo.mInTouchMode;
4839 } else {
4840 return ViewRoot.isInTouchMode();
4841 }
4842 }
4843
4844 /**
4845 * Returns the context the view is running in, through which it can
4846 * access the current theme, resources, etc.
4847 *
4848 * @return The view's Context.
4849 */
4850 @ViewDebug.CapturedViewProperty
4851 public final Context getContext() {
4852 return mContext;
4853 }
4854
4855 /**
4856 * Handle a key event before it is processed by any input method
4857 * associated with the view hierarchy. This can be used to intercept
4858 * key events in special situations before the IME consumes them; a
4859 * typical example would be handling the BACK key to update the application's
4860 * UI instead of allowing the IME to see it and close itself.
4861 *
4862 * @param keyCode The value in event.getKeyCode().
4863 * @param event Description of the key event.
4864 * @return If you handled the event, return true. If you want to allow the
4865 * event to be handled by the next receiver, return false.
4866 */
4867 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4868 return false;
4869 }
4870
4871 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004872 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
4873 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004874 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4875 * is released, if the view is enabled and clickable.
4876 *
4877 * @param keyCode A key code that represents the button pressed, from
4878 * {@link android.view.KeyEvent}.
4879 * @param event The KeyEvent object that defines the button action.
4880 */
4881 public boolean onKeyDown(int keyCode, KeyEvent event) {
4882 boolean result = false;
4883
4884 switch (keyCode) {
4885 case KeyEvent.KEYCODE_DPAD_CENTER:
4886 case KeyEvent.KEYCODE_ENTER: {
4887 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4888 return true;
4889 }
4890 // Long clickable items don't necessarily have to be clickable
4891 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4892 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4893 (event.getRepeatCount() == 0)) {
4894 setPressed(true);
4895 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004896 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004897 }
4898 return true;
4899 }
4900 break;
4901 }
4902 }
4903 return result;
4904 }
4905
4906 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004907 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4908 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4909 * the event).
4910 */
4911 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
4912 return false;
4913 }
4914
4915 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004916 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
4917 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004918 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
4919 * {@link KeyEvent#KEYCODE_ENTER} is released.
4920 *
4921 * @param keyCode A key code that represents the button pressed, from
4922 * {@link android.view.KeyEvent}.
4923 * @param event The KeyEvent object that defines the button action.
4924 */
4925 public boolean onKeyUp(int keyCode, KeyEvent event) {
4926 boolean result = false;
4927
4928 switch (keyCode) {
4929 case KeyEvent.KEYCODE_DPAD_CENTER:
4930 case KeyEvent.KEYCODE_ENTER: {
4931 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4932 return true;
4933 }
4934 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
4935 setPressed(false);
4936
4937 if (!mHasPerformedLongPress) {
4938 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05004939 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004940
4941 result = performClick();
4942 }
4943 }
4944 break;
4945 }
4946 }
4947 return result;
4948 }
4949
4950 /**
4951 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4952 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
4953 * the event).
4954 *
4955 * @param keyCode A key code that represents the button pressed, from
4956 * {@link android.view.KeyEvent}.
4957 * @param repeatCount The number of times the action was made.
4958 * @param event The KeyEvent object that defines the button action.
4959 */
4960 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4961 return false;
4962 }
4963
4964 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08004965 * Called on the focused view when a key shortcut event is not handled.
4966 * Override this method to implement local key shortcuts for the View.
4967 * Key shortcuts can also be implemented by setting the
4968 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004969 *
4970 * @param keyCode The value in event.getKeyCode().
4971 * @param event Description of the key event.
4972 * @return If you handled the event, return true. If you want to allow the
4973 * event to be handled by the next receiver, return false.
4974 */
4975 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4976 return false;
4977 }
4978
4979 /**
4980 * Check whether the called view is a text editor, in which case it
4981 * would make sense to automatically display a soft input window for
4982 * it. Subclasses should override this if they implement
4983 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004984 * a call on that method would return a non-null InputConnection, and
4985 * they are really a first-class editor that the user would normally
4986 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07004987 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004988 * <p>The default implementation always returns false. This does
4989 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
4990 * will not be called or the user can not otherwise perform edits on your
4991 * view; it is just a hint to the system that this is not the primary
4992 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07004993 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004994 * @return Returns true if this view is a text editor, else false.
4995 */
4996 public boolean onCheckIsTextEditor() {
4997 return false;
4998 }
Romain Guy8506ab42009-06-11 17:35:47 -07004999
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 /**
5001 * Create a new InputConnection for an InputMethod to interact
5002 * with the view. The default implementation returns null, since it doesn't
5003 * support input methods. You can override this to implement such support.
5004 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005005 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005006 * <p>When implementing this, you probably also want to implement
5007 * {@link #onCheckIsTextEditor()} to indicate you will return a
5008 * non-null InputConnection.
5009 *
5010 * @param outAttrs Fill in with attribute information about the connection.
5011 */
5012 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5013 return null;
5014 }
5015
5016 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005017 * Called by the {@link android.view.inputmethod.InputMethodManager}
5018 * when a view who is not the current
5019 * input connection target is trying to make a call on the manager. The
5020 * default implementation returns false; you can override this to return
5021 * true for certain views if you are performing InputConnection proxying
5022 * to them.
5023 * @param view The View that is making the InputMethodManager call.
5024 * @return Return true to allow the call, false to reject.
5025 */
5026 public boolean checkInputConnectionProxy(View view) {
5027 return false;
5028 }
Romain Guy8506ab42009-06-11 17:35:47 -07005029
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005030 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 * Show the context menu for this view. It is not safe to hold on to the
5032 * menu after returning from this method.
5033 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005034 * You should normally not overload this method. Overload
5035 * {@link #onCreateContextMenu(ContextMenu)} or define an
5036 * {@link OnCreateContextMenuListener} to add items to the context menu.
5037 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005038 * @param menu The context menu to populate
5039 */
5040 public void createContextMenu(ContextMenu menu) {
5041 ContextMenuInfo menuInfo = getContextMenuInfo();
5042
5043 // Sets the current menu info so all items added to menu will have
5044 // my extra info set.
5045 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5046
5047 onCreateContextMenu(menu);
5048 if (mOnCreateContextMenuListener != null) {
5049 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5050 }
5051
5052 // Clear the extra information so subsequent items that aren't mine don't
5053 // have my extra info.
5054 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5055
5056 if (mParent != null) {
5057 mParent.createContextMenu(menu);
5058 }
5059 }
5060
5061 /**
5062 * Views should implement this if they have extra information to associate
5063 * with the context menu. The return result is supplied as a parameter to
5064 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5065 * callback.
5066 *
5067 * @return Extra information about the item for which the context menu
5068 * should be shown. This information will vary across different
5069 * subclasses of View.
5070 */
5071 protected ContextMenuInfo getContextMenuInfo() {
5072 return null;
5073 }
5074
5075 /**
5076 * Views should implement this if the view itself is going to add items to
5077 * the context menu.
5078 *
5079 * @param menu the context menu to populate
5080 */
5081 protected void onCreateContextMenu(ContextMenu menu) {
5082 }
5083
5084 /**
5085 * Implement this method to handle trackball motion events. The
5086 * <em>relative</em> movement of the trackball since the last event
5087 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5088 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5089 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5090 * they will often be fractional values, representing the more fine-grained
5091 * movement information available from a trackball).
5092 *
5093 * @param event The motion event.
5094 * @return True if the event was handled, false otherwise.
5095 */
5096 public boolean onTrackballEvent(MotionEvent event) {
5097 return false;
5098 }
5099
5100 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005101 * Implement this method to handle generic motion events.
5102 * <p>
5103 * Generic motion events are dispatched to the focused view to describe
5104 * the motions of input devices such as joysticks. The
5105 * {@link MotionEvent#getSource() source} of the motion event specifies
5106 * the class of input that was received. Implementations of this method
5107 * must examine the bits in the source before processing the event.
5108 * The following code example shows how this is done.
5109 * </p>
5110 * <code>
5111 * public boolean onGenericMotionEvent(MotionEvent event) {
5112 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
5113 * float x = event.getX();
5114 * float y = event.getY();
5115 * // process the joystick motion
5116 * return true;
5117 * }
5118 * return super.onGenericMotionEvent(event);
5119 * }
5120 * </code>
5121 *
5122 * @param event The generic motion event being processed.
5123 *
5124 * @return Return true if you have consumed the event, false if you haven't.
5125 * The default implementation always returns false.
5126 */
5127 public boolean onGenericMotionEvent(MotionEvent event) {
5128 return false;
5129 }
5130
5131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005132 * Implement this method to handle touch screen motion events.
5133 *
5134 * @param event The motion event.
5135 * @return True if the event was handled, false otherwise.
5136 */
5137 public boolean onTouchEvent(MotionEvent event) {
5138 final int viewFlags = mViewFlags;
5139
5140 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5141 // A disabled view that is clickable still consumes the touch
5142 // events, it just doesn't respond to them.
5143 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5144 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5145 }
5146
5147 if (mTouchDelegate != null) {
5148 if (mTouchDelegate.onTouchEvent(event)) {
5149 return true;
5150 }
5151 }
5152
5153 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5154 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5155 switch (event.getAction()) {
5156 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005157 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5158 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 // take focus if we don't have it already and we should in
5160 // touch mode.
5161 boolean focusTaken = false;
5162 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5163 focusTaken = requestFocus();
5164 }
5165
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005166 if (prepressed) {
5167 // The button is being released before we actually
5168 // showed it as pressed. Make it show the pressed
5169 // state now (before scheduling the click) to ensure
5170 // the user sees it.
5171 mPrivateFlags |= PRESSED;
5172 refreshDrawableState();
5173 }
Joe Malin32736f02011-01-19 16:14:20 -08005174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005175 if (!mHasPerformedLongPress) {
5176 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005177 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005178
5179 // Only perform take click actions if we were in the pressed state
5180 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005181 // Use a Runnable and post this rather than calling
5182 // performClick directly. This lets other visual state
5183 // of the view update before click actions start.
5184 if (mPerformClick == null) {
5185 mPerformClick = new PerformClick();
5186 }
5187 if (!post(mPerformClick)) {
5188 performClick();
5189 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 }
5191 }
5192
5193 if (mUnsetPressedState == null) {
5194 mUnsetPressedState = new UnsetPressedState();
5195 }
5196
Adam Powelle14579b2009-12-16 18:39:52 -08005197 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005198 postDelayed(mUnsetPressedState,
5199 ViewConfiguration.getPressedStateDuration());
5200 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005201 // If the post failed, unpress right now
5202 mUnsetPressedState.run();
5203 }
Adam Powelle14579b2009-12-16 18:39:52 -08005204 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005205 }
5206 break;
5207
5208 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005209 if (mPendingCheckForTap == null) {
5210 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Adam Powelle14579b2009-12-16 18:39:52 -08005212 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005213 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005214 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215 break;
5216
5217 case MotionEvent.ACTION_CANCEL:
5218 mPrivateFlags &= ~PRESSED;
5219 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005220 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 break;
5222
5223 case MotionEvent.ACTION_MOVE:
5224 final int x = (int) event.getX();
5225 final int y = (int) event.getY();
5226
5227 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005228 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005230 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005231 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005232 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005233 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234
5235 // Need to switch from pressed to not pressed
5236 mPrivateFlags &= ~PRESSED;
5237 refreshDrawableState();
5238 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005239 }
5240 break;
5241 }
5242 return true;
5243 }
5244
5245 return false;
5246 }
5247
5248 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005249 * Remove the longpress detection timer.
5250 */
5251 private void removeLongPressCallback() {
5252 if (mPendingCheckForLongPress != null) {
5253 removeCallbacks(mPendingCheckForLongPress);
5254 }
5255 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005256
5257 /**
5258 * Remove the pending click action
5259 */
5260 private void removePerformClickCallback() {
5261 if (mPerformClick != null) {
5262 removeCallbacks(mPerformClick);
5263 }
5264 }
5265
Adam Powelle14579b2009-12-16 18:39:52 -08005266 /**
Romain Guya440b002010-02-24 15:57:54 -08005267 * Remove the prepress detection timer.
5268 */
5269 private void removeUnsetPressCallback() {
5270 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5271 setPressed(false);
5272 removeCallbacks(mUnsetPressedState);
5273 }
5274 }
5275
5276 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005277 * Remove the tap detection timer.
5278 */
5279 private void removeTapCallback() {
5280 if (mPendingCheckForTap != null) {
5281 mPrivateFlags &= ~PREPRESSED;
5282 removeCallbacks(mPendingCheckForTap);
5283 }
5284 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005285
5286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005287 * Cancels a pending long press. Your subclass can use this if you
5288 * want the context menu to come up if the user presses and holds
5289 * at the same place, but you don't want it to come up if they press
5290 * and then move around enough to cause scrolling.
5291 */
5292 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005293 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005294
5295 /*
5296 * The prepressed state handled by the tap callback is a display
5297 * construct, but the tap callback will post a long press callback
5298 * less its own timeout. Remove it here.
5299 */
5300 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005301 }
5302
5303 /**
5304 * Sets the TouchDelegate for this View.
5305 */
5306 public void setTouchDelegate(TouchDelegate delegate) {
5307 mTouchDelegate = delegate;
5308 }
5309
5310 /**
5311 * Gets the TouchDelegate for this View.
5312 */
5313 public TouchDelegate getTouchDelegate() {
5314 return mTouchDelegate;
5315 }
5316
5317 /**
5318 * Set flags controlling behavior of this view.
5319 *
5320 * @param flags Constant indicating the value which should be set
5321 * @param mask Constant indicating the bit range that should be changed
5322 */
5323 void setFlags(int flags, int mask) {
5324 int old = mViewFlags;
5325 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5326
5327 int changed = mViewFlags ^ old;
5328 if (changed == 0) {
5329 return;
5330 }
5331 int privateFlags = mPrivateFlags;
5332
5333 /* Check if the FOCUSABLE bit has changed */
5334 if (((changed & FOCUSABLE_MASK) != 0) &&
5335 ((privateFlags & HAS_BOUNDS) !=0)) {
5336 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5337 && ((privateFlags & FOCUSED) != 0)) {
5338 /* Give up focus if we are no longer focusable */
5339 clearFocus();
5340 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5341 && ((privateFlags & FOCUSED) == 0)) {
5342 /*
5343 * Tell the view system that we are now available to take focus
5344 * if no one else already has it.
5345 */
5346 if (mParent != null) mParent.focusableViewAvailable(this);
5347 }
5348 }
5349
5350 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5351 if ((changed & VISIBILITY_MASK) != 0) {
5352 /*
5353 * If this view is becoming visible, set the DRAWN flag so that
5354 * the next invalidate() will not be skipped.
5355 */
5356 mPrivateFlags |= DRAWN;
5357
5358 needGlobalAttributesUpdate(true);
5359
5360 // a view becoming visible is worth notifying the parent
5361 // about in case nothing has focus. even if this specific view
5362 // isn't focusable, it may contain something that is, so let
5363 // the root view try to give this focus if nothing else does.
5364 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5365 mParent.focusableViewAvailable(this);
5366 }
5367 }
5368 }
5369
5370 /* Check if the GONE bit has changed */
5371 if ((changed & GONE) != 0) {
5372 needGlobalAttributesUpdate(false);
5373 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005374 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375
Romain Guyecd80ee2009-12-03 17:13:02 -08005376 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5377 if (hasFocus()) clearFocus();
5378 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005379 }
5380 if (mAttachInfo != null) {
5381 mAttachInfo.mViewVisibilityChanged = true;
5382 }
5383 }
5384
5385 /* Check if the VISIBLE bit has changed */
5386 if ((changed & INVISIBLE) != 0) {
5387 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005388 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005389
5390 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5391 // root view becoming invisible shouldn't clear focus
5392 if (getRootView() != this) {
5393 clearFocus();
5394 }
5395 }
5396 if (mAttachInfo != null) {
5397 mAttachInfo.mViewVisibilityChanged = true;
5398 }
5399 }
5400
Adam Powell326d8082009-12-09 15:10:07 -08005401 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005402 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005403 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5404 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005405 }
Adam Powell326d8082009-12-09 15:10:07 -08005406 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5407 }
5408
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005409 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5410 destroyDrawingCache();
5411 }
5412
5413 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5414 destroyDrawingCache();
5415 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005416 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005417 }
5418
5419 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5420 destroyDrawingCache();
5421 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5422 }
5423
5424 if ((changed & DRAW_MASK) != 0) {
5425 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5426 if (mBGDrawable != null) {
5427 mPrivateFlags &= ~SKIP_DRAW;
5428 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5429 } else {
5430 mPrivateFlags |= SKIP_DRAW;
5431 }
5432 } else {
5433 mPrivateFlags &= ~SKIP_DRAW;
5434 }
5435 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005436 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005437 }
5438
5439 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005440 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005441 mParent.recomputeViewAttributes(this);
5442 }
5443 }
5444 }
5445
5446 /**
5447 * Change the view's z order in the tree, so it's on top of other sibling
5448 * views
5449 */
5450 public void bringToFront() {
5451 if (mParent != null) {
5452 mParent.bringChildToFront(this);
5453 }
5454 }
5455
5456 /**
5457 * This is called in response to an internal scroll in this view (i.e., the
5458 * view scrolled its own contents). This is typically as a result of
5459 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5460 * called.
5461 *
5462 * @param l Current horizontal scroll origin.
5463 * @param t Current vertical scroll origin.
5464 * @param oldl Previous horizontal scroll origin.
5465 * @param oldt Previous vertical scroll origin.
5466 */
5467 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5468 mBackgroundSizeChanged = true;
5469
5470 final AttachInfo ai = mAttachInfo;
5471 if (ai != null) {
5472 ai.mViewScrollChanged = true;
5473 }
5474 }
5475
5476 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005477 * Interface definition for a callback to be invoked when the layout bounds of a view
5478 * changes due to layout processing.
5479 */
5480 public interface OnLayoutChangeListener {
5481 /**
5482 * Called when the focus state of a view has changed.
5483 *
5484 * @param v The view whose state has changed.
5485 * @param left The new value of the view's left property.
5486 * @param top The new value of the view's top property.
5487 * @param right The new value of the view's right property.
5488 * @param bottom The new value of the view's bottom property.
5489 * @param oldLeft The previous value of the view's left property.
5490 * @param oldTop The previous value of the view's top property.
5491 * @param oldRight The previous value of the view's right property.
5492 * @param oldBottom The previous value of the view's bottom property.
5493 */
5494 void onLayoutChange(View v, int left, int top, int right, int bottom,
5495 int oldLeft, int oldTop, int oldRight, int oldBottom);
5496 }
5497
5498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 * This is called during layout when the size of this view has changed. If
5500 * you were just added to the view hierarchy, you're called with the old
5501 * values of 0.
5502 *
5503 * @param w Current width of this view.
5504 * @param h Current height of this view.
5505 * @param oldw Old width of this view.
5506 * @param oldh Old height of this view.
5507 */
5508 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5509 }
5510
5511 /**
5512 * Called by draw to draw the child views. This may be overridden
5513 * by derived classes to gain control just before its children are drawn
5514 * (but after its own view has been drawn).
5515 * @param canvas the canvas on which to draw the view
5516 */
5517 protected void dispatchDraw(Canvas canvas) {
5518 }
5519
5520 /**
5521 * Gets the parent of this view. Note that the parent is a
5522 * ViewParent and not necessarily a View.
5523 *
5524 * @return Parent of this view.
5525 */
5526 public final ViewParent getParent() {
5527 return mParent;
5528 }
5529
5530 /**
5531 * Return the scrolled left position of this view. This is the left edge of
5532 * the displayed part of your view. You do not need to draw any pixels
5533 * farther left, since those are outside of the frame of your view on
5534 * screen.
5535 *
5536 * @return The left edge of the displayed part of your view, in pixels.
5537 */
5538 public final int getScrollX() {
5539 return mScrollX;
5540 }
5541
5542 /**
5543 * Return the scrolled top position of this view. This is the top edge of
5544 * the displayed part of your view. You do not need to draw any pixels above
5545 * it, since those are outside of the frame of your view on screen.
5546 *
5547 * @return The top edge of the displayed part of your view, in pixels.
5548 */
5549 public final int getScrollY() {
5550 return mScrollY;
5551 }
5552
5553 /**
5554 * Return the width of the your view.
5555 *
5556 * @return The width of your view, in pixels.
5557 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005558 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 public final int getWidth() {
5560 return mRight - mLeft;
5561 }
5562
5563 /**
5564 * Return the height of your view.
5565 *
5566 * @return The height of your view, in pixels.
5567 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005568 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 public final int getHeight() {
5570 return mBottom - mTop;
5571 }
5572
5573 /**
5574 * Return the visible drawing bounds of your view. Fills in the output
5575 * rectangle with the values from getScrollX(), getScrollY(),
5576 * getWidth(), and getHeight().
5577 *
5578 * @param outRect The (scrolled) drawing bounds of the view.
5579 */
5580 public void getDrawingRect(Rect outRect) {
5581 outRect.left = mScrollX;
5582 outRect.top = mScrollY;
5583 outRect.right = mScrollX + (mRight - mLeft);
5584 outRect.bottom = mScrollY + (mBottom - mTop);
5585 }
5586
5587 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005588 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5589 * raw width component (that is the result is masked by
5590 * {@link #MEASURED_SIZE_MASK}).
5591 *
5592 * @return The raw measured width of this view.
5593 */
5594 public final int getMeasuredWidth() {
5595 return mMeasuredWidth & MEASURED_SIZE_MASK;
5596 }
5597
5598 /**
5599 * Return the full width measurement information for this view as computed
5600 * by the most recent call to {@link #measure}. This result is a bit mask
5601 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005602 * This should be used during measurement and layout calculations only. Use
5603 * {@link #getWidth()} to see how wide a view is after layout.
5604 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005605 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005607 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608 return mMeasuredWidth;
5609 }
5610
5611 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005612 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5613 * raw width component (that is the result is masked by
5614 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005616 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617 */
5618 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005619 return mMeasuredHeight & MEASURED_SIZE_MASK;
5620 }
5621
5622 /**
5623 * Return the full height measurement information for this view as computed
5624 * by the most recent call to {@link #measure}. This result is a bit mask
5625 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5626 * This should be used during measurement and layout calculations only. Use
5627 * {@link #getHeight()} to see how wide a view is after layout.
5628 *
5629 * @return The measured width of this view as a bit mask.
5630 */
5631 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 return mMeasuredHeight;
5633 }
5634
5635 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005636 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5637 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5638 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5639 * and the height component is at the shifted bits
5640 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5641 */
5642 public final int getMeasuredState() {
5643 return (mMeasuredWidth&MEASURED_STATE_MASK)
5644 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5645 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5646 }
5647
5648 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005649 * The transform matrix of this view, which is calculated based on the current
5650 * roation, scale, and pivot properties.
5651 *
5652 * @see #getRotation()
5653 * @see #getScaleX()
5654 * @see #getScaleY()
5655 * @see #getPivotX()
5656 * @see #getPivotY()
5657 * @return The current transform matrix for the view
5658 */
5659 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005660 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005661 return mMatrix;
5662 }
5663
5664 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005665 * Utility function to determine if the value is far enough away from zero to be
5666 * considered non-zero.
5667 * @param value A floating point value to check for zero-ness
5668 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5669 */
5670 private static boolean nonzero(float value) {
5671 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5672 }
5673
5674 /**
Jeff Brown86671742010-09-30 20:00:15 -07005675 * Returns true if the transform matrix is the identity matrix.
5676 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005677 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005678 * @return True if the transform matrix is the identity matrix, false otherwise.
5679 */
Jeff Brown86671742010-09-30 20:00:15 -07005680 final boolean hasIdentityMatrix() {
5681 updateMatrix();
5682 return mMatrixIsIdentity;
5683 }
5684
5685 /**
5686 * Recomputes the transform matrix if necessary.
5687 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005688 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005689 if (mMatrixDirty) {
5690 // transform-related properties have changed since the last time someone
5691 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005692
5693 // Figure out if we need to update the pivot point
5694 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005695 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005696 mPrevWidth = mRight - mLeft;
5697 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005698 mPivotX = mPrevWidth / 2f;
5699 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005700 }
5701 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005702 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005703 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5704 mMatrix.setTranslate(mTranslationX, mTranslationY);
5705 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5706 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5707 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005708 if (mCamera == null) {
5709 mCamera = new Camera();
5710 matrix3D = new Matrix();
5711 }
5712 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005713 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005714 mCamera.rotateX(mRotationX);
5715 mCamera.rotateY(mRotationY);
Chet Haase897247b2010-09-09 14:54:47 -07005716 mCamera.rotateZ(-mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005717 mCamera.getMatrix(matrix3D);
5718 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005719 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005720 mMatrix.postConcat(matrix3D);
5721 mCamera.restore();
5722 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005723 mMatrixDirty = false;
5724 mMatrixIsIdentity = mMatrix.isIdentity();
5725 mInverseMatrixDirty = true;
5726 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005727 }
5728
5729 /**
5730 * Utility method to retrieve the inverse of the current mMatrix property.
5731 * We cache the matrix to avoid recalculating it when transform properties
5732 * have not changed.
5733 *
5734 * @return The inverse of the current matrix of this view.
5735 */
Jeff Brown86671742010-09-30 20:00:15 -07005736 final Matrix getInverseMatrix() {
5737 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005738 if (mInverseMatrixDirty) {
5739 if (mInverseMatrix == null) {
5740 mInverseMatrix = new Matrix();
5741 }
5742 mMatrix.invert(mInverseMatrix);
5743 mInverseMatrixDirty = false;
5744 }
5745 return mInverseMatrix;
5746 }
5747
5748 /**
5749 * The degrees that the view is rotated around the pivot point.
5750 *
5751 * @see #getPivotX()
5752 * @see #getPivotY()
5753 * @return The degrees of rotation.
5754 */
5755 public float getRotation() {
5756 return mRotation;
5757 }
5758
5759 /**
Chet Haase897247b2010-09-09 14:54:47 -07005760 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5761 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005762 *
5763 * @param rotation The degrees of rotation.
5764 * @see #getPivotX()
5765 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005766 *
5767 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005768 */
5769 public void setRotation(float rotation) {
5770 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005771 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005772 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005773 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005774 mRotation = rotation;
5775 mMatrixDirty = true;
5776 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005777 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005778 }
5779 }
5780
5781 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005782 * The degrees that the view is rotated around the vertical axis through the pivot point.
5783 *
5784 * @see #getPivotX()
5785 * @see #getPivotY()
5786 * @return The degrees of Y rotation.
5787 */
5788 public float getRotationY() {
5789 return mRotationY;
5790 }
5791
5792 /**
Chet Haase897247b2010-09-09 14:54:47 -07005793 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5794 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5795 * down the y axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005796 *
5797 * @param rotationY The degrees of Y rotation.
5798 * @see #getPivotX()
5799 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005800 *
5801 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005802 */
5803 public void setRotationY(float rotationY) {
5804 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005805 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005806 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005807 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005808 mRotationY = rotationY;
5809 mMatrixDirty = true;
5810 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005811 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005812 }
5813 }
5814
5815 /**
5816 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5817 *
5818 * @see #getPivotX()
5819 * @see #getPivotY()
5820 * @return The degrees of X rotation.
5821 */
5822 public float getRotationX() {
5823 return mRotationX;
5824 }
5825
5826 /**
Chet Haase897247b2010-09-09 14:54:47 -07005827 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
5828 * Increasing values result in clockwise rotation from the viewpoint of looking down the
5829 * x axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005830 *
5831 * @param rotationX The degrees of X rotation.
5832 * @see #getPivotX()
5833 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005834 *
5835 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07005836 */
5837 public void setRotationX(float rotationX) {
5838 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005839 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005840 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005841 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005842 mRotationX = rotationX;
5843 mMatrixDirty = true;
5844 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005845 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005846 }
5847 }
5848
5849 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005850 * The amount that the view is scaled in x around the pivot point, as a proportion of
5851 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
5852 *
Joe Onorato93162322010-09-16 15:42:01 -04005853 * <p>By default, this is 1.0f.
5854 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005855 * @see #getPivotX()
5856 * @see #getPivotY()
5857 * @return The scaling factor.
5858 */
5859 public float getScaleX() {
5860 return mScaleX;
5861 }
5862
5863 /**
5864 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
5865 * the view's unscaled width. A value of 1 means that no scaling is applied.
5866 *
5867 * @param scaleX The scaling factor.
5868 * @see #getPivotX()
5869 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005870 *
5871 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07005872 */
5873 public void setScaleX(float scaleX) {
5874 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005875 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005876 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005877 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005878 mScaleX = scaleX;
5879 mMatrixDirty = true;
5880 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005881 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005882 }
5883 }
5884
5885 /**
5886 * The amount that the view is scaled in y around the pivot point, as a proportion of
5887 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
5888 *
Joe Onorato93162322010-09-16 15:42:01 -04005889 * <p>By default, this is 1.0f.
5890 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005891 * @see #getPivotX()
5892 * @see #getPivotY()
5893 * @return The scaling factor.
5894 */
5895 public float getScaleY() {
5896 return mScaleY;
5897 }
5898
5899 /**
5900 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
5901 * the view's unscaled width. A value of 1 means that no scaling is applied.
5902 *
5903 * @param scaleY The scaling factor.
5904 * @see #getPivotX()
5905 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005906 *
5907 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07005908 */
5909 public void setScaleY(float scaleY) {
5910 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005911 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005912 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005913 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005914 mScaleY = scaleY;
5915 mMatrixDirty = true;
5916 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005917 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005918 }
5919 }
5920
5921 /**
5922 * The x location of the point around which the view is {@link #setRotation(float) rotated}
5923 * and {@link #setScaleX(float) scaled}.
5924 *
5925 * @see #getRotation()
5926 * @see #getScaleX()
5927 * @see #getScaleY()
5928 * @see #getPivotY()
5929 * @return The x location of the pivot point.
5930 */
5931 public float getPivotX() {
5932 return mPivotX;
5933 }
5934
5935 /**
5936 * Sets the x location of the point around which the view is
5937 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07005938 * By default, the pivot point is centered on the object.
5939 * Setting this property disables this behavior and causes the view to use only the
5940 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07005941 *
5942 * @param pivotX The x location of the pivot point.
5943 * @see #getRotation()
5944 * @see #getScaleX()
5945 * @see #getScaleY()
5946 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005947 *
5948 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07005949 */
5950 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005951 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07005952 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005953 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005954 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005955 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005956 mPivotX = pivotX;
5957 mMatrixDirty = true;
5958 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005959 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005960 }
5961 }
5962
5963 /**
5964 * The y location of the point around which the view is {@link #setRotation(float) rotated}
5965 * and {@link #setScaleY(float) scaled}.
5966 *
5967 * @see #getRotation()
5968 * @see #getScaleX()
5969 * @see #getScaleY()
5970 * @see #getPivotY()
5971 * @return The y location of the pivot point.
5972 */
5973 public float getPivotY() {
5974 return mPivotY;
5975 }
5976
5977 /**
5978 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07005979 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
5980 * Setting this property disables this behavior and causes the view to use only the
5981 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07005982 *
5983 * @param pivotY The y location of the pivot point.
5984 * @see #getRotation()
5985 * @see #getScaleX()
5986 * @see #getScaleY()
5987 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005988 *
5989 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07005990 */
5991 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005992 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07005993 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005994 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005995 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005996 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005997 mPivotY = pivotY;
5998 mMatrixDirty = true;
5999 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006000 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006001 }
6002 }
6003
6004 /**
6005 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6006 * completely transparent and 1 means the view is completely opaque.
6007 *
Joe Onorato93162322010-09-16 15:42:01 -04006008 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006009 * @return The opacity of the view.
6010 */
6011 public float getAlpha() {
6012 return mAlpha;
6013 }
6014
6015 /**
Romain Guy171c5922011-01-06 10:04:23 -08006016 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6017 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006018 *
Romain Guy171c5922011-01-06 10:04:23 -08006019 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6020 * responsible for applying the opacity itself. Otherwise, calling this method is
6021 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006022 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006023 *
6024 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006025 *
Joe Malin32736f02011-01-19 16:14:20 -08006026 * @see #setLayerType(int, android.graphics.Paint)
6027 *
Chet Haase73066682010-11-29 15:55:32 -08006028 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006029 */
6030 public void setAlpha(float alpha) {
6031 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006032 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006033 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006034 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006035 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006036 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006037 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006038 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006039 invalidate(false);
6040 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006041 }
6042
6043 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006044 * Top position of this view relative to its parent.
6045 *
6046 * @return The top of this view, in pixels.
6047 */
6048 @ViewDebug.CapturedViewProperty
6049 public final int getTop() {
6050 return mTop;
6051 }
6052
6053 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006054 * Sets the top position of this view relative to its parent. This method is meant to be called
6055 * by the layout system and should not generally be called otherwise, because the property
6056 * may be changed at any time by the layout.
6057 *
6058 * @param top The top of this view, in pixels.
6059 */
6060 public final void setTop(int top) {
6061 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006062 updateMatrix();
6063 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006064 final ViewParent p = mParent;
6065 if (p != null && mAttachInfo != null) {
6066 final Rect r = mAttachInfo.mTmpInvalRect;
6067 int minTop;
6068 int yLoc;
6069 if (top < mTop) {
6070 minTop = top;
6071 yLoc = top - mTop;
6072 } else {
6073 minTop = mTop;
6074 yLoc = 0;
6075 }
6076 r.set(0, yLoc, mRight - mLeft, mBottom - minTop);
6077 p.invalidateChild(this, r);
6078 }
6079 } else {
6080 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006081 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006082 }
6083
Chet Haaseed032702010-10-01 14:05:54 -07006084 int width = mRight - mLeft;
6085 int oldHeight = mBottom - mTop;
6086
Chet Haase21cd1382010-09-01 17:42:29 -07006087 mTop = top;
6088
Chet Haaseed032702010-10-01 14:05:54 -07006089 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6090
Chet Haase21cd1382010-09-01 17:42:29 -07006091 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006092 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6093 // A change in dimension means an auto-centered pivot point changes, too
6094 mMatrixDirty = true;
6095 }
Chet Haase21cd1382010-09-01 17:42:29 -07006096 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006097 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006098 }
Chet Haase55dbb652010-12-21 20:15:08 -08006099 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006100 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006101 }
6102 }
6103
6104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006105 * Bottom position of this view relative to its parent.
6106 *
6107 * @return The bottom of this view, in pixels.
6108 */
6109 @ViewDebug.CapturedViewProperty
6110 public final int getBottom() {
6111 return mBottom;
6112 }
6113
6114 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006115 * True if this view has changed since the last time being drawn.
6116 *
6117 * @return The dirty state of this view.
6118 */
6119 public boolean isDirty() {
6120 return (mPrivateFlags & DIRTY_MASK) != 0;
6121 }
6122
6123 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006124 * Sets the bottom position of this view relative to its parent. This method is meant to be
6125 * called by the layout system and should not generally be called otherwise, because the
6126 * property may be changed at any time by the layout.
6127 *
6128 * @param bottom The bottom of this view, in pixels.
6129 */
6130 public final void setBottom(int bottom) {
6131 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006132 updateMatrix();
6133 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006134 final ViewParent p = mParent;
6135 if (p != null && mAttachInfo != null) {
6136 final Rect r = mAttachInfo.mTmpInvalRect;
6137 int maxBottom;
6138 if (bottom < mBottom) {
6139 maxBottom = mBottom;
6140 } else {
6141 maxBottom = bottom;
6142 }
6143 r.set(0, 0, mRight - mLeft, maxBottom - mTop);
6144 p.invalidateChild(this, r);
6145 }
6146 } else {
6147 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006148 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006149 }
6150
Chet Haaseed032702010-10-01 14:05:54 -07006151 int width = mRight - mLeft;
6152 int oldHeight = mBottom - mTop;
6153
Chet Haase21cd1382010-09-01 17:42:29 -07006154 mBottom = bottom;
6155
Chet Haaseed032702010-10-01 14:05:54 -07006156 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6157
Chet Haase21cd1382010-09-01 17:42:29 -07006158 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006159 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6160 // A change in dimension means an auto-centered pivot point changes, too
6161 mMatrixDirty = true;
6162 }
Chet Haase21cd1382010-09-01 17:42:29 -07006163 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006164 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006165 }
Chet Haase55dbb652010-12-21 20:15:08 -08006166 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006167 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006168 }
6169 }
6170
6171 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006172 * Left position of this view relative to its parent.
6173 *
6174 * @return The left edge of this view, in pixels.
6175 */
6176 @ViewDebug.CapturedViewProperty
6177 public final int getLeft() {
6178 return mLeft;
6179 }
6180
6181 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006182 * Sets the left position of this view relative to its parent. This method is meant to be called
6183 * by the layout system and should not generally be called otherwise, because the property
6184 * may be changed at any time by the layout.
6185 *
6186 * @param left The bottom of this view, in pixels.
6187 */
6188 public final void setLeft(int left) {
6189 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006190 updateMatrix();
6191 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006192 final ViewParent p = mParent;
6193 if (p != null && mAttachInfo != null) {
6194 final Rect r = mAttachInfo.mTmpInvalRect;
6195 int minLeft;
6196 int xLoc;
6197 if (left < mLeft) {
6198 minLeft = left;
6199 xLoc = left - mLeft;
6200 } else {
6201 minLeft = mLeft;
6202 xLoc = 0;
6203 }
6204 r.set(xLoc, 0, mRight - minLeft, mBottom - mTop);
6205 p.invalidateChild(this, r);
6206 }
6207 } else {
6208 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006209 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006210 }
6211
Chet Haaseed032702010-10-01 14:05:54 -07006212 int oldWidth = mRight - mLeft;
6213 int height = mBottom - mTop;
6214
Chet Haase21cd1382010-09-01 17:42:29 -07006215 mLeft = left;
6216
Chet Haaseed032702010-10-01 14:05:54 -07006217 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6218
Chet Haase21cd1382010-09-01 17:42:29 -07006219 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006220 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6221 // A change in dimension means an auto-centered pivot point changes, too
6222 mMatrixDirty = true;
6223 }
Chet Haase21cd1382010-09-01 17:42:29 -07006224 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006225 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006226 }
Chet Haase55dbb652010-12-21 20:15:08 -08006227 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006228 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006229 }
6230 }
6231
6232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006233 * Right position of this view relative to its parent.
6234 *
6235 * @return The right edge of this view, in pixels.
6236 */
6237 @ViewDebug.CapturedViewProperty
6238 public final int getRight() {
6239 return mRight;
6240 }
6241
6242 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006243 * Sets the right position of this view relative to its parent. This method is meant to be called
6244 * by the layout system and should not generally be called otherwise, because the property
6245 * may be changed at any time by the layout.
6246 *
6247 * @param right The bottom of this view, in pixels.
6248 */
6249 public final void setRight(int right) {
6250 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006251 updateMatrix();
6252 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006253 final ViewParent p = mParent;
6254 if (p != null && mAttachInfo != null) {
6255 final Rect r = mAttachInfo.mTmpInvalRect;
6256 int maxRight;
6257 if (right < mRight) {
6258 maxRight = mRight;
6259 } else {
6260 maxRight = right;
6261 }
6262 r.set(0, 0, maxRight - mLeft, mBottom - mTop);
6263 p.invalidateChild(this, r);
6264 }
6265 } else {
6266 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006267 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006268 }
6269
Chet Haaseed032702010-10-01 14:05:54 -07006270 int oldWidth = mRight - mLeft;
6271 int height = mBottom - mTop;
6272
Chet Haase21cd1382010-09-01 17:42:29 -07006273 mRight = right;
6274
Chet Haaseed032702010-10-01 14:05:54 -07006275 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6276
Chet Haase21cd1382010-09-01 17:42:29 -07006277 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006278 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6279 // A change in dimension means an auto-centered pivot point changes, too
6280 mMatrixDirty = true;
6281 }
Chet Haase21cd1382010-09-01 17:42:29 -07006282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006283 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006284 }
Chet Haase55dbb652010-12-21 20:15:08 -08006285 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006286 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006287 }
6288 }
6289
6290 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006291 * The visual x position of this view, in pixels. This is equivalent to the
6292 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006293 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006294 *
Chet Haasedf030d22010-07-30 17:22:38 -07006295 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006296 */
Chet Haasedf030d22010-07-30 17:22:38 -07006297 public float getX() {
6298 return mLeft + mTranslationX;
6299 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006300
Chet Haasedf030d22010-07-30 17:22:38 -07006301 /**
6302 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6303 * {@link #setTranslationX(float) translationX} property to be the difference between
6304 * the x value passed in and the current {@link #getLeft() left} property.
6305 *
6306 * @param x The visual x position of this view, in pixels.
6307 */
6308 public void setX(float x) {
6309 setTranslationX(x - mLeft);
6310 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006311
Chet Haasedf030d22010-07-30 17:22:38 -07006312 /**
6313 * The visual y position of this view, in pixels. This is equivalent to the
6314 * {@link #setTranslationY(float) translationY} property plus the current
6315 * {@link #getTop() top} property.
6316 *
6317 * @return The visual y position of this view, in pixels.
6318 */
6319 public float getY() {
6320 return mTop + mTranslationY;
6321 }
6322
6323 /**
6324 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6325 * {@link #setTranslationY(float) translationY} property to be the difference between
6326 * the y value passed in and the current {@link #getTop() top} property.
6327 *
6328 * @param y The visual y position of this view, in pixels.
6329 */
6330 public void setY(float y) {
6331 setTranslationY(y - mTop);
6332 }
6333
6334
6335 /**
6336 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6337 * This position is post-layout, in addition to wherever the object's
6338 * layout placed it.
6339 *
6340 * @return The horizontal position of this view relative to its left position, in pixels.
6341 */
6342 public float getTranslationX() {
6343 return mTranslationX;
6344 }
6345
6346 /**
6347 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6348 * This effectively positions the object post-layout, in addition to wherever the object's
6349 * layout placed it.
6350 *
6351 * @param translationX The horizontal position of this view relative to its left position,
6352 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006353 *
6354 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006355 */
6356 public void setTranslationX(float translationX) {
6357 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006358 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006359 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006360 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006361 mTranslationX = translationX;
6362 mMatrixDirty = true;
6363 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006364 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006365 }
6366 }
6367
6368 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006369 * The horizontal location of this view relative to its {@link #getTop() top} position.
6370 * This position is post-layout, in addition to wherever the object's
6371 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006372 *
Chet Haasedf030d22010-07-30 17:22:38 -07006373 * @return The vertical position of this view relative to its top position,
6374 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006375 */
Chet Haasedf030d22010-07-30 17:22:38 -07006376 public float getTranslationY() {
6377 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006378 }
6379
6380 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006381 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6382 * This effectively positions the object post-layout, in addition to wherever the object's
6383 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006384 *
Chet Haasedf030d22010-07-30 17:22:38 -07006385 * @param translationY The vertical position of this view relative to its top position,
6386 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006387 *
6388 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006389 */
Chet Haasedf030d22010-07-30 17:22:38 -07006390 public void setTranslationY(float translationY) {
6391 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006392 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006393 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006394 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006395 mTranslationY = translationY;
6396 mMatrixDirty = true;
6397 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006398 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006399 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006400 }
6401
6402 /**
Romain Guyda489792011-02-03 01:05:15 -08006403 * @hide
6404 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006405 public void setFastTranslationX(float x) {
6406 mTranslationX = x;
6407 mMatrixDirty = true;
6408 }
6409
6410 /**
6411 * @hide
6412 */
6413 public void setFastTranslationY(float y) {
6414 mTranslationY = y;
6415 mMatrixDirty = true;
6416 }
6417
6418 /**
6419 * @hide
6420 */
Romain Guyda489792011-02-03 01:05:15 -08006421 public void setFastX(float x) {
6422 mTranslationX = x - mLeft;
6423 mMatrixDirty = true;
6424 }
6425
6426 /**
6427 * @hide
6428 */
6429 public void setFastY(float y) {
6430 mTranslationY = y - mTop;
6431 mMatrixDirty = true;
6432 }
6433
6434 /**
6435 * @hide
6436 */
6437 public void setFastScaleX(float x) {
6438 mScaleX = x;
6439 mMatrixDirty = true;
6440 }
6441
6442 /**
6443 * @hide
6444 */
6445 public void setFastScaleY(float y) {
6446 mScaleY = y;
6447 mMatrixDirty = true;
6448 }
6449
6450 /**
6451 * @hide
6452 */
6453 public void setFastAlpha(float alpha) {
6454 mAlpha = alpha;
6455 }
6456
6457 /**
6458 * @hide
6459 */
6460 public void setFastRotationY(float y) {
6461 mRotationY = y;
6462 mMatrixDirty = true;
6463 }
6464
6465 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006466 * Hit rectangle in parent's coordinates
6467 *
6468 * @param outRect The hit rectangle of the view.
6469 */
6470 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006471 updateMatrix();
6472 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006473 outRect.set(mLeft, mTop, mRight, mBottom);
6474 } else {
6475 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006476 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006477 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006478 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6479 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006480 }
6481 }
6482
6483 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006484 * Determines whether the given point, in local coordinates is inside the view.
6485 */
6486 /*package*/ final boolean pointInView(float localX, float localY) {
6487 return localX >= 0 && localX < (mRight - mLeft)
6488 && localY >= 0 && localY < (mBottom - mTop);
6489 }
6490
6491 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006492 * Utility method to determine whether the given point, in local coordinates,
6493 * is inside the view, where the area of the view is expanded by the slop factor.
6494 * This method is called while processing touch-move events to determine if the event
6495 * is still within the view.
6496 */
6497 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006498 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006499 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 }
6501
6502 /**
6503 * When a view has focus and the user navigates away from it, the next view is searched for
6504 * starting from the rectangle filled in by this method.
6505 *
6506 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6507 * view maintains some idea of internal selection, such as a cursor, or a selected row
6508 * or column, you should override this method and fill in a more specific rectangle.
6509 *
6510 * @param r The rectangle to fill in, in this view's coordinates.
6511 */
6512 public void getFocusedRect(Rect r) {
6513 getDrawingRect(r);
6514 }
6515
6516 /**
6517 * If some part of this view is not clipped by any of its parents, then
6518 * return that area in r in global (root) coordinates. To convert r to local
6519 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6520 * -globalOffset.y)) If the view is completely clipped or translated out,
6521 * return false.
6522 *
6523 * @param r If true is returned, r holds the global coordinates of the
6524 * visible portion of this view.
6525 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6526 * between this view and its root. globalOffet may be null.
6527 * @return true if r is non-empty (i.e. part of the view is visible at the
6528 * root level.
6529 */
6530 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6531 int width = mRight - mLeft;
6532 int height = mBottom - mTop;
6533 if (width > 0 && height > 0) {
6534 r.set(0, 0, width, height);
6535 if (globalOffset != null) {
6536 globalOffset.set(-mScrollX, -mScrollY);
6537 }
6538 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6539 }
6540 return false;
6541 }
6542
6543 public final boolean getGlobalVisibleRect(Rect r) {
6544 return getGlobalVisibleRect(r, null);
6545 }
6546
6547 public final boolean getLocalVisibleRect(Rect r) {
6548 Point offset = new Point();
6549 if (getGlobalVisibleRect(r, offset)) {
6550 r.offset(-offset.x, -offset.y); // make r local
6551 return true;
6552 }
6553 return false;
6554 }
6555
6556 /**
6557 * Offset this view's vertical location by the specified number of pixels.
6558 *
6559 * @param offset the number of pixels to offset the view by
6560 */
6561 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006562 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006563 updateMatrix();
6564 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006565 final ViewParent p = mParent;
6566 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006567 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006568 int minTop;
6569 int maxBottom;
6570 int yLoc;
6571 if (offset < 0) {
6572 minTop = mTop + offset;
6573 maxBottom = mBottom;
6574 yLoc = offset;
6575 } else {
6576 minTop = mTop;
6577 maxBottom = mBottom + offset;
6578 yLoc = 0;
6579 }
6580 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6581 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006582 }
6583 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006584 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006585 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006586
Chet Haasec3aa3612010-06-17 08:50:37 -07006587 mTop += offset;
6588 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006589
Chet Haasec3aa3612010-06-17 08:50:37 -07006590 if (!mMatrixIsIdentity) {
6591 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006592 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006593 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006594 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006596 }
6597
6598 /**
6599 * Offset this view's horizontal location by the specified amount of pixels.
6600 *
6601 * @param offset the numer of pixels to offset the view by
6602 */
6603 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006604 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006605 updateMatrix();
6606 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006607 final ViewParent p = mParent;
6608 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006609 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006610 int minLeft;
6611 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006612 if (offset < 0) {
6613 minLeft = mLeft + offset;
6614 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006615 } else {
6616 minLeft = mLeft;
6617 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006618 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006619 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006620 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006621 }
6622 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006623 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006624 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006625
Chet Haasec3aa3612010-06-17 08:50:37 -07006626 mLeft += offset;
6627 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006628
Chet Haasec3aa3612010-06-17 08:50:37 -07006629 if (!mMatrixIsIdentity) {
6630 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006631 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006632 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006633 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006634 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 }
6636
6637 /**
6638 * Get the LayoutParams associated with this view. All views should have
6639 * layout parameters. These supply parameters to the <i>parent</i> of this
6640 * view specifying how it should be arranged. There are many subclasses of
6641 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6642 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08006643 *
6644 * This method may return null if this View is not attached to a parent
6645 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
6646 * was not invoked successfully. When a View is attached to a parent
6647 * ViewGroup, this method must not return null.
6648 *
6649 * @return The LayoutParams associated with this view, or null if no
6650 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006651 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006652 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006653 public ViewGroup.LayoutParams getLayoutParams() {
6654 return mLayoutParams;
6655 }
6656
6657 /**
6658 * Set the layout parameters associated with this view. These supply
6659 * parameters to the <i>parent</i> of this view specifying how it should be
6660 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6661 * correspond to the different subclasses of ViewGroup that are responsible
6662 * for arranging their children.
6663 *
Romain Guy01c174b2011-02-22 11:51:06 -08006664 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006665 */
6666 public void setLayoutParams(ViewGroup.LayoutParams params) {
6667 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08006668 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006669 }
6670 mLayoutParams = params;
6671 requestLayout();
6672 }
6673
6674 /**
6675 * Set the scrolled position of your view. This will cause a call to
6676 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6677 * invalidated.
6678 * @param x the x position to scroll to
6679 * @param y the y position to scroll to
6680 */
6681 public void scrollTo(int x, int y) {
6682 if (mScrollX != x || mScrollY != y) {
6683 int oldX = mScrollX;
6684 int oldY = mScrollY;
6685 mScrollX = x;
6686 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006687 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006689 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006690 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006692 }
6693 }
6694
6695 /**
6696 * Move the scrolled position of your view. This will cause a call to
6697 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6698 * invalidated.
6699 * @param x the amount of pixels to scroll by horizontally
6700 * @param y the amount of pixels to scroll by vertically
6701 */
6702 public void scrollBy(int x, int y) {
6703 scrollTo(mScrollX + x, mScrollY + y);
6704 }
6705
6706 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006707 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6708 * animation to fade the scrollbars out after a default delay. If a subclass
6709 * provides animated scrolling, the start delay should equal the duration
6710 * of the scrolling animation.</p>
6711 *
6712 * <p>The animation starts only if at least one of the scrollbars is
6713 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6714 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6715 * this method returns true, and false otherwise. If the animation is
6716 * started, this method calls {@link #invalidate()}; in that case the
6717 * caller should not call {@link #invalidate()}.</p>
6718 *
6719 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006720 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006721 *
6722 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6723 * and {@link #scrollTo(int, int)}.</p>
6724 *
6725 * @return true if the animation is played, false otherwise
6726 *
6727 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006728 * @see #scrollBy(int, int)
6729 * @see #scrollTo(int, int)
6730 * @see #isHorizontalScrollBarEnabled()
6731 * @see #isVerticalScrollBarEnabled()
6732 * @see #setHorizontalScrollBarEnabled(boolean)
6733 * @see #setVerticalScrollBarEnabled(boolean)
6734 */
6735 protected boolean awakenScrollBars() {
6736 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006737 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006738 }
6739
6740 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006741 * Trigger the scrollbars to draw.
6742 * This method differs from awakenScrollBars() only in its default duration.
6743 * initialAwakenScrollBars() will show the scroll bars for longer than
6744 * usual to give the user more of a chance to notice them.
6745 *
6746 * @return true if the animation is played, false otherwise.
6747 */
6748 private boolean initialAwakenScrollBars() {
6749 return mScrollCache != null &&
6750 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6751 }
6752
6753 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006754 * <p>
6755 * Trigger the scrollbars to draw. When invoked this method starts an
6756 * animation to fade the scrollbars out after a fixed delay. If a subclass
6757 * provides animated scrolling, the start delay should equal the duration of
6758 * the scrolling animation.
6759 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006760 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006761 * <p>
6762 * The animation starts only if at least one of the scrollbars is enabled,
6763 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6764 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6765 * this method returns true, and false otherwise. If the animation is
6766 * started, this method calls {@link #invalidate()}; in that case the caller
6767 * should not call {@link #invalidate()}.
6768 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006769 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006770 * <p>
6771 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006772 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006773 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006774 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006775 * @param startDelay the delay, in milliseconds, after which the animation
6776 * should start; when the delay is 0, the animation starts
6777 * immediately
6778 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006779 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006780 * @see #scrollBy(int, int)
6781 * @see #scrollTo(int, int)
6782 * @see #isHorizontalScrollBarEnabled()
6783 * @see #isVerticalScrollBarEnabled()
6784 * @see #setHorizontalScrollBarEnabled(boolean)
6785 * @see #setVerticalScrollBarEnabled(boolean)
6786 */
6787 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006788 return awakenScrollBars(startDelay, true);
6789 }
Joe Malin32736f02011-01-19 16:14:20 -08006790
Mike Cleron290947b2009-09-29 18:34:32 -07006791 /**
6792 * <p>
6793 * Trigger the scrollbars to draw. When invoked this method starts an
6794 * animation to fade the scrollbars out after a fixed delay. If a subclass
6795 * provides animated scrolling, the start delay should equal the duration of
6796 * the scrolling animation.
6797 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006798 *
Mike Cleron290947b2009-09-29 18:34:32 -07006799 * <p>
6800 * The animation starts only if at least one of the scrollbars is enabled,
6801 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6802 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6803 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006804 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006805 * is set to true; in that case the caller
6806 * should not call {@link #invalidate()}.
6807 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006808 *
Mike Cleron290947b2009-09-29 18:34:32 -07006809 * <p>
6810 * This method should be invoked everytime a subclass directly updates the
6811 * scroll parameters.
6812 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006813 *
Mike Cleron290947b2009-09-29 18:34:32 -07006814 * @param startDelay the delay, in milliseconds, after which the animation
6815 * should start; when the delay is 0, the animation starts
6816 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08006817 *
Mike Cleron290947b2009-09-29 18:34:32 -07006818 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08006819 *
Mike Cleron290947b2009-09-29 18:34:32 -07006820 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006821 *
Mike Cleron290947b2009-09-29 18:34:32 -07006822 * @see #scrollBy(int, int)
6823 * @see #scrollTo(int, int)
6824 * @see #isHorizontalScrollBarEnabled()
6825 * @see #isVerticalScrollBarEnabled()
6826 * @see #setHorizontalScrollBarEnabled(boolean)
6827 * @see #setVerticalScrollBarEnabled(boolean)
6828 */
6829 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006830 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08006831
Mike Cleronf116bf82009-09-27 19:14:12 -07006832 if (scrollCache == null || !scrollCache.fadeScrollBars) {
6833 return false;
6834 }
6835
6836 if (scrollCache.scrollBar == null) {
6837 scrollCache.scrollBar = new ScrollBarDrawable();
6838 }
6839
6840 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
6841
Mike Cleron290947b2009-09-29 18:34:32 -07006842 if (invalidate) {
6843 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08006844 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07006845 }
Mike Cleronf116bf82009-09-27 19:14:12 -07006846
6847 if (scrollCache.state == ScrollabilityCache.OFF) {
6848 // FIXME: this is copied from WindowManagerService.
6849 // We should get this value from the system when it
6850 // is possible to do so.
6851 final int KEY_REPEAT_FIRST_DELAY = 750;
6852 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
6853 }
6854
6855 // Tell mScrollCache when we should start fading. This may
6856 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07006857 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07006858 scrollCache.fadeStartTime = fadeStartTime;
6859 scrollCache.state = ScrollabilityCache.ON;
6860
6861 // Schedule our fader to run, unscheduling any old ones first
6862 if (mAttachInfo != null) {
6863 mAttachInfo.mHandler.removeCallbacks(scrollCache);
6864 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
6865 }
6866
6867 return true;
6868 }
6869
6870 return false;
6871 }
6872
6873 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006874 * Mark the the area defined by dirty as needing to be drawn. If the view is
6875 * visible, {@link #onDraw} will be called at some point in the future.
6876 * This must be called from a UI thread. To call from a non-UI thread, call
6877 * {@link #postInvalidate()}.
6878 *
6879 * WARNING: This method is destructive to dirty.
6880 * @param dirty the rectangle representing the bounds of the dirty region
6881 */
6882 public void invalidate(Rect dirty) {
6883 if (ViewDebug.TRACE_HIERARCHY) {
6884 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6885 }
6886
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006887 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006888 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6889 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006890 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006891 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006892 final ViewParent p = mParent;
6893 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006894 //noinspection PointlessBooleanExpression,ConstantConditions
6895 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6896 if (p != null && ai != null && ai.mHardwareAccelerated) {
6897 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6898 // with a null dirty rect, which tells the ViewRoot to redraw everything
6899 p.invalidateChild(this, null);
6900 return;
6901 }
Romain Guyaf636eb2010-12-09 17:47:21 -08006902 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006903 if (p != null && ai != null) {
6904 final int scrollX = mScrollX;
6905 final int scrollY = mScrollY;
6906 final Rect r = ai.mTmpInvalRect;
6907 r.set(dirty.left - scrollX, dirty.top - scrollY,
6908 dirty.right - scrollX, dirty.bottom - scrollY);
6909 mParent.invalidateChild(this, r);
6910 }
6911 }
6912 }
6913
6914 /**
6915 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
6916 * The coordinates of the dirty rect are relative to the view.
6917 * If the view is visible, {@link #onDraw} will be called at some point
6918 * in the future. This must be called from a UI thread. To call
6919 * from a non-UI thread, call {@link #postInvalidate()}.
6920 * @param l the left position of the dirty region
6921 * @param t the top position of the dirty region
6922 * @param r the right position of the dirty region
6923 * @param b the bottom position of the dirty region
6924 */
6925 public void invalidate(int l, int t, int r, int b) {
6926 if (ViewDebug.TRACE_HIERARCHY) {
6927 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6928 }
6929
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006930 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006931 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6932 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006933 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006934 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006935 final ViewParent p = mParent;
6936 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006937 //noinspection PointlessBooleanExpression,ConstantConditions
6938 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6939 if (p != null && ai != null && ai.mHardwareAccelerated) {
6940 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6941 // with a null dirty rect, which tells the ViewRoot to redraw everything
6942 p.invalidateChild(this, null);
6943 return;
6944 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08006945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006946 if (p != null && ai != null && l < r && t < b) {
6947 final int scrollX = mScrollX;
6948 final int scrollY = mScrollY;
6949 final Rect tmpr = ai.mTmpInvalRect;
6950 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
6951 p.invalidateChild(this, tmpr);
6952 }
6953 }
6954 }
6955
6956 /**
6957 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
6958 * be called at some point in the future. This must be called from a
6959 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
6960 */
6961 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07006962 invalidate(true);
6963 }
Joe Malin32736f02011-01-19 16:14:20 -08006964
Chet Haaseed032702010-10-01 14:05:54 -07006965 /**
6966 * This is where the invalidate() work actually happens. A full invalidate()
6967 * causes the drawing cache to be invalidated, but this function can be called with
6968 * invalidateCache set to false to skip that invalidation step for cases that do not
6969 * need it (for example, a component that remains at the same dimensions with the same
6970 * content).
6971 *
6972 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
6973 * well. This is usually true for a full invalidate, but may be set to false if the
6974 * View's contents or dimensions have not changed.
6975 */
Romain Guy849d0a32011-02-01 17:20:48 -08006976 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006977 if (ViewDebug.TRACE_HIERARCHY) {
6978 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6979 }
6980
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006981 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08006982 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08006983 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
6984 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07006985 mPrivateFlags &= ~DRAWN;
6986 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08006987 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07006988 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6989 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006990 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07006991 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08006992 //noinspection PointlessBooleanExpression,ConstantConditions
6993 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6994 if (p != null && ai != null && ai.mHardwareAccelerated) {
6995 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6996 // with a null dirty rect, which tells the ViewRoot to redraw everything
6997 p.invalidateChild(this, null);
6998 return;
6999 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007000 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007002 if (p != null && ai != null) {
7003 final Rect r = ai.mTmpInvalRect;
7004 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7005 // Don't call invalidate -- we don't want to internally scroll
7006 // our own bounds
7007 p.invalidateChild(this, r);
7008 }
7009 }
7010 }
7011
7012 /**
Romain Guyda489792011-02-03 01:05:15 -08007013 * @hide
7014 */
7015 public void fastInvalidate() {
7016 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7017 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7018 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7019 if (mParent instanceof View) {
7020 ((View) mParent).mPrivateFlags |= INVALIDATED;
7021 }
7022 mPrivateFlags &= ~DRAWN;
7023 mPrivateFlags |= INVALIDATED;
7024 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7025 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7026 mParent.invalidateChild(this, null);
7027 }
7028 }
7029 }
7030
7031 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007032 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007033 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7034 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007035 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7036 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007037 *
7038 * @hide
7039 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007040 protected void invalidateParentCaches() {
7041 if (mParent instanceof View) {
7042 ((View) mParent).mPrivateFlags |= INVALIDATED;
7043 }
7044 }
Joe Malin32736f02011-01-19 16:14:20 -08007045
Romain Guy0fd89bf2011-01-26 15:41:30 -08007046 /**
7047 * Used to indicate that the parent of this view should be invalidated. This functionality
7048 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7049 * which is necessary when various parent-managed properties of the view change, such as
7050 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7051 * an invalidation event to the parent.
7052 *
7053 * @hide
7054 */
7055 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007056 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007057 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007058 }
7059 }
7060
7061 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007062 * Indicates whether this View is opaque. An opaque View guarantees that it will
7063 * draw all the pixels overlapping its bounds using a fully opaque color.
7064 *
7065 * Subclasses of View should override this method whenever possible to indicate
7066 * whether an instance is opaque. Opaque Views are treated in a special way by
7067 * the View hierarchy, possibly allowing it to perform optimizations during
7068 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007069 *
Romain Guy24443ea2009-05-11 11:56:30 -07007070 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007071 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007072 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007073 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007074 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7075 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007076 }
7077
Adam Powell20232d02010-12-08 21:08:53 -08007078 /**
7079 * @hide
7080 */
7081 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007082 // Opaque if:
7083 // - Has a background
7084 // - Background is opaque
7085 // - Doesn't have scrollbars or scrollbars are inside overlay
7086
7087 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7088 mPrivateFlags |= OPAQUE_BACKGROUND;
7089 } else {
7090 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7091 }
7092
7093 final int flags = mViewFlags;
7094 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7095 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7096 mPrivateFlags |= OPAQUE_SCROLLBARS;
7097 } else {
7098 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7099 }
7100 }
7101
7102 /**
7103 * @hide
7104 */
7105 protected boolean hasOpaqueScrollbars() {
7106 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007107 }
7108
7109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007110 * @return A handler associated with the thread running the View. This
7111 * handler can be used to pump events in the UI events queue.
7112 */
7113 public Handler getHandler() {
7114 if (mAttachInfo != null) {
7115 return mAttachInfo.mHandler;
7116 }
7117 return null;
7118 }
7119
7120 /**
7121 * Causes the Runnable to be added to the message queue.
7122 * The runnable will be run on the user interface thread.
7123 *
7124 * @param action The Runnable that will be executed.
7125 *
7126 * @return Returns true if the Runnable was successfully placed in to the
7127 * message queue. Returns false on failure, usually because the
7128 * looper processing the message queue is exiting.
7129 */
7130 public boolean post(Runnable action) {
7131 Handler handler;
7132 if (mAttachInfo != null) {
7133 handler = mAttachInfo.mHandler;
7134 } else {
7135 // Assume that post will succeed later
7136 ViewRoot.getRunQueue().post(action);
7137 return true;
7138 }
7139
7140 return handler.post(action);
7141 }
7142
7143 /**
7144 * Causes the Runnable to be added to the message queue, to be run
7145 * after the specified amount of time elapses.
7146 * The runnable will be run on the user interface thread.
7147 *
7148 * @param action The Runnable that will be executed.
7149 * @param delayMillis The delay (in milliseconds) until the Runnable
7150 * will be executed.
7151 *
7152 * @return true if the Runnable was successfully placed in to the
7153 * message queue. Returns false on failure, usually because the
7154 * looper processing the message queue is exiting. Note that a
7155 * result of true does not mean the Runnable will be processed --
7156 * if the looper is quit before the delivery time of the message
7157 * occurs then the message will be dropped.
7158 */
7159 public boolean postDelayed(Runnable action, long delayMillis) {
7160 Handler handler;
7161 if (mAttachInfo != null) {
7162 handler = mAttachInfo.mHandler;
7163 } else {
7164 // Assume that post will succeed later
7165 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7166 return true;
7167 }
7168
7169 return handler.postDelayed(action, delayMillis);
7170 }
7171
7172 /**
7173 * Removes the specified Runnable from the message queue.
7174 *
7175 * @param action The Runnable to remove from the message handling queue
7176 *
7177 * @return true if this view could ask the Handler to remove the Runnable,
7178 * false otherwise. When the returned value is true, the Runnable
7179 * may or may not have been actually removed from the message queue
7180 * (for instance, if the Runnable was not in the queue already.)
7181 */
7182 public boolean removeCallbacks(Runnable action) {
7183 Handler handler;
7184 if (mAttachInfo != null) {
7185 handler = mAttachInfo.mHandler;
7186 } else {
7187 // Assume that post will succeed later
7188 ViewRoot.getRunQueue().removeCallbacks(action);
7189 return true;
7190 }
7191
7192 handler.removeCallbacks(action);
7193 return true;
7194 }
7195
7196 /**
7197 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7198 * Use this to invalidate the View from a non-UI thread.
7199 *
7200 * @see #invalidate()
7201 */
7202 public void postInvalidate() {
7203 postInvalidateDelayed(0);
7204 }
7205
7206 /**
7207 * Cause an invalidate of the specified area to happen on a subsequent cycle
7208 * through the event loop. Use this to invalidate the View from a non-UI thread.
7209 *
7210 * @param left The left coordinate of the rectangle to invalidate.
7211 * @param top The top coordinate of the rectangle to invalidate.
7212 * @param right The right coordinate of the rectangle to invalidate.
7213 * @param bottom The bottom coordinate of the rectangle to invalidate.
7214 *
7215 * @see #invalidate(int, int, int, int)
7216 * @see #invalidate(Rect)
7217 */
7218 public void postInvalidate(int left, int top, int right, int bottom) {
7219 postInvalidateDelayed(0, left, top, right, bottom);
7220 }
7221
7222 /**
7223 * Cause an invalidate to happen on a subsequent cycle through the event
7224 * loop. Waits for the specified amount of time.
7225 *
7226 * @param delayMilliseconds the duration in milliseconds to delay the
7227 * invalidation by
7228 */
7229 public void postInvalidateDelayed(long delayMilliseconds) {
7230 // We try only with the AttachInfo because there's no point in invalidating
7231 // if we are not attached to our window
7232 if (mAttachInfo != null) {
7233 Message msg = Message.obtain();
7234 msg.what = AttachInfo.INVALIDATE_MSG;
7235 msg.obj = this;
7236 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7237 }
7238 }
7239
7240 /**
7241 * Cause an invalidate of the specified area to happen on a subsequent cycle
7242 * through the event loop. Waits for the specified amount of time.
7243 *
7244 * @param delayMilliseconds the duration in milliseconds to delay the
7245 * invalidation by
7246 * @param left The left coordinate of the rectangle to invalidate.
7247 * @param top The top coordinate of the rectangle to invalidate.
7248 * @param right The right coordinate of the rectangle to invalidate.
7249 * @param bottom The bottom coordinate of the rectangle to invalidate.
7250 */
7251 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7252 int right, int bottom) {
7253
7254 // We try only with the AttachInfo because there's no point in invalidating
7255 // if we are not attached to our window
7256 if (mAttachInfo != null) {
7257 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7258 info.target = this;
7259 info.left = left;
7260 info.top = top;
7261 info.right = right;
7262 info.bottom = bottom;
7263
7264 final Message msg = Message.obtain();
7265 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7266 msg.obj = info;
7267 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7268 }
7269 }
7270
7271 /**
7272 * Called by a parent to request that a child update its values for mScrollX
7273 * and mScrollY if necessary. This will typically be done if the child is
7274 * animating a scroll using a {@link android.widget.Scroller Scroller}
7275 * object.
7276 */
7277 public void computeScroll() {
7278 }
7279
7280 /**
7281 * <p>Indicate whether the horizontal edges are faded when the view is
7282 * scrolled horizontally.</p>
7283 *
7284 * @return true if the horizontal edges should are faded on scroll, false
7285 * otherwise
7286 *
7287 * @see #setHorizontalFadingEdgeEnabled(boolean)
7288 * @attr ref android.R.styleable#View_fadingEdge
7289 */
7290 public boolean isHorizontalFadingEdgeEnabled() {
7291 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7292 }
7293
7294 /**
7295 * <p>Define whether the horizontal edges should be faded when this view
7296 * is scrolled horizontally.</p>
7297 *
7298 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7299 * be faded when the view is scrolled
7300 * horizontally
7301 *
7302 * @see #isHorizontalFadingEdgeEnabled()
7303 * @attr ref android.R.styleable#View_fadingEdge
7304 */
7305 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7306 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7307 if (horizontalFadingEdgeEnabled) {
7308 initScrollCache();
7309 }
7310
7311 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7312 }
7313 }
7314
7315 /**
7316 * <p>Indicate whether the vertical edges are faded when the view is
7317 * scrolled horizontally.</p>
7318 *
7319 * @return true if the vertical edges should are faded on scroll, false
7320 * otherwise
7321 *
7322 * @see #setVerticalFadingEdgeEnabled(boolean)
7323 * @attr ref android.R.styleable#View_fadingEdge
7324 */
7325 public boolean isVerticalFadingEdgeEnabled() {
7326 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7327 }
7328
7329 /**
7330 * <p>Define whether the vertical edges should be faded when this view
7331 * is scrolled vertically.</p>
7332 *
7333 * @param verticalFadingEdgeEnabled true if the vertical edges should
7334 * be faded when the view is scrolled
7335 * vertically
7336 *
7337 * @see #isVerticalFadingEdgeEnabled()
7338 * @attr ref android.R.styleable#View_fadingEdge
7339 */
7340 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7341 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7342 if (verticalFadingEdgeEnabled) {
7343 initScrollCache();
7344 }
7345
7346 mViewFlags ^= FADING_EDGE_VERTICAL;
7347 }
7348 }
7349
7350 /**
7351 * Returns the strength, or intensity, of the top faded edge. The strength is
7352 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7353 * returns 0.0 or 1.0 but no value in between.
7354 *
7355 * Subclasses should override this method to provide a smoother fade transition
7356 * when scrolling occurs.
7357 *
7358 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7359 */
7360 protected float getTopFadingEdgeStrength() {
7361 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7362 }
7363
7364 /**
7365 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7366 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7367 * returns 0.0 or 1.0 but no value in between.
7368 *
7369 * Subclasses should override this method to provide a smoother fade transition
7370 * when scrolling occurs.
7371 *
7372 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7373 */
7374 protected float getBottomFadingEdgeStrength() {
7375 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7376 computeVerticalScrollRange() ? 1.0f : 0.0f;
7377 }
7378
7379 /**
7380 * Returns the strength, or intensity, of the left faded edge. The strength is
7381 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7382 * returns 0.0 or 1.0 but no value in between.
7383 *
7384 * Subclasses should override this method to provide a smoother fade transition
7385 * when scrolling occurs.
7386 *
7387 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7388 */
7389 protected float getLeftFadingEdgeStrength() {
7390 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7391 }
7392
7393 /**
7394 * Returns the strength, or intensity, of the right faded edge. The strength is
7395 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7396 * returns 0.0 or 1.0 but no value in between.
7397 *
7398 * Subclasses should override this method to provide a smoother fade transition
7399 * when scrolling occurs.
7400 *
7401 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7402 */
7403 protected float getRightFadingEdgeStrength() {
7404 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7405 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7406 }
7407
7408 /**
7409 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7410 * scrollbar is not drawn by default.</p>
7411 *
7412 * @return true if the horizontal scrollbar should be painted, false
7413 * otherwise
7414 *
7415 * @see #setHorizontalScrollBarEnabled(boolean)
7416 */
7417 public boolean isHorizontalScrollBarEnabled() {
7418 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7419 }
7420
7421 /**
7422 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7423 * scrollbar is not drawn by default.</p>
7424 *
7425 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7426 * be painted
7427 *
7428 * @see #isHorizontalScrollBarEnabled()
7429 */
7430 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7431 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7432 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007433 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007434 recomputePadding();
7435 }
7436 }
7437
7438 /**
7439 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7440 * scrollbar is not drawn by default.</p>
7441 *
7442 * @return true if the vertical scrollbar should be painted, false
7443 * otherwise
7444 *
7445 * @see #setVerticalScrollBarEnabled(boolean)
7446 */
7447 public boolean isVerticalScrollBarEnabled() {
7448 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7449 }
7450
7451 /**
7452 * <p>Define whether the vertical scrollbar should be drawn or not. The
7453 * scrollbar is not drawn by default.</p>
7454 *
7455 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7456 * be painted
7457 *
7458 * @see #isVerticalScrollBarEnabled()
7459 */
7460 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7461 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7462 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007463 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 recomputePadding();
7465 }
7466 }
7467
Adam Powell20232d02010-12-08 21:08:53 -08007468 /**
7469 * @hide
7470 */
7471 protected void recomputePadding() {
7472 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007473 }
Joe Malin32736f02011-01-19 16:14:20 -08007474
Mike Cleronfe81d382009-09-28 14:22:16 -07007475 /**
7476 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007477 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007478 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007479 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007480 */
7481 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7482 initScrollCache();
7483 final ScrollabilityCache scrollabilityCache = mScrollCache;
7484 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007485 if (fadeScrollbars) {
7486 scrollabilityCache.state = ScrollabilityCache.OFF;
7487 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007488 scrollabilityCache.state = ScrollabilityCache.ON;
7489 }
7490 }
Joe Malin32736f02011-01-19 16:14:20 -08007491
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007492 /**
Joe Malin32736f02011-01-19 16:14:20 -08007493 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007494 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007495 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007496 * @return true if scrollbar fading is enabled
7497 */
7498 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007499 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007500 }
Joe Malin32736f02011-01-19 16:14:20 -08007501
Mike Cleron52f0a642009-09-28 18:21:37 -07007502 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007503 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7504 * inset. When inset, they add to the padding of the view. And the scrollbars
7505 * can be drawn inside the padding area or on the edge of the view. For example,
7506 * if a view has a background drawable and you want to draw the scrollbars
7507 * inside the padding specified by the drawable, you can use
7508 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7509 * appear at the edge of the view, ignoring the padding, then you can use
7510 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7511 * @param style the style of the scrollbars. Should be one of
7512 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7513 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7514 * @see #SCROLLBARS_INSIDE_OVERLAY
7515 * @see #SCROLLBARS_INSIDE_INSET
7516 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7517 * @see #SCROLLBARS_OUTSIDE_INSET
7518 */
7519 public void setScrollBarStyle(int style) {
7520 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7521 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007522 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 recomputePadding();
7524 }
7525 }
7526
7527 /**
7528 * <p>Returns the current scrollbar style.</p>
7529 * @return the current scrollbar style
7530 * @see #SCROLLBARS_INSIDE_OVERLAY
7531 * @see #SCROLLBARS_INSIDE_INSET
7532 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7533 * @see #SCROLLBARS_OUTSIDE_INSET
7534 */
7535 public int getScrollBarStyle() {
7536 return mViewFlags & SCROLLBARS_STYLE_MASK;
7537 }
7538
7539 /**
7540 * <p>Compute the horizontal range that the horizontal scrollbar
7541 * represents.</p>
7542 *
7543 * <p>The range is expressed in arbitrary units that must be the same as the
7544 * units used by {@link #computeHorizontalScrollExtent()} and
7545 * {@link #computeHorizontalScrollOffset()}.</p>
7546 *
7547 * <p>The default range is the drawing width of this view.</p>
7548 *
7549 * @return the total horizontal range represented by the horizontal
7550 * scrollbar
7551 *
7552 * @see #computeHorizontalScrollExtent()
7553 * @see #computeHorizontalScrollOffset()
7554 * @see android.widget.ScrollBarDrawable
7555 */
7556 protected int computeHorizontalScrollRange() {
7557 return getWidth();
7558 }
7559
7560 /**
7561 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7562 * within the horizontal range. This value is used to compute the position
7563 * of the thumb within the scrollbar's track.</p>
7564 *
7565 * <p>The range is expressed in arbitrary units that must be the same as the
7566 * units used by {@link #computeHorizontalScrollRange()} and
7567 * {@link #computeHorizontalScrollExtent()}.</p>
7568 *
7569 * <p>The default offset is the scroll offset of this view.</p>
7570 *
7571 * @return the horizontal offset of the scrollbar's thumb
7572 *
7573 * @see #computeHorizontalScrollRange()
7574 * @see #computeHorizontalScrollExtent()
7575 * @see android.widget.ScrollBarDrawable
7576 */
7577 protected int computeHorizontalScrollOffset() {
7578 return mScrollX;
7579 }
7580
7581 /**
7582 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7583 * within the horizontal range. This value is used to compute the length
7584 * of the thumb within the scrollbar's track.</p>
7585 *
7586 * <p>The range is expressed in arbitrary units that must be the same as the
7587 * units used by {@link #computeHorizontalScrollRange()} and
7588 * {@link #computeHorizontalScrollOffset()}.</p>
7589 *
7590 * <p>The default extent is the drawing width of this view.</p>
7591 *
7592 * @return the horizontal extent of the scrollbar's thumb
7593 *
7594 * @see #computeHorizontalScrollRange()
7595 * @see #computeHorizontalScrollOffset()
7596 * @see android.widget.ScrollBarDrawable
7597 */
7598 protected int computeHorizontalScrollExtent() {
7599 return getWidth();
7600 }
7601
7602 /**
7603 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7604 *
7605 * <p>The range is expressed in arbitrary units that must be the same as the
7606 * units used by {@link #computeVerticalScrollExtent()} and
7607 * {@link #computeVerticalScrollOffset()}.</p>
7608 *
7609 * @return the total vertical range represented by the vertical scrollbar
7610 *
7611 * <p>The default range is the drawing height of this view.</p>
7612 *
7613 * @see #computeVerticalScrollExtent()
7614 * @see #computeVerticalScrollOffset()
7615 * @see android.widget.ScrollBarDrawable
7616 */
7617 protected int computeVerticalScrollRange() {
7618 return getHeight();
7619 }
7620
7621 /**
7622 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7623 * within the horizontal range. This value is used to compute the position
7624 * of the thumb within the scrollbar's track.</p>
7625 *
7626 * <p>The range is expressed in arbitrary units that must be the same as the
7627 * units used by {@link #computeVerticalScrollRange()} and
7628 * {@link #computeVerticalScrollExtent()}.</p>
7629 *
7630 * <p>The default offset is the scroll offset of this view.</p>
7631 *
7632 * @return the vertical offset of the scrollbar's thumb
7633 *
7634 * @see #computeVerticalScrollRange()
7635 * @see #computeVerticalScrollExtent()
7636 * @see android.widget.ScrollBarDrawable
7637 */
7638 protected int computeVerticalScrollOffset() {
7639 return mScrollY;
7640 }
7641
7642 /**
7643 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7644 * within the vertical range. This value is used to compute the length
7645 * of the thumb within the scrollbar's track.</p>
7646 *
7647 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007648 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007649 * {@link #computeVerticalScrollOffset()}.</p>
7650 *
7651 * <p>The default extent is the drawing height of this view.</p>
7652 *
7653 * @return the vertical extent of the scrollbar's thumb
7654 *
7655 * @see #computeVerticalScrollRange()
7656 * @see #computeVerticalScrollOffset()
7657 * @see android.widget.ScrollBarDrawable
7658 */
7659 protected int computeVerticalScrollExtent() {
7660 return getHeight();
7661 }
7662
7663 /**
7664 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7665 * scrollbars are painted only if they have been awakened first.</p>
7666 *
7667 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007668 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007669 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007671 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 // scrollbars are drawn only when the animation is running
7673 final ScrollabilityCache cache = mScrollCache;
7674 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007675
Mike Cleronf116bf82009-09-27 19:14:12 -07007676 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007677
Mike Cleronf116bf82009-09-27 19:14:12 -07007678 if (state == ScrollabilityCache.OFF) {
7679 return;
7680 }
Joe Malin32736f02011-01-19 16:14:20 -08007681
Mike Cleronf116bf82009-09-27 19:14:12 -07007682 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007683
Mike Cleronf116bf82009-09-27 19:14:12 -07007684 if (state == ScrollabilityCache.FADING) {
7685 // We're fading -- get our fade interpolation
7686 if (cache.interpolatorValues == null) {
7687 cache.interpolatorValues = new float[1];
7688 }
Joe Malin32736f02011-01-19 16:14:20 -08007689
Mike Cleronf116bf82009-09-27 19:14:12 -07007690 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007691
Mike Cleronf116bf82009-09-27 19:14:12 -07007692 // Stops the animation if we're done
7693 if (cache.scrollBarInterpolator.timeToValues(values) ==
7694 Interpolator.Result.FREEZE_END) {
7695 cache.state = ScrollabilityCache.OFF;
7696 } else {
7697 cache.scrollBar.setAlpha(Math.round(values[0]));
7698 }
Joe Malin32736f02011-01-19 16:14:20 -08007699
7700 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007701 // drawing. We only want this when we're fading so that
7702 // we prevent excessive redraws
7703 invalidate = true;
7704 } else {
7705 // We're just on -- but we may have been fading before so
7706 // reset alpha
7707 cache.scrollBar.setAlpha(255);
7708 }
7709
Joe Malin32736f02011-01-19 16:14:20 -08007710
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007711 final int viewFlags = mViewFlags;
7712
7713 final boolean drawHorizontalScrollBar =
7714 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7715 final boolean drawVerticalScrollBar =
7716 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7717 && !isVerticalScrollBarHidden();
7718
7719 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7720 final int width = mRight - mLeft;
7721 final int height = mBottom - mTop;
7722
7723 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007724
Mike Reede8853fc2009-09-04 14:01:48 -04007725 final int scrollX = mScrollX;
7726 final int scrollY = mScrollY;
7727 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7728
Mike Cleronf116bf82009-09-27 19:14:12 -07007729 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007731 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007732 int size = scrollBar.getSize(false);
7733 if (size <= 0) {
7734 size = cache.scrollBarSize;
7735 }
7736
Mike Cleronf116bf82009-09-27 19:14:12 -07007737 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007738 computeHorizontalScrollOffset(),
7739 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007740 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007741 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007742 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007743 left = scrollX + (mPaddingLeft & inside);
7744 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7745 bottom = top + size;
7746 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7747 if (invalidate) {
7748 invalidate(left, top, right, bottom);
7749 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007750 }
7751
7752 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007753 int size = scrollBar.getSize(true);
7754 if (size <= 0) {
7755 size = cache.scrollBarSize;
7756 }
7757
Mike Reede8853fc2009-09-04 14:01:48 -04007758 scrollBar.setParameters(computeVerticalScrollRange(),
7759 computeVerticalScrollOffset(),
7760 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007761 switch (mVerticalScrollbarPosition) {
7762 default:
7763 case SCROLLBAR_POSITION_DEFAULT:
7764 case SCROLLBAR_POSITION_RIGHT:
7765 left = scrollX + width - size - (mUserPaddingRight & inside);
7766 break;
7767 case SCROLLBAR_POSITION_LEFT:
7768 left = scrollX + (mUserPaddingLeft & inside);
7769 break;
7770 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007771 top = scrollY + (mPaddingTop & inside);
7772 right = left + size;
7773 bottom = scrollY + height - (mUserPaddingBottom & inside);
7774 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7775 if (invalidate) {
7776 invalidate(left, top, right, bottom);
7777 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007778 }
7779 }
7780 }
7781 }
Romain Guy8506ab42009-06-11 17:35:47 -07007782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007784 * 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 -08007785 * FastScroller is visible.
7786 * @return whether to temporarily hide the vertical scrollbar
7787 * @hide
7788 */
7789 protected boolean isVerticalScrollBarHidden() {
7790 return false;
7791 }
7792
7793 /**
7794 * <p>Draw the horizontal scrollbar if
7795 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7796 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 * @param canvas the canvas on which to draw the scrollbar
7798 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 *
7800 * @see #isHorizontalScrollBarEnabled()
7801 * @see #computeHorizontalScrollRange()
7802 * @see #computeHorizontalScrollExtent()
7803 * @see #computeHorizontalScrollOffset()
7804 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07007805 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007806 */
Romain Guy8fb95422010-08-17 18:38:51 -07007807 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
7808 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007809 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007810 scrollBar.draw(canvas);
7811 }
Mike Reede8853fc2009-09-04 14:01:48 -04007812
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007814 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
7815 * returns true.</p>
7816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 * @param canvas the canvas on which to draw the scrollbar
7818 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007819 *
7820 * @see #isVerticalScrollBarEnabled()
7821 * @see #computeVerticalScrollRange()
7822 * @see #computeVerticalScrollExtent()
7823 * @see #computeVerticalScrollOffset()
7824 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04007825 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007826 */
Romain Guy8fb95422010-08-17 18:38:51 -07007827 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
7828 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04007829 scrollBar.setBounds(l, t, r, b);
7830 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007831 }
7832
7833 /**
7834 * Implement this to do your drawing.
7835 *
7836 * @param canvas the canvas on which the background will be drawn
7837 */
7838 protected void onDraw(Canvas canvas) {
7839 }
7840
7841 /*
7842 * Caller is responsible for calling requestLayout if necessary.
7843 * (This allows addViewInLayout to not request a new layout.)
7844 */
7845 void assignParent(ViewParent parent) {
7846 if (mParent == null) {
7847 mParent = parent;
7848 } else if (parent == null) {
7849 mParent = null;
7850 } else {
7851 throw new RuntimeException("view " + this + " being added, but"
7852 + " it already has a parent");
7853 }
7854 }
7855
7856 /**
7857 * This is called when the view is attached to a window. At this point it
7858 * has a Surface and will start drawing. Note that this function is
7859 * guaranteed to be called before {@link #onDraw}, however it may be called
7860 * any time before the first onDraw -- including before or after
7861 * {@link #onMeasure}.
7862 *
7863 * @see #onDetachedFromWindow()
7864 */
7865 protected void onAttachedToWindow() {
7866 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
7867 mParent.requestTransparentRegion(this);
7868 }
Adam Powell8568c3a2010-04-19 14:26:11 -07007869 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
7870 initialAwakenScrollBars();
7871 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
7872 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08007873 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007874 }
7875
7876 /**
7877 * This is called when the view is detached from a window. At this point it
7878 * no longer has a surface for drawing.
7879 *
7880 * @see #onAttachedToWindow()
7881 */
7882 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08007883 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08007884
Romain Guya440b002010-02-24 15:57:54 -08007885 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05007886 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08007887 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08007888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007889 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08007890
7891 if (mHardwareLayer != null) {
7892 mHardwareLayer.destroy();
7893 mHardwareLayer = null;
7894 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007895
Chet Haasedaf98e92011-01-10 14:10:36 -08007896 if (mDisplayList != null) {
7897 mDisplayList.invalidate();
7898 }
7899
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007900 if (mAttachInfo != null) {
7901 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
7902 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
7903 }
7904
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08007905 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007906 }
7907
7908 /**
7909 * @return The number of times this view has been attached to a window
7910 */
7911 protected int getWindowAttachCount() {
7912 return mWindowAttachCount;
7913 }
7914
7915 /**
7916 * Retrieve a unique token identifying the window this view is attached to.
7917 * @return Return the window's token for use in
7918 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
7919 */
7920 public IBinder getWindowToken() {
7921 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
7922 }
7923
7924 /**
7925 * Retrieve a unique token identifying the top-level "real" window of
7926 * the window that this view is attached to. That is, this is like
7927 * {@link #getWindowToken}, except if the window this view in is a panel
7928 * window (attached to another containing window), then the token of
7929 * the containing window is returned instead.
7930 *
7931 * @return Returns the associated window token, either
7932 * {@link #getWindowToken()} or the containing window's token.
7933 */
7934 public IBinder getApplicationWindowToken() {
7935 AttachInfo ai = mAttachInfo;
7936 if (ai != null) {
7937 IBinder appWindowToken = ai.mPanelParentWindowToken;
7938 if (appWindowToken == null) {
7939 appWindowToken = ai.mWindowToken;
7940 }
7941 return appWindowToken;
7942 }
7943 return null;
7944 }
7945
7946 /**
7947 * Retrieve private session object this view hierarchy is using to
7948 * communicate with the window manager.
7949 * @return the session object to communicate with the window manager
7950 */
7951 /*package*/ IWindowSession getWindowSession() {
7952 return mAttachInfo != null ? mAttachInfo.mSession : null;
7953 }
7954
7955 /**
7956 * @param info the {@link android.view.View.AttachInfo} to associated with
7957 * this view
7958 */
7959 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
7960 //System.out.println("Attached! " + this);
7961 mAttachInfo = info;
7962 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08007963 // We will need to evaluate the drawable state at least once.
7964 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 if (mFloatingTreeObserver != null) {
7966 info.mTreeObserver.merge(mFloatingTreeObserver);
7967 mFloatingTreeObserver = null;
7968 }
7969 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
7970 mAttachInfo.mScrollContainers.add(this);
7971 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
7972 }
7973 performCollectViewAttributes(visibility);
7974 onAttachedToWindow();
7975 int vis = info.mWindowVisibility;
7976 if (vis != GONE) {
7977 onWindowVisibilityChanged(vis);
7978 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08007979 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
7980 // If nobody has evaluated the drawable state yet, then do it now.
7981 refreshDrawableState();
7982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007983 }
7984
7985 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 AttachInfo info = mAttachInfo;
7987 if (info != null) {
7988 int vis = info.mWindowVisibility;
7989 if (vis != GONE) {
7990 onWindowVisibilityChanged(GONE);
7991 }
7992 }
7993
7994 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08007995
7996 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007997 mAttachInfo.mScrollContainers.remove(this);
7998 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
7999 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008001 mAttachInfo = null;
8002 }
8003
8004 /**
8005 * Store this view hierarchy's frozen state into the given container.
8006 *
8007 * @param container The SparseArray in which to save the view's state.
8008 *
8009 * @see #restoreHierarchyState
8010 * @see #dispatchSaveInstanceState
8011 * @see #onSaveInstanceState
8012 */
8013 public void saveHierarchyState(SparseArray<Parcelable> container) {
8014 dispatchSaveInstanceState(container);
8015 }
8016
8017 /**
8018 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8019 * May be overridden to modify how freezing happens to a view's children; for example, some
8020 * views may want to not store state for their children.
8021 *
8022 * @param container The SparseArray in which to save the view's state.
8023 *
8024 * @see #dispatchRestoreInstanceState
8025 * @see #saveHierarchyState
8026 * @see #onSaveInstanceState
8027 */
8028 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8029 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8030 mPrivateFlags &= ~SAVE_STATE_CALLED;
8031 Parcelable state = onSaveInstanceState();
8032 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8033 throw new IllegalStateException(
8034 "Derived class did not call super.onSaveInstanceState()");
8035 }
8036 if (state != null) {
8037 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8038 // + ": " + state);
8039 container.put(mID, state);
8040 }
8041 }
8042 }
8043
8044 /**
8045 * Hook allowing a view to generate a representation of its internal state
8046 * that can later be used to create a new instance with that same state.
8047 * This state should only contain information that is not persistent or can
8048 * not be reconstructed later. For example, you will never store your
8049 * current position on screen because that will be computed again when a
8050 * new instance of the view is placed in its view hierarchy.
8051 * <p>
8052 * Some examples of things you may store here: the current cursor position
8053 * in a text view (but usually not the text itself since that is stored in a
8054 * content provider or other persistent storage), the currently selected
8055 * item in a list view.
8056 *
8057 * @return Returns a Parcelable object containing the view's current dynamic
8058 * state, or null if there is nothing interesting to save. The
8059 * default implementation returns null.
8060 * @see #onRestoreInstanceState
8061 * @see #saveHierarchyState
8062 * @see #dispatchSaveInstanceState
8063 * @see #setSaveEnabled(boolean)
8064 */
8065 protected Parcelable onSaveInstanceState() {
8066 mPrivateFlags |= SAVE_STATE_CALLED;
8067 return BaseSavedState.EMPTY_STATE;
8068 }
8069
8070 /**
8071 * Restore this view hierarchy's frozen state from the given container.
8072 *
8073 * @param container The SparseArray which holds previously frozen states.
8074 *
8075 * @see #saveHierarchyState
8076 * @see #dispatchRestoreInstanceState
8077 * @see #onRestoreInstanceState
8078 */
8079 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8080 dispatchRestoreInstanceState(container);
8081 }
8082
8083 /**
8084 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8085 * children. May be overridden to modify how restoreing happens to a view's children; for
8086 * example, some views may want to not store state for their children.
8087 *
8088 * @param container The SparseArray which holds previously saved state.
8089 *
8090 * @see #dispatchSaveInstanceState
8091 * @see #restoreHierarchyState
8092 * @see #onRestoreInstanceState
8093 */
8094 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8095 if (mID != NO_ID) {
8096 Parcelable state = container.get(mID);
8097 if (state != null) {
8098 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8099 // + ": " + state);
8100 mPrivateFlags &= ~SAVE_STATE_CALLED;
8101 onRestoreInstanceState(state);
8102 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8103 throw new IllegalStateException(
8104 "Derived class did not call super.onRestoreInstanceState()");
8105 }
8106 }
8107 }
8108 }
8109
8110 /**
8111 * Hook allowing a view to re-apply a representation of its internal state that had previously
8112 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8113 * null state.
8114 *
8115 * @param state The frozen state that had previously been returned by
8116 * {@link #onSaveInstanceState}.
8117 *
8118 * @see #onSaveInstanceState
8119 * @see #restoreHierarchyState
8120 * @see #dispatchRestoreInstanceState
8121 */
8122 protected void onRestoreInstanceState(Parcelable state) {
8123 mPrivateFlags |= SAVE_STATE_CALLED;
8124 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008125 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8126 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008127 + "when two views of different type have the same id in the same hierarchy. "
8128 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008129 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008130 }
8131 }
8132
8133 /**
8134 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8135 *
8136 * @return the drawing start time in milliseconds
8137 */
8138 public long getDrawingTime() {
8139 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8140 }
8141
8142 /**
8143 * <p>Enables or disables the duplication of the parent's state into this view. When
8144 * duplication is enabled, this view gets its drawable state from its parent rather
8145 * than from its own internal properties.</p>
8146 *
8147 * <p>Note: in the current implementation, setting this property to true after the
8148 * view was added to a ViewGroup might have no effect at all. This property should
8149 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8150 *
8151 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8152 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008153 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008154 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8155 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008156 *
8157 * @param enabled True to enable duplication of the parent's drawable state, false
8158 * to disable it.
8159 *
8160 * @see #getDrawableState()
8161 * @see #isDuplicateParentStateEnabled()
8162 */
8163 public void setDuplicateParentStateEnabled(boolean enabled) {
8164 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8165 }
8166
8167 /**
8168 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8169 *
8170 * @return True if this view's drawable state is duplicated from the parent,
8171 * false otherwise
8172 *
8173 * @see #getDrawableState()
8174 * @see #setDuplicateParentStateEnabled(boolean)
8175 */
8176 public boolean isDuplicateParentStateEnabled() {
8177 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8178 }
8179
8180 /**
Romain Guy171c5922011-01-06 10:04:23 -08008181 * <p>Specifies the type of layer backing this view. The layer can be
8182 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8183 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008184 *
Romain Guy171c5922011-01-06 10:04:23 -08008185 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8186 * instance that controls how the layer is composed on screen. The following
8187 * properties of the paint are taken into account when composing the layer:</p>
8188 * <ul>
8189 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8190 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8191 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8192 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008193 *
Romain Guy171c5922011-01-06 10:04:23 -08008194 * <p>If this view has an alpha value set to < 1.0 by calling
8195 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8196 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8197 * equivalent to setting a hardware layer on this view and providing a paint with
8198 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008199 *
Romain Guy171c5922011-01-06 10:04:23 -08008200 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8201 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8202 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008203 *
Romain Guy171c5922011-01-06 10:04:23 -08008204 * @param layerType The ype of layer to use with this view, must be one of
8205 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8206 * {@link #LAYER_TYPE_HARDWARE}
8207 * @param paint The paint used to compose the layer. This argument is optional
8208 * and can be null. It is ignored when the layer type is
8209 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008210 *
8211 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008212 * @see #LAYER_TYPE_NONE
8213 * @see #LAYER_TYPE_SOFTWARE
8214 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008215 * @see #setAlpha(float)
8216 *
Romain Guy171c5922011-01-06 10:04:23 -08008217 * @attr ref android.R.styleable#View_layerType
8218 */
8219 public void setLayerType(int layerType, Paint paint) {
8220 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008221 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008222 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8223 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008224
Romain Guyd6cd5722011-01-17 14:42:41 -08008225 if (layerType == mLayerType) {
8226 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8227 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008228 invalidateParentCaches();
8229 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008230 }
8231 return;
8232 }
Romain Guy171c5922011-01-06 10:04:23 -08008233
8234 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008235 switch (mLayerType) {
8236 case LAYER_TYPE_SOFTWARE:
8237 if (mDrawingCache != null) {
8238 mDrawingCache.recycle();
8239 mDrawingCache = null;
8240 }
Joe Malin32736f02011-01-19 16:14:20 -08008241
Romain Guy6c319ca2011-01-11 14:29:25 -08008242 if (mUnscaledDrawingCache != null) {
8243 mUnscaledDrawingCache.recycle();
8244 mUnscaledDrawingCache = null;
8245 }
8246 break;
8247 case LAYER_TYPE_HARDWARE:
8248 if (mHardwareLayer != null) {
8249 mHardwareLayer.destroy();
8250 mHardwareLayer = null;
8251 }
8252 break;
8253 default:
8254 break;
Romain Guy171c5922011-01-06 10:04:23 -08008255 }
8256
8257 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008258 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8259 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8260 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008261
Romain Guy0fd89bf2011-01-26 15:41:30 -08008262 invalidateParentCaches();
8263 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008264 }
8265
8266 /**
8267 * Indicates what type of layer is currently associated with this view. By default
8268 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8269 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8270 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008271 *
Romain Guy171c5922011-01-06 10:04:23 -08008272 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8273 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008274 *
8275 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08008276 * @see #LAYER_TYPE_NONE
8277 * @see #LAYER_TYPE_SOFTWARE
8278 * @see #LAYER_TYPE_HARDWARE
8279 */
8280 public int getLayerType() {
8281 return mLayerType;
8282 }
Joe Malin32736f02011-01-19 16:14:20 -08008283
Romain Guy6c319ca2011-01-11 14:29:25 -08008284 /**
8285 * <p>Returns a hardware layer that can be used to draw this view again
8286 * without executing its draw method.</p>
8287 *
8288 * @return A HardwareLayer ready to render, or null if an error occurred.
8289 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008290 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008291 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8292 return null;
8293 }
8294
8295 final int width = mRight - mLeft;
8296 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008297
Romain Guy6c319ca2011-01-11 14:29:25 -08008298 if (width == 0 || height == 0) {
8299 return null;
8300 }
8301
8302 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8303 if (mHardwareLayer == null) {
8304 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8305 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008306 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008307 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8308 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008309 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008310 }
8311
Romain Guy5e7f7662011-01-24 22:35:56 -08008312 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8313 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8314 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008315 try {
8316 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008317 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008318 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008319
Romain Guy4f09f542011-01-26 22:41:43 -08008320 final int restoreCount = canvas.save();
8321
Romain Guy6c319ca2011-01-11 14:29:25 -08008322 computeScroll();
8323 canvas.translate(-mScrollX, -mScrollY);
8324
Romain Guy6c319ca2011-01-11 14:29:25 -08008325 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008326
Romain Guy6c319ca2011-01-11 14:29:25 -08008327 // Fast path for layouts with no backgrounds
8328 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8329 mPrivateFlags &= ~DIRTY_MASK;
8330 dispatchDraw(canvas);
8331 } else {
8332 draw(canvas);
8333 }
Joe Malin32736f02011-01-19 16:14:20 -08008334
Romain Guy6c319ca2011-01-11 14:29:25 -08008335 canvas.restoreToCount(restoreCount);
8336 } finally {
8337 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008338 mHardwareLayer.end(currentCanvas);
8339 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008340 }
8341 }
8342
8343 return mHardwareLayer;
8344 }
Romain Guy171c5922011-01-06 10:04:23 -08008345
8346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8348 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8349 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8350 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8351 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8352 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008353 *
Romain Guy171c5922011-01-06 10:04:23 -08008354 * <p>Enabling the drawing cache is similar to
8355 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008356 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8357 * drawing cache has no effect on rendering because the system uses a different mechanism
8358 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8359 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8360 * for information on how to enable software and hardware layers.</p>
8361 *
8362 * <p>This API can be used to manually generate
8363 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8364 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008365 *
8366 * @param enabled true to enable the drawing cache, false otherwise
8367 *
8368 * @see #isDrawingCacheEnabled()
8369 * @see #getDrawingCache()
8370 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008371 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008372 */
8373 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008374 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8376 }
8377
8378 /**
8379 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8380 *
8381 * @return true if the drawing cache is enabled
8382 *
8383 * @see #setDrawingCacheEnabled(boolean)
8384 * @see #getDrawingCache()
8385 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008386 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008387 public boolean isDrawingCacheEnabled() {
8388 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8389 }
8390
8391 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008392 * Debugging utility which recursively outputs the dirty state of a view and its
8393 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008394 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008395 * @hide
8396 */
Romain Guy676b1732011-02-14 14:45:33 -08008397 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008398 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8399 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8400 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8401 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8402 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8403 if (clear) {
8404 mPrivateFlags &= clearMask;
8405 }
8406 if (this instanceof ViewGroup) {
8407 ViewGroup parent = (ViewGroup) this;
8408 final int count = parent.getChildCount();
8409 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008410 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008411 child.outputDirtyFlags(indent + " ", clear, clearMask);
8412 }
8413 }
8414 }
8415
8416 /**
8417 * This method is used by ViewGroup to cause its children to restore or recreate their
8418 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8419 * to recreate its own display list, which would happen if it went through the normal
8420 * draw/dispatchDraw mechanisms.
8421 *
8422 * @hide
8423 */
8424 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008425
8426 /**
8427 * A view that is not attached or hardware accelerated cannot create a display list.
8428 * This method checks these conditions and returns the appropriate result.
8429 *
8430 * @return true if view has the ability to create a display list, false otherwise.
8431 *
8432 * @hide
8433 */
8434 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008435 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008436 }
Joe Malin32736f02011-01-19 16:14:20 -08008437
Chet Haasedaf98e92011-01-10 14:10:36 -08008438 /**
Romain Guyb051e892010-09-28 19:09:36 -07008439 * <p>Returns a display list that can be used to draw this view again
8440 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008441 *
Romain Guyb051e892010-09-28 19:09:36 -07008442 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008443 *
8444 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008445 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008446 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008447 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008448 return null;
8449 }
8450
Chet Haasedaf98e92011-01-10 14:10:36 -08008451 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8452 mDisplayList == null || !mDisplayList.isValid() ||
8453 mRecreateDisplayList)) {
8454 // Don't need to recreate the display list, just need to tell our
8455 // children to restore/recreate theirs
8456 if (mDisplayList != null && mDisplayList.isValid() &&
8457 !mRecreateDisplayList) {
8458 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8459 mPrivateFlags &= ~DIRTY_MASK;
8460 dispatchGetDisplayList();
8461
8462 return mDisplayList;
8463 }
8464
8465 // If we got here, we're recreating it. Mark it as such to ensure that
8466 // we copy in child display lists into ours in drawChild()
8467 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008468 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008469 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8470 // If we're creating a new display list, make sure our parent gets invalidated
8471 // since they will need to recreate their display list to account for this
8472 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008473 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008474 }
Romain Guyb051e892010-09-28 19:09:36 -07008475
8476 final HardwareCanvas canvas = mDisplayList.start();
8477 try {
8478 int width = mRight - mLeft;
8479 int height = mBottom - mTop;
8480
8481 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008482 // The dirty rect should always be null for a display list
8483 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008484
8485 final int restoreCount = canvas.save();
8486
Chet Haasedaf98e92011-01-10 14:10:36 -08008487 computeScroll();
8488 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008489 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008490
Romain Guyb051e892010-09-28 19:09:36 -07008491 // Fast path for layouts with no backgrounds
8492 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8493 mPrivateFlags &= ~DIRTY_MASK;
8494 dispatchDraw(canvas);
8495 } else {
8496 draw(canvas);
8497 }
Joe Malin32736f02011-01-19 16:14:20 -08008498
Romain Guyb051e892010-09-28 19:09:36 -07008499 canvas.restoreToCount(restoreCount);
8500 } finally {
8501 canvas.onPostDraw();
8502
8503 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008504 }
Chet Haase77785f92011-01-25 23:22:09 -08008505 } else {
8506 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8507 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008508 }
8509
8510 return mDisplayList;
8511 }
8512
8513 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008514 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008515 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008516 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008517 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008518 * @see #getDrawingCache(boolean)
8519 */
8520 public Bitmap getDrawingCache() {
8521 return getDrawingCache(false);
8522 }
8523
8524 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8526 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8527 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8528 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8529 * request the drawing cache by calling this method and draw it on screen if the
8530 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008531 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008532 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8533 * this method will create a bitmap of the same size as this view. Because this bitmap
8534 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8535 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8536 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8537 * size than the view. This implies that your application must be able to handle this
8538 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008539 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008540 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8541 * the current density of the screen when the application is in compatibility
8542 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008544 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008546 * @see #setDrawingCacheEnabled(boolean)
8547 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008548 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008549 * @see #destroyDrawingCache()
8550 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008551 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008552 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8553 return null;
8554 }
8555 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008556 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 }
Romain Guy02890fd2010-08-06 17:58:44 -07008558 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008559 }
8560
8561 /**
8562 * <p>Frees the resources used by the drawing cache. If you call
8563 * {@link #buildDrawingCache()} manually without calling
8564 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8565 * should cleanup the cache with this method afterwards.</p>
8566 *
8567 * @see #setDrawingCacheEnabled(boolean)
8568 * @see #buildDrawingCache()
8569 * @see #getDrawingCache()
8570 */
8571 public void destroyDrawingCache() {
8572 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008573 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 mDrawingCache = null;
8575 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008576 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008577 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008578 mUnscaledDrawingCache = null;
8579 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008580 }
8581
8582 /**
8583 * Setting a solid background color for the drawing cache's bitmaps will improve
8584 * perfromance and memory usage. Note, though that this should only be used if this
8585 * view will always be drawn on top of a solid color.
8586 *
8587 * @param color The background color to use for the drawing cache's bitmap
8588 *
8589 * @see #setDrawingCacheEnabled(boolean)
8590 * @see #buildDrawingCache()
8591 * @see #getDrawingCache()
8592 */
8593 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008594 if (color != mDrawingCacheBackgroundColor) {
8595 mDrawingCacheBackgroundColor = color;
8596 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008598 }
8599
8600 /**
8601 * @see #setDrawingCacheBackgroundColor(int)
8602 *
8603 * @return The background color to used for the drawing cache's bitmap
8604 */
8605 public int getDrawingCacheBackgroundColor() {
8606 return mDrawingCacheBackgroundColor;
8607 }
8608
8609 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008610 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008611 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008612 * @see #buildDrawingCache(boolean)
8613 */
8614 public void buildDrawingCache() {
8615 buildDrawingCache(false);
8616 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008617
Romain Guyfbd8f692009-06-26 14:51:58 -07008618 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008619 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8620 *
8621 * <p>If you call {@link #buildDrawingCache()} manually without calling
8622 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8623 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008624 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008625 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8626 * this method will create a bitmap of the same size as this view. Because this bitmap
8627 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8628 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8629 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8630 * size than the view. This implies that your application must be able to handle this
8631 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008632 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008633 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8634 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008635 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008636 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008637 *
8638 * @see #getDrawingCache()
8639 * @see #destroyDrawingCache()
8640 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008641 public void buildDrawingCache(boolean autoScale) {
8642 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008643 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008644 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008645
8646 if (ViewDebug.TRACE_HIERARCHY) {
8647 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008649
Romain Guy8506ab42009-06-11 17:35:47 -07008650 int width = mRight - mLeft;
8651 int height = mBottom - mTop;
8652
8653 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008654 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008655
Romain Guye1123222009-06-29 14:24:56 -07008656 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008657 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8658 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008659 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008660
8661 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008662 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008663 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664
8665 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008666 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008667 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8669 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008670 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008671 return;
8672 }
8673
8674 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008675 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008676
8677 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008678 Bitmap.Config quality;
8679 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008680 // Never pick ARGB_4444 because it looks awful
8681 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008682 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8683 case DRAWING_CACHE_QUALITY_AUTO:
8684 quality = Bitmap.Config.ARGB_8888;
8685 break;
8686 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008687 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008688 break;
8689 case DRAWING_CACHE_QUALITY_HIGH:
8690 quality = Bitmap.Config.ARGB_8888;
8691 break;
8692 default:
8693 quality = Bitmap.Config.ARGB_8888;
8694 break;
8695 }
8696 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008697 // Optimization for translucent windows
8698 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008699 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008700 }
8701
8702 // Try to cleanup memory
8703 if (bitmap != null) bitmap.recycle();
8704
8705 try {
8706 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008707 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008708 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008709 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008710 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008711 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008712 }
Adam Powell26153a32010-11-08 15:22:27 -08008713 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 } catch (OutOfMemoryError e) {
8715 // If there is not enough memory to create the bitmap cache, just
8716 // ignore the issue as bitmap caches are not required to draw the
8717 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008718 if (autoScale) {
8719 mDrawingCache = null;
8720 } else {
8721 mUnscaledDrawingCache = null;
8722 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008723 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008724 return;
8725 }
8726
8727 clear = drawingCacheBackgroundColor != 0;
8728 }
8729
8730 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 if (attachInfo != null) {
8732 canvas = attachInfo.mCanvas;
8733 if (canvas == null) {
8734 canvas = new Canvas();
8735 }
8736 canvas.setBitmap(bitmap);
8737 // Temporarily clobber the cached Canvas in case one of our children
8738 // is also using a drawing cache. Without this, the children would
8739 // steal the canvas by attaching their own bitmap to it and bad, bad
8740 // thing would happen (invisible views, corrupted drawings, etc.)
8741 attachInfo.mCanvas = null;
8742 } else {
8743 // This case should hopefully never or seldom happen
8744 canvas = new Canvas(bitmap);
8745 }
8746
8747 if (clear) {
8748 bitmap.eraseColor(drawingCacheBackgroundColor);
8749 }
8750
8751 computeScroll();
8752 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08008753
Romain Guye1123222009-06-29 14:24:56 -07008754 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008755 final float scale = attachInfo.mApplicationScale;
8756 canvas.scale(scale, scale);
8757 }
Joe Malin32736f02011-01-19 16:14:20 -08008758
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008759 canvas.translate(-mScrollX, -mScrollY);
8760
Romain Guy5bcdff42009-05-14 21:27:18 -07008761 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08008762 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
8763 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07008764 mPrivateFlags |= DRAWING_CACHE_VALID;
8765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008766
8767 // Fast path for layouts with no backgrounds
8768 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8769 if (ViewDebug.TRACE_HIERARCHY) {
8770 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8771 }
Romain Guy5bcdff42009-05-14 21:27:18 -07008772 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008773 dispatchDraw(canvas);
8774 } else {
8775 draw(canvas);
8776 }
8777
8778 canvas.restoreToCount(restoreCount);
8779
8780 if (attachInfo != null) {
8781 // Restore the cached Canvas for our siblings
8782 attachInfo.mCanvas = canvas;
8783 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 }
8785 }
8786
8787 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008788 * Create a snapshot of the view into a bitmap. We should probably make
8789 * some form of this public, but should think about the API.
8790 */
Romain Guy223ff5c2010-03-02 17:07:47 -08008791 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008792 int width = mRight - mLeft;
8793 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008794
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008795 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07008796 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008797 width = (int) ((width * scale) + 0.5f);
8798 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08008799
Romain Guy8c11e312009-09-14 15:15:30 -07008800 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008801 if (bitmap == null) {
8802 throw new OutOfMemoryError();
8803 }
8804
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008805 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08008806
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008807 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008808 if (attachInfo != null) {
8809 canvas = attachInfo.mCanvas;
8810 if (canvas == null) {
8811 canvas = new Canvas();
8812 }
8813 canvas.setBitmap(bitmap);
8814 // Temporarily clobber the cached Canvas in case one of our children
8815 // is also using a drawing cache. Without this, the children would
8816 // steal the canvas by attaching their own bitmap to it and bad, bad
8817 // things would happen (invisible views, corrupted drawings, etc.)
8818 attachInfo.mCanvas = null;
8819 } else {
8820 // This case should hopefully never or seldom happen
8821 canvas = new Canvas(bitmap);
8822 }
8823
Romain Guy5bcdff42009-05-14 21:27:18 -07008824 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008825 bitmap.eraseColor(backgroundColor);
8826 }
8827
8828 computeScroll();
8829 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008830 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008831 canvas.translate(-mScrollX, -mScrollY);
8832
Romain Guy5bcdff42009-05-14 21:27:18 -07008833 // Temporarily remove the dirty mask
8834 int flags = mPrivateFlags;
8835 mPrivateFlags &= ~DIRTY_MASK;
8836
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008837 // Fast path for layouts with no backgrounds
8838 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8839 dispatchDraw(canvas);
8840 } else {
8841 draw(canvas);
8842 }
8843
Romain Guy5bcdff42009-05-14 21:27:18 -07008844 mPrivateFlags = flags;
8845
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008846 canvas.restoreToCount(restoreCount);
8847
8848 if (attachInfo != null) {
8849 // Restore the cached Canvas for our siblings
8850 attachInfo.mCanvas = canvas;
8851 }
Romain Guy8506ab42009-06-11 17:35:47 -07008852
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008853 return bitmap;
8854 }
8855
8856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 * Indicates whether this View is currently in edit mode. A View is usually
8858 * in edit mode when displayed within a developer tool. For instance, if
8859 * this View is being drawn by a visual user interface builder, this method
8860 * should return true.
8861 *
8862 * Subclasses should check the return value of this method to provide
8863 * different behaviors if their normal behavior might interfere with the
8864 * host environment. For instance: the class spawns a thread in its
8865 * constructor, the drawing code relies on device-specific features, etc.
8866 *
8867 * This method is usually checked in the drawing code of custom widgets.
8868 *
8869 * @return True if this View is in edit mode, false otherwise.
8870 */
8871 public boolean isInEditMode() {
8872 return false;
8873 }
8874
8875 /**
8876 * If the View draws content inside its padding and enables fading edges,
8877 * it needs to support padding offsets. Padding offsets are added to the
8878 * fading edges to extend the length of the fade so that it covers pixels
8879 * drawn inside the padding.
8880 *
8881 * Subclasses of this class should override this method if they need
8882 * to draw content inside the padding.
8883 *
8884 * @return True if padding offset must be applied, false otherwise.
8885 *
8886 * @see #getLeftPaddingOffset()
8887 * @see #getRightPaddingOffset()
8888 * @see #getTopPaddingOffset()
8889 * @see #getBottomPaddingOffset()
8890 *
8891 * @since CURRENT
8892 */
8893 protected boolean isPaddingOffsetRequired() {
8894 return false;
8895 }
8896
8897 /**
8898 * Amount by which to extend the left fading region. Called only when
8899 * {@link #isPaddingOffsetRequired()} returns true.
8900 *
8901 * @return The left padding offset in pixels.
8902 *
8903 * @see #isPaddingOffsetRequired()
8904 *
8905 * @since CURRENT
8906 */
8907 protected int getLeftPaddingOffset() {
8908 return 0;
8909 }
8910
8911 /**
8912 * Amount by which to extend the right fading region. Called only when
8913 * {@link #isPaddingOffsetRequired()} returns true.
8914 *
8915 * @return The right padding offset in pixels.
8916 *
8917 * @see #isPaddingOffsetRequired()
8918 *
8919 * @since CURRENT
8920 */
8921 protected int getRightPaddingOffset() {
8922 return 0;
8923 }
8924
8925 /**
8926 * Amount by which to extend the top fading region. Called only when
8927 * {@link #isPaddingOffsetRequired()} returns true.
8928 *
8929 * @return The top padding offset in pixels.
8930 *
8931 * @see #isPaddingOffsetRequired()
8932 *
8933 * @since CURRENT
8934 */
8935 protected int getTopPaddingOffset() {
8936 return 0;
8937 }
8938
8939 /**
8940 * Amount by which to extend the bottom fading region. Called only when
8941 * {@link #isPaddingOffsetRequired()} returns true.
8942 *
8943 * @return The bottom padding offset in pixels.
8944 *
8945 * @see #isPaddingOffsetRequired()
8946 *
8947 * @since CURRENT
8948 */
8949 protected int getBottomPaddingOffset() {
8950 return 0;
8951 }
8952
8953 /**
Romain Guy2bffd262010-09-12 17:40:02 -07008954 * <p>Indicates whether this view is attached to an hardware accelerated
8955 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008956 *
Romain Guy2bffd262010-09-12 17:40:02 -07008957 * <p>Even if this method returns true, it does not mean that every call
8958 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
8959 * accelerated {@link android.graphics.Canvas}. For instance, if this view
8960 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
8961 * window is hardware accelerated,
8962 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
8963 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008964 *
Romain Guy2bffd262010-09-12 17:40:02 -07008965 * @return True if the view is attached to a window and the window is
8966 * hardware accelerated; false in any other case.
8967 */
8968 public boolean isHardwareAccelerated() {
8969 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
8970 }
Joe Malin32736f02011-01-19 16:14:20 -08008971
Romain Guy2bffd262010-09-12 17:40:02 -07008972 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008973 * Manually render this view (and all of its children) to the given Canvas.
8974 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08008975 * called. When implementing a view, implement {@link #onDraw} instead of
8976 * overriding this method. If you do need to override this method, call
8977 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008978 *
8979 * @param canvas The Canvas to which the View is rendered.
8980 */
8981 public void draw(Canvas canvas) {
8982 if (ViewDebug.TRACE_HIERARCHY) {
8983 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8984 }
8985
Romain Guy5bcdff42009-05-14 21:27:18 -07008986 final int privateFlags = mPrivateFlags;
8987 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
8988 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
8989 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07008990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 /*
8992 * Draw traversal performs several drawing steps which must be executed
8993 * in the appropriate order:
8994 *
8995 * 1. Draw the background
8996 * 2. If necessary, save the canvas' layers to prepare for fading
8997 * 3. Draw view's content
8998 * 4. Draw children
8999 * 5. If necessary, draw the fading edges and restore layers
9000 * 6. Draw decorations (scrollbars for instance)
9001 */
9002
9003 // Step 1, draw the background, if needed
9004 int saveCount;
9005
Romain Guy24443ea2009-05-11 11:56:30 -07009006 if (!dirtyOpaque) {
9007 final Drawable background = mBGDrawable;
9008 if (background != null) {
9009 final int scrollX = mScrollX;
9010 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009011
Romain Guy24443ea2009-05-11 11:56:30 -07009012 if (mBackgroundSizeChanged) {
9013 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9014 mBackgroundSizeChanged = false;
9015 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009016
Romain Guy24443ea2009-05-11 11:56:30 -07009017 if ((scrollX | scrollY) == 0) {
9018 background.draw(canvas);
9019 } else {
9020 canvas.translate(scrollX, scrollY);
9021 background.draw(canvas);
9022 canvas.translate(-scrollX, -scrollY);
9023 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009024 }
9025 }
9026
9027 // skip step 2 & 5 if possible (common case)
9028 final int viewFlags = mViewFlags;
9029 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9030 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9031 if (!verticalEdges && !horizontalEdges) {
9032 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009033 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034
9035 // Step 4, draw the children
9036 dispatchDraw(canvas);
9037
9038 // Step 6, draw decorations (scrollbars)
9039 onDrawScrollBars(canvas);
9040
9041 // we're done...
9042 return;
9043 }
9044
9045 /*
9046 * Here we do the full fledged routine...
9047 * (this is an uncommon case where speed matters less,
9048 * this is why we repeat some of the tests that have been
9049 * done above)
9050 */
9051
9052 boolean drawTop = false;
9053 boolean drawBottom = false;
9054 boolean drawLeft = false;
9055 boolean drawRight = false;
9056
9057 float topFadeStrength = 0.0f;
9058 float bottomFadeStrength = 0.0f;
9059 float leftFadeStrength = 0.0f;
9060 float rightFadeStrength = 0.0f;
9061
9062 // Step 2, save the canvas' layers
9063 int paddingLeft = mPaddingLeft;
9064 int paddingTop = mPaddingTop;
9065
9066 final boolean offsetRequired = isPaddingOffsetRequired();
9067 if (offsetRequired) {
9068 paddingLeft += getLeftPaddingOffset();
9069 paddingTop += getTopPaddingOffset();
9070 }
9071
9072 int left = mScrollX + paddingLeft;
9073 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9074 int top = mScrollY + paddingTop;
9075 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9076
9077 if (offsetRequired) {
9078 right += getRightPaddingOffset();
9079 bottom += getBottomPaddingOffset();
9080 }
9081
9082 final ScrollabilityCache scrollabilityCache = mScrollCache;
9083 int length = scrollabilityCache.fadingEdgeLength;
9084
9085 // clip the fade length if top and bottom fades overlap
9086 // overlapping fades produce odd-looking artifacts
9087 if (verticalEdges && (top + length > bottom - length)) {
9088 length = (bottom - top) / 2;
9089 }
9090
9091 // also clip horizontal fades if necessary
9092 if (horizontalEdges && (left + length > right - length)) {
9093 length = (right - left) / 2;
9094 }
9095
9096 if (verticalEdges) {
9097 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009098 drawTop = topFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009100 drawBottom = bottomFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009101 }
9102
9103 if (horizontalEdges) {
9104 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009105 drawLeft = leftFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009106 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009107 drawRight = rightFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 }
9109
9110 saveCount = canvas.getSaveCount();
9111
9112 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009113 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009114 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9115
9116 if (drawTop) {
9117 canvas.saveLayer(left, top, right, top + length, null, flags);
9118 }
9119
9120 if (drawBottom) {
9121 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9122 }
9123
9124 if (drawLeft) {
9125 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9126 }
9127
9128 if (drawRight) {
9129 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9130 }
9131 } else {
9132 scrollabilityCache.setFadeColor(solidColor);
9133 }
9134
9135 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009136 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137
9138 // Step 4, draw the children
9139 dispatchDraw(canvas);
9140
9141 // Step 5, draw the fade effect and restore layers
9142 final Paint p = scrollabilityCache.paint;
9143 final Matrix matrix = scrollabilityCache.matrix;
9144 final Shader fade = scrollabilityCache.shader;
9145 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9146
9147 if (drawTop) {
9148 matrix.setScale(1, fadeHeight * topFadeStrength);
9149 matrix.postTranslate(left, top);
9150 fade.setLocalMatrix(matrix);
9151 canvas.drawRect(left, top, right, top + length, p);
9152 }
9153
9154 if (drawBottom) {
9155 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9156 matrix.postRotate(180);
9157 matrix.postTranslate(left, bottom);
9158 fade.setLocalMatrix(matrix);
9159 canvas.drawRect(left, bottom - length, right, bottom, p);
9160 }
9161
9162 if (drawLeft) {
9163 matrix.setScale(1, fadeHeight * leftFadeStrength);
9164 matrix.postRotate(-90);
9165 matrix.postTranslate(left, top);
9166 fade.setLocalMatrix(matrix);
9167 canvas.drawRect(left, top, left + length, bottom, p);
9168 }
9169
9170 if (drawRight) {
9171 matrix.setScale(1, fadeHeight * rightFadeStrength);
9172 matrix.postRotate(90);
9173 matrix.postTranslate(right, top);
9174 fade.setLocalMatrix(matrix);
9175 canvas.drawRect(right - length, top, right, bottom, p);
9176 }
9177
9178 canvas.restoreToCount(saveCount);
9179
9180 // Step 6, draw decorations (scrollbars)
9181 onDrawScrollBars(canvas);
9182 }
9183
9184 /**
9185 * Override this if your view is known to always be drawn on top of a solid color background,
9186 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9187 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9188 * should be set to 0xFF.
9189 *
9190 * @see #setVerticalFadingEdgeEnabled
9191 * @see #setHorizontalFadingEdgeEnabled
9192 *
9193 * @return The known solid color background for this view, or 0 if the color may vary
9194 */
9195 public int getSolidColor() {
9196 return 0;
9197 }
9198
9199 /**
9200 * Build a human readable string representation of the specified view flags.
9201 *
9202 * @param flags the view flags to convert to a string
9203 * @return a String representing the supplied flags
9204 */
9205 private static String printFlags(int flags) {
9206 String output = "";
9207 int numFlags = 0;
9208 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9209 output += "TAKES_FOCUS";
9210 numFlags++;
9211 }
9212
9213 switch (flags & VISIBILITY_MASK) {
9214 case INVISIBLE:
9215 if (numFlags > 0) {
9216 output += " ";
9217 }
9218 output += "INVISIBLE";
9219 // USELESS HERE numFlags++;
9220 break;
9221 case GONE:
9222 if (numFlags > 0) {
9223 output += " ";
9224 }
9225 output += "GONE";
9226 // USELESS HERE numFlags++;
9227 break;
9228 default:
9229 break;
9230 }
9231 return output;
9232 }
9233
9234 /**
9235 * Build a human readable string representation of the specified private
9236 * view flags.
9237 *
9238 * @param privateFlags the private view flags to convert to a string
9239 * @return a String representing the supplied flags
9240 */
9241 private static String printPrivateFlags(int privateFlags) {
9242 String output = "";
9243 int numFlags = 0;
9244
9245 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9246 output += "WANTS_FOCUS";
9247 numFlags++;
9248 }
9249
9250 if ((privateFlags & FOCUSED) == FOCUSED) {
9251 if (numFlags > 0) {
9252 output += " ";
9253 }
9254 output += "FOCUSED";
9255 numFlags++;
9256 }
9257
9258 if ((privateFlags & SELECTED) == SELECTED) {
9259 if (numFlags > 0) {
9260 output += " ";
9261 }
9262 output += "SELECTED";
9263 numFlags++;
9264 }
9265
9266 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9267 if (numFlags > 0) {
9268 output += " ";
9269 }
9270 output += "IS_ROOT_NAMESPACE";
9271 numFlags++;
9272 }
9273
9274 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9275 if (numFlags > 0) {
9276 output += " ";
9277 }
9278 output += "HAS_BOUNDS";
9279 numFlags++;
9280 }
9281
9282 if ((privateFlags & DRAWN) == DRAWN) {
9283 if (numFlags > 0) {
9284 output += " ";
9285 }
9286 output += "DRAWN";
9287 // USELESS HERE numFlags++;
9288 }
9289 return output;
9290 }
9291
9292 /**
9293 * <p>Indicates whether or not this view's layout will be requested during
9294 * the next hierarchy layout pass.</p>
9295 *
9296 * @return true if the layout will be forced during next layout pass
9297 */
9298 public boolean isLayoutRequested() {
9299 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9300 }
9301
9302 /**
9303 * Assign a size and position to a view and all of its
9304 * descendants
9305 *
9306 * <p>This is the second phase of the layout mechanism.
9307 * (The first is measuring). In this phase, each parent calls
9308 * layout on all of its children to position them.
9309 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009310 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 *
Chet Haase9c087442011-01-12 16:20:16 -08009312 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009313 * Derived classes with children should override
9314 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009315 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 *
9317 * @param l Left position, relative to parent
9318 * @param t Top position, relative to parent
9319 * @param r Right position, relative to parent
9320 * @param b Bottom position, relative to parent
9321 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009322 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009323 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009324 int oldL = mLeft;
9325 int oldT = mTop;
9326 int oldB = mBottom;
9327 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009328 boolean changed = setFrame(l, t, r, b);
9329 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9330 if (ViewDebug.TRACE_HIERARCHY) {
9331 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9332 }
9333
9334 onLayout(changed, l, t, r, b);
9335 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009336
9337 if (mOnLayoutChangeListeners != null) {
9338 ArrayList<OnLayoutChangeListener> listenersCopy =
9339 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9340 int numListeners = listenersCopy.size();
9341 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009342 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009343 }
9344 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009345 }
9346 mPrivateFlags &= ~FORCE_LAYOUT;
9347 }
9348
9349 /**
9350 * Called from layout when this view should
9351 * assign a size and position to each of its children.
9352 *
9353 * Derived classes with children should override
9354 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009355 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009356 * @param changed This is a new size or position for this view
9357 * @param left Left position, relative to parent
9358 * @param top Top position, relative to parent
9359 * @param right Right position, relative to parent
9360 * @param bottom Bottom position, relative to parent
9361 */
9362 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9363 }
9364
9365 /**
9366 * Assign a size and position to this view.
9367 *
9368 * This is called from layout.
9369 *
9370 * @param left Left position, relative to parent
9371 * @param top Top position, relative to parent
9372 * @param right Right position, relative to parent
9373 * @param bottom Bottom position, relative to parent
9374 * @return true if the new size and position are different than the
9375 * previous ones
9376 * {@hide}
9377 */
9378 protected boolean setFrame(int left, int top, int right, int bottom) {
9379 boolean changed = false;
9380
9381 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009382 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009383 + right + "," + bottom + ")");
9384 }
9385
9386 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9387 changed = true;
9388
9389 // Remember our drawn bit
9390 int drawn = mPrivateFlags & DRAWN;
9391
9392 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009393 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009394
9395
9396 int oldWidth = mRight - mLeft;
9397 int oldHeight = mBottom - mTop;
9398
9399 mLeft = left;
9400 mTop = top;
9401 mRight = right;
9402 mBottom = bottom;
9403
9404 mPrivateFlags |= HAS_BOUNDS;
9405
9406 int newWidth = right - left;
9407 int newHeight = bottom - top;
9408
9409 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009410 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9411 // A change in dimension means an auto-centered pivot point changes, too
9412 mMatrixDirty = true;
9413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009414 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9415 }
9416
9417 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9418 // If we are visible, force the DRAWN bit to on so that
9419 // this invalidate will go through (at least to our parent).
9420 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009421 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 // the DRAWN bit.
9423 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009424 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009425 // parent display list may need to be recreated based on a change in the bounds
9426 // of any child
9427 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009428 }
9429
9430 // Reset drawn bit to original value (invalidate turns it off)
9431 mPrivateFlags |= drawn;
9432
9433 mBackgroundSizeChanged = true;
9434 }
9435 return changed;
9436 }
9437
9438 /**
9439 * Finalize inflating a view from XML. This is called as the last phase
9440 * of inflation, after all child views have been added.
9441 *
9442 * <p>Even if the subclass overrides onFinishInflate, they should always be
9443 * sure to call the super method, so that we get called.
9444 */
9445 protected void onFinishInflate() {
9446 }
9447
9448 /**
9449 * Returns the resources associated with this view.
9450 *
9451 * @return Resources object.
9452 */
9453 public Resources getResources() {
9454 return mResources;
9455 }
9456
9457 /**
9458 * Invalidates the specified Drawable.
9459 *
9460 * @param drawable the drawable to invalidate
9461 */
9462 public void invalidateDrawable(Drawable drawable) {
9463 if (verifyDrawable(drawable)) {
9464 final Rect dirty = drawable.getBounds();
9465 final int scrollX = mScrollX;
9466 final int scrollY = mScrollY;
9467
9468 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9469 dirty.right + scrollX, dirty.bottom + scrollY);
9470 }
9471 }
9472
9473 /**
9474 * Schedules an action on a drawable to occur at a specified time.
9475 *
9476 * @param who the recipient of the action
9477 * @param what the action to run on the drawable
9478 * @param when the time at which the action must occur. Uses the
9479 * {@link SystemClock#uptimeMillis} timebase.
9480 */
9481 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9482 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9483 mAttachInfo.mHandler.postAtTime(what, who, when);
9484 }
9485 }
9486
9487 /**
9488 * Cancels a scheduled action on a drawable.
9489 *
9490 * @param who the recipient of the action
9491 * @param what the action to cancel
9492 */
9493 public void unscheduleDrawable(Drawable who, Runnable what) {
9494 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9495 mAttachInfo.mHandler.removeCallbacks(what, who);
9496 }
9497 }
9498
9499 /**
9500 * Unschedule any events associated with the given Drawable. This can be
9501 * used when selecting a new Drawable into a view, so that the previous
9502 * one is completely unscheduled.
9503 *
9504 * @param who The Drawable to unschedule.
9505 *
9506 * @see #drawableStateChanged
9507 */
9508 public void unscheduleDrawable(Drawable who) {
9509 if (mAttachInfo != null) {
9510 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9511 }
9512 }
9513
9514 /**
9515 * If your view subclass is displaying its own Drawable objects, it should
9516 * override this function and return true for any Drawable it is
9517 * displaying. This allows animations for those drawables to be
9518 * scheduled.
9519 *
9520 * <p>Be sure to call through to the super class when overriding this
9521 * function.
9522 *
9523 * @param who The Drawable to verify. Return true if it is one you are
9524 * displaying, else return the result of calling through to the
9525 * super class.
9526 *
9527 * @return boolean If true than the Drawable is being displayed in the
9528 * view; else false and it is not allowed to animate.
9529 *
9530 * @see #unscheduleDrawable
9531 * @see #drawableStateChanged
9532 */
9533 protected boolean verifyDrawable(Drawable who) {
9534 return who == mBGDrawable;
9535 }
9536
9537 /**
9538 * This function is called whenever the state of the view changes in such
9539 * a way that it impacts the state of drawables being shown.
9540 *
9541 * <p>Be sure to call through to the superclass when overriding this
9542 * function.
9543 *
9544 * @see Drawable#setState
9545 */
9546 protected void drawableStateChanged() {
9547 Drawable d = mBGDrawable;
9548 if (d != null && d.isStateful()) {
9549 d.setState(getDrawableState());
9550 }
9551 }
9552
9553 /**
9554 * Call this to force a view to update its drawable state. This will cause
9555 * drawableStateChanged to be called on this view. Views that are interested
9556 * in the new state should call getDrawableState.
9557 *
9558 * @see #drawableStateChanged
9559 * @see #getDrawableState
9560 */
9561 public void refreshDrawableState() {
9562 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9563 drawableStateChanged();
9564
9565 ViewParent parent = mParent;
9566 if (parent != null) {
9567 parent.childDrawableStateChanged(this);
9568 }
9569 }
9570
9571 /**
9572 * Return an array of resource IDs of the drawable states representing the
9573 * current state of the view.
9574 *
9575 * @return The current drawable state
9576 *
9577 * @see Drawable#setState
9578 * @see #drawableStateChanged
9579 * @see #onCreateDrawableState
9580 */
9581 public final int[] getDrawableState() {
9582 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9583 return mDrawableState;
9584 } else {
9585 mDrawableState = onCreateDrawableState(0);
9586 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9587 return mDrawableState;
9588 }
9589 }
9590
9591 /**
9592 * Generate the new {@link android.graphics.drawable.Drawable} state for
9593 * this view. This is called by the view
9594 * system when the cached Drawable state is determined to be invalid. To
9595 * retrieve the current state, you should use {@link #getDrawableState}.
9596 *
9597 * @param extraSpace if non-zero, this is the number of extra entries you
9598 * would like in the returned array in which you can place your own
9599 * states.
9600 *
9601 * @return Returns an array holding the current {@link Drawable} state of
9602 * the view.
9603 *
9604 * @see #mergeDrawableStates
9605 */
9606 protected int[] onCreateDrawableState(int extraSpace) {
9607 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9608 mParent instanceof View) {
9609 return ((View) mParent).onCreateDrawableState(extraSpace);
9610 }
9611
9612 int[] drawableState;
9613
9614 int privateFlags = mPrivateFlags;
9615
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009616 int viewStateIndex = 0;
9617 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9618 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9619 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009620 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009621 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9622 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009623 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9624 // This is set if HW acceleration is requested, even if the current
9625 // process doesn't allow it. This is just to allow app preview
9626 // windows to better match their app.
9627 viewStateIndex |= VIEW_STATE_ACCELERATED;
9628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009629
9630 drawableState = VIEW_STATE_SETS[viewStateIndex];
9631
9632 //noinspection ConstantIfStatement
9633 if (false) {
9634 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9635 Log.i("View", toString()
9636 + " pressed=" + ((privateFlags & PRESSED) != 0)
9637 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9638 + " fo=" + hasFocus()
9639 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009640 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 + ": " + Arrays.toString(drawableState));
9642 }
9643
9644 if (extraSpace == 0) {
9645 return drawableState;
9646 }
9647
9648 final int[] fullState;
9649 if (drawableState != null) {
9650 fullState = new int[drawableState.length + extraSpace];
9651 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9652 } else {
9653 fullState = new int[extraSpace];
9654 }
9655
9656 return fullState;
9657 }
9658
9659 /**
9660 * Merge your own state values in <var>additionalState</var> into the base
9661 * state values <var>baseState</var> that were returned by
9662 * {@link #onCreateDrawableState}.
9663 *
9664 * @param baseState The base state values returned by
9665 * {@link #onCreateDrawableState}, which will be modified to also hold your
9666 * own additional state values.
9667 *
9668 * @param additionalState The additional state values you would like
9669 * added to <var>baseState</var>; this array is not modified.
9670 *
9671 * @return As a convenience, the <var>baseState</var> array you originally
9672 * passed into the function is returned.
9673 *
9674 * @see #onCreateDrawableState
9675 */
9676 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9677 final int N = baseState.length;
9678 int i = N - 1;
9679 while (i >= 0 && baseState[i] == 0) {
9680 i--;
9681 }
9682 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9683 return baseState;
9684 }
9685
9686 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009687 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9688 * on all Drawable objects associated with this view.
9689 */
9690 public void jumpDrawablesToCurrentState() {
9691 if (mBGDrawable != null) {
9692 mBGDrawable.jumpToCurrentState();
9693 }
9694 }
9695
9696 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009697 * Sets the background color for this view.
9698 * @param color the color of the background
9699 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009700 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009701 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009702 if (mBGDrawable instanceof ColorDrawable) {
9703 ((ColorDrawable) mBGDrawable).setColor(color);
9704 } else {
9705 setBackgroundDrawable(new ColorDrawable(color));
9706 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 }
9708
9709 /**
9710 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009711 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009712 * @param resid The identifier of the resource.
9713 * @attr ref android.R.styleable#View_background
9714 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009715 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 public void setBackgroundResource(int resid) {
9717 if (resid != 0 && resid == mBackgroundResource) {
9718 return;
9719 }
9720
9721 Drawable d= null;
9722 if (resid != 0) {
9723 d = mResources.getDrawable(resid);
9724 }
9725 setBackgroundDrawable(d);
9726
9727 mBackgroundResource = resid;
9728 }
9729
9730 /**
9731 * Set the background to a given Drawable, or remove the background. If the
9732 * background has padding, this View's padding is set to the background's
9733 * padding. However, when a background is removed, this View's padding isn't
9734 * touched. If setting the padding is desired, please use
9735 * {@link #setPadding(int, int, int, int)}.
9736 *
9737 * @param d The Drawable to use as the background, or null to remove the
9738 * background
9739 */
9740 public void setBackgroundDrawable(Drawable d) {
9741 boolean requestLayout = false;
9742
9743 mBackgroundResource = 0;
9744
9745 /*
9746 * Regardless of whether we're setting a new background or not, we want
9747 * to clear the previous drawable.
9748 */
9749 if (mBGDrawable != null) {
9750 mBGDrawable.setCallback(null);
9751 unscheduleDrawable(mBGDrawable);
9752 }
9753
9754 if (d != null) {
9755 Rect padding = sThreadLocal.get();
9756 if (padding == null) {
9757 padding = new Rect();
9758 sThreadLocal.set(padding);
9759 }
9760 if (d.getPadding(padding)) {
9761 setPadding(padding.left, padding.top, padding.right, padding.bottom);
9762 }
9763
9764 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
9765 // if it has a different minimum size, we should layout again
9766 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
9767 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
9768 requestLayout = true;
9769 }
9770
9771 d.setCallback(this);
9772 if (d.isStateful()) {
9773 d.setState(getDrawableState());
9774 }
9775 d.setVisible(getVisibility() == VISIBLE, false);
9776 mBGDrawable = d;
9777
9778 if ((mPrivateFlags & SKIP_DRAW) != 0) {
9779 mPrivateFlags &= ~SKIP_DRAW;
9780 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
9781 requestLayout = true;
9782 }
9783 } else {
9784 /* Remove the background */
9785 mBGDrawable = null;
9786
9787 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
9788 /*
9789 * This view ONLY drew the background before and we're removing
9790 * the background, so now it won't draw anything
9791 * (hence we SKIP_DRAW)
9792 */
9793 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
9794 mPrivateFlags |= SKIP_DRAW;
9795 }
9796
9797 /*
9798 * When the background is set, we try to apply its padding to this
9799 * View. When the background is removed, we don't touch this View's
9800 * padding. This is noted in the Javadocs. Hence, we don't need to
9801 * requestLayout(), the invalidate() below is sufficient.
9802 */
9803
9804 // The old background's minimum size could have affected this
9805 // View's layout, so let's requestLayout
9806 requestLayout = true;
9807 }
9808
Romain Guy8f1344f52009-05-15 16:03:59 -07009809 computeOpaqueFlags();
9810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009811 if (requestLayout) {
9812 requestLayout();
9813 }
9814
9815 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009816 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009817 }
9818
9819 /**
9820 * Gets the background drawable
9821 * @return The drawable used as the background for this view, if any.
9822 */
9823 public Drawable getBackground() {
9824 return mBGDrawable;
9825 }
9826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009827 /**
9828 * Sets the padding. The view may add on the space required to display
9829 * the scrollbars, depending on the style and visibility of the scrollbars.
9830 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
9831 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
9832 * from the values set in this call.
9833 *
9834 * @attr ref android.R.styleable#View_padding
9835 * @attr ref android.R.styleable#View_paddingBottom
9836 * @attr ref android.R.styleable#View_paddingLeft
9837 * @attr ref android.R.styleable#View_paddingRight
9838 * @attr ref android.R.styleable#View_paddingTop
9839 * @param left the left padding in pixels
9840 * @param top the top padding in pixels
9841 * @param right the right padding in pixels
9842 * @param bottom the bottom padding in pixels
9843 */
9844 public void setPadding(int left, int top, int right, int bottom) {
9845 boolean changed = false;
9846
Adam Powell20232d02010-12-08 21:08:53 -08009847 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009848 mUserPaddingRight = right;
9849 mUserPaddingBottom = bottom;
9850
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009851 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07009852
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009853 // Common case is there are no scroll bars.
9854 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009855 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -08009856 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
9857 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009858 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -08009859 switch (mVerticalScrollbarPosition) {
9860 case SCROLLBAR_POSITION_DEFAULT:
9861 case SCROLLBAR_POSITION_RIGHT:
9862 right += offset;
9863 break;
9864 case SCROLLBAR_POSITION_LEFT:
9865 left += offset;
9866 break;
9867 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009868 }
Adam Powell20232d02010-12-08 21:08:53 -08009869 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009870 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
9871 ? 0 : getHorizontalScrollbarHeight();
9872 }
9873 }
Romain Guy8506ab42009-06-11 17:35:47 -07009874
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009875 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 changed = true;
9877 mPaddingLeft = left;
9878 }
9879 if (mPaddingTop != top) {
9880 changed = true;
9881 mPaddingTop = top;
9882 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009883 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009884 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009885 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009887 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009889 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009890 }
9891
9892 if (changed) {
9893 requestLayout();
9894 }
9895 }
9896
9897 /**
9898 * Returns the top padding of this view.
9899 *
9900 * @return the top padding in pixels
9901 */
9902 public int getPaddingTop() {
9903 return mPaddingTop;
9904 }
9905
9906 /**
9907 * Returns the bottom padding of this view. If there are inset and enabled
9908 * scrollbars, this value may include the space required to display the
9909 * scrollbars as well.
9910 *
9911 * @return the bottom padding in pixels
9912 */
9913 public int getPaddingBottom() {
9914 return mPaddingBottom;
9915 }
9916
9917 /**
9918 * Returns the left padding of this view. If there are inset and enabled
9919 * scrollbars, this value may include the space required to display the
9920 * scrollbars as well.
9921 *
9922 * @return the left padding in pixels
9923 */
9924 public int getPaddingLeft() {
9925 return mPaddingLeft;
9926 }
9927
9928 /**
9929 * Returns the right padding of this view. If there are inset and enabled
9930 * scrollbars, this value may include the space required to display the
9931 * scrollbars as well.
9932 *
9933 * @return the right padding in pixels
9934 */
9935 public int getPaddingRight() {
9936 return mPaddingRight;
9937 }
9938
9939 /**
9940 * Changes the selection state of this view. A view can be selected or not.
9941 * Note that selection is not the same as focus. Views are typically
9942 * selected in the context of an AdapterView like ListView or GridView;
9943 * the selected view is the view that is highlighted.
9944 *
9945 * @param selected true if the view must be selected, false otherwise
9946 */
9947 public void setSelected(boolean selected) {
9948 if (((mPrivateFlags & SELECTED) != 0) != selected) {
9949 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -07009950 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -08009951 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009952 refreshDrawableState();
9953 dispatchSetSelected(selected);
9954 }
9955 }
9956
9957 /**
9958 * Dispatch setSelected to all of this View's children.
9959 *
9960 * @see #setSelected(boolean)
9961 *
9962 * @param selected The new selected state
9963 */
9964 protected void dispatchSetSelected(boolean selected) {
9965 }
9966
9967 /**
9968 * Indicates the selection state of this view.
9969 *
9970 * @return true if the view is selected, false otherwise
9971 */
9972 @ViewDebug.ExportedProperty
9973 public boolean isSelected() {
9974 return (mPrivateFlags & SELECTED) != 0;
9975 }
9976
9977 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009978 * Changes the activated state of this view. A view can be activated or not.
9979 * Note that activation is not the same as selection. Selection is
9980 * a transient property, representing the view (hierarchy) the user is
9981 * currently interacting with. Activation is a longer-term state that the
9982 * user can move views in and out of. For example, in a list view with
9983 * single or multiple selection enabled, the views in the current selection
9984 * set are activated. (Um, yeah, we are deeply sorry about the terminology
9985 * here.) The activated state is propagated down to children of the view it
9986 * is set on.
9987 *
9988 * @param activated true if the view must be activated, false otherwise
9989 */
9990 public void setActivated(boolean activated) {
9991 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
9992 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -08009993 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009994 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07009995 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009996 }
9997 }
9998
9999 /**
10000 * Dispatch setActivated to all of this View's children.
10001 *
10002 * @see #setActivated(boolean)
10003 *
10004 * @param activated The new activated state
10005 */
10006 protected void dispatchSetActivated(boolean activated) {
10007 }
10008
10009 /**
10010 * Indicates the activation state of this view.
10011 *
10012 * @return true if the view is activated, false otherwise
10013 */
10014 @ViewDebug.ExportedProperty
10015 public boolean isActivated() {
10016 return (mPrivateFlags & ACTIVATED) != 0;
10017 }
10018
10019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010020 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10021 * observer can be used to get notifications when global events, like
10022 * layout, happen.
10023 *
10024 * The returned ViewTreeObserver observer is not guaranteed to remain
10025 * valid for the lifetime of this View. If the caller of this method keeps
10026 * a long-lived reference to ViewTreeObserver, it should always check for
10027 * the return value of {@link ViewTreeObserver#isAlive()}.
10028 *
10029 * @return The ViewTreeObserver for this view's hierarchy.
10030 */
10031 public ViewTreeObserver getViewTreeObserver() {
10032 if (mAttachInfo != null) {
10033 return mAttachInfo.mTreeObserver;
10034 }
10035 if (mFloatingTreeObserver == null) {
10036 mFloatingTreeObserver = new ViewTreeObserver();
10037 }
10038 return mFloatingTreeObserver;
10039 }
10040
10041 /**
10042 * <p>Finds the topmost view in the current view hierarchy.</p>
10043 *
10044 * @return the topmost view containing this view
10045 */
10046 public View getRootView() {
10047 if (mAttachInfo != null) {
10048 final View v = mAttachInfo.mRootView;
10049 if (v != null) {
10050 return v;
10051 }
10052 }
Romain Guy8506ab42009-06-11 17:35:47 -070010053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010054 View parent = this;
10055
10056 while (parent.mParent != null && parent.mParent instanceof View) {
10057 parent = (View) parent.mParent;
10058 }
10059
10060 return parent;
10061 }
10062
10063 /**
10064 * <p>Computes the coordinates of this view on the screen. The argument
10065 * must be an array of two integers. After the method returns, the array
10066 * contains the x and y location in that order.</p>
10067 *
10068 * @param location an array of two integers in which to hold the coordinates
10069 */
10070 public void getLocationOnScreen(int[] location) {
10071 getLocationInWindow(location);
10072
10073 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010074 if (info != null) {
10075 location[0] += info.mWindowLeft;
10076 location[1] += info.mWindowTop;
10077 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010078 }
10079
10080 /**
10081 * <p>Computes the coordinates of this view in its window. The argument
10082 * must be an array of two integers. After the method returns, the array
10083 * contains the x and y location in that order.</p>
10084 *
10085 * @param location an array of two integers in which to hold the coordinates
10086 */
10087 public void getLocationInWindow(int[] location) {
10088 if (location == null || location.length < 2) {
10089 throw new IllegalArgumentException("location must be an array of "
10090 + "two integers");
10091 }
10092
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010093 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10094 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095
10096 ViewParent viewParent = mParent;
10097 while (viewParent instanceof View) {
10098 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010099 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10100 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010101 viewParent = view.mParent;
10102 }
Romain Guy8506ab42009-06-11 17:35:47 -070010103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 if (viewParent instanceof ViewRoot) {
10105 // *cough*
10106 final ViewRoot vr = (ViewRoot)viewParent;
10107 location[1] -= vr.mCurScrollY;
10108 }
10109 }
10110
10111 /**
10112 * {@hide}
10113 * @param id the id of the view to be found
10114 * @return the view of the specified id, null if cannot be found
10115 */
10116 protected View findViewTraversal(int id) {
10117 if (id == mID) {
10118 return this;
10119 }
10120 return null;
10121 }
10122
10123 /**
10124 * {@hide}
10125 * @param tag the tag of the view to be found
10126 * @return the view of specified tag, null if cannot be found
10127 */
10128 protected View findViewWithTagTraversal(Object tag) {
10129 if (tag != null && tag.equals(mTag)) {
10130 return this;
10131 }
10132 return null;
10133 }
10134
10135 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010136 * {@hide}
10137 * @param predicate The predicate to evaluate.
10138 * @return The first view that matches the predicate or null.
10139 */
10140 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10141 if (predicate.apply(this)) {
10142 return this;
10143 }
10144 return null;
10145 }
10146
10147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010148 * Look for a child view with the given id. If this view has the given
10149 * id, return this view.
10150 *
10151 * @param id The id to search for.
10152 * @return The view that has the given id in the hierarchy or null
10153 */
10154 public final View findViewById(int id) {
10155 if (id < 0) {
10156 return null;
10157 }
10158 return findViewTraversal(id);
10159 }
10160
10161 /**
10162 * Look for a child view with the given tag. If this view has the given
10163 * tag, return this view.
10164 *
10165 * @param tag The tag to search for, using "tag.equals(getTag())".
10166 * @return The View that has the given tag in the hierarchy or null
10167 */
10168 public final View findViewWithTag(Object tag) {
10169 if (tag == null) {
10170 return null;
10171 }
10172 return findViewWithTagTraversal(tag);
10173 }
10174
10175 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010176 * {@hide}
10177 * Look for a child view that matches the specified predicate.
10178 * If this view matches the predicate, return this view.
10179 *
10180 * @param predicate The predicate to evaluate.
10181 * @return The first view that matches the predicate or null.
10182 */
10183 public final View findViewByPredicate(Predicate<View> predicate) {
10184 return findViewByPredicateTraversal(predicate);
10185 }
10186
10187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 * Sets the identifier for this view. The identifier does not have to be
10189 * unique in this view's hierarchy. The identifier should be a positive
10190 * number.
10191 *
10192 * @see #NO_ID
10193 * @see #getId
10194 * @see #findViewById
10195 *
10196 * @param id a number used to identify the view
10197 *
10198 * @attr ref android.R.styleable#View_id
10199 */
10200 public void setId(int id) {
10201 mID = id;
10202 }
10203
10204 /**
10205 * {@hide}
10206 *
10207 * @param isRoot true if the view belongs to the root namespace, false
10208 * otherwise
10209 */
10210 public void setIsRootNamespace(boolean isRoot) {
10211 if (isRoot) {
10212 mPrivateFlags |= IS_ROOT_NAMESPACE;
10213 } else {
10214 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10215 }
10216 }
10217
10218 /**
10219 * {@hide}
10220 *
10221 * @return true if the view belongs to the root namespace, false otherwise
10222 */
10223 public boolean isRootNamespace() {
10224 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10225 }
10226
10227 /**
10228 * Returns this view's identifier.
10229 *
10230 * @return a positive integer used to identify the view or {@link #NO_ID}
10231 * if the view has no ID
10232 *
10233 * @see #setId
10234 * @see #findViewById
10235 * @attr ref android.R.styleable#View_id
10236 */
10237 @ViewDebug.CapturedViewProperty
10238 public int getId() {
10239 return mID;
10240 }
10241
10242 /**
10243 * Returns this view's tag.
10244 *
10245 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010246 *
10247 * @see #setTag(Object)
10248 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010249 */
10250 @ViewDebug.ExportedProperty
10251 public Object getTag() {
10252 return mTag;
10253 }
10254
10255 /**
10256 * Sets the tag associated with this view. A tag can be used to mark
10257 * a view in its hierarchy and does not have to be unique within the
10258 * hierarchy. Tags can also be used to store data within a view without
10259 * resorting to another data structure.
10260 *
10261 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010262 *
10263 * @see #getTag()
10264 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010265 */
10266 public void setTag(final Object tag) {
10267 mTag = tag;
10268 }
10269
10270 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010271 * Returns the tag associated with this view and the specified key.
10272 *
10273 * @param key The key identifying the tag
10274 *
10275 * @return the Object stored in this view as a tag
10276 *
10277 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010278 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010279 */
10280 public Object getTag(int key) {
10281 SparseArray<Object> tags = null;
10282 synchronized (sTagsLock) {
10283 if (sTags != null) {
10284 tags = sTags.get(this);
10285 }
10286 }
10287
10288 if (tags != null) return tags.get(key);
10289 return null;
10290 }
10291
10292 /**
10293 * Sets a tag associated with this view and a key. A tag can be used
10294 * to mark a view in its hierarchy and does not have to be unique within
10295 * the hierarchy. Tags can also be used to store data within a view
10296 * without resorting to another data structure.
10297 *
10298 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010299 * application to ensure it is unique (see the <a
10300 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10301 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010302 * the Android framework or not associated with any package will cause
10303 * an {@link IllegalArgumentException} to be thrown.
10304 *
10305 * @param key The key identifying the tag
10306 * @param tag An Object to tag the view with
10307 *
10308 * @throws IllegalArgumentException If they specified key is not valid
10309 *
10310 * @see #setTag(Object)
10311 * @see #getTag(int)
10312 */
10313 public void setTag(int key, final Object tag) {
10314 // If the package id is 0x00 or 0x01, it's either an undefined package
10315 // or a framework id
10316 if ((key >>> 24) < 2) {
10317 throw new IllegalArgumentException("The key must be an application-specific "
10318 + "resource id.");
10319 }
10320
10321 setTagInternal(this, key, tag);
10322 }
10323
10324 /**
10325 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10326 * framework id.
10327 *
10328 * @hide
10329 */
10330 public void setTagInternal(int key, Object tag) {
10331 if ((key >>> 24) != 0x1) {
10332 throw new IllegalArgumentException("The key must be a framework-specific "
10333 + "resource id.");
10334 }
10335
Romain Guy8506ab42009-06-11 17:35:47 -070010336 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010337 }
10338
10339 private static void setTagInternal(View view, int key, Object tag) {
10340 SparseArray<Object> tags = null;
10341 synchronized (sTagsLock) {
10342 if (sTags == null) {
10343 sTags = new WeakHashMap<View, SparseArray<Object>>();
10344 } else {
10345 tags = sTags.get(view);
10346 }
10347 }
10348
10349 if (tags == null) {
10350 tags = new SparseArray<Object>(2);
10351 synchronized (sTagsLock) {
10352 sTags.put(view, tags);
10353 }
10354 }
10355
10356 tags.put(key, tag);
10357 }
10358
10359 /**
Romain Guy13922e02009-05-12 17:56:14 -070010360 * @param consistency The type of consistency. See ViewDebug for more information.
10361 *
10362 * @hide
10363 */
10364 protected boolean dispatchConsistencyCheck(int consistency) {
10365 return onConsistencyCheck(consistency);
10366 }
10367
10368 /**
10369 * Method that subclasses should implement to check their consistency. The type of
10370 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010371 *
Romain Guy13922e02009-05-12 17:56:14 -070010372 * @param consistency The type of consistency. See ViewDebug for more information.
10373 *
10374 * @throws IllegalStateException if the view is in an inconsistent state.
10375 *
10376 * @hide
10377 */
10378 protected boolean onConsistencyCheck(int consistency) {
10379 boolean result = true;
10380
10381 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10382 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10383
10384 if (checkLayout) {
10385 if (getParent() == null) {
10386 result = false;
10387 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10388 "View " + this + " does not have a parent.");
10389 }
10390
10391 if (mAttachInfo == null) {
10392 result = false;
10393 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10394 "View " + this + " is not attached to a window.");
10395 }
10396 }
10397
10398 if (checkDrawing) {
10399 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10400 // from their draw() method
10401
10402 if ((mPrivateFlags & DRAWN) != DRAWN &&
10403 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10404 result = false;
10405 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10406 "View " + this + " was invalidated but its drawing cache is valid.");
10407 }
10408 }
10409
10410 return result;
10411 }
10412
10413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010414 * Prints information about this view in the log output, with the tag
10415 * {@link #VIEW_LOG_TAG}.
10416 *
10417 * @hide
10418 */
10419 public void debug() {
10420 debug(0);
10421 }
10422
10423 /**
10424 * Prints information about this view in the log output, with the tag
10425 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10426 * indentation defined by the <code>depth</code>.
10427 *
10428 * @param depth the indentation level
10429 *
10430 * @hide
10431 */
10432 protected void debug(int depth) {
10433 String output = debugIndent(depth - 1);
10434
10435 output += "+ " + this;
10436 int id = getId();
10437 if (id != -1) {
10438 output += " (id=" + id + ")";
10439 }
10440 Object tag = getTag();
10441 if (tag != null) {
10442 output += " (tag=" + tag + ")";
10443 }
10444 Log.d(VIEW_LOG_TAG, output);
10445
10446 if ((mPrivateFlags & FOCUSED) != 0) {
10447 output = debugIndent(depth) + " FOCUSED";
10448 Log.d(VIEW_LOG_TAG, output);
10449 }
10450
10451 output = debugIndent(depth);
10452 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10453 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10454 + "} ";
10455 Log.d(VIEW_LOG_TAG, output);
10456
10457 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10458 || mPaddingBottom != 0) {
10459 output = debugIndent(depth);
10460 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10461 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10462 Log.d(VIEW_LOG_TAG, output);
10463 }
10464
10465 output = debugIndent(depth);
10466 output += "mMeasureWidth=" + mMeasuredWidth +
10467 " mMeasureHeight=" + mMeasuredHeight;
10468 Log.d(VIEW_LOG_TAG, output);
10469
10470 output = debugIndent(depth);
10471 if (mLayoutParams == null) {
10472 output += "BAD! no layout params";
10473 } else {
10474 output = mLayoutParams.debug(output);
10475 }
10476 Log.d(VIEW_LOG_TAG, output);
10477
10478 output = debugIndent(depth);
10479 output += "flags={";
10480 output += View.printFlags(mViewFlags);
10481 output += "}";
10482 Log.d(VIEW_LOG_TAG, output);
10483
10484 output = debugIndent(depth);
10485 output += "privateFlags={";
10486 output += View.printPrivateFlags(mPrivateFlags);
10487 output += "}";
10488 Log.d(VIEW_LOG_TAG, output);
10489 }
10490
10491 /**
10492 * Creates an string of whitespaces used for indentation.
10493 *
10494 * @param depth the indentation level
10495 * @return a String containing (depth * 2 + 3) * 2 white spaces
10496 *
10497 * @hide
10498 */
10499 protected static String debugIndent(int depth) {
10500 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10501 for (int i = 0; i < (depth * 2) + 3; i++) {
10502 spaces.append(' ').append(' ');
10503 }
10504 return spaces.toString();
10505 }
10506
10507 /**
10508 * <p>Return the offset of the widget's text baseline from the widget's top
10509 * boundary. If this widget does not support baseline alignment, this
10510 * method returns -1. </p>
10511 *
10512 * @return the offset of the baseline within the widget's bounds or -1
10513 * if baseline alignment is not supported
10514 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010515 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010516 public int getBaseline() {
10517 return -1;
10518 }
10519
10520 /**
10521 * Call this when something has changed which has invalidated the
10522 * layout of this view. This will schedule a layout pass of the view
10523 * tree.
10524 */
10525 public void requestLayout() {
10526 if (ViewDebug.TRACE_HIERARCHY) {
10527 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10528 }
10529
10530 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010531 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010532
10533 if (mParent != null && !mParent.isLayoutRequested()) {
10534 mParent.requestLayout();
10535 }
10536 }
10537
10538 /**
10539 * Forces this view to be laid out during the next layout pass.
10540 * This method does not call requestLayout() or forceLayout()
10541 * on the parent.
10542 */
10543 public void forceLayout() {
10544 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010545 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 }
10547
10548 /**
10549 * <p>
10550 * This is called to find out how big a view should be. The parent
10551 * supplies constraint information in the width and height parameters.
10552 * </p>
10553 *
10554 * <p>
10555 * The actual mesurement work of a view is performed in
10556 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10557 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10558 * </p>
10559 *
10560 *
10561 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10562 * parent
10563 * @param heightMeasureSpec Vertical space requirements as imposed by the
10564 * parent
10565 *
10566 * @see #onMeasure(int, int)
10567 */
10568 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10569 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10570 widthMeasureSpec != mOldWidthMeasureSpec ||
10571 heightMeasureSpec != mOldHeightMeasureSpec) {
10572
10573 // first clears the measured dimension flag
10574 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10575
10576 if (ViewDebug.TRACE_HIERARCHY) {
10577 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10578 }
10579
10580 // measure ourselves, this should set the measured dimension flag back
10581 onMeasure(widthMeasureSpec, heightMeasureSpec);
10582
10583 // flag not set, setMeasuredDimension() was not invoked, we raise
10584 // an exception to warn the developer
10585 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10586 throw new IllegalStateException("onMeasure() did not set the"
10587 + " measured dimension by calling"
10588 + " setMeasuredDimension()");
10589 }
10590
10591 mPrivateFlags |= LAYOUT_REQUIRED;
10592 }
10593
10594 mOldWidthMeasureSpec = widthMeasureSpec;
10595 mOldHeightMeasureSpec = heightMeasureSpec;
10596 }
10597
10598 /**
10599 * <p>
10600 * Measure the view and its content to determine the measured width and the
10601 * measured height. This method is invoked by {@link #measure(int, int)} and
10602 * should be overriden by subclasses to provide accurate and efficient
10603 * measurement of their contents.
10604 * </p>
10605 *
10606 * <p>
10607 * <strong>CONTRACT:</strong> When overriding this method, you
10608 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10609 * measured width and height of this view. Failure to do so will trigger an
10610 * <code>IllegalStateException</code>, thrown by
10611 * {@link #measure(int, int)}. Calling the superclass'
10612 * {@link #onMeasure(int, int)} is a valid use.
10613 * </p>
10614 *
10615 * <p>
10616 * The base class implementation of measure defaults to the background size,
10617 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10618 * override {@link #onMeasure(int, int)} to provide better measurements of
10619 * their content.
10620 * </p>
10621 *
10622 * <p>
10623 * If this method is overridden, it is the subclass's responsibility to make
10624 * sure the measured height and width are at least the view's minimum height
10625 * and width ({@link #getSuggestedMinimumHeight()} and
10626 * {@link #getSuggestedMinimumWidth()}).
10627 * </p>
10628 *
10629 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10630 * The requirements are encoded with
10631 * {@link android.view.View.MeasureSpec}.
10632 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10633 * The requirements are encoded with
10634 * {@link android.view.View.MeasureSpec}.
10635 *
10636 * @see #getMeasuredWidth()
10637 * @see #getMeasuredHeight()
10638 * @see #setMeasuredDimension(int, int)
10639 * @see #getSuggestedMinimumHeight()
10640 * @see #getSuggestedMinimumWidth()
10641 * @see android.view.View.MeasureSpec#getMode(int)
10642 * @see android.view.View.MeasureSpec#getSize(int)
10643 */
10644 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10645 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10646 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10647 }
10648
10649 /**
10650 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10651 * measured width and measured height. Failing to do so will trigger an
10652 * exception at measurement time.</p>
10653 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010654 * @param measuredWidth The measured width of this view. May be a complex
10655 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10656 * {@link #MEASURED_STATE_TOO_SMALL}.
10657 * @param measuredHeight The measured height of this view. May be a complex
10658 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10659 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010660 */
10661 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10662 mMeasuredWidth = measuredWidth;
10663 mMeasuredHeight = measuredHeight;
10664
10665 mPrivateFlags |= MEASURED_DIMENSION_SET;
10666 }
10667
10668 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010669 * Merge two states as returned by {@link #getMeasuredState()}.
10670 * @param curState The current state as returned from a view or the result
10671 * of combining multiple views.
10672 * @param newState The new view state to combine.
10673 * @return Returns a new integer reflecting the combination of the two
10674 * states.
10675 */
10676 public static int combineMeasuredStates(int curState, int newState) {
10677 return curState | newState;
10678 }
10679
10680 /**
10681 * Version of {@link #resolveSizeAndState(int, int, int)}
10682 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10683 */
10684 public static int resolveSize(int size, int measureSpec) {
10685 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10686 }
10687
10688 /**
10689 * Utility to reconcile a desired size and state, with constraints imposed
10690 * by a MeasureSpec. Will take the desired size, unless a different size
10691 * is imposed by the constraints. The returned value is a compound integer,
10692 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10693 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10694 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 *
10696 * @param size How big the view wants to be
10697 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010698 * @return Size information bit mask as defined by
10699 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010700 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010701 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 int result = size;
10703 int specMode = MeasureSpec.getMode(measureSpec);
10704 int specSize = MeasureSpec.getSize(measureSpec);
10705 switch (specMode) {
10706 case MeasureSpec.UNSPECIFIED:
10707 result = size;
10708 break;
10709 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010710 if (specSize < size) {
10711 result = specSize | MEASURED_STATE_TOO_SMALL;
10712 } else {
10713 result = size;
10714 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010715 break;
10716 case MeasureSpec.EXACTLY:
10717 result = specSize;
10718 break;
10719 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010720 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010721 }
10722
10723 /**
10724 * Utility to return a default size. Uses the supplied size if the
10725 * MeasureSpec imposed no contraints. Will get larger if allowed
10726 * by the MeasureSpec.
10727 *
10728 * @param size Default size for this view
10729 * @param measureSpec Constraints imposed by the parent
10730 * @return The size this view should be.
10731 */
10732 public static int getDefaultSize(int size, int measureSpec) {
10733 int result = size;
10734 int specMode = MeasureSpec.getMode(measureSpec);
10735 int specSize = MeasureSpec.getSize(measureSpec);
10736
10737 switch (specMode) {
10738 case MeasureSpec.UNSPECIFIED:
10739 result = size;
10740 break;
10741 case MeasureSpec.AT_MOST:
10742 case MeasureSpec.EXACTLY:
10743 result = specSize;
10744 break;
10745 }
10746 return result;
10747 }
10748
10749 /**
10750 * Returns the suggested minimum height that the view should use. This
10751 * returns the maximum of the view's minimum height
10752 * and the background's minimum height
10753 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
10754 * <p>
10755 * When being used in {@link #onMeasure(int, int)}, the caller should still
10756 * ensure the returned height is within the requirements of the parent.
10757 *
10758 * @return The suggested minimum height of the view.
10759 */
10760 protected int getSuggestedMinimumHeight() {
10761 int suggestedMinHeight = mMinHeight;
10762
10763 if (mBGDrawable != null) {
10764 final int bgMinHeight = mBGDrawable.getMinimumHeight();
10765 if (suggestedMinHeight < bgMinHeight) {
10766 suggestedMinHeight = bgMinHeight;
10767 }
10768 }
10769
10770 return suggestedMinHeight;
10771 }
10772
10773 /**
10774 * Returns the suggested minimum width that the view should use. This
10775 * returns the maximum of the view's minimum width)
10776 * and the background's minimum width
10777 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
10778 * <p>
10779 * When being used in {@link #onMeasure(int, int)}, the caller should still
10780 * ensure the returned width is within the requirements of the parent.
10781 *
10782 * @return The suggested minimum width of the view.
10783 */
10784 protected int getSuggestedMinimumWidth() {
10785 int suggestedMinWidth = mMinWidth;
10786
10787 if (mBGDrawable != null) {
10788 final int bgMinWidth = mBGDrawable.getMinimumWidth();
10789 if (suggestedMinWidth < bgMinWidth) {
10790 suggestedMinWidth = bgMinWidth;
10791 }
10792 }
10793
10794 return suggestedMinWidth;
10795 }
10796
10797 /**
10798 * Sets the minimum height of the view. It is not guaranteed the view will
10799 * be able to achieve this minimum height (for example, if its parent layout
10800 * constrains it with less available height).
10801 *
10802 * @param minHeight The minimum height the view will try to be.
10803 */
10804 public void setMinimumHeight(int minHeight) {
10805 mMinHeight = minHeight;
10806 }
10807
10808 /**
10809 * Sets the minimum width of the view. It is not guaranteed the view will
10810 * be able to achieve this minimum width (for example, if its parent layout
10811 * constrains it with less available width).
10812 *
10813 * @param minWidth The minimum width the view will try to be.
10814 */
10815 public void setMinimumWidth(int minWidth) {
10816 mMinWidth = minWidth;
10817 }
10818
10819 /**
10820 * Get the animation currently associated with this view.
10821 *
10822 * @return The animation that is currently playing or
10823 * scheduled to play for this view.
10824 */
10825 public Animation getAnimation() {
10826 return mCurrentAnimation;
10827 }
10828
10829 /**
10830 * Start the specified animation now.
10831 *
10832 * @param animation the animation to start now
10833 */
10834 public void startAnimation(Animation animation) {
10835 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
10836 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010837 invalidateParentCaches();
10838 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 }
10840
10841 /**
10842 * Cancels any animations for this view.
10843 */
10844 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080010845 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080010846 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080010847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010848 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010849 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010850 }
10851
10852 /**
10853 * Sets the next animation to play for this view.
10854 * If you want the animation to play immediately, use
10855 * startAnimation. This method provides allows fine-grained
10856 * control over the start time and invalidation, but you
10857 * must make sure that 1) the animation has a start time set, and
10858 * 2) the view will be invalidated when the animation is supposed to
10859 * start.
10860 *
10861 * @param animation The next animation, or null.
10862 */
10863 public void setAnimation(Animation animation) {
10864 mCurrentAnimation = animation;
10865 if (animation != null) {
10866 animation.reset();
10867 }
10868 }
10869
10870 /**
10871 * Invoked by a parent ViewGroup to notify the start of the animation
10872 * currently associated with this view. If you override this method,
10873 * always call super.onAnimationStart();
10874 *
10875 * @see #setAnimation(android.view.animation.Animation)
10876 * @see #getAnimation()
10877 */
10878 protected void onAnimationStart() {
10879 mPrivateFlags |= ANIMATION_STARTED;
10880 }
10881
10882 /**
10883 * Invoked by a parent ViewGroup to notify the end of the animation
10884 * currently associated with this view. If you override this method,
10885 * always call super.onAnimationEnd();
10886 *
10887 * @see #setAnimation(android.view.animation.Animation)
10888 * @see #getAnimation()
10889 */
10890 protected void onAnimationEnd() {
10891 mPrivateFlags &= ~ANIMATION_STARTED;
10892 }
10893
10894 /**
10895 * Invoked if there is a Transform that involves alpha. Subclass that can
10896 * draw themselves with the specified alpha should return true, and then
10897 * respect that alpha when their onDraw() is called. If this returns false
10898 * then the view may be redirected to draw into an offscreen buffer to
10899 * fulfill the request, which will look fine, but may be slower than if the
10900 * subclass handles it internally. The default implementation returns false.
10901 *
10902 * @param alpha The alpha (0..255) to apply to the view's drawing
10903 * @return true if the view can draw with the specified alpha.
10904 */
10905 protected boolean onSetAlpha(int alpha) {
10906 return false;
10907 }
10908
10909 /**
10910 * This is used by the RootView to perform an optimization when
10911 * the view hierarchy contains one or several SurfaceView.
10912 * SurfaceView is always considered transparent, but its children are not,
10913 * therefore all View objects remove themselves from the global transparent
10914 * region (passed as a parameter to this function).
10915 *
10916 * @param region The transparent region for this ViewRoot (window).
10917 *
10918 * @return Returns true if the effective visibility of the view at this
10919 * point is opaque, regardless of the transparent region; returns false
10920 * if it is possible for underlying windows to be seen behind the view.
10921 *
10922 * {@hide}
10923 */
10924 public boolean gatherTransparentRegion(Region region) {
10925 final AttachInfo attachInfo = mAttachInfo;
10926 if (region != null && attachInfo != null) {
10927 final int pflags = mPrivateFlags;
10928 if ((pflags & SKIP_DRAW) == 0) {
10929 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
10930 // remove it from the transparent region.
10931 final int[] location = attachInfo.mTransparentLocation;
10932 getLocationInWindow(location);
10933 region.op(location[0], location[1], location[0] + mRight - mLeft,
10934 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
10935 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
10936 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
10937 // exists, so we remove the background drawable's non-transparent
10938 // parts from this transparent region.
10939 applyDrawableToTransparentRegion(mBGDrawable, region);
10940 }
10941 }
10942 return true;
10943 }
10944
10945 /**
10946 * Play a sound effect for this view.
10947 *
10948 * <p>The framework will play sound effects for some built in actions, such as
10949 * clicking, but you may wish to play these effects in your widget,
10950 * for instance, for internal navigation.
10951 *
10952 * <p>The sound effect will only be played if sound effects are enabled by the user, and
10953 * {@link #isSoundEffectsEnabled()} is true.
10954 *
10955 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
10956 */
10957 public void playSoundEffect(int soundConstant) {
10958 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
10959 return;
10960 }
10961 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
10962 }
10963
10964 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010965 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070010966 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010967 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 *
10969 * <p>The framework will provide haptic feedback for some built in actions,
10970 * such as long presses, but you may wish to provide feedback for your
10971 * own widget.
10972 *
10973 * <p>The feedback will only be performed if
10974 * {@link #isHapticFeedbackEnabled()} is true.
10975 *
10976 * @param feedbackConstant One of the constants defined in
10977 * {@link HapticFeedbackConstants}
10978 */
10979 public boolean performHapticFeedback(int feedbackConstant) {
10980 return performHapticFeedback(feedbackConstant, 0);
10981 }
10982
10983 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010984 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070010985 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010986 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987 *
10988 * @param feedbackConstant One of the constants defined in
10989 * {@link HapticFeedbackConstants}
10990 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
10991 */
10992 public boolean performHapticFeedback(int feedbackConstant, int flags) {
10993 if (mAttachInfo == null) {
10994 return false;
10995 }
Romain Guyf607bdc2010-09-10 19:20:06 -070010996 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070010997 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010998 && !isHapticFeedbackEnabled()) {
10999 return false;
11000 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011001 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11002 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011003 }
11004
11005 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011006 * Request that the visibility of the status bar be changed.
11007 */
11008 public void setSystemUiVisibility(int visibility) {
11009 if (visibility != mSystemUiVisibility) {
11010 mSystemUiVisibility = visibility;
11011 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11012 mParent.recomputeViewAttributes(this);
11013 }
11014 }
11015 }
11016
11017 /**
11018 * Returns the status bar visibility that this view has requested.
11019 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011020 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011021 return mSystemUiVisibility;
11022 }
11023
11024 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11025 mOnSystemUiVisibilityChangeListener = l;
11026 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11027 mParent.recomputeViewAttributes(this);
11028 }
11029 }
11030
11031 /**
11032 */
11033 public void dispatchSystemUiVisibilityChanged(int visibility) {
11034 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011035 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011036 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011037 }
11038 }
11039
11040 /**
Joe Malin32736f02011-01-19 16:14:20 -080011041 * Creates an image that the system displays during the drag and drop
11042 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11043 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11044 * appearance as the given View. The default also positions the center of the drag shadow
11045 * directly under the touch point. If no View is provided (the constructor with no parameters
11046 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11047 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11048 * default is an invisible drag shadow.
11049 * <p>
11050 * You are not required to use the View you provide to the constructor as the basis of the
11051 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11052 * anything you want as the drag shadow.
11053 * </p>
11054 * <p>
11055 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11056 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11057 * size and position of the drag shadow. It uses this data to construct a
11058 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11059 * so that your application can draw the shadow image in the Canvas.
11060 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011061 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011062 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011063 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011064
11065 /**
Joe Malin32736f02011-01-19 16:14:20 -080011066 * Constructs a shadow image builder based on a View. By default, the resulting drag
11067 * shadow will have the same appearance and dimensions as the View, with the touch point
11068 * over the center of the View.
11069 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011070 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011071 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011072 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011073 }
11074
Christopher Tate17ed60c2011-01-18 12:50:26 -080011075 /**
11076 * Construct a shadow builder object with no associated View. This
11077 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11078 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11079 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011080 * reference to any View object. If they are not overridden, then the result is an
11081 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011082 */
11083 public DragShadowBuilder() {
11084 mView = new WeakReference<View>(null);
11085 }
11086
11087 /**
11088 * Returns the View object that had been passed to the
11089 * {@link #View.DragShadowBuilder(View)}
11090 * constructor. If that View parameter was {@code null} or if the
11091 * {@link #View.DragShadowBuilder()}
11092 * constructor was used to instantiate the builder object, this method will return
11093 * null.
11094 *
11095 * @return The View object associate with this builder object.
11096 */
Chris Tate6b391282010-10-14 15:48:59 -070011097 final public View getView() {
11098 return mView.get();
11099 }
11100
Christopher Tate2c095f32010-10-04 14:13:40 -070011101 /**
Joe Malin32736f02011-01-19 16:14:20 -080011102 * Provides the metrics for the shadow image. These include the dimensions of
11103 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011104 * be centered under the touch location while dragging.
11105 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011106 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011107 * same as the dimensions of the View itself and centers the shadow under
11108 * the touch point.
11109 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011110 *
Joe Malin32736f02011-01-19 16:14:20 -080011111 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11112 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11113 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11114 * image.
11115 *
11116 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11117 * shadow image that should be underneath the touch point during the drag and drop
11118 * operation. Your application must set {@link android.graphics.Point#x} to the
11119 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011120 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011121 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011122 final View view = mView.get();
11123 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011124 shadowSize.set(view.getWidth(), view.getHeight());
11125 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011126 } else {
11127 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11128 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011129 }
11130
11131 /**
Joe Malin32736f02011-01-19 16:14:20 -080011132 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11133 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011134 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011135 *
Joe Malin32736f02011-01-19 16:14:20 -080011136 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011137 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011138 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011139 final View view = mView.get();
11140 if (view != null) {
11141 view.draw(canvas);
11142 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011143 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011144 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011145 }
11146 }
11147
11148 /**
Joe Malin32736f02011-01-19 16:14:20 -080011149 * Starts a drag and drop operation. When your application calls this method, it passes a
11150 * {@link android.view.View.DragShadowBuilder} object to the system. The
11151 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11152 * to get metrics for the drag shadow, and then calls the object's
11153 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11154 * <p>
11155 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11156 * drag events to all the View objects in your application that are currently visible. It does
11157 * this either by calling the View object's drag listener (an implementation of
11158 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11159 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11160 * Both are passed a {@link android.view.DragEvent} object that has a
11161 * {@link android.view.DragEvent#getAction()} value of
11162 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11163 * </p>
11164 * <p>
11165 * Your application can invoke startDrag() on any attached View object. The View object does not
11166 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11167 * be related to the View the user selected for dragging.
11168 * </p>
11169 * @param data A {@link android.content.ClipData} object pointing to the data to be
11170 * transferred by the drag and drop operation.
11171 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11172 * drag shadow.
11173 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11174 * drop operation. This Object is put into every DragEvent object sent by the system during the
11175 * current drag.
11176 * <p>
11177 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11178 * to the target Views. For example, it can contain flags that differentiate between a
11179 * a copy operation and a move operation.
11180 * </p>
11181 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11182 * so the parameter should be set to 0.
11183 * @return {@code true} if the method completes successfully, or
11184 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11185 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011186 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011187 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011188 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011189 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011190 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011191 }
11192 boolean okay = false;
11193
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011194 Point shadowSize = new Point();
11195 Point shadowTouchPoint = new Point();
11196 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011197
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011198 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11199 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11200 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011201 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011202
Chris Tatea32dcf72010-10-14 12:13:50 -070011203 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011204 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11205 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011206 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011207 Surface surface = new Surface();
11208 try {
11209 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011210 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011211 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011212 + " surface=" + surface);
11213 if (token != null) {
11214 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011215 try {
Chris Tate6b391282010-10-14 15:48:59 -070011216 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011217 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011218 } finally {
11219 surface.unlockCanvasAndPost(canvas);
11220 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011221
Christopher Tate407b4e92010-11-30 17:14:08 -080011222 final ViewRoot root = getViewRoot();
11223
11224 // Cache the local state object for delivery with DragEvents
11225 root.setLocalDragState(myLocalState);
11226
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011227 // repurpose 'shadowSize' for the last touch point
11228 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011229
Christopher Tatea53146c2010-09-07 11:57:52 -070011230 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011231 shadowSize.x, shadowSize.y,
11232 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011233 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011234 }
11235 } catch (Exception e) {
11236 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11237 surface.destroy();
11238 }
11239
11240 return okay;
11241 }
11242
Christopher Tatea53146c2010-09-07 11:57:52 -070011243 /**
Joe Malin32736f02011-01-19 16:14:20 -080011244 * Handles drag events sent by the system following a call to
11245 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11246 *<p>
11247 * When the system calls this method, it passes a
11248 * {@link android.view.DragEvent} object. A call to
11249 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11250 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11251 * operation.
11252 * @param event The {@link android.view.DragEvent} sent by the system.
11253 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11254 * in DragEvent, indicating the type of drag event represented by this object.
11255 * @return {@code true} if the method was successful, otherwise {@code false}.
11256 * <p>
11257 * The method should return {@code true} in response to an action type of
11258 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11259 * operation.
11260 * </p>
11261 * <p>
11262 * The method should also return {@code true} in response to an action type of
11263 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11264 * {@code false} if it didn't.
11265 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011266 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011267 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011268 return false;
11269 }
11270
11271 /**
Joe Malin32736f02011-01-19 16:14:20 -080011272 * Detects if this View is enabled and has a drag event listener.
11273 * If both are true, then it calls the drag event listener with the
11274 * {@link android.view.DragEvent} it received. If the drag event listener returns
11275 * {@code true}, then dispatchDragEvent() returns {@code true}.
11276 * <p>
11277 * For all other cases, the method calls the
11278 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11279 * method and returns its result.
11280 * </p>
11281 * <p>
11282 * This ensures that a drag event is always consumed, even if the View does not have a drag
11283 * event listener. However, if the View has a listener and the listener returns true, then
11284 * onDragEvent() is not called.
11285 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011286 */
11287 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011288 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011289 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11290 && mOnDragListener.onDrag(this, event)) {
11291 return true;
11292 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011293 return onDragEvent(event);
11294 }
11295
11296 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011297 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11298 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011299 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011300 */
11301 public void onCloseSystemDialogs(String reason) {
11302 }
Joe Malin32736f02011-01-19 16:14:20 -080011303
Dianne Hackbornffa42482009-09-23 22:20:11 -070011304 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 * Given a Drawable whose bounds have been set to draw into this view,
11306 * update a Region being computed for {@link #gatherTransparentRegion} so
11307 * that any non-transparent parts of the Drawable are removed from the
11308 * given transparent region.
11309 *
11310 * @param dr The Drawable whose transparency is to be applied to the region.
11311 * @param region A Region holding the current transparency information,
11312 * where any parts of the region that are set are considered to be
11313 * transparent. On return, this region will be modified to have the
11314 * transparency information reduced by the corresponding parts of the
11315 * Drawable that are not transparent.
11316 * {@hide}
11317 */
11318 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11319 if (DBG) {
11320 Log.i("View", "Getting transparent region for: " + this);
11321 }
11322 final Region r = dr.getTransparentRegion();
11323 final Rect db = dr.getBounds();
11324 final AttachInfo attachInfo = mAttachInfo;
11325 if (r != null && attachInfo != null) {
11326 final int w = getRight()-getLeft();
11327 final int h = getBottom()-getTop();
11328 if (db.left > 0) {
11329 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11330 r.op(0, 0, db.left, h, Region.Op.UNION);
11331 }
11332 if (db.right < w) {
11333 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11334 r.op(db.right, 0, w, h, Region.Op.UNION);
11335 }
11336 if (db.top > 0) {
11337 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11338 r.op(0, 0, w, db.top, Region.Op.UNION);
11339 }
11340 if (db.bottom < h) {
11341 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11342 r.op(0, db.bottom, w, h, Region.Op.UNION);
11343 }
11344 final int[] location = attachInfo.mTransparentLocation;
11345 getLocationInWindow(location);
11346 r.translate(location[0], location[1]);
11347 region.op(r, Region.Op.INTERSECT);
11348 } else {
11349 region.op(db, Region.Op.DIFFERENCE);
11350 }
11351 }
11352
Adam Powelle14579b2009-12-16 18:39:52 -080011353 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011354 mHasPerformedLongPress = false;
11355
11356 if (mPendingCheckForLongPress == null) {
11357 mPendingCheckForLongPress = new CheckForLongPress();
11358 }
11359 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011360 postDelayed(mPendingCheckForLongPress,
11361 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011362 }
11363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011364 /**
11365 * Inflate a view from an XML resource. This convenience method wraps the {@link
11366 * LayoutInflater} class, which provides a full range of options for view inflation.
11367 *
11368 * @param context The Context object for your activity or application.
11369 * @param resource The resource ID to inflate
11370 * @param root A view group that will be the parent. Used to properly inflate the
11371 * layout_* parameters.
11372 * @see LayoutInflater
11373 */
11374 public static View inflate(Context context, int resource, ViewGroup root) {
11375 LayoutInflater factory = LayoutInflater.from(context);
11376 return factory.inflate(resource, root);
11377 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011378
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011379 /**
Adam Powell637d3372010-08-25 14:37:03 -070011380 * Scroll the view with standard behavior for scrolling beyond the normal
11381 * content boundaries. Views that call this method should override
11382 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11383 * results of an over-scroll operation.
11384 *
11385 * Views can use this method to handle any touch or fling-based scrolling.
11386 *
11387 * @param deltaX Change in X in pixels
11388 * @param deltaY Change in Y in pixels
11389 * @param scrollX Current X scroll value in pixels before applying deltaX
11390 * @param scrollY Current Y scroll value in pixels before applying deltaY
11391 * @param scrollRangeX Maximum content scroll range along the X axis
11392 * @param scrollRangeY Maximum content scroll range along the Y axis
11393 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11394 * along the X axis.
11395 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11396 * along the Y axis.
11397 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11398 * @return true if scrolling was clamped to an over-scroll boundary along either
11399 * axis, false otherwise.
11400 */
11401 protected boolean overScrollBy(int deltaX, int deltaY,
11402 int scrollX, int scrollY,
11403 int scrollRangeX, int scrollRangeY,
11404 int maxOverScrollX, int maxOverScrollY,
11405 boolean isTouchEvent) {
11406 final int overScrollMode = mOverScrollMode;
11407 final boolean canScrollHorizontal =
11408 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11409 final boolean canScrollVertical =
11410 computeVerticalScrollRange() > computeVerticalScrollExtent();
11411 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11412 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11413 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11414 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11415
11416 int newScrollX = scrollX + deltaX;
11417 if (!overScrollHorizontal) {
11418 maxOverScrollX = 0;
11419 }
11420
11421 int newScrollY = scrollY + deltaY;
11422 if (!overScrollVertical) {
11423 maxOverScrollY = 0;
11424 }
11425
11426 // Clamp values if at the limits and record
11427 final int left = -maxOverScrollX;
11428 final int right = maxOverScrollX + scrollRangeX;
11429 final int top = -maxOverScrollY;
11430 final int bottom = maxOverScrollY + scrollRangeY;
11431
11432 boolean clampedX = false;
11433 if (newScrollX > right) {
11434 newScrollX = right;
11435 clampedX = true;
11436 } else if (newScrollX < left) {
11437 newScrollX = left;
11438 clampedX = true;
11439 }
11440
11441 boolean clampedY = false;
11442 if (newScrollY > bottom) {
11443 newScrollY = bottom;
11444 clampedY = true;
11445 } else if (newScrollY < top) {
11446 newScrollY = top;
11447 clampedY = true;
11448 }
11449
11450 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11451
11452 return clampedX || clampedY;
11453 }
11454
11455 /**
11456 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11457 * respond to the results of an over-scroll operation.
11458 *
11459 * @param scrollX New X scroll value in pixels
11460 * @param scrollY New Y scroll value in pixels
11461 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11462 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11463 */
11464 protected void onOverScrolled(int scrollX, int scrollY,
11465 boolean clampedX, boolean clampedY) {
11466 // Intentionally empty.
11467 }
11468
11469 /**
11470 * Returns the over-scroll mode for this view. The result will be
11471 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11472 * (allow over-scrolling only if the view content is larger than the container),
11473 * or {@link #OVER_SCROLL_NEVER}.
11474 *
11475 * @return This view's over-scroll mode.
11476 */
11477 public int getOverScrollMode() {
11478 return mOverScrollMode;
11479 }
11480
11481 /**
11482 * Set the over-scroll mode for this view. Valid over-scroll modes are
11483 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11484 * (allow over-scrolling only if the view content is larger than the container),
11485 * or {@link #OVER_SCROLL_NEVER}.
11486 *
11487 * Setting the over-scroll mode of a view will have an effect only if the
11488 * view is capable of scrolling.
11489 *
11490 * @param overScrollMode The new over-scroll mode for this view.
11491 */
11492 public void setOverScrollMode(int overScrollMode) {
11493 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11494 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11495 overScrollMode != OVER_SCROLL_NEVER) {
11496 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11497 }
11498 mOverScrollMode = overScrollMode;
11499 }
11500
11501 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011502 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11503 * Each MeasureSpec represents a requirement for either the width or the height.
11504 * A MeasureSpec is comprised of a size and a mode. There are three possible
11505 * modes:
11506 * <dl>
11507 * <dt>UNSPECIFIED</dt>
11508 * <dd>
11509 * The parent has not imposed any constraint on the child. It can be whatever size
11510 * it wants.
11511 * </dd>
11512 *
11513 * <dt>EXACTLY</dt>
11514 * <dd>
11515 * The parent has determined an exact size for the child. The child is going to be
11516 * given those bounds regardless of how big it wants to be.
11517 * </dd>
11518 *
11519 * <dt>AT_MOST</dt>
11520 * <dd>
11521 * The child can be as large as it wants up to the specified size.
11522 * </dd>
11523 * </dl>
11524 *
11525 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11526 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11527 */
11528 public static class MeasureSpec {
11529 private static final int MODE_SHIFT = 30;
11530 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11531
11532 /**
11533 * Measure specification mode: The parent has not imposed any constraint
11534 * on the child. It can be whatever size it wants.
11535 */
11536 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11537
11538 /**
11539 * Measure specification mode: The parent has determined an exact size
11540 * for the child. The child is going to be given those bounds regardless
11541 * of how big it wants to be.
11542 */
11543 public static final int EXACTLY = 1 << MODE_SHIFT;
11544
11545 /**
11546 * Measure specification mode: The child can be as large as it wants up
11547 * to the specified size.
11548 */
11549 public static final int AT_MOST = 2 << MODE_SHIFT;
11550
11551 /**
11552 * Creates a measure specification based on the supplied size and mode.
11553 *
11554 * The mode must always be one of the following:
11555 * <ul>
11556 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11557 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11558 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11559 * </ul>
11560 *
11561 * @param size the size of the measure specification
11562 * @param mode the mode of the measure specification
11563 * @return the measure specification based on size and mode
11564 */
11565 public static int makeMeasureSpec(int size, int mode) {
11566 return size + mode;
11567 }
11568
11569 /**
11570 * Extracts the mode from the supplied measure specification.
11571 *
11572 * @param measureSpec the measure specification to extract the mode from
11573 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11574 * {@link android.view.View.MeasureSpec#AT_MOST} or
11575 * {@link android.view.View.MeasureSpec#EXACTLY}
11576 */
11577 public static int getMode(int measureSpec) {
11578 return (measureSpec & MODE_MASK);
11579 }
11580
11581 /**
11582 * Extracts the size from the supplied measure specification.
11583 *
11584 * @param measureSpec the measure specification to extract the size from
11585 * @return the size in pixels defined in the supplied measure specification
11586 */
11587 public static int getSize(int measureSpec) {
11588 return (measureSpec & ~MODE_MASK);
11589 }
11590
11591 /**
11592 * Returns a String representation of the specified measure
11593 * specification.
11594 *
11595 * @param measureSpec the measure specification to convert to a String
11596 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11597 */
11598 public static String toString(int measureSpec) {
11599 int mode = getMode(measureSpec);
11600 int size = getSize(measureSpec);
11601
11602 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11603
11604 if (mode == UNSPECIFIED)
11605 sb.append("UNSPECIFIED ");
11606 else if (mode == EXACTLY)
11607 sb.append("EXACTLY ");
11608 else if (mode == AT_MOST)
11609 sb.append("AT_MOST ");
11610 else
11611 sb.append(mode).append(" ");
11612
11613 sb.append(size);
11614 return sb.toString();
11615 }
11616 }
11617
11618 class CheckForLongPress implements Runnable {
11619
11620 private int mOriginalWindowAttachCount;
11621
11622 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011623 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011624 && mOriginalWindowAttachCount == mWindowAttachCount) {
11625 if (performLongClick()) {
11626 mHasPerformedLongPress = true;
11627 }
11628 }
11629 }
11630
11631 public void rememberWindowAttachCount() {
11632 mOriginalWindowAttachCount = mWindowAttachCount;
11633 }
11634 }
Joe Malin32736f02011-01-19 16:14:20 -080011635
Adam Powelle14579b2009-12-16 18:39:52 -080011636 private final class CheckForTap implements Runnable {
11637 public void run() {
11638 mPrivateFlags &= ~PREPRESSED;
11639 mPrivateFlags |= PRESSED;
11640 refreshDrawableState();
11641 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11642 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11643 }
11644 }
11645 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011646
Adam Powella35d7682010-03-12 14:48:13 -080011647 private final class PerformClick implements Runnable {
11648 public void run() {
11649 performClick();
11650 }
11651 }
11652
Dianne Hackborn63042d62011-01-26 18:56:29 -080011653 /** @hide */
11654 public void hackTurnOffWindowResizeAnim(boolean off) {
11655 mAttachInfo.mTurnOffWindowResizeAnim = off;
11656 }
Joe Malin32736f02011-01-19 16:14:20 -080011657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011658 /**
11659 * Interface definition for a callback to be invoked when a key event is
11660 * dispatched to this view. The callback will be invoked before the key
11661 * event is given to the view.
11662 */
11663 public interface OnKeyListener {
11664 /**
11665 * Called when a key is dispatched to a view. This allows listeners to
11666 * get a chance to respond before the target view.
11667 *
11668 * @param v The view the key has been dispatched to.
11669 * @param keyCode The code for the physical key that was pressed
11670 * @param event The KeyEvent object containing full information about
11671 * the event.
11672 * @return True if the listener has consumed the event, false otherwise.
11673 */
11674 boolean onKey(View v, int keyCode, KeyEvent event);
11675 }
11676
11677 /**
11678 * Interface definition for a callback to be invoked when a touch event is
11679 * dispatched to this view. The callback will be invoked before the touch
11680 * event is given to the view.
11681 */
11682 public interface OnTouchListener {
11683 /**
11684 * Called when a touch event is dispatched to a view. This allows listeners to
11685 * get a chance to respond before the target view.
11686 *
11687 * @param v The view the touch event has been dispatched to.
11688 * @param event The MotionEvent object containing full information about
11689 * the event.
11690 * @return True if the listener has consumed the event, false otherwise.
11691 */
11692 boolean onTouch(View v, MotionEvent event);
11693 }
11694
11695 /**
11696 * Interface definition for a callback to be invoked when a view has been clicked and held.
11697 */
11698 public interface OnLongClickListener {
11699 /**
11700 * Called when a view has been clicked and held.
11701 *
11702 * @param v The view that was clicked and held.
11703 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080011704 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011705 */
11706 boolean onLongClick(View v);
11707 }
11708
11709 /**
Chris Tate32affef2010-10-18 15:29:21 -070011710 * Interface definition for a callback to be invoked when a drag is being dispatched
11711 * to this view. The callback will be invoked before the hosting view's own
11712 * onDrag(event) method. If the listener wants to fall back to the hosting view's
11713 * onDrag(event) behavior, it should return 'false' from this callback.
11714 */
11715 public interface OnDragListener {
11716 /**
11717 * Called when a drag event is dispatched to a view. This allows listeners
11718 * to get a chance to override base View behavior.
11719 *
Joe Malin32736f02011-01-19 16:14:20 -080011720 * @param v The View that received the drag event.
11721 * @param event The {@link android.view.DragEvent} object for the drag event.
11722 * @return {@code true} if the drag event was handled successfully, or {@code false}
11723 * if the drag event was not handled. Note that {@code false} will trigger the View
11724 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070011725 */
11726 boolean onDrag(View v, DragEvent event);
11727 }
11728
11729 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011730 * Interface definition for a callback to be invoked when the focus state of
11731 * a view changed.
11732 */
11733 public interface OnFocusChangeListener {
11734 /**
11735 * Called when the focus state of a view has changed.
11736 *
11737 * @param v The view whose state has changed.
11738 * @param hasFocus The new focus state of v.
11739 */
11740 void onFocusChange(View v, boolean hasFocus);
11741 }
11742
11743 /**
11744 * Interface definition for a callback to be invoked when a view is clicked.
11745 */
11746 public interface OnClickListener {
11747 /**
11748 * Called when a view has been clicked.
11749 *
11750 * @param v The view that was clicked.
11751 */
11752 void onClick(View v);
11753 }
11754
11755 /**
11756 * Interface definition for a callback to be invoked when the context menu
11757 * for this view is being built.
11758 */
11759 public interface OnCreateContextMenuListener {
11760 /**
11761 * Called when the context menu for this view is being built. It is not
11762 * safe to hold onto the menu after this method returns.
11763 *
11764 * @param menu The context menu that is being built
11765 * @param v The view for which the context menu is being built
11766 * @param menuInfo Extra information about the item for which the
11767 * context menu should be shown. This information will vary
11768 * depending on the class of v.
11769 */
11770 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
11771 }
11772
Joe Onorato664644d2011-01-23 17:53:23 -080011773 /**
11774 * Interface definition for a callback to be invoked when the status bar changes
11775 * visibility.
11776 *
11777 * @see #setOnSystemUiVisibilityChangeListener
11778 */
11779 public interface OnSystemUiVisibilityChangeListener {
11780 /**
11781 * Called when the status bar changes visibility because of a call to
11782 * {@link #setSystemUiVisibility}.
11783 *
11784 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
11785 */
11786 public void onSystemUiVisibilityChange(int visibility);
11787 }
11788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011789 private final class UnsetPressedState implements Runnable {
11790 public void run() {
11791 setPressed(false);
11792 }
11793 }
11794
11795 /**
11796 * Base class for derived classes that want to save and restore their own
11797 * state in {@link android.view.View#onSaveInstanceState()}.
11798 */
11799 public static class BaseSavedState extends AbsSavedState {
11800 /**
11801 * Constructor used when reading from a parcel. Reads the state of the superclass.
11802 *
11803 * @param source
11804 */
11805 public BaseSavedState(Parcel source) {
11806 super(source);
11807 }
11808
11809 /**
11810 * Constructor called by derived classes when creating their SavedState objects
11811 *
11812 * @param superState The state of the superclass of this view
11813 */
11814 public BaseSavedState(Parcelable superState) {
11815 super(superState);
11816 }
11817
11818 public static final Parcelable.Creator<BaseSavedState> CREATOR =
11819 new Parcelable.Creator<BaseSavedState>() {
11820 public BaseSavedState createFromParcel(Parcel in) {
11821 return new BaseSavedState(in);
11822 }
11823
11824 public BaseSavedState[] newArray(int size) {
11825 return new BaseSavedState[size];
11826 }
11827 };
11828 }
11829
11830 /**
11831 * A set of information given to a view when it is attached to its parent
11832 * window.
11833 */
11834 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011835 interface Callbacks {
11836 void playSoundEffect(int effectId);
11837 boolean performHapticFeedback(int effectId, boolean always);
11838 }
11839
11840 /**
11841 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
11842 * to a Handler. This class contains the target (View) to invalidate and
11843 * the coordinates of the dirty rectangle.
11844 *
11845 * For performance purposes, this class also implements a pool of up to
11846 * POOL_LIMIT objects that get reused. This reduces memory allocations
11847 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011848 */
Romain Guyd928d682009-03-31 17:52:16 -070011849 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070011851 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
11852 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070011853 public InvalidateInfo newInstance() {
11854 return new InvalidateInfo();
11855 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011856
Romain Guyd928d682009-03-31 17:52:16 -070011857 public void onAcquired(InvalidateInfo element) {
11858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011859
Romain Guyd928d682009-03-31 17:52:16 -070011860 public void onReleased(InvalidateInfo element) {
11861 }
11862 }, POOL_LIMIT)
11863 );
11864
11865 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011866
11867 View target;
11868
11869 int left;
11870 int top;
11871 int right;
11872 int bottom;
11873
Romain Guyd928d682009-03-31 17:52:16 -070011874 public void setNextPoolable(InvalidateInfo element) {
11875 mNext = element;
11876 }
11877
11878 public InvalidateInfo getNextPoolable() {
11879 return mNext;
11880 }
11881
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011882 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070011883 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011884 }
11885
11886 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070011887 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011888 }
11889 }
11890
11891 final IWindowSession mSession;
11892
11893 final IWindow mWindow;
11894
11895 final IBinder mWindowToken;
11896
11897 final Callbacks mRootCallbacks;
11898
Chet Haasedaf98e92011-01-10 14:10:36 -080011899 Canvas mHardwareCanvas;
11900
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011901 /**
11902 * The top view of the hierarchy.
11903 */
11904 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070011905
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906 IBinder mPanelParentWindowToken;
11907 Surface mSurface;
11908
Romain Guyb051e892010-09-28 19:09:36 -070011909 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011910 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070011911 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080011912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011914 * Scale factor used by the compatibility mode
11915 */
11916 float mApplicationScale;
11917
11918 /**
11919 * Indicates whether the application is in compatibility mode
11920 */
11921 boolean mScalingRequired;
11922
11923 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080011924 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
11925 */
11926 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080011927
Dianne Hackborn63042d62011-01-26 18:56:29 -080011928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011929 * Left position of this view's window
11930 */
11931 int mWindowLeft;
11932
11933 /**
11934 * Top position of this view's window
11935 */
11936 int mWindowTop;
11937
11938 /**
Adam Powell26153a32010-11-08 15:22:27 -080011939 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070011940 */
Adam Powell26153a32010-11-08 15:22:27 -080011941 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070011942
11943 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011944 * For windows that are full-screen but using insets to layout inside
11945 * of the screen decorations, these are the current insets for the
11946 * content of the window.
11947 */
11948 final Rect mContentInsets = new Rect();
11949
11950 /**
11951 * For windows that are full-screen but using insets to layout inside
11952 * of the screen decorations, these are the current insets for the
11953 * actual visible parts of the window.
11954 */
11955 final Rect mVisibleInsets = new Rect();
11956
11957 /**
11958 * The internal insets given by this window. This value is
11959 * supplied by the client (through
11960 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
11961 * be given to the window manager when changed to be used in laying
11962 * out windows behind it.
11963 */
11964 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
11965 = new ViewTreeObserver.InternalInsetsInfo();
11966
11967 /**
11968 * All views in the window's hierarchy that serve as scroll containers,
11969 * used to determine if the window can be resized or must be panned
11970 * to adjust for a soft input area.
11971 */
11972 final ArrayList<View> mScrollContainers = new ArrayList<View>();
11973
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070011974 final KeyEvent.DispatcherState mKeyDispatchState
11975 = new KeyEvent.DispatcherState();
11976
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011977 /**
11978 * Indicates whether the view's window currently has the focus.
11979 */
11980 boolean mHasWindowFocus;
11981
11982 /**
11983 * The current visibility of the window.
11984 */
11985 int mWindowVisibility;
11986
11987 /**
11988 * Indicates the time at which drawing started to occur.
11989 */
11990 long mDrawingTime;
11991
11992 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070011993 * Indicates whether or not ignoring the DIRTY_MASK flags.
11994 */
11995 boolean mIgnoreDirtyState;
11996
11997 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011998 * Indicates whether the view's window is currently in touch mode.
11999 */
12000 boolean mInTouchMode;
12001
12002 /**
12003 * Indicates that ViewRoot should trigger a global layout change
12004 * the next time it performs a traversal
12005 */
12006 boolean mRecomputeGlobalAttributes;
12007
12008 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012009 * Set during a traveral if any views want to keep the screen on.
12010 */
12011 boolean mKeepScreenOn;
12012
12013 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012014 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12015 */
12016 int mSystemUiVisibility;
12017
12018 /**
12019 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12020 * attached.
12021 */
12022 boolean mHasSystemUiListeners;
12023
12024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012025 * Set if the visibility of any views has changed.
12026 */
12027 boolean mViewVisibilityChanged;
12028
12029 /**
12030 * Set to true if a view has been scrolled.
12031 */
12032 boolean mViewScrollChanged;
12033
12034 /**
12035 * Global to the view hierarchy used as a temporary for dealing with
12036 * x/y points in the transparent region computations.
12037 */
12038 final int[] mTransparentLocation = new int[2];
12039
12040 /**
12041 * Global to the view hierarchy used as a temporary for dealing with
12042 * x/y points in the ViewGroup.invalidateChild implementation.
12043 */
12044 final int[] mInvalidateChildLocation = new int[2];
12045
Chet Haasec3aa3612010-06-17 08:50:37 -070012046
12047 /**
12048 * Global to the view hierarchy used as a temporary for dealing with
12049 * x/y location when view is transformed.
12050 */
12051 final float[] mTmpTransformLocation = new float[2];
12052
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012053 /**
12054 * The view tree observer used to dispatch global events like
12055 * layout, pre-draw, touch mode change, etc.
12056 */
12057 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12058
12059 /**
12060 * A Canvas used by the view hierarchy to perform bitmap caching.
12061 */
12062 Canvas mCanvas;
12063
12064 /**
12065 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12066 * handler can be used to pump events in the UI events queue.
12067 */
12068 final Handler mHandler;
12069
12070 /**
12071 * Identifier for messages requesting the view to be invalidated.
12072 * Such messages should be sent to {@link #mHandler}.
12073 */
12074 static final int INVALIDATE_MSG = 0x1;
12075
12076 /**
12077 * Identifier for messages requesting the view to invalidate a region.
12078 * Such messages should be sent to {@link #mHandler}.
12079 */
12080 static final int INVALIDATE_RECT_MSG = 0x2;
12081
12082 /**
12083 * Temporary for use in computing invalidate rectangles while
12084 * calling up the hierarchy.
12085 */
12086 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012087
12088 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012089 * Temporary for use in computing hit areas with transformed views
12090 */
12091 final RectF mTmpTransformRect = new RectF();
12092
12093 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012094 * Temporary list for use in collecting focusable descendents of a view.
12095 */
12096 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12097
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012098 /**
12099 * Creates a new set of attachment information with the specified
12100 * events handler and thread.
12101 *
12102 * @param handler the events handler the view must use
12103 */
12104 AttachInfo(IWindowSession session, IWindow window,
12105 Handler handler, Callbacks effectPlayer) {
12106 mSession = session;
12107 mWindow = window;
12108 mWindowToken = window.asBinder();
12109 mHandler = handler;
12110 mRootCallbacks = effectPlayer;
12111 }
12112 }
12113
12114 /**
12115 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12116 * is supported. This avoids keeping too many unused fields in most
12117 * instances of View.</p>
12118 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012119 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012120
Mike Cleronf116bf82009-09-27 19:14:12 -070012121 /**
12122 * Scrollbars are not visible
12123 */
12124 public static final int OFF = 0;
12125
12126 /**
12127 * Scrollbars are visible
12128 */
12129 public static final int ON = 1;
12130
12131 /**
12132 * Scrollbars are fading away
12133 */
12134 public static final int FADING = 2;
12135
12136 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012138 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012139 public int scrollBarDefaultDelayBeforeFade;
12140 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012141
12142 public int scrollBarSize;
12143 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012144 public float[] interpolatorValues;
12145 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012146
12147 public final Paint paint;
12148 public final Matrix matrix;
12149 public Shader shader;
12150
Mike Cleronf116bf82009-09-27 19:14:12 -070012151 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12152
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012153 private static final float[] OPAQUE = { 255 };
12154 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012155
Mike Cleronf116bf82009-09-27 19:14:12 -070012156 /**
12157 * When fading should start. This time moves into the future every time
12158 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12159 */
12160 public long fadeStartTime;
12161
12162
12163 /**
12164 * The current state of the scrollbars: ON, OFF, or FADING
12165 */
12166 public int state = OFF;
12167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012168 private int mLastColor;
12169
Mike Cleronf116bf82009-09-27 19:14:12 -070012170 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012171 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12172 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012173 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12174 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012175
12176 paint = new Paint();
12177 matrix = new Matrix();
12178 // use use a height of 1, and then wack the matrix each time we
12179 // actually use it.
12180 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012181
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012182 paint.setShader(shader);
12183 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012184 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012185 }
Romain Guy8506ab42009-06-11 17:35:47 -070012186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012187 public void setFadeColor(int color) {
12188 if (color != 0 && color != mLastColor) {
12189 mLastColor = color;
12190 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012191
Romain Guye55e1a72009-08-27 10:42:26 -070012192 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12193 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012195 paint.setShader(shader);
12196 // Restore the default transfer mode (src_over)
12197 paint.setXfermode(null);
12198 }
12199 }
Joe Malin32736f02011-01-19 16:14:20 -080012200
Mike Cleronf116bf82009-09-27 19:14:12 -070012201 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012202 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012203 if (now >= fadeStartTime) {
12204
12205 // the animation fades the scrollbars out by changing
12206 // the opacity (alpha) from fully opaque to fully
12207 // transparent
12208 int nextFrame = (int) now;
12209 int framesCount = 0;
12210
12211 Interpolator interpolator = scrollBarInterpolator;
12212
12213 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012214 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012215
12216 // End transparent
12217 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012218 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012219
12220 state = FADING;
12221
12222 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012223 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012224 }
12225 }
12226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012227 }
12228}