blob: 7d821b50bba74ed469513f8714385af82b803cee [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
svetoslavganov75986cf2009-05-14 22:28:01 -070019import com.android.internal.R;
20import com.android.internal.view.menu.MenuBuilder;
21
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.Context;
23import android.content.res.Resources;
24import android.content.res.TypedArray;
25import android.graphics.Bitmap;
26import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
36import android.graphics.Region;
37import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.drawable.ColorDrawable;
39import android.graphics.drawable.Drawable;
40import android.os.Handler;
41import android.os.IBinder;
42import android.os.Message;
43import android.os.Parcel;
44import android.os.Parcelable;
45import android.os.RemoteException;
46import android.os.SystemClock;
47import android.os.SystemProperties;
48import android.util.AttributeSet;
svetoslavganov75986cf2009-05-14 22:28:01 -070049import android.util.Config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.util.EventLog;
51import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
56import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.view.accessibility.AccessibilityEvent;
59import android.view.accessibility.AccessibilityEventSource;
60import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070062import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.inputmethod.InputConnection;
65import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.ScrollBarDrawable;
67
svetoslavganov75986cf2009-05-14 22:28:01 -070068import java.lang.ref.SoftReference;
69import java.lang.reflect.InvocationTargetException;
70import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import java.util.ArrayList;
72import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070073import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074
75/**
76 * <p>
77 * This class represents the basic building block for user interface components. A View
78 * occupies a rectangular area on the screen and is responsible for drawing and
79 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070080 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
82 * are invisible containers that hold other Views (or other ViewGroups) and define
83 * their layout properties.
84 * </p>
85 *
86 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070087 * <p>For an introduction to using this class to develop your
88 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070090 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
92 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
93 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
94 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
95 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
99 * </p>
100 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * <a name="Using"></a>
103 * <h3>Using Views</h3>
104 * <p>
105 * All of the views in a window are arranged in a single tree. You can add views
106 * either from code or by specifying a tree of views in one or more XML layout
107 * files. There are many specialized subclasses of views that act as controls or
108 * are capable of displaying text, images, or other content.
109 * </p>
110 * <p>
111 * Once you have created a tree of views, there are typically a few types of
112 * common operations you may wish to perform:
113 * <ul>
114 * <li><strong>Set properties:</strong> for example setting the text of a
115 * {@link android.widget.TextView}. The available properties and the methods
116 * that set them will vary among the different subclasses of views. Note that
117 * properties that are known at build time can be set in the XML layout
118 * files.</li>
119 * <li><strong>Set focus:</strong> The framework will handled moving focus in
120 * response to user input. To force focus to a specific view, call
121 * {@link #requestFocus}.</li>
122 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
123 * that will be notified when something interesting happens to the view. For
124 * example, all views will let you set a listener to be notified when the view
125 * gains or loses focus. You can register such a listener using
126 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
127 * specialized listeners. For example, a Button exposes a listener to notify
128 * clients when the button is clicked.</li>
129 * <li><strong>Set visibility:</strong> You can hide or show views using
130 * {@link #setVisibility}.</li>
131 * </ul>
132 * </p>
133 * <p><em>
134 * Note: The Android framework is responsible for measuring, laying out and
135 * drawing views. You should not call methods that perform these actions on
136 * views yourself unless you are actually implementing a
137 * {@link android.view.ViewGroup}.
138 * </em></p>
139 *
140 * <a name="Lifecycle"></a>
141 * <h3>Implementing a Custom View</h3>
142 *
143 * <p>
144 * To implement a custom view, you will usually begin by providing overrides for
145 * some of the standard methods that the framework calls on all views. You do
146 * not need to override all of these methods. In fact, you can start by just
147 * overriding {@link #onDraw(android.graphics.Canvas)}.
148 * <table border="2" width="85%" align="center" cellpadding="5">
149 * <thead>
150 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
151 * </thead>
152 *
153 * <tbody>
154 * <tr>
155 * <td rowspan="2">Creation</td>
156 * <td>Constructors</td>
157 * <td>There is a form of the constructor that are called when the view
158 * is created from code and a form that is called when the view is
159 * inflated from a layout file. The second form should parse and apply
160 * any attributes defined in the layout file.
161 * </td>
162 * </tr>
163 * <tr>
164 * <td><code>{@link #onFinishInflate()}</code></td>
165 * <td>Called after a view and all of its children has been inflated
166 * from XML.</td>
167 * </tr>
168 *
169 * <tr>
170 * <td rowspan="3">Layout</td>
171 * <td><code>{@link #onMeasure}</code></td>
172 * <td>Called to determine the size requirements for this view and all
173 * of its children.
174 * </td>
175 * </tr>
176 * <tr>
177 * <td><code>{@link #onLayout}</code></td>
178 * <td>Called when this view should assign a size and position to all
179 * of its children.
180 * </td>
181 * </tr>
182 * <tr>
183 * <td><code>{@link #onSizeChanged}</code></td>
184 * <td>Called when the size of this view has changed.
185 * </td>
186 * </tr>
187 *
188 * <tr>
189 * <td>Drawing</td>
190 * <td><code>{@link #onDraw}</code></td>
191 * <td>Called when the view should render its content.
192 * </td>
193 * </tr>
194 *
195 * <tr>
196 * <td rowspan="4">Event processing</td>
197 * <td><code>{@link #onKeyDown}</code></td>
198 * <td>Called when a new key event occurs.
199 * </td>
200 * </tr>
201 * <tr>
202 * <td><code>{@link #onKeyUp}</code></td>
203 * <td>Called when a key up event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onTrackballEvent}</code></td>
208 * <td>Called when a trackball motion event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTouchEvent}</code></td>
213 * <td>Called when a touch screen motion event occurs.
214 * </td>
215 * </tr>
216 *
217 * <tr>
218 * <td rowspan="2">Focus</td>
219 * <td><code>{@link #onFocusChanged}</code></td>
220 * <td>Called when the view gains or loses focus.
221 * </td>
222 * </tr>
223 *
224 * <tr>
225 * <td><code>{@link #onWindowFocusChanged}</code></td>
226 * <td>Called when the window containing the view gains or loses focus.
227 * </td>
228 * </tr>
229 *
230 * <tr>
231 * <td rowspan="3">Attaching</td>
232 * <td><code>{@link #onAttachedToWindow()}</code></td>
233 * <td>Called when the view is attached to a window.
234 * </td>
235 * </tr>
236 *
237 * <tr>
238 * <td><code>{@link #onDetachedFromWindow}</code></td>
239 * <td>Called when the view is detached from its window.
240 * </td>
241 * </tr>
242 *
243 * <tr>
244 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
245 * <td>Called when the visibility of the window containing the view
246 * has changed.
247 * </td>
248 * </tr>
249 * </tbody>
250 *
251 * </table>
252 * </p>
253 *
254 * <a name="IDs"></a>
255 * <h3>IDs</h3>
256 * Views may have an integer id associated with them. These ids are typically
257 * assigned in the layout XML files, and are used to find specific views within
258 * the view tree. A common pattern is to:
259 * <ul>
260 * <li>Define a Button in the layout file and assign it a unique ID.
261 * <pre>
262 * &lt;Button id="@+id/my_button"
263 * android:layout_width="wrap_content"
264 * android:layout_height="wrap_content"
265 * android:text="@string/my_button_text"/&gt;
266 * </pre></li>
267 * <li>From the onCreate method of an Activity, find the Button
268 * <pre class="prettyprint">
269 * Button myButton = (Button) findViewById(R.id.my_button);
270 * </pre></li>
271 * </ul>
272 * <p>
273 * View IDs need not be unique throughout the tree, but it is good practice to
274 * ensure that they are at least unique within the part of the tree you are
275 * searching.
276 * </p>
277 *
278 * <a name="Position"></a>
279 * <h3>Position</h3>
280 * <p>
281 * The geometry of a view is that of a rectangle. A view has a location,
282 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
283 * two dimensions, expressed as a width and a height. The unit for location
284 * and dimensions is the pixel.
285 * </p>
286 *
287 * <p>
288 * It is possible to retrieve the location of a view by invoking the methods
289 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
290 * coordinate of the rectangle representing the view. The latter returns the
291 * top, or Y, coordinate of the rectangle representing the view. These methods
292 * both return the location of the view relative to its parent. For instance,
293 * when getLeft() returns 20, that means the view is located 20 pixels to the
294 * right of the left edge of its direct parent.
295 * </p>
296 *
297 * <p>
298 * In addition, several convenience methods are offered to avoid unnecessary
299 * computations, namely {@link #getRight()} and {@link #getBottom()}.
300 * These methods return the coordinates of the right and bottom edges of the
301 * rectangle representing the view. For instance, calling {@link #getRight()}
302 * is similar to the following computation: <code>getLeft() + getWidth()</code>
303 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
304 * </p>
305 *
306 * <a name="SizePaddingMargins"></a>
307 * <h3>Size, padding and margins</h3>
308 * <p>
309 * The size of a view is expressed with a width and a height. A view actually
310 * possess two pairs of width and height values.
311 * </p>
312 *
313 * <p>
314 * The first pair is known as <em>measured width</em> and
315 * <em>measured height</em>. These dimensions define how big a view wants to be
316 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
317 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
318 * and {@link #getMeasuredHeight()}.
319 * </p>
320 *
321 * <p>
322 * The second pair is simply known as <em>width</em> and <em>height</em>, or
323 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
324 * dimensions define the actual size of the view on screen, at drawing time and
325 * after layout. These values may, but do not have to, be different from the
326 * measured width and height. The width and height can be obtained by calling
327 * {@link #getWidth()} and {@link #getHeight()}.
328 * </p>
329 *
330 * <p>
331 * To measure its dimensions, a view takes into account its padding. The padding
332 * is expressed in pixels for the left, top, right and bottom parts of the view.
333 * Padding can be used to offset the content of the view by a specific amount of
334 * pixels. For instance, a left padding of 2 will push the view's content by
335 * 2 pixels to the right of the left edge. Padding can be set using the
336 * {@link #setPadding(int, int, int, int)} method and queried by calling
337 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
338 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
339 * </p>
340 *
341 * <p>
342 * Even though a view can define a padding, it does not provide any support for
343 * margins. However, view groups provide such a support. Refer to
344 * {@link android.view.ViewGroup} and
345 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
346 * </p>
347 *
348 * <a name="Layout"></a>
349 * <h3>Layout</h3>
350 * <p>
351 * Layout is a two pass process: a measure pass and a layout pass. The measuring
352 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
353 * of the view tree. Each view pushes dimension specifications down the tree
354 * during the recursion. At the end of the measure pass, every view has stored
355 * its measurements. The second pass happens in
356 * {@link #layout(int,int,int,int)} and is also top-down. During
357 * this pass each parent is responsible for positioning all of its children
358 * using the sizes computed in the measure pass.
359 * </p>
360 *
361 * <p>
362 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
363 * {@link #getMeasuredHeight()} values must be set, along with those for all of
364 * that view's descendants. A view's measured width and measured height values
365 * must respect the constraints imposed by the view's parents. This guarantees
366 * that at the end of the measure pass, all parents accept all of their
367 * children's measurements. A parent view may call measure() more than once on
368 * its children. For example, the parent may measure each child once with
369 * unspecified dimensions to find out how big they want to be, then call
370 * measure() on them again with actual numbers if the sum of all the children's
371 * unconstrained sizes is too big or too small.
372 * </p>
373 *
374 * <p>
375 * The measure pass uses two classes to communicate dimensions. The
376 * {@link MeasureSpec} class is used by views to tell their parents how they
377 * want to be measured and positioned. The base LayoutParams class just
378 * describes how big the view wants to be for both width and height. For each
379 * dimension, it can specify one of:
380 * <ul>
381 * <li> an exact number
382 * <li>FILL_PARENT, which means the view wants to be as big as its parent
383 * (minus padding)
384 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
385 * enclose its content (plus padding).
386 * </ul>
387 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
388 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
389 * an X and Y value.
390 * </p>
391 *
392 * <p>
393 * MeasureSpecs are used to push requirements down the tree from parent to
394 * child. A MeasureSpec can be in one of three modes:
395 * <ul>
396 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
397 * of a child view. For example, a LinearLayout may call measure() on its child
398 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
399 * tall the child view wants to be given a width of 240 pixels.
400 * <li>EXACTLY: This is used by the parent to impose an exact size on the
401 * child. The child must use this size, and guarantee that all of its
402 * descendants will fit within this size.
403 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
404 * child. The child must gurantee that it and all of its descendants will fit
405 * within this size.
406 * </ul>
407 * </p>
408 *
409 * <p>
410 * To intiate a layout, call {@link #requestLayout}. This method is typically
411 * called by a view on itself when it believes that is can no longer fit within
412 * its current bounds.
413 * </p>
414 *
415 * <a name="Drawing"></a>
416 * <h3>Drawing</h3>
417 * <p>
418 * Drawing is handled by walking the tree and rendering each view that
419 * intersects the the invalid region. Because the tree is traversed in-order,
420 * this means that parents will draw before (i.e., behind) their children, with
421 * siblings drawn in the order they appear in the tree.
422 * If you set a background drawable for a View, then the View will draw it for you
423 * before calling back to its <code>onDraw()</code> method.
424 * </p>
425 *
426 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700427 * 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 -0800428 * </p>
429 *
430 * <p>
431 * To force a view to draw, call {@link #invalidate()}.
432 * </p>
433 *
434 * <a name="EventHandlingThreading"></a>
435 * <h3>Event Handling and Threading</h3>
436 * <p>
437 * The basic cycle of a view is as follows:
438 * <ol>
439 * <li>An event comes in and is dispatched to the appropriate view. The view
440 * handles the event and notifies any listeners.</li>
441 * <li>If in the course of processing the event, the view's bounds may need
442 * to be changed, the view will call {@link #requestLayout()}.</li>
443 * <li>Similarly, if in the course of processing the event the view's appearance
444 * may need to be changed, the view will call {@link #invalidate()}.</li>
445 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
446 * the framework will take care of measuring, laying out, and drawing the tree
447 * as appropriate.</li>
448 * </ol>
449 * </p>
450 *
451 * <p><em>Note: The entire view tree is single threaded. You must always be on
452 * the UI thread when calling any method on any view.</em>
453 * If you are doing work on other threads and want to update the state of a view
454 * from that thread, you should use a {@link Handler}.
455 * </p>
456 *
457 * <a name="FocusHandling"></a>
458 * <h3>Focus Handling</h3>
459 * <p>
460 * The framework will handle routine focus movement in response to user input.
461 * This includes changing the focus as views are removed or hidden, or as new
462 * views become available. Views indicate their willingness to take focus
463 * through the {@link #isFocusable} method. To change whether a view can take
464 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
465 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
466 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
467 * </p>
468 * <p>
469 * Focus movement is based on an algorithm which finds the nearest neighbor in a
470 * given direction. In rare cases, the default algorithm may not match the
471 * intended behavior of the developer. In these situations, you can provide
472 * explicit overrides by using these XML attributes in the layout file:
473 * <pre>
474 * nextFocusDown
475 * nextFocusLeft
476 * nextFocusRight
477 * nextFocusUp
478 * </pre>
479 * </p>
480 *
481 *
482 * <p>
483 * To get a particular view to take focus, call {@link #requestFocus()}.
484 * </p>
485 *
486 * <a name="TouchMode"></a>
487 * <h3>Touch Mode</h3>
488 * <p>
489 * When a user is navigating a user interface via directional keys such as a D-pad, it is
490 * necessary to give focus to actionable items such as buttons so the user can see
491 * what will take input. If the device has touch capabilities, however, and the user
492 * begins interacting with the interface by touching it, it is no longer necessary to
493 * always highlight, or give focus to, a particular view. This motivates a mode
494 * for interaction named 'touch mode'.
495 * </p>
496 * <p>
497 * For a touch capable device, once the user touches the screen, the device
498 * will enter touch mode. From this point onward, only views for which
499 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
500 * Other views that are touchable, like buttons, will not take focus when touched; they will
501 * only fire the on click listeners.
502 * </p>
503 * <p>
504 * Any time a user hits a directional key, such as a D-pad direction, the view device will
505 * exit touch mode, and find a view to take focus, so that the user may resume interacting
506 * with the user interface without touching the screen again.
507 * </p>
508 * <p>
509 * The touch mode state is maintained across {@link android.app.Activity}s. Call
510 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
511 * </p>
512 *
513 * <a name="Scrolling"></a>
514 * <h3>Scrolling</h3>
515 * <p>
516 * The framework provides basic support for views that wish to internally
517 * scroll their content. This includes keeping track of the X and Y scroll
518 * offset as well as mechanisms for drawing scrollbars. See
Mike Cleronf116bf82009-09-27 19:14:12 -0700519 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
520 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 * </p>
522 *
523 * <a name="Tags"></a>
524 * <h3>Tags</h3>
525 * <p>
526 * Unlike IDs, tags are not used to identify views. Tags are essentially an
527 * extra piece of information that can be associated with a view. They are most
528 * often used as a convenience to store data related to views in the views
529 * themselves rather than by putting them in a separate structure.
530 * </p>
531 *
532 * <a name="Animation"></a>
533 * <h3>Animation</h3>
534 * <p>
535 * You can attach an {@link Animation} object to a view using
536 * {@link #setAnimation(Animation)} or
537 * {@link #startAnimation(Animation)}. The animation can alter the scale,
538 * rotation, translation and alpha of a view over time. If the animation is
539 * attached to a view that has children, the animation will affect the entire
540 * subtree rooted by that node. When an animation is started, the framework will
541 * take care of redrawing the appropriate views until the animation completes.
542 * </p>
543 *
Romain Guyd6a463a2009-05-21 23:10:10 -0700544 * @attr ref android.R.styleable#View_background
545 * @attr ref android.R.styleable#View_clickable
546 * @attr ref android.R.styleable#View_contentDescription
547 * @attr ref android.R.styleable#View_drawingCacheQuality
548 * @attr ref android.R.styleable#View_duplicateParentState
549 * @attr ref android.R.styleable#View_id
550 * @attr ref android.R.styleable#View_fadingEdge
551 * @attr ref android.R.styleable#View_fadingEdgeLength
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700553 * @attr ref android.R.styleable#View_isScrollContainer
554 * @attr ref android.R.styleable#View_focusable
555 * @attr ref android.R.styleable#View_focusableInTouchMode
556 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
557 * @attr ref android.R.styleable#View_keepScreenOn
558 * @attr ref android.R.styleable#View_longClickable
559 * @attr ref android.R.styleable#View_minHeight
560 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 * @attr ref android.R.styleable#View_nextFocusDown
562 * @attr ref android.R.styleable#View_nextFocusLeft
563 * @attr ref android.R.styleable#View_nextFocusRight
564 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700565 * @attr ref android.R.styleable#View_onClick
566 * @attr ref android.R.styleable#View_padding
567 * @attr ref android.R.styleable#View_paddingBottom
568 * @attr ref android.R.styleable#View_paddingLeft
569 * @attr ref android.R.styleable#View_paddingRight
570 * @attr ref android.R.styleable#View_paddingTop
571 * @attr ref android.R.styleable#View_saveEnabled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 * @attr ref android.R.styleable#View_scrollX
573 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700574 * @attr ref android.R.styleable#View_scrollbarSize
575 * @attr ref android.R.styleable#View_scrollbarStyle
576 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700577 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
578 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
580 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 * @attr ref android.R.styleable#View_scrollbarThumbVertical
582 * @attr ref android.R.styleable#View_scrollbarTrackVertical
583 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
584 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_soundEffectsEnabled
586 * @attr ref android.R.styleable#View_tag
587 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800588 *
589 * @see android.view.ViewGroup
590 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700591public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592 private static final boolean DBG = false;
593
594 /**
595 * The logging tag used by this class with android.util.Log.
596 */
597 protected static final String VIEW_LOG_TAG = "View";
598
599 /**
600 * Used to mark a View that has no ID.
601 */
602 public static final int NO_ID = -1;
603
604 /**
605 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
606 * calling setFlags.
607 */
608 private static final int NOT_FOCUSABLE = 0x00000000;
609
610 /**
611 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
612 * setFlags.
613 */
614 private static final int FOCUSABLE = 0x00000001;
615
616 /**
617 * Mask for use with setFlags indicating bits used for focus.
618 */
619 private static final int FOCUSABLE_MASK = 0x00000001;
620
621 /**
622 * This view will adjust its padding to fit sytem windows (e.g. status bar)
623 */
624 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
625
626 /**
627 * This view is visible. Use with {@link #setVisibility}.
628 */
629 public static final int VISIBLE = 0x00000000;
630
631 /**
632 * This view is invisible, but it still takes up space for layout purposes.
633 * Use with {@link #setVisibility}.
634 */
635 public static final int INVISIBLE = 0x00000004;
636
637 /**
638 * This view is invisible, and it doesn't take any space for layout
639 * purposes. Use with {@link #setVisibility}.
640 */
641 public static final int GONE = 0x00000008;
642
643 /**
644 * Mask for use with setFlags indicating bits used for visibility.
645 * {@hide}
646 */
647 static final int VISIBILITY_MASK = 0x0000000C;
648
649 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
650
651 /**
652 * This view is enabled. Intrepretation varies by subclass.
653 * Use with ENABLED_MASK when calling setFlags.
654 * {@hide}
655 */
656 static final int ENABLED = 0x00000000;
657
658 /**
659 * This view is disabled. Intrepretation varies by subclass.
660 * Use with ENABLED_MASK when calling setFlags.
661 * {@hide}
662 */
663 static final int DISABLED = 0x00000020;
664
665 /**
666 * Mask for use with setFlags indicating bits used for indicating whether
667 * this view is enabled
668 * {@hide}
669 */
670 static final int ENABLED_MASK = 0x00000020;
671
672 /**
673 * This view won't draw. {@link #onDraw} won't be called and further
674 * optimizations
675 * will be performed. It is okay to have this flag set and a background.
676 * Use with DRAW_MASK when calling setFlags.
677 * {@hide}
678 */
679 static final int WILL_NOT_DRAW = 0x00000080;
680
681 /**
682 * Mask for use with setFlags indicating bits used for indicating whether
683 * this view is will draw
684 * {@hide}
685 */
686 static final int DRAW_MASK = 0x00000080;
687
688 /**
689 * <p>This view doesn't show scrollbars.</p>
690 * {@hide}
691 */
692 static final int SCROLLBARS_NONE = 0x00000000;
693
694 /**
695 * <p>This view shows horizontal scrollbars.</p>
696 * {@hide}
697 */
698 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
699
700 /**
701 * <p>This view shows vertical scrollbars.</p>
702 * {@hide}
703 */
704 static final int SCROLLBARS_VERTICAL = 0x00000200;
705
706 /**
707 * <p>Mask for use with setFlags indicating bits used for indicating which
708 * scrollbars are enabled.</p>
709 * {@hide}
710 */
711 static final int SCROLLBARS_MASK = 0x00000300;
712
713 // note 0x00000400 and 0x00000800 are now available for next flags...
714
715 /**
716 * <p>This view doesn't show fading edges.</p>
717 * {@hide}
718 */
719 static final int FADING_EDGE_NONE = 0x00000000;
720
721 /**
722 * <p>This view shows horizontal fading edges.</p>
723 * {@hide}
724 */
725 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
726
727 /**
728 * <p>This view shows vertical fading edges.</p>
729 * {@hide}
730 */
731 static final int FADING_EDGE_VERTICAL = 0x00002000;
732
733 /**
734 * <p>Mask for use with setFlags indicating bits used for indicating which
735 * fading edges are enabled.</p>
736 * {@hide}
737 */
738 static final int FADING_EDGE_MASK = 0x00003000;
739
740 /**
741 * <p>Indicates this view can be clicked. When clickable, a View reacts
742 * to clicks by notifying the OnClickListener.<p>
743 * {@hide}
744 */
745 static final int CLICKABLE = 0x00004000;
746
747 /**
748 * <p>Indicates this view is caching its drawing into a bitmap.</p>
749 * {@hide}
750 */
751 static final int DRAWING_CACHE_ENABLED = 0x00008000;
752
753 /**
754 * <p>Indicates that no icicle should be saved for this view.<p>
755 * {@hide}
756 */
757 static final int SAVE_DISABLED = 0x000010000;
758
759 /**
760 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
761 * property.</p>
762 * {@hide}
763 */
764 static final int SAVE_DISABLED_MASK = 0x000010000;
765
766 /**
767 * <p>Indicates that no drawing cache should ever be created for this view.<p>
768 * {@hide}
769 */
770 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
771
772 /**
773 * <p>Indicates this view can take / keep focus when int touch mode.</p>
774 * {@hide}
775 */
776 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
777
778 /**
779 * <p>Enables low quality mode for the drawing cache.</p>
780 */
781 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
782
783 /**
784 * <p>Enables high quality mode for the drawing cache.</p>
785 */
786 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
787
788 /**
789 * <p>Enables automatic quality mode for the drawing cache.</p>
790 */
791 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
792
793 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
794 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
795 };
796
797 /**
798 * <p>Mask for use with setFlags indicating bits used for the cache
799 * quality property.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
803
804 /**
805 * <p>
806 * Indicates this view can be long clicked. When long clickable, a View
807 * reacts to long clicks by notifying the OnLongClickListener or showing a
808 * context menu.
809 * </p>
810 * {@hide}
811 */
812 static final int LONG_CLICKABLE = 0x00200000;
813
814 /**
815 * <p>Indicates that this view gets its drawable states from its direct parent
816 * and ignores its original internal states.</p>
817 *
818 * @hide
819 */
820 static final int DUPLICATE_PARENT_STATE = 0x00400000;
821
822 /**
823 * The scrollbar style to display the scrollbars inside the content area,
824 * without increasing the padding. The scrollbars will be overlaid with
825 * translucency on the view's content.
826 */
827 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
828
829 /**
830 * The scrollbar style to display the scrollbars inside the padded area,
831 * increasing the padding of the view. The scrollbars will not overlap the
832 * content area of the view.
833 */
834 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
835
836 /**
837 * The scrollbar style to display the scrollbars at the edge of the view,
838 * without increasing the padding. The scrollbars will be overlaid with
839 * translucency.
840 */
841 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
842
843 /**
844 * The scrollbar style to display the scrollbars at the edge of the view,
845 * increasing the padding of the view. The scrollbars will only overlap the
846 * background, if any.
847 */
848 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
849
850 /**
851 * Mask to check if the scrollbar style is overlay or inset.
852 * {@hide}
853 */
854 static final int SCROLLBARS_INSET_MASK = 0x01000000;
855
856 /**
857 * Mask to check if the scrollbar style is inside or outside.
858 * {@hide}
859 */
860 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
861
862 /**
863 * Mask for scrollbar style.
864 * {@hide}
865 */
866 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
867
868 /**
869 * View flag indicating that the screen should remain on while the
870 * window containing this view is visible to the user. This effectively
871 * takes care of automatically setting the WindowManager's
872 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
873 */
874 public static final int KEEP_SCREEN_ON = 0x04000000;
875
876 /**
877 * View flag indicating whether this view should have sound effects enabled
878 * for events such as clicking and touching.
879 */
880 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
881
882 /**
883 * View flag indicating whether this view should have haptic feedback
884 * enabled for events such as long presses.
885 */
886 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
887
888 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700889 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
890 * should add all focusable Views regardless if they are focusable in touch mode.
891 */
892 public static final int FOCUSABLES_ALL = 0x00000000;
893
894 /**
895 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
896 * should add only Views focusable in touch mode.
897 */
898 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
899
900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * Use with {@link #focusSearch}. Move focus to the previous selectable
902 * item.
903 */
904 public static final int FOCUS_BACKWARD = 0x00000001;
905
906 /**
907 * Use with {@link #focusSearch}. Move focus to the next selectable
908 * item.
909 */
910 public static final int FOCUS_FORWARD = 0x00000002;
911
912 /**
913 * Use with {@link #focusSearch}. Move focus to the left.
914 */
915 public static final int FOCUS_LEFT = 0x00000011;
916
917 /**
918 * Use with {@link #focusSearch}. Move focus up.
919 */
920 public static final int FOCUS_UP = 0x00000021;
921
922 /**
923 * Use with {@link #focusSearch}. Move focus to the right.
924 */
925 public static final int FOCUS_RIGHT = 0x00000042;
926
927 /**
928 * Use with {@link #focusSearch}. Move focus down.
929 */
930 public static final int FOCUS_DOWN = 0x00000082;
931
932 /**
933 * Base View state sets
934 */
935 // Singles
936 /**
937 * Indicates the view has no states set. States are used with
938 * {@link android.graphics.drawable.Drawable} to change the drawing of the
939 * view depending on its state.
940 *
941 * @see android.graphics.drawable.Drawable
942 * @see #getDrawableState()
943 */
944 protected static final int[] EMPTY_STATE_SET = {};
945 /**
946 * Indicates the view is enabled. States are used with
947 * {@link android.graphics.drawable.Drawable} to change the drawing of the
948 * view depending on its state.
949 *
950 * @see android.graphics.drawable.Drawable
951 * @see #getDrawableState()
952 */
953 protected static final int[] ENABLED_STATE_SET = {R.attr.state_enabled};
954 /**
955 * Indicates the view is focused. States are used with
956 * {@link android.graphics.drawable.Drawable} to change the drawing of the
957 * view depending on its state.
958 *
959 * @see android.graphics.drawable.Drawable
960 * @see #getDrawableState()
961 */
962 protected static final int[] FOCUSED_STATE_SET = {R.attr.state_focused};
963 /**
964 * Indicates the view is selected. States are used with
965 * {@link android.graphics.drawable.Drawable} to change the drawing of the
966 * view depending on its state.
967 *
968 * @see android.graphics.drawable.Drawable
969 * @see #getDrawableState()
970 */
971 protected static final int[] SELECTED_STATE_SET = {R.attr.state_selected};
972 /**
973 * Indicates the view is pressed. States are used with
974 * {@link android.graphics.drawable.Drawable} to change the drawing of the
975 * view depending on its state.
976 *
977 * @see android.graphics.drawable.Drawable
978 * @see #getDrawableState()
979 * @hide
980 */
981 protected static final int[] PRESSED_STATE_SET = {R.attr.state_pressed};
982 /**
983 * Indicates the view's window has focus. States are used with
984 * {@link android.graphics.drawable.Drawable} to change the drawing of the
985 * view depending on its state.
986 *
987 * @see android.graphics.drawable.Drawable
988 * @see #getDrawableState()
989 */
990 protected static final int[] WINDOW_FOCUSED_STATE_SET =
991 {R.attr.state_window_focused};
992 // Doubles
993 /**
994 * Indicates the view is enabled and has the focus.
995 *
996 * @see #ENABLED_STATE_SET
997 * @see #FOCUSED_STATE_SET
998 */
999 protected static final int[] ENABLED_FOCUSED_STATE_SET =
1000 stateSetUnion(ENABLED_STATE_SET, FOCUSED_STATE_SET);
1001 /**
1002 * Indicates the view is enabled and selected.
1003 *
1004 * @see #ENABLED_STATE_SET
1005 * @see #SELECTED_STATE_SET
1006 */
1007 protected static final int[] ENABLED_SELECTED_STATE_SET =
1008 stateSetUnion(ENABLED_STATE_SET, SELECTED_STATE_SET);
1009 /**
1010 * Indicates the view is enabled and that its window has focus.
1011 *
1012 * @see #ENABLED_STATE_SET
1013 * @see #WINDOW_FOCUSED_STATE_SET
1014 */
1015 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET =
1016 stateSetUnion(ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1017 /**
1018 * Indicates the view is focused and selected.
1019 *
1020 * @see #FOCUSED_STATE_SET
1021 * @see #SELECTED_STATE_SET
1022 */
1023 protected static final int[] FOCUSED_SELECTED_STATE_SET =
1024 stateSetUnion(FOCUSED_STATE_SET, SELECTED_STATE_SET);
1025 /**
1026 * Indicates the view has the focus and that its window has the focus.
1027 *
1028 * @see #FOCUSED_STATE_SET
1029 * @see #WINDOW_FOCUSED_STATE_SET
1030 */
1031 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET =
1032 stateSetUnion(FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1033 /**
1034 * Indicates the view is selected and that its window has the focus.
1035 *
1036 * @see #SELECTED_STATE_SET
1037 * @see #WINDOW_FOCUSED_STATE_SET
1038 */
1039 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET =
1040 stateSetUnion(SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1041 // Triples
1042 /**
1043 * Indicates the view is enabled, focused and selected.
1044 *
1045 * @see #ENABLED_STATE_SET
1046 * @see #FOCUSED_STATE_SET
1047 * @see #SELECTED_STATE_SET
1048 */
1049 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET =
1050 stateSetUnion(ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1051 /**
1052 * Indicates the view is enabled, focused and its window has the focus.
1053 *
1054 * @see #ENABLED_STATE_SET
1055 * @see #FOCUSED_STATE_SET
1056 * @see #WINDOW_FOCUSED_STATE_SET
1057 */
1058 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1059 stateSetUnion(ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1060 /**
1061 * Indicates the view is enabled, selected and its window has the focus.
1062 *
1063 * @see #ENABLED_STATE_SET
1064 * @see #SELECTED_STATE_SET
1065 * @see #WINDOW_FOCUSED_STATE_SET
1066 */
1067 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1068 stateSetUnion(ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1069 /**
1070 * Indicates the view is focused, selected and its window has the focus.
1071 *
1072 * @see #FOCUSED_STATE_SET
1073 * @see #SELECTED_STATE_SET
1074 * @see #WINDOW_FOCUSED_STATE_SET
1075 */
1076 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1077 stateSetUnion(FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1078 /**
1079 * Indicates the view is enabled, focused, selected and its window
1080 * has the focus.
1081 *
1082 * @see #ENABLED_STATE_SET
1083 * @see #FOCUSED_STATE_SET
1084 * @see #SELECTED_STATE_SET
1085 * @see #WINDOW_FOCUSED_STATE_SET
1086 */
1087 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1088 stateSetUnion(ENABLED_FOCUSED_SELECTED_STATE_SET,
1089 WINDOW_FOCUSED_STATE_SET);
1090
1091 /**
1092 * Indicates the view is pressed and its window has the focus.
1093 *
1094 * @see #PRESSED_STATE_SET
1095 * @see #WINDOW_FOCUSED_STATE_SET
1096 */
1097 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET =
1098 stateSetUnion(PRESSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1099
1100 /**
1101 * Indicates the view is pressed and selected.
1102 *
1103 * @see #PRESSED_STATE_SET
1104 * @see #SELECTED_STATE_SET
1105 */
1106 protected static final int[] PRESSED_SELECTED_STATE_SET =
1107 stateSetUnion(PRESSED_STATE_SET, SELECTED_STATE_SET);
1108
1109 /**
1110 * Indicates the view is pressed, selected and its window has the focus.
1111 *
1112 * @see #PRESSED_STATE_SET
1113 * @see #SELECTED_STATE_SET
1114 * @see #WINDOW_FOCUSED_STATE_SET
1115 */
1116 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1117 stateSetUnion(PRESSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1118
1119 /**
1120 * Indicates the view is pressed and focused.
1121 *
1122 * @see #PRESSED_STATE_SET
1123 * @see #FOCUSED_STATE_SET
1124 */
1125 protected static final int[] PRESSED_FOCUSED_STATE_SET =
1126 stateSetUnion(PRESSED_STATE_SET, FOCUSED_STATE_SET);
1127
1128 /**
1129 * Indicates the view is pressed, focused and its window has the focus.
1130 *
1131 * @see #PRESSED_STATE_SET
1132 * @see #FOCUSED_STATE_SET
1133 * @see #WINDOW_FOCUSED_STATE_SET
1134 */
1135 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1136 stateSetUnion(PRESSED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1137
1138 /**
1139 * Indicates the view is pressed, focused and selected.
1140 *
1141 * @see #PRESSED_STATE_SET
1142 * @see #SELECTED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 */
1145 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET =
1146 stateSetUnion(PRESSED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1147
1148 /**
1149 * Indicates the view is pressed, focused, selected and its window has the focus.
1150 *
1151 * @see #PRESSED_STATE_SET
1152 * @see #FOCUSED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
1156 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1157 stateSetUnion(PRESSED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1158
1159 /**
1160 * Indicates the view is pressed and enabled.
1161 *
1162 * @see #PRESSED_STATE_SET
1163 * @see #ENABLED_STATE_SET
1164 */
1165 protected static final int[] PRESSED_ENABLED_STATE_SET =
1166 stateSetUnion(PRESSED_STATE_SET, ENABLED_STATE_SET);
1167
1168 /**
1169 * Indicates the view is pressed, enabled and its window has the focus.
1170 *
1171 * @see #PRESSED_STATE_SET
1172 * @see #ENABLED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
1175 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET =
1176 stateSetUnion(PRESSED_ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1177
1178 /**
1179 * Indicates the view is pressed, enabled and selected.
1180 *
1181 * @see #PRESSED_STATE_SET
1182 * @see #ENABLED_STATE_SET
1183 * @see #SELECTED_STATE_SET
1184 */
1185 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET =
1186 stateSetUnion(PRESSED_ENABLED_STATE_SET, SELECTED_STATE_SET);
1187
1188 /**
1189 * Indicates the view is pressed, enabled, selected and its window has the
1190 * focus.
1191 *
1192 * @see #PRESSED_STATE_SET
1193 * @see #ENABLED_STATE_SET
1194 * @see #SELECTED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
1197 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1198 stateSetUnion(PRESSED_ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1199
1200 /**
1201 * Indicates the view is pressed, enabled and focused.
1202 *
1203 * @see #PRESSED_STATE_SET
1204 * @see #ENABLED_STATE_SET
1205 * @see #FOCUSED_STATE_SET
1206 */
1207 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET =
1208 stateSetUnion(PRESSED_ENABLED_STATE_SET, FOCUSED_STATE_SET);
1209
1210 /**
1211 * Indicates the view is pressed, enabled, focused and its window has the
1212 * focus.
1213 *
1214 * @see #PRESSED_STATE_SET
1215 * @see #ENABLED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #WINDOW_FOCUSED_STATE_SET
1218 */
1219 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1220 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1221
1222 /**
1223 * Indicates the view is pressed, enabled, focused and selected.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #ENABLED_STATE_SET
1227 * @see #SELECTED_STATE_SET
1228 * @see #FOCUSED_STATE_SET
1229 */
1230 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET =
1231 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1232
1233 /**
1234 * Indicates the view is pressed, enabled, focused, selected and its window
1235 * has the focus.
1236 *
1237 * @see #PRESSED_STATE_SET
1238 * @see #ENABLED_STATE_SET
1239 * @see #SELECTED_STATE_SET
1240 * @see #FOCUSED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
1243 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1244 stateSetUnion(PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1245
1246 /**
1247 * The order here is very important to {@link #getDrawableState()}
1248 */
1249 private static final int[][] VIEW_STATE_SETS = {
1250 EMPTY_STATE_SET, // 0 0 0 0 0
1251 WINDOW_FOCUSED_STATE_SET, // 0 0 0 0 1
1252 SELECTED_STATE_SET, // 0 0 0 1 0
1253 SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 0 1 1
1254 FOCUSED_STATE_SET, // 0 0 1 0 0
1255 FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 0 1
1256 FOCUSED_SELECTED_STATE_SET, // 0 0 1 1 0
1257 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 1 1
1258 ENABLED_STATE_SET, // 0 1 0 0 0
1259 ENABLED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 0 1
1260 ENABLED_SELECTED_STATE_SET, // 0 1 0 1 0
1261 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 1 1
1262 ENABLED_FOCUSED_STATE_SET, // 0 1 1 0 0
1263 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 0 1
1264 ENABLED_FOCUSED_SELECTED_STATE_SET, // 0 1 1 1 0
1265 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 1 1
1266 PRESSED_STATE_SET, // 1 0 0 0 0
1267 PRESSED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 0 1
1268 PRESSED_SELECTED_STATE_SET, // 1 0 0 1 0
1269 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 1 1
1270 PRESSED_FOCUSED_STATE_SET, // 1 0 1 0 0
1271 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 0 1
1272 PRESSED_FOCUSED_SELECTED_STATE_SET, // 1 0 1 1 0
1273 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 1 1
1274 PRESSED_ENABLED_STATE_SET, // 1 1 0 0 0
1275 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 0 1
1276 PRESSED_ENABLED_SELECTED_STATE_SET, // 1 1 0 1 0
1277 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 1 1
1278 PRESSED_ENABLED_FOCUSED_STATE_SET, // 1 1 1 0 0
1279 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 0 1
1280 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, // 1 1 1 1 0
1281 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 1 1
1282 };
1283
1284 /**
1285 * Used by views that contain lists of items. This state indicates that
1286 * the view is showing the last item.
1287 * @hide
1288 */
1289 protected static final int[] LAST_STATE_SET = {R.attr.state_last};
1290 /**
1291 * Used by views that contain lists of items. This state indicates that
1292 * the view is showing the first item.
1293 * @hide
1294 */
1295 protected static final int[] FIRST_STATE_SET = {R.attr.state_first};
1296 /**
1297 * Used by views that contain lists of items. This state indicates that
1298 * the view is showing the middle item.
1299 * @hide
1300 */
1301 protected static final int[] MIDDLE_STATE_SET = {R.attr.state_middle};
1302 /**
1303 * Used by views that contain lists of items. This state indicates that
1304 * the view is showing only one item.
1305 * @hide
1306 */
1307 protected static final int[] SINGLE_STATE_SET = {R.attr.state_single};
1308 /**
1309 * Used by views that contain lists of items. This state indicates that
1310 * the view is pressed and showing the last item.
1311 * @hide
1312 */
1313 protected static final int[] PRESSED_LAST_STATE_SET = {R.attr.state_last, R.attr.state_pressed};
1314 /**
1315 * Used by views that contain lists of items. This state indicates that
1316 * the view is pressed and showing the first item.
1317 * @hide
1318 */
1319 protected static final int[] PRESSED_FIRST_STATE_SET = {R.attr.state_first, R.attr.state_pressed};
1320 /**
1321 * Used by views that contain lists of items. This state indicates that
1322 * the view is pressed and showing the middle item.
1323 * @hide
1324 */
1325 protected static final int[] PRESSED_MIDDLE_STATE_SET = {R.attr.state_middle, R.attr.state_pressed};
1326 /**
1327 * Used by views that contain lists of items. This state indicates that
1328 * the view is pressed and showing only one item.
1329 * @hide
1330 */
1331 protected static final int[] PRESSED_SINGLE_STATE_SET = {R.attr.state_single, R.attr.state_pressed};
1332
1333 /**
1334 * Temporary Rect currently for use in setBackground(). This will probably
1335 * be extended in the future to hold our own class with more than just
1336 * a Rect. :)
1337 */
1338 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001339
1340 /**
1341 * Map used to store views' tags.
1342 */
1343 private static WeakHashMap<View, SparseArray<Object>> sTags;
1344
1345 /**
1346 * Lock used to access sTags.
1347 */
1348 private static final Object sTagsLock = new Object();
1349
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 /**
1351 * The animation currently associated with this view.
1352 * @hide
1353 */
1354 protected Animation mCurrentAnimation = null;
1355
1356 /**
1357 * Width as measured during measure pass.
1358 * {@hide}
1359 */
1360 @ViewDebug.ExportedProperty
1361 protected int mMeasuredWidth;
1362
1363 /**
1364 * Height as measured during measure pass.
1365 * {@hide}
1366 */
1367 @ViewDebug.ExportedProperty
1368 protected int mMeasuredHeight;
1369
1370 /**
1371 * The view's identifier.
1372 * {@hide}
1373 *
1374 * @see #setId(int)
1375 * @see #getId()
1376 */
1377 @ViewDebug.ExportedProperty(resolveId = true)
1378 int mID = NO_ID;
1379
1380 /**
1381 * The view's tag.
1382 * {@hide}
1383 *
1384 * @see #setTag(Object)
1385 * @see #getTag()
1386 */
1387 protected Object mTag;
1388
1389 // for mPrivateFlags:
1390 /** {@hide} */
1391 static final int WANTS_FOCUS = 0x00000001;
1392 /** {@hide} */
1393 static final int FOCUSED = 0x00000002;
1394 /** {@hide} */
1395 static final int SELECTED = 0x00000004;
1396 /** {@hide} */
1397 static final int IS_ROOT_NAMESPACE = 0x00000008;
1398 /** {@hide} */
1399 static final int HAS_BOUNDS = 0x00000010;
1400 /** {@hide} */
1401 static final int DRAWN = 0x00000020;
1402 /**
1403 * When this flag is set, this view is running an animation on behalf of its
1404 * children and should therefore not cancel invalidate requests, even if they
1405 * lie outside of this view's bounds.
1406 *
1407 * {@hide}
1408 */
1409 static final int DRAW_ANIMATION = 0x00000040;
1410 /** {@hide} */
1411 static final int SKIP_DRAW = 0x00000080;
1412 /** {@hide} */
1413 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1414 /** {@hide} */
1415 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1416 /** {@hide} */
1417 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1418 /** {@hide} */
1419 static final int MEASURED_DIMENSION_SET = 0x00000800;
1420 /** {@hide} */
1421 static final int FORCE_LAYOUT = 0x00001000;
1422
1423 private static final int LAYOUT_REQUIRED = 0x00002000;
1424
1425 private static final int PRESSED = 0x00004000;
1426
1427 /** {@hide} */
1428 static final int DRAWING_CACHE_VALID = 0x00008000;
1429 /**
1430 * Flag used to indicate that this view should be drawn once more (and only once
1431 * more) after its animation has completed.
1432 * {@hide}
1433 */
1434 static final int ANIMATION_STARTED = 0x00010000;
1435
1436 private static final int SAVE_STATE_CALLED = 0x00020000;
1437
1438 /**
1439 * Indicates that the View returned true when onSetAlpha() was called and that
1440 * the alpha must be restored.
1441 * {@hide}
1442 */
1443 static final int ALPHA_SET = 0x00040000;
1444
1445 /**
1446 * Set by {@link #setScrollContainer(boolean)}.
1447 */
1448 static final int SCROLL_CONTAINER = 0x00080000;
1449
1450 /**
1451 * Set by {@link #setScrollContainer(boolean)}.
1452 */
1453 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1454
1455 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001456 * View flag indicating whether this view was invalidated (fully or partially.)
1457 *
1458 * @hide
1459 */
1460 static final int DIRTY = 0x00200000;
1461
1462 /**
1463 * View flag indicating whether this view was invalidated by an opaque
1464 * invalidate request.
1465 *
1466 * @hide
1467 */
1468 static final int DIRTY_OPAQUE = 0x00400000;
1469
1470 /**
1471 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1472 *
1473 * @hide
1474 */
1475 static final int DIRTY_MASK = 0x00600000;
1476
1477 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001478 * Indicates whether the background is opaque.
1479 *
1480 * @hide
1481 */
1482 static final int OPAQUE_BACKGROUND = 0x00800000;
1483
1484 /**
1485 * Indicates whether the scrollbars are opaque.
1486 *
1487 * @hide
1488 */
1489 static final int OPAQUE_SCROLLBARS = 0x01000000;
1490
1491 /**
1492 * Indicates whether the view is opaque.
1493 *
1494 * @hide
1495 */
1496 static final int OPAQUE_MASK = 0x01800000;
1497
1498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 * The parent this view is attached to.
1500 * {@hide}
1501 *
1502 * @see #getParent()
1503 */
1504 protected ViewParent mParent;
1505
1506 /**
1507 * {@hide}
1508 */
1509 AttachInfo mAttachInfo;
1510
1511 /**
1512 * {@hide}
1513 */
Romain Guy809a7f62009-05-14 15:44:42 -07001514 @ViewDebug.ExportedProperty(flagMapping = {
1515 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1516 name = "FORCE_LAYOUT"),
1517 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1518 name = "LAYOUT_REQUIRED"),
1519 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001520 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001521 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1522 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1523 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1524 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1525 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001526 int mPrivateFlags;
1527
1528 /**
1529 * Count of how many windows this view has been attached to.
1530 */
1531 int mWindowAttachCount;
1532
1533 /**
1534 * The layout parameters associated with this view and used by the parent
1535 * {@link android.view.ViewGroup} to determine how this view should be
1536 * laid out.
1537 * {@hide}
1538 */
1539 protected ViewGroup.LayoutParams mLayoutParams;
1540
1541 /**
1542 * The view flags hold various views states.
1543 * {@hide}
1544 */
1545 @ViewDebug.ExportedProperty
1546 int mViewFlags;
1547
1548 /**
1549 * The distance in pixels from the left edge of this view's parent
1550 * to the left edge of this view.
1551 * {@hide}
1552 */
1553 @ViewDebug.ExportedProperty
1554 protected int mLeft;
1555 /**
1556 * The distance in pixels from the left edge of this view's parent
1557 * to the right edge of this view.
1558 * {@hide}
1559 */
1560 @ViewDebug.ExportedProperty
1561 protected int mRight;
1562 /**
1563 * The distance in pixels from the top edge of this view's parent
1564 * to the top edge of this view.
1565 * {@hide}
1566 */
1567 @ViewDebug.ExportedProperty
1568 protected int mTop;
1569 /**
1570 * The distance in pixels from the top edge of this view's parent
1571 * to the bottom edge of this view.
1572 * {@hide}
1573 */
1574 @ViewDebug.ExportedProperty
1575 protected int mBottom;
1576
1577 /**
1578 * The offset, in pixels, by which the content of this view is scrolled
1579 * horizontally.
1580 * {@hide}
1581 */
1582 @ViewDebug.ExportedProperty
1583 protected int mScrollX;
1584 /**
1585 * The offset, in pixels, by which the content of this view is scrolled
1586 * vertically.
1587 * {@hide}
1588 */
1589 @ViewDebug.ExportedProperty
1590 protected int mScrollY;
1591
1592 /**
1593 * The left padding in pixels, that is the distance in pixels between the
1594 * left edge of this view and the left edge of its content.
1595 * {@hide}
1596 */
1597 @ViewDebug.ExportedProperty
1598 protected int mPaddingLeft;
1599 /**
1600 * The right padding in pixels, that is the distance in pixels between the
1601 * right edge of this view and the right edge of its content.
1602 * {@hide}
1603 */
1604 @ViewDebug.ExportedProperty
1605 protected int mPaddingRight;
1606 /**
1607 * The top padding in pixels, that is the distance in pixels between the
1608 * top edge of this view and the top edge of its content.
1609 * {@hide}
1610 */
1611 @ViewDebug.ExportedProperty
1612 protected int mPaddingTop;
1613 /**
1614 * The bottom padding in pixels, that is the distance in pixels between the
1615 * bottom edge of this view and the bottom edge of its content.
1616 * {@hide}
1617 */
1618 @ViewDebug.ExportedProperty
1619 protected int mPaddingBottom;
1620
1621 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001622 * Briefly describes the view and is primarily used for accessibility support.
1623 */
1624 private CharSequence mContentDescription;
1625
1626 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 * Cache the paddingRight set by the user to append to the scrollbar's size.
1628 */
1629 @ViewDebug.ExportedProperty
1630 int mUserPaddingRight;
1631
1632 /**
1633 * Cache the paddingBottom set by the user to append to the scrollbar's size.
1634 */
1635 @ViewDebug.ExportedProperty
1636 int mUserPaddingBottom;
1637
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001638 /**
1639 * @hide
1640 */
1641 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
1642 /**
1643 * @hide
1644 */
1645 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646
1647 private Resources mResources = null;
1648
1649 private Drawable mBGDrawable;
1650
1651 private int mBackgroundResource;
1652 private boolean mBackgroundSizeChanged;
1653
1654 /**
1655 * Listener used to dispatch focus change events.
1656 * This field should be made private, so it is hidden from the SDK.
1657 * {@hide}
1658 */
1659 protected OnFocusChangeListener mOnFocusChangeListener;
1660
1661 /**
1662 * Listener used to dispatch click events.
1663 * This field should be made private, so it is hidden from the SDK.
1664 * {@hide}
1665 */
1666 protected OnClickListener mOnClickListener;
1667
1668 /**
1669 * Listener used to dispatch long click events.
1670 * This field should be made private, so it is hidden from the SDK.
1671 * {@hide}
1672 */
1673 protected OnLongClickListener mOnLongClickListener;
1674
1675 /**
1676 * Listener used to build the context menu.
1677 * This field should be made private, so it is hidden from the SDK.
1678 * {@hide}
1679 */
1680 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
1681
1682 private OnKeyListener mOnKeyListener;
1683
1684 private OnTouchListener mOnTouchListener;
1685
1686 /**
1687 * The application environment this view lives in.
1688 * This field should be made private, so it is hidden from the SDK.
1689 * {@hide}
1690 */
1691 protected Context mContext;
1692
1693 private ScrollabilityCache mScrollCache;
1694
1695 private int[] mDrawableState = null;
1696
1697 private SoftReference<Bitmap> mDrawingCache;
Romain Guyfbd8f692009-06-26 14:51:58 -07001698 private SoftReference<Bitmap> mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699
1700 /**
1701 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
1702 * the user may specify which view to go to next.
1703 */
1704 private int mNextFocusLeftId = View.NO_ID;
1705
1706 /**
1707 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
1708 * the user may specify which view to go to next.
1709 */
1710 private int mNextFocusRightId = View.NO_ID;
1711
1712 /**
1713 * When this view has focus and the next focus is {@link #FOCUS_UP},
1714 * the user may specify which view to go to next.
1715 */
1716 private int mNextFocusUpId = View.NO_ID;
1717
1718 /**
1719 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
1720 * the user may specify which view to go to next.
1721 */
1722 private int mNextFocusDownId = View.NO_ID;
1723
1724 private CheckForLongPress mPendingCheckForLongPress;
1725 private UnsetPressedState mUnsetPressedState;
1726
1727 /**
1728 * Whether the long press's action has been invoked. The tap's action is invoked on the
1729 * up event while a long press is invoked as soon as the long press duration is reached, so
1730 * a long press could be performed before the tap is checked, in which case the tap's action
1731 * should not be invoked.
1732 */
1733 private boolean mHasPerformedLongPress;
1734
1735 /**
1736 * The minimum height of the view. We'll try our best to have the height
1737 * of this view to at least this amount.
1738 */
1739 @ViewDebug.ExportedProperty
1740 private int mMinHeight;
1741
1742 /**
1743 * The minimum width of the view. We'll try our best to have the width
1744 * of this view to at least this amount.
1745 */
1746 @ViewDebug.ExportedProperty
1747 private int mMinWidth;
1748
1749 /**
1750 * The delegate to handle touch events that are physically in this view
1751 * but should be handled by another view.
1752 */
1753 private TouchDelegate mTouchDelegate = null;
1754
1755 /**
1756 * Solid color to use as a background when creating the drawing cache. Enables
1757 * the cache to use 16 bit bitmaps instead of 32 bit.
1758 */
1759 private int mDrawingCacheBackgroundColor = 0;
1760
1761 /**
1762 * Special tree observer used when mAttachInfo is null.
1763 */
1764 private ViewTreeObserver mFloatingTreeObserver;
1765
1766 // Used for debug only
1767 static long sInstanceCount = 0;
1768
1769 /**
1770 * Simple constructor to use when creating a view from code.
1771 *
1772 * @param context The Context the view is running in, through which it can
1773 * access the current theme, resources, etc.
1774 */
1775 public View(Context context) {
1776 mContext = context;
1777 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07001778 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001779 ++sInstanceCount;
1780 }
1781
1782 /**
1783 * Constructor that is called when inflating a view from XML. This is called
1784 * when a view is being constructed from an XML file, supplying attributes
1785 * that were specified in the XML file. This version uses a default style of
1786 * 0, so the only attribute values applied are those in the Context's Theme
1787 * and the given AttributeSet.
1788 *
1789 * <p>
1790 * The method onFinishInflate() will be called after all children have been
1791 * added.
1792 *
1793 * @param context The Context the view is running in, through which it can
1794 * access the current theme, resources, etc.
1795 * @param attrs The attributes of the XML tag that is inflating the view.
1796 * @see #View(Context, AttributeSet, int)
1797 */
1798 public View(Context context, AttributeSet attrs) {
1799 this(context, attrs, 0);
1800 }
1801
1802 /**
1803 * Perform inflation from XML and apply a class-specific base style. This
1804 * constructor of View allows subclasses to use their own base style when
1805 * they are inflating. For example, a Button class's constructor would call
1806 * this version of the super class constructor and supply
1807 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
1808 * the theme's button style to modify all of the base view attributes (in
1809 * particular its background) as well as the Button class's attributes.
1810 *
1811 * @param context The Context the view is running in, through which it can
1812 * access the current theme, resources, etc.
1813 * @param attrs The attributes of the XML tag that is inflating the view.
1814 * @param defStyle The default style to apply to this view. If 0, no style
1815 * will be applied (beyond what is included in the theme). This may
1816 * either be an attribute resource, whose value will be retrieved
1817 * from the current theme, or an explicit style resource.
1818 * @see #View(Context, AttributeSet)
1819 */
1820 public View(Context context, AttributeSet attrs, int defStyle) {
1821 this(context);
1822
1823 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
1824 defStyle, 0);
1825
1826 Drawable background = null;
1827
1828 int leftPadding = -1;
1829 int topPadding = -1;
1830 int rightPadding = -1;
1831 int bottomPadding = -1;
1832
1833 int padding = -1;
1834
1835 int viewFlagValues = 0;
1836 int viewFlagMasks = 0;
1837
1838 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 int x = 0;
1841 int y = 0;
1842
1843 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
1844
1845 final int N = a.getIndexCount();
1846 for (int i = 0; i < N; i++) {
1847 int attr = a.getIndex(i);
1848 switch (attr) {
1849 case com.android.internal.R.styleable.View_background:
1850 background = a.getDrawable(attr);
1851 break;
1852 case com.android.internal.R.styleable.View_padding:
1853 padding = a.getDimensionPixelSize(attr, -1);
1854 break;
1855 case com.android.internal.R.styleable.View_paddingLeft:
1856 leftPadding = a.getDimensionPixelSize(attr, -1);
1857 break;
1858 case com.android.internal.R.styleable.View_paddingTop:
1859 topPadding = a.getDimensionPixelSize(attr, -1);
1860 break;
1861 case com.android.internal.R.styleable.View_paddingRight:
1862 rightPadding = a.getDimensionPixelSize(attr, -1);
1863 break;
1864 case com.android.internal.R.styleable.View_paddingBottom:
1865 bottomPadding = a.getDimensionPixelSize(attr, -1);
1866 break;
1867 case com.android.internal.R.styleable.View_scrollX:
1868 x = a.getDimensionPixelOffset(attr, 0);
1869 break;
1870 case com.android.internal.R.styleable.View_scrollY:
1871 y = a.getDimensionPixelOffset(attr, 0);
1872 break;
1873 case com.android.internal.R.styleable.View_id:
1874 mID = a.getResourceId(attr, NO_ID);
1875 break;
1876 case com.android.internal.R.styleable.View_tag:
1877 mTag = a.getText(attr);
1878 break;
1879 case com.android.internal.R.styleable.View_fitsSystemWindows:
1880 if (a.getBoolean(attr, false)) {
1881 viewFlagValues |= FITS_SYSTEM_WINDOWS;
1882 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
1883 }
1884 break;
1885 case com.android.internal.R.styleable.View_focusable:
1886 if (a.getBoolean(attr, false)) {
1887 viewFlagValues |= FOCUSABLE;
1888 viewFlagMasks |= FOCUSABLE_MASK;
1889 }
1890 break;
1891 case com.android.internal.R.styleable.View_focusableInTouchMode:
1892 if (a.getBoolean(attr, false)) {
1893 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
1894 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
1895 }
1896 break;
1897 case com.android.internal.R.styleable.View_clickable:
1898 if (a.getBoolean(attr, false)) {
1899 viewFlagValues |= CLICKABLE;
1900 viewFlagMasks |= CLICKABLE;
1901 }
1902 break;
1903 case com.android.internal.R.styleable.View_longClickable:
1904 if (a.getBoolean(attr, false)) {
1905 viewFlagValues |= LONG_CLICKABLE;
1906 viewFlagMasks |= LONG_CLICKABLE;
1907 }
1908 break;
1909 case com.android.internal.R.styleable.View_saveEnabled:
1910 if (!a.getBoolean(attr, true)) {
1911 viewFlagValues |= SAVE_DISABLED;
1912 viewFlagMasks |= SAVE_DISABLED_MASK;
1913 }
1914 break;
1915 case com.android.internal.R.styleable.View_duplicateParentState:
1916 if (a.getBoolean(attr, false)) {
1917 viewFlagValues |= DUPLICATE_PARENT_STATE;
1918 viewFlagMasks |= DUPLICATE_PARENT_STATE;
1919 }
1920 break;
1921 case com.android.internal.R.styleable.View_visibility:
1922 final int visibility = a.getInt(attr, 0);
1923 if (visibility != 0) {
1924 viewFlagValues |= VISIBILITY_FLAGS[visibility];
1925 viewFlagMasks |= VISIBILITY_MASK;
1926 }
1927 break;
1928 case com.android.internal.R.styleable.View_drawingCacheQuality:
1929 final int cacheQuality = a.getInt(attr, 0);
1930 if (cacheQuality != 0) {
1931 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
1932 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
1933 }
1934 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07001935 case com.android.internal.R.styleable.View_contentDescription:
1936 mContentDescription = a.getString(attr);
1937 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 case com.android.internal.R.styleable.View_soundEffectsEnabled:
1939 if (!a.getBoolean(attr, true)) {
1940 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
1941 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
1942 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07001943 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001944 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
1945 if (!a.getBoolean(attr, true)) {
1946 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
1947 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
1948 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07001949 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001950 case R.styleable.View_scrollbars:
1951 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
1952 if (scrollbars != SCROLLBARS_NONE) {
1953 viewFlagValues |= scrollbars;
1954 viewFlagMasks |= SCROLLBARS_MASK;
1955 initializeScrollbars(a);
1956 }
1957 break;
1958 case R.styleable.View_fadingEdge:
1959 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
1960 if (fadingEdge != FADING_EDGE_NONE) {
1961 viewFlagValues |= fadingEdge;
1962 viewFlagMasks |= FADING_EDGE_MASK;
1963 initializeFadingEdge(a);
1964 }
1965 break;
1966 case R.styleable.View_scrollbarStyle:
1967 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
1968 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
1969 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
1970 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
1971 }
1972 break;
1973 case R.styleable.View_isScrollContainer:
1974 setScrollContainer = true;
1975 if (a.getBoolean(attr, false)) {
1976 setScrollContainer(true);
1977 }
1978 break;
1979 case com.android.internal.R.styleable.View_keepScreenOn:
1980 if (a.getBoolean(attr, false)) {
1981 viewFlagValues |= KEEP_SCREEN_ON;
1982 viewFlagMasks |= KEEP_SCREEN_ON;
1983 }
1984 break;
1985 case R.styleable.View_nextFocusLeft:
1986 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
1987 break;
1988 case R.styleable.View_nextFocusRight:
1989 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
1990 break;
1991 case R.styleable.View_nextFocusUp:
1992 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
1993 break;
1994 case R.styleable.View_nextFocusDown:
1995 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
1996 break;
1997 case R.styleable.View_minWidth:
1998 mMinWidth = a.getDimensionPixelSize(attr, 0);
1999 break;
2000 case R.styleable.View_minHeight:
2001 mMinHeight = a.getDimensionPixelSize(attr, 0);
2002 break;
Romain Guy9a817362009-05-01 10:57:14 -07002003 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002004 if (context.isRestricted()) {
2005 throw new IllegalStateException("The android:onClick attribute cannot "
2006 + "be used within a restricted context");
2007 }
2008
Romain Guy9a817362009-05-01 10:57:14 -07002009 final String handlerName = a.getString(attr);
2010 if (handlerName != null) {
2011 setOnClickListener(new OnClickListener() {
2012 private Method mHandler;
2013
2014 public void onClick(View v) {
2015 if (mHandler == null) {
2016 try {
2017 mHandler = getContext().getClass().getMethod(handlerName,
2018 View.class);
2019 } catch (NoSuchMethodException e) {
2020 throw new IllegalStateException("Could not find a method " +
2021 handlerName + "(View) in the activity", e);
2022 }
2023 }
2024
2025 try {
2026 mHandler.invoke(getContext(), View.this);
2027 } catch (IllegalAccessException e) {
2028 throw new IllegalStateException("Could not execute non "
2029 + "public method of the activity", e);
2030 } catch (InvocationTargetException e) {
2031 throw new IllegalStateException("Could not execute "
2032 + "method of the activity", e);
2033 }
2034 }
2035 });
2036 }
2037 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 }
2039 }
2040
2041 if (background != null) {
2042 setBackgroundDrawable(background);
2043 }
2044
2045 if (padding >= 0) {
2046 leftPadding = padding;
2047 topPadding = padding;
2048 rightPadding = padding;
2049 bottomPadding = padding;
2050 }
2051
2052 // If the user specified the padding (either with android:padding or
2053 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2054 // use the default padding or the padding from the background drawable
2055 // (stored at this point in mPadding*)
2056 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2057 topPadding >= 0 ? topPadding : mPaddingTop,
2058 rightPadding >= 0 ? rightPadding : mPaddingRight,
2059 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2060
2061 if (viewFlagMasks != 0) {
2062 setFlags(viewFlagValues, viewFlagMasks);
2063 }
2064
2065 // Needs to be called after mViewFlags is set
2066 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2067 recomputePadding();
2068 }
2069
2070 if (x != 0 || y != 0) {
2071 scrollTo(x, y);
2072 }
2073
2074 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2075 setScrollContainer(true);
2076 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002077
2078 computeOpaqueFlags();
2079
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002080 a.recycle();
2081 }
2082
2083 /**
2084 * Non-public constructor for use in testing
2085 */
2086 View() {
2087 }
2088
2089 @Override
2090 protected void finalize() throws Throwable {
2091 super.finalize();
2092 --sInstanceCount;
2093 }
2094
2095 /**
2096 * <p>
2097 * Initializes the fading edges from a given set of styled attributes. This
2098 * method should be called by subclasses that need fading edges and when an
2099 * instance of these subclasses is created programmatically rather than
2100 * being inflated from XML. This method is automatically called when the XML
2101 * is inflated.
2102 * </p>
2103 *
2104 * @param a the styled attributes set to initialize the fading edges from
2105 */
2106 protected void initializeFadingEdge(TypedArray a) {
2107 initScrollCache();
2108
2109 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2110 R.styleable.View_fadingEdgeLength,
2111 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2112 }
2113
2114 /**
2115 * Returns the size of the vertical faded edges used to indicate that more
2116 * content in this view is visible.
2117 *
2118 * @return The size in pixels of the vertical faded edge or 0 if vertical
2119 * faded edges are not enabled for this view.
2120 * @attr ref android.R.styleable#View_fadingEdgeLength
2121 */
2122 public int getVerticalFadingEdgeLength() {
2123 if (isVerticalFadingEdgeEnabled()) {
2124 ScrollabilityCache cache = mScrollCache;
2125 if (cache != null) {
2126 return cache.fadingEdgeLength;
2127 }
2128 }
2129 return 0;
2130 }
2131
2132 /**
2133 * Set the size of the faded edge used to indicate that more content in this
2134 * view is available. Will not change whether the fading edge is enabled; use
2135 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2136 * to enable the fading edge for the vertical or horizontal fading edges.
2137 *
2138 * @param length The size in pixels of the faded edge used to indicate that more
2139 * content in this view is visible.
2140 */
2141 public void setFadingEdgeLength(int length) {
2142 initScrollCache();
2143 mScrollCache.fadingEdgeLength = length;
2144 }
2145
2146 /**
2147 * Returns the size of the horizontal faded edges used to indicate that more
2148 * content in this view is visible.
2149 *
2150 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2151 * faded edges are not enabled for this view.
2152 * @attr ref android.R.styleable#View_fadingEdgeLength
2153 */
2154 public int getHorizontalFadingEdgeLength() {
2155 if (isHorizontalFadingEdgeEnabled()) {
2156 ScrollabilityCache cache = mScrollCache;
2157 if (cache != null) {
2158 return cache.fadingEdgeLength;
2159 }
2160 }
2161 return 0;
2162 }
2163
2164 /**
2165 * Returns the width of the vertical scrollbar.
2166 *
2167 * @return The width in pixels of the vertical scrollbar or 0 if there
2168 * is no vertical scrollbar.
2169 */
2170 public int getVerticalScrollbarWidth() {
2171 ScrollabilityCache cache = mScrollCache;
2172 if (cache != null) {
2173 ScrollBarDrawable scrollBar = cache.scrollBar;
2174 if (scrollBar != null) {
2175 int size = scrollBar.getSize(true);
2176 if (size <= 0) {
2177 size = cache.scrollBarSize;
2178 }
2179 return size;
2180 }
2181 return 0;
2182 }
2183 return 0;
2184 }
2185
2186 /**
2187 * Returns the height of the horizontal scrollbar.
2188 *
2189 * @return The height in pixels of the horizontal scrollbar or 0 if
2190 * there is no horizontal scrollbar.
2191 */
2192 protected int getHorizontalScrollbarHeight() {
2193 ScrollabilityCache cache = mScrollCache;
2194 if (cache != null) {
2195 ScrollBarDrawable scrollBar = cache.scrollBar;
2196 if (scrollBar != null) {
2197 int size = scrollBar.getSize(false);
2198 if (size <= 0) {
2199 size = cache.scrollBarSize;
2200 }
2201 return size;
2202 }
2203 return 0;
2204 }
2205 return 0;
2206 }
2207
2208 /**
2209 * <p>
2210 * Initializes the scrollbars from a given set of styled attributes. This
2211 * method should be called by subclasses that need scrollbars and when an
2212 * instance of these subclasses is created programmatically rather than
2213 * being inflated from XML. This method is automatically called when the XML
2214 * is inflated.
2215 * </p>
2216 *
2217 * @param a the styled attributes set to initialize the scrollbars from
2218 */
2219 protected void initializeScrollbars(TypedArray a) {
2220 initScrollCache();
2221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002222 final ScrollabilityCache scrollabilityCache = mScrollCache;
Mike Cleronf116bf82009-09-27 19:14:12 -07002223
2224 if (scrollabilityCache.scrollBar == null) {
2225 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2226 }
2227
2228 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229
Mike Cleronf116bf82009-09-27 19:14:12 -07002230 if (!fadeScrollbars) {
2231 scrollabilityCache.state = ScrollabilityCache.ON;
2232 }
2233 scrollabilityCache.fadeScrollBars = fadeScrollbars;
2234
2235
2236 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2237 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2238 .getScrollBarFadeDuration());
2239 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2240 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2241 ViewConfiguration.getScrollDefaultDelay());
2242
2243
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2245 com.android.internal.R.styleable.View_scrollbarSize,
2246 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2247
2248 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2249 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2250
2251 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2252 if (thumb != null) {
2253 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2254 }
2255
2256 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2257 false);
2258 if (alwaysDraw) {
2259 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2260 }
2261
2262 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2263 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2264
2265 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2266 if (thumb != null) {
2267 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2268 }
2269
2270 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2271 false);
2272 if (alwaysDraw) {
2273 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2274 }
2275
2276 // Re-apply user/background padding so that scrollbar(s) get added
2277 recomputePadding();
2278 }
2279
2280 /**
2281 * <p>
2282 * Initalizes the scrollability cache if necessary.
2283 * </p>
2284 */
2285 private void initScrollCache() {
2286 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002287 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
2289 }
2290
2291 /**
2292 * Register a callback to be invoked when focus of this view changed.
2293 *
2294 * @param l The callback that will run.
2295 */
2296 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2297 mOnFocusChangeListener = l;
2298 }
2299
2300 /**
2301 * Returns the focus-change callback registered for this view.
2302 *
2303 * @return The callback, or null if one is not registered.
2304 */
2305 public OnFocusChangeListener getOnFocusChangeListener() {
2306 return mOnFocusChangeListener;
2307 }
2308
2309 /**
2310 * Register a callback to be invoked when this view is clicked. If this view is not
2311 * clickable, it becomes clickable.
2312 *
2313 * @param l The callback that will run
2314 *
2315 * @see #setClickable(boolean)
2316 */
2317 public void setOnClickListener(OnClickListener l) {
2318 if (!isClickable()) {
2319 setClickable(true);
2320 }
2321 mOnClickListener = l;
2322 }
2323
2324 /**
2325 * Register a callback to be invoked when this view is clicked and held. If this view is not
2326 * long clickable, it becomes long clickable.
2327 *
2328 * @param l The callback that will run
2329 *
2330 * @see #setLongClickable(boolean)
2331 */
2332 public void setOnLongClickListener(OnLongClickListener l) {
2333 if (!isLongClickable()) {
2334 setLongClickable(true);
2335 }
2336 mOnLongClickListener = l;
2337 }
2338
2339 /**
2340 * Register a callback to be invoked when the context menu for this view is
2341 * being built. If this view is not long clickable, it becomes long clickable.
2342 *
2343 * @param l The callback that will run
2344 *
2345 */
2346 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
2347 if (!isLongClickable()) {
2348 setLongClickable(true);
2349 }
2350 mOnCreateContextMenuListener = l;
2351 }
2352
2353 /**
2354 * Call this view's OnClickListener, if it is defined.
2355 *
2356 * @return True there was an assigned OnClickListener that was called, false
2357 * otherwise is returned.
2358 */
2359 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07002360 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
2361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002362 if (mOnClickListener != null) {
2363 playSoundEffect(SoundEffectConstants.CLICK);
2364 mOnClickListener.onClick(this);
2365 return true;
2366 }
2367
2368 return false;
2369 }
2370
2371 /**
2372 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu
2373 * if the OnLongClickListener did not consume the event.
2374 *
2375 * @return True there was an assigned OnLongClickListener that was called, false
2376 * otherwise is returned.
2377 */
2378 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07002379 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
2380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 boolean handled = false;
2382 if (mOnLongClickListener != null) {
2383 handled = mOnLongClickListener.onLongClick(View.this);
2384 }
2385 if (!handled) {
2386 handled = showContextMenu();
2387 }
2388 if (handled) {
2389 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
2390 }
2391 return handled;
2392 }
2393
2394 /**
2395 * Bring up the context menu for this view.
2396 *
2397 * @return Whether a context menu was displayed.
2398 */
2399 public boolean showContextMenu() {
2400 return getParent().showContextMenuForChild(this);
2401 }
2402
2403 /**
2404 * Register a callback to be invoked when a key is pressed in this view.
2405 * @param l the key listener to attach to this view
2406 */
2407 public void setOnKeyListener(OnKeyListener l) {
2408 mOnKeyListener = l;
2409 }
2410
2411 /**
2412 * Register a callback to be invoked when a touch event is sent to this view.
2413 * @param l the touch listener to attach to this view
2414 */
2415 public void setOnTouchListener(OnTouchListener l) {
2416 mOnTouchListener = l;
2417 }
2418
2419 /**
2420 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
2421 *
2422 * Note: this does not check whether this {@link View} should get focus, it just
2423 * gives it focus no matter what. It should only be called internally by framework
2424 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
2425 *
2426 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
2427 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
2428 * focus moved when requestFocus() is called. It may not always
2429 * apply, in which case use the default View.FOCUS_DOWN.
2430 * @param previouslyFocusedRect The rectangle of the view that had focus
2431 * prior in this View's coordinate system.
2432 */
2433 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
2434 if (DBG) {
2435 System.out.println(this + " requestFocus()");
2436 }
2437
2438 if ((mPrivateFlags & FOCUSED) == 0) {
2439 mPrivateFlags |= FOCUSED;
2440
2441 if (mParent != null) {
2442 mParent.requestChildFocus(this, this);
2443 }
2444
2445 onFocusChanged(true, direction, previouslyFocusedRect);
2446 refreshDrawableState();
2447 }
2448 }
2449
2450 /**
2451 * Request that a rectangle of this view be visible on the screen,
2452 * scrolling if necessary just enough.
2453 *
2454 * <p>A View should call this if it maintains some notion of which part
2455 * of its content is interesting. For example, a text editing view
2456 * should call this when its cursor moves.
2457 *
2458 * @param rectangle The rectangle.
2459 * @return Whether any parent scrolled.
2460 */
2461 public boolean requestRectangleOnScreen(Rect rectangle) {
2462 return requestRectangleOnScreen(rectangle, false);
2463 }
2464
2465 /**
2466 * Request that a rectangle of this view be visible on the screen,
2467 * scrolling if necessary just enough.
2468 *
2469 * <p>A View should call this if it maintains some notion of which part
2470 * of its content is interesting. For example, a text editing view
2471 * should call this when its cursor moves.
2472 *
2473 * <p>When <code>immediate</code> is set to true, scrolling will not be
2474 * animated.
2475 *
2476 * @param rectangle The rectangle.
2477 * @param immediate True to forbid animated scrolling, false otherwise
2478 * @return Whether any parent scrolled.
2479 */
2480 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
2481 View child = this;
2482 ViewParent parent = mParent;
2483 boolean scrolled = false;
2484 while (parent != null) {
2485 scrolled |= parent.requestChildRectangleOnScreen(child,
2486 rectangle, immediate);
2487
2488 // offset rect so next call has the rectangle in the
2489 // coordinate system of its direct child.
2490 rectangle.offset(child.getLeft(), child.getTop());
2491 rectangle.offset(-child.getScrollX(), -child.getScrollY());
2492
2493 if (!(parent instanceof View)) {
2494 break;
2495 }
Romain Guy8506ab42009-06-11 17:35:47 -07002496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002497 child = (View) parent;
2498 parent = child.getParent();
2499 }
2500 return scrolled;
2501 }
2502
2503 /**
2504 * Called when this view wants to give up focus. This will cause
2505 * {@link #onFocusChanged} to be called.
2506 */
2507 public void clearFocus() {
2508 if (DBG) {
2509 System.out.println(this + " clearFocus()");
2510 }
2511
2512 if ((mPrivateFlags & FOCUSED) != 0) {
2513 mPrivateFlags &= ~FOCUSED;
2514
2515 if (mParent != null) {
2516 mParent.clearChildFocus(this);
2517 }
2518
2519 onFocusChanged(false, 0, null);
2520 refreshDrawableState();
2521 }
2522 }
2523
2524 /**
2525 * Called to clear the focus of a view that is about to be removed.
2526 * Doesn't call clearChildFocus, which prevents this view from taking
2527 * focus again before it has been removed from the parent
2528 */
2529 void clearFocusForRemoval() {
2530 if ((mPrivateFlags & FOCUSED) != 0) {
2531 mPrivateFlags &= ~FOCUSED;
2532
2533 onFocusChanged(false, 0, null);
2534 refreshDrawableState();
2535 }
2536 }
2537
2538 /**
2539 * Called internally by the view system when a new view is getting focus.
2540 * This is what clears the old focus.
2541 */
2542 void unFocus() {
2543 if (DBG) {
2544 System.out.println(this + " unFocus()");
2545 }
2546
2547 if ((mPrivateFlags & FOCUSED) != 0) {
2548 mPrivateFlags &= ~FOCUSED;
2549
2550 onFocusChanged(false, 0, null);
2551 refreshDrawableState();
2552 }
2553 }
2554
2555 /**
2556 * Returns true if this view has focus iteself, or is the ancestor of the
2557 * view that has focus.
2558 *
2559 * @return True if this view has or contains focus, false otherwise.
2560 */
2561 @ViewDebug.ExportedProperty
2562 public boolean hasFocus() {
2563 return (mPrivateFlags & FOCUSED) != 0;
2564 }
2565
2566 /**
2567 * Returns true if this view is focusable or if it contains a reachable View
2568 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
2569 * is a View whose parents do not block descendants focus.
2570 *
2571 * Only {@link #VISIBLE} views are considered focusable.
2572 *
2573 * @return True if the view is focusable or if the view contains a focusable
2574 * View, false otherwise.
2575 *
2576 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
2577 */
2578 public boolean hasFocusable() {
2579 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
2580 }
2581
2582 /**
2583 * Called by the view system when the focus state of this view changes.
2584 * When the focus change event is caused by directional navigation, direction
2585 * and previouslyFocusedRect provide insight into where the focus is coming from.
2586 * When overriding, be sure to call up through to the super class so that
2587 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07002588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002589 * @param gainFocus True if the View has focus; false otherwise.
2590 * @param direction The direction focus has moved when requestFocus()
2591 * is called to give this view focus. Values are
2592 * View.FOCUS_UP, View.FOCUS_DOWN, View.FOCUS_LEFT or
2593 * View.FOCUS_RIGHT. It may not always apply, in which
2594 * case use the default.
2595 * @param previouslyFocusedRect The rectangle, in this view's coordinate
2596 * system, of the previously focused view. If applicable, this will be
2597 * passed in as finer grained information about where the focus is coming
2598 * from (in addition to direction). Will be <code>null</code> otherwise.
2599 */
2600 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07002601 if (gainFocus) {
2602 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2603 }
2604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002605 InputMethodManager imm = InputMethodManager.peekInstance();
2606 if (!gainFocus) {
2607 if (isPressed()) {
2608 setPressed(false);
2609 }
2610 if (imm != null && mAttachInfo != null
2611 && mAttachInfo.mHasWindowFocus) {
2612 imm.focusOut(this);
2613 }
Romain Guya2431d02009-04-30 16:30:00 -07002614 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002615 } else if (imm != null && mAttachInfo != null
2616 && mAttachInfo.mHasWindowFocus) {
2617 imm.focusIn(this);
2618 }
Romain Guy8506ab42009-06-11 17:35:47 -07002619
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002620 invalidate();
2621 if (mOnFocusChangeListener != null) {
2622 mOnFocusChangeListener.onFocusChange(this, gainFocus);
2623 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002624
2625 if (mAttachInfo != null) {
2626 mAttachInfo.mKeyDispatchState.reset(this);
2627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002628 }
2629
2630 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002631 * {@inheritDoc}
2632 */
2633 public void sendAccessibilityEvent(int eventType) {
2634 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
2635 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
2636 }
2637 }
2638
2639 /**
2640 * {@inheritDoc}
2641 */
2642 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
2643 event.setClassName(getClass().getName());
2644 event.setPackageName(getContext().getPackageName());
2645 event.setEnabled(isEnabled());
2646 event.setContentDescription(mContentDescription);
2647
2648 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
2649 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
2650 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
2651 event.setItemCount(focusablesTempList.size());
2652 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
2653 focusablesTempList.clear();
2654 }
2655
2656 dispatchPopulateAccessibilityEvent(event);
2657
2658 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
2659 }
2660
2661 /**
2662 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
2663 * to be populated.
2664 *
2665 * @param event The event.
2666 *
2667 * @return True if the event population was completed.
2668 */
2669 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2670 return false;
2671 }
2672
2673 /**
2674 * Gets the {@link View} description. It briefly describes the view and is
2675 * primarily used for accessibility support. Set this property to enable
2676 * better accessibility support for your application. This is especially
2677 * true for views that do not have textual representation (For example,
2678 * ImageButton).
2679 *
2680 * @return The content descriptiopn.
2681 *
2682 * @attr ref android.R.styleable#View_contentDescription
2683 */
2684 public CharSequence getContentDescription() {
2685 return mContentDescription;
2686 }
2687
2688 /**
2689 * Sets the {@link View} description. It briefly describes the view and is
2690 * primarily used for accessibility support. Set this property to enable
2691 * better accessibility support for your application. This is especially
2692 * true for views that do not have textual representation (For example,
2693 * ImageButton).
2694 *
2695 * @param contentDescription The content description.
2696 *
2697 * @attr ref android.R.styleable#View_contentDescription
2698 */
2699 public void setContentDescription(CharSequence contentDescription) {
2700 mContentDescription = contentDescription;
2701 }
2702
2703 /**
Romain Guya2431d02009-04-30 16:30:00 -07002704 * Invoked whenever this view loses focus, either by losing window focus or by losing
2705 * focus within its window. This method can be used to clear any state tied to the
2706 * focus. For instance, if a button is held pressed with the trackball and the window
2707 * loses focus, this method can be used to cancel the press.
2708 *
2709 * Subclasses of View overriding this method should always call super.onFocusLost().
2710 *
2711 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07002712 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07002713 *
2714 * @hide pending API council approval
2715 */
2716 protected void onFocusLost() {
2717 resetPressedState();
2718 }
2719
2720 private void resetPressedState() {
2721 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
2722 return;
2723 }
2724
2725 if (isPressed()) {
2726 setPressed(false);
2727
2728 if (!mHasPerformedLongPress) {
2729 if (mPendingCheckForLongPress != null) {
2730 removeCallbacks(mPendingCheckForLongPress);
2731 }
2732 }
2733 }
2734 }
2735
2736 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002737 * Returns true if this view has focus
2738 *
2739 * @return True if this view has focus, false otherwise.
2740 */
2741 @ViewDebug.ExportedProperty
2742 public boolean isFocused() {
2743 return (mPrivateFlags & FOCUSED) != 0;
2744 }
2745
2746 /**
2747 * Find the view in the hierarchy rooted at this view that currently has
2748 * focus.
2749 *
2750 * @return The view that currently has focus, or null if no focused view can
2751 * be found.
2752 */
2753 public View findFocus() {
2754 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
2755 }
2756
2757 /**
2758 * Change whether this view is one of the set of scrollable containers in
2759 * its window. This will be used to determine whether the window can
2760 * resize or must pan when a soft input area is open -- scrollable
2761 * containers allow the window to use resize mode since the container
2762 * will appropriately shrink.
2763 */
2764 public void setScrollContainer(boolean isScrollContainer) {
2765 if (isScrollContainer) {
2766 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
2767 mAttachInfo.mScrollContainers.add(this);
2768 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
2769 }
2770 mPrivateFlags |= SCROLL_CONTAINER;
2771 } else {
2772 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
2773 mAttachInfo.mScrollContainers.remove(this);
2774 }
2775 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
2776 }
2777 }
2778
2779 /**
2780 * Returns the quality of the drawing cache.
2781 *
2782 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
2783 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
2784 *
2785 * @see #setDrawingCacheQuality(int)
2786 * @see #setDrawingCacheEnabled(boolean)
2787 * @see #isDrawingCacheEnabled()
2788 *
2789 * @attr ref android.R.styleable#View_drawingCacheQuality
2790 */
2791 public int getDrawingCacheQuality() {
2792 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
2793 }
2794
2795 /**
2796 * Set the drawing cache quality of this view. This value is used only when the
2797 * drawing cache is enabled
2798 *
2799 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
2800 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
2801 *
2802 * @see #getDrawingCacheQuality()
2803 * @see #setDrawingCacheEnabled(boolean)
2804 * @see #isDrawingCacheEnabled()
2805 *
2806 * @attr ref android.R.styleable#View_drawingCacheQuality
2807 */
2808 public void setDrawingCacheQuality(int quality) {
2809 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
2810 }
2811
2812 /**
2813 * Returns whether the screen should remain on, corresponding to the current
2814 * value of {@link #KEEP_SCREEN_ON}.
2815 *
2816 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
2817 *
2818 * @see #setKeepScreenOn(boolean)
2819 *
2820 * @attr ref android.R.styleable#View_keepScreenOn
2821 */
2822 public boolean getKeepScreenOn() {
2823 return (mViewFlags & KEEP_SCREEN_ON) != 0;
2824 }
2825
2826 /**
2827 * Controls whether the screen should remain on, modifying the
2828 * value of {@link #KEEP_SCREEN_ON}.
2829 *
2830 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
2831 *
2832 * @see #getKeepScreenOn()
2833 *
2834 * @attr ref android.R.styleable#View_keepScreenOn
2835 */
2836 public void setKeepScreenOn(boolean keepScreenOn) {
2837 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
2838 }
2839
2840 /**
2841 * @return The user specified next focus ID.
2842 *
2843 * @attr ref android.R.styleable#View_nextFocusLeft
2844 */
2845 public int getNextFocusLeftId() {
2846 return mNextFocusLeftId;
2847 }
2848
2849 /**
2850 * Set the id of the view to use for the next focus
2851 *
2852 * @param nextFocusLeftId
2853 *
2854 * @attr ref android.R.styleable#View_nextFocusLeft
2855 */
2856 public void setNextFocusLeftId(int nextFocusLeftId) {
2857 mNextFocusLeftId = nextFocusLeftId;
2858 }
2859
2860 /**
2861 * @return The user specified next focus ID.
2862 *
2863 * @attr ref android.R.styleable#View_nextFocusRight
2864 */
2865 public int getNextFocusRightId() {
2866 return mNextFocusRightId;
2867 }
2868
2869 /**
2870 * Set the id of the view to use for the next focus
2871 *
2872 * @param nextFocusRightId
2873 *
2874 * @attr ref android.R.styleable#View_nextFocusRight
2875 */
2876 public void setNextFocusRightId(int nextFocusRightId) {
2877 mNextFocusRightId = nextFocusRightId;
2878 }
2879
2880 /**
2881 * @return The user specified next focus ID.
2882 *
2883 * @attr ref android.R.styleable#View_nextFocusUp
2884 */
2885 public int getNextFocusUpId() {
2886 return mNextFocusUpId;
2887 }
2888
2889 /**
2890 * Set the id of the view to use for the next focus
2891 *
2892 * @param nextFocusUpId
2893 *
2894 * @attr ref android.R.styleable#View_nextFocusUp
2895 */
2896 public void setNextFocusUpId(int nextFocusUpId) {
2897 mNextFocusUpId = nextFocusUpId;
2898 }
2899
2900 /**
2901 * @return The user specified next focus ID.
2902 *
2903 * @attr ref android.R.styleable#View_nextFocusDown
2904 */
2905 public int getNextFocusDownId() {
2906 return mNextFocusDownId;
2907 }
2908
2909 /**
2910 * Set the id of the view to use for the next focus
2911 *
2912 * @param nextFocusDownId
2913 *
2914 * @attr ref android.R.styleable#View_nextFocusDown
2915 */
2916 public void setNextFocusDownId(int nextFocusDownId) {
2917 mNextFocusDownId = nextFocusDownId;
2918 }
2919
2920 /**
2921 * Returns the visibility of this view and all of its ancestors
2922 *
2923 * @return True if this view and all of its ancestors are {@link #VISIBLE}
2924 */
2925 public boolean isShown() {
2926 View current = this;
2927 //noinspection ConstantConditions
2928 do {
2929 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
2930 return false;
2931 }
2932 ViewParent parent = current.mParent;
2933 if (parent == null) {
2934 return false; // We are not attached to the view root
2935 }
2936 if (!(parent instanceof View)) {
2937 return true;
2938 }
2939 current = (View) parent;
2940 } while (current != null);
2941
2942 return false;
2943 }
2944
2945 /**
2946 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
2947 * is set
2948 *
2949 * @param insets Insets for system windows
2950 *
2951 * @return True if this view applied the insets, false otherwise
2952 */
2953 protected boolean fitSystemWindows(Rect insets) {
2954 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
2955 mPaddingLeft = insets.left;
2956 mPaddingTop = insets.top;
2957 mPaddingRight = insets.right;
2958 mPaddingBottom = insets.bottom;
2959 requestLayout();
2960 return true;
2961 }
2962 return false;
2963 }
2964
2965 /**
2966 * Returns the visibility status for this view.
2967 *
2968 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
2969 * @attr ref android.R.styleable#View_visibility
2970 */
2971 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002972 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
2973 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
2974 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002975 })
2976 public int getVisibility() {
2977 return mViewFlags & VISIBILITY_MASK;
2978 }
2979
2980 /**
2981 * Set the enabled state of this view.
2982 *
2983 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
2984 * @attr ref android.R.styleable#View_visibility
2985 */
2986 @RemotableViewMethod
2987 public void setVisibility(int visibility) {
2988 setFlags(visibility, VISIBILITY_MASK);
2989 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
2990 }
2991
2992 /**
2993 * Returns the enabled status for this view. The interpretation of the
2994 * enabled state varies by subclass.
2995 *
2996 * @return True if this view is enabled, false otherwise.
2997 */
2998 @ViewDebug.ExportedProperty
2999 public boolean isEnabled() {
3000 return (mViewFlags & ENABLED_MASK) == ENABLED;
3001 }
3002
3003 /**
3004 * Set the enabled state of this view. The interpretation of the enabled
3005 * state varies by subclass.
3006 *
3007 * @param enabled True if this view is enabled, false otherwise.
3008 */
3009 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003010 if (enabled == isEnabled()) return;
3011
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003012 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3013
3014 /*
3015 * The View most likely has to change its appearance, so refresh
3016 * the drawable state.
3017 */
3018 refreshDrawableState();
3019
3020 // Invalidate too, since the default behavior for views is to be
3021 // be drawn at 50% alpha rather than to change the drawable.
3022 invalidate();
3023 }
3024
3025 /**
3026 * Set whether this view can receive the focus.
3027 *
3028 * Setting this to false will also ensure that this view is not focusable
3029 * in touch mode.
3030 *
3031 * @param focusable If true, this view can receive the focus.
3032 *
3033 * @see #setFocusableInTouchMode(boolean)
3034 * @attr ref android.R.styleable#View_focusable
3035 */
3036 public void setFocusable(boolean focusable) {
3037 if (!focusable) {
3038 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3039 }
3040 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3041 }
3042
3043 /**
3044 * Set whether this view can receive focus while in touch mode.
3045 *
3046 * Setting this to true will also ensure that this view is focusable.
3047 *
3048 * @param focusableInTouchMode If true, this view can receive the focus while
3049 * in touch mode.
3050 *
3051 * @see #setFocusable(boolean)
3052 * @attr ref android.R.styleable#View_focusableInTouchMode
3053 */
3054 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3055 // Focusable in touch mode should always be set before the focusable flag
3056 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3057 // which, in touch mode, will not successfully request focus on this view
3058 // because the focusable in touch mode flag is not set
3059 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3060 if (focusableInTouchMode) {
3061 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3062 }
3063 }
3064
3065 /**
3066 * Set whether this view should have sound effects enabled for events such as
3067 * clicking and touching.
3068 *
3069 * <p>You may wish to disable sound effects for a view if you already play sounds,
3070 * for instance, a dial key that plays dtmf tones.
3071 *
3072 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3073 * @see #isSoundEffectsEnabled()
3074 * @see #playSoundEffect(int)
3075 * @attr ref android.R.styleable#View_soundEffectsEnabled
3076 */
3077 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3078 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3079 }
3080
3081 /**
3082 * @return whether this view should have sound effects enabled for events such as
3083 * clicking and touching.
3084 *
3085 * @see #setSoundEffectsEnabled(boolean)
3086 * @see #playSoundEffect(int)
3087 * @attr ref android.R.styleable#View_soundEffectsEnabled
3088 */
3089 @ViewDebug.ExportedProperty
3090 public boolean isSoundEffectsEnabled() {
3091 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3092 }
3093
3094 /**
3095 * Set whether this view should have haptic feedback for events such as
3096 * long presses.
3097 *
3098 * <p>You may wish to disable haptic feedback if your view already controls
3099 * its own haptic feedback.
3100 *
3101 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3102 * @see #isHapticFeedbackEnabled()
3103 * @see #performHapticFeedback(int)
3104 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3105 */
3106 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3107 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3108 }
3109
3110 /**
3111 * @return whether this view should have haptic feedback enabled for events
3112 * long presses.
3113 *
3114 * @see #setHapticFeedbackEnabled(boolean)
3115 * @see #performHapticFeedback(int)
3116 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3117 */
3118 @ViewDebug.ExportedProperty
3119 public boolean isHapticFeedbackEnabled() {
3120 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3121 }
3122
3123 /**
3124 * If this view doesn't do any drawing on its own, set this flag to
3125 * allow further optimizations. By default, this flag is not set on
3126 * View, but could be set on some View subclasses such as ViewGroup.
3127 *
3128 * Typically, if you override {@link #onDraw} you should clear this flag.
3129 *
3130 * @param willNotDraw whether or not this View draw on its own
3131 */
3132 public void setWillNotDraw(boolean willNotDraw) {
3133 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3134 }
3135
3136 /**
3137 * Returns whether or not this View draws on its own.
3138 *
3139 * @return true if this view has nothing to draw, false otherwise
3140 */
3141 @ViewDebug.ExportedProperty
3142 public boolean willNotDraw() {
3143 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3144 }
3145
3146 /**
3147 * When a View's drawing cache is enabled, drawing is redirected to an
3148 * offscreen bitmap. Some views, like an ImageView, must be able to
3149 * bypass this mechanism if they already draw a single bitmap, to avoid
3150 * unnecessary usage of the memory.
3151 *
3152 * @param willNotCacheDrawing true if this view does not cache its
3153 * drawing, false otherwise
3154 */
3155 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3156 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3157 }
3158
3159 /**
3160 * Returns whether or not this View can cache its drawing or not.
3161 *
3162 * @return true if this view does not cache its drawing, false otherwise
3163 */
3164 @ViewDebug.ExportedProperty
3165 public boolean willNotCacheDrawing() {
3166 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3167 }
3168
3169 /**
3170 * Indicates whether this view reacts to click events or not.
3171 *
3172 * @return true if the view is clickable, false otherwise
3173 *
3174 * @see #setClickable(boolean)
3175 * @attr ref android.R.styleable#View_clickable
3176 */
3177 @ViewDebug.ExportedProperty
3178 public boolean isClickable() {
3179 return (mViewFlags & CLICKABLE) == CLICKABLE;
3180 }
3181
3182 /**
3183 * Enables or disables click events for this view. When a view
3184 * is clickable it will change its state to "pressed" on every click.
3185 * Subclasses should set the view clickable to visually react to
3186 * user's clicks.
3187 *
3188 * @param clickable true to make the view clickable, false otherwise
3189 *
3190 * @see #isClickable()
3191 * @attr ref android.R.styleable#View_clickable
3192 */
3193 public void setClickable(boolean clickable) {
3194 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3195 }
3196
3197 /**
3198 * Indicates whether this view reacts to long click events or not.
3199 *
3200 * @return true if the view is long clickable, false otherwise
3201 *
3202 * @see #setLongClickable(boolean)
3203 * @attr ref android.R.styleable#View_longClickable
3204 */
3205 public boolean isLongClickable() {
3206 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3207 }
3208
3209 /**
3210 * Enables or disables long click events for this view. When a view is long
3211 * clickable it reacts to the user holding down the button for a longer
3212 * duration than a tap. This event can either launch the listener or a
3213 * context menu.
3214 *
3215 * @param longClickable true to make the view long clickable, false otherwise
3216 * @see #isLongClickable()
3217 * @attr ref android.R.styleable#View_longClickable
3218 */
3219 public void setLongClickable(boolean longClickable) {
3220 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
3221 }
3222
3223 /**
3224 * Sets the pressed that for this view.
3225 *
3226 * @see #isClickable()
3227 * @see #setClickable(boolean)
3228 *
3229 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
3230 * the View's internal state from a previously set "pressed" state.
3231 */
3232 public void setPressed(boolean pressed) {
3233 if (pressed) {
3234 mPrivateFlags |= PRESSED;
3235 } else {
3236 mPrivateFlags &= ~PRESSED;
3237 }
3238 refreshDrawableState();
3239 dispatchSetPressed(pressed);
3240 }
3241
3242 /**
3243 * Dispatch setPressed to all of this View's children.
3244 *
3245 * @see #setPressed(boolean)
3246 *
3247 * @param pressed The new pressed state
3248 */
3249 protected void dispatchSetPressed(boolean pressed) {
3250 }
3251
3252 /**
3253 * Indicates whether the view is currently in pressed state. Unless
3254 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
3255 * the pressed state.
3256 *
3257 * @see #setPressed
3258 * @see #isClickable()
3259 * @see #setClickable(boolean)
3260 *
3261 * @return true if the view is currently pressed, false otherwise
3262 */
3263 public boolean isPressed() {
3264 return (mPrivateFlags & PRESSED) == PRESSED;
3265 }
3266
3267 /**
3268 * Indicates whether this view will save its state (that is,
3269 * whether its {@link #onSaveInstanceState} method will be called).
3270 *
3271 * @return Returns true if the view state saving is enabled, else false.
3272 *
3273 * @see #setSaveEnabled(boolean)
3274 * @attr ref android.R.styleable#View_saveEnabled
3275 */
3276 public boolean isSaveEnabled() {
3277 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
3278 }
3279
3280 /**
3281 * Controls whether the saving of this view's state is
3282 * enabled (that is, whether its {@link #onSaveInstanceState} method
3283 * will be called). Note that even if freezing is enabled, the
3284 * view still must have an id assigned to it (via {@link #setId setId()})
3285 * for its state to be saved. This flag can only disable the
3286 * saving of this view; any child views may still have their state saved.
3287 *
3288 * @param enabled Set to false to <em>disable</em> state saving, or true
3289 * (the default) to allow it.
3290 *
3291 * @see #isSaveEnabled()
3292 * @see #setId(int)
3293 * @see #onSaveInstanceState()
3294 * @attr ref android.R.styleable#View_saveEnabled
3295 */
3296 public void setSaveEnabled(boolean enabled) {
3297 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
3298 }
3299
3300
3301 /**
3302 * Returns whether this View is able to take focus.
3303 *
3304 * @return True if this view can take focus, or false otherwise.
3305 * @attr ref android.R.styleable#View_focusable
3306 */
3307 @ViewDebug.ExportedProperty
3308 public final boolean isFocusable() {
3309 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
3310 }
3311
3312 /**
3313 * When a view is focusable, it may not want to take focus when in touch mode.
3314 * For example, a button would like focus when the user is navigating via a D-pad
3315 * so that the user can click on it, but once the user starts touching the screen,
3316 * the button shouldn't take focus
3317 * @return Whether the view is focusable in touch mode.
3318 * @attr ref android.R.styleable#View_focusableInTouchMode
3319 */
3320 @ViewDebug.ExportedProperty
3321 public final boolean isFocusableInTouchMode() {
3322 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
3323 }
3324
3325 /**
3326 * Find the nearest view in the specified direction that can take focus.
3327 * This does not actually give focus to that view.
3328 *
3329 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3330 *
3331 * @return The nearest focusable in the specified direction, or null if none
3332 * can be found.
3333 */
3334 public View focusSearch(int direction) {
3335 if (mParent != null) {
3336 return mParent.focusSearch(this, direction);
3337 } else {
3338 return null;
3339 }
3340 }
3341
3342 /**
3343 * This method is the last chance for the focused view and its ancestors to
3344 * respond to an arrow key. This is called when the focused view did not
3345 * consume the key internally, nor could the view system find a new view in
3346 * the requested direction to give focus to.
3347 *
3348 * @param focused The currently focused view.
3349 * @param direction The direction focus wants to move. One of FOCUS_UP,
3350 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
3351 * @return True if the this view consumed this unhandled move.
3352 */
3353 public boolean dispatchUnhandledMove(View focused, int direction) {
3354 return false;
3355 }
3356
3357 /**
3358 * If a user manually specified the next view id for a particular direction,
3359 * use the root to look up the view. Once a view is found, it is cached
3360 * for future lookups.
3361 * @param root The root view of the hierarchy containing this view.
3362 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3363 * @return The user specified next view, or null if there is none.
3364 */
3365 View findUserSetNextFocus(View root, int direction) {
3366 switch (direction) {
3367 case FOCUS_LEFT:
3368 if (mNextFocusLeftId == View.NO_ID) return null;
3369 return findViewShouldExist(root, mNextFocusLeftId);
3370 case FOCUS_RIGHT:
3371 if (mNextFocusRightId == View.NO_ID) return null;
3372 return findViewShouldExist(root, mNextFocusRightId);
3373 case FOCUS_UP:
3374 if (mNextFocusUpId == View.NO_ID) return null;
3375 return findViewShouldExist(root, mNextFocusUpId);
3376 case FOCUS_DOWN:
3377 if (mNextFocusDownId == View.NO_ID) return null;
3378 return findViewShouldExist(root, mNextFocusDownId);
3379 }
3380 return null;
3381 }
3382
3383 private static View findViewShouldExist(View root, int childViewId) {
3384 View result = root.findViewById(childViewId);
3385 if (result == null) {
3386 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
3387 + "by user for id " + childViewId);
3388 }
3389 return result;
3390 }
3391
3392 /**
3393 * Find and return all focusable views that are descendants of this view,
3394 * possibly including this view if it is focusable itself.
3395 *
3396 * @param direction The direction of the focus
3397 * @return A list of focusable views
3398 */
3399 public ArrayList<View> getFocusables(int direction) {
3400 ArrayList<View> result = new ArrayList<View>(24);
3401 addFocusables(result, direction);
3402 return result;
3403 }
3404
3405 /**
3406 * Add any focusable views that are descendants of this view (possibly
3407 * including this view if it is focusable itself) to views. If we are in touch mode,
3408 * only add views that are also focusable in touch mode.
3409 *
3410 * @param views Focusable views found so far
3411 * @param direction The direction of the focus
3412 */
3413 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003414 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
3415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416
svetoslavganov75986cf2009-05-14 22:28:01 -07003417 /**
3418 * Adds any focusable views that are descendants of this view (possibly
3419 * including this view if it is focusable itself) to views. This method
3420 * adds all focusable views regardless if we are in touch mode or
3421 * only views focusable in touch mode if we are in touch mode depending on
3422 * the focusable mode paramater.
3423 *
3424 * @param views Focusable views found so far or null if all we are interested is
3425 * the number of focusables.
3426 * @param direction The direction of the focus.
3427 * @param focusableMode The type of focusables to be added.
3428 *
3429 * @see #FOCUSABLES_ALL
3430 * @see #FOCUSABLES_TOUCH_MODE
3431 */
3432 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
3433 if (!isFocusable()) {
3434 return;
3435 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003436
svetoslavganov75986cf2009-05-14 22:28:01 -07003437 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
3438 isInTouchMode() && !isFocusableInTouchMode()) {
3439 return;
3440 }
3441
3442 if (views != null) {
3443 views.add(this);
3444 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 }
3446
3447 /**
3448 * Find and return all touchable views that are descendants of this view,
3449 * possibly including this view if it is touchable itself.
3450 *
3451 * @return A list of touchable views
3452 */
3453 public ArrayList<View> getTouchables() {
3454 ArrayList<View> result = new ArrayList<View>();
3455 addTouchables(result);
3456 return result;
3457 }
3458
3459 /**
3460 * Add any touchable views that are descendants of this view (possibly
3461 * including this view if it is touchable itself) to views.
3462 *
3463 * @param views Touchable views found so far
3464 */
3465 public void addTouchables(ArrayList<View> views) {
3466 final int viewFlags = mViewFlags;
3467
3468 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
3469 && (viewFlags & ENABLED_MASK) == ENABLED) {
3470 views.add(this);
3471 }
3472 }
3473
3474 /**
3475 * Call this to try to give focus to a specific view or to one of its
3476 * descendants.
3477 *
3478 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3479 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3480 * while the device is in touch mode.
3481 *
3482 * See also {@link #focusSearch}, which is what you call to say that you
3483 * have focus, and you want your parent to look for the next one.
3484 *
3485 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
3486 * {@link #FOCUS_DOWN} and <code>null</code>.
3487 *
3488 * @return Whether this view or one of its descendants actually took focus.
3489 */
3490 public final boolean requestFocus() {
3491 return requestFocus(View.FOCUS_DOWN);
3492 }
3493
3494
3495 /**
3496 * Call this to try to give focus to a specific view or to one of its
3497 * descendants and give it a hint about what direction focus is heading.
3498 *
3499 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3500 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3501 * while the device is in touch mode.
3502 *
3503 * See also {@link #focusSearch}, which is what you call to say that you
3504 * have focus, and you want your parent to look for the next one.
3505 *
3506 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
3507 * <code>null</code> set for the previously focused rectangle.
3508 *
3509 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3510 * @return Whether this view or one of its descendants actually took focus.
3511 */
3512 public final boolean requestFocus(int direction) {
3513 return requestFocus(direction, null);
3514 }
3515
3516 /**
3517 * Call this to try to give focus to a specific view or to one of its descendants
3518 * and give it hints about the direction and a specific rectangle that the focus
3519 * is coming from. The rectangle can help give larger views a finer grained hint
3520 * about where focus is coming from, and therefore, where to show selection, or
3521 * forward focus change internally.
3522 *
3523 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3524 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3525 * while the device is in touch mode.
3526 *
3527 * A View will not take focus if it is not visible.
3528 *
3529 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
3530 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
3531 *
3532 * See also {@link #focusSearch}, which is what you call to say that you
3533 * have focus, and you want your parent to look for the next one.
3534 *
3535 * You may wish to override this method if your custom {@link View} has an internal
3536 * {@link View} that it wishes to forward the request to.
3537 *
3538 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3539 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
3540 * to give a finer grained hint about where focus is coming from. May be null
3541 * if there is no hint.
3542 * @return Whether this view or one of its descendants actually took focus.
3543 */
3544 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
3545 // need to be focusable
3546 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
3547 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3548 return false;
3549 }
3550
3551 // need to be focusable in touch mode if in touch mode
3552 if (isInTouchMode() &&
3553 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
3554 return false;
3555 }
3556
3557 // need to not have any parents blocking us
3558 if (hasAncestorThatBlocksDescendantFocus()) {
3559 return false;
3560 }
3561
3562 handleFocusGainInternal(direction, previouslyFocusedRect);
3563 return true;
3564 }
3565
3566 /**
3567 * Call this to try to give focus to a specific view or to one of its descendants. This is a
3568 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
3569 * touch mode to request focus when they are touched.
3570 *
3571 * @return Whether this view or one of its descendants actually took focus.
3572 *
3573 * @see #isInTouchMode()
3574 *
3575 */
3576 public final boolean requestFocusFromTouch() {
3577 // Leave touch mode if we need to
3578 if (isInTouchMode()) {
3579 View root = getRootView();
3580 if (root != null) {
3581 ViewRoot viewRoot = (ViewRoot)root.getParent();
3582 if (viewRoot != null) {
3583 viewRoot.ensureTouchMode(false);
3584 }
3585 }
3586 }
3587 return requestFocus(View.FOCUS_DOWN);
3588 }
3589
3590 /**
3591 * @return Whether any ancestor of this view blocks descendant focus.
3592 */
3593 private boolean hasAncestorThatBlocksDescendantFocus() {
3594 ViewParent ancestor = mParent;
3595 while (ancestor instanceof ViewGroup) {
3596 final ViewGroup vgAncestor = (ViewGroup) ancestor;
3597 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3598 return true;
3599 } else {
3600 ancestor = vgAncestor.getParent();
3601 }
3602 }
3603 return false;
3604 }
3605
3606 /**
3607 * This is called when a container is going to temporarily detach a child
3608 * that currently has focus, with
3609 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
3610 * It will either be followed by {@link #onFinishTemporaryDetach()} or
3611 * {@link #onDetachedFromWindow()} when the container is done. Generally
3612 * this is currently only done ListView for a view with focus.
3613 */
3614 public void onStartTemporaryDetach() {
3615 }
Romain Guy8506ab42009-06-11 17:35:47 -07003616
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003617 /**
3618 * Called after {@link #onStartTemporaryDetach} when the container is done
3619 * changing the view.
3620 */
3621 public void onFinishTemporaryDetach() {
3622 }
Romain Guy8506ab42009-06-11 17:35:47 -07003623
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003624 /**
3625 * capture information of this view for later analysis: developement only
3626 * check dynamic switch to make sure we only dump view
3627 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
3628 */
3629 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003630 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003631 return;
3632 }
3633 ViewDebug.dumpCapturedView(subTag, v);
3634 }
3635
3636 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003637 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
3638 * for this view's window. Returns null if the view is not currently attached
3639 * to the window. Normally you will not need to use this directly, but
3640 * just use the standard high-level event callbacks like {@link #onKeyDown}.
3641 */
3642 public KeyEvent.DispatcherState getKeyDispatcherState() {
3643 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
3644 }
3645
3646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003647 * Dispatch a key event before it is processed by any input method
3648 * associated with the view hierarchy. This can be used to intercept
3649 * key events in special situations before the IME consumes them; a
3650 * typical example would be handling the BACK key to update the application's
3651 * UI instead of allowing the IME to see it and close itself.
3652 *
3653 * @param event The key event to be dispatched.
3654 * @return True if the event was handled, false otherwise.
3655 */
3656 public boolean dispatchKeyEventPreIme(KeyEvent event) {
3657 return onKeyPreIme(event.getKeyCode(), event);
3658 }
3659
3660 /**
3661 * Dispatch a key event to the next view on the focus path. This path runs
3662 * from the top of the view tree down to the currently focused view. If this
3663 * view has focus, it will dispatch to itself. Otherwise it will dispatch
3664 * the next node down the focus path. This method also fires any key
3665 * listeners.
3666 *
3667 * @param event The key event to be dispatched.
3668 * @return True if the event was handled, false otherwise.
3669 */
3670 public boolean dispatchKeyEvent(KeyEvent event) {
3671 // If any attached key listener a first crack at the event.
3672 //noinspection SimplifiableIfStatement
3673
3674 if (android.util.Config.LOGV) {
3675 captureViewInfo("captureViewKeyEvent", this);
3676 }
3677
3678 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
3679 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
3680 return true;
3681 }
3682
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003683 return event.dispatch(this, mAttachInfo != null
3684 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003685 }
3686
3687 /**
3688 * Dispatches a key shortcut event.
3689 *
3690 * @param event The key event to be dispatched.
3691 * @return True if the event was handled by the view, false otherwise.
3692 */
3693 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3694 return onKeyShortcut(event.getKeyCode(), event);
3695 }
3696
3697 /**
3698 * Pass the touch screen motion event down to the target view, or this
3699 * view if it is the target.
3700 *
3701 * @param event The motion event to be dispatched.
3702 * @return True if the event was handled by the view, false otherwise.
3703 */
3704 public boolean dispatchTouchEvent(MotionEvent event) {
3705 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
3706 mOnTouchListener.onTouch(this, event)) {
3707 return true;
3708 }
3709 return onTouchEvent(event);
3710 }
3711
3712 /**
3713 * Pass a trackball motion event down to the focused view.
3714 *
3715 * @param event The motion event to be dispatched.
3716 * @return True if the event was handled by the view, false otherwise.
3717 */
3718 public boolean dispatchTrackballEvent(MotionEvent event) {
3719 //Log.i("view", "view=" + this + ", " + event.toString());
3720 return onTrackballEvent(event);
3721 }
3722
3723 /**
3724 * Called when the window containing this view gains or loses window focus.
3725 * ViewGroups should override to route to their children.
3726 *
3727 * @param hasFocus True if the window containing this view now has focus,
3728 * false otherwise.
3729 */
3730 public void dispatchWindowFocusChanged(boolean hasFocus) {
3731 onWindowFocusChanged(hasFocus);
3732 }
3733
3734 /**
3735 * Called when the window containing this view gains or loses focus. Note
3736 * that this is separate from view focus: to receive key events, both
3737 * your view and its window must have focus. If a window is displayed
3738 * on top of yours that takes input focus, then your own window will lose
3739 * focus but the view focus will remain unchanged.
3740 *
3741 * @param hasWindowFocus True if the window containing this view now has
3742 * focus, false otherwise.
3743 */
3744 public void onWindowFocusChanged(boolean hasWindowFocus) {
3745 InputMethodManager imm = InputMethodManager.peekInstance();
3746 if (!hasWindowFocus) {
3747 if (isPressed()) {
3748 setPressed(false);
3749 }
3750 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
3751 imm.focusOut(this);
3752 }
The Android Open Source Project10592532009-03-18 17:39:46 -07003753 if (mPendingCheckForLongPress != null) {
3754 removeCallbacks(mPendingCheckForLongPress);
3755 }
Romain Guya2431d02009-04-30 16:30:00 -07003756 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
3758 imm.focusIn(this);
3759 }
3760 refreshDrawableState();
3761 }
3762
3763 /**
3764 * Returns true if this view is in a window that currently has window focus.
3765 * Note that this is not the same as the view itself having focus.
3766 *
3767 * @return True if this view is in a window that currently has window focus.
3768 */
3769 public boolean hasWindowFocus() {
3770 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
3771 }
3772
3773 /**
3774 * Dispatch a window visibility change down the view hierarchy.
3775 * ViewGroups should override to route to their children.
3776 *
3777 * @param visibility The new visibility of the window.
3778 *
3779 * @see #onWindowVisibilityChanged
3780 */
3781 public void dispatchWindowVisibilityChanged(int visibility) {
3782 onWindowVisibilityChanged(visibility);
3783 }
3784
3785 /**
3786 * Called when the window containing has change its visibility
3787 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
3788 * that this tells you whether or not your window is being made visible
3789 * to the window manager; this does <em>not</em> tell you whether or not
3790 * your window is obscured by other windows on the screen, even if it
3791 * is itself visible.
3792 *
3793 * @param visibility The new visibility of the window.
3794 */
3795 protected void onWindowVisibilityChanged(int visibility) {
3796 }
3797
3798 /**
3799 * Returns the current visibility of the window this view is attached to
3800 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
3801 *
3802 * @return Returns the current visibility of the view's window.
3803 */
3804 public int getWindowVisibility() {
3805 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
3806 }
3807
3808 /**
3809 * Retrieve the overall visible display size in which the window this view is
3810 * attached to has been positioned in. This takes into account screen
3811 * decorations above the window, for both cases where the window itself
3812 * is being position inside of them or the window is being placed under
3813 * then and covered insets are used for the window to position its content
3814 * inside. In effect, this tells you the available area where content can
3815 * be placed and remain visible to users.
3816 *
3817 * <p>This function requires an IPC back to the window manager to retrieve
3818 * the requested information, so should not be used in performance critical
3819 * code like drawing.
3820 *
3821 * @param outRect Filled in with the visible display frame. If the view
3822 * is not attached to a window, this is simply the raw display size.
3823 */
3824 public void getWindowVisibleDisplayFrame(Rect outRect) {
3825 if (mAttachInfo != null) {
3826 try {
3827 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
3828 } catch (RemoteException e) {
3829 return;
3830 }
3831 // XXX This is really broken, and probably all needs to be done
3832 // in the window manager, and we need to know more about whether
3833 // we want the area behind or in front of the IME.
3834 final Rect insets = mAttachInfo.mVisibleInsets;
3835 outRect.left += insets.left;
3836 outRect.top += insets.top;
3837 outRect.right -= insets.right;
3838 outRect.bottom -= insets.bottom;
3839 return;
3840 }
3841 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
3842 outRect.set(0, 0, d.getWidth(), d.getHeight());
3843 }
3844
3845 /**
3846 * Private function to aggregate all per-view attributes in to the view
3847 * root.
3848 */
3849 void dispatchCollectViewAttributes(int visibility) {
3850 performCollectViewAttributes(visibility);
3851 }
3852
3853 void performCollectViewAttributes(int visibility) {
3854 //noinspection PointlessBitwiseExpression
3855 if (((visibility | mViewFlags) & (VISIBILITY_MASK | KEEP_SCREEN_ON))
3856 == (VISIBLE | KEEP_SCREEN_ON)) {
3857 mAttachInfo.mKeepScreenOn = true;
3858 }
3859 }
3860
3861 void needGlobalAttributesUpdate(boolean force) {
3862 AttachInfo ai = mAttachInfo;
3863 if (ai != null) {
3864 if (ai.mKeepScreenOn || force) {
3865 ai.mRecomputeGlobalAttributes = true;
3866 }
3867 }
3868 }
3869
3870 /**
3871 * Returns whether the device is currently in touch mode. Touch mode is entered
3872 * once the user begins interacting with the device by touch, and affects various
3873 * things like whether focus is always visible to the user.
3874 *
3875 * @return Whether the device is in touch mode.
3876 */
3877 @ViewDebug.ExportedProperty
3878 public boolean isInTouchMode() {
3879 if (mAttachInfo != null) {
3880 return mAttachInfo.mInTouchMode;
3881 } else {
3882 return ViewRoot.isInTouchMode();
3883 }
3884 }
3885
3886 /**
3887 * Returns the context the view is running in, through which it can
3888 * access the current theme, resources, etc.
3889 *
3890 * @return The view's Context.
3891 */
3892 @ViewDebug.CapturedViewProperty
3893 public final Context getContext() {
3894 return mContext;
3895 }
3896
3897 /**
3898 * Handle a key event before it is processed by any input method
3899 * associated with the view hierarchy. This can be used to intercept
3900 * key events in special situations before the IME consumes them; a
3901 * typical example would be handling the BACK key to update the application's
3902 * UI instead of allowing the IME to see it and close itself.
3903 *
3904 * @param keyCode The value in event.getKeyCode().
3905 * @param event Description of the key event.
3906 * @return If you handled the event, return true. If you want to allow the
3907 * event to be handled by the next receiver, return false.
3908 */
3909 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
3910 return false;
3911 }
3912
3913 /**
3914 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3915 * KeyEvent.Callback.onKeyMultiple()}: perform press of the view
3916 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
3917 * is released, if the view is enabled and clickable.
3918 *
3919 * @param keyCode A key code that represents the button pressed, from
3920 * {@link android.view.KeyEvent}.
3921 * @param event The KeyEvent object that defines the button action.
3922 */
3923 public boolean onKeyDown(int keyCode, KeyEvent event) {
3924 boolean result = false;
3925
3926 switch (keyCode) {
3927 case KeyEvent.KEYCODE_DPAD_CENTER:
3928 case KeyEvent.KEYCODE_ENTER: {
3929 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3930 return true;
3931 }
3932 // Long clickable items don't necessarily have to be clickable
3933 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
3934 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
3935 (event.getRepeatCount() == 0)) {
3936 setPressed(true);
3937 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
3938 postCheckForLongClick();
3939 }
3940 return true;
3941 }
3942 break;
3943 }
3944 }
3945 return result;
3946 }
3947
3948 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003949 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
3950 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
3951 * the event).
3952 */
3953 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
3954 return false;
3955 }
3956
3957 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003958 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3959 * KeyEvent.Callback.onKeyMultiple()}: perform clicking of the view
3960 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
3961 * {@link KeyEvent#KEYCODE_ENTER} is released.
3962 *
3963 * @param keyCode A key code that represents the button pressed, from
3964 * {@link android.view.KeyEvent}.
3965 * @param event The KeyEvent object that defines the button action.
3966 */
3967 public boolean onKeyUp(int keyCode, KeyEvent event) {
3968 boolean result = false;
3969
3970 switch (keyCode) {
3971 case KeyEvent.KEYCODE_DPAD_CENTER:
3972 case KeyEvent.KEYCODE_ENTER: {
3973 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3974 return true;
3975 }
3976 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
3977 setPressed(false);
3978
3979 if (!mHasPerformedLongPress) {
3980 // This is a tap, so remove the longpress check
3981 if (mPendingCheckForLongPress != null) {
3982 removeCallbacks(mPendingCheckForLongPress);
3983 }
3984
3985 result = performClick();
3986 }
3987 }
3988 break;
3989 }
3990 }
3991 return result;
3992 }
3993
3994 /**
3995 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
3996 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
3997 * the event).
3998 *
3999 * @param keyCode A key code that represents the button pressed, from
4000 * {@link android.view.KeyEvent}.
4001 * @param repeatCount The number of times the action was made.
4002 * @param event The KeyEvent object that defines the button action.
4003 */
4004 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4005 return false;
4006 }
4007
4008 /**
4009 * Called when an unhandled key shortcut event occurs.
4010 *
4011 * @param keyCode The value in event.getKeyCode().
4012 * @param event Description of the key event.
4013 * @return If you handled the event, return true. If you want to allow the
4014 * event to be handled by the next receiver, return false.
4015 */
4016 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4017 return false;
4018 }
4019
4020 /**
4021 * Check whether the called view is a text editor, in which case it
4022 * would make sense to automatically display a soft input window for
4023 * it. Subclasses should override this if they implement
4024 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004025 * a call on that method would return a non-null InputConnection, and
4026 * they are really a first-class editor that the user would normally
4027 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07004028 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004029 * <p>The default implementation always returns false. This does
4030 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
4031 * will not be called or the user can not otherwise perform edits on your
4032 * view; it is just a hint to the system that this is not the primary
4033 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07004034 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004035 * @return Returns true if this view is a text editor, else false.
4036 */
4037 public boolean onCheckIsTextEditor() {
4038 return false;
4039 }
Romain Guy8506ab42009-06-11 17:35:47 -07004040
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004041 /**
4042 * Create a new InputConnection for an InputMethod to interact
4043 * with the view. The default implementation returns null, since it doesn't
4044 * support input methods. You can override this to implement such support.
4045 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07004046 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004047 * <p>When implementing this, you probably also want to implement
4048 * {@link #onCheckIsTextEditor()} to indicate you will return a
4049 * non-null InputConnection.
4050 *
4051 * @param outAttrs Fill in with attribute information about the connection.
4052 */
4053 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
4054 return null;
4055 }
4056
4057 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004058 * Called by the {@link android.view.inputmethod.InputMethodManager}
4059 * when a view who is not the current
4060 * input connection target is trying to make a call on the manager. The
4061 * default implementation returns false; you can override this to return
4062 * true for certain views if you are performing InputConnection proxying
4063 * to them.
4064 * @param view The View that is making the InputMethodManager call.
4065 * @return Return true to allow the call, false to reject.
4066 */
4067 public boolean checkInputConnectionProxy(View view) {
4068 return false;
4069 }
Romain Guy8506ab42009-06-11 17:35:47 -07004070
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004072 * Show the context menu for this view. It is not safe to hold on to the
4073 * menu after returning from this method.
4074 *
4075 * @param menu The context menu to populate
4076 */
4077 public void createContextMenu(ContextMenu menu) {
4078 ContextMenuInfo menuInfo = getContextMenuInfo();
4079
4080 // Sets the current menu info so all items added to menu will have
4081 // my extra info set.
4082 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
4083
4084 onCreateContextMenu(menu);
4085 if (mOnCreateContextMenuListener != null) {
4086 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
4087 }
4088
4089 // Clear the extra information so subsequent items that aren't mine don't
4090 // have my extra info.
4091 ((MenuBuilder)menu).setCurrentMenuInfo(null);
4092
4093 if (mParent != null) {
4094 mParent.createContextMenu(menu);
4095 }
4096 }
4097
4098 /**
4099 * Views should implement this if they have extra information to associate
4100 * with the context menu. The return result is supplied as a parameter to
4101 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
4102 * callback.
4103 *
4104 * @return Extra information about the item for which the context menu
4105 * should be shown. This information will vary across different
4106 * subclasses of View.
4107 */
4108 protected ContextMenuInfo getContextMenuInfo() {
4109 return null;
4110 }
4111
4112 /**
4113 * Views should implement this if the view itself is going to add items to
4114 * the context menu.
4115 *
4116 * @param menu the context menu to populate
4117 */
4118 protected void onCreateContextMenu(ContextMenu menu) {
4119 }
4120
4121 /**
4122 * Implement this method to handle trackball motion events. The
4123 * <em>relative</em> movement of the trackball since the last event
4124 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
4125 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
4126 * that a movement of 1 corresponds to the user pressing one DPAD key (so
4127 * they will often be fractional values, representing the more fine-grained
4128 * movement information available from a trackball).
4129 *
4130 * @param event The motion event.
4131 * @return True if the event was handled, false otherwise.
4132 */
4133 public boolean onTrackballEvent(MotionEvent event) {
4134 return false;
4135 }
4136
4137 /**
4138 * Implement this method to handle touch screen motion events.
4139 *
4140 * @param event The motion event.
4141 * @return True if the event was handled, false otherwise.
4142 */
4143 public boolean onTouchEvent(MotionEvent event) {
4144 final int viewFlags = mViewFlags;
4145
4146 if ((viewFlags & ENABLED_MASK) == DISABLED) {
4147 // A disabled view that is clickable still consumes the touch
4148 // events, it just doesn't respond to them.
4149 return (((viewFlags & CLICKABLE) == CLICKABLE ||
4150 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
4151 }
4152
4153 if (mTouchDelegate != null) {
4154 if (mTouchDelegate.onTouchEvent(event)) {
4155 return true;
4156 }
4157 }
4158
4159 if (((viewFlags & CLICKABLE) == CLICKABLE ||
4160 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
4161 switch (event.getAction()) {
4162 case MotionEvent.ACTION_UP:
4163 if ((mPrivateFlags & PRESSED) != 0) {
4164 // take focus if we don't have it already and we should in
4165 // touch mode.
4166 boolean focusTaken = false;
4167 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
4168 focusTaken = requestFocus();
4169 }
4170
4171 if (!mHasPerformedLongPress) {
4172 // This is a tap, so remove the longpress check
4173 if (mPendingCheckForLongPress != null) {
4174 removeCallbacks(mPendingCheckForLongPress);
4175 }
4176
4177 // Only perform take click actions if we were in the pressed state
4178 if (!focusTaken) {
4179 performClick();
4180 }
4181 }
4182
4183 if (mUnsetPressedState == null) {
4184 mUnsetPressedState = new UnsetPressedState();
4185 }
4186
4187 if (!post(mUnsetPressedState)) {
4188 // If the post failed, unpress right now
4189 mUnsetPressedState.run();
4190 }
4191 }
4192 break;
4193
4194 case MotionEvent.ACTION_DOWN:
4195 mPrivateFlags |= PRESSED;
4196 refreshDrawableState();
4197 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
4198 postCheckForLongClick();
4199 }
4200 break;
4201
4202 case MotionEvent.ACTION_CANCEL:
4203 mPrivateFlags &= ~PRESSED;
4204 refreshDrawableState();
4205 break;
4206
4207 case MotionEvent.ACTION_MOVE:
4208 final int x = (int) event.getX();
4209 final int y = (int) event.getY();
4210
4211 // Be lenient about moving outside of buttons
4212 int slop = ViewConfiguration.get(mContext).getScaledTouchSlop();
4213 if ((x < 0 - slop) || (x >= getWidth() + slop) ||
4214 (y < 0 - slop) || (y >= getHeight() + slop)) {
4215 // Outside button
4216 if ((mPrivateFlags & PRESSED) != 0) {
4217 // Remove any future long press checks
4218 if (mPendingCheckForLongPress != null) {
4219 removeCallbacks(mPendingCheckForLongPress);
4220 }
4221
4222 // Need to switch from pressed to not pressed
4223 mPrivateFlags &= ~PRESSED;
4224 refreshDrawableState();
4225 }
4226 } else {
4227 // Inside button
4228 if ((mPrivateFlags & PRESSED) == 0) {
4229 // Need to switch from not pressed to pressed
4230 mPrivateFlags |= PRESSED;
4231 refreshDrawableState();
4232 }
4233 }
4234 break;
4235 }
4236 return true;
4237 }
4238
4239 return false;
4240 }
4241
4242 /**
4243 * Cancels a pending long press. Your subclass can use this if you
4244 * want the context menu to come up if the user presses and holds
4245 * at the same place, but you don't want it to come up if they press
4246 * and then move around enough to cause scrolling.
4247 */
4248 public void cancelLongPress() {
4249 if (mPendingCheckForLongPress != null) {
4250 removeCallbacks(mPendingCheckForLongPress);
4251 }
4252 }
4253
4254 /**
4255 * Sets the TouchDelegate for this View.
4256 */
4257 public void setTouchDelegate(TouchDelegate delegate) {
4258 mTouchDelegate = delegate;
4259 }
4260
4261 /**
4262 * Gets the TouchDelegate for this View.
4263 */
4264 public TouchDelegate getTouchDelegate() {
4265 return mTouchDelegate;
4266 }
4267
4268 /**
4269 * Set flags controlling behavior of this view.
4270 *
4271 * @param flags Constant indicating the value which should be set
4272 * @param mask Constant indicating the bit range that should be changed
4273 */
4274 void setFlags(int flags, int mask) {
4275 int old = mViewFlags;
4276 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
4277
4278 int changed = mViewFlags ^ old;
4279 if (changed == 0) {
4280 return;
4281 }
4282 int privateFlags = mPrivateFlags;
4283
4284 /* Check if the FOCUSABLE bit has changed */
4285 if (((changed & FOCUSABLE_MASK) != 0) &&
4286 ((privateFlags & HAS_BOUNDS) !=0)) {
4287 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
4288 && ((privateFlags & FOCUSED) != 0)) {
4289 /* Give up focus if we are no longer focusable */
4290 clearFocus();
4291 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
4292 && ((privateFlags & FOCUSED) == 0)) {
4293 /*
4294 * Tell the view system that we are now available to take focus
4295 * if no one else already has it.
4296 */
4297 if (mParent != null) mParent.focusableViewAvailable(this);
4298 }
4299 }
4300
4301 if ((flags & VISIBILITY_MASK) == VISIBLE) {
4302 if ((changed & VISIBILITY_MASK) != 0) {
4303 /*
4304 * If this view is becoming visible, set the DRAWN flag so that
4305 * the next invalidate() will not be skipped.
4306 */
4307 mPrivateFlags |= DRAWN;
4308
4309 needGlobalAttributesUpdate(true);
4310
4311 // a view becoming visible is worth notifying the parent
4312 // about in case nothing has focus. even if this specific view
4313 // isn't focusable, it may contain something that is, so let
4314 // the root view try to give this focus if nothing else does.
4315 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
4316 mParent.focusableViewAvailable(this);
4317 }
4318 }
4319 }
4320
4321 /* Check if the GONE bit has changed */
4322 if ((changed & GONE) != 0) {
4323 needGlobalAttributesUpdate(false);
4324 requestLayout();
4325 invalidate();
4326
4327 if (((mViewFlags & VISIBILITY_MASK) == GONE) && hasFocus()) {
4328 clearFocus();
4329 }
4330 if (mAttachInfo != null) {
4331 mAttachInfo.mViewVisibilityChanged = true;
4332 }
4333 }
4334
4335 /* Check if the VISIBLE bit has changed */
4336 if ((changed & INVISIBLE) != 0) {
4337 needGlobalAttributesUpdate(false);
4338 invalidate();
4339
4340 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
4341 // root view becoming invisible shouldn't clear focus
4342 if (getRootView() != this) {
4343 clearFocus();
4344 }
4345 }
4346 if (mAttachInfo != null) {
4347 mAttachInfo.mViewVisibilityChanged = true;
4348 }
4349 }
4350
4351 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
4352 destroyDrawingCache();
4353 }
4354
4355 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
4356 destroyDrawingCache();
4357 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4358 }
4359
4360 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
4361 destroyDrawingCache();
4362 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4363 }
4364
4365 if ((changed & DRAW_MASK) != 0) {
4366 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
4367 if (mBGDrawable != null) {
4368 mPrivateFlags &= ~SKIP_DRAW;
4369 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
4370 } else {
4371 mPrivateFlags |= SKIP_DRAW;
4372 }
4373 } else {
4374 mPrivateFlags &= ~SKIP_DRAW;
4375 }
4376 requestLayout();
4377 invalidate();
4378 }
4379
4380 if ((changed & KEEP_SCREEN_ON) != 0) {
4381 if (mParent != null) {
4382 mParent.recomputeViewAttributes(this);
4383 }
4384 }
4385 }
4386
4387 /**
4388 * Change the view's z order in the tree, so it's on top of other sibling
4389 * views
4390 */
4391 public void bringToFront() {
4392 if (mParent != null) {
4393 mParent.bringChildToFront(this);
4394 }
4395 }
4396
4397 /**
4398 * This is called in response to an internal scroll in this view (i.e., the
4399 * view scrolled its own contents). This is typically as a result of
4400 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
4401 * called.
4402 *
4403 * @param l Current horizontal scroll origin.
4404 * @param t Current vertical scroll origin.
4405 * @param oldl Previous horizontal scroll origin.
4406 * @param oldt Previous vertical scroll origin.
4407 */
4408 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
4409 mBackgroundSizeChanged = true;
4410
4411 final AttachInfo ai = mAttachInfo;
4412 if (ai != null) {
4413 ai.mViewScrollChanged = true;
4414 }
4415 }
4416
4417 /**
4418 * This is called during layout when the size of this view has changed. If
4419 * you were just added to the view hierarchy, you're called with the old
4420 * values of 0.
4421 *
4422 * @param w Current width of this view.
4423 * @param h Current height of this view.
4424 * @param oldw Old width of this view.
4425 * @param oldh Old height of this view.
4426 */
4427 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
4428 }
4429
4430 /**
4431 * Called by draw to draw the child views. This may be overridden
4432 * by derived classes to gain control just before its children are drawn
4433 * (but after its own view has been drawn).
4434 * @param canvas the canvas on which to draw the view
4435 */
4436 protected void dispatchDraw(Canvas canvas) {
4437 }
4438
4439 /**
4440 * Gets the parent of this view. Note that the parent is a
4441 * ViewParent and not necessarily a View.
4442 *
4443 * @return Parent of this view.
4444 */
4445 public final ViewParent getParent() {
4446 return mParent;
4447 }
4448
4449 /**
4450 * Return the scrolled left position of this view. This is the left edge of
4451 * the displayed part of your view. You do not need to draw any pixels
4452 * farther left, since those are outside of the frame of your view on
4453 * screen.
4454 *
4455 * @return The left edge of the displayed part of your view, in pixels.
4456 */
4457 public final int getScrollX() {
4458 return mScrollX;
4459 }
4460
4461 /**
4462 * Return the scrolled top position of this view. This is the top edge of
4463 * the displayed part of your view. You do not need to draw any pixels above
4464 * it, since those are outside of the frame of your view on screen.
4465 *
4466 * @return The top edge of the displayed part of your view, in pixels.
4467 */
4468 public final int getScrollY() {
4469 return mScrollY;
4470 }
4471
4472 /**
4473 * Return the width of the your view.
4474 *
4475 * @return The width of your view, in pixels.
4476 */
4477 @ViewDebug.ExportedProperty
4478 public final int getWidth() {
4479 return mRight - mLeft;
4480 }
4481
4482 /**
4483 * Return the height of your view.
4484 *
4485 * @return The height of your view, in pixels.
4486 */
4487 @ViewDebug.ExportedProperty
4488 public final int getHeight() {
4489 return mBottom - mTop;
4490 }
4491
4492 /**
4493 * Return the visible drawing bounds of your view. Fills in the output
4494 * rectangle with the values from getScrollX(), getScrollY(),
4495 * getWidth(), and getHeight().
4496 *
4497 * @param outRect The (scrolled) drawing bounds of the view.
4498 */
4499 public void getDrawingRect(Rect outRect) {
4500 outRect.left = mScrollX;
4501 outRect.top = mScrollY;
4502 outRect.right = mScrollX + (mRight - mLeft);
4503 outRect.bottom = mScrollY + (mBottom - mTop);
4504 }
4505
4506 /**
4507 * The width of this view as measured in the most recent call to measure().
4508 * This should be used during measurement and layout calculations only. Use
4509 * {@link #getWidth()} to see how wide a view is after layout.
4510 *
4511 * @return The measured width of this view.
4512 */
4513 public final int getMeasuredWidth() {
4514 return mMeasuredWidth;
4515 }
4516
4517 /**
4518 * The height of this view as measured in the most recent call to measure().
4519 * This should be used during measurement and layout calculations only. Use
4520 * {@link #getHeight()} to see how tall a view is after layout.
4521 *
4522 * @return The measured height of this view.
4523 */
4524 public final int getMeasuredHeight() {
4525 return mMeasuredHeight;
4526 }
4527
4528 /**
4529 * Top position of this view relative to its parent.
4530 *
4531 * @return The top of this view, in pixels.
4532 */
4533 @ViewDebug.CapturedViewProperty
4534 public final int getTop() {
4535 return mTop;
4536 }
4537
4538 /**
4539 * Bottom position of this view relative to its parent.
4540 *
4541 * @return The bottom of this view, in pixels.
4542 */
4543 @ViewDebug.CapturedViewProperty
4544 public final int getBottom() {
4545 return mBottom;
4546 }
4547
4548 /**
4549 * Left position of this view relative to its parent.
4550 *
4551 * @return The left edge of this view, in pixels.
4552 */
4553 @ViewDebug.CapturedViewProperty
4554 public final int getLeft() {
4555 return mLeft;
4556 }
4557
4558 /**
4559 * Right position of this view relative to its parent.
4560 *
4561 * @return The right edge of this view, in pixels.
4562 */
4563 @ViewDebug.CapturedViewProperty
4564 public final int getRight() {
4565 return mRight;
4566 }
4567
4568 /**
4569 * Hit rectangle in parent's coordinates
4570 *
4571 * @param outRect The hit rectangle of the view.
4572 */
4573 public void getHitRect(Rect outRect) {
4574 outRect.set(mLeft, mTop, mRight, mBottom);
4575 }
4576
4577 /**
4578 * When a view has focus and the user navigates away from it, the next view is searched for
4579 * starting from the rectangle filled in by this method.
4580 *
4581 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
4582 * view maintains some idea of internal selection, such as a cursor, or a selected row
4583 * or column, you should override this method and fill in a more specific rectangle.
4584 *
4585 * @param r The rectangle to fill in, in this view's coordinates.
4586 */
4587 public void getFocusedRect(Rect r) {
4588 getDrawingRect(r);
4589 }
4590
4591 /**
4592 * If some part of this view is not clipped by any of its parents, then
4593 * return that area in r in global (root) coordinates. To convert r to local
4594 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
4595 * -globalOffset.y)) If the view is completely clipped or translated out,
4596 * return false.
4597 *
4598 * @param r If true is returned, r holds the global coordinates of the
4599 * visible portion of this view.
4600 * @param globalOffset If true is returned, globalOffset holds the dx,dy
4601 * between this view and its root. globalOffet may be null.
4602 * @return true if r is non-empty (i.e. part of the view is visible at the
4603 * root level.
4604 */
4605 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
4606 int width = mRight - mLeft;
4607 int height = mBottom - mTop;
4608 if (width > 0 && height > 0) {
4609 r.set(0, 0, width, height);
4610 if (globalOffset != null) {
4611 globalOffset.set(-mScrollX, -mScrollY);
4612 }
4613 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
4614 }
4615 return false;
4616 }
4617
4618 public final boolean getGlobalVisibleRect(Rect r) {
4619 return getGlobalVisibleRect(r, null);
4620 }
4621
4622 public final boolean getLocalVisibleRect(Rect r) {
4623 Point offset = new Point();
4624 if (getGlobalVisibleRect(r, offset)) {
4625 r.offset(-offset.x, -offset.y); // make r local
4626 return true;
4627 }
4628 return false;
4629 }
4630
4631 /**
4632 * Offset this view's vertical location by the specified number of pixels.
4633 *
4634 * @param offset the number of pixels to offset the view by
4635 */
4636 public void offsetTopAndBottom(int offset) {
4637 mTop += offset;
4638 mBottom += offset;
4639 }
4640
4641 /**
4642 * Offset this view's horizontal location by the specified amount of pixels.
4643 *
4644 * @param offset the numer of pixels to offset the view by
4645 */
4646 public void offsetLeftAndRight(int offset) {
4647 mLeft += offset;
4648 mRight += offset;
4649 }
4650
4651 /**
4652 * Get the LayoutParams associated with this view. All views should have
4653 * layout parameters. These supply parameters to the <i>parent</i> of this
4654 * view specifying how it should be arranged. There are many subclasses of
4655 * ViewGroup.LayoutParams, and these correspond to the different subclasses
4656 * of ViewGroup that are responsible for arranging their children.
4657 * @return The LayoutParams associated with this view
4658 */
4659 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
4660 public ViewGroup.LayoutParams getLayoutParams() {
4661 return mLayoutParams;
4662 }
4663
4664 /**
4665 * Set the layout parameters associated with this view. These supply
4666 * parameters to the <i>parent</i> of this view specifying how it should be
4667 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
4668 * correspond to the different subclasses of ViewGroup that are responsible
4669 * for arranging their children.
4670 *
4671 * @param params the layout parameters for this view
4672 */
4673 public void setLayoutParams(ViewGroup.LayoutParams params) {
4674 if (params == null) {
4675 throw new NullPointerException("params == null");
4676 }
4677 mLayoutParams = params;
4678 requestLayout();
4679 }
4680
4681 /**
4682 * Set the scrolled position of your view. This will cause a call to
4683 * {@link #onScrollChanged(int, int, int, int)} and the view will be
4684 * invalidated.
4685 * @param x the x position to scroll to
4686 * @param y the y position to scroll to
4687 */
4688 public void scrollTo(int x, int y) {
4689 if (mScrollX != x || mScrollY != y) {
4690 int oldX = mScrollX;
4691 int oldY = mScrollY;
4692 mScrollX = x;
4693 mScrollY = y;
4694 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07004695 if (!awakenScrollBars()) {
4696 invalidate();
4697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004698 }
4699 }
4700
4701 /**
4702 * Move the scrolled position of your view. This will cause a call to
4703 * {@link #onScrollChanged(int, int, int, int)} and the view will be
4704 * invalidated.
4705 * @param x the amount of pixels to scroll by horizontally
4706 * @param y the amount of pixels to scroll by vertically
4707 */
4708 public void scrollBy(int x, int y) {
4709 scrollTo(mScrollX + x, mScrollY + y);
4710 }
4711
4712 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07004713 * <p>Trigger the scrollbars to draw. When invoked this method starts an
4714 * animation to fade the scrollbars out after a default delay. If a subclass
4715 * provides animated scrolling, the start delay should equal the duration
4716 * of the scrolling animation.</p>
4717 *
4718 * <p>The animation starts only if at least one of the scrollbars is
4719 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
4720 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
4721 * this method returns true, and false otherwise. If the animation is
4722 * started, this method calls {@link #invalidate()}; in that case the
4723 * caller should not call {@link #invalidate()}.</p>
4724 *
4725 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07004726 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07004727 *
4728 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
4729 * and {@link #scrollTo(int, int)}.</p>
4730 *
4731 * @return true if the animation is played, false otherwise
4732 *
4733 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07004734 * @see #scrollBy(int, int)
4735 * @see #scrollTo(int, int)
4736 * @see #isHorizontalScrollBarEnabled()
4737 * @see #isVerticalScrollBarEnabled()
4738 * @see #setHorizontalScrollBarEnabled(boolean)
4739 * @see #setVerticalScrollBarEnabled(boolean)
4740 */
4741 protected boolean awakenScrollBars() {
4742 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07004743 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07004744 }
4745
4746 /**
4747 * <p>
4748 * Trigger the scrollbars to draw. When invoked this method starts an
4749 * animation to fade the scrollbars out after a fixed delay. If a subclass
4750 * provides animated scrolling, the start delay should equal the duration of
4751 * the scrolling animation.
4752 * </p>
4753 *
4754 * <p>
4755 * The animation starts only if at least one of the scrollbars is enabled,
4756 * as specified by {@link #isHorizontalScrollBarEnabled()} and
4757 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
4758 * this method returns true, and false otherwise. If the animation is
4759 * started, this method calls {@link #invalidate()}; in that case the caller
4760 * should not call {@link #invalidate()}.
4761 * </p>
4762 *
4763 * <p>
4764 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07004765 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07004766 * </p>
4767 *
4768 * @param startDelay the delay, in milliseconds, after which the animation
4769 * should start; when the delay is 0, the animation starts
4770 * immediately
4771 * @return true if the animation is played, false otherwise
4772 *
Mike Cleronf116bf82009-09-27 19:14:12 -07004773 * @see #scrollBy(int, int)
4774 * @see #scrollTo(int, int)
4775 * @see #isHorizontalScrollBarEnabled()
4776 * @see #isVerticalScrollBarEnabled()
4777 * @see #setHorizontalScrollBarEnabled(boolean)
4778 * @see #setVerticalScrollBarEnabled(boolean)
4779 */
4780 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07004781 return awakenScrollBars(startDelay, true);
4782 }
4783
4784 /**
4785 * <p>
4786 * Trigger the scrollbars to draw. When invoked this method starts an
4787 * animation to fade the scrollbars out after a fixed delay. If a subclass
4788 * provides animated scrolling, the start delay should equal the duration of
4789 * the scrolling animation.
4790 * </p>
4791 *
4792 * <p>
4793 * The animation starts only if at least one of the scrollbars is enabled,
4794 * as specified by {@link #isHorizontalScrollBarEnabled()} and
4795 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
4796 * this method returns true, and false otherwise. If the animation is
4797 * started, this method calls {@link #invalidate()} if the invalidate parameter
4798 * is set to true; in that case the caller
4799 * should not call {@link #invalidate()}.
4800 * </p>
4801 *
4802 * <p>
4803 * This method should be invoked everytime a subclass directly updates the
4804 * scroll parameters.
4805 * </p>
4806 *
4807 * @param startDelay the delay, in milliseconds, after which the animation
4808 * should start; when the delay is 0, the animation starts
4809 * immediately
4810 *
4811 * @param invalidate Wheter this method should call invalidate
4812 *
4813 * @return true if the animation is played, false otherwise
4814 *
4815 * @see #scrollBy(int, int)
4816 * @see #scrollTo(int, int)
4817 * @see #isHorizontalScrollBarEnabled()
4818 * @see #isVerticalScrollBarEnabled()
4819 * @see #setHorizontalScrollBarEnabled(boolean)
4820 * @see #setVerticalScrollBarEnabled(boolean)
4821 */
4822 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07004823 final ScrollabilityCache scrollCache = mScrollCache;
4824
4825 if (scrollCache == null || !scrollCache.fadeScrollBars) {
4826 return false;
4827 }
4828
4829 if (scrollCache.scrollBar == null) {
4830 scrollCache.scrollBar = new ScrollBarDrawable();
4831 }
4832
4833 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
4834
Mike Cleron290947b2009-09-29 18:34:32 -07004835 if (invalidate) {
4836 // Invalidate to show the scrollbars
4837 invalidate();
4838 }
Mike Cleronf116bf82009-09-27 19:14:12 -07004839
4840 if (scrollCache.state == ScrollabilityCache.OFF) {
4841 // FIXME: this is copied from WindowManagerService.
4842 // We should get this value from the system when it
4843 // is possible to do so.
4844 final int KEY_REPEAT_FIRST_DELAY = 750;
4845 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
4846 }
4847
4848 // Tell mScrollCache when we should start fading. This may
4849 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07004850 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07004851 scrollCache.fadeStartTime = fadeStartTime;
4852 scrollCache.state = ScrollabilityCache.ON;
4853
4854 // Schedule our fader to run, unscheduling any old ones first
4855 if (mAttachInfo != null) {
4856 mAttachInfo.mHandler.removeCallbacks(scrollCache);
4857 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
4858 }
4859
4860 return true;
4861 }
4862
4863 return false;
4864 }
4865
4866 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004867 * Mark the the area defined by dirty as needing to be drawn. If the view is
4868 * visible, {@link #onDraw} will be called at some point in the future.
4869 * This must be called from a UI thread. To call from a non-UI thread, call
4870 * {@link #postInvalidate()}.
4871 *
4872 * WARNING: This method is destructive to dirty.
4873 * @param dirty the rectangle representing the bounds of the dirty region
4874 */
4875 public void invalidate(Rect dirty) {
4876 if (ViewDebug.TRACE_HIERARCHY) {
4877 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
4878 }
4879
4880 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
4881 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4882 final ViewParent p = mParent;
4883 final AttachInfo ai = mAttachInfo;
4884 if (p != null && ai != null) {
4885 final int scrollX = mScrollX;
4886 final int scrollY = mScrollY;
4887 final Rect r = ai.mTmpInvalRect;
4888 r.set(dirty.left - scrollX, dirty.top - scrollY,
4889 dirty.right - scrollX, dirty.bottom - scrollY);
4890 mParent.invalidateChild(this, r);
4891 }
4892 }
4893 }
4894
4895 /**
4896 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
4897 * The coordinates of the dirty rect are relative to the view.
4898 * If the view is visible, {@link #onDraw} will be called at some point
4899 * in the future. This must be called from a UI thread. To call
4900 * from a non-UI thread, call {@link #postInvalidate()}.
4901 * @param l the left position of the dirty region
4902 * @param t the top position of the dirty region
4903 * @param r the right position of the dirty region
4904 * @param b the bottom position of the dirty region
4905 */
4906 public void invalidate(int l, int t, int r, int b) {
4907 if (ViewDebug.TRACE_HIERARCHY) {
4908 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
4909 }
4910
4911 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
4912 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4913 final ViewParent p = mParent;
4914 final AttachInfo ai = mAttachInfo;
4915 if (p != null && ai != null && l < r && t < b) {
4916 final int scrollX = mScrollX;
4917 final int scrollY = mScrollY;
4918 final Rect tmpr = ai.mTmpInvalRect;
4919 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
4920 p.invalidateChild(this, tmpr);
4921 }
4922 }
4923 }
4924
4925 /**
4926 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
4927 * be called at some point in the future. This must be called from a
4928 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
4929 */
4930 public void invalidate() {
4931 if (ViewDebug.TRACE_HIERARCHY) {
4932 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
4933 }
4934
4935 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
4936 mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
4937 final ViewParent p = mParent;
4938 final AttachInfo ai = mAttachInfo;
4939 if (p != null && ai != null) {
4940 final Rect r = ai.mTmpInvalRect;
4941 r.set(0, 0, mRight - mLeft, mBottom - mTop);
4942 // Don't call invalidate -- we don't want to internally scroll
4943 // our own bounds
4944 p.invalidateChild(this, r);
4945 }
4946 }
4947 }
4948
4949 /**
Romain Guy24443ea2009-05-11 11:56:30 -07004950 * Indicates whether this View is opaque. An opaque View guarantees that it will
4951 * draw all the pixels overlapping its bounds using a fully opaque color.
4952 *
4953 * Subclasses of View should override this method whenever possible to indicate
4954 * whether an instance is opaque. Opaque Views are treated in a special way by
4955 * the View hierarchy, possibly allowing it to perform optimizations during
4956 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07004957 *
Romain Guy24443ea2009-05-11 11:56:30 -07004958 * @return True if this View is guaranteed to be fully opaque, false otherwise.
4959 *
4960 * @hide Pending API council approval
4961 */
Romain Guy83b21072009-05-12 10:54:51 -07004962 @ViewDebug.ExportedProperty
Romain Guy24443ea2009-05-11 11:56:30 -07004963 public boolean isOpaque() {
Romain Guy8f1344f52009-05-15 16:03:59 -07004964 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK;
4965 }
4966
4967 private void computeOpaqueFlags() {
4968 // Opaque if:
4969 // - Has a background
4970 // - Background is opaque
4971 // - Doesn't have scrollbars or scrollbars are inside overlay
4972
4973 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
4974 mPrivateFlags |= OPAQUE_BACKGROUND;
4975 } else {
4976 mPrivateFlags &= ~OPAQUE_BACKGROUND;
4977 }
4978
4979 final int flags = mViewFlags;
4980 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
4981 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
4982 mPrivateFlags |= OPAQUE_SCROLLBARS;
4983 } else {
4984 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
4985 }
4986 }
4987
4988 /**
4989 * @hide
4990 */
4991 protected boolean hasOpaqueScrollbars() {
4992 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07004993 }
4994
4995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 * @return A handler associated with the thread running the View. This
4997 * handler can be used to pump events in the UI events queue.
4998 */
4999 public Handler getHandler() {
5000 if (mAttachInfo != null) {
5001 return mAttachInfo.mHandler;
5002 }
5003 return null;
5004 }
5005
5006 /**
5007 * Causes the Runnable to be added to the message queue.
5008 * The runnable will be run on the user interface thread.
5009 *
5010 * @param action The Runnable that will be executed.
5011 *
5012 * @return Returns true if the Runnable was successfully placed in to the
5013 * message queue. Returns false on failure, usually because the
5014 * looper processing the message queue is exiting.
5015 */
5016 public boolean post(Runnable action) {
5017 Handler handler;
5018 if (mAttachInfo != null) {
5019 handler = mAttachInfo.mHandler;
5020 } else {
5021 // Assume that post will succeed later
5022 ViewRoot.getRunQueue().post(action);
5023 return true;
5024 }
5025
5026 return handler.post(action);
5027 }
5028
5029 /**
5030 * Causes the Runnable to be added to the message queue, to be run
5031 * after the specified amount of time elapses.
5032 * The runnable will be run on the user interface thread.
5033 *
5034 * @param action The Runnable that will be executed.
5035 * @param delayMillis The delay (in milliseconds) until the Runnable
5036 * will be executed.
5037 *
5038 * @return true if the Runnable was successfully placed in to the
5039 * message queue. Returns false on failure, usually because the
5040 * looper processing the message queue is exiting. Note that a
5041 * result of true does not mean the Runnable will be processed --
5042 * if the looper is quit before the delivery time of the message
5043 * occurs then the message will be dropped.
5044 */
5045 public boolean postDelayed(Runnable action, long delayMillis) {
5046 Handler handler;
5047 if (mAttachInfo != null) {
5048 handler = mAttachInfo.mHandler;
5049 } else {
5050 // Assume that post will succeed later
5051 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
5052 return true;
5053 }
5054
5055 return handler.postDelayed(action, delayMillis);
5056 }
5057
5058 /**
5059 * Removes the specified Runnable from the message queue.
5060 *
5061 * @param action The Runnable to remove from the message handling queue
5062 *
5063 * @return true if this view could ask the Handler to remove the Runnable,
5064 * false otherwise. When the returned value is true, the Runnable
5065 * may or may not have been actually removed from the message queue
5066 * (for instance, if the Runnable was not in the queue already.)
5067 */
5068 public boolean removeCallbacks(Runnable action) {
5069 Handler handler;
5070 if (mAttachInfo != null) {
5071 handler = mAttachInfo.mHandler;
5072 } else {
5073 // Assume that post will succeed later
5074 ViewRoot.getRunQueue().removeCallbacks(action);
5075 return true;
5076 }
5077
5078 handler.removeCallbacks(action);
5079 return true;
5080 }
5081
5082 /**
5083 * Cause an invalidate to happen on a subsequent cycle through the event loop.
5084 * Use this to invalidate the View from a non-UI thread.
5085 *
5086 * @see #invalidate()
5087 */
5088 public void postInvalidate() {
5089 postInvalidateDelayed(0);
5090 }
5091
5092 /**
5093 * Cause an invalidate of the specified area to happen on a subsequent cycle
5094 * through the event loop. Use this to invalidate the View from a non-UI thread.
5095 *
5096 * @param left The left coordinate of the rectangle to invalidate.
5097 * @param top The top coordinate of the rectangle to invalidate.
5098 * @param right The right coordinate of the rectangle to invalidate.
5099 * @param bottom The bottom coordinate of the rectangle to invalidate.
5100 *
5101 * @see #invalidate(int, int, int, int)
5102 * @see #invalidate(Rect)
5103 */
5104 public void postInvalidate(int left, int top, int right, int bottom) {
5105 postInvalidateDelayed(0, left, top, right, bottom);
5106 }
5107
5108 /**
5109 * Cause an invalidate to happen on a subsequent cycle through the event
5110 * loop. Waits for the specified amount of time.
5111 *
5112 * @param delayMilliseconds the duration in milliseconds to delay the
5113 * invalidation by
5114 */
5115 public void postInvalidateDelayed(long delayMilliseconds) {
5116 // We try only with the AttachInfo because there's no point in invalidating
5117 // if we are not attached to our window
5118 if (mAttachInfo != null) {
5119 Message msg = Message.obtain();
5120 msg.what = AttachInfo.INVALIDATE_MSG;
5121 msg.obj = this;
5122 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
5123 }
5124 }
5125
5126 /**
5127 * Cause an invalidate of the specified area to happen on a subsequent cycle
5128 * through the event loop. Waits for the specified amount of time.
5129 *
5130 * @param delayMilliseconds the duration in milliseconds to delay the
5131 * invalidation by
5132 * @param left The left coordinate of the rectangle to invalidate.
5133 * @param top The top coordinate of the rectangle to invalidate.
5134 * @param right The right coordinate of the rectangle to invalidate.
5135 * @param bottom The bottom coordinate of the rectangle to invalidate.
5136 */
5137 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
5138 int right, int bottom) {
5139
5140 // We try only with the AttachInfo because there's no point in invalidating
5141 // if we are not attached to our window
5142 if (mAttachInfo != null) {
5143 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
5144 info.target = this;
5145 info.left = left;
5146 info.top = top;
5147 info.right = right;
5148 info.bottom = bottom;
5149
5150 final Message msg = Message.obtain();
5151 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
5152 msg.obj = info;
5153 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
5154 }
5155 }
5156
5157 /**
5158 * Called by a parent to request that a child update its values for mScrollX
5159 * and mScrollY if necessary. This will typically be done if the child is
5160 * animating a scroll using a {@link android.widget.Scroller Scroller}
5161 * object.
5162 */
5163 public void computeScroll() {
5164 }
5165
5166 /**
5167 * <p>Indicate whether the horizontal edges are faded when the view is
5168 * scrolled horizontally.</p>
5169 *
5170 * @return true if the horizontal edges should are faded on scroll, false
5171 * otherwise
5172 *
5173 * @see #setHorizontalFadingEdgeEnabled(boolean)
5174 * @attr ref android.R.styleable#View_fadingEdge
5175 */
5176 public boolean isHorizontalFadingEdgeEnabled() {
5177 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
5178 }
5179
5180 /**
5181 * <p>Define whether the horizontal edges should be faded when this view
5182 * is scrolled horizontally.</p>
5183 *
5184 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
5185 * be faded when the view is scrolled
5186 * horizontally
5187 *
5188 * @see #isHorizontalFadingEdgeEnabled()
5189 * @attr ref android.R.styleable#View_fadingEdge
5190 */
5191 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
5192 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
5193 if (horizontalFadingEdgeEnabled) {
5194 initScrollCache();
5195 }
5196
5197 mViewFlags ^= FADING_EDGE_HORIZONTAL;
5198 }
5199 }
5200
5201 /**
5202 * <p>Indicate whether the vertical edges are faded when the view is
5203 * scrolled horizontally.</p>
5204 *
5205 * @return true if the vertical edges should are faded on scroll, false
5206 * otherwise
5207 *
5208 * @see #setVerticalFadingEdgeEnabled(boolean)
5209 * @attr ref android.R.styleable#View_fadingEdge
5210 */
5211 public boolean isVerticalFadingEdgeEnabled() {
5212 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
5213 }
5214
5215 /**
5216 * <p>Define whether the vertical edges should be faded when this view
5217 * is scrolled vertically.</p>
5218 *
5219 * @param verticalFadingEdgeEnabled true if the vertical edges should
5220 * be faded when the view is scrolled
5221 * vertically
5222 *
5223 * @see #isVerticalFadingEdgeEnabled()
5224 * @attr ref android.R.styleable#View_fadingEdge
5225 */
5226 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
5227 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
5228 if (verticalFadingEdgeEnabled) {
5229 initScrollCache();
5230 }
5231
5232 mViewFlags ^= FADING_EDGE_VERTICAL;
5233 }
5234 }
5235
5236 /**
5237 * Returns the strength, or intensity, of the top faded edge. The strength is
5238 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
5239 * returns 0.0 or 1.0 but no value in between.
5240 *
5241 * Subclasses should override this method to provide a smoother fade transition
5242 * when scrolling occurs.
5243 *
5244 * @return the intensity of the top fade as a float between 0.0f and 1.0f
5245 */
5246 protected float getTopFadingEdgeStrength() {
5247 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
5248 }
5249
5250 /**
5251 * Returns the strength, or intensity, of the bottom faded edge. The strength is
5252 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
5253 * returns 0.0 or 1.0 but no value in between.
5254 *
5255 * Subclasses should override this method to provide a smoother fade transition
5256 * when scrolling occurs.
5257 *
5258 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
5259 */
5260 protected float getBottomFadingEdgeStrength() {
5261 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
5262 computeVerticalScrollRange() ? 1.0f : 0.0f;
5263 }
5264
5265 /**
5266 * Returns the strength, or intensity, of the left faded edge. The strength is
5267 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
5268 * returns 0.0 or 1.0 but no value in between.
5269 *
5270 * Subclasses should override this method to provide a smoother fade transition
5271 * when scrolling occurs.
5272 *
5273 * @return the intensity of the left fade as a float between 0.0f and 1.0f
5274 */
5275 protected float getLeftFadingEdgeStrength() {
5276 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
5277 }
5278
5279 /**
5280 * Returns the strength, or intensity, of the right faded edge. The strength is
5281 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
5282 * returns 0.0 or 1.0 but no value in between.
5283 *
5284 * Subclasses should override this method to provide a smoother fade transition
5285 * when scrolling occurs.
5286 *
5287 * @return the intensity of the right fade as a float between 0.0f and 1.0f
5288 */
5289 protected float getRightFadingEdgeStrength() {
5290 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
5291 computeHorizontalScrollRange() ? 1.0f : 0.0f;
5292 }
5293
5294 /**
5295 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
5296 * scrollbar is not drawn by default.</p>
5297 *
5298 * @return true if the horizontal scrollbar should be painted, false
5299 * otherwise
5300 *
5301 * @see #setHorizontalScrollBarEnabled(boolean)
5302 */
5303 public boolean isHorizontalScrollBarEnabled() {
5304 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
5305 }
5306
5307 /**
5308 * <p>Define whether the horizontal scrollbar should be drawn or not. The
5309 * scrollbar is not drawn by default.</p>
5310 *
5311 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
5312 * be painted
5313 *
5314 * @see #isHorizontalScrollBarEnabled()
5315 */
5316 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
5317 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
5318 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07005319 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005320 recomputePadding();
5321 }
5322 }
5323
5324 /**
5325 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
5326 * scrollbar is not drawn by default.</p>
5327 *
5328 * @return true if the vertical scrollbar should be painted, false
5329 * otherwise
5330 *
5331 * @see #setVerticalScrollBarEnabled(boolean)
5332 */
5333 public boolean isVerticalScrollBarEnabled() {
5334 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
5335 }
5336
5337 /**
5338 * <p>Define whether the vertical scrollbar should be drawn or not. The
5339 * scrollbar is not drawn by default.</p>
5340 *
5341 * @param verticalScrollBarEnabled true if the vertical scrollbar should
5342 * be painted
5343 *
5344 * @see #isVerticalScrollBarEnabled()
5345 */
5346 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
5347 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
5348 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07005349 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005350 recomputePadding();
5351 }
5352 }
5353
5354 private void recomputePadding() {
5355 setPadding(mPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
5356 }
Mike Cleronfe81d382009-09-28 14:22:16 -07005357
5358 /**
5359 * Define whether scrollbars will fade when the view is not scrolling.
5360 *
5361 * @param fadeScrollbars wheter to enable fading
5362 *
5363 */
5364 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
5365 initScrollCache();
5366 final ScrollabilityCache scrollabilityCache = mScrollCache;
5367 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07005368 if (fadeScrollbars) {
5369 scrollabilityCache.state = ScrollabilityCache.OFF;
5370 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07005371 scrollabilityCache.state = ScrollabilityCache.ON;
5372 }
5373 }
5374
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005375 /**
Mike Cleron52f0a642009-09-28 18:21:37 -07005376 *
5377 * Returns true if scrollbars will fade when this view is not scrolling
5378 *
5379 * @return true if scrollbar fading is enabled
5380 */
5381 public boolean isScrollbarFadingEnabled() {
5382 return mScrollCache != null && mScrollCache.fadeScrollBars;
5383 }
5384
5385 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005386 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
5387 * inset. When inset, they add to the padding of the view. And the scrollbars
5388 * can be drawn inside the padding area or on the edge of the view. For example,
5389 * if a view has a background drawable and you want to draw the scrollbars
5390 * inside the padding specified by the drawable, you can use
5391 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
5392 * appear at the edge of the view, ignoring the padding, then you can use
5393 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
5394 * @param style the style of the scrollbars. Should be one of
5395 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
5396 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
5397 * @see #SCROLLBARS_INSIDE_OVERLAY
5398 * @see #SCROLLBARS_INSIDE_INSET
5399 * @see #SCROLLBARS_OUTSIDE_OVERLAY
5400 * @see #SCROLLBARS_OUTSIDE_INSET
5401 */
5402 public void setScrollBarStyle(int style) {
5403 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
5404 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07005405 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005406 recomputePadding();
5407 }
5408 }
5409
5410 /**
5411 * <p>Returns the current scrollbar style.</p>
5412 * @return the current scrollbar style
5413 * @see #SCROLLBARS_INSIDE_OVERLAY
5414 * @see #SCROLLBARS_INSIDE_INSET
5415 * @see #SCROLLBARS_OUTSIDE_OVERLAY
5416 * @see #SCROLLBARS_OUTSIDE_INSET
5417 */
5418 public int getScrollBarStyle() {
5419 return mViewFlags & SCROLLBARS_STYLE_MASK;
5420 }
5421
5422 /**
5423 * <p>Compute the horizontal range that the horizontal scrollbar
5424 * represents.</p>
5425 *
5426 * <p>The range is expressed in arbitrary units that must be the same as the
5427 * units used by {@link #computeHorizontalScrollExtent()} and
5428 * {@link #computeHorizontalScrollOffset()}.</p>
5429 *
5430 * <p>The default range is the drawing width of this view.</p>
5431 *
5432 * @return the total horizontal range represented by the horizontal
5433 * scrollbar
5434 *
5435 * @see #computeHorizontalScrollExtent()
5436 * @see #computeHorizontalScrollOffset()
5437 * @see android.widget.ScrollBarDrawable
5438 */
5439 protected int computeHorizontalScrollRange() {
5440 return getWidth();
5441 }
5442
5443 /**
5444 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
5445 * within the horizontal range. This value is used to compute the position
5446 * of the thumb within the scrollbar's track.</p>
5447 *
5448 * <p>The range is expressed in arbitrary units that must be the same as the
5449 * units used by {@link #computeHorizontalScrollRange()} and
5450 * {@link #computeHorizontalScrollExtent()}.</p>
5451 *
5452 * <p>The default offset is the scroll offset of this view.</p>
5453 *
5454 * @return the horizontal offset of the scrollbar's thumb
5455 *
5456 * @see #computeHorizontalScrollRange()
5457 * @see #computeHorizontalScrollExtent()
5458 * @see android.widget.ScrollBarDrawable
5459 */
5460 protected int computeHorizontalScrollOffset() {
5461 return mScrollX;
5462 }
5463
5464 /**
5465 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
5466 * within the horizontal range. This value is used to compute the length
5467 * of the thumb within the scrollbar's track.</p>
5468 *
5469 * <p>The range is expressed in arbitrary units that must be the same as the
5470 * units used by {@link #computeHorizontalScrollRange()} and
5471 * {@link #computeHorizontalScrollOffset()}.</p>
5472 *
5473 * <p>The default extent is the drawing width of this view.</p>
5474 *
5475 * @return the horizontal extent of the scrollbar's thumb
5476 *
5477 * @see #computeHorizontalScrollRange()
5478 * @see #computeHorizontalScrollOffset()
5479 * @see android.widget.ScrollBarDrawable
5480 */
5481 protected int computeHorizontalScrollExtent() {
5482 return getWidth();
5483 }
5484
5485 /**
5486 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
5487 *
5488 * <p>The range is expressed in arbitrary units that must be the same as the
5489 * units used by {@link #computeVerticalScrollExtent()} and
5490 * {@link #computeVerticalScrollOffset()}.</p>
5491 *
5492 * @return the total vertical range represented by the vertical scrollbar
5493 *
5494 * <p>The default range is the drawing height of this view.</p>
5495 *
5496 * @see #computeVerticalScrollExtent()
5497 * @see #computeVerticalScrollOffset()
5498 * @see android.widget.ScrollBarDrawable
5499 */
5500 protected int computeVerticalScrollRange() {
5501 return getHeight();
5502 }
5503
5504 /**
5505 * <p>Compute the vertical offset of the vertical scrollbar's thumb
5506 * within the horizontal range. This value is used to compute the position
5507 * of the thumb within the scrollbar's track.</p>
5508 *
5509 * <p>The range is expressed in arbitrary units that must be the same as the
5510 * units used by {@link #computeVerticalScrollRange()} and
5511 * {@link #computeVerticalScrollExtent()}.</p>
5512 *
5513 * <p>The default offset is the scroll offset of this view.</p>
5514 *
5515 * @return the vertical offset of the scrollbar's thumb
5516 *
5517 * @see #computeVerticalScrollRange()
5518 * @see #computeVerticalScrollExtent()
5519 * @see android.widget.ScrollBarDrawable
5520 */
5521 protected int computeVerticalScrollOffset() {
5522 return mScrollY;
5523 }
5524
5525 /**
5526 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
5527 * within the vertical range. This value is used to compute the length
5528 * of the thumb within the scrollbar's track.</p>
5529 *
5530 * <p>The range is expressed in arbitrary units that must be the same as the
5531 * units used by {@link #computeHorizontalScrollRange()} and
5532 * {@link #computeVerticalScrollOffset()}.</p>
5533 *
5534 * <p>The default extent is the drawing height of this view.</p>
5535 *
5536 * @return the vertical extent of the scrollbar's thumb
5537 *
5538 * @see #computeVerticalScrollRange()
5539 * @see #computeVerticalScrollOffset()
5540 * @see android.widget.ScrollBarDrawable
5541 */
5542 protected int computeVerticalScrollExtent() {
5543 return getHeight();
5544 }
5545
5546 /**
5547 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
5548 * scrollbars are painted only if they have been awakened first.</p>
5549 *
5550 * @param canvas the canvas on which to draw the scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -07005551 *
5552 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005553 */
5554 private void onDrawScrollBars(Canvas canvas) {
5555 // scrollbars are drawn only when the animation is running
5556 final ScrollabilityCache cache = mScrollCache;
5557 if (cache != null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07005558
5559 int state = cache.state;
5560
5561 if (state == ScrollabilityCache.OFF) {
5562 return;
5563 }
5564
5565 boolean invalidate = false;
5566
5567 if (state == ScrollabilityCache.FADING) {
5568 // We're fading -- get our fade interpolation
5569 if (cache.interpolatorValues == null) {
5570 cache.interpolatorValues = new float[1];
5571 }
5572
5573 float[] values = cache.interpolatorValues;
5574
5575 // Stops the animation if we're done
5576 if (cache.scrollBarInterpolator.timeToValues(values) ==
5577 Interpolator.Result.FREEZE_END) {
5578 cache.state = ScrollabilityCache.OFF;
5579 } else {
5580 cache.scrollBar.setAlpha(Math.round(values[0]));
5581 }
5582
5583 // This will make the scroll bars inval themselves after
5584 // drawing. We only want this when we're fading so that
5585 // we prevent excessive redraws
5586 invalidate = true;
5587 } else {
5588 // We're just on -- but we may have been fading before so
5589 // reset alpha
5590 cache.scrollBar.setAlpha(255);
5591 }
5592
5593
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 final int viewFlags = mViewFlags;
5595
5596 final boolean drawHorizontalScrollBar =
5597 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
5598 final boolean drawVerticalScrollBar =
5599 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
5600 && !isVerticalScrollBarHidden();
5601
5602 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
5603 final int width = mRight - mLeft;
5604 final int height = mBottom - mTop;
5605
5606 final ScrollBarDrawable scrollBar = cache.scrollBar;
5607 int size = scrollBar.getSize(false);
5608 if (size <= 0) {
5609 size = cache.scrollBarSize;
5610 }
5611
Mike Reede8853fc2009-09-04 14:01:48 -04005612 final int scrollX = mScrollX;
5613 final int scrollY = mScrollY;
5614 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
5615
Mike Cleronf116bf82009-09-27 19:14:12 -07005616 int left, top, right, bottom;
5617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 if (drawHorizontalScrollBar) {
Mike Cleronf116bf82009-09-27 19:14:12 -07005619 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04005620 computeHorizontalScrollOffset(),
5621 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04005622 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07005623 getVerticalScrollbarWidth() : 0;
5624 top = scrollY + height - size - (mUserPaddingBottom & inside);
5625 left = scrollX + (mPaddingLeft & inside);
5626 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
5627 bottom = top + size;
5628 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
5629 if (invalidate) {
5630 invalidate(left, top, right, bottom);
5631 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005632 }
5633
5634 if (drawVerticalScrollBar) {
Mike Reede8853fc2009-09-04 14:01:48 -04005635 scrollBar.setParameters(computeVerticalScrollRange(),
5636 computeVerticalScrollOffset(),
5637 computeVerticalScrollExtent(), true);
5638 // TODO: Deal with RTL languages to position scrollbar on left
Mike Cleronf116bf82009-09-27 19:14:12 -07005639 left = scrollX + width - size - (mUserPaddingRight & inside);
5640 top = scrollY + (mPaddingTop & inside);
5641 right = left + size;
5642 bottom = scrollY + height - (mUserPaddingBottom & inside);
5643 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
5644 if (invalidate) {
5645 invalidate(left, top, right, bottom);
5646 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 }
5648 }
5649 }
5650 }
Romain Guy8506ab42009-06-11 17:35:47 -07005651
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005652 /**
Romain Guy8506ab42009-06-11 17:35:47 -07005653 * 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 -08005654 * FastScroller is visible.
5655 * @return whether to temporarily hide the vertical scrollbar
5656 * @hide
5657 */
5658 protected boolean isVerticalScrollBarHidden() {
5659 return false;
5660 }
5661
5662 /**
5663 * <p>Draw the horizontal scrollbar if
5664 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
5665 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 * @param canvas the canvas on which to draw the scrollbar
5667 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668 *
5669 * @see #isHorizontalScrollBarEnabled()
5670 * @see #computeHorizontalScrollRange()
5671 * @see #computeHorizontalScrollExtent()
5672 * @see #computeHorizontalScrollOffset()
5673 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07005674 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04005675 */
Mike Reede8853fc2009-09-04 14:01:48 -04005676 protected void onDrawHorizontalScrollBar(Canvas canvas,
5677 Drawable scrollBar,
5678 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04005679 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04005680 scrollBar.draw(canvas);
5681 }
Mike Reede8853fc2009-09-04 14:01:48 -04005682
Mike Reed4d6fe5f2009-09-03 13:29:05 -04005683 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
5685 * returns true.</p>
5686 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 * @param canvas the canvas on which to draw the scrollbar
5688 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005689 *
5690 * @see #isVerticalScrollBarEnabled()
5691 * @see #computeVerticalScrollRange()
5692 * @see #computeVerticalScrollExtent()
5693 * @see #computeVerticalScrollOffset()
5694 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04005695 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005696 */
Mike Reede8853fc2009-09-04 14:01:48 -04005697 protected void onDrawVerticalScrollBar(Canvas canvas,
5698 Drawable scrollBar,
5699 int l, int t, int r, int b) {
5700 scrollBar.setBounds(l, t, r, b);
5701 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 }
5703
5704 /**
5705 * Implement this to do your drawing.
5706 *
5707 * @param canvas the canvas on which the background will be drawn
5708 */
5709 protected void onDraw(Canvas canvas) {
5710 }
5711
5712 /*
5713 * Caller is responsible for calling requestLayout if necessary.
5714 * (This allows addViewInLayout to not request a new layout.)
5715 */
5716 void assignParent(ViewParent parent) {
5717 if (mParent == null) {
5718 mParent = parent;
5719 } else if (parent == null) {
5720 mParent = null;
5721 } else {
5722 throw new RuntimeException("view " + this + " being added, but"
5723 + " it already has a parent");
5724 }
5725 }
5726
5727 /**
5728 * This is called when the view is attached to a window. At this point it
5729 * has a Surface and will start drawing. Note that this function is
5730 * guaranteed to be called before {@link #onDraw}, however it may be called
5731 * any time before the first onDraw -- including before or after
5732 * {@link #onMeasure}.
5733 *
5734 * @see #onDetachedFromWindow()
5735 */
5736 protected void onAttachedToWindow() {
5737 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
5738 mParent.requestTransparentRegion(this);
5739 }
5740 }
5741
5742 /**
5743 * This is called when the view is detached from a window. At this point it
5744 * no longer has a surface for drawing.
5745 *
5746 * @see #onAttachedToWindow()
5747 */
5748 protected void onDetachedFromWindow() {
5749 if (mPendingCheckForLongPress != null) {
5750 removeCallbacks(mPendingCheckForLongPress);
5751 }
5752 destroyDrawingCache();
5753 }
5754
5755 /**
5756 * @return The number of times this view has been attached to a window
5757 */
5758 protected int getWindowAttachCount() {
5759 return mWindowAttachCount;
5760 }
5761
5762 /**
5763 * Retrieve a unique token identifying the window this view is attached to.
5764 * @return Return the window's token for use in
5765 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
5766 */
5767 public IBinder getWindowToken() {
5768 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
5769 }
5770
5771 /**
5772 * Retrieve a unique token identifying the top-level "real" window of
5773 * the window that this view is attached to. That is, this is like
5774 * {@link #getWindowToken}, except if the window this view in is a panel
5775 * window (attached to another containing window), then the token of
5776 * the containing window is returned instead.
5777 *
5778 * @return Returns the associated window token, either
5779 * {@link #getWindowToken()} or the containing window's token.
5780 */
5781 public IBinder getApplicationWindowToken() {
5782 AttachInfo ai = mAttachInfo;
5783 if (ai != null) {
5784 IBinder appWindowToken = ai.mPanelParentWindowToken;
5785 if (appWindowToken == null) {
5786 appWindowToken = ai.mWindowToken;
5787 }
5788 return appWindowToken;
5789 }
5790 return null;
5791 }
5792
5793 /**
5794 * Retrieve private session object this view hierarchy is using to
5795 * communicate with the window manager.
5796 * @return the session object to communicate with the window manager
5797 */
5798 /*package*/ IWindowSession getWindowSession() {
5799 return mAttachInfo != null ? mAttachInfo.mSession : null;
5800 }
5801
5802 /**
5803 * @param info the {@link android.view.View.AttachInfo} to associated with
5804 * this view
5805 */
5806 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
5807 //System.out.println("Attached! " + this);
5808 mAttachInfo = info;
5809 mWindowAttachCount++;
5810 if (mFloatingTreeObserver != null) {
5811 info.mTreeObserver.merge(mFloatingTreeObserver);
5812 mFloatingTreeObserver = null;
5813 }
5814 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
5815 mAttachInfo.mScrollContainers.add(this);
5816 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
5817 }
5818 performCollectViewAttributes(visibility);
5819 onAttachedToWindow();
5820 int vis = info.mWindowVisibility;
5821 if (vis != GONE) {
5822 onWindowVisibilityChanged(vis);
5823 }
5824 }
5825
5826 void dispatchDetachedFromWindow() {
5827 //System.out.println("Detached! " + this);
5828 AttachInfo info = mAttachInfo;
5829 if (info != null) {
5830 int vis = info.mWindowVisibility;
5831 if (vis != GONE) {
5832 onWindowVisibilityChanged(GONE);
5833 }
5834 }
5835
5836 onDetachedFromWindow();
5837 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
5838 mAttachInfo.mScrollContainers.remove(this);
5839 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
5840 }
5841 mAttachInfo = null;
5842 }
5843
5844 /**
5845 * Store this view hierarchy's frozen state into the given container.
5846 *
5847 * @param container The SparseArray in which to save the view's state.
5848 *
5849 * @see #restoreHierarchyState
5850 * @see #dispatchSaveInstanceState
5851 * @see #onSaveInstanceState
5852 */
5853 public void saveHierarchyState(SparseArray<Parcelable> container) {
5854 dispatchSaveInstanceState(container);
5855 }
5856
5857 /**
5858 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
5859 * May be overridden to modify how freezing happens to a view's children; for example, some
5860 * views may want to not store state for their children.
5861 *
5862 * @param container The SparseArray in which to save the view's state.
5863 *
5864 * @see #dispatchRestoreInstanceState
5865 * @see #saveHierarchyState
5866 * @see #onSaveInstanceState
5867 */
5868 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
5869 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
5870 mPrivateFlags &= ~SAVE_STATE_CALLED;
5871 Parcelable state = onSaveInstanceState();
5872 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
5873 throw new IllegalStateException(
5874 "Derived class did not call super.onSaveInstanceState()");
5875 }
5876 if (state != null) {
5877 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
5878 // + ": " + state);
5879 container.put(mID, state);
5880 }
5881 }
5882 }
5883
5884 /**
5885 * Hook allowing a view to generate a representation of its internal state
5886 * that can later be used to create a new instance with that same state.
5887 * This state should only contain information that is not persistent or can
5888 * not be reconstructed later. For example, you will never store your
5889 * current position on screen because that will be computed again when a
5890 * new instance of the view is placed in its view hierarchy.
5891 * <p>
5892 * Some examples of things you may store here: the current cursor position
5893 * in a text view (but usually not the text itself since that is stored in a
5894 * content provider or other persistent storage), the currently selected
5895 * item in a list view.
5896 *
5897 * @return Returns a Parcelable object containing the view's current dynamic
5898 * state, or null if there is nothing interesting to save. The
5899 * default implementation returns null.
5900 * @see #onRestoreInstanceState
5901 * @see #saveHierarchyState
5902 * @see #dispatchSaveInstanceState
5903 * @see #setSaveEnabled(boolean)
5904 */
5905 protected Parcelable onSaveInstanceState() {
5906 mPrivateFlags |= SAVE_STATE_CALLED;
5907 return BaseSavedState.EMPTY_STATE;
5908 }
5909
5910 /**
5911 * Restore this view hierarchy's frozen state from the given container.
5912 *
5913 * @param container The SparseArray which holds previously frozen states.
5914 *
5915 * @see #saveHierarchyState
5916 * @see #dispatchRestoreInstanceState
5917 * @see #onRestoreInstanceState
5918 */
5919 public void restoreHierarchyState(SparseArray<Parcelable> container) {
5920 dispatchRestoreInstanceState(container);
5921 }
5922
5923 /**
5924 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
5925 * children. May be overridden to modify how restoreing happens to a view's children; for
5926 * example, some views may want to not store state for their children.
5927 *
5928 * @param container The SparseArray which holds previously saved state.
5929 *
5930 * @see #dispatchSaveInstanceState
5931 * @see #restoreHierarchyState
5932 * @see #onRestoreInstanceState
5933 */
5934 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
5935 if (mID != NO_ID) {
5936 Parcelable state = container.get(mID);
5937 if (state != null) {
5938 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
5939 // + ": " + state);
5940 mPrivateFlags &= ~SAVE_STATE_CALLED;
5941 onRestoreInstanceState(state);
5942 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
5943 throw new IllegalStateException(
5944 "Derived class did not call super.onRestoreInstanceState()");
5945 }
5946 }
5947 }
5948 }
5949
5950 /**
5951 * Hook allowing a view to re-apply a representation of its internal state that had previously
5952 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
5953 * null state.
5954 *
5955 * @param state The frozen state that had previously been returned by
5956 * {@link #onSaveInstanceState}.
5957 *
5958 * @see #onSaveInstanceState
5959 * @see #restoreHierarchyState
5960 * @see #dispatchRestoreInstanceState
5961 */
5962 protected void onRestoreInstanceState(Parcelable state) {
5963 mPrivateFlags |= SAVE_STATE_CALLED;
5964 if (state != BaseSavedState.EMPTY_STATE && state != null) {
5965 throw new IllegalArgumentException("Wrong state class -- expecting View State");
5966 }
5967 }
5968
5969 /**
5970 * <p>Return the time at which the drawing of the view hierarchy started.</p>
5971 *
5972 * @return the drawing start time in milliseconds
5973 */
5974 public long getDrawingTime() {
5975 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
5976 }
5977
5978 /**
5979 * <p>Enables or disables the duplication of the parent's state into this view. When
5980 * duplication is enabled, this view gets its drawable state from its parent rather
5981 * than from its own internal properties.</p>
5982 *
5983 * <p>Note: in the current implementation, setting this property to true after the
5984 * view was added to a ViewGroup might have no effect at all. This property should
5985 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
5986 *
5987 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
5988 * property is enabled, an exception will be thrown.</p>
5989 *
5990 * @param enabled True to enable duplication of the parent's drawable state, false
5991 * to disable it.
5992 *
5993 * @see #getDrawableState()
5994 * @see #isDuplicateParentStateEnabled()
5995 */
5996 public void setDuplicateParentStateEnabled(boolean enabled) {
5997 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
5998 }
5999
6000 /**
6001 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
6002 *
6003 * @return True if this view's drawable state is duplicated from the parent,
6004 * false otherwise
6005 *
6006 * @see #getDrawableState()
6007 * @see #setDuplicateParentStateEnabled(boolean)
6008 */
6009 public boolean isDuplicateParentStateEnabled() {
6010 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
6011 }
6012
6013 /**
6014 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
6015 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
6016 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
6017 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
6018 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
6019 * null.</p>
6020 *
6021 * @param enabled true to enable the drawing cache, false otherwise
6022 *
6023 * @see #isDrawingCacheEnabled()
6024 * @see #getDrawingCache()
6025 * @see #buildDrawingCache()
6026 */
6027 public void setDrawingCacheEnabled(boolean enabled) {
6028 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
6029 }
6030
6031 /**
6032 * <p>Indicates whether the drawing cache is enabled for this view.</p>
6033 *
6034 * @return true if the drawing cache is enabled
6035 *
6036 * @see #setDrawingCacheEnabled(boolean)
6037 * @see #getDrawingCache()
6038 */
6039 @ViewDebug.ExportedProperty
6040 public boolean isDrawingCacheEnabled() {
6041 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
6042 }
6043
6044 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07006045 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
6046 *
6047 * @return A non-scaled bitmap representing this view or null if cache is disabled.
6048 *
6049 * @see #getDrawingCache(boolean)
6050 */
6051 public Bitmap getDrawingCache() {
6052 return getDrawingCache(false);
6053 }
6054
6055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006056 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
6057 * is null when caching is disabled. If caching is enabled and the cache is not ready,
6058 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
6059 * draw from the cache when the cache is enabled. To benefit from the cache, you must
6060 * request the drawing cache by calling this method and draw it on screen if the
6061 * returned bitmap is not null.</p>
Romain Guyfbd8f692009-06-26 14:51:58 -07006062 *
6063 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
6064 * this method will create a bitmap of the same size as this view. Because this bitmap
6065 * will be drawn scaled by the parent ViewGroup, the result on screen might show
6066 * scaling artifacts. To avoid such artifacts, you should call this method by setting
6067 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
6068 * size than the view. This implies that your application must be able to handle this
6069 * size.</p>
6070 *
6071 * @param autoScale Indicates whether the generated bitmap should be scaled based on
6072 * the current density of the screen when the application is in compatibility
6073 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006074 *
Romain Guyfbd8f692009-06-26 14:51:58 -07006075 * @return A bitmap representing this view or null if cache is disabled.
6076 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006077 * @see #setDrawingCacheEnabled(boolean)
6078 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07006079 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006080 * @see #destroyDrawingCache()
6081 */
Romain Guyfbd8f692009-06-26 14:51:58 -07006082 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006083 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
6084 return null;
6085 }
6086 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07006087 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006088 }
Romain Guyfbd8f692009-06-26 14:51:58 -07006089 return autoScale ? (mDrawingCache == null ? null : mDrawingCache.get()) :
6090 (mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006091 }
6092
6093 /**
6094 * <p>Frees the resources used by the drawing cache. If you call
6095 * {@link #buildDrawingCache()} manually without calling
6096 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
6097 * should cleanup the cache with this method afterwards.</p>
6098 *
6099 * @see #setDrawingCacheEnabled(boolean)
6100 * @see #buildDrawingCache()
6101 * @see #getDrawingCache()
6102 */
6103 public void destroyDrawingCache() {
6104 if (mDrawingCache != null) {
6105 final Bitmap bitmap = mDrawingCache.get();
6106 if (bitmap != null) bitmap.recycle();
6107 mDrawingCache = null;
6108 }
Romain Guyfbd8f692009-06-26 14:51:58 -07006109 if (mUnscaledDrawingCache != null) {
6110 final Bitmap bitmap = mUnscaledDrawingCache.get();
6111 if (bitmap != null) bitmap.recycle();
6112 mUnscaledDrawingCache = null;
6113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006114 }
6115
6116 /**
6117 * Setting a solid background color for the drawing cache's bitmaps will improve
6118 * perfromance and memory usage. Note, though that this should only be used if this
6119 * view will always be drawn on top of a solid color.
6120 *
6121 * @param color The background color to use for the drawing cache's bitmap
6122 *
6123 * @see #setDrawingCacheEnabled(boolean)
6124 * @see #buildDrawingCache()
6125 * @see #getDrawingCache()
6126 */
6127 public void setDrawingCacheBackgroundColor(int color) {
6128 mDrawingCacheBackgroundColor = color;
6129 }
6130
6131 /**
6132 * @see #setDrawingCacheBackgroundColor(int)
6133 *
6134 * @return The background color to used for the drawing cache's bitmap
6135 */
6136 public int getDrawingCacheBackgroundColor() {
6137 return mDrawingCacheBackgroundColor;
6138 }
6139
6140 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07006141 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
6142 *
6143 * @see #buildDrawingCache(boolean)
6144 */
6145 public void buildDrawingCache() {
6146 buildDrawingCache(false);
6147 }
6148
6149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006150 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
6151 *
6152 * <p>If you call {@link #buildDrawingCache()} manually without calling
6153 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
6154 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Romain Guyfbd8f692009-06-26 14:51:58 -07006155 *
6156 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
6157 * this method will create a bitmap of the same size as this view. Because this bitmap
6158 * will be drawn scaled by the parent ViewGroup, the result on screen might show
6159 * scaling artifacts. To avoid such artifacts, you should call this method by setting
6160 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
6161 * size than the view. This implies that your application must be able to handle this
6162 * size.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006163 *
6164 * @see #getDrawingCache()
6165 * @see #destroyDrawingCache()
6166 */
Romain Guyfbd8f692009-06-26 14:51:58 -07006167 public void buildDrawingCache(boolean autoScale) {
6168 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
6169 (mDrawingCache == null || mDrawingCache.get() == null) :
6170 (mUnscaledDrawingCache == null || mUnscaledDrawingCache.get() == null))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006171
6172 if (ViewDebug.TRACE_HIERARCHY) {
6173 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
6174 }
Romain Guy13922e02009-05-12 17:56:14 -07006175 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006176 EventLog.writeEvent(60002, hashCode());
6177 }
6178
Romain Guy8506ab42009-06-11 17:35:47 -07006179 int width = mRight - mLeft;
6180 int height = mBottom - mTop;
6181
6182 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07006183 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07006184
Romain Guye1123222009-06-29 14:24:56 -07006185 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07006186 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
6187 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07006188 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006189
6190 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
6191 final boolean opaque = drawingCacheBackgroundColor != 0 ||
6192 (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE);
6193
6194 if (width <= 0 || height <= 0 ||
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006195 (width * height * (opaque ? 2 : 4) > // Projected bitmap size in bytes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006196 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
6197 destroyDrawingCache();
6198 return;
6199 }
6200
6201 boolean clear = true;
Romain Guyfbd8f692009-06-26 14:51:58 -07006202 Bitmap bitmap = autoScale ? (mDrawingCache == null ? null : mDrawingCache.get()) :
6203 (mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006204
6205 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
6206
6207 Bitmap.Config quality;
6208 if (!opaque) {
6209 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
6210 case DRAWING_CACHE_QUALITY_AUTO:
6211 quality = Bitmap.Config.ARGB_8888;
6212 break;
6213 case DRAWING_CACHE_QUALITY_LOW:
6214 quality = Bitmap.Config.ARGB_4444;
6215 break;
6216 case DRAWING_CACHE_QUALITY_HIGH:
6217 quality = Bitmap.Config.ARGB_8888;
6218 break;
6219 default:
6220 quality = Bitmap.Config.ARGB_8888;
6221 break;
6222 }
6223 } else {
6224 quality = Bitmap.Config.RGB_565;
6225 }
6226
6227 // Try to cleanup memory
6228 if (bitmap != null) bitmap.recycle();
6229
6230 try {
6231 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07006232 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07006233 if (autoScale) {
6234 mDrawingCache = new SoftReference<Bitmap>(bitmap);
6235 } else {
6236 mUnscaledDrawingCache = new SoftReference<Bitmap>(bitmap);
6237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006238 } catch (OutOfMemoryError e) {
6239 // If there is not enough memory to create the bitmap cache, just
6240 // ignore the issue as bitmap caches are not required to draw the
6241 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07006242 if (autoScale) {
6243 mDrawingCache = null;
6244 } else {
6245 mUnscaledDrawingCache = null;
6246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 return;
6248 }
6249
6250 clear = drawingCacheBackgroundColor != 0;
6251 }
6252
6253 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006254 if (attachInfo != null) {
6255 canvas = attachInfo.mCanvas;
6256 if (canvas == null) {
6257 canvas = new Canvas();
6258 }
6259 canvas.setBitmap(bitmap);
6260 // Temporarily clobber the cached Canvas in case one of our children
6261 // is also using a drawing cache. Without this, the children would
6262 // steal the canvas by attaching their own bitmap to it and bad, bad
6263 // thing would happen (invisible views, corrupted drawings, etc.)
6264 attachInfo.mCanvas = null;
6265 } else {
6266 // This case should hopefully never or seldom happen
6267 canvas = new Canvas(bitmap);
6268 }
6269
6270 if (clear) {
6271 bitmap.eraseColor(drawingCacheBackgroundColor);
6272 }
6273
6274 computeScroll();
6275 final int restoreCount = canvas.save();
Romain Guyfbd8f692009-06-26 14:51:58 -07006276
Romain Guye1123222009-06-29 14:24:56 -07006277 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07006278 final float scale = attachInfo.mApplicationScale;
6279 canvas.scale(scale, scale);
6280 }
6281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 canvas.translate(-mScrollX, -mScrollY);
6283
Romain Guy5bcdff42009-05-14 21:27:18 -07006284 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285
6286 // Fast path for layouts with no backgrounds
6287 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
6288 if (ViewDebug.TRACE_HIERARCHY) {
6289 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
6290 }
Romain Guy5bcdff42009-05-14 21:27:18 -07006291 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 dispatchDraw(canvas);
6293 } else {
6294 draw(canvas);
6295 }
6296
6297 canvas.restoreToCount(restoreCount);
6298
6299 if (attachInfo != null) {
6300 // Restore the cached Canvas for our siblings
6301 attachInfo.mCanvas = canvas;
6302 }
6303 mPrivateFlags |= DRAWING_CACHE_VALID;
6304 }
6305 }
6306
6307 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006308 * Create a snapshot of the view into a bitmap. We should probably make
6309 * some form of this public, but should think about the API.
6310 */
Romain Guya2431d02009-04-30 16:30:00 -07006311 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07006312 int width = mRight - mLeft;
6313 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006314
Dianne Hackborn8cae1242009-09-10 14:32:16 -07006315 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07006316 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07006317 width = (int) ((width * scale) + 0.5f);
6318 height = (int) ((height * scale) + 0.5f);
6319
Romain Guy8c11e312009-09-14 15:15:30 -07006320 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006321 if (bitmap == null) {
6322 throw new OutOfMemoryError();
6323 }
6324
Dianne Hackborn8cae1242009-09-10 14:32:16 -07006325 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
6326
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006327 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006328 if (attachInfo != null) {
6329 canvas = attachInfo.mCanvas;
6330 if (canvas == null) {
6331 canvas = new Canvas();
6332 }
6333 canvas.setBitmap(bitmap);
6334 // Temporarily clobber the cached Canvas in case one of our children
6335 // is also using a drawing cache. Without this, the children would
6336 // steal the canvas by attaching their own bitmap to it and bad, bad
6337 // things would happen (invisible views, corrupted drawings, etc.)
6338 attachInfo.mCanvas = null;
6339 } else {
6340 // This case should hopefully never or seldom happen
6341 canvas = new Canvas(bitmap);
6342 }
6343
Romain Guy5bcdff42009-05-14 21:27:18 -07006344 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006345 bitmap.eraseColor(backgroundColor);
6346 }
6347
6348 computeScroll();
6349 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07006350 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006351 canvas.translate(-mScrollX, -mScrollY);
6352
Romain Guy5bcdff42009-05-14 21:27:18 -07006353 // Temporarily remove the dirty mask
6354 int flags = mPrivateFlags;
6355 mPrivateFlags &= ~DIRTY_MASK;
6356
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006357 // Fast path for layouts with no backgrounds
6358 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
6359 dispatchDraw(canvas);
6360 } else {
6361 draw(canvas);
6362 }
6363
Romain Guy5bcdff42009-05-14 21:27:18 -07006364 mPrivateFlags = flags;
6365
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006366 canvas.restoreToCount(restoreCount);
6367
6368 if (attachInfo != null) {
6369 // Restore the cached Canvas for our siblings
6370 attachInfo.mCanvas = canvas;
6371 }
Romain Guy8506ab42009-06-11 17:35:47 -07006372
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07006373 return bitmap;
6374 }
6375
6376 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006377 * Indicates whether this View is currently in edit mode. A View is usually
6378 * in edit mode when displayed within a developer tool. For instance, if
6379 * this View is being drawn by a visual user interface builder, this method
6380 * should return true.
6381 *
6382 * Subclasses should check the return value of this method to provide
6383 * different behaviors if their normal behavior might interfere with the
6384 * host environment. For instance: the class spawns a thread in its
6385 * constructor, the drawing code relies on device-specific features, etc.
6386 *
6387 * This method is usually checked in the drawing code of custom widgets.
6388 *
6389 * @return True if this View is in edit mode, false otherwise.
6390 */
6391 public boolean isInEditMode() {
6392 return false;
6393 }
6394
6395 /**
6396 * If the View draws content inside its padding and enables fading edges,
6397 * it needs to support padding offsets. Padding offsets are added to the
6398 * fading edges to extend the length of the fade so that it covers pixels
6399 * drawn inside the padding.
6400 *
6401 * Subclasses of this class should override this method if they need
6402 * to draw content inside the padding.
6403 *
6404 * @return True if padding offset must be applied, false otherwise.
6405 *
6406 * @see #getLeftPaddingOffset()
6407 * @see #getRightPaddingOffset()
6408 * @see #getTopPaddingOffset()
6409 * @see #getBottomPaddingOffset()
6410 *
6411 * @since CURRENT
6412 */
6413 protected boolean isPaddingOffsetRequired() {
6414 return false;
6415 }
6416
6417 /**
6418 * Amount by which to extend the left fading region. Called only when
6419 * {@link #isPaddingOffsetRequired()} returns true.
6420 *
6421 * @return The left padding offset in pixels.
6422 *
6423 * @see #isPaddingOffsetRequired()
6424 *
6425 * @since CURRENT
6426 */
6427 protected int getLeftPaddingOffset() {
6428 return 0;
6429 }
6430
6431 /**
6432 * Amount by which to extend the right fading region. Called only when
6433 * {@link #isPaddingOffsetRequired()} returns true.
6434 *
6435 * @return The right padding offset in pixels.
6436 *
6437 * @see #isPaddingOffsetRequired()
6438 *
6439 * @since CURRENT
6440 */
6441 protected int getRightPaddingOffset() {
6442 return 0;
6443 }
6444
6445 /**
6446 * Amount by which to extend the top fading region. Called only when
6447 * {@link #isPaddingOffsetRequired()} returns true.
6448 *
6449 * @return The top padding offset in pixels.
6450 *
6451 * @see #isPaddingOffsetRequired()
6452 *
6453 * @since CURRENT
6454 */
6455 protected int getTopPaddingOffset() {
6456 return 0;
6457 }
6458
6459 /**
6460 * Amount by which to extend the bottom fading region. Called only when
6461 * {@link #isPaddingOffsetRequired()} returns true.
6462 *
6463 * @return The bottom padding offset in pixels.
6464 *
6465 * @see #isPaddingOffsetRequired()
6466 *
6467 * @since CURRENT
6468 */
6469 protected int getBottomPaddingOffset() {
6470 return 0;
6471 }
6472
6473 /**
6474 * Manually render this view (and all of its children) to the given Canvas.
6475 * The view must have already done a full layout before this function is
6476 * called. When implementing a view, do not override this method; instead,
6477 * you should implement {@link #onDraw}.
6478 *
6479 * @param canvas The Canvas to which the View is rendered.
6480 */
6481 public void draw(Canvas canvas) {
6482 if (ViewDebug.TRACE_HIERARCHY) {
6483 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
6484 }
6485
Romain Guy5bcdff42009-05-14 21:27:18 -07006486 final int privateFlags = mPrivateFlags;
6487 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
6488 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
6489 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07006490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 /*
6492 * Draw traversal performs several drawing steps which must be executed
6493 * in the appropriate order:
6494 *
6495 * 1. Draw the background
6496 * 2. If necessary, save the canvas' layers to prepare for fading
6497 * 3. Draw view's content
6498 * 4. Draw children
6499 * 5. If necessary, draw the fading edges and restore layers
6500 * 6. Draw decorations (scrollbars for instance)
6501 */
6502
6503 // Step 1, draw the background, if needed
6504 int saveCount;
6505
Romain Guy24443ea2009-05-11 11:56:30 -07006506 if (!dirtyOpaque) {
6507 final Drawable background = mBGDrawable;
6508 if (background != null) {
6509 final int scrollX = mScrollX;
6510 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511
Romain Guy24443ea2009-05-11 11:56:30 -07006512 if (mBackgroundSizeChanged) {
6513 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
6514 mBackgroundSizeChanged = false;
6515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006516
Romain Guy24443ea2009-05-11 11:56:30 -07006517 if ((scrollX | scrollY) == 0) {
6518 background.draw(canvas);
6519 } else {
6520 canvas.translate(scrollX, scrollY);
6521 background.draw(canvas);
6522 canvas.translate(-scrollX, -scrollY);
6523 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006524 }
6525 }
6526
6527 // skip step 2 & 5 if possible (common case)
6528 final int viewFlags = mViewFlags;
6529 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
6530 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
6531 if (!verticalEdges && !horizontalEdges) {
6532 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07006533 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534
6535 // Step 4, draw the children
6536 dispatchDraw(canvas);
6537
6538 // Step 6, draw decorations (scrollbars)
6539 onDrawScrollBars(canvas);
6540
6541 // we're done...
6542 return;
6543 }
6544
6545 /*
6546 * Here we do the full fledged routine...
6547 * (this is an uncommon case where speed matters less,
6548 * this is why we repeat some of the tests that have been
6549 * done above)
6550 */
6551
6552 boolean drawTop = false;
6553 boolean drawBottom = false;
6554 boolean drawLeft = false;
6555 boolean drawRight = false;
6556
6557 float topFadeStrength = 0.0f;
6558 float bottomFadeStrength = 0.0f;
6559 float leftFadeStrength = 0.0f;
6560 float rightFadeStrength = 0.0f;
6561
6562 // Step 2, save the canvas' layers
6563 int paddingLeft = mPaddingLeft;
6564 int paddingTop = mPaddingTop;
6565
6566 final boolean offsetRequired = isPaddingOffsetRequired();
6567 if (offsetRequired) {
6568 paddingLeft += getLeftPaddingOffset();
6569 paddingTop += getTopPaddingOffset();
6570 }
6571
6572 int left = mScrollX + paddingLeft;
6573 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
6574 int top = mScrollY + paddingTop;
6575 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
6576
6577 if (offsetRequired) {
6578 right += getRightPaddingOffset();
6579 bottom += getBottomPaddingOffset();
6580 }
6581
6582 final ScrollabilityCache scrollabilityCache = mScrollCache;
6583 int length = scrollabilityCache.fadingEdgeLength;
6584
6585 // clip the fade length if top and bottom fades overlap
6586 // overlapping fades produce odd-looking artifacts
6587 if (verticalEdges && (top + length > bottom - length)) {
6588 length = (bottom - top) / 2;
6589 }
6590
6591 // also clip horizontal fades if necessary
6592 if (horizontalEdges && (left + length > right - length)) {
6593 length = (right - left) / 2;
6594 }
6595
6596 if (verticalEdges) {
6597 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
6598 drawTop = topFadeStrength >= 0.0f;
6599 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
6600 drawBottom = bottomFadeStrength >= 0.0f;
6601 }
6602
6603 if (horizontalEdges) {
6604 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
6605 drawLeft = leftFadeStrength >= 0.0f;
6606 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
6607 drawRight = rightFadeStrength >= 0.0f;
6608 }
6609
6610 saveCount = canvas.getSaveCount();
6611
6612 int solidColor = getSolidColor();
6613 if (solidColor == 0) {
6614 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
6615
6616 if (drawTop) {
6617 canvas.saveLayer(left, top, right, top + length, null, flags);
6618 }
6619
6620 if (drawBottom) {
6621 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
6622 }
6623
6624 if (drawLeft) {
6625 canvas.saveLayer(left, top, left + length, bottom, null, flags);
6626 }
6627
6628 if (drawRight) {
6629 canvas.saveLayer(right - length, top, right, bottom, null, flags);
6630 }
6631 } else {
6632 scrollabilityCache.setFadeColor(solidColor);
6633 }
6634
6635 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07006636 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637
6638 // Step 4, draw the children
6639 dispatchDraw(canvas);
6640
6641 // Step 5, draw the fade effect and restore layers
6642 final Paint p = scrollabilityCache.paint;
6643 final Matrix matrix = scrollabilityCache.matrix;
6644 final Shader fade = scrollabilityCache.shader;
6645 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
6646
6647 if (drawTop) {
6648 matrix.setScale(1, fadeHeight * topFadeStrength);
6649 matrix.postTranslate(left, top);
6650 fade.setLocalMatrix(matrix);
6651 canvas.drawRect(left, top, right, top + length, p);
6652 }
6653
6654 if (drawBottom) {
6655 matrix.setScale(1, fadeHeight * bottomFadeStrength);
6656 matrix.postRotate(180);
6657 matrix.postTranslate(left, bottom);
6658 fade.setLocalMatrix(matrix);
6659 canvas.drawRect(left, bottom - length, right, bottom, p);
6660 }
6661
6662 if (drawLeft) {
6663 matrix.setScale(1, fadeHeight * leftFadeStrength);
6664 matrix.postRotate(-90);
6665 matrix.postTranslate(left, top);
6666 fade.setLocalMatrix(matrix);
6667 canvas.drawRect(left, top, left + length, bottom, p);
6668 }
6669
6670 if (drawRight) {
6671 matrix.setScale(1, fadeHeight * rightFadeStrength);
6672 matrix.postRotate(90);
6673 matrix.postTranslate(right, top);
6674 fade.setLocalMatrix(matrix);
6675 canvas.drawRect(right - length, top, right, bottom, p);
6676 }
6677
6678 canvas.restoreToCount(saveCount);
6679
6680 // Step 6, draw decorations (scrollbars)
6681 onDrawScrollBars(canvas);
6682 }
6683
6684 /**
6685 * Override this if your view is known to always be drawn on top of a solid color background,
6686 * and needs to draw fading edges. Returning a non-zero color enables the view system to
6687 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
6688 * should be set to 0xFF.
6689 *
6690 * @see #setVerticalFadingEdgeEnabled
6691 * @see #setHorizontalFadingEdgeEnabled
6692 *
6693 * @return The known solid color background for this view, or 0 if the color may vary
6694 */
6695 public int getSolidColor() {
6696 return 0;
6697 }
6698
6699 /**
6700 * Build a human readable string representation of the specified view flags.
6701 *
6702 * @param flags the view flags to convert to a string
6703 * @return a String representing the supplied flags
6704 */
6705 private static String printFlags(int flags) {
6706 String output = "";
6707 int numFlags = 0;
6708 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
6709 output += "TAKES_FOCUS";
6710 numFlags++;
6711 }
6712
6713 switch (flags & VISIBILITY_MASK) {
6714 case INVISIBLE:
6715 if (numFlags > 0) {
6716 output += " ";
6717 }
6718 output += "INVISIBLE";
6719 // USELESS HERE numFlags++;
6720 break;
6721 case GONE:
6722 if (numFlags > 0) {
6723 output += " ";
6724 }
6725 output += "GONE";
6726 // USELESS HERE numFlags++;
6727 break;
6728 default:
6729 break;
6730 }
6731 return output;
6732 }
6733
6734 /**
6735 * Build a human readable string representation of the specified private
6736 * view flags.
6737 *
6738 * @param privateFlags the private view flags to convert to a string
6739 * @return a String representing the supplied flags
6740 */
6741 private static String printPrivateFlags(int privateFlags) {
6742 String output = "";
6743 int numFlags = 0;
6744
6745 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
6746 output += "WANTS_FOCUS";
6747 numFlags++;
6748 }
6749
6750 if ((privateFlags & FOCUSED) == FOCUSED) {
6751 if (numFlags > 0) {
6752 output += " ";
6753 }
6754 output += "FOCUSED";
6755 numFlags++;
6756 }
6757
6758 if ((privateFlags & SELECTED) == SELECTED) {
6759 if (numFlags > 0) {
6760 output += " ";
6761 }
6762 output += "SELECTED";
6763 numFlags++;
6764 }
6765
6766 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
6767 if (numFlags > 0) {
6768 output += " ";
6769 }
6770 output += "IS_ROOT_NAMESPACE";
6771 numFlags++;
6772 }
6773
6774 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
6775 if (numFlags > 0) {
6776 output += " ";
6777 }
6778 output += "HAS_BOUNDS";
6779 numFlags++;
6780 }
6781
6782 if ((privateFlags & DRAWN) == DRAWN) {
6783 if (numFlags > 0) {
6784 output += " ";
6785 }
6786 output += "DRAWN";
6787 // USELESS HERE numFlags++;
6788 }
6789 return output;
6790 }
6791
6792 /**
6793 * <p>Indicates whether or not this view's layout will be requested during
6794 * the next hierarchy layout pass.</p>
6795 *
6796 * @return true if the layout will be forced during next layout pass
6797 */
6798 public boolean isLayoutRequested() {
6799 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
6800 }
6801
6802 /**
6803 * Assign a size and position to a view and all of its
6804 * descendants
6805 *
6806 * <p>This is the second phase of the layout mechanism.
6807 * (The first is measuring). In this phase, each parent calls
6808 * layout on all of its children to position them.
6809 * This is typically done using the child measurements
6810 * that were stored in the measure pass().
6811 *
6812 * Derived classes with children should override
6813 * onLayout. In that method, they should
6814 * call layout on each of their their children.
6815 *
6816 * @param l Left position, relative to parent
6817 * @param t Top position, relative to parent
6818 * @param r Right position, relative to parent
6819 * @param b Bottom position, relative to parent
6820 */
6821 public final void layout(int l, int t, int r, int b) {
6822 boolean changed = setFrame(l, t, r, b);
6823 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
6824 if (ViewDebug.TRACE_HIERARCHY) {
6825 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
6826 }
6827
6828 onLayout(changed, l, t, r, b);
6829 mPrivateFlags &= ~LAYOUT_REQUIRED;
6830 }
6831 mPrivateFlags &= ~FORCE_LAYOUT;
6832 }
6833
6834 /**
6835 * Called from layout when this view should
6836 * assign a size and position to each of its children.
6837 *
6838 * Derived classes with children should override
6839 * this method and call layout on each of
6840 * their their children.
6841 * @param changed This is a new size or position for this view
6842 * @param left Left position, relative to parent
6843 * @param top Top position, relative to parent
6844 * @param right Right position, relative to parent
6845 * @param bottom Bottom position, relative to parent
6846 */
6847 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
6848 }
6849
6850 /**
6851 * Assign a size and position to this view.
6852 *
6853 * This is called from layout.
6854 *
6855 * @param left Left position, relative to parent
6856 * @param top Top position, relative to parent
6857 * @param right Right position, relative to parent
6858 * @param bottom Bottom position, relative to parent
6859 * @return true if the new size and position are different than the
6860 * previous ones
6861 * {@hide}
6862 */
6863 protected boolean setFrame(int left, int top, int right, int bottom) {
6864 boolean changed = false;
6865
6866 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07006867 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006868 + right + "," + bottom + ")");
6869 }
6870
6871 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
6872 changed = true;
6873
6874 // Remember our drawn bit
6875 int drawn = mPrivateFlags & DRAWN;
6876
6877 // Invalidate our old position
6878 invalidate();
6879
6880
6881 int oldWidth = mRight - mLeft;
6882 int oldHeight = mBottom - mTop;
6883
6884 mLeft = left;
6885 mTop = top;
6886 mRight = right;
6887 mBottom = bottom;
6888
6889 mPrivateFlags |= HAS_BOUNDS;
6890
6891 int newWidth = right - left;
6892 int newHeight = bottom - top;
6893
6894 if (newWidth != oldWidth || newHeight != oldHeight) {
6895 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
6896 }
6897
6898 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
6899 // If we are visible, force the DRAWN bit to on so that
6900 // this invalidate will go through (at least to our parent).
6901 // This is because someone may have invalidated this view
6902 // before this call to setFrame came in, therby clearing
6903 // the DRAWN bit.
6904 mPrivateFlags |= DRAWN;
6905 invalidate();
6906 }
6907
6908 // Reset drawn bit to original value (invalidate turns it off)
6909 mPrivateFlags |= drawn;
6910
6911 mBackgroundSizeChanged = true;
6912 }
6913 return changed;
6914 }
6915
6916 /**
6917 * Finalize inflating a view from XML. This is called as the last phase
6918 * of inflation, after all child views have been added.
6919 *
6920 * <p>Even if the subclass overrides onFinishInflate, they should always be
6921 * sure to call the super method, so that we get called.
6922 */
6923 protected void onFinishInflate() {
6924 }
6925
6926 /**
6927 * Returns the resources associated with this view.
6928 *
6929 * @return Resources object.
6930 */
6931 public Resources getResources() {
6932 return mResources;
6933 }
6934
6935 /**
6936 * Invalidates the specified Drawable.
6937 *
6938 * @param drawable the drawable to invalidate
6939 */
6940 public void invalidateDrawable(Drawable drawable) {
6941 if (verifyDrawable(drawable)) {
6942 final Rect dirty = drawable.getBounds();
6943 final int scrollX = mScrollX;
6944 final int scrollY = mScrollY;
6945
6946 invalidate(dirty.left + scrollX, dirty.top + scrollY,
6947 dirty.right + scrollX, dirty.bottom + scrollY);
6948 }
6949 }
6950
6951 /**
6952 * Schedules an action on a drawable to occur at a specified time.
6953 *
6954 * @param who the recipient of the action
6955 * @param what the action to run on the drawable
6956 * @param when the time at which the action must occur. Uses the
6957 * {@link SystemClock#uptimeMillis} timebase.
6958 */
6959 public void scheduleDrawable(Drawable who, Runnable what, long when) {
6960 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
6961 mAttachInfo.mHandler.postAtTime(what, who, when);
6962 }
6963 }
6964
6965 /**
6966 * Cancels a scheduled action on a drawable.
6967 *
6968 * @param who the recipient of the action
6969 * @param what the action to cancel
6970 */
6971 public void unscheduleDrawable(Drawable who, Runnable what) {
6972 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
6973 mAttachInfo.mHandler.removeCallbacks(what, who);
6974 }
6975 }
6976
6977 /**
6978 * Unschedule any events associated with the given Drawable. This can be
6979 * used when selecting a new Drawable into a view, so that the previous
6980 * one is completely unscheduled.
6981 *
6982 * @param who The Drawable to unschedule.
6983 *
6984 * @see #drawableStateChanged
6985 */
6986 public void unscheduleDrawable(Drawable who) {
6987 if (mAttachInfo != null) {
6988 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
6989 }
6990 }
6991
6992 /**
6993 * If your view subclass is displaying its own Drawable objects, it should
6994 * override this function and return true for any Drawable it is
6995 * displaying. This allows animations for those drawables to be
6996 * scheduled.
6997 *
6998 * <p>Be sure to call through to the super class when overriding this
6999 * function.
7000 *
7001 * @param who The Drawable to verify. Return true if it is one you are
7002 * displaying, else return the result of calling through to the
7003 * super class.
7004 *
7005 * @return boolean If true than the Drawable is being displayed in the
7006 * view; else false and it is not allowed to animate.
7007 *
7008 * @see #unscheduleDrawable
7009 * @see #drawableStateChanged
7010 */
7011 protected boolean verifyDrawable(Drawable who) {
7012 return who == mBGDrawable;
7013 }
7014
7015 /**
7016 * This function is called whenever the state of the view changes in such
7017 * a way that it impacts the state of drawables being shown.
7018 *
7019 * <p>Be sure to call through to the superclass when overriding this
7020 * function.
7021 *
7022 * @see Drawable#setState
7023 */
7024 protected void drawableStateChanged() {
7025 Drawable d = mBGDrawable;
7026 if (d != null && d.isStateful()) {
7027 d.setState(getDrawableState());
7028 }
7029 }
7030
7031 /**
7032 * Call this to force a view to update its drawable state. This will cause
7033 * drawableStateChanged to be called on this view. Views that are interested
7034 * in the new state should call getDrawableState.
7035 *
7036 * @see #drawableStateChanged
7037 * @see #getDrawableState
7038 */
7039 public void refreshDrawableState() {
7040 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
7041 drawableStateChanged();
7042
7043 ViewParent parent = mParent;
7044 if (parent != null) {
7045 parent.childDrawableStateChanged(this);
7046 }
7047 }
7048
7049 /**
7050 * Return an array of resource IDs of the drawable states representing the
7051 * current state of the view.
7052 *
7053 * @return The current drawable state
7054 *
7055 * @see Drawable#setState
7056 * @see #drawableStateChanged
7057 * @see #onCreateDrawableState
7058 */
7059 public final int[] getDrawableState() {
7060 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
7061 return mDrawableState;
7062 } else {
7063 mDrawableState = onCreateDrawableState(0);
7064 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
7065 return mDrawableState;
7066 }
7067 }
7068
7069 /**
7070 * Generate the new {@link android.graphics.drawable.Drawable} state for
7071 * this view. This is called by the view
7072 * system when the cached Drawable state is determined to be invalid. To
7073 * retrieve the current state, you should use {@link #getDrawableState}.
7074 *
7075 * @param extraSpace if non-zero, this is the number of extra entries you
7076 * would like in the returned array in which you can place your own
7077 * states.
7078 *
7079 * @return Returns an array holding the current {@link Drawable} state of
7080 * the view.
7081 *
7082 * @see #mergeDrawableStates
7083 */
7084 protected int[] onCreateDrawableState(int extraSpace) {
7085 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
7086 mParent instanceof View) {
7087 return ((View) mParent).onCreateDrawableState(extraSpace);
7088 }
7089
7090 int[] drawableState;
7091
7092 int privateFlags = mPrivateFlags;
7093
7094 int viewStateIndex = (((privateFlags & PRESSED) != 0) ? 1 : 0);
7095
7096 viewStateIndex = (viewStateIndex << 1)
7097 + (((mViewFlags & ENABLED_MASK) == ENABLED) ? 1 : 0);
7098
7099 viewStateIndex = (viewStateIndex << 1) + (isFocused() ? 1 : 0);
7100
7101 viewStateIndex = (viewStateIndex << 1)
7102 + (((privateFlags & SELECTED) != 0) ? 1 : 0);
7103
7104 final boolean hasWindowFocus = hasWindowFocus();
7105 viewStateIndex = (viewStateIndex << 1) + (hasWindowFocus ? 1 : 0);
7106
7107 drawableState = VIEW_STATE_SETS[viewStateIndex];
7108
7109 //noinspection ConstantIfStatement
7110 if (false) {
7111 Log.i("View", "drawableStateIndex=" + viewStateIndex);
7112 Log.i("View", toString()
7113 + " pressed=" + ((privateFlags & PRESSED) != 0)
7114 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
7115 + " fo=" + hasFocus()
7116 + " sl=" + ((privateFlags & SELECTED) != 0)
7117 + " wf=" + hasWindowFocus
7118 + ": " + Arrays.toString(drawableState));
7119 }
7120
7121 if (extraSpace == 0) {
7122 return drawableState;
7123 }
7124
7125 final int[] fullState;
7126 if (drawableState != null) {
7127 fullState = new int[drawableState.length + extraSpace];
7128 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
7129 } else {
7130 fullState = new int[extraSpace];
7131 }
7132
7133 return fullState;
7134 }
7135
7136 /**
7137 * Merge your own state values in <var>additionalState</var> into the base
7138 * state values <var>baseState</var> that were returned by
7139 * {@link #onCreateDrawableState}.
7140 *
7141 * @param baseState The base state values returned by
7142 * {@link #onCreateDrawableState}, which will be modified to also hold your
7143 * own additional state values.
7144 *
7145 * @param additionalState The additional state values you would like
7146 * added to <var>baseState</var>; this array is not modified.
7147 *
7148 * @return As a convenience, the <var>baseState</var> array you originally
7149 * passed into the function is returned.
7150 *
7151 * @see #onCreateDrawableState
7152 */
7153 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
7154 final int N = baseState.length;
7155 int i = N - 1;
7156 while (i >= 0 && baseState[i] == 0) {
7157 i--;
7158 }
7159 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
7160 return baseState;
7161 }
7162
7163 /**
7164 * Sets the background color for this view.
7165 * @param color the color of the background
7166 */
7167 public void setBackgroundColor(int color) {
7168 setBackgroundDrawable(new ColorDrawable(color));
7169 }
7170
7171 /**
7172 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07007173 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007174 * @param resid The identifier of the resource.
7175 * @attr ref android.R.styleable#View_background
7176 */
7177 public void setBackgroundResource(int resid) {
7178 if (resid != 0 && resid == mBackgroundResource) {
7179 return;
7180 }
7181
7182 Drawable d= null;
7183 if (resid != 0) {
7184 d = mResources.getDrawable(resid);
7185 }
7186 setBackgroundDrawable(d);
7187
7188 mBackgroundResource = resid;
7189 }
7190
7191 /**
7192 * Set the background to a given Drawable, or remove the background. If the
7193 * background has padding, this View's padding is set to the background's
7194 * padding. However, when a background is removed, this View's padding isn't
7195 * touched. If setting the padding is desired, please use
7196 * {@link #setPadding(int, int, int, int)}.
7197 *
7198 * @param d The Drawable to use as the background, or null to remove the
7199 * background
7200 */
7201 public void setBackgroundDrawable(Drawable d) {
7202 boolean requestLayout = false;
7203
7204 mBackgroundResource = 0;
7205
7206 /*
7207 * Regardless of whether we're setting a new background or not, we want
7208 * to clear the previous drawable.
7209 */
7210 if (mBGDrawable != null) {
7211 mBGDrawable.setCallback(null);
7212 unscheduleDrawable(mBGDrawable);
7213 }
7214
7215 if (d != null) {
7216 Rect padding = sThreadLocal.get();
7217 if (padding == null) {
7218 padding = new Rect();
7219 sThreadLocal.set(padding);
7220 }
7221 if (d.getPadding(padding)) {
7222 setPadding(padding.left, padding.top, padding.right, padding.bottom);
7223 }
7224
7225 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
7226 // if it has a different minimum size, we should layout again
7227 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
7228 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
7229 requestLayout = true;
7230 }
7231
7232 d.setCallback(this);
7233 if (d.isStateful()) {
7234 d.setState(getDrawableState());
7235 }
7236 d.setVisible(getVisibility() == VISIBLE, false);
7237 mBGDrawable = d;
7238
7239 if ((mPrivateFlags & SKIP_DRAW) != 0) {
7240 mPrivateFlags &= ~SKIP_DRAW;
7241 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
7242 requestLayout = true;
7243 }
7244 } else {
7245 /* Remove the background */
7246 mBGDrawable = null;
7247
7248 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
7249 /*
7250 * This view ONLY drew the background before and we're removing
7251 * the background, so now it won't draw anything
7252 * (hence we SKIP_DRAW)
7253 */
7254 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
7255 mPrivateFlags |= SKIP_DRAW;
7256 }
7257
7258 /*
7259 * When the background is set, we try to apply its padding to this
7260 * View. When the background is removed, we don't touch this View's
7261 * padding. This is noted in the Javadocs. Hence, we don't need to
7262 * requestLayout(), the invalidate() below is sufficient.
7263 */
7264
7265 // The old background's minimum size could have affected this
7266 // View's layout, so let's requestLayout
7267 requestLayout = true;
7268 }
7269
Romain Guy8f1344f52009-05-15 16:03:59 -07007270 computeOpaqueFlags();
7271
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 if (requestLayout) {
7273 requestLayout();
7274 }
7275
7276 mBackgroundSizeChanged = true;
7277 invalidate();
7278 }
7279
7280 /**
7281 * Gets the background drawable
7282 * @return The drawable used as the background for this view, if any.
7283 */
7284 public Drawable getBackground() {
7285 return mBGDrawable;
7286 }
7287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007288 /**
7289 * Sets the padding. The view may add on the space required to display
7290 * the scrollbars, depending on the style and visibility of the scrollbars.
7291 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
7292 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
7293 * from the values set in this call.
7294 *
7295 * @attr ref android.R.styleable#View_padding
7296 * @attr ref android.R.styleable#View_paddingBottom
7297 * @attr ref android.R.styleable#View_paddingLeft
7298 * @attr ref android.R.styleable#View_paddingRight
7299 * @attr ref android.R.styleable#View_paddingTop
7300 * @param left the left padding in pixels
7301 * @param top the top padding in pixels
7302 * @param right the right padding in pixels
7303 * @param bottom the bottom padding in pixels
7304 */
7305 public void setPadding(int left, int top, int right, int bottom) {
7306 boolean changed = false;
7307
7308 mUserPaddingRight = right;
7309 mUserPaddingBottom = bottom;
7310
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007311 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07007312
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007313 // Common case is there are no scroll bars.
7314 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
7315 // TODO: Deal with RTL languages to adjust left padding instead of right.
7316 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
7317 right += (viewFlags & SCROLLBARS_INSET_MASK) == 0
7318 ? 0 : getVerticalScrollbarWidth();
7319 }
7320 if ((viewFlags & SCROLLBARS_HORIZONTAL) == 0) {
7321 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
7322 ? 0 : getHorizontalScrollbarHeight();
7323 }
7324 }
Romain Guy8506ab42009-06-11 17:35:47 -07007325
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007326 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007327 changed = true;
7328 mPaddingLeft = left;
7329 }
7330 if (mPaddingTop != top) {
7331 changed = true;
7332 mPaddingTop = top;
7333 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007334 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007335 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007336 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007337 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007338 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007339 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07007340 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007341 }
7342
7343 if (changed) {
7344 requestLayout();
7345 }
7346 }
7347
7348 /**
7349 * Returns the top padding of this view.
7350 *
7351 * @return the top padding in pixels
7352 */
7353 public int getPaddingTop() {
7354 return mPaddingTop;
7355 }
7356
7357 /**
7358 * Returns the bottom padding of this view. If there are inset and enabled
7359 * scrollbars, this value may include the space required to display the
7360 * scrollbars as well.
7361 *
7362 * @return the bottom padding in pixels
7363 */
7364 public int getPaddingBottom() {
7365 return mPaddingBottom;
7366 }
7367
7368 /**
7369 * Returns the left padding of this view. If there are inset and enabled
7370 * scrollbars, this value may include the space required to display the
7371 * scrollbars as well.
7372 *
7373 * @return the left padding in pixels
7374 */
7375 public int getPaddingLeft() {
7376 return mPaddingLeft;
7377 }
7378
7379 /**
7380 * Returns the right padding of this view. If there are inset and enabled
7381 * scrollbars, this value may include the space required to display the
7382 * scrollbars as well.
7383 *
7384 * @return the right padding in pixels
7385 */
7386 public int getPaddingRight() {
7387 return mPaddingRight;
7388 }
7389
7390 /**
7391 * Changes the selection state of this view. A view can be selected or not.
7392 * Note that selection is not the same as focus. Views are typically
7393 * selected in the context of an AdapterView like ListView or GridView;
7394 * the selected view is the view that is highlighted.
7395 *
7396 * @param selected true if the view must be selected, false otherwise
7397 */
7398 public void setSelected(boolean selected) {
7399 if (((mPrivateFlags & SELECTED) != 0) != selected) {
7400 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -07007401 if (!selected) resetPressedState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007402 invalidate();
7403 refreshDrawableState();
7404 dispatchSetSelected(selected);
7405 }
7406 }
7407
7408 /**
7409 * Dispatch setSelected to all of this View's children.
7410 *
7411 * @see #setSelected(boolean)
7412 *
7413 * @param selected The new selected state
7414 */
7415 protected void dispatchSetSelected(boolean selected) {
7416 }
7417
7418 /**
7419 * Indicates the selection state of this view.
7420 *
7421 * @return true if the view is selected, false otherwise
7422 */
7423 @ViewDebug.ExportedProperty
7424 public boolean isSelected() {
7425 return (mPrivateFlags & SELECTED) != 0;
7426 }
7427
7428 /**
7429 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
7430 * observer can be used to get notifications when global events, like
7431 * layout, happen.
7432 *
7433 * The returned ViewTreeObserver observer is not guaranteed to remain
7434 * valid for the lifetime of this View. If the caller of this method keeps
7435 * a long-lived reference to ViewTreeObserver, it should always check for
7436 * the return value of {@link ViewTreeObserver#isAlive()}.
7437 *
7438 * @return The ViewTreeObserver for this view's hierarchy.
7439 */
7440 public ViewTreeObserver getViewTreeObserver() {
7441 if (mAttachInfo != null) {
7442 return mAttachInfo.mTreeObserver;
7443 }
7444 if (mFloatingTreeObserver == null) {
7445 mFloatingTreeObserver = new ViewTreeObserver();
7446 }
7447 return mFloatingTreeObserver;
7448 }
7449
7450 /**
7451 * <p>Finds the topmost view in the current view hierarchy.</p>
7452 *
7453 * @return the topmost view containing this view
7454 */
7455 public View getRootView() {
7456 if (mAttachInfo != null) {
7457 final View v = mAttachInfo.mRootView;
7458 if (v != null) {
7459 return v;
7460 }
7461 }
Romain Guy8506ab42009-06-11 17:35:47 -07007462
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007463 View parent = this;
7464
7465 while (parent.mParent != null && parent.mParent instanceof View) {
7466 parent = (View) parent.mParent;
7467 }
7468
7469 return parent;
7470 }
7471
7472 /**
7473 * <p>Computes the coordinates of this view on the screen. The argument
7474 * must be an array of two integers. After the method returns, the array
7475 * contains the x and y location in that order.</p>
7476 *
7477 * @param location an array of two integers in which to hold the coordinates
7478 */
7479 public void getLocationOnScreen(int[] location) {
7480 getLocationInWindow(location);
7481
7482 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -07007483 if (info != null) {
7484 location[0] += info.mWindowLeft;
7485 location[1] += info.mWindowTop;
7486 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007487 }
7488
7489 /**
7490 * <p>Computes the coordinates of this view in its window. The argument
7491 * must be an array of two integers. After the method returns, the array
7492 * contains the x and y location in that order.</p>
7493 *
7494 * @param location an array of two integers in which to hold the coordinates
7495 */
7496 public void getLocationInWindow(int[] location) {
7497 if (location == null || location.length < 2) {
7498 throw new IllegalArgumentException("location must be an array of "
7499 + "two integers");
7500 }
7501
7502 location[0] = mLeft;
7503 location[1] = mTop;
7504
7505 ViewParent viewParent = mParent;
7506 while (viewParent instanceof View) {
7507 final View view = (View)viewParent;
7508 location[0] += view.mLeft - view.mScrollX;
7509 location[1] += view.mTop - view.mScrollY;
7510 viewParent = view.mParent;
7511 }
Romain Guy8506ab42009-06-11 17:35:47 -07007512
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007513 if (viewParent instanceof ViewRoot) {
7514 // *cough*
7515 final ViewRoot vr = (ViewRoot)viewParent;
7516 location[1] -= vr.mCurScrollY;
7517 }
7518 }
7519
7520 /**
7521 * {@hide}
7522 * @param id the id of the view to be found
7523 * @return the view of the specified id, null if cannot be found
7524 */
7525 protected View findViewTraversal(int id) {
7526 if (id == mID) {
7527 return this;
7528 }
7529 return null;
7530 }
7531
7532 /**
7533 * {@hide}
7534 * @param tag the tag of the view to be found
7535 * @return the view of specified tag, null if cannot be found
7536 */
7537 protected View findViewWithTagTraversal(Object tag) {
7538 if (tag != null && tag.equals(mTag)) {
7539 return this;
7540 }
7541 return null;
7542 }
7543
7544 /**
7545 * Look for a child view with the given id. If this view has the given
7546 * id, return this view.
7547 *
7548 * @param id The id to search for.
7549 * @return The view that has the given id in the hierarchy or null
7550 */
7551 public final View findViewById(int id) {
7552 if (id < 0) {
7553 return null;
7554 }
7555 return findViewTraversal(id);
7556 }
7557
7558 /**
7559 * Look for a child view with the given tag. If this view has the given
7560 * tag, return this view.
7561 *
7562 * @param tag The tag to search for, using "tag.equals(getTag())".
7563 * @return The View that has the given tag in the hierarchy or null
7564 */
7565 public final View findViewWithTag(Object tag) {
7566 if (tag == null) {
7567 return null;
7568 }
7569 return findViewWithTagTraversal(tag);
7570 }
7571
7572 /**
7573 * Sets the identifier for this view. The identifier does not have to be
7574 * unique in this view's hierarchy. The identifier should be a positive
7575 * number.
7576 *
7577 * @see #NO_ID
7578 * @see #getId
7579 * @see #findViewById
7580 *
7581 * @param id a number used to identify the view
7582 *
7583 * @attr ref android.R.styleable#View_id
7584 */
7585 public void setId(int id) {
7586 mID = id;
7587 }
7588
7589 /**
7590 * {@hide}
7591 *
7592 * @param isRoot true if the view belongs to the root namespace, false
7593 * otherwise
7594 */
7595 public void setIsRootNamespace(boolean isRoot) {
7596 if (isRoot) {
7597 mPrivateFlags |= IS_ROOT_NAMESPACE;
7598 } else {
7599 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
7600 }
7601 }
7602
7603 /**
7604 * {@hide}
7605 *
7606 * @return true if the view belongs to the root namespace, false otherwise
7607 */
7608 public boolean isRootNamespace() {
7609 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
7610 }
7611
7612 /**
7613 * Returns this view's identifier.
7614 *
7615 * @return a positive integer used to identify the view or {@link #NO_ID}
7616 * if the view has no ID
7617 *
7618 * @see #setId
7619 * @see #findViewById
7620 * @attr ref android.R.styleable#View_id
7621 */
7622 @ViewDebug.CapturedViewProperty
7623 public int getId() {
7624 return mID;
7625 }
7626
7627 /**
7628 * Returns this view's tag.
7629 *
7630 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -07007631 *
7632 * @see #setTag(Object)
7633 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007634 */
7635 @ViewDebug.ExportedProperty
7636 public Object getTag() {
7637 return mTag;
7638 }
7639
7640 /**
7641 * Sets the tag associated with this view. A tag can be used to mark
7642 * a view in its hierarchy and does not have to be unique within the
7643 * hierarchy. Tags can also be used to store data within a view without
7644 * resorting to another data structure.
7645 *
7646 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -07007647 *
7648 * @see #getTag()
7649 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 */
7651 public void setTag(final Object tag) {
7652 mTag = tag;
7653 }
7654
7655 /**
Romain Guyd90a3312009-05-06 14:54:28 -07007656 * Returns the tag associated with this view and the specified key.
7657 *
7658 * @param key The key identifying the tag
7659 *
7660 * @return the Object stored in this view as a tag
7661 *
7662 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -07007663 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -07007664 */
7665 public Object getTag(int key) {
7666 SparseArray<Object> tags = null;
7667 synchronized (sTagsLock) {
7668 if (sTags != null) {
7669 tags = sTags.get(this);
7670 }
7671 }
7672
7673 if (tags != null) return tags.get(key);
7674 return null;
7675 }
7676
7677 /**
7678 * Sets a tag associated with this view and a key. A tag can be used
7679 * to mark a view in its hierarchy and does not have to be unique within
7680 * the hierarchy. Tags can also be used to store data within a view
7681 * without resorting to another data structure.
7682 *
7683 * The specified key should be an id declared in the resources of the
7684 * application to ensure it is unique. Keys identified as belonging to
7685 * the Android framework or not associated with any package will cause
7686 * an {@link IllegalArgumentException} to be thrown.
7687 *
7688 * @param key The key identifying the tag
7689 * @param tag An Object to tag the view with
7690 *
7691 * @throws IllegalArgumentException If they specified key is not valid
7692 *
7693 * @see #setTag(Object)
7694 * @see #getTag(int)
7695 */
7696 public void setTag(int key, final Object tag) {
7697 // If the package id is 0x00 or 0x01, it's either an undefined package
7698 // or a framework id
7699 if ((key >>> 24) < 2) {
7700 throw new IllegalArgumentException("The key must be an application-specific "
7701 + "resource id.");
7702 }
7703
7704 setTagInternal(this, key, tag);
7705 }
7706
7707 /**
7708 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
7709 * framework id.
7710 *
7711 * @hide
7712 */
7713 public void setTagInternal(int key, Object tag) {
7714 if ((key >>> 24) != 0x1) {
7715 throw new IllegalArgumentException("The key must be a framework-specific "
7716 + "resource id.");
7717 }
7718
Romain Guy8506ab42009-06-11 17:35:47 -07007719 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -07007720 }
7721
7722 private static void setTagInternal(View view, int key, Object tag) {
7723 SparseArray<Object> tags = null;
7724 synchronized (sTagsLock) {
7725 if (sTags == null) {
7726 sTags = new WeakHashMap<View, SparseArray<Object>>();
7727 } else {
7728 tags = sTags.get(view);
7729 }
7730 }
7731
7732 if (tags == null) {
7733 tags = new SparseArray<Object>(2);
7734 synchronized (sTagsLock) {
7735 sTags.put(view, tags);
7736 }
7737 }
7738
7739 tags.put(key, tag);
7740 }
7741
7742 /**
Romain Guy13922e02009-05-12 17:56:14 -07007743 * @param consistency The type of consistency. See ViewDebug for more information.
7744 *
7745 * @hide
7746 */
7747 protected boolean dispatchConsistencyCheck(int consistency) {
7748 return onConsistencyCheck(consistency);
7749 }
7750
7751 /**
7752 * Method that subclasses should implement to check their consistency. The type of
7753 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -07007754 *
Romain Guy13922e02009-05-12 17:56:14 -07007755 * @param consistency The type of consistency. See ViewDebug for more information.
7756 *
7757 * @throws IllegalStateException if the view is in an inconsistent state.
7758 *
7759 * @hide
7760 */
7761 protected boolean onConsistencyCheck(int consistency) {
7762 boolean result = true;
7763
7764 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
7765 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
7766
7767 if (checkLayout) {
7768 if (getParent() == null) {
7769 result = false;
7770 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
7771 "View " + this + " does not have a parent.");
7772 }
7773
7774 if (mAttachInfo == null) {
7775 result = false;
7776 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
7777 "View " + this + " is not attached to a window.");
7778 }
7779 }
7780
7781 if (checkDrawing) {
7782 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
7783 // from their draw() method
7784
7785 if ((mPrivateFlags & DRAWN) != DRAWN &&
7786 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
7787 result = false;
7788 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
7789 "View " + this + " was invalidated but its drawing cache is valid.");
7790 }
7791 }
7792
7793 return result;
7794 }
7795
7796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007797 * Prints information about this view in the log output, with the tag
7798 * {@link #VIEW_LOG_TAG}.
7799 *
7800 * @hide
7801 */
7802 public void debug() {
7803 debug(0);
7804 }
7805
7806 /**
7807 * Prints information about this view in the log output, with the tag
7808 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
7809 * indentation defined by the <code>depth</code>.
7810 *
7811 * @param depth the indentation level
7812 *
7813 * @hide
7814 */
7815 protected void debug(int depth) {
7816 String output = debugIndent(depth - 1);
7817
7818 output += "+ " + this;
7819 int id = getId();
7820 if (id != -1) {
7821 output += " (id=" + id + ")";
7822 }
7823 Object tag = getTag();
7824 if (tag != null) {
7825 output += " (tag=" + tag + ")";
7826 }
7827 Log.d(VIEW_LOG_TAG, output);
7828
7829 if ((mPrivateFlags & FOCUSED) != 0) {
7830 output = debugIndent(depth) + " FOCUSED";
7831 Log.d(VIEW_LOG_TAG, output);
7832 }
7833
7834 output = debugIndent(depth);
7835 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
7836 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
7837 + "} ";
7838 Log.d(VIEW_LOG_TAG, output);
7839
7840 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
7841 || mPaddingBottom != 0) {
7842 output = debugIndent(depth);
7843 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
7844 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
7845 Log.d(VIEW_LOG_TAG, output);
7846 }
7847
7848 output = debugIndent(depth);
7849 output += "mMeasureWidth=" + mMeasuredWidth +
7850 " mMeasureHeight=" + mMeasuredHeight;
7851 Log.d(VIEW_LOG_TAG, output);
7852
7853 output = debugIndent(depth);
7854 if (mLayoutParams == null) {
7855 output += "BAD! no layout params";
7856 } else {
7857 output = mLayoutParams.debug(output);
7858 }
7859 Log.d(VIEW_LOG_TAG, output);
7860
7861 output = debugIndent(depth);
7862 output += "flags={";
7863 output += View.printFlags(mViewFlags);
7864 output += "}";
7865 Log.d(VIEW_LOG_TAG, output);
7866
7867 output = debugIndent(depth);
7868 output += "privateFlags={";
7869 output += View.printPrivateFlags(mPrivateFlags);
7870 output += "}";
7871 Log.d(VIEW_LOG_TAG, output);
7872 }
7873
7874 /**
7875 * Creates an string of whitespaces used for indentation.
7876 *
7877 * @param depth the indentation level
7878 * @return a String containing (depth * 2 + 3) * 2 white spaces
7879 *
7880 * @hide
7881 */
7882 protected static String debugIndent(int depth) {
7883 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
7884 for (int i = 0; i < (depth * 2) + 3; i++) {
7885 spaces.append(' ').append(' ');
7886 }
7887 return spaces.toString();
7888 }
7889
7890 /**
7891 * <p>Return the offset of the widget's text baseline from the widget's top
7892 * boundary. If this widget does not support baseline alignment, this
7893 * method returns -1. </p>
7894 *
7895 * @return the offset of the baseline within the widget's bounds or -1
7896 * if baseline alignment is not supported
7897 */
7898 @ViewDebug.ExportedProperty
7899 public int getBaseline() {
7900 return -1;
7901 }
7902
7903 /**
7904 * Call this when something has changed which has invalidated the
7905 * layout of this view. This will schedule a layout pass of the view
7906 * tree.
7907 */
7908 public void requestLayout() {
7909 if (ViewDebug.TRACE_HIERARCHY) {
7910 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
7911 }
7912
7913 mPrivateFlags |= FORCE_LAYOUT;
7914
7915 if (mParent != null && !mParent.isLayoutRequested()) {
7916 mParent.requestLayout();
7917 }
7918 }
7919
7920 /**
7921 * Forces this view to be laid out during the next layout pass.
7922 * This method does not call requestLayout() or forceLayout()
7923 * on the parent.
7924 */
7925 public void forceLayout() {
7926 mPrivateFlags |= FORCE_LAYOUT;
7927 }
7928
7929 /**
7930 * <p>
7931 * This is called to find out how big a view should be. The parent
7932 * supplies constraint information in the width and height parameters.
7933 * </p>
7934 *
7935 * <p>
7936 * The actual mesurement work of a view is performed in
7937 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
7938 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
7939 * </p>
7940 *
7941 *
7942 * @param widthMeasureSpec Horizontal space requirements as imposed by the
7943 * parent
7944 * @param heightMeasureSpec Vertical space requirements as imposed by the
7945 * parent
7946 *
7947 * @see #onMeasure(int, int)
7948 */
7949 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
7950 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
7951 widthMeasureSpec != mOldWidthMeasureSpec ||
7952 heightMeasureSpec != mOldHeightMeasureSpec) {
7953
7954 // first clears the measured dimension flag
7955 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
7956
7957 if (ViewDebug.TRACE_HIERARCHY) {
7958 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
7959 }
7960
7961 // measure ourselves, this should set the measured dimension flag back
7962 onMeasure(widthMeasureSpec, heightMeasureSpec);
7963
7964 // flag not set, setMeasuredDimension() was not invoked, we raise
7965 // an exception to warn the developer
7966 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
7967 throw new IllegalStateException("onMeasure() did not set the"
7968 + " measured dimension by calling"
7969 + " setMeasuredDimension()");
7970 }
7971
7972 mPrivateFlags |= LAYOUT_REQUIRED;
7973 }
7974
7975 mOldWidthMeasureSpec = widthMeasureSpec;
7976 mOldHeightMeasureSpec = heightMeasureSpec;
7977 }
7978
7979 /**
7980 * <p>
7981 * Measure the view and its content to determine the measured width and the
7982 * measured height. This method is invoked by {@link #measure(int, int)} and
7983 * should be overriden by subclasses to provide accurate and efficient
7984 * measurement of their contents.
7985 * </p>
7986 *
7987 * <p>
7988 * <strong>CONTRACT:</strong> When overriding this method, you
7989 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
7990 * measured width and height of this view. Failure to do so will trigger an
7991 * <code>IllegalStateException</code>, thrown by
7992 * {@link #measure(int, int)}. Calling the superclass'
7993 * {@link #onMeasure(int, int)} is a valid use.
7994 * </p>
7995 *
7996 * <p>
7997 * The base class implementation of measure defaults to the background size,
7998 * unless a larger size is allowed by the MeasureSpec. Subclasses should
7999 * override {@link #onMeasure(int, int)} to provide better measurements of
8000 * their content.
8001 * </p>
8002 *
8003 * <p>
8004 * If this method is overridden, it is the subclass's responsibility to make
8005 * sure the measured height and width are at least the view's minimum height
8006 * and width ({@link #getSuggestedMinimumHeight()} and
8007 * {@link #getSuggestedMinimumWidth()}).
8008 * </p>
8009 *
8010 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
8011 * The requirements are encoded with
8012 * {@link android.view.View.MeasureSpec}.
8013 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
8014 * The requirements are encoded with
8015 * {@link android.view.View.MeasureSpec}.
8016 *
8017 * @see #getMeasuredWidth()
8018 * @see #getMeasuredHeight()
8019 * @see #setMeasuredDimension(int, int)
8020 * @see #getSuggestedMinimumHeight()
8021 * @see #getSuggestedMinimumWidth()
8022 * @see android.view.View.MeasureSpec#getMode(int)
8023 * @see android.view.View.MeasureSpec#getSize(int)
8024 */
8025 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
8026 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
8027 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
8028 }
8029
8030 /**
8031 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
8032 * measured width and measured height. Failing to do so will trigger an
8033 * exception at measurement time.</p>
8034 *
8035 * @param measuredWidth the measured width of this view
8036 * @param measuredHeight the measured height of this view
8037 */
8038 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
8039 mMeasuredWidth = measuredWidth;
8040 mMeasuredHeight = measuredHeight;
8041
8042 mPrivateFlags |= MEASURED_DIMENSION_SET;
8043 }
8044
8045 /**
8046 * Utility to reconcile a desired size with constraints imposed by a MeasureSpec.
8047 * Will take the desired size, unless a different size is imposed by the constraints.
8048 *
8049 * @param size How big the view wants to be
8050 * @param measureSpec Constraints imposed by the parent
8051 * @return The size this view should be.
8052 */
8053 public static int resolveSize(int size, int measureSpec) {
8054 int result = size;
8055 int specMode = MeasureSpec.getMode(measureSpec);
8056 int specSize = MeasureSpec.getSize(measureSpec);
8057 switch (specMode) {
8058 case MeasureSpec.UNSPECIFIED:
8059 result = size;
8060 break;
8061 case MeasureSpec.AT_MOST:
8062 result = Math.min(size, specSize);
8063 break;
8064 case MeasureSpec.EXACTLY:
8065 result = specSize;
8066 break;
8067 }
8068 return result;
8069 }
8070
8071 /**
8072 * Utility to return a default size. Uses the supplied size if the
8073 * MeasureSpec imposed no contraints. Will get larger if allowed
8074 * by the MeasureSpec.
8075 *
8076 * @param size Default size for this view
8077 * @param measureSpec Constraints imposed by the parent
8078 * @return The size this view should be.
8079 */
8080 public static int getDefaultSize(int size, int measureSpec) {
8081 int result = size;
8082 int specMode = MeasureSpec.getMode(measureSpec);
8083 int specSize = MeasureSpec.getSize(measureSpec);
8084
8085 switch (specMode) {
8086 case MeasureSpec.UNSPECIFIED:
8087 result = size;
8088 break;
8089 case MeasureSpec.AT_MOST:
8090 case MeasureSpec.EXACTLY:
8091 result = specSize;
8092 break;
8093 }
8094 return result;
8095 }
8096
8097 /**
8098 * Returns the suggested minimum height that the view should use. This
8099 * returns the maximum of the view's minimum height
8100 * and the background's minimum height
8101 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
8102 * <p>
8103 * When being used in {@link #onMeasure(int, int)}, the caller should still
8104 * ensure the returned height is within the requirements of the parent.
8105 *
8106 * @return The suggested minimum height of the view.
8107 */
8108 protected int getSuggestedMinimumHeight() {
8109 int suggestedMinHeight = mMinHeight;
8110
8111 if (mBGDrawable != null) {
8112 final int bgMinHeight = mBGDrawable.getMinimumHeight();
8113 if (suggestedMinHeight < bgMinHeight) {
8114 suggestedMinHeight = bgMinHeight;
8115 }
8116 }
8117
8118 return suggestedMinHeight;
8119 }
8120
8121 /**
8122 * Returns the suggested minimum width that the view should use. This
8123 * returns the maximum of the view's minimum width)
8124 * and the background's minimum width
8125 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
8126 * <p>
8127 * When being used in {@link #onMeasure(int, int)}, the caller should still
8128 * ensure the returned width is within the requirements of the parent.
8129 *
8130 * @return The suggested minimum width of the view.
8131 */
8132 protected int getSuggestedMinimumWidth() {
8133 int suggestedMinWidth = mMinWidth;
8134
8135 if (mBGDrawable != null) {
8136 final int bgMinWidth = mBGDrawable.getMinimumWidth();
8137 if (suggestedMinWidth < bgMinWidth) {
8138 suggestedMinWidth = bgMinWidth;
8139 }
8140 }
8141
8142 return suggestedMinWidth;
8143 }
8144
8145 /**
8146 * Sets the minimum height of the view. It is not guaranteed the view will
8147 * be able to achieve this minimum height (for example, if its parent layout
8148 * constrains it with less available height).
8149 *
8150 * @param minHeight The minimum height the view will try to be.
8151 */
8152 public void setMinimumHeight(int minHeight) {
8153 mMinHeight = minHeight;
8154 }
8155
8156 /**
8157 * Sets the minimum width of the view. It is not guaranteed the view will
8158 * be able to achieve this minimum width (for example, if its parent layout
8159 * constrains it with less available width).
8160 *
8161 * @param minWidth The minimum width the view will try to be.
8162 */
8163 public void setMinimumWidth(int minWidth) {
8164 mMinWidth = minWidth;
8165 }
8166
8167 /**
8168 * Get the animation currently associated with this view.
8169 *
8170 * @return The animation that is currently playing or
8171 * scheduled to play for this view.
8172 */
8173 public Animation getAnimation() {
8174 return mCurrentAnimation;
8175 }
8176
8177 /**
8178 * Start the specified animation now.
8179 *
8180 * @param animation the animation to start now
8181 */
8182 public void startAnimation(Animation animation) {
8183 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
8184 setAnimation(animation);
8185 invalidate();
8186 }
8187
8188 /**
8189 * Cancels any animations for this view.
8190 */
8191 public void clearAnimation() {
8192 mCurrentAnimation = null;
8193 }
8194
8195 /**
8196 * Sets the next animation to play for this view.
8197 * If you want the animation to play immediately, use
8198 * startAnimation. This method provides allows fine-grained
8199 * control over the start time and invalidation, but you
8200 * must make sure that 1) the animation has a start time set, and
8201 * 2) the view will be invalidated when the animation is supposed to
8202 * start.
8203 *
8204 * @param animation The next animation, or null.
8205 */
8206 public void setAnimation(Animation animation) {
8207 mCurrentAnimation = animation;
8208 if (animation != null) {
8209 animation.reset();
8210 }
8211 }
8212
8213 /**
8214 * Invoked by a parent ViewGroup to notify the start of the animation
8215 * currently associated with this view. If you override this method,
8216 * always call super.onAnimationStart();
8217 *
8218 * @see #setAnimation(android.view.animation.Animation)
8219 * @see #getAnimation()
8220 */
8221 protected void onAnimationStart() {
8222 mPrivateFlags |= ANIMATION_STARTED;
8223 }
8224
8225 /**
8226 * Invoked by a parent ViewGroup to notify the end of the animation
8227 * currently associated with this view. If you override this method,
8228 * always call super.onAnimationEnd();
8229 *
8230 * @see #setAnimation(android.view.animation.Animation)
8231 * @see #getAnimation()
8232 */
8233 protected void onAnimationEnd() {
8234 mPrivateFlags &= ~ANIMATION_STARTED;
8235 }
8236
8237 /**
8238 * Invoked if there is a Transform that involves alpha. Subclass that can
8239 * draw themselves with the specified alpha should return true, and then
8240 * respect that alpha when their onDraw() is called. If this returns false
8241 * then the view may be redirected to draw into an offscreen buffer to
8242 * fulfill the request, which will look fine, but may be slower than if the
8243 * subclass handles it internally. The default implementation returns false.
8244 *
8245 * @param alpha The alpha (0..255) to apply to the view's drawing
8246 * @return true if the view can draw with the specified alpha.
8247 */
8248 protected boolean onSetAlpha(int alpha) {
8249 return false;
8250 }
8251
8252 /**
8253 * This is used by the RootView to perform an optimization when
8254 * the view hierarchy contains one or several SurfaceView.
8255 * SurfaceView is always considered transparent, but its children are not,
8256 * therefore all View objects remove themselves from the global transparent
8257 * region (passed as a parameter to this function).
8258 *
8259 * @param region The transparent region for this ViewRoot (window).
8260 *
8261 * @return Returns true if the effective visibility of the view at this
8262 * point is opaque, regardless of the transparent region; returns false
8263 * if it is possible for underlying windows to be seen behind the view.
8264 *
8265 * {@hide}
8266 */
8267 public boolean gatherTransparentRegion(Region region) {
8268 final AttachInfo attachInfo = mAttachInfo;
8269 if (region != null && attachInfo != null) {
8270 final int pflags = mPrivateFlags;
8271 if ((pflags & SKIP_DRAW) == 0) {
8272 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
8273 // remove it from the transparent region.
8274 final int[] location = attachInfo.mTransparentLocation;
8275 getLocationInWindow(location);
8276 region.op(location[0], location[1], location[0] + mRight - mLeft,
8277 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
8278 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
8279 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
8280 // exists, so we remove the background drawable's non-transparent
8281 // parts from this transparent region.
8282 applyDrawableToTransparentRegion(mBGDrawable, region);
8283 }
8284 }
8285 return true;
8286 }
8287
8288 /**
8289 * Play a sound effect for this view.
8290 *
8291 * <p>The framework will play sound effects for some built in actions, such as
8292 * clicking, but you may wish to play these effects in your widget,
8293 * for instance, for internal navigation.
8294 *
8295 * <p>The sound effect will only be played if sound effects are enabled by the user, and
8296 * {@link #isSoundEffectsEnabled()} is true.
8297 *
8298 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
8299 */
8300 public void playSoundEffect(int soundConstant) {
8301 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
8302 return;
8303 }
8304 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
8305 }
8306
8307 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07008308 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -07008309 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07008310 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008311 *
8312 * <p>The framework will provide haptic feedback for some built in actions,
8313 * such as long presses, but you may wish to provide feedback for your
8314 * own widget.
8315 *
8316 * <p>The feedback will only be performed if
8317 * {@link #isHapticFeedbackEnabled()} is true.
8318 *
8319 * @param feedbackConstant One of the constants defined in
8320 * {@link HapticFeedbackConstants}
8321 */
8322 public boolean performHapticFeedback(int feedbackConstant) {
8323 return performHapticFeedback(feedbackConstant, 0);
8324 }
8325
8326 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07008327 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -07008328 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07008329 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330 *
8331 * @param feedbackConstant One of the constants defined in
8332 * {@link HapticFeedbackConstants}
8333 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
8334 */
8335 public boolean performHapticFeedback(int feedbackConstant, int flags) {
8336 if (mAttachInfo == null) {
8337 return false;
8338 }
8339 if ((flags&HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
8340 && !isHapticFeedbackEnabled()) {
8341 return false;
8342 }
8343 return mAttachInfo.mRootCallbacks.performHapticFeedback(
8344 feedbackConstant,
8345 (flags&HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
8346 }
8347
8348 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -07008349 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
8350 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -07008351 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -07008352 */
8353 public void onCloseSystemDialogs(String reason) {
8354 }
8355
8356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 * Given a Drawable whose bounds have been set to draw into this view,
8358 * update a Region being computed for {@link #gatherTransparentRegion} so
8359 * that any non-transparent parts of the Drawable are removed from the
8360 * given transparent region.
8361 *
8362 * @param dr The Drawable whose transparency is to be applied to the region.
8363 * @param region A Region holding the current transparency information,
8364 * where any parts of the region that are set are considered to be
8365 * transparent. On return, this region will be modified to have the
8366 * transparency information reduced by the corresponding parts of the
8367 * Drawable that are not transparent.
8368 * {@hide}
8369 */
8370 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
8371 if (DBG) {
8372 Log.i("View", "Getting transparent region for: " + this);
8373 }
8374 final Region r = dr.getTransparentRegion();
8375 final Rect db = dr.getBounds();
8376 final AttachInfo attachInfo = mAttachInfo;
8377 if (r != null && attachInfo != null) {
8378 final int w = getRight()-getLeft();
8379 final int h = getBottom()-getTop();
8380 if (db.left > 0) {
8381 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
8382 r.op(0, 0, db.left, h, Region.Op.UNION);
8383 }
8384 if (db.right < w) {
8385 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
8386 r.op(db.right, 0, w, h, Region.Op.UNION);
8387 }
8388 if (db.top > 0) {
8389 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
8390 r.op(0, 0, w, db.top, Region.Op.UNION);
8391 }
8392 if (db.bottom < h) {
8393 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
8394 r.op(0, db.bottom, w, h, Region.Op.UNION);
8395 }
8396 final int[] location = attachInfo.mTransparentLocation;
8397 getLocationInWindow(location);
8398 r.translate(location[0], location[1]);
8399 region.op(r, Region.Op.INTERSECT);
8400 } else {
8401 region.op(db, Region.Op.DIFFERENCE);
8402 }
8403 }
8404
8405 private void postCheckForLongClick() {
8406 mHasPerformedLongPress = false;
8407
8408 if (mPendingCheckForLongPress == null) {
8409 mPendingCheckForLongPress = new CheckForLongPress();
8410 }
8411 mPendingCheckForLongPress.rememberWindowAttachCount();
8412 postDelayed(mPendingCheckForLongPress, ViewConfiguration.getLongPressTimeout());
8413 }
8414
8415 private static int[] stateSetUnion(final int[] stateSet1,
8416 final int[] stateSet2) {
8417 final int stateSet1Length = stateSet1.length;
8418 final int stateSet2Length = stateSet2.length;
8419 final int[] newSet = new int[stateSet1Length + stateSet2Length];
8420 int k = 0;
8421 int i = 0;
8422 int j = 0;
8423 // This is a merge of the two input state sets and assumes that the
8424 // input sets are sorted by the order imposed by ViewDrawableStates.
8425 for (int viewState : R.styleable.ViewDrawableStates) {
8426 if (i < stateSet1Length && stateSet1[i] == viewState) {
8427 newSet[k++] = viewState;
8428 i++;
8429 } else if (j < stateSet2Length && stateSet2[j] == viewState) {
8430 newSet[k++] = viewState;
8431 j++;
8432 }
8433 if (k > 1) {
8434 assert(newSet[k - 1] > newSet[k - 2]);
8435 }
8436 }
8437 return newSet;
8438 }
8439
8440 /**
8441 * Inflate a view from an XML resource. This convenience method wraps the {@link
8442 * LayoutInflater} class, which provides a full range of options for view inflation.
8443 *
8444 * @param context The Context object for your activity or application.
8445 * @param resource The resource ID to inflate
8446 * @param root A view group that will be the parent. Used to properly inflate the
8447 * layout_* parameters.
8448 * @see LayoutInflater
8449 */
8450 public static View inflate(Context context, int resource, ViewGroup root) {
8451 LayoutInflater factory = LayoutInflater.from(context);
8452 return factory.inflate(resource, root);
8453 }
8454
8455 /**
8456 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
8457 * Each MeasureSpec represents a requirement for either the width or the height.
8458 * A MeasureSpec is comprised of a size and a mode. There are three possible
8459 * modes:
8460 * <dl>
8461 * <dt>UNSPECIFIED</dt>
8462 * <dd>
8463 * The parent has not imposed any constraint on the child. It can be whatever size
8464 * it wants.
8465 * </dd>
8466 *
8467 * <dt>EXACTLY</dt>
8468 * <dd>
8469 * The parent has determined an exact size for the child. The child is going to be
8470 * given those bounds regardless of how big it wants to be.
8471 * </dd>
8472 *
8473 * <dt>AT_MOST</dt>
8474 * <dd>
8475 * The child can be as large as it wants up to the specified size.
8476 * </dd>
8477 * </dl>
8478 *
8479 * MeasureSpecs are implemented as ints to reduce object allocation. This class
8480 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
8481 */
8482 public static class MeasureSpec {
8483 private static final int MODE_SHIFT = 30;
8484 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
8485
8486 /**
8487 * Measure specification mode: The parent has not imposed any constraint
8488 * on the child. It can be whatever size it wants.
8489 */
8490 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
8491
8492 /**
8493 * Measure specification mode: The parent has determined an exact size
8494 * for the child. The child is going to be given those bounds regardless
8495 * of how big it wants to be.
8496 */
8497 public static final int EXACTLY = 1 << MODE_SHIFT;
8498
8499 /**
8500 * Measure specification mode: The child can be as large as it wants up
8501 * to the specified size.
8502 */
8503 public static final int AT_MOST = 2 << MODE_SHIFT;
8504
8505 /**
8506 * Creates a measure specification based on the supplied size and mode.
8507 *
8508 * The mode must always be one of the following:
8509 * <ul>
8510 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
8511 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
8512 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
8513 * </ul>
8514 *
8515 * @param size the size of the measure specification
8516 * @param mode the mode of the measure specification
8517 * @return the measure specification based on size and mode
8518 */
8519 public static int makeMeasureSpec(int size, int mode) {
8520 return size + mode;
8521 }
8522
8523 /**
8524 * Extracts the mode from the supplied measure specification.
8525 *
8526 * @param measureSpec the measure specification to extract the mode from
8527 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
8528 * {@link android.view.View.MeasureSpec#AT_MOST} or
8529 * {@link android.view.View.MeasureSpec#EXACTLY}
8530 */
8531 public static int getMode(int measureSpec) {
8532 return (measureSpec & MODE_MASK);
8533 }
8534
8535 /**
8536 * Extracts the size from the supplied measure specification.
8537 *
8538 * @param measureSpec the measure specification to extract the size from
8539 * @return the size in pixels defined in the supplied measure specification
8540 */
8541 public static int getSize(int measureSpec) {
8542 return (measureSpec & ~MODE_MASK);
8543 }
8544
8545 /**
8546 * Returns a String representation of the specified measure
8547 * specification.
8548 *
8549 * @param measureSpec the measure specification to convert to a String
8550 * @return a String with the following format: "MeasureSpec: MODE SIZE"
8551 */
8552 public static String toString(int measureSpec) {
8553 int mode = getMode(measureSpec);
8554 int size = getSize(measureSpec);
8555
8556 StringBuilder sb = new StringBuilder("MeasureSpec: ");
8557
8558 if (mode == UNSPECIFIED)
8559 sb.append("UNSPECIFIED ");
8560 else if (mode == EXACTLY)
8561 sb.append("EXACTLY ");
8562 else if (mode == AT_MOST)
8563 sb.append("AT_MOST ");
8564 else
8565 sb.append(mode).append(" ");
8566
8567 sb.append(size);
8568 return sb.toString();
8569 }
8570 }
8571
8572 class CheckForLongPress implements Runnable {
8573
8574 private int mOriginalWindowAttachCount;
8575
8576 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -07008577 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008578 && mOriginalWindowAttachCount == mWindowAttachCount) {
8579 if (performLongClick()) {
8580 mHasPerformedLongPress = true;
8581 }
8582 }
8583 }
8584
8585 public void rememberWindowAttachCount() {
8586 mOriginalWindowAttachCount = mWindowAttachCount;
8587 }
8588 }
8589
8590 /**
8591 * Interface definition for a callback to be invoked when a key event is
8592 * dispatched to this view. The callback will be invoked before the key
8593 * event is given to the view.
8594 */
8595 public interface OnKeyListener {
8596 /**
8597 * Called when a key is dispatched to a view. This allows listeners to
8598 * get a chance to respond before the target view.
8599 *
8600 * @param v The view the key has been dispatched to.
8601 * @param keyCode The code for the physical key that was pressed
8602 * @param event The KeyEvent object containing full information about
8603 * the event.
8604 * @return True if the listener has consumed the event, false otherwise.
8605 */
8606 boolean onKey(View v, int keyCode, KeyEvent event);
8607 }
8608
8609 /**
8610 * Interface definition for a callback to be invoked when a touch event is
8611 * dispatched to this view. The callback will be invoked before the touch
8612 * event is given to the view.
8613 */
8614 public interface OnTouchListener {
8615 /**
8616 * Called when a touch event is dispatched to a view. This allows listeners to
8617 * get a chance to respond before the target view.
8618 *
8619 * @param v The view the touch event has been dispatched to.
8620 * @param event The MotionEvent object containing full information about
8621 * the event.
8622 * @return True if the listener has consumed the event, false otherwise.
8623 */
8624 boolean onTouch(View v, MotionEvent event);
8625 }
8626
8627 /**
8628 * Interface definition for a callback to be invoked when a view has been clicked and held.
8629 */
8630 public interface OnLongClickListener {
8631 /**
8632 * Called when a view has been clicked and held.
8633 *
8634 * @param v The view that was clicked and held.
8635 *
8636 * return True if the callback consumed the long click, false otherwise
8637 */
8638 boolean onLongClick(View v);
8639 }
8640
8641 /**
8642 * Interface definition for a callback to be invoked when the focus state of
8643 * a view changed.
8644 */
8645 public interface OnFocusChangeListener {
8646 /**
8647 * Called when the focus state of a view has changed.
8648 *
8649 * @param v The view whose state has changed.
8650 * @param hasFocus The new focus state of v.
8651 */
8652 void onFocusChange(View v, boolean hasFocus);
8653 }
8654
8655 /**
8656 * Interface definition for a callback to be invoked when a view is clicked.
8657 */
8658 public interface OnClickListener {
8659 /**
8660 * Called when a view has been clicked.
8661 *
8662 * @param v The view that was clicked.
8663 */
8664 void onClick(View v);
8665 }
8666
8667 /**
8668 * Interface definition for a callback to be invoked when the context menu
8669 * for this view is being built.
8670 */
8671 public interface OnCreateContextMenuListener {
8672 /**
8673 * Called when the context menu for this view is being built. It is not
8674 * safe to hold onto the menu after this method returns.
8675 *
8676 * @param menu The context menu that is being built
8677 * @param v The view for which the context menu is being built
8678 * @param menuInfo Extra information about the item for which the
8679 * context menu should be shown. This information will vary
8680 * depending on the class of v.
8681 */
8682 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
8683 }
8684
8685 private final class UnsetPressedState implements Runnable {
8686 public void run() {
8687 setPressed(false);
8688 }
8689 }
8690
8691 /**
8692 * Base class for derived classes that want to save and restore their own
8693 * state in {@link android.view.View#onSaveInstanceState()}.
8694 */
8695 public static class BaseSavedState extends AbsSavedState {
8696 /**
8697 * Constructor used when reading from a parcel. Reads the state of the superclass.
8698 *
8699 * @param source
8700 */
8701 public BaseSavedState(Parcel source) {
8702 super(source);
8703 }
8704
8705 /**
8706 * Constructor called by derived classes when creating their SavedState objects
8707 *
8708 * @param superState The state of the superclass of this view
8709 */
8710 public BaseSavedState(Parcelable superState) {
8711 super(superState);
8712 }
8713
8714 public static final Parcelable.Creator<BaseSavedState> CREATOR =
8715 new Parcelable.Creator<BaseSavedState>() {
8716 public BaseSavedState createFromParcel(Parcel in) {
8717 return new BaseSavedState(in);
8718 }
8719
8720 public BaseSavedState[] newArray(int size) {
8721 return new BaseSavedState[size];
8722 }
8723 };
8724 }
8725
8726 /**
8727 * A set of information given to a view when it is attached to its parent
8728 * window.
8729 */
8730 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008731 interface Callbacks {
8732 void playSoundEffect(int effectId);
8733 boolean performHapticFeedback(int effectId, boolean always);
8734 }
8735
8736 /**
8737 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
8738 * to a Handler. This class contains the target (View) to invalidate and
8739 * the coordinates of the dirty rectangle.
8740 *
8741 * For performance purposes, this class also implements a pool of up to
8742 * POOL_LIMIT objects that get reused. This reduces memory allocations
8743 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 */
Romain Guyd928d682009-03-31 17:52:16 -07008745 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008746 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -07008747 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
8748 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -07008749 public InvalidateInfo newInstance() {
8750 return new InvalidateInfo();
8751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752
Romain Guyd928d682009-03-31 17:52:16 -07008753 public void onAcquired(InvalidateInfo element) {
8754 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008755
Romain Guyd928d682009-03-31 17:52:16 -07008756 public void onReleased(InvalidateInfo element) {
8757 }
8758 }, POOL_LIMIT)
8759 );
8760
8761 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008762
8763 View target;
8764
8765 int left;
8766 int top;
8767 int right;
8768 int bottom;
8769
Romain Guyd928d682009-03-31 17:52:16 -07008770 public void setNextPoolable(InvalidateInfo element) {
8771 mNext = element;
8772 }
8773
8774 public InvalidateInfo getNextPoolable() {
8775 return mNext;
8776 }
8777
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008778 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -07008779 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008780 }
8781
8782 void release() {
Romain Guyd928d682009-03-31 17:52:16 -07008783 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008784 }
8785 }
8786
8787 final IWindowSession mSession;
8788
8789 final IWindow mWindow;
8790
8791 final IBinder mWindowToken;
8792
8793 final Callbacks mRootCallbacks;
8794
8795 /**
8796 * The top view of the hierarchy.
8797 */
8798 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -07008799
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008800 IBinder mPanelParentWindowToken;
8801 Surface mSurface;
8802
8803 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008804 * Scale factor used by the compatibility mode
8805 */
8806 float mApplicationScale;
8807
8808 /**
8809 * Indicates whether the application is in compatibility mode
8810 */
8811 boolean mScalingRequired;
8812
8813 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 * Left position of this view's window
8815 */
8816 int mWindowLeft;
8817
8818 /**
8819 * Top position of this view's window
8820 */
8821 int mWindowTop;
8822
8823 /**
8824 * For windows that are full-screen but using insets to layout inside
8825 * of the screen decorations, these are the current insets for the
8826 * content of the window.
8827 */
8828 final Rect mContentInsets = new Rect();
8829
8830 /**
8831 * For windows that are full-screen but using insets to layout inside
8832 * of the screen decorations, these are the current insets for the
8833 * actual visible parts of the window.
8834 */
8835 final Rect mVisibleInsets = new Rect();
8836
8837 /**
8838 * The internal insets given by this window. This value is
8839 * supplied by the client (through
8840 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
8841 * be given to the window manager when changed to be used in laying
8842 * out windows behind it.
8843 */
8844 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
8845 = new ViewTreeObserver.InternalInsetsInfo();
8846
8847 /**
8848 * All views in the window's hierarchy that serve as scroll containers,
8849 * used to determine if the window can be resized or must be panned
8850 * to adjust for a soft input area.
8851 */
8852 final ArrayList<View> mScrollContainers = new ArrayList<View>();
8853
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07008854 final KeyEvent.DispatcherState mKeyDispatchState
8855 = new KeyEvent.DispatcherState();
8856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008857 /**
8858 * Indicates whether the view's window currently has the focus.
8859 */
8860 boolean mHasWindowFocus;
8861
8862 /**
8863 * The current visibility of the window.
8864 */
8865 int mWindowVisibility;
8866
8867 /**
8868 * Indicates the time at which drawing started to occur.
8869 */
8870 long mDrawingTime;
8871
8872 /**
Romain Guy5bcdff42009-05-14 21:27:18 -07008873 * Indicates whether or not ignoring the DIRTY_MASK flags.
8874 */
8875 boolean mIgnoreDirtyState;
8876
8877 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008878 * Indicates whether the view's window is currently in touch mode.
8879 */
8880 boolean mInTouchMode;
8881
8882 /**
8883 * Indicates that ViewRoot should trigger a global layout change
8884 * the next time it performs a traversal
8885 */
8886 boolean mRecomputeGlobalAttributes;
8887
8888 /**
8889 * Set to true when attributes (like mKeepScreenOn) need to be
8890 * recomputed.
8891 */
8892 boolean mAttributesChanged;
8893
8894 /**
8895 * Set during a traveral if any views want to keep the screen on.
8896 */
8897 boolean mKeepScreenOn;
8898
8899 /**
8900 * Set if the visibility of any views has changed.
8901 */
8902 boolean mViewVisibilityChanged;
8903
8904 /**
8905 * Set to true if a view has been scrolled.
8906 */
8907 boolean mViewScrollChanged;
8908
8909 /**
8910 * Global to the view hierarchy used as a temporary for dealing with
8911 * x/y points in the transparent region computations.
8912 */
8913 final int[] mTransparentLocation = new int[2];
8914
8915 /**
8916 * Global to the view hierarchy used as a temporary for dealing with
8917 * x/y points in the ViewGroup.invalidateChild implementation.
8918 */
8919 final int[] mInvalidateChildLocation = new int[2];
8920
8921 /**
8922 * The view tree observer used to dispatch global events like
8923 * layout, pre-draw, touch mode change, etc.
8924 */
8925 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
8926
8927 /**
8928 * A Canvas used by the view hierarchy to perform bitmap caching.
8929 */
8930 Canvas mCanvas;
8931
8932 /**
8933 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
8934 * handler can be used to pump events in the UI events queue.
8935 */
8936 final Handler mHandler;
8937
8938 /**
8939 * Identifier for messages requesting the view to be invalidated.
8940 * Such messages should be sent to {@link #mHandler}.
8941 */
8942 static final int INVALIDATE_MSG = 0x1;
8943
8944 /**
8945 * Identifier for messages requesting the view to invalidate a region.
8946 * Such messages should be sent to {@link #mHandler}.
8947 */
8948 static final int INVALIDATE_RECT_MSG = 0x2;
8949
8950 /**
8951 * Temporary for use in computing invalidate rectangles while
8952 * calling up the hierarchy.
8953 */
8954 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -07008955
8956 /**
8957 * Temporary list for use in collecting focusable descendents of a view.
8958 */
8959 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
8960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 /**
8962 * Creates a new set of attachment information with the specified
8963 * events handler and thread.
8964 *
8965 * @param handler the events handler the view must use
8966 */
8967 AttachInfo(IWindowSession session, IWindow window,
8968 Handler handler, Callbacks effectPlayer) {
8969 mSession = session;
8970 mWindow = window;
8971 mWindowToken = window.asBinder();
8972 mHandler = handler;
8973 mRootCallbacks = effectPlayer;
8974 }
8975 }
8976
8977 /**
8978 * <p>ScrollabilityCache holds various fields used by a View when scrolling
8979 * is supported. This avoids keeping too many unused fields in most
8980 * instances of View.</p>
8981 */
Mike Cleronf116bf82009-09-27 19:14:12 -07008982 private static class ScrollabilityCache implements Runnable {
8983
8984 /**
8985 * Scrollbars are not visible
8986 */
8987 public static final int OFF = 0;
8988
8989 /**
8990 * Scrollbars are visible
8991 */
8992 public static final int ON = 1;
8993
8994 /**
8995 * Scrollbars are fading away
8996 */
8997 public static final int FADING = 2;
8998
8999 public boolean fadeScrollBars;
9000
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -07009002 public int scrollBarDefaultDelayBeforeFade;
9003 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004
9005 public int scrollBarSize;
9006 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -07009007 public float[] interpolatorValues;
9008 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009009
9010 public final Paint paint;
9011 public final Matrix matrix;
9012 public Shader shader;
9013
Mike Cleronf116bf82009-09-27 19:14:12 -07009014 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
9015
9016 private final float[] mOpaque = {255.0f};
9017 private final float[] mTransparent = {0.0f};
9018
9019 /**
9020 * When fading should start. This time moves into the future every time
9021 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
9022 */
9023 public long fadeStartTime;
9024
9025
9026 /**
9027 * The current state of the scrollbars: ON, OFF, or FADING
9028 */
9029 public int state = OFF;
9030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009031 private int mLastColor;
9032
Mike Cleronf116bf82009-09-27 19:14:12 -07009033 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
9035 scrollBarSize = configuration.getScaledScrollBarSize();
Mike Cleronf116bf82009-09-27 19:14:12 -07009036 scrollBarDefaultDelayBeforeFade = configuration.getScrollDefaultDelay();
9037 scrollBarFadeDuration = configuration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009038
9039 paint = new Paint();
9040 matrix = new Matrix();
9041 // use use a height of 1, and then wack the matrix each time we
9042 // actually use it.
9043 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -07009044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009045 paint.setShader(shader);
9046 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -07009047 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009048 }
Romain Guy8506ab42009-06-11 17:35:47 -07009049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 public void setFadeColor(int color) {
9051 if (color != 0 && color != mLastColor) {
9052 mLastColor = color;
9053 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -07009054
Romain Guye55e1a72009-08-27 10:42:26 -07009055 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
9056 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -07009057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009058 paint.setShader(shader);
9059 // Restore the default transfer mode (src_over)
9060 paint.setXfermode(null);
9061 }
9062 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009063
9064 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009065 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -07009066 if (now >= fadeStartTime) {
9067
9068 // the animation fades the scrollbars out by changing
9069 // the opacity (alpha) from fully opaque to fully
9070 // transparent
9071 int nextFrame = (int) now;
9072 int framesCount = 0;
9073
9074 Interpolator interpolator = scrollBarInterpolator;
9075
9076 // Start opaque
9077 interpolator.setKeyFrame(framesCount++, nextFrame, mOpaque);
9078
9079 // End transparent
9080 nextFrame += scrollBarFadeDuration;
9081 interpolator.setKeyFrame(framesCount, nextFrame, mTransparent);
9082
9083 state = FADING;
9084
9085 // Kick off the fade animation
9086 host.invalidate();
9087 }
9088 }
9089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 }
9091}