blob: 745839d4dc32ca3c3d97a6d6f3689f338018dbbb [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;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080023import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.res.Resources;
25import android.content.res.TypedArray;
26import android.graphics.Bitmap;
27import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070028import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.graphics.LinearGradient;
30import android.graphics.Matrix;
31import android.graphics.Paint;
32import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070033import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.graphics.PorterDuff;
35import android.graphics.PorterDuffXfermode;
36import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070037import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.graphics.Region;
39import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.drawable.ColorDrawable;
41import android.graphics.drawable.Drawable;
42import android.os.Handler;
43import android.os.IBinder;
44import android.os.Message;
45import android.os.Parcel;
46import android.os.Parcelable;
47import android.os.RemoteException;
48import android.os.SystemClock;
49import android.os.SystemProperties;
50import android.util.AttributeSet;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.util.Config;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.util.EventLog;
53import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070056import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.Pools;
58import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
Adam Powell6e346362010-07-23 10:18:23 -070060import android.view.View.MeasureSpec;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
svetoslavganov75986cf2009-05-14 22:28:01 -070071import java.lang.ref.SoftReference;
72import java.lang.reflect.InvocationTargetException;
73import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.util.ArrayList;
75import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070076import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78/**
79 * <p>
80 * This class represents the basic building block for user interface components. A View
81 * occupies a rectangular area on the screen and is responsible for drawing and
82 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070083 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
85 * are invisible containers that hold other Views (or other ViewGroups) and define
86 * their layout properties.
87 * </p>
88 *
89 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070090 * <p>For an introduction to using this class to develop your
91 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070093 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
95 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
102 * </p>
103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
129 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
130 * specialized listeners. For example, a Button exposes a listener to notify
131 * clients when the button is clicked.</li>
132 * <li><strong>Set visibility:</strong> You can hide or show views using
133 * {@link #setVisibility}.</li>
134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
174 * <td><code>{@link #onMeasure}</code></td>
175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
180 * <td><code>{@link #onLayout}</code></td>
181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
186 * <td><code>{@link #onSizeChanged}</code></td>
187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
193 * <td><code>{@link #onDraw}</code></td>
194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
200 * <td><code>{@link #onKeyDown}</code></td>
201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
205 * <td><code>{@link #onKeyUp}</code></td>
206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
210 * <td><code>{@link #onTrackballEvent}</code></td>
211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
215 * <td><code>{@link #onTouchEvent}</code></td>
216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
222 * <td><code>{@link #onFocusChanged}</code></td>
223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td><code>{@link #onWindowFocusChanged}</code></td>
229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
265 * &lt;Button id="@+id/my_button"
266 * android:layout_width="wrap_content"
267 * android:layout_height="wrap_content"
268 * android:text="@string/my_button_text"/&gt;
269 * </pre></li>
270 * <li>From the onCreate method of an Activity, find the Button
271 * <pre class="prettyprint">
272 * Button myButton = (Button) findViewById(R.id.my_button);
273 * </pre></li>
274 * </ul>
275 * <p>
276 * View IDs need not be unique throughout the tree, but it is good practice to
277 * ensure that they are at least unique within the part of the tree you are
278 * searching.
279 * </p>
280 *
281 * <a name="Position"></a>
282 * <h3>Position</h3>
283 * <p>
284 * The geometry of a view is that of a rectangle. A view has a location,
285 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
286 * two dimensions, expressed as a width and a height. The unit for location
287 * and dimensions is the pixel.
288 * </p>
289 *
290 * <p>
291 * It is possible to retrieve the location of a view by invoking the methods
292 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
293 * coordinate of the rectangle representing the view. The latter returns the
294 * top, or Y, coordinate of the rectangle representing the view. These methods
295 * both return the location of the view relative to its parent. For instance,
296 * when getLeft() returns 20, that means the view is located 20 pixels to the
297 * right of the left edge of its direct parent.
298 * </p>
299 *
300 * <p>
301 * In addition, several convenience methods are offered to avoid unnecessary
302 * computations, namely {@link #getRight()} and {@link #getBottom()}.
303 * These methods return the coordinates of the right and bottom edges of the
304 * rectangle representing the view. For instance, calling {@link #getRight()}
305 * is similar to the following computation: <code>getLeft() + getWidth()</code>
306 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
307 * </p>
308 *
309 * <a name="SizePaddingMargins"></a>
310 * <h3>Size, padding and margins</h3>
311 * <p>
312 * The size of a view is expressed with a width and a height. A view actually
313 * possess two pairs of width and height values.
314 * </p>
315 *
316 * <p>
317 * The first pair is known as <em>measured width</em> and
318 * <em>measured height</em>. These dimensions define how big a view wants to be
319 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
320 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
321 * and {@link #getMeasuredHeight()}.
322 * </p>
323 *
324 * <p>
325 * The second pair is simply known as <em>width</em> and <em>height</em>, or
326 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
327 * dimensions define the actual size of the view on screen, at drawing time and
328 * after layout. These values may, but do not have to, be different from the
329 * measured width and height. The width and height can be obtained by calling
330 * {@link #getWidth()} and {@link #getHeight()}.
331 * </p>
332 *
333 * <p>
334 * To measure its dimensions, a view takes into account its padding. The padding
335 * is expressed in pixels for the left, top, right and bottom parts of the view.
336 * Padding can be used to offset the content of the view by a specific amount of
337 * pixels. For instance, a left padding of 2 will push the view's content by
338 * 2 pixels to the right of the left edge. Padding can be set using the
339 * {@link #setPadding(int, int, int, int)} method and queried by calling
340 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
341 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
342 * </p>
343 *
344 * <p>
345 * Even though a view can define a padding, it does not provide any support for
346 * margins. However, view groups provide such a support. Refer to
347 * {@link android.view.ViewGroup} and
348 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
349 * </p>
350 *
351 * <a name="Layout"></a>
352 * <h3>Layout</h3>
353 * <p>
354 * Layout is a two pass process: a measure pass and a layout pass. The measuring
355 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
356 * of the view tree. Each view pushes dimension specifications down the tree
357 * during the recursion. At the end of the measure pass, every view has stored
358 * its measurements. The second pass happens in
359 * {@link #layout(int,int,int,int)} and is also top-down. During
360 * this pass each parent is responsible for positioning all of its children
361 * using the sizes computed in the measure pass.
362 * </p>
363 *
364 * <p>
365 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
366 * {@link #getMeasuredHeight()} values must be set, along with those for all of
367 * that view's descendants. A view's measured width and measured height values
368 * must respect the constraints imposed by the view's parents. This guarantees
369 * that at the end of the measure pass, all parents accept all of their
370 * children's measurements. A parent view may call measure() more than once on
371 * its children. For example, the parent may measure each child once with
372 * unspecified dimensions to find out how big they want to be, then call
373 * measure() on them again with actual numbers if the sum of all the children's
374 * unconstrained sizes is too big or too small.
375 * </p>
376 *
377 * <p>
378 * The measure pass uses two classes to communicate dimensions. The
379 * {@link MeasureSpec} class is used by views to tell their parents how they
380 * want to be measured and positioned. The base LayoutParams class just
381 * describes how big the view wants to be for both width and height. For each
382 * dimension, it can specify one of:
383 * <ul>
384 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800385 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 * (minus padding)
387 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
388 * enclose its content (plus padding).
389 * </ul>
390 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
391 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
392 * an X and Y value.
393 * </p>
394 *
395 * <p>
396 * MeasureSpecs are used to push requirements down the tree from parent to
397 * child. A MeasureSpec can be in one of three modes:
398 * <ul>
399 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
400 * of a child view. For example, a LinearLayout may call measure() on its child
401 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
402 * tall the child view wants to be given a width of 240 pixels.
403 * <li>EXACTLY: This is used by the parent to impose an exact size on the
404 * child. The child must use this size, and guarantee that all of its
405 * descendants will fit within this size.
406 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
407 * child. The child must gurantee that it and all of its descendants will fit
408 * within this size.
409 * </ul>
410 * </p>
411 *
412 * <p>
413 * To intiate a layout, call {@link #requestLayout}. This method is typically
414 * called by a view on itself when it believes that is can no longer fit within
415 * its current bounds.
416 * </p>
417 *
418 * <a name="Drawing"></a>
419 * <h3>Drawing</h3>
420 * <p>
421 * Drawing is handled by walking the tree and rendering each view that
422 * intersects the the invalid region. Because the tree is traversed in-order,
423 * this means that parents will draw before (i.e., behind) their children, with
424 * siblings drawn in the order they appear in the tree.
425 * If you set a background drawable for a View, then the View will draw it for you
426 * before calling back to its <code>onDraw()</code> method.
427 * </p>
428 *
429 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700430 * 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 -0800431 * </p>
432 *
433 * <p>
434 * To force a view to draw, call {@link #invalidate()}.
435 * </p>
436 *
437 * <a name="EventHandlingThreading"></a>
438 * <h3>Event Handling and Threading</h3>
439 * <p>
440 * The basic cycle of a view is as follows:
441 * <ol>
442 * <li>An event comes in and is dispatched to the appropriate view. The view
443 * handles the event and notifies any listeners.</li>
444 * <li>If in the course of processing the event, the view's bounds may need
445 * to be changed, the view will call {@link #requestLayout()}.</li>
446 * <li>Similarly, if in the course of processing the event the view's appearance
447 * may need to be changed, the view will call {@link #invalidate()}.</li>
448 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
449 * the framework will take care of measuring, laying out, and drawing the tree
450 * as appropriate.</li>
451 * </ol>
452 * </p>
453 *
454 * <p><em>Note: The entire view tree is single threaded. You must always be on
455 * the UI thread when calling any method on any view.</em>
456 * If you are doing work on other threads and want to update the state of a view
457 * from that thread, you should use a {@link Handler}.
458 * </p>
459 *
460 * <a name="FocusHandling"></a>
461 * <h3>Focus Handling</h3>
462 * <p>
463 * The framework will handle routine focus movement in response to user input.
464 * This includes changing the focus as views are removed or hidden, or as new
465 * views become available. Views indicate their willingness to take focus
466 * through the {@link #isFocusable} method. To change whether a view can take
467 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
468 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
469 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
470 * </p>
471 * <p>
472 * Focus movement is based on an algorithm which finds the nearest neighbor in a
473 * given direction. In rare cases, the default algorithm may not match the
474 * intended behavior of the developer. In these situations, you can provide
475 * explicit overrides by using these XML attributes in the layout file:
476 * <pre>
477 * nextFocusDown
478 * nextFocusLeft
479 * nextFocusRight
480 * nextFocusUp
481 * </pre>
482 * </p>
483 *
484 *
485 * <p>
486 * To get a particular view to take focus, call {@link #requestFocus()}.
487 * </p>
488 *
489 * <a name="TouchMode"></a>
490 * <h3>Touch Mode</h3>
491 * <p>
492 * When a user is navigating a user interface via directional keys such as a D-pad, it is
493 * necessary to give focus to actionable items such as buttons so the user can see
494 * what will take input. If the device has touch capabilities, however, and the user
495 * begins interacting with the interface by touching it, it is no longer necessary to
496 * always highlight, or give focus to, a particular view. This motivates a mode
497 * for interaction named 'touch mode'.
498 * </p>
499 * <p>
500 * For a touch capable device, once the user touches the screen, the device
501 * will enter touch mode. From this point onward, only views for which
502 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
503 * Other views that are touchable, like buttons, will not take focus when touched; they will
504 * only fire the on click listeners.
505 * </p>
506 * <p>
507 * Any time a user hits a directional key, such as a D-pad direction, the view device will
508 * exit touch mode, and find a view to take focus, so that the user may resume interacting
509 * with the user interface without touching the screen again.
510 * </p>
511 * <p>
512 * The touch mode state is maintained across {@link android.app.Activity}s. Call
513 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
514 * </p>
515 *
516 * <a name="Scrolling"></a>
517 * <h3>Scrolling</h3>
518 * <p>
519 * The framework provides basic support for views that wish to internally
520 * scroll their content. This includes keeping track of the X and Y scroll
521 * offset as well as mechanisms for drawing scrollbars. See
Mike Cleronf116bf82009-09-27 19:14:12 -0700522 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
523 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 * </p>
525 *
526 * <a name="Tags"></a>
527 * <h3>Tags</h3>
528 * <p>
529 * Unlike IDs, tags are not used to identify views. Tags are essentially an
530 * extra piece of information that can be associated with a view. They are most
531 * often used as a convenience to store data related to views in the views
532 * themselves rather than by putting them in a separate structure.
533 * </p>
534 *
535 * <a name="Animation"></a>
536 * <h3>Animation</h3>
537 * <p>
538 * You can attach an {@link Animation} object to a view using
539 * {@link #setAnimation(Animation)} or
540 * {@link #startAnimation(Animation)}. The animation can alter the scale,
541 * rotation, translation and alpha of a view over time. If the animation is
542 * attached to a view that has children, the animation will affect the entire
543 * subtree rooted by that node. When an animation is started, the framework will
544 * take care of redrawing the appropriate views until the animation completes.
545 * </p>
546 *
Romain Guyd6a463a2009-05-21 23:10:10 -0700547 * @attr ref android.R.styleable#View_background
548 * @attr ref android.R.styleable#View_clickable
549 * @attr ref android.R.styleable#View_contentDescription
550 * @attr ref android.R.styleable#View_drawingCacheQuality
551 * @attr ref android.R.styleable#View_duplicateParentState
552 * @attr ref android.R.styleable#View_id
553 * @attr ref android.R.styleable#View_fadingEdge
554 * @attr ref android.R.styleable#View_fadingEdgeLength
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700556 * @attr ref android.R.styleable#View_isScrollContainer
557 * @attr ref android.R.styleable#View_focusable
558 * @attr ref android.R.styleable#View_focusableInTouchMode
559 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
560 * @attr ref android.R.styleable#View_keepScreenOn
561 * @attr ref android.R.styleable#View_longClickable
562 * @attr ref android.R.styleable#View_minHeight
563 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 * @attr ref android.R.styleable#View_nextFocusDown
565 * @attr ref android.R.styleable#View_nextFocusLeft
566 * @attr ref android.R.styleable#View_nextFocusRight
567 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700568 * @attr ref android.R.styleable#View_onClick
569 * @attr ref android.R.styleable#View_padding
570 * @attr ref android.R.styleable#View_paddingBottom
571 * @attr ref android.R.styleable#View_paddingLeft
572 * @attr ref android.R.styleable#View_paddingRight
573 * @attr ref android.R.styleable#View_paddingTop
574 * @attr ref android.R.styleable#View_saveEnabled
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 * @attr ref android.R.styleable#View_scrollX
576 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_scrollbarSize
578 * @attr ref android.R.styleable#View_scrollbarStyle
579 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700580 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
581 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
583 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_scrollbarThumbVertical
585 * @attr ref android.R.styleable#View_scrollbarTrackVertical
586 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
587 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700588 * @attr ref android.R.styleable#View_soundEffectsEnabled
589 * @attr ref android.R.styleable#View_tag
590 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 *
592 * @see android.view.ViewGroup
593 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700594public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800595 private static final boolean DBG = false;
596
597 /**
598 * The logging tag used by this class with android.util.Log.
599 */
600 protected static final String VIEW_LOG_TAG = "View";
601
602 /**
603 * Used to mark a View that has no ID.
604 */
605 public static final int NO_ID = -1;
606
607 /**
608 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
609 * calling setFlags.
610 */
611 private static final int NOT_FOCUSABLE = 0x00000000;
612
613 /**
614 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
615 * setFlags.
616 */
617 private static final int FOCUSABLE = 0x00000001;
618
619 /**
620 * Mask for use with setFlags indicating bits used for focus.
621 */
622 private static final int FOCUSABLE_MASK = 0x00000001;
623
624 /**
625 * This view will adjust its padding to fit sytem windows (e.g. status bar)
626 */
627 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
628
629 /**
630 * This view is visible. Use with {@link #setVisibility}.
631 */
632 public static final int VISIBLE = 0x00000000;
633
634 /**
635 * This view is invisible, but it still takes up space for layout purposes.
636 * Use with {@link #setVisibility}.
637 */
638 public static final int INVISIBLE = 0x00000004;
639
640 /**
641 * This view is invisible, and it doesn't take any space for layout
642 * purposes. Use with {@link #setVisibility}.
643 */
644 public static final int GONE = 0x00000008;
645
646 /**
647 * Mask for use with setFlags indicating bits used for visibility.
648 * {@hide}
649 */
650 static final int VISIBILITY_MASK = 0x0000000C;
651
652 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
653
654 /**
655 * This view is enabled. Intrepretation varies by subclass.
656 * Use with ENABLED_MASK when calling setFlags.
657 * {@hide}
658 */
659 static final int ENABLED = 0x00000000;
660
661 /**
662 * This view is disabled. Intrepretation varies by subclass.
663 * Use with ENABLED_MASK when calling setFlags.
664 * {@hide}
665 */
666 static final int DISABLED = 0x00000020;
667
668 /**
669 * Mask for use with setFlags indicating bits used for indicating whether
670 * this view is enabled
671 * {@hide}
672 */
673 static final int ENABLED_MASK = 0x00000020;
674
675 /**
676 * This view won't draw. {@link #onDraw} won't be called and further
677 * optimizations
678 * will be performed. It is okay to have this flag set and a background.
679 * Use with DRAW_MASK when calling setFlags.
680 * {@hide}
681 */
682 static final int WILL_NOT_DRAW = 0x00000080;
683
684 /**
685 * Mask for use with setFlags indicating bits used for indicating whether
686 * this view is will draw
687 * {@hide}
688 */
689 static final int DRAW_MASK = 0x00000080;
690
691 /**
692 * <p>This view doesn't show scrollbars.</p>
693 * {@hide}
694 */
695 static final int SCROLLBARS_NONE = 0x00000000;
696
697 /**
698 * <p>This view shows horizontal scrollbars.</p>
699 * {@hide}
700 */
701 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
702
703 /**
704 * <p>This view shows vertical scrollbars.</p>
705 * {@hide}
706 */
707 static final int SCROLLBARS_VERTICAL = 0x00000200;
708
709 /**
710 * <p>Mask for use with setFlags indicating bits used for indicating which
711 * scrollbars are enabled.</p>
712 * {@hide}
713 */
714 static final int SCROLLBARS_MASK = 0x00000300;
715
716 // note 0x00000400 and 0x00000800 are now available for next flags...
717
718 /**
719 * <p>This view doesn't show fading edges.</p>
720 * {@hide}
721 */
722 static final int FADING_EDGE_NONE = 0x00000000;
723
724 /**
725 * <p>This view shows horizontal fading edges.</p>
726 * {@hide}
727 */
728 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
729
730 /**
731 * <p>This view shows vertical fading edges.</p>
732 * {@hide}
733 */
734 static final int FADING_EDGE_VERTICAL = 0x00002000;
735
736 /**
737 * <p>Mask for use with setFlags indicating bits used for indicating which
738 * fading edges are enabled.</p>
739 * {@hide}
740 */
741 static final int FADING_EDGE_MASK = 0x00003000;
742
743 /**
744 * <p>Indicates this view can be clicked. When clickable, a View reacts
745 * to clicks by notifying the OnClickListener.<p>
746 * {@hide}
747 */
748 static final int CLICKABLE = 0x00004000;
749
750 /**
751 * <p>Indicates this view is caching its drawing into a bitmap.</p>
752 * {@hide}
753 */
754 static final int DRAWING_CACHE_ENABLED = 0x00008000;
755
756 /**
757 * <p>Indicates that no icicle should be saved for this view.<p>
758 * {@hide}
759 */
760 static final int SAVE_DISABLED = 0x000010000;
761
762 /**
763 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
764 * property.</p>
765 * {@hide}
766 */
767 static final int SAVE_DISABLED_MASK = 0x000010000;
768
769 /**
770 * <p>Indicates that no drawing cache should ever be created for this view.<p>
771 * {@hide}
772 */
773 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
774
775 /**
776 * <p>Indicates this view can take / keep focus when int touch mode.</p>
777 * {@hide}
778 */
779 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
780
781 /**
782 * <p>Enables low quality mode for the drawing cache.</p>
783 */
784 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
785
786 /**
787 * <p>Enables high quality mode for the drawing cache.</p>
788 */
789 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
790
791 /**
792 * <p>Enables automatic quality mode for the drawing cache.</p>
793 */
794 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
795
796 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
797 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
798 };
799
800 /**
801 * <p>Mask for use with setFlags indicating bits used for the cache
802 * quality property.</p>
803 * {@hide}
804 */
805 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
806
807 /**
808 * <p>
809 * Indicates this view can be long clicked. When long clickable, a View
810 * reacts to long clicks by notifying the OnLongClickListener or showing a
811 * context menu.
812 * </p>
813 * {@hide}
814 */
815 static final int LONG_CLICKABLE = 0x00200000;
816
817 /**
818 * <p>Indicates that this view gets its drawable states from its direct parent
819 * and ignores its original internal states.</p>
820 *
821 * @hide
822 */
823 static final int DUPLICATE_PARENT_STATE = 0x00400000;
824
825 /**
826 * The scrollbar style to display the scrollbars inside the content area,
827 * without increasing the padding. The scrollbars will be overlaid with
828 * translucency on the view's content.
829 */
830 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
831
832 /**
833 * The scrollbar style to display the scrollbars inside the padded area,
834 * increasing the padding of the view. The scrollbars will not overlap the
835 * content area of the view.
836 */
837 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
838
839 /**
840 * The scrollbar style to display the scrollbars at the edge of the view,
841 * without increasing the padding. The scrollbars will be overlaid with
842 * translucency.
843 */
844 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
845
846 /**
847 * The scrollbar style to display the scrollbars at the edge of the view,
848 * increasing the padding of the view. The scrollbars will only overlap the
849 * background, if any.
850 */
851 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
852
853 /**
854 * Mask to check if the scrollbar style is overlay or inset.
855 * {@hide}
856 */
857 static final int SCROLLBARS_INSET_MASK = 0x01000000;
858
859 /**
860 * Mask to check if the scrollbar style is inside or outside.
861 * {@hide}
862 */
863 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
864
865 /**
866 * Mask for scrollbar style.
867 * {@hide}
868 */
869 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
870
871 /**
872 * View flag indicating that the screen should remain on while the
873 * window containing this view is visible to the user. This effectively
874 * takes care of automatically setting the WindowManager's
875 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
876 */
877 public static final int KEEP_SCREEN_ON = 0x04000000;
878
879 /**
880 * View flag indicating whether this view should have sound effects enabled
881 * for events such as clicking and touching.
882 */
883 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
884
885 /**
886 * View flag indicating whether this view should have haptic feedback
887 * enabled for events such as long presses.
888 */
889 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
890
891 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700892 * <p>Indicates that the view hierarchy should stop saving state when
893 * it reaches this view. If state saving is initiated immediately at
894 * the view, it will be allowed.
895 * {@hide}
896 */
897 static final int PARENT_SAVE_DISABLED = 0x20000000;
898
899 /**
900 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
901 * {@hide}
902 */
903 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
904
905 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700906 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
907 * should add all focusable Views regardless if they are focusable in touch mode.
908 */
909 public static final int FOCUSABLES_ALL = 0x00000000;
910
911 /**
912 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
913 * should add only Views focusable in touch mode.
914 */
915 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
916
917 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 * Use with {@link #focusSearch}. Move focus to the previous selectable
919 * item.
920 */
921 public static final int FOCUS_BACKWARD = 0x00000001;
922
923 /**
924 * Use with {@link #focusSearch}. Move focus to the next selectable
925 * item.
926 */
927 public static final int FOCUS_FORWARD = 0x00000002;
928
929 /**
930 * Use with {@link #focusSearch}. Move focus to the left.
931 */
932 public static final int FOCUS_LEFT = 0x00000011;
933
934 /**
935 * Use with {@link #focusSearch}. Move focus up.
936 */
937 public static final int FOCUS_UP = 0x00000021;
938
939 /**
940 * Use with {@link #focusSearch}. Move focus to the right.
941 */
942 public static final int FOCUS_RIGHT = 0x00000042;
943
944 /**
945 * Use with {@link #focusSearch}. Move focus down.
946 */
947 public static final int FOCUS_DOWN = 0x00000082;
948
949 /**
950 * Base View state sets
951 */
952 // Singles
953 /**
954 * Indicates the view has no states set. States are used with
955 * {@link android.graphics.drawable.Drawable} to change the drawing of the
956 * view depending on its state.
957 *
958 * @see android.graphics.drawable.Drawable
959 * @see #getDrawableState()
960 */
961 protected static final int[] EMPTY_STATE_SET = {};
962 /**
963 * Indicates the view is enabled. States are used with
964 * {@link android.graphics.drawable.Drawable} to change the drawing of the
965 * view depending on its state.
966 *
967 * @see android.graphics.drawable.Drawable
968 * @see #getDrawableState()
969 */
970 protected static final int[] ENABLED_STATE_SET = {R.attr.state_enabled};
971 /**
972 * Indicates the view is focused. States are used with
973 * {@link android.graphics.drawable.Drawable} to change the drawing of the
974 * view depending on its state.
975 *
976 * @see android.graphics.drawable.Drawable
977 * @see #getDrawableState()
978 */
979 protected static final int[] FOCUSED_STATE_SET = {R.attr.state_focused};
980 /**
981 * Indicates the view is selected. States are used with
982 * {@link android.graphics.drawable.Drawable} to change the drawing of the
983 * view depending on its state.
984 *
985 * @see android.graphics.drawable.Drawable
986 * @see #getDrawableState()
987 */
988 protected static final int[] SELECTED_STATE_SET = {R.attr.state_selected};
989 /**
990 * Indicates the view is pressed. States are used with
991 * {@link android.graphics.drawable.Drawable} to change the drawing of the
992 * view depending on its state.
993 *
994 * @see android.graphics.drawable.Drawable
995 * @see #getDrawableState()
996 * @hide
997 */
998 protected static final int[] PRESSED_STATE_SET = {R.attr.state_pressed};
999 /**
1000 * Indicates the view's window has focus. States are used with
1001 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1002 * view depending on its state.
1003 *
1004 * @see android.graphics.drawable.Drawable
1005 * @see #getDrawableState()
1006 */
1007 protected static final int[] WINDOW_FOCUSED_STATE_SET =
1008 {R.attr.state_window_focused};
1009 // Doubles
1010 /**
1011 * Indicates the view is enabled and has the focus.
1012 *
1013 * @see #ENABLED_STATE_SET
1014 * @see #FOCUSED_STATE_SET
1015 */
1016 protected static final int[] ENABLED_FOCUSED_STATE_SET =
1017 stateSetUnion(ENABLED_STATE_SET, FOCUSED_STATE_SET);
1018 /**
1019 * Indicates the view is enabled and selected.
1020 *
1021 * @see #ENABLED_STATE_SET
1022 * @see #SELECTED_STATE_SET
1023 */
1024 protected static final int[] ENABLED_SELECTED_STATE_SET =
1025 stateSetUnion(ENABLED_STATE_SET, SELECTED_STATE_SET);
1026 /**
1027 * Indicates the view is enabled and that its window has focus.
1028 *
1029 * @see #ENABLED_STATE_SET
1030 * @see #WINDOW_FOCUSED_STATE_SET
1031 */
1032 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET =
1033 stateSetUnion(ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1034 /**
1035 * Indicates the view is focused and selected.
1036 *
1037 * @see #FOCUSED_STATE_SET
1038 * @see #SELECTED_STATE_SET
1039 */
1040 protected static final int[] FOCUSED_SELECTED_STATE_SET =
1041 stateSetUnion(FOCUSED_STATE_SET, SELECTED_STATE_SET);
1042 /**
1043 * Indicates the view has the focus and that its window has the focus.
1044 *
1045 * @see #FOCUSED_STATE_SET
1046 * @see #WINDOW_FOCUSED_STATE_SET
1047 */
1048 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET =
1049 stateSetUnion(FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1050 /**
1051 * Indicates the view is selected and that its window has the focus.
1052 *
1053 * @see #SELECTED_STATE_SET
1054 * @see #WINDOW_FOCUSED_STATE_SET
1055 */
1056 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET =
1057 stateSetUnion(SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1058 // Triples
1059 /**
1060 * Indicates the view is enabled, focused and selected.
1061 *
1062 * @see #ENABLED_STATE_SET
1063 * @see #FOCUSED_STATE_SET
1064 * @see #SELECTED_STATE_SET
1065 */
1066 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET =
1067 stateSetUnion(ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1068 /**
1069 * Indicates the view is enabled, focused and its window has the focus.
1070 *
1071 * @see #ENABLED_STATE_SET
1072 * @see #FOCUSED_STATE_SET
1073 * @see #WINDOW_FOCUSED_STATE_SET
1074 */
1075 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1076 stateSetUnion(ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1077 /**
1078 * Indicates the view is enabled, selected and its window has the focus.
1079 *
1080 * @see #ENABLED_STATE_SET
1081 * @see #SELECTED_STATE_SET
1082 * @see #WINDOW_FOCUSED_STATE_SET
1083 */
1084 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1085 stateSetUnion(ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1086 /**
1087 * Indicates the view is focused, selected and its window has the focus.
1088 *
1089 * @see #FOCUSED_STATE_SET
1090 * @see #SELECTED_STATE_SET
1091 * @see #WINDOW_FOCUSED_STATE_SET
1092 */
1093 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1094 stateSetUnion(FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1095 /**
1096 * Indicates the view is enabled, focused, selected and its window
1097 * has the focus.
1098 *
1099 * @see #ENABLED_STATE_SET
1100 * @see #FOCUSED_STATE_SET
1101 * @see #SELECTED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
1104 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1105 stateSetUnion(ENABLED_FOCUSED_SELECTED_STATE_SET,
1106 WINDOW_FOCUSED_STATE_SET);
1107
1108 /**
1109 * Indicates the view is pressed and its window has the focus.
1110 *
1111 * @see #PRESSED_STATE_SET
1112 * @see #WINDOW_FOCUSED_STATE_SET
1113 */
1114 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET =
1115 stateSetUnion(PRESSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1116
1117 /**
1118 * Indicates the view is pressed and selected.
1119 *
1120 * @see #PRESSED_STATE_SET
1121 * @see #SELECTED_STATE_SET
1122 */
1123 protected static final int[] PRESSED_SELECTED_STATE_SET =
1124 stateSetUnion(PRESSED_STATE_SET, SELECTED_STATE_SET);
1125
1126 /**
1127 * Indicates the view is pressed, selected and its window has the focus.
1128 *
1129 * @see #PRESSED_STATE_SET
1130 * @see #SELECTED_STATE_SET
1131 * @see #WINDOW_FOCUSED_STATE_SET
1132 */
1133 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1134 stateSetUnion(PRESSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1135
1136 /**
1137 * Indicates the view is pressed and focused.
1138 *
1139 * @see #PRESSED_STATE_SET
1140 * @see #FOCUSED_STATE_SET
1141 */
1142 protected static final int[] PRESSED_FOCUSED_STATE_SET =
1143 stateSetUnion(PRESSED_STATE_SET, FOCUSED_STATE_SET);
1144
1145 /**
1146 * Indicates the view is pressed, focused and its window has the focus.
1147 *
1148 * @see #PRESSED_STATE_SET
1149 * @see #FOCUSED_STATE_SET
1150 * @see #WINDOW_FOCUSED_STATE_SET
1151 */
1152 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1153 stateSetUnion(PRESSED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1154
1155 /**
1156 * Indicates the view is pressed, focused and selected.
1157 *
1158 * @see #PRESSED_STATE_SET
1159 * @see #SELECTED_STATE_SET
1160 * @see #FOCUSED_STATE_SET
1161 */
1162 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET =
1163 stateSetUnion(PRESSED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1164
1165 /**
1166 * Indicates the view is pressed, focused, selected and its window has the focus.
1167 *
1168 * @see #PRESSED_STATE_SET
1169 * @see #FOCUSED_STATE_SET
1170 * @see #SELECTED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
1173 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1174 stateSetUnion(PRESSED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1175
1176 /**
1177 * Indicates the view is pressed and enabled.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #ENABLED_STATE_SET
1181 */
1182 protected static final int[] PRESSED_ENABLED_STATE_SET =
1183 stateSetUnion(PRESSED_STATE_SET, ENABLED_STATE_SET);
1184
1185 /**
1186 * Indicates the view is pressed, enabled and its window has the focus.
1187 *
1188 * @see #PRESSED_STATE_SET
1189 * @see #ENABLED_STATE_SET
1190 * @see #WINDOW_FOCUSED_STATE_SET
1191 */
1192 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET =
1193 stateSetUnion(PRESSED_ENABLED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1194
1195 /**
1196 * Indicates the view is pressed, enabled and selected.
1197 *
1198 * @see #PRESSED_STATE_SET
1199 * @see #ENABLED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 */
1202 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET =
1203 stateSetUnion(PRESSED_ENABLED_STATE_SET, SELECTED_STATE_SET);
1204
1205 /**
1206 * Indicates the view is pressed, enabled, selected and its window has the
1207 * focus.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #ENABLED_STATE_SET
1211 * @see #SELECTED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
1214 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1215 stateSetUnion(PRESSED_ENABLED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1216
1217 /**
1218 * Indicates the view is pressed, enabled and focused.
1219 *
1220 * @see #PRESSED_STATE_SET
1221 * @see #ENABLED_STATE_SET
1222 * @see #FOCUSED_STATE_SET
1223 */
1224 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET =
1225 stateSetUnion(PRESSED_ENABLED_STATE_SET, FOCUSED_STATE_SET);
1226
1227 /**
1228 * Indicates the view is pressed, enabled, focused and its window has the
1229 * focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #ENABLED_STATE_SET
1233 * @see #FOCUSED_STATE_SET
1234 * @see #WINDOW_FOCUSED_STATE_SET
1235 */
1236 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET =
1237 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1238
1239 /**
1240 * Indicates the view is pressed, enabled, focused and selected.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #ENABLED_STATE_SET
1244 * @see #SELECTED_STATE_SET
1245 * @see #FOCUSED_STATE_SET
1246 */
1247 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET =
1248 stateSetUnion(PRESSED_ENABLED_FOCUSED_STATE_SET, SELECTED_STATE_SET);
1249
1250 /**
1251 * Indicates the view is pressed, enabled, focused, selected and its window
1252 * has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #ENABLED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
1260 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET =
1261 stateSetUnion(PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, WINDOW_FOCUSED_STATE_SET);
1262
1263 /**
1264 * The order here is very important to {@link #getDrawableState()}
1265 */
1266 private static final int[][] VIEW_STATE_SETS = {
1267 EMPTY_STATE_SET, // 0 0 0 0 0
1268 WINDOW_FOCUSED_STATE_SET, // 0 0 0 0 1
1269 SELECTED_STATE_SET, // 0 0 0 1 0
1270 SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 0 1 1
1271 FOCUSED_STATE_SET, // 0 0 1 0 0
1272 FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 0 1
1273 FOCUSED_SELECTED_STATE_SET, // 0 0 1 1 0
1274 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 1 1
1275 ENABLED_STATE_SET, // 0 1 0 0 0
1276 ENABLED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 0 1
1277 ENABLED_SELECTED_STATE_SET, // 0 1 0 1 0
1278 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 1 1
1279 ENABLED_FOCUSED_STATE_SET, // 0 1 1 0 0
1280 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 0 1
1281 ENABLED_FOCUSED_SELECTED_STATE_SET, // 0 1 1 1 0
1282 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 1 1
1283 PRESSED_STATE_SET, // 1 0 0 0 0
1284 PRESSED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 0 1
1285 PRESSED_SELECTED_STATE_SET, // 1 0 0 1 0
1286 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 0 1 1
1287 PRESSED_FOCUSED_STATE_SET, // 1 0 1 0 0
1288 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 0 1
1289 PRESSED_FOCUSED_SELECTED_STATE_SET, // 1 0 1 1 0
1290 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 0 1 1 1
1291 PRESSED_ENABLED_STATE_SET, // 1 1 0 0 0
1292 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 0 1
1293 PRESSED_ENABLED_SELECTED_STATE_SET, // 1 1 0 1 0
1294 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 0 1 1
1295 PRESSED_ENABLED_FOCUSED_STATE_SET, // 1 1 1 0 0
1296 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 0 1
1297 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET, // 1 1 1 1 0
1298 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 1 1 1 1 1
1299 };
1300
1301 /**
1302 * Used by views that contain lists of items. This state indicates that
1303 * the view is showing the last item.
1304 * @hide
1305 */
1306 protected static final int[] LAST_STATE_SET = {R.attr.state_last};
1307 /**
1308 * Used by views that contain lists of items. This state indicates that
1309 * the view is showing the first item.
1310 * @hide
1311 */
1312 protected static final int[] FIRST_STATE_SET = {R.attr.state_first};
1313 /**
1314 * Used by views that contain lists of items. This state indicates that
1315 * the view is showing the middle item.
1316 * @hide
1317 */
1318 protected static final int[] MIDDLE_STATE_SET = {R.attr.state_middle};
1319 /**
1320 * Used by views that contain lists of items. This state indicates that
1321 * the view is showing only one item.
1322 * @hide
1323 */
1324 protected static final int[] SINGLE_STATE_SET = {R.attr.state_single};
1325 /**
1326 * Used by views that contain lists of items. This state indicates that
1327 * the view is pressed and showing the last item.
1328 * @hide
1329 */
1330 protected static final int[] PRESSED_LAST_STATE_SET = {R.attr.state_last, R.attr.state_pressed};
1331 /**
1332 * Used by views that contain lists of items. This state indicates that
1333 * the view is pressed and showing the first item.
1334 * @hide
1335 */
1336 protected static final int[] PRESSED_FIRST_STATE_SET = {R.attr.state_first, R.attr.state_pressed};
1337 /**
1338 * Used by views that contain lists of items. This state indicates that
1339 * the view is pressed and showing the middle item.
1340 * @hide
1341 */
1342 protected static final int[] PRESSED_MIDDLE_STATE_SET = {R.attr.state_middle, R.attr.state_pressed};
1343 /**
1344 * Used by views that contain lists of items. This state indicates that
1345 * the view is pressed and showing only one item.
1346 * @hide
1347 */
1348 protected static final int[] PRESSED_SINGLE_STATE_SET = {R.attr.state_single, R.attr.state_pressed};
1349
1350 /**
1351 * Temporary Rect currently for use in setBackground(). This will probably
1352 * be extended in the future to hold our own class with more than just
1353 * a Rect. :)
1354 */
1355 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001356
1357 /**
1358 * Map used to store views' tags.
1359 */
1360 private static WeakHashMap<View, SparseArray<Object>> sTags;
1361
1362 /**
1363 * Lock used to access sTags.
1364 */
1365 private static final Object sTagsLock = new Object();
1366
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 /**
1368 * The animation currently associated with this view.
1369 * @hide
1370 */
1371 protected Animation mCurrentAnimation = null;
1372
1373 /**
1374 * Width as measured during measure pass.
1375 * {@hide}
1376 */
1377 @ViewDebug.ExportedProperty
1378 protected int mMeasuredWidth;
1379
1380 /**
1381 * Height as measured during measure pass.
1382 * {@hide}
1383 */
1384 @ViewDebug.ExportedProperty
1385 protected int mMeasuredHeight;
1386
1387 /**
1388 * The view's identifier.
1389 * {@hide}
1390 *
1391 * @see #setId(int)
1392 * @see #getId()
1393 */
1394 @ViewDebug.ExportedProperty(resolveId = true)
1395 int mID = NO_ID;
1396
1397 /**
1398 * The view's tag.
1399 * {@hide}
1400 *
1401 * @see #setTag(Object)
1402 * @see #getTag()
1403 */
1404 protected Object mTag;
1405
1406 // for mPrivateFlags:
1407 /** {@hide} */
1408 static final int WANTS_FOCUS = 0x00000001;
1409 /** {@hide} */
1410 static final int FOCUSED = 0x00000002;
1411 /** {@hide} */
1412 static final int SELECTED = 0x00000004;
1413 /** {@hide} */
1414 static final int IS_ROOT_NAMESPACE = 0x00000008;
1415 /** {@hide} */
1416 static final int HAS_BOUNDS = 0x00000010;
1417 /** {@hide} */
1418 static final int DRAWN = 0x00000020;
1419 /**
1420 * When this flag is set, this view is running an animation on behalf of its
1421 * children and should therefore not cancel invalidate requests, even if they
1422 * lie outside of this view's bounds.
1423 *
1424 * {@hide}
1425 */
1426 static final int DRAW_ANIMATION = 0x00000040;
1427 /** {@hide} */
1428 static final int SKIP_DRAW = 0x00000080;
1429 /** {@hide} */
1430 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1431 /** {@hide} */
1432 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1433 /** {@hide} */
1434 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1435 /** {@hide} */
1436 static final int MEASURED_DIMENSION_SET = 0x00000800;
1437 /** {@hide} */
1438 static final int FORCE_LAYOUT = 0x00001000;
1439
1440 private static final int LAYOUT_REQUIRED = 0x00002000;
1441
1442 private static final int PRESSED = 0x00004000;
1443
1444 /** {@hide} */
1445 static final int DRAWING_CACHE_VALID = 0x00008000;
1446 /**
1447 * Flag used to indicate that this view should be drawn once more (and only once
1448 * more) after its animation has completed.
1449 * {@hide}
1450 */
1451 static final int ANIMATION_STARTED = 0x00010000;
1452
1453 private static final int SAVE_STATE_CALLED = 0x00020000;
1454
1455 /**
1456 * Indicates that the View returned true when onSetAlpha() was called and that
1457 * the alpha must be restored.
1458 * {@hide}
1459 */
1460 static final int ALPHA_SET = 0x00040000;
1461
1462 /**
1463 * Set by {@link #setScrollContainer(boolean)}.
1464 */
1465 static final int SCROLL_CONTAINER = 0x00080000;
1466
1467 /**
1468 * Set by {@link #setScrollContainer(boolean)}.
1469 */
1470 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1471
1472 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001473 * View flag indicating whether this view was invalidated (fully or partially.)
1474 *
1475 * @hide
1476 */
1477 static final int DIRTY = 0x00200000;
1478
1479 /**
1480 * View flag indicating whether this view was invalidated by an opaque
1481 * invalidate request.
1482 *
1483 * @hide
1484 */
1485 static final int DIRTY_OPAQUE = 0x00400000;
1486
1487 /**
1488 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1489 *
1490 * @hide
1491 */
1492 static final int DIRTY_MASK = 0x00600000;
1493
1494 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001495 * Indicates whether the background is opaque.
1496 *
1497 * @hide
1498 */
1499 static final int OPAQUE_BACKGROUND = 0x00800000;
1500
1501 /**
1502 * Indicates whether the scrollbars are opaque.
1503 *
1504 * @hide
1505 */
1506 static final int OPAQUE_SCROLLBARS = 0x01000000;
1507
1508 /**
1509 * Indicates whether the view is opaque.
1510 *
1511 * @hide
1512 */
1513 static final int OPAQUE_MASK = 0x01800000;
Adam Powelle14579b2009-12-16 18:39:52 -08001514
1515 /**
1516 * Indicates a prepressed state;
1517 * the short time between ACTION_DOWN and recognizing
1518 * a 'real' press. Prepressed is used to recognize quick taps
1519 * even when they are shorter than ViewConfiguration.getTapTimeout().
1520 *
1521 * @hide
1522 */
1523 private static final int PREPRESSED = 0x02000000;
Adam Powellc9fbaab2010-02-16 17:16:19 -08001524
1525 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001526 * Indicates whether the view is temporarily detached.
1527 *
1528 * @hide
1529 */
1530 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Adam Powell8568c3a2010-04-19 14:26:11 -07001531
1532 /**
1533 * Indicates that we should awaken scroll bars once attached
1534 *
1535 * @hide
1536 */
1537 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The parent this view is attached to.
1541 * {@hide}
1542 *
1543 * @see #getParent()
1544 */
1545 protected ViewParent mParent;
1546
1547 /**
1548 * {@hide}
1549 */
1550 AttachInfo mAttachInfo;
1551
1552 /**
1553 * {@hide}
1554 */
Romain Guy809a7f62009-05-14 15:44:42 -07001555 @ViewDebug.ExportedProperty(flagMapping = {
1556 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1557 name = "FORCE_LAYOUT"),
1558 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1559 name = "LAYOUT_REQUIRED"),
1560 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001561 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001562 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1563 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1564 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1565 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1566 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001567 int mPrivateFlags;
1568
1569 /**
1570 * Count of how many windows this view has been attached to.
1571 */
1572 int mWindowAttachCount;
1573
1574 /**
1575 * The layout parameters associated with this view and used by the parent
1576 * {@link android.view.ViewGroup} to determine how this view should be
1577 * laid out.
1578 * {@hide}
1579 */
1580 protected ViewGroup.LayoutParams mLayoutParams;
1581
1582 /**
1583 * The view flags hold various views states.
1584 * {@hide}
1585 */
1586 @ViewDebug.ExportedProperty
1587 int mViewFlags;
1588
1589 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001590 * The transform matrix for the View. This transform is calculated internally
1591 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1592 * is used by default. Do *not* use this variable directly; instead call
1593 * getMatrix(), which will automatically recalculate the matrix if necessary
1594 * to get the correct matrix based on the latest rotation and scale properties.
1595 */
1596 private final Matrix mMatrix = new Matrix();
1597
1598 /**
1599 * The transform matrix for the View. This transform is calculated internally
1600 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1601 * is used by default. Do *not* use this variable directly; instead call
1602 * getMatrix(), which will automatically recalculate the matrix if necessary
1603 * to get the correct matrix based on the latest rotation and scale properties.
1604 */
1605 private Matrix mInverseMatrix;
1606
1607 /**
1608 * An internal variable that tracks whether we need to recalculate the
1609 * transform matrix, based on whether the rotation or scaleX/Y properties
1610 * have changed since the matrix was last calculated.
1611 */
1612 private boolean mMatrixDirty = false;
1613
1614 /**
1615 * An internal variable that tracks whether we need to recalculate the
1616 * transform matrix, based on whether the rotation or scaleX/Y properties
1617 * have changed since the matrix was last calculated.
1618 */
1619 private boolean mInverseMatrixDirty = true;
1620
1621 /**
1622 * A variable that tracks whether we need to recalculate the
1623 * transform matrix, based on whether the rotation or scaleX/Y properties
1624 * have changed since the matrix was last calculated. This variable
1625 * is only valid after a call to getMatrix().
1626 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001627 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001628
1629 /**
1630 * The degrees rotation around the pivot point.
1631 */
1632 @ViewDebug.ExportedProperty
1633 private float mRotation = 0f;
1634
1635 /**
1636 * The amount of scale in the x direction around the pivot point. A
1637 * value of 1 means no scaling is applied.
1638 */
1639 @ViewDebug.ExportedProperty
1640 private float mScaleX = 1f;
1641
1642 /**
1643 * The amount of scale in the y direction around the pivot point. A
1644 * value of 1 means no scaling is applied.
1645 */
1646 @ViewDebug.ExportedProperty
1647 private float mScaleY = 1f;
1648
1649 /**
1650 * The amount of scale in the x direction around the pivot point. A
1651 * value of 1 means no scaling is applied.
1652 */
1653 @ViewDebug.ExportedProperty
1654 private float mPivotX = 0f;
1655
1656 /**
1657 * The amount of scale in the y direction around the pivot point. A
1658 * value of 1 means no scaling is applied.
1659 */
1660 @ViewDebug.ExportedProperty
1661 private float mPivotY = 0f;
1662
1663 /**
1664 * The opacity of the View. This is a value from 0 to 1, where 0 means
1665 * completely transparent and 1 means completely opaque.
1666 */
1667 @ViewDebug.ExportedProperty
1668 private float mAlpha = 1f;
1669
1670 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 * The distance in pixels from the left edge of this view's parent
1672 * to the left edge of this view.
1673 * {@hide}
1674 */
1675 @ViewDebug.ExportedProperty
1676 protected int mLeft;
1677 /**
1678 * The distance in pixels from the left edge of this view's parent
1679 * to the right edge of this view.
1680 * {@hide}
1681 */
1682 @ViewDebug.ExportedProperty
1683 protected int mRight;
1684 /**
1685 * The distance in pixels from the top edge of this view's parent
1686 * to the top edge of this view.
1687 * {@hide}
1688 */
1689 @ViewDebug.ExportedProperty
1690 protected int mTop;
1691 /**
1692 * The distance in pixels from the top edge of this view's parent
1693 * to the bottom edge of this view.
1694 * {@hide}
1695 */
1696 @ViewDebug.ExportedProperty
1697 protected int mBottom;
1698
1699 /**
1700 * The offset, in pixels, by which the content of this view is scrolled
1701 * horizontally.
1702 * {@hide}
1703 */
1704 @ViewDebug.ExportedProperty
1705 protected int mScrollX;
1706 /**
1707 * The offset, in pixels, by which the content of this view is scrolled
1708 * vertically.
1709 * {@hide}
1710 */
1711 @ViewDebug.ExportedProperty
1712 protected int mScrollY;
1713
1714 /**
1715 * The left padding in pixels, that is the distance in pixels between the
1716 * left edge of this view and the left edge of its content.
1717 * {@hide}
1718 */
1719 @ViewDebug.ExportedProperty
1720 protected int mPaddingLeft;
1721 /**
1722 * The right padding in pixels, that is the distance in pixels between the
1723 * right edge of this view and the right edge of its content.
1724 * {@hide}
1725 */
1726 @ViewDebug.ExportedProperty
1727 protected int mPaddingRight;
1728 /**
1729 * The top padding in pixels, that is the distance in pixels between the
1730 * top edge of this view and the top edge of its content.
1731 * {@hide}
1732 */
1733 @ViewDebug.ExportedProperty
1734 protected int mPaddingTop;
1735 /**
1736 * The bottom padding in pixels, that is the distance in pixels between the
1737 * bottom edge of this view and the bottom edge of its content.
1738 * {@hide}
1739 */
1740 @ViewDebug.ExportedProperty
1741 protected int mPaddingBottom;
1742
1743 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001744 * Briefly describes the view and is primarily used for accessibility support.
1745 */
1746 private CharSequence mContentDescription;
1747
1748 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 * Cache the paddingRight set by the user to append to the scrollbar's size.
1750 */
1751 @ViewDebug.ExportedProperty
1752 int mUserPaddingRight;
1753
1754 /**
1755 * Cache the paddingBottom set by the user to append to the scrollbar's size.
1756 */
1757 @ViewDebug.ExportedProperty
1758 int mUserPaddingBottom;
1759
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07001760 /**
1761 * @hide
1762 */
1763 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
1764 /**
1765 * @hide
1766 */
1767 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768
1769 private Resources mResources = null;
1770
1771 private Drawable mBGDrawable;
1772
1773 private int mBackgroundResource;
1774 private boolean mBackgroundSizeChanged;
1775
1776 /**
1777 * Listener used to dispatch focus change events.
1778 * This field should be made private, so it is hidden from the SDK.
1779 * {@hide}
1780 */
1781 protected OnFocusChangeListener mOnFocusChangeListener;
1782
1783 /**
1784 * Listener used to dispatch click events.
1785 * This field should be made private, so it is hidden from the SDK.
1786 * {@hide}
1787 */
1788 protected OnClickListener mOnClickListener;
1789
1790 /**
1791 * Listener used to dispatch long click events.
1792 * This field should be made private, so it is hidden from the SDK.
1793 * {@hide}
1794 */
1795 protected OnLongClickListener mOnLongClickListener;
1796
1797 /**
1798 * Listener used to build the context menu.
1799 * This field should be made private, so it is hidden from the SDK.
1800 * {@hide}
1801 */
1802 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
1803
1804 private OnKeyListener mOnKeyListener;
1805
1806 private OnTouchListener mOnTouchListener;
1807
1808 /**
1809 * The application environment this view lives in.
1810 * This field should be made private, so it is hidden from the SDK.
1811 * {@hide}
1812 */
1813 protected Context mContext;
1814
1815 private ScrollabilityCache mScrollCache;
1816
1817 private int[] mDrawableState = null;
1818
1819 private SoftReference<Bitmap> mDrawingCache;
Romain Guyfbd8f692009-06-26 14:51:58 -07001820 private SoftReference<Bitmap> mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821
1822 /**
1823 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
1824 * the user may specify which view to go to next.
1825 */
1826 private int mNextFocusLeftId = View.NO_ID;
1827
1828 /**
1829 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
1830 * the user may specify which view to go to next.
1831 */
1832 private int mNextFocusRightId = View.NO_ID;
1833
1834 /**
1835 * When this view has focus and the next focus is {@link #FOCUS_UP},
1836 * the user may specify which view to go to next.
1837 */
1838 private int mNextFocusUpId = View.NO_ID;
1839
1840 /**
1841 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
1842 * the user may specify which view to go to next.
1843 */
1844 private int mNextFocusDownId = View.NO_ID;
1845
1846 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08001847 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08001848 private PerformClick mPerformClick;
Adam Powelle14579b2009-12-16 18:39:52 -08001849
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001850 private UnsetPressedState mUnsetPressedState;
1851
1852 /**
1853 * Whether the long press's action has been invoked. The tap's action is invoked on the
1854 * up event while a long press is invoked as soon as the long press duration is reached, so
1855 * a long press could be performed before the tap is checked, in which case the tap's action
1856 * should not be invoked.
1857 */
1858 private boolean mHasPerformedLongPress;
1859
1860 /**
1861 * The minimum height of the view. We'll try our best to have the height
1862 * of this view to at least this amount.
1863 */
1864 @ViewDebug.ExportedProperty
1865 private int mMinHeight;
1866
1867 /**
1868 * The minimum width of the view. We'll try our best to have the width
1869 * of this view to at least this amount.
1870 */
1871 @ViewDebug.ExportedProperty
1872 private int mMinWidth;
1873
1874 /**
1875 * The delegate to handle touch events that are physically in this view
1876 * but should be handled by another view.
1877 */
1878 private TouchDelegate mTouchDelegate = null;
1879
1880 /**
1881 * Solid color to use as a background when creating the drawing cache. Enables
1882 * the cache to use 16 bit bitmaps instead of 32 bit.
1883 */
1884 private int mDrawingCacheBackgroundColor = 0;
1885
1886 /**
1887 * Special tree observer used when mAttachInfo is null.
1888 */
1889 private ViewTreeObserver mFloatingTreeObserver;
Adam Powelle14579b2009-12-16 18:39:52 -08001890
1891 /**
1892 * Cache the touch slop from the context that created the view.
1893 */
1894 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895
1896 // Used for debug only
1897 static long sInstanceCount = 0;
1898
1899 /**
1900 * Simple constructor to use when creating a view from code.
1901 *
1902 * @param context The Context the view is running in, through which it can
1903 * access the current theme, resources, etc.
1904 */
1905 public View(Context context) {
1906 mContext = context;
1907 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07001908 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Carl Shapiro82fe5642010-02-24 00:14:23 -08001909 // Used for debug only
1910 //++sInstanceCount;
Adam Powelle14579b2009-12-16 18:39:52 -08001911 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 }
1913
1914 /**
1915 * Constructor that is called when inflating a view from XML. This is called
1916 * when a view is being constructed from an XML file, supplying attributes
1917 * that were specified in the XML file. This version uses a default style of
1918 * 0, so the only attribute values applied are those in the Context's Theme
1919 * and the given AttributeSet.
1920 *
1921 * <p>
1922 * The method onFinishInflate() will be called after all children have been
1923 * added.
1924 *
1925 * @param context The Context the view is running in, through which it can
1926 * access the current theme, resources, etc.
1927 * @param attrs The attributes of the XML tag that is inflating the view.
1928 * @see #View(Context, AttributeSet, int)
1929 */
1930 public View(Context context, AttributeSet attrs) {
1931 this(context, attrs, 0);
1932 }
1933
1934 /**
1935 * Perform inflation from XML and apply a class-specific base style. This
1936 * constructor of View allows subclasses to use their own base style when
1937 * they are inflating. For example, a Button class's constructor would call
1938 * this version of the super class constructor and supply
1939 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
1940 * the theme's button style to modify all of the base view attributes (in
1941 * particular its background) as well as the Button class's attributes.
1942 *
1943 * @param context The Context the view is running in, through which it can
1944 * access the current theme, resources, etc.
1945 * @param attrs The attributes of the XML tag that is inflating the view.
1946 * @param defStyle The default style to apply to this view. If 0, no style
1947 * will be applied (beyond what is included in the theme). This may
1948 * either be an attribute resource, whose value will be retrieved
1949 * from the current theme, or an explicit style resource.
1950 * @see #View(Context, AttributeSet)
1951 */
1952 public View(Context context, AttributeSet attrs, int defStyle) {
1953 this(context);
1954
1955 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
1956 defStyle, 0);
1957
1958 Drawable background = null;
1959
1960 int leftPadding = -1;
1961 int topPadding = -1;
1962 int rightPadding = -1;
1963 int bottomPadding = -1;
1964
1965 int padding = -1;
1966
1967 int viewFlagValues = 0;
1968 int viewFlagMasks = 0;
1969
1970 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07001971
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 int x = 0;
1973 int y = 0;
1974
1975 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
1976
1977 final int N = a.getIndexCount();
1978 for (int i = 0; i < N; i++) {
1979 int attr = a.getIndex(i);
1980 switch (attr) {
1981 case com.android.internal.R.styleable.View_background:
1982 background = a.getDrawable(attr);
1983 break;
1984 case com.android.internal.R.styleable.View_padding:
1985 padding = a.getDimensionPixelSize(attr, -1);
1986 break;
1987 case com.android.internal.R.styleable.View_paddingLeft:
1988 leftPadding = a.getDimensionPixelSize(attr, -1);
1989 break;
1990 case com.android.internal.R.styleable.View_paddingTop:
1991 topPadding = a.getDimensionPixelSize(attr, -1);
1992 break;
1993 case com.android.internal.R.styleable.View_paddingRight:
1994 rightPadding = a.getDimensionPixelSize(attr, -1);
1995 break;
1996 case com.android.internal.R.styleable.View_paddingBottom:
1997 bottomPadding = a.getDimensionPixelSize(attr, -1);
1998 break;
1999 case com.android.internal.R.styleable.View_scrollX:
2000 x = a.getDimensionPixelOffset(attr, 0);
2001 break;
2002 case com.android.internal.R.styleable.View_scrollY:
2003 y = a.getDimensionPixelOffset(attr, 0);
2004 break;
2005 case com.android.internal.R.styleable.View_id:
2006 mID = a.getResourceId(attr, NO_ID);
2007 break;
2008 case com.android.internal.R.styleable.View_tag:
2009 mTag = a.getText(attr);
2010 break;
2011 case com.android.internal.R.styleable.View_fitsSystemWindows:
2012 if (a.getBoolean(attr, false)) {
2013 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2014 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2015 }
2016 break;
2017 case com.android.internal.R.styleable.View_focusable:
2018 if (a.getBoolean(attr, false)) {
2019 viewFlagValues |= FOCUSABLE;
2020 viewFlagMasks |= FOCUSABLE_MASK;
2021 }
2022 break;
2023 case com.android.internal.R.styleable.View_focusableInTouchMode:
2024 if (a.getBoolean(attr, false)) {
2025 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2026 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2027 }
2028 break;
2029 case com.android.internal.R.styleable.View_clickable:
2030 if (a.getBoolean(attr, false)) {
2031 viewFlagValues |= CLICKABLE;
2032 viewFlagMasks |= CLICKABLE;
2033 }
2034 break;
2035 case com.android.internal.R.styleable.View_longClickable:
2036 if (a.getBoolean(attr, false)) {
2037 viewFlagValues |= LONG_CLICKABLE;
2038 viewFlagMasks |= LONG_CLICKABLE;
2039 }
2040 break;
2041 case com.android.internal.R.styleable.View_saveEnabled:
2042 if (!a.getBoolean(attr, true)) {
2043 viewFlagValues |= SAVE_DISABLED;
2044 viewFlagMasks |= SAVE_DISABLED_MASK;
2045 }
2046 break;
2047 case com.android.internal.R.styleable.View_duplicateParentState:
2048 if (a.getBoolean(attr, false)) {
2049 viewFlagValues |= DUPLICATE_PARENT_STATE;
2050 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2051 }
2052 break;
2053 case com.android.internal.R.styleable.View_visibility:
2054 final int visibility = a.getInt(attr, 0);
2055 if (visibility != 0) {
2056 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2057 viewFlagMasks |= VISIBILITY_MASK;
2058 }
2059 break;
2060 case com.android.internal.R.styleable.View_drawingCacheQuality:
2061 final int cacheQuality = a.getInt(attr, 0);
2062 if (cacheQuality != 0) {
2063 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2064 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2065 }
2066 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002067 case com.android.internal.R.styleable.View_contentDescription:
2068 mContentDescription = a.getString(attr);
2069 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2071 if (!a.getBoolean(attr, true)) {
2072 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2073 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2074 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002075 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2077 if (!a.getBoolean(attr, true)) {
2078 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2079 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2080 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002081 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 case R.styleable.View_scrollbars:
2083 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2084 if (scrollbars != SCROLLBARS_NONE) {
2085 viewFlagValues |= scrollbars;
2086 viewFlagMasks |= SCROLLBARS_MASK;
2087 initializeScrollbars(a);
2088 }
2089 break;
2090 case R.styleable.View_fadingEdge:
2091 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2092 if (fadingEdge != FADING_EDGE_NONE) {
2093 viewFlagValues |= fadingEdge;
2094 viewFlagMasks |= FADING_EDGE_MASK;
2095 initializeFadingEdge(a);
2096 }
2097 break;
2098 case R.styleable.View_scrollbarStyle:
2099 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2100 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2101 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2102 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2103 }
2104 break;
2105 case R.styleable.View_isScrollContainer:
2106 setScrollContainer = true;
2107 if (a.getBoolean(attr, false)) {
2108 setScrollContainer(true);
2109 }
2110 break;
2111 case com.android.internal.R.styleable.View_keepScreenOn:
2112 if (a.getBoolean(attr, false)) {
2113 viewFlagValues |= KEEP_SCREEN_ON;
2114 viewFlagMasks |= KEEP_SCREEN_ON;
2115 }
2116 break;
2117 case R.styleable.View_nextFocusLeft:
2118 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2119 break;
2120 case R.styleable.View_nextFocusRight:
2121 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2122 break;
2123 case R.styleable.View_nextFocusUp:
2124 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2125 break;
2126 case R.styleable.View_nextFocusDown:
2127 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2128 break;
2129 case R.styleable.View_minWidth:
2130 mMinWidth = a.getDimensionPixelSize(attr, 0);
2131 break;
2132 case R.styleable.View_minHeight:
2133 mMinHeight = a.getDimensionPixelSize(attr, 0);
2134 break;
Romain Guy9a817362009-05-01 10:57:14 -07002135 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002136 if (context.isRestricted()) {
2137 throw new IllegalStateException("The android:onClick attribute cannot "
2138 + "be used within a restricted context");
2139 }
2140
Romain Guy9a817362009-05-01 10:57:14 -07002141 final String handlerName = a.getString(attr);
2142 if (handlerName != null) {
2143 setOnClickListener(new OnClickListener() {
2144 private Method mHandler;
2145
2146 public void onClick(View v) {
2147 if (mHandler == null) {
2148 try {
2149 mHandler = getContext().getClass().getMethod(handlerName,
2150 View.class);
2151 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002152 int id = getId();
2153 String idText = id == NO_ID ? "" : " with id '"
2154 + getContext().getResources().getResourceEntryName(
2155 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002156 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002157 handlerName + "(View) in the activity "
2158 + getContext().getClass() + " for onClick handler"
2159 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002160 }
2161 }
2162
2163 try {
2164 mHandler.invoke(getContext(), View.this);
2165 } catch (IllegalAccessException e) {
2166 throw new IllegalStateException("Could not execute non "
2167 + "public method of the activity", e);
2168 } catch (InvocationTargetException e) {
2169 throw new IllegalStateException("Could not execute "
2170 + "method of the activity", e);
2171 }
2172 }
2173 });
2174 }
2175 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 }
2177 }
2178
2179 if (background != null) {
2180 setBackgroundDrawable(background);
2181 }
2182
2183 if (padding >= 0) {
2184 leftPadding = padding;
2185 topPadding = padding;
2186 rightPadding = padding;
2187 bottomPadding = padding;
2188 }
2189
2190 // If the user specified the padding (either with android:padding or
2191 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2192 // use the default padding or the padding from the background drawable
2193 // (stored at this point in mPadding*)
2194 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2195 topPadding >= 0 ? topPadding : mPaddingTop,
2196 rightPadding >= 0 ? rightPadding : mPaddingRight,
2197 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2198
2199 if (viewFlagMasks != 0) {
2200 setFlags(viewFlagValues, viewFlagMasks);
2201 }
2202
2203 // Needs to be called after mViewFlags is set
2204 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2205 recomputePadding();
2206 }
2207
2208 if (x != 0 || y != 0) {
2209 scrollTo(x, y);
2210 }
2211
2212 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2213 setScrollContainer(true);
2214 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002215
2216 computeOpaqueFlags();
2217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 a.recycle();
2219 }
2220
2221 /**
2222 * Non-public constructor for use in testing
2223 */
2224 View() {
2225 }
2226
Carl Shapiro82fe5642010-02-24 00:14:23 -08002227 // Used for debug only
2228 /*
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 @Override
2230 protected void finalize() throws Throwable {
2231 super.finalize();
2232 --sInstanceCount;
2233 }
Carl Shapiro82fe5642010-02-24 00:14:23 -08002234 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002235
2236 /**
2237 * <p>
2238 * Initializes the fading edges from a given set of styled attributes. This
2239 * method should be called by subclasses that need fading edges and when an
2240 * instance of these subclasses is created programmatically rather than
2241 * being inflated from XML. This method is automatically called when the XML
2242 * is inflated.
2243 * </p>
2244 *
2245 * @param a the styled attributes set to initialize the fading edges from
2246 */
2247 protected void initializeFadingEdge(TypedArray a) {
2248 initScrollCache();
2249
2250 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2251 R.styleable.View_fadingEdgeLength,
2252 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2253 }
2254
2255 /**
2256 * Returns the size of the vertical faded edges used to indicate that more
2257 * content in this view is visible.
2258 *
2259 * @return The size in pixels of the vertical faded edge or 0 if vertical
2260 * faded edges are not enabled for this view.
2261 * @attr ref android.R.styleable#View_fadingEdgeLength
2262 */
2263 public int getVerticalFadingEdgeLength() {
2264 if (isVerticalFadingEdgeEnabled()) {
2265 ScrollabilityCache cache = mScrollCache;
2266 if (cache != null) {
2267 return cache.fadingEdgeLength;
2268 }
2269 }
2270 return 0;
2271 }
2272
2273 /**
2274 * Set the size of the faded edge used to indicate that more content in this
2275 * view is available. Will not change whether the fading edge is enabled; use
2276 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2277 * to enable the fading edge for the vertical or horizontal fading edges.
2278 *
2279 * @param length The size in pixels of the faded edge used to indicate that more
2280 * content in this view is visible.
2281 */
2282 public void setFadingEdgeLength(int length) {
2283 initScrollCache();
2284 mScrollCache.fadingEdgeLength = length;
2285 }
2286
2287 /**
2288 * Returns the size of the horizontal faded edges used to indicate that more
2289 * content in this view is visible.
2290 *
2291 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2292 * faded edges are not enabled for this view.
2293 * @attr ref android.R.styleable#View_fadingEdgeLength
2294 */
2295 public int getHorizontalFadingEdgeLength() {
2296 if (isHorizontalFadingEdgeEnabled()) {
2297 ScrollabilityCache cache = mScrollCache;
2298 if (cache != null) {
2299 return cache.fadingEdgeLength;
2300 }
2301 }
2302 return 0;
2303 }
2304
2305 /**
2306 * Returns the width of the vertical scrollbar.
2307 *
2308 * @return The width in pixels of the vertical scrollbar or 0 if there
2309 * is no vertical scrollbar.
2310 */
2311 public int getVerticalScrollbarWidth() {
2312 ScrollabilityCache cache = mScrollCache;
2313 if (cache != null) {
2314 ScrollBarDrawable scrollBar = cache.scrollBar;
2315 if (scrollBar != null) {
2316 int size = scrollBar.getSize(true);
2317 if (size <= 0) {
2318 size = cache.scrollBarSize;
2319 }
2320 return size;
2321 }
2322 return 0;
2323 }
2324 return 0;
2325 }
2326
2327 /**
2328 * Returns the height of the horizontal scrollbar.
2329 *
2330 * @return The height in pixels of the horizontal scrollbar or 0 if
2331 * there is no horizontal scrollbar.
2332 */
2333 protected int getHorizontalScrollbarHeight() {
2334 ScrollabilityCache cache = mScrollCache;
2335 if (cache != null) {
2336 ScrollBarDrawable scrollBar = cache.scrollBar;
2337 if (scrollBar != null) {
2338 int size = scrollBar.getSize(false);
2339 if (size <= 0) {
2340 size = cache.scrollBarSize;
2341 }
2342 return size;
2343 }
2344 return 0;
2345 }
2346 return 0;
2347 }
2348
2349 /**
2350 * <p>
2351 * Initializes the scrollbars from a given set of styled attributes. This
2352 * method should be called by subclasses that need scrollbars and when an
2353 * instance of these subclasses is created programmatically rather than
2354 * being inflated from XML. This method is automatically called when the XML
2355 * is inflated.
2356 * </p>
2357 *
2358 * @param a the styled attributes set to initialize the scrollbars from
2359 */
2360 protected void initializeScrollbars(TypedArray a) {
2361 initScrollCache();
2362
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002363 final ScrollabilityCache scrollabilityCache = mScrollCache;
Mike Cleronf116bf82009-09-27 19:14:12 -07002364
2365 if (scrollabilityCache.scrollBar == null) {
2366 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2367 }
2368
Romain Guy8bda2482010-03-02 11:42:11 -08002369 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002370
Mike Cleronf116bf82009-09-27 19:14:12 -07002371 if (!fadeScrollbars) {
2372 scrollabilityCache.state = ScrollabilityCache.ON;
2373 }
2374 scrollabilityCache.fadeScrollBars = fadeScrollbars;
2375
2376
2377 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2378 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2379 .getScrollBarFadeDuration());
2380 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2381 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2382 ViewConfiguration.getScrollDefaultDelay());
2383
2384
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002385 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2386 com.android.internal.R.styleable.View_scrollbarSize,
2387 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2388
2389 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2390 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2391
2392 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2393 if (thumb != null) {
2394 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2395 }
2396
2397 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2398 false);
2399 if (alwaysDraw) {
2400 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2401 }
2402
2403 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2404 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2405
2406 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2407 if (thumb != null) {
2408 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2409 }
2410
2411 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2412 false);
2413 if (alwaysDraw) {
2414 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2415 }
2416
2417 // Re-apply user/background padding so that scrollbar(s) get added
2418 recomputePadding();
2419 }
2420
2421 /**
2422 * <p>
2423 * Initalizes the scrollability cache if necessary.
2424 * </p>
2425 */
2426 private void initScrollCache() {
2427 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002428 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 }
2430 }
2431
2432 /**
2433 * Register a callback to be invoked when focus of this view changed.
2434 *
2435 * @param l The callback that will run.
2436 */
2437 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2438 mOnFocusChangeListener = l;
2439 }
2440
2441 /**
2442 * Returns the focus-change callback registered for this view.
2443 *
2444 * @return The callback, or null if one is not registered.
2445 */
2446 public OnFocusChangeListener getOnFocusChangeListener() {
2447 return mOnFocusChangeListener;
2448 }
2449
2450 /**
2451 * Register a callback to be invoked when this view is clicked. If this view is not
2452 * clickable, it becomes clickable.
2453 *
2454 * @param l The callback that will run
2455 *
2456 * @see #setClickable(boolean)
2457 */
2458 public void setOnClickListener(OnClickListener l) {
2459 if (!isClickable()) {
2460 setClickable(true);
2461 }
2462 mOnClickListener = l;
2463 }
2464
2465 /**
2466 * Register a callback to be invoked when this view is clicked and held. If this view is not
2467 * long clickable, it becomes long clickable.
2468 *
2469 * @param l The callback that will run
2470 *
2471 * @see #setLongClickable(boolean)
2472 */
2473 public void setOnLongClickListener(OnLongClickListener l) {
2474 if (!isLongClickable()) {
2475 setLongClickable(true);
2476 }
2477 mOnLongClickListener = l;
2478 }
2479
2480 /**
2481 * Register a callback to be invoked when the context menu for this view is
2482 * being built. If this view is not long clickable, it becomes long clickable.
2483 *
2484 * @param l The callback that will run
2485 *
2486 */
2487 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
2488 if (!isLongClickable()) {
2489 setLongClickable(true);
2490 }
2491 mOnCreateContextMenuListener = l;
2492 }
2493
2494 /**
2495 * Call this view's OnClickListener, if it is defined.
2496 *
2497 * @return True there was an assigned OnClickListener that was called, false
2498 * otherwise is returned.
2499 */
2500 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07002501 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
2502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 if (mOnClickListener != null) {
2504 playSoundEffect(SoundEffectConstants.CLICK);
2505 mOnClickListener.onClick(this);
2506 return true;
2507 }
2508
2509 return false;
2510 }
2511
2512 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07002513 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
2514 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07002516 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002517 */
2518 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07002519 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
2520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 boolean handled = false;
2522 if (mOnLongClickListener != null) {
2523 handled = mOnLongClickListener.onLongClick(View.this);
2524 }
2525 if (!handled) {
2526 handled = showContextMenu();
2527 }
2528 if (handled) {
2529 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
2530 }
2531 return handled;
2532 }
2533
2534 /**
2535 * Bring up the context menu for this view.
2536 *
2537 * @return Whether a context menu was displayed.
2538 */
2539 public boolean showContextMenu() {
2540 return getParent().showContextMenuForChild(this);
2541 }
2542
2543 /**
Adam Powell6e346362010-07-23 10:18:23 -07002544 * Start an action mode.
2545 *
2546 * @param callback Callback that will control the lifecycle of the action mode
2547 * @return The new action mode if it is started, null otherwise
2548 *
2549 * @see ActionMode
2550 */
2551 public ActionMode startActionMode(ActionMode.Callback callback) {
2552 return getParent().startActionModeForChild(this, callback);
2553 }
2554
2555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 * Register a callback to be invoked when a key is pressed in this view.
2557 * @param l the key listener to attach to this view
2558 */
2559 public void setOnKeyListener(OnKeyListener l) {
2560 mOnKeyListener = l;
2561 }
2562
2563 /**
2564 * Register a callback to be invoked when a touch event is sent to this view.
2565 * @param l the touch listener to attach to this view
2566 */
2567 public void setOnTouchListener(OnTouchListener l) {
2568 mOnTouchListener = l;
2569 }
2570
2571 /**
2572 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
2573 *
2574 * Note: this does not check whether this {@link View} should get focus, it just
2575 * gives it focus no matter what. It should only be called internally by framework
2576 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
2577 *
2578 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
2579 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
2580 * focus moved when requestFocus() is called. It may not always
2581 * apply, in which case use the default View.FOCUS_DOWN.
2582 * @param previouslyFocusedRect The rectangle of the view that had focus
2583 * prior in this View's coordinate system.
2584 */
2585 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
2586 if (DBG) {
2587 System.out.println(this + " requestFocus()");
2588 }
2589
2590 if ((mPrivateFlags & FOCUSED) == 0) {
2591 mPrivateFlags |= FOCUSED;
2592
2593 if (mParent != null) {
2594 mParent.requestChildFocus(this, this);
2595 }
2596
2597 onFocusChanged(true, direction, previouslyFocusedRect);
2598 refreshDrawableState();
2599 }
2600 }
2601
2602 /**
2603 * Request that a rectangle of this view be visible on the screen,
2604 * scrolling if necessary just enough.
2605 *
2606 * <p>A View should call this if it maintains some notion of which part
2607 * of its content is interesting. For example, a text editing view
2608 * should call this when its cursor moves.
2609 *
2610 * @param rectangle The rectangle.
2611 * @return Whether any parent scrolled.
2612 */
2613 public boolean requestRectangleOnScreen(Rect rectangle) {
2614 return requestRectangleOnScreen(rectangle, false);
2615 }
2616
2617 /**
2618 * Request that a rectangle of this view be visible on the screen,
2619 * scrolling if necessary just enough.
2620 *
2621 * <p>A View should call this if it maintains some notion of which part
2622 * of its content is interesting. For example, a text editing view
2623 * should call this when its cursor moves.
2624 *
2625 * <p>When <code>immediate</code> is set to true, scrolling will not be
2626 * animated.
2627 *
2628 * @param rectangle The rectangle.
2629 * @param immediate True to forbid animated scrolling, false otherwise
2630 * @return Whether any parent scrolled.
2631 */
2632 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
2633 View child = this;
2634 ViewParent parent = mParent;
2635 boolean scrolled = false;
2636 while (parent != null) {
2637 scrolled |= parent.requestChildRectangleOnScreen(child,
2638 rectangle, immediate);
2639
2640 // offset rect so next call has the rectangle in the
2641 // coordinate system of its direct child.
2642 rectangle.offset(child.getLeft(), child.getTop());
2643 rectangle.offset(-child.getScrollX(), -child.getScrollY());
2644
2645 if (!(parent instanceof View)) {
2646 break;
2647 }
Romain Guy8506ab42009-06-11 17:35:47 -07002648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002649 child = (View) parent;
2650 parent = child.getParent();
2651 }
2652 return scrolled;
2653 }
2654
2655 /**
2656 * Called when this view wants to give up focus. This will cause
2657 * {@link #onFocusChanged} to be called.
2658 */
2659 public void clearFocus() {
2660 if (DBG) {
2661 System.out.println(this + " clearFocus()");
2662 }
2663
2664 if ((mPrivateFlags & FOCUSED) != 0) {
2665 mPrivateFlags &= ~FOCUSED;
2666
2667 if (mParent != null) {
2668 mParent.clearChildFocus(this);
2669 }
2670
2671 onFocusChanged(false, 0, null);
2672 refreshDrawableState();
2673 }
2674 }
2675
2676 /**
2677 * Called to clear the focus of a view that is about to be removed.
2678 * Doesn't call clearChildFocus, which prevents this view from taking
2679 * focus again before it has been removed from the parent
2680 */
2681 void clearFocusForRemoval() {
2682 if ((mPrivateFlags & FOCUSED) != 0) {
2683 mPrivateFlags &= ~FOCUSED;
2684
2685 onFocusChanged(false, 0, null);
2686 refreshDrawableState();
2687 }
2688 }
2689
2690 /**
2691 * Called internally by the view system when a new view is getting focus.
2692 * This is what clears the old focus.
2693 */
2694 void unFocus() {
2695 if (DBG) {
2696 System.out.println(this + " unFocus()");
2697 }
2698
2699 if ((mPrivateFlags & FOCUSED) != 0) {
2700 mPrivateFlags &= ~FOCUSED;
2701
2702 onFocusChanged(false, 0, null);
2703 refreshDrawableState();
2704 }
2705 }
2706
2707 /**
2708 * Returns true if this view has focus iteself, or is the ancestor of the
2709 * view that has focus.
2710 *
2711 * @return True if this view has or contains focus, false otherwise.
2712 */
2713 @ViewDebug.ExportedProperty
2714 public boolean hasFocus() {
2715 return (mPrivateFlags & FOCUSED) != 0;
2716 }
2717
2718 /**
2719 * Returns true if this view is focusable or if it contains a reachable View
2720 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
2721 * is a View whose parents do not block descendants focus.
2722 *
2723 * Only {@link #VISIBLE} views are considered focusable.
2724 *
2725 * @return True if the view is focusable or if the view contains a focusable
2726 * View, false otherwise.
2727 *
2728 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
2729 */
2730 public boolean hasFocusable() {
2731 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
2732 }
2733
2734 /**
2735 * Called by the view system when the focus state of this view changes.
2736 * When the focus change event is caused by directional navigation, direction
2737 * and previouslyFocusedRect provide insight into where the focus is coming from.
2738 * When overriding, be sure to call up through to the super class so that
2739 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07002740 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002741 * @param gainFocus True if the View has focus; false otherwise.
2742 * @param direction The direction focus has moved when requestFocus()
2743 * is called to give this view focus. Values are
Romain Guyea4823c2009-12-08 15:03:39 -08002744 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT} or
2745 * {@link #FOCUS_RIGHT}. It may not always apply, in which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002746 * case use the default.
2747 * @param previouslyFocusedRect The rectangle, in this view's coordinate
2748 * system, of the previously focused view. If applicable, this will be
2749 * passed in as finer grained information about where the focus is coming
2750 * from (in addition to direction). Will be <code>null</code> otherwise.
2751 */
2752 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07002753 if (gainFocus) {
2754 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
2755 }
2756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002757 InputMethodManager imm = InputMethodManager.peekInstance();
2758 if (!gainFocus) {
2759 if (isPressed()) {
2760 setPressed(false);
2761 }
2762 if (imm != null && mAttachInfo != null
2763 && mAttachInfo.mHasWindowFocus) {
2764 imm.focusOut(this);
2765 }
Romain Guya2431d02009-04-30 16:30:00 -07002766 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002767 } else if (imm != null && mAttachInfo != null
2768 && mAttachInfo.mHasWindowFocus) {
2769 imm.focusIn(this);
2770 }
Romain Guy8506ab42009-06-11 17:35:47 -07002771
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002772 invalidate();
2773 if (mOnFocusChangeListener != null) {
2774 mOnFocusChangeListener.onFocusChange(this, gainFocus);
2775 }
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07002776
2777 if (mAttachInfo != null) {
2778 mAttachInfo.mKeyDispatchState.reset(this);
2779 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002780 }
2781
2782 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002783 * {@inheritDoc}
2784 */
2785 public void sendAccessibilityEvent(int eventType) {
2786 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
2787 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
2788 }
2789 }
2790
2791 /**
2792 * {@inheritDoc}
2793 */
2794 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
2795 event.setClassName(getClass().getName());
2796 event.setPackageName(getContext().getPackageName());
2797 event.setEnabled(isEnabled());
2798 event.setContentDescription(mContentDescription);
2799
2800 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
2801 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
2802 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
2803 event.setItemCount(focusablesTempList.size());
2804 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
2805 focusablesTempList.clear();
2806 }
2807
2808 dispatchPopulateAccessibilityEvent(event);
2809
2810 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
2811 }
2812
2813 /**
2814 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
2815 * to be populated.
2816 *
2817 * @param event The event.
2818 *
2819 * @return True if the event population was completed.
2820 */
2821 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
2822 return false;
2823 }
2824
2825 /**
2826 * Gets the {@link View} description. It briefly describes the view and is
2827 * primarily used for accessibility support. Set this property to enable
2828 * better accessibility support for your application. This is especially
2829 * true for views that do not have textual representation (For example,
2830 * ImageButton).
2831 *
2832 * @return The content descriptiopn.
2833 *
2834 * @attr ref android.R.styleable#View_contentDescription
2835 */
2836 public CharSequence getContentDescription() {
2837 return mContentDescription;
2838 }
2839
2840 /**
2841 * Sets the {@link View} description. It briefly describes the view and is
2842 * primarily used for accessibility support. Set this property to enable
2843 * better accessibility support for your application. This is especially
2844 * true for views that do not have textual representation (For example,
2845 * ImageButton).
2846 *
2847 * @param contentDescription The content description.
2848 *
2849 * @attr ref android.R.styleable#View_contentDescription
2850 */
2851 public void setContentDescription(CharSequence contentDescription) {
2852 mContentDescription = contentDescription;
2853 }
2854
2855 /**
Romain Guya2431d02009-04-30 16:30:00 -07002856 * Invoked whenever this view loses focus, either by losing window focus or by losing
2857 * focus within its window. This method can be used to clear any state tied to the
2858 * focus. For instance, if a button is held pressed with the trackball and the window
2859 * loses focus, this method can be used to cancel the press.
2860 *
2861 * Subclasses of View overriding this method should always call super.onFocusLost().
2862 *
2863 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07002864 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07002865 *
2866 * @hide pending API council approval
2867 */
2868 protected void onFocusLost() {
2869 resetPressedState();
2870 }
2871
2872 private void resetPressedState() {
2873 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
2874 return;
2875 }
2876
2877 if (isPressed()) {
2878 setPressed(false);
2879
2880 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05002881 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07002882 }
2883 }
2884 }
2885
2886 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002887 * Returns true if this view has focus
2888 *
2889 * @return True if this view has focus, false otherwise.
2890 */
2891 @ViewDebug.ExportedProperty
2892 public boolean isFocused() {
2893 return (mPrivateFlags & FOCUSED) != 0;
2894 }
2895
2896 /**
2897 * Find the view in the hierarchy rooted at this view that currently has
2898 * focus.
2899 *
2900 * @return The view that currently has focus, or null if no focused view can
2901 * be found.
2902 */
2903 public View findFocus() {
2904 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
2905 }
2906
2907 /**
2908 * Change whether this view is one of the set of scrollable containers in
2909 * its window. This will be used to determine whether the window can
2910 * resize or must pan when a soft input area is open -- scrollable
2911 * containers allow the window to use resize mode since the container
2912 * will appropriately shrink.
2913 */
2914 public void setScrollContainer(boolean isScrollContainer) {
2915 if (isScrollContainer) {
2916 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
2917 mAttachInfo.mScrollContainers.add(this);
2918 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
2919 }
2920 mPrivateFlags |= SCROLL_CONTAINER;
2921 } else {
2922 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
2923 mAttachInfo.mScrollContainers.remove(this);
2924 }
2925 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
2926 }
2927 }
2928
2929 /**
2930 * Returns the quality of the drawing cache.
2931 *
2932 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
2933 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
2934 *
2935 * @see #setDrawingCacheQuality(int)
2936 * @see #setDrawingCacheEnabled(boolean)
2937 * @see #isDrawingCacheEnabled()
2938 *
2939 * @attr ref android.R.styleable#View_drawingCacheQuality
2940 */
2941 public int getDrawingCacheQuality() {
2942 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
2943 }
2944
2945 /**
2946 * Set the drawing cache quality of this view. This value is used only when the
2947 * drawing cache is enabled
2948 *
2949 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
2950 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
2951 *
2952 * @see #getDrawingCacheQuality()
2953 * @see #setDrawingCacheEnabled(boolean)
2954 * @see #isDrawingCacheEnabled()
2955 *
2956 * @attr ref android.R.styleable#View_drawingCacheQuality
2957 */
2958 public void setDrawingCacheQuality(int quality) {
2959 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
2960 }
2961
2962 /**
2963 * Returns whether the screen should remain on, corresponding to the current
2964 * value of {@link #KEEP_SCREEN_ON}.
2965 *
2966 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
2967 *
2968 * @see #setKeepScreenOn(boolean)
2969 *
2970 * @attr ref android.R.styleable#View_keepScreenOn
2971 */
2972 public boolean getKeepScreenOn() {
2973 return (mViewFlags & KEEP_SCREEN_ON) != 0;
2974 }
2975
2976 /**
2977 * Controls whether the screen should remain on, modifying the
2978 * value of {@link #KEEP_SCREEN_ON}.
2979 *
2980 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
2981 *
2982 * @see #getKeepScreenOn()
2983 *
2984 * @attr ref android.R.styleable#View_keepScreenOn
2985 */
2986 public void setKeepScreenOn(boolean keepScreenOn) {
2987 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
2988 }
2989
2990 /**
2991 * @return The user specified next focus ID.
2992 *
2993 * @attr ref android.R.styleable#View_nextFocusLeft
2994 */
2995 public int getNextFocusLeftId() {
2996 return mNextFocusLeftId;
2997 }
2998
2999 /**
3000 * Set the id of the view to use for the next focus
3001 *
3002 * @param nextFocusLeftId
3003 *
3004 * @attr ref android.R.styleable#View_nextFocusLeft
3005 */
3006 public void setNextFocusLeftId(int nextFocusLeftId) {
3007 mNextFocusLeftId = nextFocusLeftId;
3008 }
3009
3010 /**
3011 * @return The user specified next focus ID.
3012 *
3013 * @attr ref android.R.styleable#View_nextFocusRight
3014 */
3015 public int getNextFocusRightId() {
3016 return mNextFocusRightId;
3017 }
3018
3019 /**
3020 * Set the id of the view to use for the next focus
3021 *
3022 * @param nextFocusRightId
3023 *
3024 * @attr ref android.R.styleable#View_nextFocusRight
3025 */
3026 public void setNextFocusRightId(int nextFocusRightId) {
3027 mNextFocusRightId = nextFocusRightId;
3028 }
3029
3030 /**
3031 * @return The user specified next focus ID.
3032 *
3033 * @attr ref android.R.styleable#View_nextFocusUp
3034 */
3035 public int getNextFocusUpId() {
3036 return mNextFocusUpId;
3037 }
3038
3039 /**
3040 * Set the id of the view to use for the next focus
3041 *
3042 * @param nextFocusUpId
3043 *
3044 * @attr ref android.R.styleable#View_nextFocusUp
3045 */
3046 public void setNextFocusUpId(int nextFocusUpId) {
3047 mNextFocusUpId = nextFocusUpId;
3048 }
3049
3050 /**
3051 * @return The user specified next focus ID.
3052 *
3053 * @attr ref android.R.styleable#View_nextFocusDown
3054 */
3055 public int getNextFocusDownId() {
3056 return mNextFocusDownId;
3057 }
3058
3059 /**
3060 * Set the id of the view to use for the next focus
3061 *
3062 * @param nextFocusDownId
3063 *
3064 * @attr ref android.R.styleable#View_nextFocusDown
3065 */
3066 public void setNextFocusDownId(int nextFocusDownId) {
3067 mNextFocusDownId = nextFocusDownId;
3068 }
3069
3070 /**
3071 * Returns the visibility of this view and all of its ancestors
3072 *
3073 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3074 */
3075 public boolean isShown() {
3076 View current = this;
3077 //noinspection ConstantConditions
3078 do {
3079 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3080 return false;
3081 }
3082 ViewParent parent = current.mParent;
3083 if (parent == null) {
3084 return false; // We are not attached to the view root
3085 }
3086 if (!(parent instanceof View)) {
3087 return true;
3088 }
3089 current = (View) parent;
3090 } while (current != null);
3091
3092 return false;
3093 }
3094
3095 /**
3096 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3097 * is set
3098 *
3099 * @param insets Insets for system windows
3100 *
3101 * @return True if this view applied the insets, false otherwise
3102 */
3103 protected boolean fitSystemWindows(Rect insets) {
3104 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3105 mPaddingLeft = insets.left;
3106 mPaddingTop = insets.top;
3107 mPaddingRight = insets.right;
3108 mPaddingBottom = insets.bottom;
3109 requestLayout();
3110 return true;
3111 }
3112 return false;
3113 }
3114
3115 /**
Jim Miller0b2a6d02010-07-13 18:01:29 -07003116 * Determine if this view has the FITS_SYSTEM_WINDOWS flag set.
3117 * @return True if window has FITS_SYSTEM_WINDOWS set
3118 *
3119 * @hide
3120 */
3121 public boolean isFitsSystemWindowsFlagSet() {
3122 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
3123 }
3124
3125 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003126 * Returns the visibility status for this view.
3127 *
3128 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3129 * @attr ref android.R.styleable#View_visibility
3130 */
3131 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003132 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3133 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3134 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003135 })
3136 public int getVisibility() {
3137 return mViewFlags & VISIBILITY_MASK;
3138 }
3139
3140 /**
3141 * Set the enabled state of this view.
3142 *
3143 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3144 * @attr ref android.R.styleable#View_visibility
3145 */
3146 @RemotableViewMethod
3147 public void setVisibility(int visibility) {
3148 setFlags(visibility, VISIBILITY_MASK);
3149 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3150 }
3151
3152 /**
3153 * Returns the enabled status for this view. The interpretation of the
3154 * enabled state varies by subclass.
3155 *
3156 * @return True if this view is enabled, false otherwise.
3157 */
3158 @ViewDebug.ExportedProperty
3159 public boolean isEnabled() {
3160 return (mViewFlags & ENABLED_MASK) == ENABLED;
3161 }
3162
3163 /**
3164 * Set the enabled state of this view. The interpretation of the enabled
3165 * state varies by subclass.
3166 *
3167 * @param enabled True if this view is enabled, false otherwise.
3168 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003169 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003170 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003171 if (enabled == isEnabled()) return;
3172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003173 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3174
3175 /*
3176 * The View most likely has to change its appearance, so refresh
3177 * the drawable state.
3178 */
3179 refreshDrawableState();
3180
3181 // Invalidate too, since the default behavior for views is to be
3182 // be drawn at 50% alpha rather than to change the drawable.
3183 invalidate();
3184 }
3185
3186 /**
3187 * Set whether this view can receive the focus.
3188 *
3189 * Setting this to false will also ensure that this view is not focusable
3190 * in touch mode.
3191 *
3192 * @param focusable If true, this view can receive the focus.
3193 *
3194 * @see #setFocusableInTouchMode(boolean)
3195 * @attr ref android.R.styleable#View_focusable
3196 */
3197 public void setFocusable(boolean focusable) {
3198 if (!focusable) {
3199 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3200 }
3201 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3202 }
3203
3204 /**
3205 * Set whether this view can receive focus while in touch mode.
3206 *
3207 * Setting this to true will also ensure that this view is focusable.
3208 *
3209 * @param focusableInTouchMode If true, this view can receive the focus while
3210 * in touch mode.
3211 *
3212 * @see #setFocusable(boolean)
3213 * @attr ref android.R.styleable#View_focusableInTouchMode
3214 */
3215 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3216 // Focusable in touch mode should always be set before the focusable flag
3217 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3218 // which, in touch mode, will not successfully request focus on this view
3219 // because the focusable in touch mode flag is not set
3220 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3221 if (focusableInTouchMode) {
3222 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3223 }
3224 }
3225
3226 /**
3227 * Set whether this view should have sound effects enabled for events such as
3228 * clicking and touching.
3229 *
3230 * <p>You may wish to disable sound effects for a view if you already play sounds,
3231 * for instance, a dial key that plays dtmf tones.
3232 *
3233 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3234 * @see #isSoundEffectsEnabled()
3235 * @see #playSoundEffect(int)
3236 * @attr ref android.R.styleable#View_soundEffectsEnabled
3237 */
3238 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3239 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3240 }
3241
3242 /**
3243 * @return whether this view should have sound effects enabled for events such as
3244 * clicking and touching.
3245 *
3246 * @see #setSoundEffectsEnabled(boolean)
3247 * @see #playSoundEffect(int)
3248 * @attr ref android.R.styleable#View_soundEffectsEnabled
3249 */
3250 @ViewDebug.ExportedProperty
3251 public boolean isSoundEffectsEnabled() {
3252 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3253 }
3254
3255 /**
3256 * Set whether this view should have haptic feedback for events such as
3257 * long presses.
3258 *
3259 * <p>You may wish to disable haptic feedback if your view already controls
3260 * its own haptic feedback.
3261 *
3262 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3263 * @see #isHapticFeedbackEnabled()
3264 * @see #performHapticFeedback(int)
3265 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3266 */
3267 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3268 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3269 }
3270
3271 /**
3272 * @return whether this view should have haptic feedback enabled for events
3273 * long presses.
3274 *
3275 * @see #setHapticFeedbackEnabled(boolean)
3276 * @see #performHapticFeedback(int)
3277 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3278 */
3279 @ViewDebug.ExportedProperty
3280 public boolean isHapticFeedbackEnabled() {
3281 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3282 }
3283
3284 /**
3285 * If this view doesn't do any drawing on its own, set this flag to
3286 * allow further optimizations. By default, this flag is not set on
3287 * View, but could be set on some View subclasses such as ViewGroup.
3288 *
3289 * Typically, if you override {@link #onDraw} you should clear this flag.
3290 *
3291 * @param willNotDraw whether or not this View draw on its own
3292 */
3293 public void setWillNotDraw(boolean willNotDraw) {
3294 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3295 }
3296
3297 /**
3298 * Returns whether or not this View draws on its own.
3299 *
3300 * @return true if this view has nothing to draw, false otherwise
3301 */
3302 @ViewDebug.ExportedProperty
3303 public boolean willNotDraw() {
3304 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3305 }
3306
3307 /**
3308 * When a View's drawing cache is enabled, drawing is redirected to an
3309 * offscreen bitmap. Some views, like an ImageView, must be able to
3310 * bypass this mechanism if they already draw a single bitmap, to avoid
3311 * unnecessary usage of the memory.
3312 *
3313 * @param willNotCacheDrawing true if this view does not cache its
3314 * drawing, false otherwise
3315 */
3316 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3317 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3318 }
3319
3320 /**
3321 * Returns whether or not this View can cache its drawing or not.
3322 *
3323 * @return true if this view does not cache its drawing, false otherwise
3324 */
3325 @ViewDebug.ExportedProperty
3326 public boolean willNotCacheDrawing() {
3327 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3328 }
3329
3330 /**
3331 * Indicates whether this view reacts to click events or not.
3332 *
3333 * @return true if the view is clickable, false otherwise
3334 *
3335 * @see #setClickable(boolean)
3336 * @attr ref android.R.styleable#View_clickable
3337 */
3338 @ViewDebug.ExportedProperty
3339 public boolean isClickable() {
3340 return (mViewFlags & CLICKABLE) == CLICKABLE;
3341 }
3342
3343 /**
3344 * Enables or disables click events for this view. When a view
3345 * is clickable it will change its state to "pressed" on every click.
3346 * Subclasses should set the view clickable to visually react to
3347 * user's clicks.
3348 *
3349 * @param clickable true to make the view clickable, false otherwise
3350 *
3351 * @see #isClickable()
3352 * @attr ref android.R.styleable#View_clickable
3353 */
3354 public void setClickable(boolean clickable) {
3355 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3356 }
3357
3358 /**
3359 * Indicates whether this view reacts to long click events or not.
3360 *
3361 * @return true if the view is long clickable, false otherwise
3362 *
3363 * @see #setLongClickable(boolean)
3364 * @attr ref android.R.styleable#View_longClickable
3365 */
3366 public boolean isLongClickable() {
3367 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3368 }
3369
3370 /**
3371 * Enables or disables long click events for this view. When a view is long
3372 * clickable it reacts to the user holding down the button for a longer
3373 * duration than a tap. This event can either launch the listener or a
3374 * context menu.
3375 *
3376 * @param longClickable true to make the view long clickable, false otherwise
3377 * @see #isLongClickable()
3378 * @attr ref android.R.styleable#View_longClickable
3379 */
3380 public void setLongClickable(boolean longClickable) {
3381 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
3382 }
3383
3384 /**
3385 * Sets the pressed that for this view.
3386 *
3387 * @see #isClickable()
3388 * @see #setClickable(boolean)
3389 *
3390 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
3391 * the View's internal state from a previously set "pressed" state.
3392 */
3393 public void setPressed(boolean pressed) {
3394 if (pressed) {
3395 mPrivateFlags |= PRESSED;
3396 } else {
3397 mPrivateFlags &= ~PRESSED;
3398 }
3399 refreshDrawableState();
3400 dispatchSetPressed(pressed);
3401 }
3402
3403 /**
3404 * Dispatch setPressed to all of this View's children.
3405 *
3406 * @see #setPressed(boolean)
3407 *
3408 * @param pressed The new pressed state
3409 */
3410 protected void dispatchSetPressed(boolean pressed) {
3411 }
3412
3413 /**
3414 * Indicates whether the view is currently in pressed state. Unless
3415 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
3416 * the pressed state.
3417 *
3418 * @see #setPressed
3419 * @see #isClickable()
3420 * @see #setClickable(boolean)
3421 *
3422 * @return true if the view is currently pressed, false otherwise
3423 */
3424 public boolean isPressed() {
3425 return (mPrivateFlags & PRESSED) == PRESSED;
3426 }
3427
3428 /**
3429 * Indicates whether this view will save its state (that is,
3430 * whether its {@link #onSaveInstanceState} method will be called).
3431 *
3432 * @return Returns true if the view state saving is enabled, else false.
3433 *
3434 * @see #setSaveEnabled(boolean)
3435 * @attr ref android.R.styleable#View_saveEnabled
3436 */
3437 public boolean isSaveEnabled() {
3438 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
3439 }
3440
3441 /**
3442 * Controls whether the saving of this view's state is
3443 * enabled (that is, whether its {@link #onSaveInstanceState} method
3444 * will be called). Note that even if freezing is enabled, the
3445 * view still must have an id assigned to it (via {@link #setId setId()})
3446 * for its state to be saved. This flag can only disable the
3447 * saving of this view; any child views may still have their state saved.
3448 *
3449 * @param enabled Set to false to <em>disable</em> state saving, or true
3450 * (the default) to allow it.
3451 *
3452 * @see #isSaveEnabled()
3453 * @see #setId(int)
3454 * @see #onSaveInstanceState()
3455 * @attr ref android.R.styleable#View_saveEnabled
3456 */
3457 public void setSaveEnabled(boolean enabled) {
3458 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
3459 }
3460
3461
3462 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07003463 * Indicates whether the entire hierarchy under this view will save its
3464 * state when a state saving traversal occurs from its parent. The default
3465 * is true; if false, these views will not be saved unless
3466 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
3467 *
3468 * @return Returns true if the view state saving from parent is enabled, else false.
3469 *
3470 * @see #setSaveFromParentEnabled(boolean)
3471 */
3472 public boolean isSaveFromParentEnabled() {
3473 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
3474 }
3475
3476 /**
3477 * Controls whether the entire hierarchy under this view will save its
3478 * state when a state saving traversal occurs from its parent. The default
3479 * is true; if false, these views will not be saved unless
3480 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
3481 *
3482 * @param enabled Set to false to <em>disable</em> state saving, or true
3483 * (the default) to allow it.
3484 *
3485 * @see #isSaveFromParentEnabled()
3486 * @see #setId(int)
3487 * @see #onSaveInstanceState()
3488 */
3489 public void setSaveFromParentEnabled(boolean enabled) {
3490 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
3491 }
3492
3493
3494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003495 * Returns whether this View is able to take focus.
3496 *
3497 * @return True if this view can take focus, or false otherwise.
3498 * @attr ref android.R.styleable#View_focusable
3499 */
3500 @ViewDebug.ExportedProperty
3501 public final boolean isFocusable() {
3502 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
3503 }
3504
3505 /**
3506 * When a view is focusable, it may not want to take focus when in touch mode.
3507 * For example, a button would like focus when the user is navigating via a D-pad
3508 * so that the user can click on it, but once the user starts touching the screen,
3509 * the button shouldn't take focus
3510 * @return Whether the view is focusable in touch mode.
3511 * @attr ref android.R.styleable#View_focusableInTouchMode
3512 */
3513 @ViewDebug.ExportedProperty
3514 public final boolean isFocusableInTouchMode() {
3515 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
3516 }
3517
3518 /**
3519 * Find the nearest view in the specified direction that can take focus.
3520 * This does not actually give focus to that view.
3521 *
3522 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3523 *
3524 * @return The nearest focusable in the specified direction, or null if none
3525 * can be found.
3526 */
3527 public View focusSearch(int direction) {
3528 if (mParent != null) {
3529 return mParent.focusSearch(this, direction);
3530 } else {
3531 return null;
3532 }
3533 }
3534
3535 /**
3536 * This method is the last chance for the focused view and its ancestors to
3537 * respond to an arrow key. This is called when the focused view did not
3538 * consume the key internally, nor could the view system find a new view in
3539 * the requested direction to give focus to.
3540 *
3541 * @param focused The currently focused view.
3542 * @param direction The direction focus wants to move. One of FOCUS_UP,
3543 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
3544 * @return True if the this view consumed this unhandled move.
3545 */
3546 public boolean dispatchUnhandledMove(View focused, int direction) {
3547 return false;
3548 }
3549
3550 /**
3551 * If a user manually specified the next view id for a particular direction,
3552 * use the root to look up the view. Once a view is found, it is cached
3553 * for future lookups.
3554 * @param root The root view of the hierarchy containing this view.
3555 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3556 * @return The user specified next view, or null if there is none.
3557 */
3558 View findUserSetNextFocus(View root, int direction) {
3559 switch (direction) {
3560 case FOCUS_LEFT:
3561 if (mNextFocusLeftId == View.NO_ID) return null;
3562 return findViewShouldExist(root, mNextFocusLeftId);
3563 case FOCUS_RIGHT:
3564 if (mNextFocusRightId == View.NO_ID) return null;
3565 return findViewShouldExist(root, mNextFocusRightId);
3566 case FOCUS_UP:
3567 if (mNextFocusUpId == View.NO_ID) return null;
3568 return findViewShouldExist(root, mNextFocusUpId);
3569 case FOCUS_DOWN:
3570 if (mNextFocusDownId == View.NO_ID) return null;
3571 return findViewShouldExist(root, mNextFocusDownId);
3572 }
3573 return null;
3574 }
3575
3576 private static View findViewShouldExist(View root, int childViewId) {
3577 View result = root.findViewById(childViewId);
3578 if (result == null) {
3579 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
3580 + "by user for id " + childViewId);
3581 }
3582 return result;
3583 }
3584
3585 /**
3586 * Find and return all focusable views that are descendants of this view,
3587 * possibly including this view if it is focusable itself.
3588 *
3589 * @param direction The direction of the focus
3590 * @return A list of focusable views
3591 */
3592 public ArrayList<View> getFocusables(int direction) {
3593 ArrayList<View> result = new ArrayList<View>(24);
3594 addFocusables(result, direction);
3595 return result;
3596 }
3597
3598 /**
3599 * Add any focusable views that are descendants of this view (possibly
3600 * including this view if it is focusable itself) to views. If we are in touch mode,
3601 * only add views that are also focusable in touch mode.
3602 *
3603 * @param views Focusable views found so far
3604 * @param direction The direction of the focus
3605 */
3606 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003607 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
3608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003609
svetoslavganov75986cf2009-05-14 22:28:01 -07003610 /**
3611 * Adds any focusable views that are descendants of this view (possibly
3612 * including this view if it is focusable itself) to views. This method
3613 * adds all focusable views regardless if we are in touch mode or
3614 * only views focusable in touch mode if we are in touch mode depending on
3615 * the focusable mode paramater.
3616 *
3617 * @param views Focusable views found so far or null if all we are interested is
3618 * the number of focusables.
3619 * @param direction The direction of the focus.
3620 * @param focusableMode The type of focusables to be added.
3621 *
3622 * @see #FOCUSABLES_ALL
3623 * @see #FOCUSABLES_TOUCH_MODE
3624 */
3625 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
3626 if (!isFocusable()) {
3627 return;
3628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003629
svetoslavganov75986cf2009-05-14 22:28:01 -07003630 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
3631 isInTouchMode() && !isFocusableInTouchMode()) {
3632 return;
3633 }
3634
3635 if (views != null) {
3636 views.add(this);
3637 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003638 }
3639
3640 /**
3641 * Find and return all touchable views that are descendants of this view,
3642 * possibly including this view if it is touchable itself.
3643 *
3644 * @return A list of touchable views
3645 */
3646 public ArrayList<View> getTouchables() {
3647 ArrayList<View> result = new ArrayList<View>();
3648 addTouchables(result);
3649 return result;
3650 }
3651
3652 /**
3653 * Add any touchable views that are descendants of this view (possibly
3654 * including this view if it is touchable itself) to views.
3655 *
3656 * @param views Touchable views found so far
3657 */
3658 public void addTouchables(ArrayList<View> views) {
3659 final int viewFlags = mViewFlags;
3660
3661 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
3662 && (viewFlags & ENABLED_MASK) == ENABLED) {
3663 views.add(this);
3664 }
3665 }
3666
3667 /**
3668 * Call this to try to give focus to a specific view or to one of its
3669 * descendants.
3670 *
3671 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3672 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3673 * while the device is in touch mode.
3674 *
3675 * See also {@link #focusSearch}, which is what you call to say that you
3676 * have focus, and you want your parent to look for the next one.
3677 *
3678 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
3679 * {@link #FOCUS_DOWN} and <code>null</code>.
3680 *
3681 * @return Whether this view or one of its descendants actually took focus.
3682 */
3683 public final boolean requestFocus() {
3684 return requestFocus(View.FOCUS_DOWN);
3685 }
3686
3687
3688 /**
3689 * Call this to try to give focus to a specific view or to one of its
3690 * descendants and give it a hint about what direction focus is heading.
3691 *
3692 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3693 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3694 * while the device is in touch mode.
3695 *
3696 * See also {@link #focusSearch}, which is what you call to say that you
3697 * have focus, and you want your parent to look for the next one.
3698 *
3699 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
3700 * <code>null</code> set for the previously focused rectangle.
3701 *
3702 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3703 * @return Whether this view or one of its descendants actually took focus.
3704 */
3705 public final boolean requestFocus(int direction) {
3706 return requestFocus(direction, null);
3707 }
3708
3709 /**
3710 * Call this to try to give focus to a specific view or to one of its descendants
3711 * and give it hints about the direction and a specific rectangle that the focus
3712 * is coming from. The rectangle can help give larger views a finer grained hint
3713 * about where focus is coming from, and therefore, where to show selection, or
3714 * forward focus change internally.
3715 *
3716 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
3717 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
3718 * while the device is in touch mode.
3719 *
3720 * A View will not take focus if it is not visible.
3721 *
3722 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
3723 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
3724 *
3725 * See also {@link #focusSearch}, which is what you call to say that you
3726 * have focus, and you want your parent to look for the next one.
3727 *
3728 * You may wish to override this method if your custom {@link View} has an internal
3729 * {@link View} that it wishes to forward the request to.
3730 *
3731 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
3732 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
3733 * to give a finer grained hint about where focus is coming from. May be null
3734 * if there is no hint.
3735 * @return Whether this view or one of its descendants actually took focus.
3736 */
3737 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
3738 // need to be focusable
3739 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
3740 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3741 return false;
3742 }
3743
3744 // need to be focusable in touch mode if in touch mode
3745 if (isInTouchMode() &&
3746 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
3747 return false;
3748 }
3749
3750 // need to not have any parents blocking us
3751 if (hasAncestorThatBlocksDescendantFocus()) {
3752 return false;
3753 }
3754
3755 handleFocusGainInternal(direction, previouslyFocusedRect);
3756 return true;
3757 }
3758
3759 /**
3760 * Call this to try to give focus to a specific view or to one of its descendants. This is a
3761 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
3762 * touch mode to request focus when they are touched.
3763 *
3764 * @return Whether this view or one of its descendants actually took focus.
3765 *
3766 * @see #isInTouchMode()
3767 *
3768 */
3769 public final boolean requestFocusFromTouch() {
3770 // Leave touch mode if we need to
3771 if (isInTouchMode()) {
3772 View root = getRootView();
3773 if (root != null) {
3774 ViewRoot viewRoot = (ViewRoot)root.getParent();
3775 if (viewRoot != null) {
3776 viewRoot.ensureTouchMode(false);
3777 }
3778 }
3779 }
3780 return requestFocus(View.FOCUS_DOWN);
3781 }
3782
3783 /**
3784 * @return Whether any ancestor of this view blocks descendant focus.
3785 */
3786 private boolean hasAncestorThatBlocksDescendantFocus() {
3787 ViewParent ancestor = mParent;
3788 while (ancestor instanceof ViewGroup) {
3789 final ViewGroup vgAncestor = (ViewGroup) ancestor;
3790 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
3791 return true;
3792 } else {
3793 ancestor = vgAncestor.getParent();
3794 }
3795 }
3796 return false;
3797 }
3798
3799 /**
Romain Guya440b002010-02-24 15:57:54 -08003800 * @hide
3801 */
3802 public void dispatchStartTemporaryDetach() {
3803 onStartTemporaryDetach();
3804 }
3805
3806 /**
3807 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003808 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
3809 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08003810 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003811 */
3812 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08003813 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08003814 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08003815 }
3816
3817 /**
3818 * @hide
3819 */
3820 public void dispatchFinishTemporaryDetach() {
3821 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003822 }
Romain Guy8506ab42009-06-11 17:35:47 -07003823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003824 /**
3825 * Called after {@link #onStartTemporaryDetach} when the container is done
3826 * changing the view.
3827 */
3828 public void onFinishTemporaryDetach() {
3829 }
Romain Guy8506ab42009-06-11 17:35:47 -07003830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003831 /**
3832 * capture information of this view for later analysis: developement only
3833 * check dynamic switch to make sure we only dump view
3834 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
3835 */
3836 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003837 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003838 return;
3839 }
3840 ViewDebug.dumpCapturedView(subTag, v);
3841 }
3842
3843 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003844 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
3845 * for this view's window. Returns null if the view is not currently attached
3846 * to the window. Normally you will not need to use this directly, but
3847 * just use the standard high-level event callbacks like {@link #onKeyDown}.
3848 */
3849 public KeyEvent.DispatcherState getKeyDispatcherState() {
3850 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
3851 }
3852
3853 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003854 * Dispatch a key event before it is processed by any input method
3855 * associated with the view hierarchy. This can be used to intercept
3856 * key events in special situations before the IME consumes them; a
3857 * typical example would be handling the BACK key to update the application's
3858 * UI instead of allowing the IME to see it and close itself.
3859 *
3860 * @param event The key event to be dispatched.
3861 * @return True if the event was handled, false otherwise.
3862 */
3863 public boolean dispatchKeyEventPreIme(KeyEvent event) {
3864 return onKeyPreIme(event.getKeyCode(), event);
3865 }
3866
3867 /**
3868 * Dispatch a key event to the next view on the focus path. This path runs
3869 * from the top of the view tree down to the currently focused view. If this
3870 * view has focus, it will dispatch to itself. Otherwise it will dispatch
3871 * the next node down the focus path. This method also fires any key
3872 * listeners.
3873 *
3874 * @param event The key event to be dispatched.
3875 * @return True if the event was handled, false otherwise.
3876 */
3877 public boolean dispatchKeyEvent(KeyEvent event) {
3878 // If any attached key listener a first crack at the event.
3879 //noinspection SimplifiableIfStatement
3880
3881 if (android.util.Config.LOGV) {
3882 captureViewInfo("captureViewKeyEvent", this);
3883 }
3884
3885 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
3886 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
3887 return true;
3888 }
3889
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003890 return event.dispatch(this, mAttachInfo != null
3891 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003892 }
3893
3894 /**
3895 * Dispatches a key shortcut event.
3896 *
3897 * @param event The key event to be dispatched.
3898 * @return True if the event was handled by the view, false otherwise.
3899 */
3900 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
3901 return onKeyShortcut(event.getKeyCode(), event);
3902 }
3903
3904 /**
3905 * Pass the touch screen motion event down to the target view, or this
3906 * view if it is the target.
3907 *
3908 * @param event The motion event to be dispatched.
3909 * @return True if the event was handled by the view, false otherwise.
3910 */
3911 public boolean dispatchTouchEvent(MotionEvent event) {
3912 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
3913 mOnTouchListener.onTouch(this, event)) {
3914 return true;
3915 }
3916 return onTouchEvent(event);
3917 }
3918
3919 /**
3920 * Pass a trackball motion event down to the focused view.
3921 *
3922 * @param event The motion event to be dispatched.
3923 * @return True if the event was handled by the view, false otherwise.
3924 */
3925 public boolean dispatchTrackballEvent(MotionEvent event) {
3926 //Log.i("view", "view=" + this + ", " + event.toString());
3927 return onTrackballEvent(event);
3928 }
3929
3930 /**
3931 * Called when the window containing this view gains or loses window focus.
3932 * ViewGroups should override to route to their children.
3933 *
3934 * @param hasFocus True if the window containing this view now has focus,
3935 * false otherwise.
3936 */
3937 public void dispatchWindowFocusChanged(boolean hasFocus) {
3938 onWindowFocusChanged(hasFocus);
3939 }
3940
3941 /**
3942 * Called when the window containing this view gains or loses focus. Note
3943 * that this is separate from view focus: to receive key events, both
3944 * your view and its window must have focus. If a window is displayed
3945 * on top of yours that takes input focus, then your own window will lose
3946 * focus but the view focus will remain unchanged.
3947 *
3948 * @param hasWindowFocus True if the window containing this view now has
3949 * focus, false otherwise.
3950 */
3951 public void onWindowFocusChanged(boolean hasWindowFocus) {
3952 InputMethodManager imm = InputMethodManager.peekInstance();
3953 if (!hasWindowFocus) {
3954 if (isPressed()) {
3955 setPressed(false);
3956 }
3957 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
3958 imm.focusOut(this);
3959 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05003960 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003961 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003962 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
3963 imm.focusIn(this);
3964 }
3965 refreshDrawableState();
3966 }
3967
3968 /**
3969 * Returns true if this view is in a window that currently has window focus.
3970 * Note that this is not the same as the view itself having focus.
3971 *
3972 * @return True if this view is in a window that currently has window focus.
3973 */
3974 public boolean hasWindowFocus() {
3975 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
3976 }
3977
3978 /**
Adam Powell326d8082009-12-09 15:10:07 -08003979 * Dispatch a view visibility change down the view hierarchy.
3980 * ViewGroups should override to route to their children.
3981 * @param changedView The view whose visibility changed. Could be 'this' or
3982 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08003983 * @param visibility The new visibility of changedView: {@link #VISIBLE},
3984 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08003985 */
3986 protected void dispatchVisibilityChanged(View changedView, int visibility) {
3987 onVisibilityChanged(changedView, visibility);
3988 }
3989
3990 /**
3991 * Called when the visibility of the view or an ancestor of the view is changed.
3992 * @param changedView The view whose visibility changed. Could be 'this' or
3993 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08003994 * @param visibility The new visibility of changedView: {@link #VISIBLE},
3995 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08003996 */
3997 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07003998 if (visibility == VISIBLE) {
3999 if (mAttachInfo != null) {
4000 initialAwakenScrollBars();
4001 } else {
4002 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4003 }
4004 }
Adam Powell326d8082009-12-09 15:10:07 -08004005 }
4006
4007 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004008 * Dispatch a hint about whether this view is displayed. For instance, when
4009 * a View moves out of the screen, it might receives a display hint indicating
4010 * the view is not displayed. Applications should not <em>rely</em> on this hint
4011 * as there is no guarantee that they will receive one.
4012 *
4013 * @param hint A hint about whether or not this view is displayed:
4014 * {@link #VISIBLE} or {@link #INVISIBLE}.
4015 */
4016 public void dispatchDisplayHint(int hint) {
4017 onDisplayHint(hint);
4018 }
4019
4020 /**
4021 * Gives this view a hint about whether is displayed or not. For instance, when
4022 * a View moves out of the screen, it might receives a display hint indicating
4023 * the view is not displayed. Applications should not <em>rely</em> on this hint
4024 * as there is no guarantee that they will receive one.
4025 *
4026 * @param hint A hint about whether or not this view is displayed:
4027 * {@link #VISIBLE} or {@link #INVISIBLE}.
4028 */
4029 protected void onDisplayHint(int hint) {
4030 }
4031
4032 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 * Dispatch a window visibility change down the view hierarchy.
4034 * ViewGroups should override to route to their children.
4035 *
4036 * @param visibility The new visibility of the window.
4037 *
4038 * @see #onWindowVisibilityChanged
4039 */
4040 public void dispatchWindowVisibilityChanged(int visibility) {
4041 onWindowVisibilityChanged(visibility);
4042 }
4043
4044 /**
4045 * Called when the window containing has change its visibility
4046 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4047 * that this tells you whether or not your window is being made visible
4048 * to the window manager; this does <em>not</em> tell you whether or not
4049 * your window is obscured by other windows on the screen, even if it
4050 * is itself visible.
4051 *
4052 * @param visibility The new visibility of the window.
4053 */
4054 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004055 if (visibility == VISIBLE) {
4056 initialAwakenScrollBars();
4057 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004058 }
4059
4060 /**
4061 * Returns the current visibility of the window this view is attached to
4062 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4063 *
4064 * @return Returns the current visibility of the view's window.
4065 */
4066 public int getWindowVisibility() {
4067 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4068 }
4069
4070 /**
4071 * Retrieve the overall visible display size in which the window this view is
4072 * attached to has been positioned in. This takes into account screen
4073 * decorations above the window, for both cases where the window itself
4074 * is being position inside of them or the window is being placed under
4075 * then and covered insets are used for the window to position its content
4076 * inside. In effect, this tells you the available area where content can
4077 * be placed and remain visible to users.
4078 *
4079 * <p>This function requires an IPC back to the window manager to retrieve
4080 * the requested information, so should not be used in performance critical
4081 * code like drawing.
4082 *
4083 * @param outRect Filled in with the visible display frame. If the view
4084 * is not attached to a window, this is simply the raw display size.
4085 */
4086 public void getWindowVisibleDisplayFrame(Rect outRect) {
4087 if (mAttachInfo != null) {
4088 try {
4089 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4090 } catch (RemoteException e) {
4091 return;
4092 }
4093 // XXX This is really broken, and probably all needs to be done
4094 // in the window manager, and we need to know more about whether
4095 // we want the area behind or in front of the IME.
4096 final Rect insets = mAttachInfo.mVisibleInsets;
4097 outRect.left += insets.left;
4098 outRect.top += insets.top;
4099 outRect.right -= insets.right;
4100 outRect.bottom -= insets.bottom;
4101 return;
4102 }
4103 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4104 outRect.set(0, 0, d.getWidth(), d.getHeight());
4105 }
4106
4107 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004108 * Dispatch a notification about a resource configuration change down
4109 * the view hierarchy.
4110 * ViewGroups should override to route to their children.
4111 *
4112 * @param newConfig The new resource configuration.
4113 *
4114 * @see #onConfigurationChanged
4115 */
4116 public void dispatchConfigurationChanged(Configuration newConfig) {
4117 onConfigurationChanged(newConfig);
4118 }
4119
4120 /**
4121 * Called when the current configuration of the resources being used
4122 * by the application have changed. You can use this to decide when
4123 * to reload resources that can changed based on orientation and other
4124 * configuration characterstics. You only need to use this if you are
4125 * not relying on the normal {@link android.app.Activity} mechanism of
4126 * recreating the activity instance upon a configuration change.
4127 *
4128 * @param newConfig The new resource configuration.
4129 */
4130 protected void onConfigurationChanged(Configuration newConfig) {
4131 }
4132
4133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004134 * Private function to aggregate all per-view attributes in to the view
4135 * root.
4136 */
4137 void dispatchCollectViewAttributes(int visibility) {
4138 performCollectViewAttributes(visibility);
4139 }
4140
4141 void performCollectViewAttributes(int visibility) {
4142 //noinspection PointlessBitwiseExpression
4143 if (((visibility | mViewFlags) & (VISIBILITY_MASK | KEEP_SCREEN_ON))
4144 == (VISIBLE | KEEP_SCREEN_ON)) {
4145 mAttachInfo.mKeepScreenOn = true;
4146 }
4147 }
4148
4149 void needGlobalAttributesUpdate(boolean force) {
4150 AttachInfo ai = mAttachInfo;
4151 if (ai != null) {
4152 if (ai.mKeepScreenOn || force) {
4153 ai.mRecomputeGlobalAttributes = true;
4154 }
4155 }
4156 }
4157
4158 /**
4159 * Returns whether the device is currently in touch mode. Touch mode is entered
4160 * once the user begins interacting with the device by touch, and affects various
4161 * things like whether focus is always visible to the user.
4162 *
4163 * @return Whether the device is in touch mode.
4164 */
4165 @ViewDebug.ExportedProperty
4166 public boolean isInTouchMode() {
4167 if (mAttachInfo != null) {
4168 return mAttachInfo.mInTouchMode;
4169 } else {
4170 return ViewRoot.isInTouchMode();
4171 }
4172 }
4173
4174 /**
4175 * Returns the context the view is running in, through which it can
4176 * access the current theme, resources, etc.
4177 *
4178 * @return The view's Context.
4179 */
4180 @ViewDebug.CapturedViewProperty
4181 public final Context getContext() {
4182 return mContext;
4183 }
4184
4185 /**
4186 * Handle a key event before it is processed by any input method
4187 * associated with the view hierarchy. This can be used to intercept
4188 * key events in special situations before the IME consumes them; a
4189 * typical example would be handling the BACK key to update the application's
4190 * UI instead of allowing the IME to see it and close itself.
4191 *
4192 * @param keyCode The value in event.getKeyCode().
4193 * @param event Description of the key event.
4194 * @return If you handled the event, return true. If you want to allow the
4195 * event to be handled by the next receiver, return false.
4196 */
4197 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4198 return false;
4199 }
4200
4201 /**
4202 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4203 * KeyEvent.Callback.onKeyMultiple()}: perform press of the view
4204 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4205 * is released, if the view is enabled and clickable.
4206 *
4207 * @param keyCode A key code that represents the button pressed, from
4208 * {@link android.view.KeyEvent}.
4209 * @param event The KeyEvent object that defines the button action.
4210 */
4211 public boolean onKeyDown(int keyCode, KeyEvent event) {
4212 boolean result = false;
4213
4214 switch (keyCode) {
4215 case KeyEvent.KEYCODE_DPAD_CENTER:
4216 case KeyEvent.KEYCODE_ENTER: {
4217 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4218 return true;
4219 }
4220 // Long clickable items don't necessarily have to be clickable
4221 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4222 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4223 (event.getRepeatCount() == 0)) {
4224 setPressed(true);
4225 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004226 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004227 }
4228 return true;
4229 }
4230 break;
4231 }
4232 }
4233 return result;
4234 }
4235
4236 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004237 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4238 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4239 * the event).
4240 */
4241 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
4242 return false;
4243 }
4244
4245 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004246 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4247 * KeyEvent.Callback.onKeyMultiple()}: perform clicking of the view
4248 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
4249 * {@link KeyEvent#KEYCODE_ENTER} is released.
4250 *
4251 * @param keyCode A key code that represents the button pressed, from
4252 * {@link android.view.KeyEvent}.
4253 * @param event The KeyEvent object that defines the button action.
4254 */
4255 public boolean onKeyUp(int keyCode, KeyEvent event) {
4256 boolean result = false;
4257
4258 switch (keyCode) {
4259 case KeyEvent.KEYCODE_DPAD_CENTER:
4260 case KeyEvent.KEYCODE_ENTER: {
4261 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4262 return true;
4263 }
4264 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
4265 setPressed(false);
4266
4267 if (!mHasPerformedLongPress) {
4268 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05004269 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004270
4271 result = performClick();
4272 }
4273 }
4274 break;
4275 }
4276 }
4277 return result;
4278 }
4279
4280 /**
4281 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4282 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
4283 * the event).
4284 *
4285 * @param keyCode A key code that represents the button pressed, from
4286 * {@link android.view.KeyEvent}.
4287 * @param repeatCount The number of times the action was made.
4288 * @param event The KeyEvent object that defines the button action.
4289 */
4290 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4291 return false;
4292 }
4293
4294 /**
4295 * Called when an unhandled key shortcut event occurs.
4296 *
4297 * @param keyCode The value in event.getKeyCode().
4298 * @param event Description of the key event.
4299 * @return If you handled the event, return true. If you want to allow the
4300 * event to be handled by the next receiver, return false.
4301 */
4302 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4303 return false;
4304 }
4305
4306 /**
4307 * Check whether the called view is a text editor, in which case it
4308 * would make sense to automatically display a soft input window for
4309 * it. Subclasses should override this if they implement
4310 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004311 * a call on that method would return a non-null InputConnection, and
4312 * they are really a first-class editor that the user would normally
4313 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07004314 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004315 * <p>The default implementation always returns false. This does
4316 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
4317 * will not be called or the user can not otherwise perform edits on your
4318 * view; it is just a hint to the system that this is not the primary
4319 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07004320 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004321 * @return Returns true if this view is a text editor, else false.
4322 */
4323 public boolean onCheckIsTextEditor() {
4324 return false;
4325 }
Romain Guy8506ab42009-06-11 17:35:47 -07004326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004327 /**
4328 * Create a new InputConnection for an InputMethod to interact
4329 * with the view. The default implementation returns null, since it doesn't
4330 * support input methods. You can override this to implement such support.
4331 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07004332 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004333 * <p>When implementing this, you probably also want to implement
4334 * {@link #onCheckIsTextEditor()} to indicate you will return a
4335 * non-null InputConnection.
4336 *
4337 * @param outAttrs Fill in with attribute information about the connection.
4338 */
4339 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
4340 return null;
4341 }
4342
4343 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004344 * Called by the {@link android.view.inputmethod.InputMethodManager}
4345 * when a view who is not the current
4346 * input connection target is trying to make a call on the manager. The
4347 * default implementation returns false; you can override this to return
4348 * true for certain views if you are performing InputConnection proxying
4349 * to them.
4350 * @param view The View that is making the InputMethodManager call.
4351 * @return Return true to allow the call, false to reject.
4352 */
4353 public boolean checkInputConnectionProxy(View view) {
4354 return false;
4355 }
Romain Guy8506ab42009-06-11 17:35:47 -07004356
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004357 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 * Show the context menu for this view. It is not safe to hold on to the
4359 * menu after returning from this method.
4360 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07004361 * You should normally not overload this method. Overload
4362 * {@link #onCreateContextMenu(ContextMenu)} or define an
4363 * {@link OnCreateContextMenuListener} to add items to the context menu.
4364 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004365 * @param menu The context menu to populate
4366 */
4367 public void createContextMenu(ContextMenu menu) {
4368 ContextMenuInfo menuInfo = getContextMenuInfo();
4369
4370 // Sets the current menu info so all items added to menu will have
4371 // my extra info set.
4372 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
4373
4374 onCreateContextMenu(menu);
4375 if (mOnCreateContextMenuListener != null) {
4376 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
4377 }
4378
4379 // Clear the extra information so subsequent items that aren't mine don't
4380 // have my extra info.
4381 ((MenuBuilder)menu).setCurrentMenuInfo(null);
4382
4383 if (mParent != null) {
4384 mParent.createContextMenu(menu);
4385 }
4386 }
4387
4388 /**
4389 * Views should implement this if they have extra information to associate
4390 * with the context menu. The return result is supplied as a parameter to
4391 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
4392 * callback.
4393 *
4394 * @return Extra information about the item for which the context menu
4395 * should be shown. This information will vary across different
4396 * subclasses of View.
4397 */
4398 protected ContextMenuInfo getContextMenuInfo() {
4399 return null;
4400 }
4401
4402 /**
4403 * Views should implement this if the view itself is going to add items to
4404 * the context menu.
4405 *
4406 * @param menu the context menu to populate
4407 */
4408 protected void onCreateContextMenu(ContextMenu menu) {
4409 }
4410
4411 /**
4412 * Implement this method to handle trackball motion events. The
4413 * <em>relative</em> movement of the trackball since the last event
4414 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
4415 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
4416 * that a movement of 1 corresponds to the user pressing one DPAD key (so
4417 * they will often be fractional values, representing the more fine-grained
4418 * movement information available from a trackball).
4419 *
4420 * @param event The motion event.
4421 * @return True if the event was handled, false otherwise.
4422 */
4423 public boolean onTrackballEvent(MotionEvent event) {
4424 return false;
4425 }
4426
4427 /**
4428 * Implement this method to handle touch screen motion events.
4429 *
4430 * @param event The motion event.
4431 * @return True if the event was handled, false otherwise.
4432 */
4433 public boolean onTouchEvent(MotionEvent event) {
4434 final int viewFlags = mViewFlags;
4435
4436 if ((viewFlags & ENABLED_MASK) == DISABLED) {
4437 // A disabled view that is clickable still consumes the touch
4438 // events, it just doesn't respond to them.
4439 return (((viewFlags & CLICKABLE) == CLICKABLE ||
4440 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
4441 }
4442
4443 if (mTouchDelegate != null) {
4444 if (mTouchDelegate.onTouchEvent(event)) {
4445 return true;
4446 }
4447 }
4448
4449 if (((viewFlags & CLICKABLE) == CLICKABLE ||
4450 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
4451 switch (event.getAction()) {
4452 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08004453 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
4454 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004455 // take focus if we don't have it already and we should in
4456 // touch mode.
4457 boolean focusTaken = false;
4458 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
4459 focusTaken = requestFocus();
4460 }
4461
4462 if (!mHasPerformedLongPress) {
4463 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05004464 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465
4466 // Only perform take click actions if we were in the pressed state
4467 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08004468 // Use a Runnable and post this rather than calling
4469 // performClick directly. This lets other visual state
4470 // of the view update before click actions start.
4471 if (mPerformClick == null) {
4472 mPerformClick = new PerformClick();
4473 }
4474 if (!post(mPerformClick)) {
4475 performClick();
4476 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004477 }
4478 }
4479
4480 if (mUnsetPressedState == null) {
4481 mUnsetPressedState = new UnsetPressedState();
4482 }
4483
Adam Powelle14579b2009-12-16 18:39:52 -08004484 if (prepressed) {
4485 mPrivateFlags |= PRESSED;
4486 refreshDrawableState();
4487 postDelayed(mUnsetPressedState,
4488 ViewConfiguration.getPressedStateDuration());
4489 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004490 // If the post failed, unpress right now
4491 mUnsetPressedState.run();
4492 }
Adam Powelle14579b2009-12-16 18:39:52 -08004493 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004494 }
4495 break;
4496
4497 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08004498 if (mPendingCheckForTap == null) {
4499 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004500 }
Adam Powelle14579b2009-12-16 18:39:52 -08004501 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08004502 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08004503 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004504 break;
4505
4506 case MotionEvent.ACTION_CANCEL:
4507 mPrivateFlags &= ~PRESSED;
4508 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08004509 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004510 break;
4511
4512 case MotionEvent.ACTION_MOVE:
4513 final int x = (int) event.getX();
4514 final int y = (int) event.getY();
4515
4516 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07004517 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004518 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08004519 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004520 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08004521 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05004522 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004523
4524 // Need to switch from pressed to not pressed
4525 mPrivateFlags &= ~PRESSED;
4526 refreshDrawableState();
4527 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004528 }
4529 break;
4530 }
4531 return true;
4532 }
4533
4534 return false;
4535 }
4536
4537 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05004538 * Remove the longpress detection timer.
4539 */
4540 private void removeLongPressCallback() {
4541 if (mPendingCheckForLongPress != null) {
4542 removeCallbacks(mPendingCheckForLongPress);
4543 }
4544 }
Adam Powelle14579b2009-12-16 18:39:52 -08004545
4546 /**
Romain Guya440b002010-02-24 15:57:54 -08004547 * Remove the prepress detection timer.
4548 */
4549 private void removeUnsetPressCallback() {
4550 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
4551 setPressed(false);
4552 removeCallbacks(mUnsetPressedState);
4553 }
4554 }
4555
4556 /**
Adam Powelle14579b2009-12-16 18:39:52 -08004557 * Remove the tap detection timer.
4558 */
4559 private void removeTapCallback() {
4560 if (mPendingCheckForTap != null) {
4561 mPrivateFlags &= ~PREPRESSED;
4562 removeCallbacks(mPendingCheckForTap);
4563 }
4564 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004565
4566 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004567 * Cancels a pending long press. Your subclass can use this if you
4568 * want the context menu to come up if the user presses and holds
4569 * at the same place, but you don't want it to come up if they press
4570 * and then move around enough to cause scrolling.
4571 */
4572 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004573 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08004574
4575 /*
4576 * The prepressed state handled by the tap callback is a display
4577 * construct, but the tap callback will post a long press callback
4578 * less its own timeout. Remove it here.
4579 */
4580 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004581 }
4582
4583 /**
4584 * Sets the TouchDelegate for this View.
4585 */
4586 public void setTouchDelegate(TouchDelegate delegate) {
4587 mTouchDelegate = delegate;
4588 }
4589
4590 /**
4591 * Gets the TouchDelegate for this View.
4592 */
4593 public TouchDelegate getTouchDelegate() {
4594 return mTouchDelegate;
4595 }
4596
4597 /**
4598 * Set flags controlling behavior of this view.
4599 *
4600 * @param flags Constant indicating the value which should be set
4601 * @param mask Constant indicating the bit range that should be changed
4602 */
4603 void setFlags(int flags, int mask) {
4604 int old = mViewFlags;
4605 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
4606
4607 int changed = mViewFlags ^ old;
4608 if (changed == 0) {
4609 return;
4610 }
4611 int privateFlags = mPrivateFlags;
4612
4613 /* Check if the FOCUSABLE bit has changed */
4614 if (((changed & FOCUSABLE_MASK) != 0) &&
4615 ((privateFlags & HAS_BOUNDS) !=0)) {
4616 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
4617 && ((privateFlags & FOCUSED) != 0)) {
4618 /* Give up focus if we are no longer focusable */
4619 clearFocus();
4620 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
4621 && ((privateFlags & FOCUSED) == 0)) {
4622 /*
4623 * Tell the view system that we are now available to take focus
4624 * if no one else already has it.
4625 */
4626 if (mParent != null) mParent.focusableViewAvailable(this);
4627 }
4628 }
4629
4630 if ((flags & VISIBILITY_MASK) == VISIBLE) {
4631 if ((changed & VISIBILITY_MASK) != 0) {
4632 /*
4633 * If this view is becoming visible, set the DRAWN flag so that
4634 * the next invalidate() will not be skipped.
4635 */
4636 mPrivateFlags |= DRAWN;
4637
4638 needGlobalAttributesUpdate(true);
4639
4640 // a view becoming visible is worth notifying the parent
4641 // about in case nothing has focus. even if this specific view
4642 // isn't focusable, it may contain something that is, so let
4643 // the root view try to give this focus if nothing else does.
4644 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
4645 mParent.focusableViewAvailable(this);
4646 }
4647 }
4648 }
4649
4650 /* Check if the GONE bit has changed */
4651 if ((changed & GONE) != 0) {
4652 needGlobalAttributesUpdate(false);
4653 requestLayout();
4654 invalidate();
4655
Romain Guyecd80ee2009-12-03 17:13:02 -08004656 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
4657 if (hasFocus()) clearFocus();
4658 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004659 }
4660 if (mAttachInfo != null) {
4661 mAttachInfo.mViewVisibilityChanged = true;
4662 }
4663 }
4664
4665 /* Check if the VISIBLE bit has changed */
4666 if ((changed & INVISIBLE) != 0) {
4667 needGlobalAttributesUpdate(false);
4668 invalidate();
4669
4670 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
4671 // root view becoming invisible shouldn't clear focus
4672 if (getRootView() != this) {
4673 clearFocus();
4674 }
4675 }
4676 if (mAttachInfo != null) {
4677 mAttachInfo.mViewVisibilityChanged = true;
4678 }
4679 }
4680
Adam Powell326d8082009-12-09 15:10:07 -08004681 if ((changed & VISIBILITY_MASK) != 0) {
4682 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
4683 }
4684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004685 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
4686 destroyDrawingCache();
4687 }
4688
4689 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
4690 destroyDrawingCache();
4691 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4692 }
4693
4694 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
4695 destroyDrawingCache();
4696 mPrivateFlags &= ~DRAWING_CACHE_VALID;
4697 }
4698
4699 if ((changed & DRAW_MASK) != 0) {
4700 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
4701 if (mBGDrawable != null) {
4702 mPrivateFlags &= ~SKIP_DRAW;
4703 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
4704 } else {
4705 mPrivateFlags |= SKIP_DRAW;
4706 }
4707 } else {
4708 mPrivateFlags &= ~SKIP_DRAW;
4709 }
4710 requestLayout();
4711 invalidate();
4712 }
4713
4714 if ((changed & KEEP_SCREEN_ON) != 0) {
4715 if (mParent != null) {
4716 mParent.recomputeViewAttributes(this);
4717 }
4718 }
4719 }
4720
4721 /**
4722 * Change the view's z order in the tree, so it's on top of other sibling
4723 * views
4724 */
4725 public void bringToFront() {
4726 if (mParent != null) {
4727 mParent.bringChildToFront(this);
4728 }
4729 }
4730
4731 /**
4732 * This is called in response to an internal scroll in this view (i.e., the
4733 * view scrolled its own contents). This is typically as a result of
4734 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
4735 * called.
4736 *
4737 * @param l Current horizontal scroll origin.
4738 * @param t Current vertical scroll origin.
4739 * @param oldl Previous horizontal scroll origin.
4740 * @param oldt Previous vertical scroll origin.
4741 */
4742 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
4743 mBackgroundSizeChanged = true;
4744
4745 final AttachInfo ai = mAttachInfo;
4746 if (ai != null) {
4747 ai.mViewScrollChanged = true;
4748 }
4749 }
4750
4751 /**
4752 * This is called during layout when the size of this view has changed. If
4753 * you were just added to the view hierarchy, you're called with the old
4754 * values of 0.
4755 *
4756 * @param w Current width of this view.
4757 * @param h Current height of this view.
4758 * @param oldw Old width of this view.
4759 * @param oldh Old height of this view.
4760 */
4761 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
4762 }
4763
4764 /**
4765 * Called by draw to draw the child views. This may be overridden
4766 * by derived classes to gain control just before its children are drawn
4767 * (but after its own view has been drawn).
4768 * @param canvas the canvas on which to draw the view
4769 */
4770 protected void dispatchDraw(Canvas canvas) {
4771 }
4772
4773 /**
4774 * Gets the parent of this view. Note that the parent is a
4775 * ViewParent and not necessarily a View.
4776 *
4777 * @return Parent of this view.
4778 */
4779 public final ViewParent getParent() {
4780 return mParent;
4781 }
4782
4783 /**
4784 * Return the scrolled left position of this view. This is the left edge of
4785 * the displayed part of your view. You do not need to draw any pixels
4786 * farther left, since those are outside of the frame of your view on
4787 * screen.
4788 *
4789 * @return The left edge of the displayed part of your view, in pixels.
4790 */
4791 public final int getScrollX() {
4792 return mScrollX;
4793 }
4794
4795 /**
4796 * Return the scrolled top position of this view. This is the top edge of
4797 * the displayed part of your view. You do not need to draw any pixels above
4798 * it, since those are outside of the frame of your view on screen.
4799 *
4800 * @return The top edge of the displayed part of your view, in pixels.
4801 */
4802 public final int getScrollY() {
4803 return mScrollY;
4804 }
4805
4806 /**
4807 * Return the width of the your view.
4808 *
4809 * @return The width of your view, in pixels.
4810 */
4811 @ViewDebug.ExportedProperty
4812 public final int getWidth() {
4813 return mRight - mLeft;
4814 }
4815
4816 /**
4817 * Return the height of your view.
4818 *
4819 * @return The height of your view, in pixels.
4820 */
4821 @ViewDebug.ExportedProperty
4822 public final int getHeight() {
4823 return mBottom - mTop;
4824 }
4825
4826 /**
4827 * Return the visible drawing bounds of your view. Fills in the output
4828 * rectangle with the values from getScrollX(), getScrollY(),
4829 * getWidth(), and getHeight().
4830 *
4831 * @param outRect The (scrolled) drawing bounds of the view.
4832 */
4833 public void getDrawingRect(Rect outRect) {
4834 outRect.left = mScrollX;
4835 outRect.top = mScrollY;
4836 outRect.right = mScrollX + (mRight - mLeft);
4837 outRect.bottom = mScrollY + (mBottom - mTop);
4838 }
4839
4840 /**
4841 * The width of this view as measured in the most recent call to measure().
4842 * This should be used during measurement and layout calculations only. Use
4843 * {@link #getWidth()} to see how wide a view is after layout.
4844 *
4845 * @return The measured width of this view.
4846 */
4847 public final int getMeasuredWidth() {
4848 return mMeasuredWidth;
4849 }
4850
4851 /**
4852 * The height of this view as measured in the most recent call to measure().
4853 * This should be used during measurement and layout calculations only. Use
4854 * {@link #getHeight()} to see how tall a view is after layout.
4855 *
4856 * @return The measured height of this view.
4857 */
4858 public final int getMeasuredHeight() {
4859 return mMeasuredHeight;
4860 }
4861
4862 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07004863 * The transform matrix of this view, which is calculated based on the current
4864 * roation, scale, and pivot properties.
4865 *
4866 * @see #getRotation()
4867 * @see #getScaleX()
4868 * @see #getScaleY()
4869 * @see #getPivotX()
4870 * @see #getPivotY()
4871 * @return The current transform matrix for the view
4872 */
4873 public Matrix getMatrix() {
Romain Guy33e72ae2010-07-17 12:40:29 -07004874 hasIdentityMatrix();
4875 return mMatrix;
4876 }
4877
4878 /**
4879 * Recomputes the transform matrix if necessary.
4880 *
4881 * @return True if the transform matrix is the identity matrix, false otherwise.
4882 */
4883 boolean hasIdentityMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07004884 if (mMatrixDirty) {
4885 // transform-related properties have changed since the last time someone
4886 // asked for the matrix; recalculate it with the current values
4887 mMatrix.reset();
4888 mMatrix.setRotate(mRotation, mPivotX, mPivotY);
4889 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
4890 mMatrixDirty = false;
4891 mMatrixIsIdentity = mMatrix.isIdentity();
4892 mInverseMatrixDirty = true;
4893 }
Romain Guy33e72ae2010-07-17 12:40:29 -07004894 return mMatrixIsIdentity;
Chet Haasec3aa3612010-06-17 08:50:37 -07004895 }
4896
4897 /**
4898 * Utility method to retrieve the inverse of the current mMatrix property.
4899 * We cache the matrix to avoid recalculating it when transform properties
4900 * have not changed.
4901 *
4902 * @return The inverse of the current matrix of this view.
4903 */
4904 Matrix getInverseMatrix() {
4905 if (mInverseMatrixDirty) {
4906 if (mInverseMatrix == null) {
4907 mInverseMatrix = new Matrix();
4908 }
4909 mMatrix.invert(mInverseMatrix);
4910 mInverseMatrixDirty = false;
4911 }
4912 return mInverseMatrix;
4913 }
4914
4915 /**
4916 * The degrees that the view is rotated around the pivot point.
4917 *
4918 * @see #getPivotX()
4919 * @see #getPivotY()
4920 * @return The degrees of rotation.
4921 */
4922 public float getRotation() {
4923 return mRotation;
4924 }
4925
4926 /**
4927 * Sets the degrees that the view is rotated around the pivot point.
4928 *
4929 * @param rotation The degrees of rotation.
4930 * @see #getPivotX()
4931 * @see #getPivotY()
4932 */
4933 public void setRotation(float rotation) {
4934 if (mRotation != rotation) {
4935 // Double-invalidation is necessary to capture view's old and new areas
4936 invalidate();
4937 mRotation = rotation;
4938 mMatrixDirty = true;
4939 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
4940 invalidate();
4941 }
4942 }
4943
4944 /**
4945 * The amount that the view is scaled in x around the pivot point, as a proportion of
4946 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
4947 *
4948 * @default 1.0f
4949 * @see #getPivotX()
4950 * @see #getPivotY()
4951 * @return The scaling factor.
4952 */
4953 public float getScaleX() {
4954 return mScaleX;
4955 }
4956
4957 /**
4958 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
4959 * the view's unscaled width. A value of 1 means that no scaling is applied.
4960 *
4961 * @param scaleX The scaling factor.
4962 * @see #getPivotX()
4963 * @see #getPivotY()
4964 */
4965 public void setScaleX(float scaleX) {
4966 if (mScaleX != scaleX) {
4967 // Double-invalidation is necessary to capture view's old and new areas
4968 invalidate();
4969 mScaleX = scaleX;
4970 mMatrixDirty = true;
4971 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
4972 invalidate();
4973 }
4974 }
4975
4976 /**
4977 * The amount that the view is scaled in y around the pivot point, as a proportion of
4978 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
4979 *
4980 * @default 1.0f
4981 * @see #getPivotX()
4982 * @see #getPivotY()
4983 * @return The scaling factor.
4984 */
4985 public float getScaleY() {
4986 return mScaleY;
4987 }
4988
4989 /**
4990 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
4991 * the view's unscaled width. A value of 1 means that no scaling is applied.
4992 *
4993 * @param scaleY The scaling factor.
4994 * @see #getPivotX()
4995 * @see #getPivotY()
4996 */
4997 public void setScaleY(float scaleY) {
4998 if (mScaleY != scaleY) {
4999 // Double-invalidation is necessary to capture view's old and new areas
5000 invalidate();
5001 mScaleY = scaleY;
5002 mMatrixDirty = true;
5003 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5004 invalidate();
5005 }
5006 }
5007
5008 /**
5009 * The x location of the point around which the view is {@link #setRotation(float) rotated}
5010 * and {@link #setScaleX(float) scaled}.
5011 *
5012 * @see #getRotation()
5013 * @see #getScaleX()
5014 * @see #getScaleY()
5015 * @see #getPivotY()
5016 * @return The x location of the pivot point.
5017 */
5018 public float getPivotX() {
5019 return mPivotX;
5020 }
5021
5022 /**
5023 * Sets the x location of the point around which the view is
5024 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
5025 *
5026 * @param pivotX The x location of the pivot point.
5027 * @see #getRotation()
5028 * @see #getScaleX()
5029 * @see #getScaleY()
5030 * @see #getPivotY()
5031 */
5032 public void setPivotX(float pivotX) {
5033 if (mPivotX != pivotX) {
5034 // Double-invalidation is necessary to capture view's old and new areas
5035 invalidate();
5036 mPivotX = pivotX;
5037 mMatrixDirty = true;
5038 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5039 invalidate();
5040 }
5041 }
5042
5043 /**
5044 * The y location of the point around which the view is {@link #setRotation(float) rotated}
5045 * and {@link #setScaleY(float) scaled}.
5046 *
5047 * @see #getRotation()
5048 * @see #getScaleX()
5049 * @see #getScaleY()
5050 * @see #getPivotY()
5051 * @return The y location of the pivot point.
5052 */
5053 public float getPivotY() {
5054 return mPivotY;
5055 }
5056
5057 /**
5058 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
5059 * and {@link #setScaleY(float) scaled}.
5060 *
5061 * @param pivotY The y location of the pivot point.
5062 * @see #getRotation()
5063 * @see #getScaleX()
5064 * @see #getScaleY()
5065 * @see #getPivotY()
5066 */
5067 public void setPivotY(float pivotY) {
5068 if (mPivotY != pivotY) {
5069 // Double-invalidation is necessary to capture view's old and new areas
5070 invalidate();
5071 mPivotY = pivotY;
5072 mMatrixDirty = true;
5073 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5074 invalidate();
5075 }
5076 }
5077
5078 /**
5079 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
5080 * completely transparent and 1 means the view is completely opaque.
5081 *
5082 * @default 1.0f
5083 * @return The opacity of the view.
5084 */
5085 public float getAlpha() {
5086 return mAlpha;
5087 }
5088
5089 /**
5090 * Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
5091 * completely transparent and 1 means the view is completely opaque.
5092 *
5093 * @param alpha The opacity of the view.
5094 */
5095 public void setAlpha(float alpha) {
5096 mAlpha = alpha;
5097 invalidate();
5098 }
5099
5100 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005101 * Top position of this view relative to its parent.
5102 *
5103 * @return The top of this view, in pixels.
5104 */
5105 @ViewDebug.CapturedViewProperty
5106 public final int getTop() {
5107 return mTop;
5108 }
5109
5110 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005111 * Sets the top position of this view relative to its parent.
5112 *
5113 * @param top The top of this view, in pixels.
5114 */
5115 public final void setTop(int top) {
5116 if (top != mTop) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005117 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005118 final ViewParent p = mParent;
5119 if (p != null && mAttachInfo != null) {
5120 final int[] location = mAttachInfo.mInvalidateChildLocation;
5121 final Rect r = mAttachInfo.mTmpInvalRect;
5122 int minTop = Math.min(mTop, top);
5123 location[0] = mLeft;
5124 location[1] = minTop;
5125 r.set(0, 0, mRight - mLeft, mBottom - minTop);
5126 p.invalidateChildInParent(location, r);
5127 }
5128 } else {
5129 // Double-invalidation is necessary to capture view's old and new areas
5130 invalidate();
5131 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005132
Chet Haasec3aa3612010-06-17 08:50:37 -07005133 mTop = top;
Romain Guy33e72ae2010-07-17 12:40:29 -07005134
Chet Haasec3aa3612010-06-17 08:50:37 -07005135 if (!mMatrixIsIdentity) {
5136 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5137 invalidate();
5138 }
5139 }
5140 }
5141
5142 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005143 * Bottom position of this view relative to its parent.
5144 *
5145 * @return The bottom of this view, in pixels.
5146 */
5147 @ViewDebug.CapturedViewProperty
5148 public final int getBottom() {
5149 return mBottom;
5150 }
5151
5152 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005153 * Sets the bottom position of this view relative to its parent.
5154 *
5155 * @param bottom The bottom of this view, in pixels.
5156 */
5157 public final void setBottom(int bottom) {
5158 if (bottom != mBottom) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005159 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005160 final ViewParent p = mParent;
5161 if (p != null && mAttachInfo != null) {
5162 final int[] location = mAttachInfo.mInvalidateChildLocation;
5163 final Rect r = mAttachInfo.mTmpInvalRect;
5164 int maxBottom = Math.max(mBottom, bottom);
5165 location[0] = mLeft;
5166 location[1] = mTop;
5167 r.set(0, 0, mRight - mLeft, maxBottom - mTop);
5168 p.invalidateChildInParent(location, r);
5169 }
5170 } else {
5171 // Double-invalidation is necessary to capture view's old and new areas
5172 invalidate();
5173 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005174
Chet Haasec3aa3612010-06-17 08:50:37 -07005175 mBottom = bottom;
Romain Guy33e72ae2010-07-17 12:40:29 -07005176
Chet Haasec3aa3612010-06-17 08:50:37 -07005177 if (!mMatrixIsIdentity) {
5178 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5179 invalidate();
5180 }
5181 }
5182 }
5183
5184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005185 * Left position of this view relative to its parent.
5186 *
5187 * @return The left edge of this view, in pixels.
5188 */
5189 @ViewDebug.CapturedViewProperty
5190 public final int getLeft() {
5191 return mLeft;
5192 }
5193
5194 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005195 * Sets the left position of this view relative to its parent.
5196 *
5197 * @param left The bottom of this view, in pixels.
5198 */
5199 public final void setLeft(int left) {
5200 if (left != mLeft) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005201 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005202 final ViewParent p = mParent;
5203 if (p != null && mAttachInfo != null) {
5204 final int[] location = mAttachInfo.mInvalidateChildLocation;
5205 final Rect r = mAttachInfo.mTmpInvalRect;
5206 int minLeft = Math.min(mLeft, left);
5207 location[0] = minLeft;
5208 location[1] = mTop;
5209 r.set(0, 0, mRight - minLeft, mBottom - mTop);
5210 p.invalidateChildInParent(location, r);
5211 }
5212 } else {
5213 // Double-invalidation is necessary to capture view's old and new areas
5214 invalidate();
5215 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005216
Chet Haasec3aa3612010-06-17 08:50:37 -07005217 mLeft = left;
Romain Guy33e72ae2010-07-17 12:40:29 -07005218
Chet Haasec3aa3612010-06-17 08:50:37 -07005219 if (!mMatrixIsIdentity) {
5220 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5221 invalidate();
5222 }
5223 }
5224 }
5225
5226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 * Right position of this view relative to its parent.
5228 *
5229 * @return The right edge of this view, in pixels.
5230 */
5231 @ViewDebug.CapturedViewProperty
5232 public final int getRight() {
5233 return mRight;
5234 }
5235
5236 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005237 * Sets the right position of this view relative to its parent.
5238 *
5239 * @param right The bottom of this view, in pixels.
5240 */
5241 public final void setRight(int right) {
5242 if (right != mRight) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005243 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005244 final ViewParent p = mParent;
5245 if (p != null && mAttachInfo != null) {
5246 final int[] location = mAttachInfo.mInvalidateChildLocation;
5247 final Rect r = mAttachInfo.mTmpInvalRect;
5248 int maxRight = Math.max(mRight, right);
5249 location[0] = mLeft;
5250 location[1] = mTop;
5251 r.set(0, 0, maxRight - mLeft, mBottom - mTop);
5252 p.invalidateChildInParent(location, r);
5253 }
5254 } else {
5255 // Double-invalidation is necessary to capture view's old and new areas
5256 invalidate();
5257 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005258
Chet Haasec3aa3612010-06-17 08:50:37 -07005259 mRight = right;
Romain Guy33e72ae2010-07-17 12:40:29 -07005260
Chet Haasec3aa3612010-06-17 08:50:37 -07005261 if (!mMatrixIsIdentity) {
5262 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5263 invalidate();
5264 }
5265 }
5266 }
5267
5268 /**
5269 * The horizontal location of this view relative to its parent. This value is equivalent to the
5270 * {@link #getLeft() left} property.
5271 *
5272 * @return The horizontal position of this view, in pixels.
5273 */
5274 public int getX() {
5275 return mLeft;
5276 }
5277
5278 /**
5279 * Sets the horizontal location of this view relative to its parent. Setting this value will
5280 * affect both the {@link #setLeft(int) left} and {@link #setRight(int) right} properties
5281 * of this view.
5282 *
5283 * @param x The horizontal position of this view, in pixels.
5284 */
5285 public void setX(int x) {
5286 offsetLeftAndRight(x - mLeft);
5287 }
5288
5289 /**
5290 * The vertical location of this view relative to its parent. This value is equivalent to the
5291 * {@link #getTop() left} property.
5292 *
5293 * @return The vertical position of this view, in pixels.
5294 */
5295 public int getY() {
5296 return mTop;
5297 }
5298
5299 /**
5300 * Sets the vertical location of this view relative to its parent. Setting this value will
5301 * affect both the {@link #setTop(int) left} and {@link #setBottom(int) right} properties
5302 * of this view.
5303 *
5304 * @param y The vertical position of this view, in pixels.
5305 */
5306 public void setY(int y) {
5307 offsetTopAndBottom(y - mTop);
5308 }
5309
5310 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005311 * Hit rectangle in parent's coordinates
5312 *
5313 * @param outRect The hit rectangle of the view.
5314 */
5315 public void getHitRect(Rect outRect) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005316 if (hasIdentityMatrix() || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005317 outRect.set(mLeft, mTop, mRight, mBottom);
5318 } else {
Adam Powellb5de9f32010-07-17 01:00:53 -07005319 Matrix m = getMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005320 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07005321 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Chet Haasec3aa3612010-06-17 08:50:37 -07005322 m.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07005323 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
5324 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07005325 }
5326 }
5327
5328 /**
5329 * This method detects whether the given event is inside the view and, if so,
5330 * handles it via the dispatchEvent(MotionEvent) method.
5331 *
5332 * @param ev The event that is being dispatched.
5333 * @param parentX The x location of the event in the parent's coordinates.
5334 * @param parentY The y location of the event in the parent's coordinates.
5335 * @return true if the event was inside this view, false otherwise.
5336 */
5337 boolean dispatchTouchEvent(MotionEvent ev, float parentX, float parentY) {
5338 float localX = parentX - mLeft;
5339 float localY = parentY - mTop;
Romain Guy33e72ae2010-07-17 12:40:29 -07005340 if (!hasIdentityMatrix() && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005341 // non-identity matrix: transform the point into the view's coordinates
5342 final float[] localXY = mAttachInfo.mTmpTransformLocation;
5343 localXY[0] = localX;
5344 localXY[1] = localY;
5345 getInverseMatrix().mapPoints(localXY);
5346 localX = localXY[0];
5347 localY = localXY[1];
5348 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005349 if (localX >= 0 && localY >= 0 && localX < (mRight - mLeft) && localY < (mBottom - mTop)) {
Adam Powellb5de9f32010-07-17 01:00:53 -07005350 // It would be safer to clone the event here but we don't for performance.
5351 // There are many subtle interactions in touch event dispatch; change at your own risk.
5352 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Chet Haasec3aa3612010-06-17 08:50:37 -07005353 ev.setLocation(localX, localY);
5354 return dispatchTouchEvent(ev);
5355 }
5356 return false;
5357 }
5358
5359 /**
5360 * Utility method to determine whether the given point, in local coordinates,
5361 * is inside the view, where the area of the view is expanded by the slop factor.
5362 * This method is called while processing touch-move events to determine if the event
5363 * is still within the view.
5364 */
5365 private boolean pointInView(float localX, float localY, float slop) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005366 if (!hasIdentityMatrix() && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005367 // non-identity matrix: transform the point into the view's coordinates
5368 final float[] localXY = mAttachInfo.mTmpTransformLocation;
5369 localXY[0] = localX;
5370 localXY[1] = localY;
5371 getInverseMatrix().mapPoints(localXY);
5372 localX = localXY[0];
5373 localY = localXY[1];
5374 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005375 return localX > -slop && localY > -slop && localX < ((mRight - mLeft) + slop) &&
5376 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
5378
5379 /**
5380 * When a view has focus and the user navigates away from it, the next view is searched for
5381 * starting from the rectangle filled in by this method.
5382 *
5383 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
5384 * view maintains some idea of internal selection, such as a cursor, or a selected row
5385 * or column, you should override this method and fill in a more specific rectangle.
5386 *
5387 * @param r The rectangle to fill in, in this view's coordinates.
5388 */
5389 public void getFocusedRect(Rect r) {
5390 getDrawingRect(r);
5391 }
5392
5393 /**
5394 * If some part of this view is not clipped by any of its parents, then
5395 * return that area in r in global (root) coordinates. To convert r to local
5396 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
5397 * -globalOffset.y)) If the view is completely clipped or translated out,
5398 * return false.
5399 *
5400 * @param r If true is returned, r holds the global coordinates of the
5401 * visible portion of this view.
5402 * @param globalOffset If true is returned, globalOffset holds the dx,dy
5403 * between this view and its root. globalOffet may be null.
5404 * @return true if r is non-empty (i.e. part of the view is visible at the
5405 * root level.
5406 */
5407 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
5408 int width = mRight - mLeft;
5409 int height = mBottom - mTop;
5410 if (width > 0 && height > 0) {
5411 r.set(0, 0, width, height);
5412 if (globalOffset != null) {
5413 globalOffset.set(-mScrollX, -mScrollY);
5414 }
5415 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
5416 }
5417 return false;
5418 }
5419
5420 public final boolean getGlobalVisibleRect(Rect r) {
5421 return getGlobalVisibleRect(r, null);
5422 }
5423
5424 public final boolean getLocalVisibleRect(Rect r) {
5425 Point offset = new Point();
5426 if (getGlobalVisibleRect(r, offset)) {
5427 r.offset(-offset.x, -offset.y); // make r local
5428 return true;
5429 }
5430 return false;
5431 }
5432
5433 /**
5434 * Offset this view's vertical location by the specified number of pixels.
5435 *
5436 * @param offset the number of pixels to offset the view by
5437 */
5438 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005439 if (offset != 0) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005440 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005441 final ViewParent p = mParent;
5442 if (p != null && mAttachInfo != null) {
5443 final int[] location = mAttachInfo.mInvalidateChildLocation;
5444 final Rect r = mAttachInfo.mTmpInvalRect;
5445 int minTop = offset < 0 ? mTop + offset : mTop;
5446 int maxBottom = offset < 0 ? mBottom : mBottom + offset;
5447 location[0] = mLeft;
5448 location[1] = minTop;
5449 r.set(0, 0, mRight - mLeft, maxBottom - minTop);
5450 p.invalidateChildInParent(location, r);
5451 }
5452 } else {
5453 invalidate();
5454 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005455
Chet Haasec3aa3612010-06-17 08:50:37 -07005456 mTop += offset;
5457 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07005458
Chet Haasec3aa3612010-06-17 08:50:37 -07005459 if (!mMatrixIsIdentity) {
5460 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5461 invalidate();
5462 }
5463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005464 }
5465
5466 /**
5467 * Offset this view's horizontal location by the specified amount of pixels.
5468 *
5469 * @param offset the numer of pixels to offset the view by
5470 */
5471 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005472 if (offset != 0) {
Romain Guy33e72ae2010-07-17 12:40:29 -07005473 if (hasIdentityMatrix()) {
Chet Haasec3aa3612010-06-17 08:50:37 -07005474 final ViewParent p = mParent;
5475 if (p != null && mAttachInfo != null) {
5476 final int[] location = mAttachInfo.mInvalidateChildLocation;
5477 final Rect r = mAttachInfo.mTmpInvalRect;
5478 int minLeft = offset < 0 ? mLeft + offset : mLeft;
5479 int maxRight = offset < 0 ? mRight : mRight + offset;
5480 location[0] = minLeft;
5481 location[1] = mTop;
5482 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
5483 p.invalidateChildInParent(location, r);
5484 }
5485 } else {
5486 invalidate();
5487 }
Romain Guy33e72ae2010-07-17 12:40:29 -07005488
Chet Haasec3aa3612010-06-17 08:50:37 -07005489 mLeft += offset;
5490 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07005491
Chet Haasec3aa3612010-06-17 08:50:37 -07005492 if (!mMatrixIsIdentity) {
5493 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
5494 invalidate();
5495 }
5496 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 }
5498
5499 /**
5500 * Get the LayoutParams associated with this view. All views should have
5501 * layout parameters. These supply parameters to the <i>parent</i> of this
5502 * view specifying how it should be arranged. There are many subclasses of
5503 * ViewGroup.LayoutParams, and these correspond to the different subclasses
5504 * of ViewGroup that are responsible for arranging their children.
5505 * @return The LayoutParams associated with this view
5506 */
5507 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
5508 public ViewGroup.LayoutParams getLayoutParams() {
5509 return mLayoutParams;
5510 }
5511
5512 /**
5513 * Set the layout parameters associated with this view. These supply
5514 * parameters to the <i>parent</i> of this view specifying how it should be
5515 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
5516 * correspond to the different subclasses of ViewGroup that are responsible
5517 * for arranging their children.
5518 *
5519 * @param params the layout parameters for this view
5520 */
5521 public void setLayoutParams(ViewGroup.LayoutParams params) {
5522 if (params == null) {
5523 throw new NullPointerException("params == null");
5524 }
5525 mLayoutParams = params;
5526 requestLayout();
5527 }
5528
5529 /**
5530 * Set the scrolled position of your view. This will cause a call to
5531 * {@link #onScrollChanged(int, int, int, int)} and the view will be
5532 * invalidated.
5533 * @param x the x position to scroll to
5534 * @param y the y position to scroll to
5535 */
5536 public void scrollTo(int x, int y) {
5537 if (mScrollX != x || mScrollY != y) {
5538 int oldX = mScrollX;
5539 int oldY = mScrollY;
5540 mScrollX = x;
5541 mScrollY = y;
5542 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07005543 if (!awakenScrollBars()) {
5544 invalidate();
5545 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005546 }
5547 }
5548
5549 /**
5550 * Move the scrolled position of your view. This will cause a call to
5551 * {@link #onScrollChanged(int, int, int, int)} and the view will be
5552 * invalidated.
5553 * @param x the amount of pixels to scroll by horizontally
5554 * @param y the amount of pixels to scroll by vertically
5555 */
5556 public void scrollBy(int x, int y) {
5557 scrollTo(mScrollX + x, mScrollY + y);
5558 }
5559
5560 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07005561 * <p>Trigger the scrollbars to draw. When invoked this method starts an
5562 * animation to fade the scrollbars out after a default delay. If a subclass
5563 * provides animated scrolling, the start delay should equal the duration
5564 * of the scrolling animation.</p>
5565 *
5566 * <p>The animation starts only if at least one of the scrollbars is
5567 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
5568 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
5569 * this method returns true, and false otherwise. If the animation is
5570 * started, this method calls {@link #invalidate()}; in that case the
5571 * caller should not call {@link #invalidate()}.</p>
5572 *
5573 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07005574 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07005575 *
5576 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
5577 * and {@link #scrollTo(int, int)}.</p>
5578 *
5579 * @return true if the animation is played, false otherwise
5580 *
5581 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07005582 * @see #scrollBy(int, int)
5583 * @see #scrollTo(int, int)
5584 * @see #isHorizontalScrollBarEnabled()
5585 * @see #isVerticalScrollBarEnabled()
5586 * @see #setHorizontalScrollBarEnabled(boolean)
5587 * @see #setVerticalScrollBarEnabled(boolean)
5588 */
5589 protected boolean awakenScrollBars() {
5590 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07005591 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07005592 }
5593
5594 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07005595 * Trigger the scrollbars to draw.
5596 * This method differs from awakenScrollBars() only in its default duration.
5597 * initialAwakenScrollBars() will show the scroll bars for longer than
5598 * usual to give the user more of a chance to notice them.
5599 *
5600 * @return true if the animation is played, false otherwise.
5601 */
5602 private boolean initialAwakenScrollBars() {
5603 return mScrollCache != null &&
5604 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
5605 }
5606
5607 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07005608 * <p>
5609 * Trigger the scrollbars to draw. When invoked this method starts an
5610 * animation to fade the scrollbars out after a fixed delay. If a subclass
5611 * provides animated scrolling, the start delay should equal the duration of
5612 * the scrolling animation.
5613 * </p>
5614 *
5615 * <p>
5616 * The animation starts only if at least one of the scrollbars is enabled,
5617 * as specified by {@link #isHorizontalScrollBarEnabled()} and
5618 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
5619 * this method returns true, and false otherwise. If the animation is
5620 * started, this method calls {@link #invalidate()}; in that case the caller
5621 * should not call {@link #invalidate()}.
5622 * </p>
5623 *
5624 * <p>
5625 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07005626 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07005627 * </p>
5628 *
5629 * @param startDelay the delay, in milliseconds, after which the animation
5630 * should start; when the delay is 0, the animation starts
5631 * immediately
5632 * @return true if the animation is played, false otherwise
5633 *
Mike Cleronf116bf82009-09-27 19:14:12 -07005634 * @see #scrollBy(int, int)
5635 * @see #scrollTo(int, int)
5636 * @see #isHorizontalScrollBarEnabled()
5637 * @see #isVerticalScrollBarEnabled()
5638 * @see #setHorizontalScrollBarEnabled(boolean)
5639 * @see #setVerticalScrollBarEnabled(boolean)
5640 */
5641 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07005642 return awakenScrollBars(startDelay, true);
5643 }
5644
5645 /**
5646 * <p>
5647 * Trigger the scrollbars to draw. When invoked this method starts an
5648 * animation to fade the scrollbars out after a fixed delay. If a subclass
5649 * provides animated scrolling, the start delay should equal the duration of
5650 * the scrolling animation.
5651 * </p>
5652 *
5653 * <p>
5654 * The animation starts only if at least one of the scrollbars is enabled,
5655 * as specified by {@link #isHorizontalScrollBarEnabled()} and
5656 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
5657 * this method returns true, and false otherwise. If the animation is
5658 * started, this method calls {@link #invalidate()} if the invalidate parameter
5659 * is set to true; in that case the caller
5660 * should not call {@link #invalidate()}.
5661 * </p>
5662 *
5663 * <p>
5664 * This method should be invoked everytime a subclass directly updates the
5665 * scroll parameters.
5666 * </p>
5667 *
5668 * @param startDelay the delay, in milliseconds, after which the animation
5669 * should start; when the delay is 0, the animation starts
5670 * immediately
5671 *
5672 * @param invalidate Wheter this method should call invalidate
5673 *
5674 * @return true if the animation is played, false otherwise
5675 *
5676 * @see #scrollBy(int, int)
5677 * @see #scrollTo(int, int)
5678 * @see #isHorizontalScrollBarEnabled()
5679 * @see #isVerticalScrollBarEnabled()
5680 * @see #setHorizontalScrollBarEnabled(boolean)
5681 * @see #setVerticalScrollBarEnabled(boolean)
5682 */
5683 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07005684 final ScrollabilityCache scrollCache = mScrollCache;
5685
5686 if (scrollCache == null || !scrollCache.fadeScrollBars) {
5687 return false;
5688 }
5689
5690 if (scrollCache.scrollBar == null) {
5691 scrollCache.scrollBar = new ScrollBarDrawable();
5692 }
5693
5694 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
5695
Mike Cleron290947b2009-09-29 18:34:32 -07005696 if (invalidate) {
5697 // Invalidate to show the scrollbars
5698 invalidate();
5699 }
Mike Cleronf116bf82009-09-27 19:14:12 -07005700
5701 if (scrollCache.state == ScrollabilityCache.OFF) {
5702 // FIXME: this is copied from WindowManagerService.
5703 // We should get this value from the system when it
5704 // is possible to do so.
5705 final int KEY_REPEAT_FIRST_DELAY = 750;
5706 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
5707 }
5708
5709 // Tell mScrollCache when we should start fading. This may
5710 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07005711 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07005712 scrollCache.fadeStartTime = fadeStartTime;
5713 scrollCache.state = ScrollabilityCache.ON;
5714
5715 // Schedule our fader to run, unscheduling any old ones first
5716 if (mAttachInfo != null) {
5717 mAttachInfo.mHandler.removeCallbacks(scrollCache);
5718 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
5719 }
5720
5721 return true;
5722 }
5723
5724 return false;
5725 }
5726
5727 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 * Mark the the area defined by dirty as needing to be drawn. If the view is
5729 * visible, {@link #onDraw} will be called at some point in the future.
5730 * This must be called from a UI thread. To call from a non-UI thread, call
5731 * {@link #postInvalidate()}.
5732 *
5733 * WARNING: This method is destructive to dirty.
5734 * @param dirty the rectangle representing the bounds of the dirty region
5735 */
5736 public void invalidate(Rect dirty) {
5737 if (ViewDebug.TRACE_HIERARCHY) {
5738 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
5739 }
5740
5741 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
5742 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5743 final ViewParent p = mParent;
5744 final AttachInfo ai = mAttachInfo;
5745 if (p != null && ai != null) {
5746 final int scrollX = mScrollX;
5747 final int scrollY = mScrollY;
5748 final Rect r = ai.mTmpInvalRect;
5749 r.set(dirty.left - scrollX, dirty.top - scrollY,
5750 dirty.right - scrollX, dirty.bottom - scrollY);
5751 mParent.invalidateChild(this, r);
5752 }
5753 }
5754 }
5755
5756 /**
5757 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
5758 * The coordinates of the dirty rect are relative to the view.
5759 * If the view is visible, {@link #onDraw} will be called at some point
5760 * in the future. This must be called from a UI thread. To call
5761 * from a non-UI thread, call {@link #postInvalidate()}.
5762 * @param l the left position of the dirty region
5763 * @param t the top position of the dirty region
5764 * @param r the right position of the dirty region
5765 * @param b the bottom position of the dirty region
5766 */
5767 public void invalidate(int l, int t, int r, int b) {
5768 if (ViewDebug.TRACE_HIERARCHY) {
5769 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
5770 }
5771
5772 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
5773 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5774 final ViewParent p = mParent;
5775 final AttachInfo ai = mAttachInfo;
5776 if (p != null && ai != null && l < r && t < b) {
5777 final int scrollX = mScrollX;
5778 final int scrollY = mScrollY;
5779 final Rect tmpr = ai.mTmpInvalRect;
5780 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
5781 p.invalidateChild(this, tmpr);
5782 }
5783 }
5784 }
5785
5786 /**
5787 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
5788 * be called at some point in the future. This must be called from a
5789 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
5790 */
5791 public void invalidate() {
5792 if (ViewDebug.TRACE_HIERARCHY) {
5793 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
5794 }
5795
5796 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS)) {
5797 mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID;
5798 final ViewParent p = mParent;
5799 final AttachInfo ai = mAttachInfo;
5800 if (p != null && ai != null) {
5801 final Rect r = ai.mTmpInvalRect;
5802 r.set(0, 0, mRight - mLeft, mBottom - mTop);
5803 // Don't call invalidate -- we don't want to internally scroll
5804 // our own bounds
5805 p.invalidateChild(this, r);
5806 }
5807 }
5808 }
5809
5810 /**
Romain Guy24443ea2009-05-11 11:56:30 -07005811 * Indicates whether this View is opaque. An opaque View guarantees that it will
5812 * draw all the pixels overlapping its bounds using a fully opaque color.
5813 *
5814 * Subclasses of View should override this method whenever possible to indicate
5815 * whether an instance is opaque. Opaque Views are treated in a special way by
5816 * the View hierarchy, possibly allowing it to perform optimizations during
5817 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07005818 *
Romain Guy24443ea2009-05-11 11:56:30 -07005819 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07005820 */
Romain Guy83b21072009-05-12 10:54:51 -07005821 @ViewDebug.ExportedProperty
Romain Guy24443ea2009-05-11 11:56:30 -07005822 public boolean isOpaque() {
Romain Guy8f1344f52009-05-15 16:03:59 -07005823 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK;
5824 }
5825
5826 private void computeOpaqueFlags() {
5827 // Opaque if:
5828 // - Has a background
5829 // - Background is opaque
5830 // - Doesn't have scrollbars or scrollbars are inside overlay
5831
5832 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
5833 mPrivateFlags |= OPAQUE_BACKGROUND;
5834 } else {
5835 mPrivateFlags &= ~OPAQUE_BACKGROUND;
5836 }
5837
5838 final int flags = mViewFlags;
5839 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
5840 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
5841 mPrivateFlags |= OPAQUE_SCROLLBARS;
5842 } else {
5843 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
5844 }
5845 }
5846
5847 /**
5848 * @hide
5849 */
5850 protected boolean hasOpaqueScrollbars() {
5851 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07005852 }
5853
5854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 * @return A handler associated with the thread running the View. This
5856 * handler can be used to pump events in the UI events queue.
5857 */
5858 public Handler getHandler() {
5859 if (mAttachInfo != null) {
5860 return mAttachInfo.mHandler;
5861 }
5862 return null;
5863 }
5864
5865 /**
5866 * Causes the Runnable to be added to the message queue.
5867 * The runnable will be run on the user interface thread.
5868 *
5869 * @param action The Runnable that will be executed.
5870 *
5871 * @return Returns true if the Runnable was successfully placed in to the
5872 * message queue. Returns false on failure, usually because the
5873 * looper processing the message queue is exiting.
5874 */
5875 public boolean post(Runnable action) {
5876 Handler handler;
5877 if (mAttachInfo != null) {
5878 handler = mAttachInfo.mHandler;
5879 } else {
5880 // Assume that post will succeed later
5881 ViewRoot.getRunQueue().post(action);
5882 return true;
5883 }
5884
5885 return handler.post(action);
5886 }
5887
5888 /**
5889 * Causes the Runnable to be added to the message queue, to be run
5890 * after the specified amount of time elapses.
5891 * The runnable will be run on the user interface thread.
5892 *
5893 * @param action The Runnable that will be executed.
5894 * @param delayMillis The delay (in milliseconds) until the Runnable
5895 * will be executed.
5896 *
5897 * @return true if the Runnable was successfully placed in to the
5898 * message queue. Returns false on failure, usually because the
5899 * looper processing the message queue is exiting. Note that a
5900 * result of true does not mean the Runnable will be processed --
5901 * if the looper is quit before the delivery time of the message
5902 * occurs then the message will be dropped.
5903 */
5904 public boolean postDelayed(Runnable action, long delayMillis) {
5905 Handler handler;
5906 if (mAttachInfo != null) {
5907 handler = mAttachInfo.mHandler;
5908 } else {
5909 // Assume that post will succeed later
5910 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
5911 return true;
5912 }
5913
5914 return handler.postDelayed(action, delayMillis);
5915 }
5916
5917 /**
5918 * Removes the specified Runnable from the message queue.
5919 *
5920 * @param action The Runnable to remove from the message handling queue
5921 *
5922 * @return true if this view could ask the Handler to remove the Runnable,
5923 * false otherwise. When the returned value is true, the Runnable
5924 * may or may not have been actually removed from the message queue
5925 * (for instance, if the Runnable was not in the queue already.)
5926 */
5927 public boolean removeCallbacks(Runnable action) {
5928 Handler handler;
5929 if (mAttachInfo != null) {
5930 handler = mAttachInfo.mHandler;
5931 } else {
5932 // Assume that post will succeed later
5933 ViewRoot.getRunQueue().removeCallbacks(action);
5934 return true;
5935 }
5936
5937 handler.removeCallbacks(action);
5938 return true;
5939 }
5940
5941 /**
5942 * Cause an invalidate to happen on a subsequent cycle through the event loop.
5943 * Use this to invalidate the View from a non-UI thread.
5944 *
5945 * @see #invalidate()
5946 */
5947 public void postInvalidate() {
5948 postInvalidateDelayed(0);
5949 }
5950
5951 /**
5952 * Cause an invalidate of the specified area to happen on a subsequent cycle
5953 * through the event loop. Use this to invalidate the View from a non-UI thread.
5954 *
5955 * @param left The left coordinate of the rectangle to invalidate.
5956 * @param top The top coordinate of the rectangle to invalidate.
5957 * @param right The right coordinate of the rectangle to invalidate.
5958 * @param bottom The bottom coordinate of the rectangle to invalidate.
5959 *
5960 * @see #invalidate(int, int, int, int)
5961 * @see #invalidate(Rect)
5962 */
5963 public void postInvalidate(int left, int top, int right, int bottom) {
5964 postInvalidateDelayed(0, left, top, right, bottom);
5965 }
5966
5967 /**
5968 * Cause an invalidate to happen on a subsequent cycle through the event
5969 * loop. Waits for the specified amount of time.
5970 *
5971 * @param delayMilliseconds the duration in milliseconds to delay the
5972 * invalidation by
5973 */
5974 public void postInvalidateDelayed(long delayMilliseconds) {
5975 // We try only with the AttachInfo because there's no point in invalidating
5976 // if we are not attached to our window
5977 if (mAttachInfo != null) {
5978 Message msg = Message.obtain();
5979 msg.what = AttachInfo.INVALIDATE_MSG;
5980 msg.obj = this;
5981 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
5982 }
5983 }
5984
5985 /**
5986 * Cause an invalidate of the specified area to happen on a subsequent cycle
5987 * through the event loop. Waits for the specified amount of time.
5988 *
5989 * @param delayMilliseconds the duration in milliseconds to delay the
5990 * invalidation by
5991 * @param left The left coordinate of the rectangle to invalidate.
5992 * @param top The top coordinate of the rectangle to invalidate.
5993 * @param right The right coordinate of the rectangle to invalidate.
5994 * @param bottom The bottom coordinate of the rectangle to invalidate.
5995 */
5996 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
5997 int right, int bottom) {
5998
5999 // We try only with the AttachInfo because there's no point in invalidating
6000 // if we are not attached to our window
6001 if (mAttachInfo != null) {
6002 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
6003 info.target = this;
6004 info.left = left;
6005 info.top = top;
6006 info.right = right;
6007 info.bottom = bottom;
6008
6009 final Message msg = Message.obtain();
6010 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
6011 msg.obj = info;
6012 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
6013 }
6014 }
6015
6016 /**
6017 * Called by a parent to request that a child update its values for mScrollX
6018 * and mScrollY if necessary. This will typically be done if the child is
6019 * animating a scroll using a {@link android.widget.Scroller Scroller}
6020 * object.
6021 */
6022 public void computeScroll() {
6023 }
6024
6025 /**
6026 * <p>Indicate whether the horizontal edges are faded when the view is
6027 * scrolled horizontally.</p>
6028 *
6029 * @return true if the horizontal edges should are faded on scroll, false
6030 * otherwise
6031 *
6032 * @see #setHorizontalFadingEdgeEnabled(boolean)
6033 * @attr ref android.R.styleable#View_fadingEdge
6034 */
6035 public boolean isHorizontalFadingEdgeEnabled() {
6036 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
6037 }
6038
6039 /**
6040 * <p>Define whether the horizontal edges should be faded when this view
6041 * is scrolled horizontally.</p>
6042 *
6043 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
6044 * be faded when the view is scrolled
6045 * horizontally
6046 *
6047 * @see #isHorizontalFadingEdgeEnabled()
6048 * @attr ref android.R.styleable#View_fadingEdge
6049 */
6050 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
6051 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
6052 if (horizontalFadingEdgeEnabled) {
6053 initScrollCache();
6054 }
6055
6056 mViewFlags ^= FADING_EDGE_HORIZONTAL;
6057 }
6058 }
6059
6060 /**
6061 * <p>Indicate whether the vertical edges are faded when the view is
6062 * scrolled horizontally.</p>
6063 *
6064 * @return true if the vertical edges should are faded on scroll, false
6065 * otherwise
6066 *
6067 * @see #setVerticalFadingEdgeEnabled(boolean)
6068 * @attr ref android.R.styleable#View_fadingEdge
6069 */
6070 public boolean isVerticalFadingEdgeEnabled() {
6071 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
6072 }
6073
6074 /**
6075 * <p>Define whether the vertical edges should be faded when this view
6076 * is scrolled vertically.</p>
6077 *
6078 * @param verticalFadingEdgeEnabled true if the vertical edges should
6079 * be faded when the view is scrolled
6080 * vertically
6081 *
6082 * @see #isVerticalFadingEdgeEnabled()
6083 * @attr ref android.R.styleable#View_fadingEdge
6084 */
6085 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
6086 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
6087 if (verticalFadingEdgeEnabled) {
6088 initScrollCache();
6089 }
6090
6091 mViewFlags ^= FADING_EDGE_VERTICAL;
6092 }
6093 }
6094
6095 /**
6096 * Returns the strength, or intensity, of the top faded edge. The strength is
6097 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
6098 * returns 0.0 or 1.0 but no value in between.
6099 *
6100 * Subclasses should override this method to provide a smoother fade transition
6101 * when scrolling occurs.
6102 *
6103 * @return the intensity of the top fade as a float between 0.0f and 1.0f
6104 */
6105 protected float getTopFadingEdgeStrength() {
6106 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
6107 }
6108
6109 /**
6110 * Returns the strength, or intensity, of the bottom faded edge. The strength is
6111 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
6112 * returns 0.0 or 1.0 but no value in between.
6113 *
6114 * Subclasses should override this method to provide a smoother fade transition
6115 * when scrolling occurs.
6116 *
6117 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
6118 */
6119 protected float getBottomFadingEdgeStrength() {
6120 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
6121 computeVerticalScrollRange() ? 1.0f : 0.0f;
6122 }
6123
6124 /**
6125 * Returns the strength, or intensity, of the left faded edge. The strength is
6126 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
6127 * returns 0.0 or 1.0 but no value in between.
6128 *
6129 * Subclasses should override this method to provide a smoother fade transition
6130 * when scrolling occurs.
6131 *
6132 * @return the intensity of the left fade as a float between 0.0f and 1.0f
6133 */
6134 protected float getLeftFadingEdgeStrength() {
6135 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
6136 }
6137
6138 /**
6139 * Returns the strength, or intensity, of the right faded edge. The strength is
6140 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
6141 * returns 0.0 or 1.0 but no value in between.
6142 *
6143 * Subclasses should override this method to provide a smoother fade transition
6144 * when scrolling occurs.
6145 *
6146 * @return the intensity of the right fade as a float between 0.0f and 1.0f
6147 */
6148 protected float getRightFadingEdgeStrength() {
6149 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
6150 computeHorizontalScrollRange() ? 1.0f : 0.0f;
6151 }
6152
6153 /**
6154 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
6155 * scrollbar is not drawn by default.</p>
6156 *
6157 * @return true if the horizontal scrollbar should be painted, false
6158 * otherwise
6159 *
6160 * @see #setHorizontalScrollBarEnabled(boolean)
6161 */
6162 public boolean isHorizontalScrollBarEnabled() {
6163 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
6164 }
6165
6166 /**
6167 * <p>Define whether the horizontal scrollbar should be drawn or not. The
6168 * scrollbar is not drawn by default.</p>
6169 *
6170 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
6171 * be painted
6172 *
6173 * @see #isHorizontalScrollBarEnabled()
6174 */
6175 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
6176 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
6177 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07006178 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006179 recomputePadding();
6180 }
6181 }
6182
6183 /**
6184 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
6185 * scrollbar is not drawn by default.</p>
6186 *
6187 * @return true if the vertical scrollbar should be painted, false
6188 * otherwise
6189 *
6190 * @see #setVerticalScrollBarEnabled(boolean)
6191 */
6192 public boolean isVerticalScrollBarEnabled() {
6193 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
6194 }
6195
6196 /**
6197 * <p>Define whether the vertical scrollbar should be drawn or not. The
6198 * scrollbar is not drawn by default.</p>
6199 *
6200 * @param verticalScrollBarEnabled true if the vertical scrollbar should
6201 * be painted
6202 *
6203 * @see #isVerticalScrollBarEnabled()
6204 */
6205 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
6206 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
6207 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07006208 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 recomputePadding();
6210 }
6211 }
6212
6213 private void recomputePadding() {
6214 setPadding(mPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
6215 }
Mike Cleronfe81d382009-09-28 14:22:16 -07006216
6217 /**
6218 * Define whether scrollbars will fade when the view is not scrolling.
6219 *
6220 * @param fadeScrollbars wheter to enable fading
6221 *
6222 */
6223 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
6224 initScrollCache();
6225 final ScrollabilityCache scrollabilityCache = mScrollCache;
6226 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07006227 if (fadeScrollbars) {
6228 scrollabilityCache.state = ScrollabilityCache.OFF;
6229 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07006230 scrollabilityCache.state = ScrollabilityCache.ON;
6231 }
6232 }
6233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006234 /**
Mike Cleron52f0a642009-09-28 18:21:37 -07006235 *
6236 * Returns true if scrollbars will fade when this view is not scrolling
6237 *
6238 * @return true if scrollbar fading is enabled
6239 */
6240 public boolean isScrollbarFadingEnabled() {
6241 return mScrollCache != null && mScrollCache.fadeScrollBars;
6242 }
6243
6244 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006245 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
6246 * inset. When inset, they add to the padding of the view. And the scrollbars
6247 * can be drawn inside the padding area or on the edge of the view. For example,
6248 * if a view has a background drawable and you want to draw the scrollbars
6249 * inside the padding specified by the drawable, you can use
6250 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
6251 * appear at the edge of the view, ignoring the padding, then you can use
6252 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
6253 * @param style the style of the scrollbars. Should be one of
6254 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
6255 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
6256 * @see #SCROLLBARS_INSIDE_OVERLAY
6257 * @see #SCROLLBARS_INSIDE_INSET
6258 * @see #SCROLLBARS_OUTSIDE_OVERLAY
6259 * @see #SCROLLBARS_OUTSIDE_INSET
6260 */
6261 public void setScrollBarStyle(int style) {
6262 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
6263 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07006264 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 recomputePadding();
6266 }
6267 }
6268
6269 /**
6270 * <p>Returns the current scrollbar style.</p>
6271 * @return the current scrollbar style
6272 * @see #SCROLLBARS_INSIDE_OVERLAY
6273 * @see #SCROLLBARS_INSIDE_INSET
6274 * @see #SCROLLBARS_OUTSIDE_OVERLAY
6275 * @see #SCROLLBARS_OUTSIDE_INSET
6276 */
6277 public int getScrollBarStyle() {
6278 return mViewFlags & SCROLLBARS_STYLE_MASK;
6279 }
6280
6281 /**
6282 * <p>Compute the horizontal range that the horizontal scrollbar
6283 * represents.</p>
6284 *
6285 * <p>The range is expressed in arbitrary units that must be the same as the
6286 * units used by {@link #computeHorizontalScrollExtent()} and
6287 * {@link #computeHorizontalScrollOffset()}.</p>
6288 *
6289 * <p>The default range is the drawing width of this view.</p>
6290 *
6291 * @return the total horizontal range represented by the horizontal
6292 * scrollbar
6293 *
6294 * @see #computeHorizontalScrollExtent()
6295 * @see #computeHorizontalScrollOffset()
6296 * @see android.widget.ScrollBarDrawable
6297 */
6298 protected int computeHorizontalScrollRange() {
6299 return getWidth();
6300 }
6301
6302 /**
6303 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
6304 * within the horizontal range. This value is used to compute the position
6305 * of the thumb within the scrollbar's track.</p>
6306 *
6307 * <p>The range is expressed in arbitrary units that must be the same as the
6308 * units used by {@link #computeHorizontalScrollRange()} and
6309 * {@link #computeHorizontalScrollExtent()}.</p>
6310 *
6311 * <p>The default offset is the scroll offset of this view.</p>
6312 *
6313 * @return the horizontal offset of the scrollbar's thumb
6314 *
6315 * @see #computeHorizontalScrollRange()
6316 * @see #computeHorizontalScrollExtent()
6317 * @see android.widget.ScrollBarDrawable
6318 */
6319 protected int computeHorizontalScrollOffset() {
6320 return mScrollX;
6321 }
6322
6323 /**
6324 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
6325 * within the horizontal range. This value is used to compute the length
6326 * of the thumb within the scrollbar's track.</p>
6327 *
6328 * <p>The range is expressed in arbitrary units that must be the same as the
6329 * units used by {@link #computeHorizontalScrollRange()} and
6330 * {@link #computeHorizontalScrollOffset()}.</p>
6331 *
6332 * <p>The default extent is the drawing width of this view.</p>
6333 *
6334 * @return the horizontal extent of the scrollbar's thumb
6335 *
6336 * @see #computeHorizontalScrollRange()
6337 * @see #computeHorizontalScrollOffset()
6338 * @see android.widget.ScrollBarDrawable
6339 */
6340 protected int computeHorizontalScrollExtent() {
6341 return getWidth();
6342 }
6343
6344 /**
6345 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
6346 *
6347 * <p>The range is expressed in arbitrary units that must be the same as the
6348 * units used by {@link #computeVerticalScrollExtent()} and
6349 * {@link #computeVerticalScrollOffset()}.</p>
6350 *
6351 * @return the total vertical range represented by the vertical scrollbar
6352 *
6353 * <p>The default range is the drawing height of this view.</p>
6354 *
6355 * @see #computeVerticalScrollExtent()
6356 * @see #computeVerticalScrollOffset()
6357 * @see android.widget.ScrollBarDrawable
6358 */
6359 protected int computeVerticalScrollRange() {
6360 return getHeight();
6361 }
6362
6363 /**
6364 * <p>Compute the vertical offset of the vertical scrollbar's thumb
6365 * within the horizontal range. This value is used to compute the position
6366 * of the thumb within the scrollbar's track.</p>
6367 *
6368 * <p>The range is expressed in arbitrary units that must be the same as the
6369 * units used by {@link #computeVerticalScrollRange()} and
6370 * {@link #computeVerticalScrollExtent()}.</p>
6371 *
6372 * <p>The default offset is the scroll offset of this view.</p>
6373 *
6374 * @return the vertical offset of the scrollbar's thumb
6375 *
6376 * @see #computeVerticalScrollRange()
6377 * @see #computeVerticalScrollExtent()
6378 * @see android.widget.ScrollBarDrawable
6379 */
6380 protected int computeVerticalScrollOffset() {
6381 return mScrollY;
6382 }
6383
6384 /**
6385 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
6386 * within the vertical range. This value is used to compute the length
6387 * of the thumb within the scrollbar's track.</p>
6388 *
6389 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08006390 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006391 * {@link #computeVerticalScrollOffset()}.</p>
6392 *
6393 * <p>The default extent is the drawing height of this view.</p>
6394 *
6395 * @return the vertical extent of the scrollbar's thumb
6396 *
6397 * @see #computeVerticalScrollRange()
6398 * @see #computeVerticalScrollOffset()
6399 * @see android.widget.ScrollBarDrawable
6400 */
6401 protected int computeVerticalScrollExtent() {
6402 return getHeight();
6403 }
6404
6405 /**
6406 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
6407 * scrollbars are painted only if they have been awakened first.</p>
6408 *
6409 * @param canvas the canvas on which to draw the scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -07006410 *
6411 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006412 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08006413 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006414 // scrollbars are drawn only when the animation is running
6415 final ScrollabilityCache cache = mScrollCache;
6416 if (cache != null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006417
6418 int state = cache.state;
6419
6420 if (state == ScrollabilityCache.OFF) {
6421 return;
6422 }
6423
6424 boolean invalidate = false;
6425
6426 if (state == ScrollabilityCache.FADING) {
6427 // We're fading -- get our fade interpolation
6428 if (cache.interpolatorValues == null) {
6429 cache.interpolatorValues = new float[1];
6430 }
6431
6432 float[] values = cache.interpolatorValues;
6433
6434 // Stops the animation if we're done
6435 if (cache.scrollBarInterpolator.timeToValues(values) ==
6436 Interpolator.Result.FREEZE_END) {
6437 cache.state = ScrollabilityCache.OFF;
6438 } else {
6439 cache.scrollBar.setAlpha(Math.round(values[0]));
6440 }
6441
6442 // This will make the scroll bars inval themselves after
6443 // drawing. We only want this when we're fading so that
6444 // we prevent excessive redraws
6445 invalidate = true;
6446 } else {
6447 // We're just on -- but we may have been fading before so
6448 // reset alpha
6449 cache.scrollBar.setAlpha(255);
6450 }
6451
6452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453 final int viewFlags = mViewFlags;
6454
6455 final boolean drawHorizontalScrollBar =
6456 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
6457 final boolean drawVerticalScrollBar =
6458 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
6459 && !isVerticalScrollBarHidden();
6460
6461 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
6462 final int width = mRight - mLeft;
6463 final int height = mBottom - mTop;
6464
6465 final ScrollBarDrawable scrollBar = cache.scrollBar;
6466 int size = scrollBar.getSize(false);
6467 if (size <= 0) {
6468 size = cache.scrollBarSize;
6469 }
6470
Mike Reede8853fc2009-09-04 14:01:48 -04006471 final int scrollX = mScrollX;
6472 final int scrollY = mScrollY;
6473 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
6474
Mike Cleronf116bf82009-09-27 19:14:12 -07006475 int left, top, right, bottom;
6476
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006477 if (drawHorizontalScrollBar) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006478 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04006479 computeHorizontalScrollOffset(),
6480 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04006481 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07006482 getVerticalScrollbarWidth() : 0;
6483 top = scrollY + height - size - (mUserPaddingBottom & inside);
6484 left = scrollX + (mPaddingLeft & inside);
6485 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
6486 bottom = top + size;
6487 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
6488 if (invalidate) {
6489 invalidate(left, top, right, bottom);
6490 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006491 }
6492
6493 if (drawVerticalScrollBar) {
Mike Reede8853fc2009-09-04 14:01:48 -04006494 scrollBar.setParameters(computeVerticalScrollRange(),
6495 computeVerticalScrollOffset(),
6496 computeVerticalScrollExtent(), true);
6497 // TODO: Deal with RTL languages to position scrollbar on left
Mike Cleronf116bf82009-09-27 19:14:12 -07006498 left = scrollX + width - size - (mUserPaddingRight & inside);
6499 top = scrollY + (mPaddingTop & inside);
6500 right = left + size;
6501 bottom = scrollY + height - (mUserPaddingBottom & inside);
6502 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
6503 if (invalidate) {
6504 invalidate(left, top, right, bottom);
6505 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 }
6507 }
6508 }
6509 }
Romain Guy8506ab42009-06-11 17:35:47 -07006510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006511 /**
Romain Guy8506ab42009-06-11 17:35:47 -07006512 * 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 -08006513 * FastScroller is visible.
6514 * @return whether to temporarily hide the vertical scrollbar
6515 * @hide
6516 */
6517 protected boolean isVerticalScrollBarHidden() {
6518 return false;
6519 }
6520
6521 /**
6522 * <p>Draw the horizontal scrollbar if
6523 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
6524 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006525 * @param canvas the canvas on which to draw the scrollbar
6526 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006527 *
6528 * @see #isHorizontalScrollBarEnabled()
6529 * @see #computeHorizontalScrollRange()
6530 * @see #computeHorizontalScrollExtent()
6531 * @see #computeHorizontalScrollOffset()
6532 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07006533 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04006534 */
Mike Reede8853fc2009-09-04 14:01:48 -04006535 protected void onDrawHorizontalScrollBar(Canvas canvas,
6536 Drawable scrollBar,
6537 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04006538 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04006539 scrollBar.draw(canvas);
6540 }
Mike Reede8853fc2009-09-04 14:01:48 -04006541
Mike Reed4d6fe5f2009-09-03 13:29:05 -04006542 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006543 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
6544 * returns true.</p>
6545 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006546 * @param canvas the canvas on which to draw the scrollbar
6547 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006548 *
6549 * @see #isVerticalScrollBarEnabled()
6550 * @see #computeVerticalScrollRange()
6551 * @see #computeVerticalScrollExtent()
6552 * @see #computeVerticalScrollOffset()
6553 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04006554 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006555 */
Mike Reede8853fc2009-09-04 14:01:48 -04006556 protected void onDrawVerticalScrollBar(Canvas canvas,
6557 Drawable scrollBar,
6558 int l, int t, int r, int b) {
6559 scrollBar.setBounds(l, t, r, b);
6560 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006561 }
6562
6563 /**
6564 * Implement this to do your drawing.
6565 *
6566 * @param canvas the canvas on which the background will be drawn
6567 */
6568 protected void onDraw(Canvas canvas) {
6569 }
6570
6571 /*
6572 * Caller is responsible for calling requestLayout if necessary.
6573 * (This allows addViewInLayout to not request a new layout.)
6574 */
6575 void assignParent(ViewParent parent) {
6576 if (mParent == null) {
6577 mParent = parent;
6578 } else if (parent == null) {
6579 mParent = null;
6580 } else {
6581 throw new RuntimeException("view " + this + " being added, but"
6582 + " it already has a parent");
6583 }
6584 }
6585
6586 /**
6587 * This is called when the view is attached to a window. At this point it
6588 * has a Surface and will start drawing. Note that this function is
6589 * guaranteed to be called before {@link #onDraw}, however it may be called
6590 * any time before the first onDraw -- including before or after
6591 * {@link #onMeasure}.
6592 *
6593 * @see #onDetachedFromWindow()
6594 */
6595 protected void onAttachedToWindow() {
6596 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
6597 mParent.requestTransparentRegion(this);
6598 }
Adam Powell8568c3a2010-04-19 14:26:11 -07006599 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
6600 initialAwakenScrollBars();
6601 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
6602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006603 }
6604
6605 /**
6606 * This is called when the view is detached from a window. At this point it
6607 * no longer has a surface for drawing.
6608 *
6609 * @see #onAttachedToWindow()
6610 */
6611 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08006612 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08006613 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05006614 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006615 destroyDrawingCache();
6616 }
6617
6618 /**
6619 * @return The number of times this view has been attached to a window
6620 */
6621 protected int getWindowAttachCount() {
6622 return mWindowAttachCount;
6623 }
6624
6625 /**
6626 * Retrieve a unique token identifying the window this view is attached to.
6627 * @return Return the window's token for use in
6628 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
6629 */
6630 public IBinder getWindowToken() {
6631 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
6632 }
6633
6634 /**
6635 * Retrieve a unique token identifying the top-level "real" window of
6636 * the window that this view is attached to. That is, this is like
6637 * {@link #getWindowToken}, except if the window this view in is a panel
6638 * window (attached to another containing window), then the token of
6639 * the containing window is returned instead.
6640 *
6641 * @return Returns the associated window token, either
6642 * {@link #getWindowToken()} or the containing window's token.
6643 */
6644 public IBinder getApplicationWindowToken() {
6645 AttachInfo ai = mAttachInfo;
6646 if (ai != null) {
6647 IBinder appWindowToken = ai.mPanelParentWindowToken;
6648 if (appWindowToken == null) {
6649 appWindowToken = ai.mWindowToken;
6650 }
6651 return appWindowToken;
6652 }
6653 return null;
6654 }
6655
6656 /**
6657 * Retrieve private session object this view hierarchy is using to
6658 * communicate with the window manager.
6659 * @return the session object to communicate with the window manager
6660 */
6661 /*package*/ IWindowSession getWindowSession() {
6662 return mAttachInfo != null ? mAttachInfo.mSession : null;
6663 }
6664
6665 /**
6666 * @param info the {@link android.view.View.AttachInfo} to associated with
6667 * this view
6668 */
6669 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
6670 //System.out.println("Attached! " + this);
6671 mAttachInfo = info;
6672 mWindowAttachCount++;
6673 if (mFloatingTreeObserver != null) {
6674 info.mTreeObserver.merge(mFloatingTreeObserver);
6675 mFloatingTreeObserver = null;
6676 }
6677 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
6678 mAttachInfo.mScrollContainers.add(this);
6679 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
6680 }
6681 performCollectViewAttributes(visibility);
6682 onAttachedToWindow();
6683 int vis = info.mWindowVisibility;
6684 if (vis != GONE) {
6685 onWindowVisibilityChanged(vis);
6686 }
6687 }
6688
6689 void dispatchDetachedFromWindow() {
6690 //System.out.println("Detached! " + this);
6691 AttachInfo info = mAttachInfo;
6692 if (info != null) {
6693 int vis = info.mWindowVisibility;
6694 if (vis != GONE) {
6695 onWindowVisibilityChanged(GONE);
6696 }
6697 }
6698
6699 onDetachedFromWindow();
6700 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
6701 mAttachInfo.mScrollContainers.remove(this);
6702 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
6703 }
6704 mAttachInfo = null;
6705 }
6706
6707 /**
6708 * Store this view hierarchy's frozen state into the given container.
6709 *
6710 * @param container The SparseArray in which to save the view's state.
6711 *
6712 * @see #restoreHierarchyState
6713 * @see #dispatchSaveInstanceState
6714 * @see #onSaveInstanceState
6715 */
6716 public void saveHierarchyState(SparseArray<Parcelable> container) {
6717 dispatchSaveInstanceState(container);
6718 }
6719
6720 /**
6721 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
6722 * May be overridden to modify how freezing happens to a view's children; for example, some
6723 * views may want to not store state for their children.
6724 *
6725 * @param container The SparseArray in which to save the view's state.
6726 *
6727 * @see #dispatchRestoreInstanceState
6728 * @see #saveHierarchyState
6729 * @see #onSaveInstanceState
6730 */
6731 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
6732 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
6733 mPrivateFlags &= ~SAVE_STATE_CALLED;
6734 Parcelable state = onSaveInstanceState();
6735 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
6736 throw new IllegalStateException(
6737 "Derived class did not call super.onSaveInstanceState()");
6738 }
6739 if (state != null) {
6740 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
6741 // + ": " + state);
6742 container.put(mID, state);
6743 }
6744 }
6745 }
6746
6747 /**
6748 * Hook allowing a view to generate a representation of its internal state
6749 * that can later be used to create a new instance with that same state.
6750 * This state should only contain information that is not persistent or can
6751 * not be reconstructed later. For example, you will never store your
6752 * current position on screen because that will be computed again when a
6753 * new instance of the view is placed in its view hierarchy.
6754 * <p>
6755 * Some examples of things you may store here: the current cursor position
6756 * in a text view (but usually not the text itself since that is stored in a
6757 * content provider or other persistent storage), the currently selected
6758 * item in a list view.
6759 *
6760 * @return Returns a Parcelable object containing the view's current dynamic
6761 * state, or null if there is nothing interesting to save. The
6762 * default implementation returns null.
6763 * @see #onRestoreInstanceState
6764 * @see #saveHierarchyState
6765 * @see #dispatchSaveInstanceState
6766 * @see #setSaveEnabled(boolean)
6767 */
6768 protected Parcelable onSaveInstanceState() {
6769 mPrivateFlags |= SAVE_STATE_CALLED;
6770 return BaseSavedState.EMPTY_STATE;
6771 }
6772
6773 /**
6774 * Restore this view hierarchy's frozen state from the given container.
6775 *
6776 * @param container The SparseArray which holds previously frozen states.
6777 *
6778 * @see #saveHierarchyState
6779 * @see #dispatchRestoreInstanceState
6780 * @see #onRestoreInstanceState
6781 */
6782 public void restoreHierarchyState(SparseArray<Parcelable> container) {
6783 dispatchRestoreInstanceState(container);
6784 }
6785
6786 /**
6787 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
6788 * children. May be overridden to modify how restoreing happens to a view's children; for
6789 * example, some views may want to not store state for their children.
6790 *
6791 * @param container The SparseArray which holds previously saved state.
6792 *
6793 * @see #dispatchSaveInstanceState
6794 * @see #restoreHierarchyState
6795 * @see #onRestoreInstanceState
6796 */
6797 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
6798 if (mID != NO_ID) {
6799 Parcelable state = container.get(mID);
6800 if (state != null) {
6801 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
6802 // + ": " + state);
6803 mPrivateFlags &= ~SAVE_STATE_CALLED;
6804 onRestoreInstanceState(state);
6805 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
6806 throw new IllegalStateException(
6807 "Derived class did not call super.onRestoreInstanceState()");
6808 }
6809 }
6810 }
6811 }
6812
6813 /**
6814 * Hook allowing a view to re-apply a representation of its internal state that had previously
6815 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
6816 * null state.
6817 *
6818 * @param state The frozen state that had previously been returned by
6819 * {@link #onSaveInstanceState}.
6820 *
6821 * @see #onSaveInstanceState
6822 * @see #restoreHierarchyState
6823 * @see #dispatchRestoreInstanceState
6824 */
6825 protected void onRestoreInstanceState(Parcelable state) {
6826 mPrivateFlags |= SAVE_STATE_CALLED;
6827 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08006828 throw new IllegalArgumentException("Wrong state class, expecting View State but "
6829 + "received " + state.getClass().toString() + " instead. This usually happens "
6830 + "when two views of different type have the same id in the same hierarchy. "
6831 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
6832 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006833 }
6834 }
6835
6836 /**
6837 * <p>Return the time at which the drawing of the view hierarchy started.</p>
6838 *
6839 * @return the drawing start time in milliseconds
6840 */
6841 public long getDrawingTime() {
6842 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
6843 }
6844
6845 /**
6846 * <p>Enables or disables the duplication of the parent's state into this view. When
6847 * duplication is enabled, this view gets its drawable state from its parent rather
6848 * than from its own internal properties.</p>
6849 *
6850 * <p>Note: in the current implementation, setting this property to true after the
6851 * view was added to a ViewGroup might have no effect at all. This property should
6852 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
6853 *
6854 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
6855 * property is enabled, an exception will be thrown.</p>
6856 *
6857 * @param enabled True to enable duplication of the parent's drawable state, false
6858 * to disable it.
6859 *
6860 * @see #getDrawableState()
6861 * @see #isDuplicateParentStateEnabled()
6862 */
6863 public void setDuplicateParentStateEnabled(boolean enabled) {
6864 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
6865 }
6866
6867 /**
6868 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
6869 *
6870 * @return True if this view's drawable state is duplicated from the parent,
6871 * false otherwise
6872 *
6873 * @see #getDrawableState()
6874 * @see #setDuplicateParentStateEnabled(boolean)
6875 */
6876 public boolean isDuplicateParentStateEnabled() {
6877 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
6878 }
6879
6880 /**
6881 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
6882 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
6883 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
6884 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
6885 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
6886 * null.</p>
6887 *
6888 * @param enabled true to enable the drawing cache, false otherwise
6889 *
6890 * @see #isDrawingCacheEnabled()
6891 * @see #getDrawingCache()
6892 * @see #buildDrawingCache()
6893 */
6894 public void setDrawingCacheEnabled(boolean enabled) {
6895 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
6896 }
6897
6898 /**
6899 * <p>Indicates whether the drawing cache is enabled for this view.</p>
6900 *
6901 * @return true if the drawing cache is enabled
6902 *
6903 * @see #setDrawingCacheEnabled(boolean)
6904 * @see #getDrawingCache()
6905 */
6906 @ViewDebug.ExportedProperty
6907 public boolean isDrawingCacheEnabled() {
6908 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
6909 }
6910
6911 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07006912 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
6913 *
6914 * @return A non-scaled bitmap representing this view or null if cache is disabled.
6915 *
6916 * @see #getDrawingCache(boolean)
6917 */
6918 public Bitmap getDrawingCache() {
6919 return getDrawingCache(false);
6920 }
6921
6922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006923 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
6924 * is null when caching is disabled. If caching is enabled and the cache is not ready,
6925 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
6926 * draw from the cache when the cache is enabled. To benefit from the cache, you must
6927 * request the drawing cache by calling this method and draw it on screen if the
6928 * returned bitmap is not null.</p>
Romain Guyfbd8f692009-06-26 14:51:58 -07006929 *
6930 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
6931 * this method will create a bitmap of the same size as this view. Because this bitmap
6932 * will be drawn scaled by the parent ViewGroup, the result on screen might show
6933 * scaling artifacts. To avoid such artifacts, you should call this method by setting
6934 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
6935 * size than the view. This implies that your application must be able to handle this
6936 * size.</p>
6937 *
6938 * @param autoScale Indicates whether the generated bitmap should be scaled based on
6939 * the current density of the screen when the application is in compatibility
6940 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006941 *
Romain Guyfbd8f692009-06-26 14:51:58 -07006942 * @return A bitmap representing this view or null if cache is disabled.
6943 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006944 * @see #setDrawingCacheEnabled(boolean)
6945 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07006946 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006947 * @see #destroyDrawingCache()
6948 */
Romain Guyfbd8f692009-06-26 14:51:58 -07006949 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006950 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
6951 return null;
6952 }
6953 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07006954 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006955 }
Romain Guyfbd8f692009-06-26 14:51:58 -07006956 return autoScale ? (mDrawingCache == null ? null : mDrawingCache.get()) :
6957 (mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006958 }
6959
6960 /**
6961 * <p>Frees the resources used by the drawing cache. If you call
6962 * {@link #buildDrawingCache()} manually without calling
6963 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
6964 * should cleanup the cache with this method afterwards.</p>
6965 *
6966 * @see #setDrawingCacheEnabled(boolean)
6967 * @see #buildDrawingCache()
6968 * @see #getDrawingCache()
6969 */
6970 public void destroyDrawingCache() {
6971 if (mDrawingCache != null) {
6972 final Bitmap bitmap = mDrawingCache.get();
6973 if (bitmap != null) bitmap.recycle();
6974 mDrawingCache = null;
6975 }
Romain Guyfbd8f692009-06-26 14:51:58 -07006976 if (mUnscaledDrawingCache != null) {
6977 final Bitmap bitmap = mUnscaledDrawingCache.get();
6978 if (bitmap != null) bitmap.recycle();
6979 mUnscaledDrawingCache = null;
6980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 }
6982
6983 /**
6984 * Setting a solid background color for the drawing cache's bitmaps will improve
6985 * perfromance and memory usage. Note, though that this should only be used if this
6986 * view will always be drawn on top of a solid color.
6987 *
6988 * @param color The background color to use for the drawing cache's bitmap
6989 *
6990 * @see #setDrawingCacheEnabled(boolean)
6991 * @see #buildDrawingCache()
6992 * @see #getDrawingCache()
6993 */
6994 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08006995 if (color != mDrawingCacheBackgroundColor) {
6996 mDrawingCacheBackgroundColor = color;
6997 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6998 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006999 }
7000
7001 /**
7002 * @see #setDrawingCacheBackgroundColor(int)
7003 *
7004 * @return The background color to used for the drawing cache's bitmap
7005 */
7006 public int getDrawingCacheBackgroundColor() {
7007 return mDrawingCacheBackgroundColor;
7008 }
7009
7010 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07007011 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
7012 *
7013 * @see #buildDrawingCache(boolean)
7014 */
7015 public void buildDrawingCache() {
7016 buildDrawingCache(false);
7017 }
7018
7019 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007020 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
7021 *
7022 * <p>If you call {@link #buildDrawingCache()} manually without calling
7023 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
7024 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Romain Guyfbd8f692009-06-26 14:51:58 -07007025 *
7026 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
7027 * this method will create a bitmap of the same size as this view. Because this bitmap
7028 * will be drawn scaled by the parent ViewGroup, the result on screen might show
7029 * scaling artifacts. To avoid such artifacts, you should call this method by setting
7030 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
7031 * size than the view. This implies that your application must be able to handle this
7032 * size.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 *
7034 * @see #getDrawingCache()
7035 * @see #destroyDrawingCache()
7036 */
Romain Guyfbd8f692009-06-26 14:51:58 -07007037 public void buildDrawingCache(boolean autoScale) {
7038 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
7039 (mDrawingCache == null || mDrawingCache.get() == null) :
7040 (mUnscaledDrawingCache == null || mUnscaledDrawingCache.get() == null))) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007041
7042 if (ViewDebug.TRACE_HIERARCHY) {
7043 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
7044 }
Romain Guy13922e02009-05-12 17:56:14 -07007045 if (Config.DEBUG && ViewDebug.profileDrawing) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007046 EventLog.writeEvent(60002, hashCode());
7047 }
7048
Romain Guy8506ab42009-06-11 17:35:47 -07007049 int width = mRight - mLeft;
7050 int height = mBottom - mTop;
7051
7052 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07007053 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07007054
Romain Guye1123222009-06-29 14:24:56 -07007055 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07007056 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
7057 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07007058 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007059
7060 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07007061 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Romain Guya62e4702009-10-08 10:48:54 -07007062 final boolean translucentWindow = attachInfo != null && attachInfo.mTranslucentWindow;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007063
7064 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07007065 // Projected bitmap size in bytes
7066 (width * height * (opaque && !translucentWindow ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007067 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
7068 destroyDrawingCache();
7069 return;
7070 }
7071
7072 boolean clear = true;
Romain Guyfbd8f692009-06-26 14:51:58 -07007073 Bitmap bitmap = autoScale ? (mDrawingCache == null ? null : mDrawingCache.get()) :
7074 (mUnscaledDrawingCache == null ? null : mUnscaledDrawingCache.get());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007075
7076 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007077 Bitmap.Config quality;
7078 if (!opaque) {
7079 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
7080 case DRAWING_CACHE_QUALITY_AUTO:
7081 quality = Bitmap.Config.ARGB_8888;
7082 break;
7083 case DRAWING_CACHE_QUALITY_LOW:
7084 quality = Bitmap.Config.ARGB_4444;
7085 break;
7086 case DRAWING_CACHE_QUALITY_HIGH:
7087 quality = Bitmap.Config.ARGB_8888;
7088 break;
7089 default:
7090 quality = Bitmap.Config.ARGB_8888;
7091 break;
7092 }
7093 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07007094 // Optimization for translucent windows
7095 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
7096 quality = translucentWindow ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007097 }
7098
7099 // Try to cleanup memory
7100 if (bitmap != null) bitmap.recycle();
7101
7102 try {
7103 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07007104 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07007105 if (autoScale) {
7106 mDrawingCache = new SoftReference<Bitmap>(bitmap);
7107 } else {
7108 mUnscaledDrawingCache = new SoftReference<Bitmap>(bitmap);
7109 }
Romain Guy35b38ce2009-10-07 13:38:55 -07007110 if (opaque && translucentWindow) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007111 } catch (OutOfMemoryError e) {
7112 // If there is not enough memory to create the bitmap cache, just
7113 // ignore the issue as bitmap caches are not required to draw the
7114 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07007115 if (autoScale) {
7116 mDrawingCache = null;
7117 } else {
7118 mUnscaledDrawingCache = null;
7119 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007120 return;
7121 }
7122
7123 clear = drawingCacheBackgroundColor != 0;
7124 }
7125
7126 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007127 if (attachInfo != null) {
7128 canvas = attachInfo.mCanvas;
7129 if (canvas == null) {
7130 canvas = new Canvas();
7131 }
7132 canvas.setBitmap(bitmap);
7133 // Temporarily clobber the cached Canvas in case one of our children
7134 // is also using a drawing cache. Without this, the children would
7135 // steal the canvas by attaching their own bitmap to it and bad, bad
7136 // thing would happen (invisible views, corrupted drawings, etc.)
7137 attachInfo.mCanvas = null;
7138 } else {
7139 // This case should hopefully never or seldom happen
7140 canvas = new Canvas(bitmap);
7141 }
7142
7143 if (clear) {
7144 bitmap.eraseColor(drawingCacheBackgroundColor);
7145 }
7146
7147 computeScroll();
7148 final int restoreCount = canvas.save();
Romain Guyfbd8f692009-06-26 14:51:58 -07007149
Romain Guye1123222009-06-29 14:24:56 -07007150 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07007151 final float scale = attachInfo.mApplicationScale;
7152 canvas.scale(scale, scale);
7153 }
7154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007155 canvas.translate(-mScrollX, -mScrollY);
7156
Romain Guy5bcdff42009-05-14 21:27:18 -07007157 mPrivateFlags |= DRAWN;
Romain Guyecd80ee2009-12-03 17:13:02 -08007158 mPrivateFlags |= DRAWING_CACHE_VALID;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007159
7160 // Fast path for layouts with no backgrounds
7161 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
7162 if (ViewDebug.TRACE_HIERARCHY) {
7163 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
7164 }
Romain Guy5bcdff42009-05-14 21:27:18 -07007165 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007166 dispatchDraw(canvas);
7167 } else {
7168 draw(canvas);
7169 }
7170
7171 canvas.restoreToCount(restoreCount);
7172
7173 if (attachInfo != null) {
7174 // Restore the cached Canvas for our siblings
7175 attachInfo.mCanvas = canvas;
7176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007177 }
7178 }
7179
7180 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007181 * Create a snapshot of the view into a bitmap. We should probably make
7182 * some form of this public, but should think about the API.
7183 */
Romain Guy223ff5c2010-03-02 17:07:47 -08007184 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07007185 int width = mRight - mLeft;
7186 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007187
Dianne Hackborn8cae1242009-09-10 14:32:16 -07007188 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07007189 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07007190 width = (int) ((width * scale) + 0.5f);
7191 height = (int) ((height * scale) + 0.5f);
7192
Romain Guy8c11e312009-09-14 15:15:30 -07007193 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007194 if (bitmap == null) {
7195 throw new OutOfMemoryError();
7196 }
7197
Dianne Hackborn8cae1242009-09-10 14:32:16 -07007198 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
7199
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007200 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007201 if (attachInfo != null) {
7202 canvas = attachInfo.mCanvas;
7203 if (canvas == null) {
7204 canvas = new Canvas();
7205 }
7206 canvas.setBitmap(bitmap);
7207 // Temporarily clobber the cached Canvas in case one of our children
7208 // is also using a drawing cache. Without this, the children would
7209 // steal the canvas by attaching their own bitmap to it and bad, bad
7210 // things would happen (invisible views, corrupted drawings, etc.)
7211 attachInfo.mCanvas = null;
7212 } else {
7213 // This case should hopefully never or seldom happen
7214 canvas = new Canvas(bitmap);
7215 }
7216
Romain Guy5bcdff42009-05-14 21:27:18 -07007217 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007218 bitmap.eraseColor(backgroundColor);
7219 }
7220
7221 computeScroll();
7222 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07007223 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007224 canvas.translate(-mScrollX, -mScrollY);
7225
Romain Guy5bcdff42009-05-14 21:27:18 -07007226 // Temporarily remove the dirty mask
7227 int flags = mPrivateFlags;
7228 mPrivateFlags &= ~DIRTY_MASK;
7229
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007230 // Fast path for layouts with no backgrounds
7231 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
7232 dispatchDraw(canvas);
7233 } else {
7234 draw(canvas);
7235 }
7236
Romain Guy5bcdff42009-05-14 21:27:18 -07007237 mPrivateFlags = flags;
7238
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007239 canvas.restoreToCount(restoreCount);
7240
7241 if (attachInfo != null) {
7242 // Restore the cached Canvas for our siblings
7243 attachInfo.mCanvas = canvas;
7244 }
Romain Guy8506ab42009-06-11 17:35:47 -07007245
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07007246 return bitmap;
7247 }
7248
7249 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007250 * Indicates whether this View is currently in edit mode. A View is usually
7251 * in edit mode when displayed within a developer tool. For instance, if
7252 * this View is being drawn by a visual user interface builder, this method
7253 * should return true.
7254 *
7255 * Subclasses should check the return value of this method to provide
7256 * different behaviors if their normal behavior might interfere with the
7257 * host environment. For instance: the class spawns a thread in its
7258 * constructor, the drawing code relies on device-specific features, etc.
7259 *
7260 * This method is usually checked in the drawing code of custom widgets.
7261 *
7262 * @return True if this View is in edit mode, false otherwise.
7263 */
7264 public boolean isInEditMode() {
7265 return false;
7266 }
7267
7268 /**
7269 * If the View draws content inside its padding and enables fading edges,
7270 * it needs to support padding offsets. Padding offsets are added to the
7271 * fading edges to extend the length of the fade so that it covers pixels
7272 * drawn inside the padding.
7273 *
7274 * Subclasses of this class should override this method if they need
7275 * to draw content inside the padding.
7276 *
7277 * @return True if padding offset must be applied, false otherwise.
7278 *
7279 * @see #getLeftPaddingOffset()
7280 * @see #getRightPaddingOffset()
7281 * @see #getTopPaddingOffset()
7282 * @see #getBottomPaddingOffset()
7283 *
7284 * @since CURRENT
7285 */
7286 protected boolean isPaddingOffsetRequired() {
7287 return false;
7288 }
7289
7290 /**
7291 * Amount by which to extend the left fading region. Called only when
7292 * {@link #isPaddingOffsetRequired()} returns true.
7293 *
7294 * @return The left padding offset in pixels.
7295 *
7296 * @see #isPaddingOffsetRequired()
7297 *
7298 * @since CURRENT
7299 */
7300 protected int getLeftPaddingOffset() {
7301 return 0;
7302 }
7303
7304 /**
7305 * Amount by which to extend the right fading region. Called only when
7306 * {@link #isPaddingOffsetRequired()} returns true.
7307 *
7308 * @return The right padding offset in pixels.
7309 *
7310 * @see #isPaddingOffsetRequired()
7311 *
7312 * @since CURRENT
7313 */
7314 protected int getRightPaddingOffset() {
7315 return 0;
7316 }
7317
7318 /**
7319 * Amount by which to extend the top fading region. Called only when
7320 * {@link #isPaddingOffsetRequired()} returns true.
7321 *
7322 * @return The top padding offset in pixels.
7323 *
7324 * @see #isPaddingOffsetRequired()
7325 *
7326 * @since CURRENT
7327 */
7328 protected int getTopPaddingOffset() {
7329 return 0;
7330 }
7331
7332 /**
7333 * Amount by which to extend the bottom fading region. Called only when
7334 * {@link #isPaddingOffsetRequired()} returns true.
7335 *
7336 * @return The bottom padding offset in pixels.
7337 *
7338 * @see #isPaddingOffsetRequired()
7339 *
7340 * @since CURRENT
7341 */
7342 protected int getBottomPaddingOffset() {
7343 return 0;
7344 }
7345
7346 /**
7347 * Manually render this view (and all of its children) to the given Canvas.
7348 * The view must have already done a full layout before this function is
7349 * called. When implementing a view, do not override this method; instead,
7350 * you should implement {@link #onDraw}.
7351 *
7352 * @param canvas The Canvas to which the View is rendered.
7353 */
7354 public void draw(Canvas canvas) {
7355 if (ViewDebug.TRACE_HIERARCHY) {
7356 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
7357 }
7358
Romain Guy5bcdff42009-05-14 21:27:18 -07007359 final int privateFlags = mPrivateFlags;
7360 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
7361 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
7362 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07007363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007364 /*
7365 * Draw traversal performs several drawing steps which must be executed
7366 * in the appropriate order:
7367 *
7368 * 1. Draw the background
7369 * 2. If necessary, save the canvas' layers to prepare for fading
7370 * 3. Draw view's content
7371 * 4. Draw children
7372 * 5. If necessary, draw the fading edges and restore layers
7373 * 6. Draw decorations (scrollbars for instance)
7374 */
7375
7376 // Step 1, draw the background, if needed
7377 int saveCount;
7378
Romain Guy24443ea2009-05-11 11:56:30 -07007379 if (!dirtyOpaque) {
7380 final Drawable background = mBGDrawable;
7381 if (background != null) {
7382 final int scrollX = mScrollX;
7383 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384
Romain Guy24443ea2009-05-11 11:56:30 -07007385 if (mBackgroundSizeChanged) {
7386 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
7387 mBackgroundSizeChanged = false;
7388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007389
Romain Guy24443ea2009-05-11 11:56:30 -07007390 if ((scrollX | scrollY) == 0) {
7391 background.draw(canvas);
7392 } else {
7393 canvas.translate(scrollX, scrollY);
7394 background.draw(canvas);
7395 canvas.translate(-scrollX, -scrollY);
7396 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007397 }
7398 }
7399
7400 // skip step 2 & 5 if possible (common case)
7401 final int viewFlags = mViewFlags;
7402 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
7403 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
7404 if (!verticalEdges && !horizontalEdges) {
7405 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07007406 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007407
7408 // Step 4, draw the children
7409 dispatchDraw(canvas);
7410
7411 // Step 6, draw decorations (scrollbars)
7412 onDrawScrollBars(canvas);
7413
7414 // we're done...
7415 return;
7416 }
7417
7418 /*
7419 * Here we do the full fledged routine...
7420 * (this is an uncommon case where speed matters less,
7421 * this is why we repeat some of the tests that have been
7422 * done above)
7423 */
7424
7425 boolean drawTop = false;
7426 boolean drawBottom = false;
7427 boolean drawLeft = false;
7428 boolean drawRight = false;
7429
7430 float topFadeStrength = 0.0f;
7431 float bottomFadeStrength = 0.0f;
7432 float leftFadeStrength = 0.0f;
7433 float rightFadeStrength = 0.0f;
7434
7435 // Step 2, save the canvas' layers
7436 int paddingLeft = mPaddingLeft;
7437 int paddingTop = mPaddingTop;
7438
7439 final boolean offsetRequired = isPaddingOffsetRequired();
7440 if (offsetRequired) {
7441 paddingLeft += getLeftPaddingOffset();
7442 paddingTop += getTopPaddingOffset();
7443 }
7444
7445 int left = mScrollX + paddingLeft;
7446 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
7447 int top = mScrollY + paddingTop;
7448 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
7449
7450 if (offsetRequired) {
7451 right += getRightPaddingOffset();
7452 bottom += getBottomPaddingOffset();
7453 }
7454
7455 final ScrollabilityCache scrollabilityCache = mScrollCache;
7456 int length = scrollabilityCache.fadingEdgeLength;
7457
7458 // clip the fade length if top and bottom fades overlap
7459 // overlapping fades produce odd-looking artifacts
7460 if (verticalEdges && (top + length > bottom - length)) {
7461 length = (bottom - top) / 2;
7462 }
7463
7464 // also clip horizontal fades if necessary
7465 if (horizontalEdges && (left + length > right - length)) {
7466 length = (right - left) / 2;
7467 }
7468
7469 if (verticalEdges) {
7470 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07007471 drawTop = topFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07007473 drawBottom = bottomFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007474 }
7475
7476 if (horizontalEdges) {
7477 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07007478 drawLeft = leftFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007479 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07007480 drawRight = rightFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007481 }
7482
7483 saveCount = canvas.getSaveCount();
7484
7485 int solidColor = getSolidColor();
7486 if (solidColor == 0) {
7487 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
7488
7489 if (drawTop) {
7490 canvas.saveLayer(left, top, right, top + length, null, flags);
7491 }
7492
7493 if (drawBottom) {
7494 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
7495 }
7496
7497 if (drawLeft) {
7498 canvas.saveLayer(left, top, left + length, bottom, null, flags);
7499 }
7500
7501 if (drawRight) {
7502 canvas.saveLayer(right - length, top, right, bottom, null, flags);
7503 }
7504 } else {
7505 scrollabilityCache.setFadeColor(solidColor);
7506 }
7507
7508 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07007509 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007510
7511 // Step 4, draw the children
7512 dispatchDraw(canvas);
7513
7514 // Step 5, draw the fade effect and restore layers
7515 final Paint p = scrollabilityCache.paint;
7516 final Matrix matrix = scrollabilityCache.matrix;
7517 final Shader fade = scrollabilityCache.shader;
7518 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
7519
7520 if (drawTop) {
7521 matrix.setScale(1, fadeHeight * topFadeStrength);
7522 matrix.postTranslate(left, top);
7523 fade.setLocalMatrix(matrix);
7524 canvas.drawRect(left, top, right, top + length, p);
7525 }
7526
7527 if (drawBottom) {
7528 matrix.setScale(1, fadeHeight * bottomFadeStrength);
7529 matrix.postRotate(180);
7530 matrix.postTranslate(left, bottom);
7531 fade.setLocalMatrix(matrix);
7532 canvas.drawRect(left, bottom - length, right, bottom, p);
7533 }
7534
7535 if (drawLeft) {
7536 matrix.setScale(1, fadeHeight * leftFadeStrength);
7537 matrix.postRotate(-90);
7538 matrix.postTranslate(left, top);
7539 fade.setLocalMatrix(matrix);
7540 canvas.drawRect(left, top, left + length, bottom, p);
7541 }
7542
7543 if (drawRight) {
7544 matrix.setScale(1, fadeHeight * rightFadeStrength);
7545 matrix.postRotate(90);
7546 matrix.postTranslate(right, top);
7547 fade.setLocalMatrix(matrix);
7548 canvas.drawRect(right - length, top, right, bottom, p);
7549 }
7550
7551 canvas.restoreToCount(saveCount);
7552
7553 // Step 6, draw decorations (scrollbars)
7554 onDrawScrollBars(canvas);
7555 }
7556
7557 /**
7558 * Override this if your view is known to always be drawn on top of a solid color background,
7559 * and needs to draw fading edges. Returning a non-zero color enables the view system to
7560 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
7561 * should be set to 0xFF.
7562 *
7563 * @see #setVerticalFadingEdgeEnabled
7564 * @see #setHorizontalFadingEdgeEnabled
7565 *
7566 * @return The known solid color background for this view, or 0 if the color may vary
7567 */
7568 public int getSolidColor() {
7569 return 0;
7570 }
7571
7572 /**
7573 * Build a human readable string representation of the specified view flags.
7574 *
7575 * @param flags the view flags to convert to a string
7576 * @return a String representing the supplied flags
7577 */
7578 private static String printFlags(int flags) {
7579 String output = "";
7580 int numFlags = 0;
7581 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
7582 output += "TAKES_FOCUS";
7583 numFlags++;
7584 }
7585
7586 switch (flags & VISIBILITY_MASK) {
7587 case INVISIBLE:
7588 if (numFlags > 0) {
7589 output += " ";
7590 }
7591 output += "INVISIBLE";
7592 // USELESS HERE numFlags++;
7593 break;
7594 case GONE:
7595 if (numFlags > 0) {
7596 output += " ";
7597 }
7598 output += "GONE";
7599 // USELESS HERE numFlags++;
7600 break;
7601 default:
7602 break;
7603 }
7604 return output;
7605 }
7606
7607 /**
7608 * Build a human readable string representation of the specified private
7609 * view flags.
7610 *
7611 * @param privateFlags the private view flags to convert to a string
7612 * @return a String representing the supplied flags
7613 */
7614 private static String printPrivateFlags(int privateFlags) {
7615 String output = "";
7616 int numFlags = 0;
7617
7618 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
7619 output += "WANTS_FOCUS";
7620 numFlags++;
7621 }
7622
7623 if ((privateFlags & FOCUSED) == FOCUSED) {
7624 if (numFlags > 0) {
7625 output += " ";
7626 }
7627 output += "FOCUSED";
7628 numFlags++;
7629 }
7630
7631 if ((privateFlags & SELECTED) == SELECTED) {
7632 if (numFlags > 0) {
7633 output += " ";
7634 }
7635 output += "SELECTED";
7636 numFlags++;
7637 }
7638
7639 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
7640 if (numFlags > 0) {
7641 output += " ";
7642 }
7643 output += "IS_ROOT_NAMESPACE";
7644 numFlags++;
7645 }
7646
7647 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
7648 if (numFlags > 0) {
7649 output += " ";
7650 }
7651 output += "HAS_BOUNDS";
7652 numFlags++;
7653 }
7654
7655 if ((privateFlags & DRAWN) == DRAWN) {
7656 if (numFlags > 0) {
7657 output += " ";
7658 }
7659 output += "DRAWN";
7660 // USELESS HERE numFlags++;
7661 }
7662 return output;
7663 }
7664
7665 /**
7666 * <p>Indicates whether or not this view's layout will be requested during
7667 * the next hierarchy layout pass.</p>
7668 *
7669 * @return true if the layout will be forced during next layout pass
7670 */
7671 public boolean isLayoutRequested() {
7672 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
7673 }
7674
7675 /**
7676 * Assign a size and position to a view and all of its
7677 * descendants
7678 *
7679 * <p>This is the second phase of the layout mechanism.
7680 * (The first is measuring). In this phase, each parent calls
7681 * layout on all of its children to position them.
7682 * This is typically done using the child measurements
7683 * that were stored in the measure pass().
7684 *
7685 * Derived classes with children should override
7686 * onLayout. In that method, they should
7687 * call layout on each of their their children.
7688 *
7689 * @param l Left position, relative to parent
7690 * @param t Top position, relative to parent
7691 * @param r Right position, relative to parent
7692 * @param b Bottom position, relative to parent
7693 */
7694 public final void layout(int l, int t, int r, int b) {
7695 boolean changed = setFrame(l, t, r, b);
7696 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
7697 if (ViewDebug.TRACE_HIERARCHY) {
7698 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
7699 }
7700
7701 onLayout(changed, l, t, r, b);
7702 mPrivateFlags &= ~LAYOUT_REQUIRED;
7703 }
7704 mPrivateFlags &= ~FORCE_LAYOUT;
7705 }
7706
7707 /**
7708 * Called from layout when this view should
7709 * assign a size and position to each of its children.
7710 *
7711 * Derived classes with children should override
7712 * this method and call layout on each of
7713 * their their children.
7714 * @param changed This is a new size or position for this view
7715 * @param left Left position, relative to parent
7716 * @param top Top position, relative to parent
7717 * @param right Right position, relative to parent
7718 * @param bottom Bottom position, relative to parent
7719 */
7720 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
7721 }
7722
7723 /**
7724 * Assign a size and position to this view.
7725 *
7726 * This is called from layout.
7727 *
7728 * @param left Left position, relative to parent
7729 * @param top Top position, relative to parent
7730 * @param right Right position, relative to parent
7731 * @param bottom Bottom position, relative to parent
7732 * @return true if the new size and position are different than the
7733 * previous ones
7734 * {@hide}
7735 */
7736 protected boolean setFrame(int left, int top, int right, int bottom) {
7737 boolean changed = false;
7738
7739 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07007740 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 + right + "," + bottom + ")");
7742 }
7743
7744 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
7745 changed = true;
7746
7747 // Remember our drawn bit
7748 int drawn = mPrivateFlags & DRAWN;
7749
7750 // Invalidate our old position
7751 invalidate();
7752
7753
7754 int oldWidth = mRight - mLeft;
7755 int oldHeight = mBottom - mTop;
7756
7757 mLeft = left;
7758 mTop = top;
7759 mRight = right;
7760 mBottom = bottom;
7761
7762 mPrivateFlags |= HAS_BOUNDS;
7763
7764 int newWidth = right - left;
7765 int newHeight = bottom - top;
7766
7767 if (newWidth != oldWidth || newHeight != oldHeight) {
7768 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
7769 }
7770
7771 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
7772 // If we are visible, force the DRAWN bit to on so that
7773 // this invalidate will go through (at least to our parent).
7774 // This is because someone may have invalidated this view
7775 // before this call to setFrame came in, therby clearing
7776 // the DRAWN bit.
7777 mPrivateFlags |= DRAWN;
7778 invalidate();
7779 }
7780
7781 // Reset drawn bit to original value (invalidate turns it off)
7782 mPrivateFlags |= drawn;
7783
7784 mBackgroundSizeChanged = true;
7785 }
7786 return changed;
7787 }
7788
7789 /**
7790 * Finalize inflating a view from XML. This is called as the last phase
7791 * of inflation, after all child views have been added.
7792 *
7793 * <p>Even if the subclass overrides onFinishInflate, they should always be
7794 * sure to call the super method, so that we get called.
7795 */
7796 protected void onFinishInflate() {
7797 }
7798
7799 /**
7800 * Returns the resources associated with this view.
7801 *
7802 * @return Resources object.
7803 */
7804 public Resources getResources() {
7805 return mResources;
7806 }
7807
7808 /**
7809 * Invalidates the specified Drawable.
7810 *
7811 * @param drawable the drawable to invalidate
7812 */
7813 public void invalidateDrawable(Drawable drawable) {
7814 if (verifyDrawable(drawable)) {
7815 final Rect dirty = drawable.getBounds();
7816 final int scrollX = mScrollX;
7817 final int scrollY = mScrollY;
7818
7819 invalidate(dirty.left + scrollX, dirty.top + scrollY,
7820 dirty.right + scrollX, dirty.bottom + scrollY);
7821 }
7822 }
7823
7824 /**
7825 * Schedules an action on a drawable to occur at a specified time.
7826 *
7827 * @param who the recipient of the action
7828 * @param what the action to run on the drawable
7829 * @param when the time at which the action must occur. Uses the
7830 * {@link SystemClock#uptimeMillis} timebase.
7831 */
7832 public void scheduleDrawable(Drawable who, Runnable what, long when) {
7833 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
7834 mAttachInfo.mHandler.postAtTime(what, who, when);
7835 }
7836 }
7837
7838 /**
7839 * Cancels a scheduled action on a drawable.
7840 *
7841 * @param who the recipient of the action
7842 * @param what the action to cancel
7843 */
7844 public void unscheduleDrawable(Drawable who, Runnable what) {
7845 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
7846 mAttachInfo.mHandler.removeCallbacks(what, who);
7847 }
7848 }
7849
7850 /**
7851 * Unschedule any events associated with the given Drawable. This can be
7852 * used when selecting a new Drawable into a view, so that the previous
7853 * one is completely unscheduled.
7854 *
7855 * @param who The Drawable to unschedule.
7856 *
7857 * @see #drawableStateChanged
7858 */
7859 public void unscheduleDrawable(Drawable who) {
7860 if (mAttachInfo != null) {
7861 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
7862 }
7863 }
7864
7865 /**
7866 * If your view subclass is displaying its own Drawable objects, it should
7867 * override this function and return true for any Drawable it is
7868 * displaying. This allows animations for those drawables to be
7869 * scheduled.
7870 *
7871 * <p>Be sure to call through to the super class when overriding this
7872 * function.
7873 *
7874 * @param who The Drawable to verify. Return true if it is one you are
7875 * displaying, else return the result of calling through to the
7876 * super class.
7877 *
7878 * @return boolean If true than the Drawable is being displayed in the
7879 * view; else false and it is not allowed to animate.
7880 *
7881 * @see #unscheduleDrawable
7882 * @see #drawableStateChanged
7883 */
7884 protected boolean verifyDrawable(Drawable who) {
7885 return who == mBGDrawable;
7886 }
7887
7888 /**
7889 * This function is called whenever the state of the view changes in such
7890 * a way that it impacts the state of drawables being shown.
7891 *
7892 * <p>Be sure to call through to the superclass when overriding this
7893 * function.
7894 *
7895 * @see Drawable#setState
7896 */
7897 protected void drawableStateChanged() {
7898 Drawable d = mBGDrawable;
7899 if (d != null && d.isStateful()) {
7900 d.setState(getDrawableState());
7901 }
7902 }
7903
7904 /**
7905 * Call this to force a view to update its drawable state. This will cause
7906 * drawableStateChanged to be called on this view. Views that are interested
7907 * in the new state should call getDrawableState.
7908 *
7909 * @see #drawableStateChanged
7910 * @see #getDrawableState
7911 */
7912 public void refreshDrawableState() {
7913 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
7914 drawableStateChanged();
7915
7916 ViewParent parent = mParent;
7917 if (parent != null) {
7918 parent.childDrawableStateChanged(this);
7919 }
7920 }
7921
7922 /**
7923 * Return an array of resource IDs of the drawable states representing the
7924 * current state of the view.
7925 *
7926 * @return The current drawable state
7927 *
7928 * @see Drawable#setState
7929 * @see #drawableStateChanged
7930 * @see #onCreateDrawableState
7931 */
7932 public final int[] getDrawableState() {
7933 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
7934 return mDrawableState;
7935 } else {
7936 mDrawableState = onCreateDrawableState(0);
7937 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
7938 return mDrawableState;
7939 }
7940 }
7941
7942 /**
7943 * Generate the new {@link android.graphics.drawable.Drawable} state for
7944 * this view. This is called by the view
7945 * system when the cached Drawable state is determined to be invalid. To
7946 * retrieve the current state, you should use {@link #getDrawableState}.
7947 *
7948 * @param extraSpace if non-zero, this is the number of extra entries you
7949 * would like in the returned array in which you can place your own
7950 * states.
7951 *
7952 * @return Returns an array holding the current {@link Drawable} state of
7953 * the view.
7954 *
7955 * @see #mergeDrawableStates
7956 */
7957 protected int[] onCreateDrawableState(int extraSpace) {
7958 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
7959 mParent instanceof View) {
7960 return ((View) mParent).onCreateDrawableState(extraSpace);
7961 }
7962
7963 int[] drawableState;
7964
7965 int privateFlags = mPrivateFlags;
7966
7967 int viewStateIndex = (((privateFlags & PRESSED) != 0) ? 1 : 0);
7968
7969 viewStateIndex = (viewStateIndex << 1)
7970 + (((mViewFlags & ENABLED_MASK) == ENABLED) ? 1 : 0);
7971
7972 viewStateIndex = (viewStateIndex << 1) + (isFocused() ? 1 : 0);
7973
7974 viewStateIndex = (viewStateIndex << 1)
7975 + (((privateFlags & SELECTED) != 0) ? 1 : 0);
7976
7977 final boolean hasWindowFocus = hasWindowFocus();
7978 viewStateIndex = (viewStateIndex << 1) + (hasWindowFocus ? 1 : 0);
7979
7980 drawableState = VIEW_STATE_SETS[viewStateIndex];
7981
7982 //noinspection ConstantIfStatement
7983 if (false) {
7984 Log.i("View", "drawableStateIndex=" + viewStateIndex);
7985 Log.i("View", toString()
7986 + " pressed=" + ((privateFlags & PRESSED) != 0)
7987 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
7988 + " fo=" + hasFocus()
7989 + " sl=" + ((privateFlags & SELECTED) != 0)
7990 + " wf=" + hasWindowFocus
7991 + ": " + Arrays.toString(drawableState));
7992 }
7993
7994 if (extraSpace == 0) {
7995 return drawableState;
7996 }
7997
7998 final int[] fullState;
7999 if (drawableState != null) {
8000 fullState = new int[drawableState.length + extraSpace];
8001 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
8002 } else {
8003 fullState = new int[extraSpace];
8004 }
8005
8006 return fullState;
8007 }
8008
8009 /**
8010 * Merge your own state values in <var>additionalState</var> into the base
8011 * state values <var>baseState</var> that were returned by
8012 * {@link #onCreateDrawableState}.
8013 *
8014 * @param baseState The base state values returned by
8015 * {@link #onCreateDrawableState}, which will be modified to also hold your
8016 * own additional state values.
8017 *
8018 * @param additionalState The additional state values you would like
8019 * added to <var>baseState</var>; this array is not modified.
8020 *
8021 * @return As a convenience, the <var>baseState</var> array you originally
8022 * passed into the function is returned.
8023 *
8024 * @see #onCreateDrawableState
8025 */
8026 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
8027 final int N = baseState.length;
8028 int i = N - 1;
8029 while (i >= 0 && baseState[i] == 0) {
8030 i--;
8031 }
8032 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
8033 return baseState;
8034 }
8035
8036 /**
8037 * Sets the background color for this view.
8038 * @param color the color of the background
8039 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00008040 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 public void setBackgroundColor(int color) {
8042 setBackgroundDrawable(new ColorDrawable(color));
8043 }
8044
8045 /**
8046 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07008047 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008048 * @param resid The identifier of the resource.
8049 * @attr ref android.R.styleable#View_background
8050 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00008051 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008052 public void setBackgroundResource(int resid) {
8053 if (resid != 0 && resid == mBackgroundResource) {
8054 return;
8055 }
8056
8057 Drawable d= null;
8058 if (resid != 0) {
8059 d = mResources.getDrawable(resid);
8060 }
8061 setBackgroundDrawable(d);
8062
8063 mBackgroundResource = resid;
8064 }
8065
8066 /**
8067 * Set the background to a given Drawable, or remove the background. If the
8068 * background has padding, this View's padding is set to the background's
8069 * padding. However, when a background is removed, this View's padding isn't
8070 * touched. If setting the padding is desired, please use
8071 * {@link #setPadding(int, int, int, int)}.
8072 *
8073 * @param d The Drawable to use as the background, or null to remove the
8074 * background
8075 */
8076 public void setBackgroundDrawable(Drawable d) {
8077 boolean requestLayout = false;
8078
8079 mBackgroundResource = 0;
8080
8081 /*
8082 * Regardless of whether we're setting a new background or not, we want
8083 * to clear the previous drawable.
8084 */
8085 if (mBGDrawable != null) {
8086 mBGDrawable.setCallback(null);
8087 unscheduleDrawable(mBGDrawable);
8088 }
8089
8090 if (d != null) {
8091 Rect padding = sThreadLocal.get();
8092 if (padding == null) {
8093 padding = new Rect();
8094 sThreadLocal.set(padding);
8095 }
8096 if (d.getPadding(padding)) {
8097 setPadding(padding.left, padding.top, padding.right, padding.bottom);
8098 }
8099
8100 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
8101 // if it has a different minimum size, we should layout again
8102 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
8103 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
8104 requestLayout = true;
8105 }
8106
8107 d.setCallback(this);
8108 if (d.isStateful()) {
8109 d.setState(getDrawableState());
8110 }
8111 d.setVisible(getVisibility() == VISIBLE, false);
8112 mBGDrawable = d;
8113
8114 if ((mPrivateFlags & SKIP_DRAW) != 0) {
8115 mPrivateFlags &= ~SKIP_DRAW;
8116 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
8117 requestLayout = true;
8118 }
8119 } else {
8120 /* Remove the background */
8121 mBGDrawable = null;
8122
8123 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
8124 /*
8125 * This view ONLY drew the background before and we're removing
8126 * the background, so now it won't draw anything
8127 * (hence we SKIP_DRAW)
8128 */
8129 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
8130 mPrivateFlags |= SKIP_DRAW;
8131 }
8132
8133 /*
8134 * When the background is set, we try to apply its padding to this
8135 * View. When the background is removed, we don't touch this View's
8136 * padding. This is noted in the Javadocs. Hence, we don't need to
8137 * requestLayout(), the invalidate() below is sufficient.
8138 */
8139
8140 // The old background's minimum size could have affected this
8141 // View's layout, so let's requestLayout
8142 requestLayout = true;
8143 }
8144
Romain Guy8f1344f52009-05-15 16:03:59 -07008145 computeOpaqueFlags();
8146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 if (requestLayout) {
8148 requestLayout();
8149 }
8150
8151 mBackgroundSizeChanged = true;
8152 invalidate();
8153 }
8154
8155 /**
8156 * Gets the background drawable
8157 * @return The drawable used as the background for this view, if any.
8158 */
8159 public Drawable getBackground() {
8160 return mBGDrawable;
8161 }
8162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 /**
8164 * Sets the padding. The view may add on the space required to display
8165 * the scrollbars, depending on the style and visibility of the scrollbars.
8166 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
8167 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
8168 * from the values set in this call.
8169 *
8170 * @attr ref android.R.styleable#View_padding
8171 * @attr ref android.R.styleable#View_paddingBottom
8172 * @attr ref android.R.styleable#View_paddingLeft
8173 * @attr ref android.R.styleable#View_paddingRight
8174 * @attr ref android.R.styleable#View_paddingTop
8175 * @param left the left padding in pixels
8176 * @param top the top padding in pixels
8177 * @param right the right padding in pixels
8178 * @param bottom the bottom padding in pixels
8179 */
8180 public void setPadding(int left, int top, int right, int bottom) {
8181 boolean changed = false;
8182
8183 mUserPaddingRight = right;
8184 mUserPaddingBottom = bottom;
8185
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008186 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07008187
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008188 // Common case is there are no scroll bars.
8189 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
8190 // TODO: Deal with RTL languages to adjust left padding instead of right.
8191 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
8192 right += (viewFlags & SCROLLBARS_INSET_MASK) == 0
8193 ? 0 : getVerticalScrollbarWidth();
8194 }
8195 if ((viewFlags & SCROLLBARS_HORIZONTAL) == 0) {
8196 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
8197 ? 0 : getHorizontalScrollbarHeight();
8198 }
8199 }
Romain Guy8506ab42009-06-11 17:35:47 -07008200
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008201 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008202 changed = true;
8203 mPaddingLeft = left;
8204 }
8205 if (mPaddingTop != top) {
8206 changed = true;
8207 mPaddingTop = top;
8208 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008209 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008211 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008213 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07008215 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 }
8217
8218 if (changed) {
8219 requestLayout();
8220 }
8221 }
8222
8223 /**
8224 * Returns the top padding of this view.
8225 *
8226 * @return the top padding in pixels
8227 */
8228 public int getPaddingTop() {
8229 return mPaddingTop;
8230 }
8231
8232 /**
8233 * Returns the bottom padding of this view. If there are inset and enabled
8234 * scrollbars, this value may include the space required to display the
8235 * scrollbars as well.
8236 *
8237 * @return the bottom padding in pixels
8238 */
8239 public int getPaddingBottom() {
8240 return mPaddingBottom;
8241 }
8242
8243 /**
8244 * Returns the left padding of this view. If there are inset and enabled
8245 * scrollbars, this value may include the space required to display the
8246 * scrollbars as well.
8247 *
8248 * @return the left padding in pixels
8249 */
8250 public int getPaddingLeft() {
8251 return mPaddingLeft;
8252 }
8253
8254 /**
8255 * Returns the right padding of this view. If there are inset and enabled
8256 * scrollbars, this value may include the space required to display the
8257 * scrollbars as well.
8258 *
8259 * @return the right padding in pixels
8260 */
8261 public int getPaddingRight() {
8262 return mPaddingRight;
8263 }
8264
8265 /**
8266 * Changes the selection state of this view. A view can be selected or not.
8267 * Note that selection is not the same as focus. Views are typically
8268 * selected in the context of an AdapterView like ListView or GridView;
8269 * the selected view is the view that is highlighted.
8270 *
8271 * @param selected true if the view must be selected, false otherwise
8272 */
8273 public void setSelected(boolean selected) {
8274 if (((mPrivateFlags & SELECTED) != 0) != selected) {
8275 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -07008276 if (!selected) resetPressedState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008277 invalidate();
8278 refreshDrawableState();
8279 dispatchSetSelected(selected);
8280 }
8281 }
8282
8283 /**
8284 * Dispatch setSelected to all of this View's children.
8285 *
8286 * @see #setSelected(boolean)
8287 *
8288 * @param selected The new selected state
8289 */
8290 protected void dispatchSetSelected(boolean selected) {
8291 }
8292
8293 /**
8294 * Indicates the selection state of this view.
8295 *
8296 * @return true if the view is selected, false otherwise
8297 */
8298 @ViewDebug.ExportedProperty
8299 public boolean isSelected() {
8300 return (mPrivateFlags & SELECTED) != 0;
8301 }
8302
8303 /**
8304 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
8305 * observer can be used to get notifications when global events, like
8306 * layout, happen.
8307 *
8308 * The returned ViewTreeObserver observer is not guaranteed to remain
8309 * valid for the lifetime of this View. If the caller of this method keeps
8310 * a long-lived reference to ViewTreeObserver, it should always check for
8311 * the return value of {@link ViewTreeObserver#isAlive()}.
8312 *
8313 * @return The ViewTreeObserver for this view's hierarchy.
8314 */
8315 public ViewTreeObserver getViewTreeObserver() {
8316 if (mAttachInfo != null) {
8317 return mAttachInfo.mTreeObserver;
8318 }
8319 if (mFloatingTreeObserver == null) {
8320 mFloatingTreeObserver = new ViewTreeObserver();
8321 }
8322 return mFloatingTreeObserver;
8323 }
8324
8325 /**
8326 * <p>Finds the topmost view in the current view hierarchy.</p>
8327 *
8328 * @return the topmost view containing this view
8329 */
8330 public View getRootView() {
8331 if (mAttachInfo != null) {
8332 final View v = mAttachInfo.mRootView;
8333 if (v != null) {
8334 return v;
8335 }
8336 }
Romain Guy8506ab42009-06-11 17:35:47 -07008337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 View parent = this;
8339
8340 while (parent.mParent != null && parent.mParent instanceof View) {
8341 parent = (View) parent.mParent;
8342 }
8343
8344 return parent;
8345 }
8346
8347 /**
8348 * <p>Computes the coordinates of this view on the screen. The argument
8349 * must be an array of two integers. After the method returns, the array
8350 * contains the x and y location in that order.</p>
8351 *
8352 * @param location an array of two integers in which to hold the coordinates
8353 */
8354 public void getLocationOnScreen(int[] location) {
8355 getLocationInWindow(location);
8356
8357 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -07008358 if (info != null) {
8359 location[0] += info.mWindowLeft;
8360 location[1] += info.mWindowTop;
8361 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 }
8363
8364 /**
8365 * <p>Computes the coordinates of this view in its window. The argument
8366 * must be an array of two integers. After the method returns, the array
8367 * contains the x and y location in that order.</p>
8368 *
8369 * @param location an array of two integers in which to hold the coordinates
8370 */
8371 public void getLocationInWindow(int[] location) {
8372 if (location == null || location.length < 2) {
8373 throw new IllegalArgumentException("location must be an array of "
8374 + "two integers");
8375 }
8376
8377 location[0] = mLeft;
8378 location[1] = mTop;
8379
8380 ViewParent viewParent = mParent;
8381 while (viewParent instanceof View) {
8382 final View view = (View)viewParent;
8383 location[0] += view.mLeft - view.mScrollX;
8384 location[1] += view.mTop - view.mScrollY;
8385 viewParent = view.mParent;
8386 }
Romain Guy8506ab42009-06-11 17:35:47 -07008387
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008388 if (viewParent instanceof ViewRoot) {
8389 // *cough*
8390 final ViewRoot vr = (ViewRoot)viewParent;
8391 location[1] -= vr.mCurScrollY;
8392 }
8393 }
8394
8395 /**
8396 * {@hide}
8397 * @param id the id of the view to be found
8398 * @return the view of the specified id, null if cannot be found
8399 */
8400 protected View findViewTraversal(int id) {
8401 if (id == mID) {
8402 return this;
8403 }
8404 return null;
8405 }
8406
8407 /**
8408 * {@hide}
8409 * @param tag the tag of the view to be found
8410 * @return the view of specified tag, null if cannot be found
8411 */
8412 protected View findViewWithTagTraversal(Object tag) {
8413 if (tag != null && tag.equals(mTag)) {
8414 return this;
8415 }
8416 return null;
8417 }
8418
8419 /**
8420 * Look for a child view with the given id. If this view has the given
8421 * id, return this view.
8422 *
8423 * @param id The id to search for.
8424 * @return The view that has the given id in the hierarchy or null
8425 */
8426 public final View findViewById(int id) {
8427 if (id < 0) {
8428 return null;
8429 }
8430 return findViewTraversal(id);
8431 }
8432
8433 /**
8434 * Look for a child view with the given tag. If this view has the given
8435 * tag, return this view.
8436 *
8437 * @param tag The tag to search for, using "tag.equals(getTag())".
8438 * @return The View that has the given tag in the hierarchy or null
8439 */
8440 public final View findViewWithTag(Object tag) {
8441 if (tag == null) {
8442 return null;
8443 }
8444 return findViewWithTagTraversal(tag);
8445 }
8446
8447 /**
8448 * Sets the identifier for this view. The identifier does not have to be
8449 * unique in this view's hierarchy. The identifier should be a positive
8450 * number.
8451 *
8452 * @see #NO_ID
8453 * @see #getId
8454 * @see #findViewById
8455 *
8456 * @param id a number used to identify the view
8457 *
8458 * @attr ref android.R.styleable#View_id
8459 */
8460 public void setId(int id) {
8461 mID = id;
8462 }
8463
8464 /**
8465 * {@hide}
8466 *
8467 * @param isRoot true if the view belongs to the root namespace, false
8468 * otherwise
8469 */
8470 public void setIsRootNamespace(boolean isRoot) {
8471 if (isRoot) {
8472 mPrivateFlags |= IS_ROOT_NAMESPACE;
8473 } else {
8474 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
8475 }
8476 }
8477
8478 /**
8479 * {@hide}
8480 *
8481 * @return true if the view belongs to the root namespace, false otherwise
8482 */
8483 public boolean isRootNamespace() {
8484 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
8485 }
8486
8487 /**
8488 * Returns this view's identifier.
8489 *
8490 * @return a positive integer used to identify the view or {@link #NO_ID}
8491 * if the view has no ID
8492 *
8493 * @see #setId
8494 * @see #findViewById
8495 * @attr ref android.R.styleable#View_id
8496 */
8497 @ViewDebug.CapturedViewProperty
8498 public int getId() {
8499 return mID;
8500 }
8501
8502 /**
8503 * Returns this view's tag.
8504 *
8505 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -07008506 *
8507 * @see #setTag(Object)
8508 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 */
8510 @ViewDebug.ExportedProperty
8511 public Object getTag() {
8512 return mTag;
8513 }
8514
8515 /**
8516 * Sets the tag associated with this view. A tag can be used to mark
8517 * a view in its hierarchy and does not have to be unique within the
8518 * hierarchy. Tags can also be used to store data within a view without
8519 * resorting to another data structure.
8520 *
8521 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -07008522 *
8523 * @see #getTag()
8524 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008525 */
8526 public void setTag(final Object tag) {
8527 mTag = tag;
8528 }
8529
8530 /**
Romain Guyd90a3312009-05-06 14:54:28 -07008531 * Returns the tag associated with this view and the specified key.
8532 *
8533 * @param key The key identifying the tag
8534 *
8535 * @return the Object stored in this view as a tag
8536 *
8537 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -07008538 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -07008539 */
8540 public Object getTag(int key) {
8541 SparseArray<Object> tags = null;
8542 synchronized (sTagsLock) {
8543 if (sTags != null) {
8544 tags = sTags.get(this);
8545 }
8546 }
8547
8548 if (tags != null) return tags.get(key);
8549 return null;
8550 }
8551
8552 /**
8553 * Sets a tag associated with this view and a key. A tag can be used
8554 * to mark a view in its hierarchy and does not have to be unique within
8555 * the hierarchy. Tags can also be used to store data within a view
8556 * without resorting to another data structure.
8557 *
8558 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -07008559 * application to ensure it is unique (see the <a
8560 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
8561 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -07008562 * the Android framework or not associated with any package will cause
8563 * an {@link IllegalArgumentException} to be thrown.
8564 *
8565 * @param key The key identifying the tag
8566 * @param tag An Object to tag the view with
8567 *
8568 * @throws IllegalArgumentException If they specified key is not valid
8569 *
8570 * @see #setTag(Object)
8571 * @see #getTag(int)
8572 */
8573 public void setTag(int key, final Object tag) {
8574 // If the package id is 0x00 or 0x01, it's either an undefined package
8575 // or a framework id
8576 if ((key >>> 24) < 2) {
8577 throw new IllegalArgumentException("The key must be an application-specific "
8578 + "resource id.");
8579 }
8580
8581 setTagInternal(this, key, tag);
8582 }
8583
8584 /**
8585 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
8586 * framework id.
8587 *
8588 * @hide
8589 */
8590 public void setTagInternal(int key, Object tag) {
8591 if ((key >>> 24) != 0x1) {
8592 throw new IllegalArgumentException("The key must be a framework-specific "
8593 + "resource id.");
8594 }
8595
Romain Guy8506ab42009-06-11 17:35:47 -07008596 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -07008597 }
8598
8599 private static void setTagInternal(View view, int key, Object tag) {
8600 SparseArray<Object> tags = null;
8601 synchronized (sTagsLock) {
8602 if (sTags == null) {
8603 sTags = new WeakHashMap<View, SparseArray<Object>>();
8604 } else {
8605 tags = sTags.get(view);
8606 }
8607 }
8608
8609 if (tags == null) {
8610 tags = new SparseArray<Object>(2);
8611 synchronized (sTagsLock) {
8612 sTags.put(view, tags);
8613 }
8614 }
8615
8616 tags.put(key, tag);
8617 }
8618
8619 /**
Romain Guy13922e02009-05-12 17:56:14 -07008620 * @param consistency The type of consistency. See ViewDebug for more information.
8621 *
8622 * @hide
8623 */
8624 protected boolean dispatchConsistencyCheck(int consistency) {
8625 return onConsistencyCheck(consistency);
8626 }
8627
8628 /**
8629 * Method that subclasses should implement to check their consistency. The type of
8630 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -07008631 *
Romain Guy13922e02009-05-12 17:56:14 -07008632 * @param consistency The type of consistency. See ViewDebug for more information.
8633 *
8634 * @throws IllegalStateException if the view is in an inconsistent state.
8635 *
8636 * @hide
8637 */
8638 protected boolean onConsistencyCheck(int consistency) {
8639 boolean result = true;
8640
8641 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
8642 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
8643
8644 if (checkLayout) {
8645 if (getParent() == null) {
8646 result = false;
8647 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
8648 "View " + this + " does not have a parent.");
8649 }
8650
8651 if (mAttachInfo == null) {
8652 result = false;
8653 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
8654 "View " + this + " is not attached to a window.");
8655 }
8656 }
8657
8658 if (checkDrawing) {
8659 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
8660 // from their draw() method
8661
8662 if ((mPrivateFlags & DRAWN) != DRAWN &&
8663 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
8664 result = false;
8665 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
8666 "View " + this + " was invalidated but its drawing cache is valid.");
8667 }
8668 }
8669
8670 return result;
8671 }
8672
8673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008674 * Prints information about this view in the log output, with the tag
8675 * {@link #VIEW_LOG_TAG}.
8676 *
8677 * @hide
8678 */
8679 public void debug() {
8680 debug(0);
8681 }
8682
8683 /**
8684 * Prints information about this view in the log output, with the tag
8685 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
8686 * indentation defined by the <code>depth</code>.
8687 *
8688 * @param depth the indentation level
8689 *
8690 * @hide
8691 */
8692 protected void debug(int depth) {
8693 String output = debugIndent(depth - 1);
8694
8695 output += "+ " + this;
8696 int id = getId();
8697 if (id != -1) {
8698 output += " (id=" + id + ")";
8699 }
8700 Object tag = getTag();
8701 if (tag != null) {
8702 output += " (tag=" + tag + ")";
8703 }
8704 Log.d(VIEW_LOG_TAG, output);
8705
8706 if ((mPrivateFlags & FOCUSED) != 0) {
8707 output = debugIndent(depth) + " FOCUSED";
8708 Log.d(VIEW_LOG_TAG, output);
8709 }
8710
8711 output = debugIndent(depth);
8712 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
8713 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
8714 + "} ";
8715 Log.d(VIEW_LOG_TAG, output);
8716
8717 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
8718 || mPaddingBottom != 0) {
8719 output = debugIndent(depth);
8720 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
8721 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
8722 Log.d(VIEW_LOG_TAG, output);
8723 }
8724
8725 output = debugIndent(depth);
8726 output += "mMeasureWidth=" + mMeasuredWidth +
8727 " mMeasureHeight=" + mMeasuredHeight;
8728 Log.d(VIEW_LOG_TAG, output);
8729
8730 output = debugIndent(depth);
8731 if (mLayoutParams == null) {
8732 output += "BAD! no layout params";
8733 } else {
8734 output = mLayoutParams.debug(output);
8735 }
8736 Log.d(VIEW_LOG_TAG, output);
8737
8738 output = debugIndent(depth);
8739 output += "flags={";
8740 output += View.printFlags(mViewFlags);
8741 output += "}";
8742 Log.d(VIEW_LOG_TAG, output);
8743
8744 output = debugIndent(depth);
8745 output += "privateFlags={";
8746 output += View.printPrivateFlags(mPrivateFlags);
8747 output += "}";
8748 Log.d(VIEW_LOG_TAG, output);
8749 }
8750
8751 /**
8752 * Creates an string of whitespaces used for indentation.
8753 *
8754 * @param depth the indentation level
8755 * @return a String containing (depth * 2 + 3) * 2 white spaces
8756 *
8757 * @hide
8758 */
8759 protected static String debugIndent(int depth) {
8760 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
8761 for (int i = 0; i < (depth * 2) + 3; i++) {
8762 spaces.append(' ').append(' ');
8763 }
8764 return spaces.toString();
8765 }
8766
8767 /**
8768 * <p>Return the offset of the widget's text baseline from the widget's top
8769 * boundary. If this widget does not support baseline alignment, this
8770 * method returns -1. </p>
8771 *
8772 * @return the offset of the baseline within the widget's bounds or -1
8773 * if baseline alignment is not supported
8774 */
8775 @ViewDebug.ExportedProperty
8776 public int getBaseline() {
8777 return -1;
8778 }
8779
8780 /**
8781 * Call this when something has changed which has invalidated the
8782 * layout of this view. This will schedule a layout pass of the view
8783 * tree.
8784 */
8785 public void requestLayout() {
8786 if (ViewDebug.TRACE_HIERARCHY) {
8787 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
8788 }
8789
8790 mPrivateFlags |= FORCE_LAYOUT;
8791
8792 if (mParent != null && !mParent.isLayoutRequested()) {
8793 mParent.requestLayout();
8794 }
8795 }
8796
8797 /**
8798 * Forces this view to be laid out during the next layout pass.
8799 * This method does not call requestLayout() or forceLayout()
8800 * on the parent.
8801 */
8802 public void forceLayout() {
8803 mPrivateFlags |= FORCE_LAYOUT;
8804 }
8805
8806 /**
8807 * <p>
8808 * This is called to find out how big a view should be. The parent
8809 * supplies constraint information in the width and height parameters.
8810 * </p>
8811 *
8812 * <p>
8813 * The actual mesurement work of a view is performed in
8814 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
8815 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
8816 * </p>
8817 *
8818 *
8819 * @param widthMeasureSpec Horizontal space requirements as imposed by the
8820 * parent
8821 * @param heightMeasureSpec Vertical space requirements as imposed by the
8822 * parent
8823 *
8824 * @see #onMeasure(int, int)
8825 */
8826 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
8827 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
8828 widthMeasureSpec != mOldWidthMeasureSpec ||
8829 heightMeasureSpec != mOldHeightMeasureSpec) {
8830
8831 // first clears the measured dimension flag
8832 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
8833
8834 if (ViewDebug.TRACE_HIERARCHY) {
8835 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
8836 }
8837
8838 // measure ourselves, this should set the measured dimension flag back
8839 onMeasure(widthMeasureSpec, heightMeasureSpec);
8840
8841 // flag not set, setMeasuredDimension() was not invoked, we raise
8842 // an exception to warn the developer
8843 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
8844 throw new IllegalStateException("onMeasure() did not set the"
8845 + " measured dimension by calling"
8846 + " setMeasuredDimension()");
8847 }
8848
8849 mPrivateFlags |= LAYOUT_REQUIRED;
8850 }
8851
8852 mOldWidthMeasureSpec = widthMeasureSpec;
8853 mOldHeightMeasureSpec = heightMeasureSpec;
8854 }
8855
8856 /**
8857 * <p>
8858 * Measure the view and its content to determine the measured width and the
8859 * measured height. This method is invoked by {@link #measure(int, int)} and
8860 * should be overriden by subclasses to provide accurate and efficient
8861 * measurement of their contents.
8862 * </p>
8863 *
8864 * <p>
8865 * <strong>CONTRACT:</strong> When overriding this method, you
8866 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
8867 * measured width and height of this view. Failure to do so will trigger an
8868 * <code>IllegalStateException</code>, thrown by
8869 * {@link #measure(int, int)}. Calling the superclass'
8870 * {@link #onMeasure(int, int)} is a valid use.
8871 * </p>
8872 *
8873 * <p>
8874 * The base class implementation of measure defaults to the background size,
8875 * unless a larger size is allowed by the MeasureSpec. Subclasses should
8876 * override {@link #onMeasure(int, int)} to provide better measurements of
8877 * their content.
8878 * </p>
8879 *
8880 * <p>
8881 * If this method is overridden, it is the subclass's responsibility to make
8882 * sure the measured height and width are at least the view's minimum height
8883 * and width ({@link #getSuggestedMinimumHeight()} and
8884 * {@link #getSuggestedMinimumWidth()}).
8885 * </p>
8886 *
8887 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
8888 * The requirements are encoded with
8889 * {@link android.view.View.MeasureSpec}.
8890 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
8891 * The requirements are encoded with
8892 * {@link android.view.View.MeasureSpec}.
8893 *
8894 * @see #getMeasuredWidth()
8895 * @see #getMeasuredHeight()
8896 * @see #setMeasuredDimension(int, int)
8897 * @see #getSuggestedMinimumHeight()
8898 * @see #getSuggestedMinimumWidth()
8899 * @see android.view.View.MeasureSpec#getMode(int)
8900 * @see android.view.View.MeasureSpec#getSize(int)
8901 */
8902 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
8903 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
8904 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
8905 }
8906
8907 /**
8908 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
8909 * measured width and measured height. Failing to do so will trigger an
8910 * exception at measurement time.</p>
8911 *
8912 * @param measuredWidth the measured width of this view
8913 * @param measuredHeight the measured height of this view
8914 */
8915 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
8916 mMeasuredWidth = measuredWidth;
8917 mMeasuredHeight = measuredHeight;
8918
8919 mPrivateFlags |= MEASURED_DIMENSION_SET;
8920 }
8921
8922 /**
8923 * Utility to reconcile a desired size with constraints imposed by a MeasureSpec.
8924 * Will take the desired size, unless a different size is imposed by the constraints.
8925 *
8926 * @param size How big the view wants to be
8927 * @param measureSpec Constraints imposed by the parent
8928 * @return The size this view should be.
8929 */
8930 public static int resolveSize(int size, int measureSpec) {
8931 int result = size;
8932 int specMode = MeasureSpec.getMode(measureSpec);
8933 int specSize = MeasureSpec.getSize(measureSpec);
8934 switch (specMode) {
8935 case MeasureSpec.UNSPECIFIED:
8936 result = size;
8937 break;
8938 case MeasureSpec.AT_MOST:
8939 result = Math.min(size, specSize);
8940 break;
8941 case MeasureSpec.EXACTLY:
8942 result = specSize;
8943 break;
8944 }
8945 return result;
8946 }
8947
8948 /**
8949 * Utility to return a default size. Uses the supplied size if the
8950 * MeasureSpec imposed no contraints. Will get larger if allowed
8951 * by the MeasureSpec.
8952 *
8953 * @param size Default size for this view
8954 * @param measureSpec Constraints imposed by the parent
8955 * @return The size this view should be.
8956 */
8957 public static int getDefaultSize(int size, int measureSpec) {
8958 int result = size;
8959 int specMode = MeasureSpec.getMode(measureSpec);
8960 int specSize = MeasureSpec.getSize(measureSpec);
8961
8962 switch (specMode) {
8963 case MeasureSpec.UNSPECIFIED:
8964 result = size;
8965 break;
8966 case MeasureSpec.AT_MOST:
8967 case MeasureSpec.EXACTLY:
8968 result = specSize;
8969 break;
8970 }
8971 return result;
8972 }
8973
8974 /**
8975 * Returns the suggested minimum height that the view should use. This
8976 * returns the maximum of the view's minimum height
8977 * and the background's minimum height
8978 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
8979 * <p>
8980 * When being used in {@link #onMeasure(int, int)}, the caller should still
8981 * ensure the returned height is within the requirements of the parent.
8982 *
8983 * @return The suggested minimum height of the view.
8984 */
8985 protected int getSuggestedMinimumHeight() {
8986 int suggestedMinHeight = mMinHeight;
8987
8988 if (mBGDrawable != null) {
8989 final int bgMinHeight = mBGDrawable.getMinimumHeight();
8990 if (suggestedMinHeight < bgMinHeight) {
8991 suggestedMinHeight = bgMinHeight;
8992 }
8993 }
8994
8995 return suggestedMinHeight;
8996 }
8997
8998 /**
8999 * Returns the suggested minimum width that the view should use. This
9000 * returns the maximum of the view's minimum width)
9001 * and the background's minimum width
9002 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
9003 * <p>
9004 * When being used in {@link #onMeasure(int, int)}, the caller should still
9005 * ensure the returned width is within the requirements of the parent.
9006 *
9007 * @return The suggested minimum width of the view.
9008 */
9009 protected int getSuggestedMinimumWidth() {
9010 int suggestedMinWidth = mMinWidth;
9011
9012 if (mBGDrawable != null) {
9013 final int bgMinWidth = mBGDrawable.getMinimumWidth();
9014 if (suggestedMinWidth < bgMinWidth) {
9015 suggestedMinWidth = bgMinWidth;
9016 }
9017 }
9018
9019 return suggestedMinWidth;
9020 }
9021
9022 /**
9023 * Sets the minimum height of the view. It is not guaranteed the view will
9024 * be able to achieve this minimum height (for example, if its parent layout
9025 * constrains it with less available height).
9026 *
9027 * @param minHeight The minimum height the view will try to be.
9028 */
9029 public void setMinimumHeight(int minHeight) {
9030 mMinHeight = minHeight;
9031 }
9032
9033 /**
9034 * Sets the minimum width of the view. It is not guaranteed the view will
9035 * be able to achieve this minimum width (for example, if its parent layout
9036 * constrains it with less available width).
9037 *
9038 * @param minWidth The minimum width the view will try to be.
9039 */
9040 public void setMinimumWidth(int minWidth) {
9041 mMinWidth = minWidth;
9042 }
9043
9044 /**
9045 * Get the animation currently associated with this view.
9046 *
9047 * @return The animation that is currently playing or
9048 * scheduled to play for this view.
9049 */
9050 public Animation getAnimation() {
9051 return mCurrentAnimation;
9052 }
9053
9054 /**
9055 * Start the specified animation now.
9056 *
9057 * @param animation the animation to start now
9058 */
9059 public void startAnimation(Animation animation) {
9060 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
9061 setAnimation(animation);
9062 invalidate();
9063 }
9064
9065 /**
9066 * Cancels any animations for this view.
9067 */
9068 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -08009069 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -08009070 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -08009071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009072 mCurrentAnimation = null;
9073 }
9074
9075 /**
9076 * Sets the next animation to play for this view.
9077 * If you want the animation to play immediately, use
9078 * startAnimation. This method provides allows fine-grained
9079 * control over the start time and invalidation, but you
9080 * must make sure that 1) the animation has a start time set, and
9081 * 2) the view will be invalidated when the animation is supposed to
9082 * start.
9083 *
9084 * @param animation The next animation, or null.
9085 */
9086 public void setAnimation(Animation animation) {
9087 mCurrentAnimation = animation;
9088 if (animation != null) {
9089 animation.reset();
9090 }
9091 }
9092
9093 /**
9094 * Invoked by a parent ViewGroup to notify the start of the animation
9095 * currently associated with this view. If you override this method,
9096 * always call super.onAnimationStart();
9097 *
9098 * @see #setAnimation(android.view.animation.Animation)
9099 * @see #getAnimation()
9100 */
9101 protected void onAnimationStart() {
9102 mPrivateFlags |= ANIMATION_STARTED;
9103 }
9104
9105 /**
9106 * Invoked by a parent ViewGroup to notify the end of the animation
9107 * currently associated with this view. If you override this method,
9108 * always call super.onAnimationEnd();
9109 *
9110 * @see #setAnimation(android.view.animation.Animation)
9111 * @see #getAnimation()
9112 */
9113 protected void onAnimationEnd() {
9114 mPrivateFlags &= ~ANIMATION_STARTED;
9115 }
9116
9117 /**
9118 * Invoked if there is a Transform that involves alpha. Subclass that can
9119 * draw themselves with the specified alpha should return true, and then
9120 * respect that alpha when their onDraw() is called. If this returns false
9121 * then the view may be redirected to draw into an offscreen buffer to
9122 * fulfill the request, which will look fine, but may be slower than if the
9123 * subclass handles it internally. The default implementation returns false.
9124 *
9125 * @param alpha The alpha (0..255) to apply to the view's drawing
9126 * @return true if the view can draw with the specified alpha.
9127 */
9128 protected boolean onSetAlpha(int alpha) {
9129 return false;
9130 }
9131
9132 /**
9133 * This is used by the RootView to perform an optimization when
9134 * the view hierarchy contains one or several SurfaceView.
9135 * SurfaceView is always considered transparent, but its children are not,
9136 * therefore all View objects remove themselves from the global transparent
9137 * region (passed as a parameter to this function).
9138 *
9139 * @param region The transparent region for this ViewRoot (window).
9140 *
9141 * @return Returns true if the effective visibility of the view at this
9142 * point is opaque, regardless of the transparent region; returns false
9143 * if it is possible for underlying windows to be seen behind the view.
9144 *
9145 * {@hide}
9146 */
9147 public boolean gatherTransparentRegion(Region region) {
9148 final AttachInfo attachInfo = mAttachInfo;
9149 if (region != null && attachInfo != null) {
9150 final int pflags = mPrivateFlags;
9151 if ((pflags & SKIP_DRAW) == 0) {
9152 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
9153 // remove it from the transparent region.
9154 final int[] location = attachInfo.mTransparentLocation;
9155 getLocationInWindow(location);
9156 region.op(location[0], location[1], location[0] + mRight - mLeft,
9157 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
9158 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
9159 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
9160 // exists, so we remove the background drawable's non-transparent
9161 // parts from this transparent region.
9162 applyDrawableToTransparentRegion(mBGDrawable, region);
9163 }
9164 }
9165 return true;
9166 }
9167
9168 /**
9169 * Play a sound effect for this view.
9170 *
9171 * <p>The framework will play sound effects for some built in actions, such as
9172 * clicking, but you may wish to play these effects in your widget,
9173 * for instance, for internal navigation.
9174 *
9175 * <p>The sound effect will only be played if sound effects are enabled by the user, and
9176 * {@link #isSoundEffectsEnabled()} is true.
9177 *
9178 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
9179 */
9180 public void playSoundEffect(int soundConstant) {
9181 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
9182 return;
9183 }
9184 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
9185 }
9186
9187 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07009188 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -07009189 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07009190 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009191 *
9192 * <p>The framework will provide haptic feedback for some built in actions,
9193 * such as long presses, but you may wish to provide feedback for your
9194 * own widget.
9195 *
9196 * <p>The feedback will only be performed if
9197 * {@link #isHapticFeedbackEnabled()} is true.
9198 *
9199 * @param feedbackConstant One of the constants defined in
9200 * {@link HapticFeedbackConstants}
9201 */
9202 public boolean performHapticFeedback(int feedbackConstant) {
9203 return performHapticFeedback(feedbackConstant, 0);
9204 }
9205
9206 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07009207 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -07009208 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -07009209 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 *
9211 * @param feedbackConstant One of the constants defined in
9212 * {@link HapticFeedbackConstants}
9213 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
9214 */
9215 public boolean performHapticFeedback(int feedbackConstant, int flags) {
9216 if (mAttachInfo == null) {
9217 return false;
9218 }
Romain Guy812ccbe2010-06-01 14:07:24 -07009219 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009220 && !isHapticFeedbackEnabled()) {
9221 return false;
9222 }
Romain Guy812ccbe2010-06-01 14:07:24 -07009223 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
9224 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009225 }
9226
9227 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -07009228 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
9229 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -07009230 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -07009231 */
9232 public void onCloseSystemDialogs(String reason) {
9233 }
9234
9235 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236 * Given a Drawable whose bounds have been set to draw into this view,
9237 * update a Region being computed for {@link #gatherTransparentRegion} so
9238 * that any non-transparent parts of the Drawable are removed from the
9239 * given transparent region.
9240 *
9241 * @param dr The Drawable whose transparency is to be applied to the region.
9242 * @param region A Region holding the current transparency information,
9243 * where any parts of the region that are set are considered to be
9244 * transparent. On return, this region will be modified to have the
9245 * transparency information reduced by the corresponding parts of the
9246 * Drawable that are not transparent.
9247 * {@hide}
9248 */
9249 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
9250 if (DBG) {
9251 Log.i("View", "Getting transparent region for: " + this);
9252 }
9253 final Region r = dr.getTransparentRegion();
9254 final Rect db = dr.getBounds();
9255 final AttachInfo attachInfo = mAttachInfo;
9256 if (r != null && attachInfo != null) {
9257 final int w = getRight()-getLeft();
9258 final int h = getBottom()-getTop();
9259 if (db.left > 0) {
9260 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
9261 r.op(0, 0, db.left, h, Region.Op.UNION);
9262 }
9263 if (db.right < w) {
9264 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
9265 r.op(db.right, 0, w, h, Region.Op.UNION);
9266 }
9267 if (db.top > 0) {
9268 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
9269 r.op(0, 0, w, db.top, Region.Op.UNION);
9270 }
9271 if (db.bottom < h) {
9272 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
9273 r.op(0, db.bottom, w, h, Region.Op.UNION);
9274 }
9275 final int[] location = attachInfo.mTransparentLocation;
9276 getLocationInWindow(location);
9277 r.translate(location[0], location[1]);
9278 region.op(r, Region.Op.INTERSECT);
9279 } else {
9280 region.op(db, Region.Op.DIFFERENCE);
9281 }
9282 }
9283
Adam Powelle14579b2009-12-16 18:39:52 -08009284 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009285 mHasPerformedLongPress = false;
9286
9287 if (mPendingCheckForLongPress == null) {
9288 mPendingCheckForLongPress = new CheckForLongPress();
9289 }
9290 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -08009291 postDelayed(mPendingCheckForLongPress,
9292 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 }
9294
Romain Guy812ccbe2010-06-01 14:07:24 -07009295 private static int[] stateSetUnion(final int[] stateSet1, final int[] stateSet2) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009296 final int stateSet1Length = stateSet1.length;
9297 final int stateSet2Length = stateSet2.length;
9298 final int[] newSet = new int[stateSet1Length + stateSet2Length];
9299 int k = 0;
9300 int i = 0;
9301 int j = 0;
9302 // This is a merge of the two input state sets and assumes that the
9303 // input sets are sorted by the order imposed by ViewDrawableStates.
9304 for (int viewState : R.styleable.ViewDrawableStates) {
9305 if (i < stateSet1Length && stateSet1[i] == viewState) {
9306 newSet[k++] = viewState;
9307 i++;
9308 } else if (j < stateSet2Length && stateSet2[j] == viewState) {
9309 newSet[k++] = viewState;
9310 j++;
9311 }
9312 if (k > 1) {
9313 assert(newSet[k - 1] > newSet[k - 2]);
9314 }
9315 }
9316 return newSet;
9317 }
9318
9319 /**
9320 * Inflate a view from an XML resource. This convenience method wraps the {@link
9321 * LayoutInflater} class, which provides a full range of options for view inflation.
9322 *
9323 * @param context The Context object for your activity or application.
9324 * @param resource The resource ID to inflate
9325 * @param root A view group that will be the parent. Used to properly inflate the
9326 * layout_* parameters.
9327 * @see LayoutInflater
9328 */
9329 public static View inflate(Context context, int resource, ViewGroup root) {
9330 LayoutInflater factory = LayoutInflater.from(context);
9331 return factory.inflate(resource, root);
9332 }
Romain Guy33e72ae2010-07-17 12:40:29 -07009333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 /**
9335 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
9336 * Each MeasureSpec represents a requirement for either the width or the height.
9337 * A MeasureSpec is comprised of a size and a mode. There are three possible
9338 * modes:
9339 * <dl>
9340 * <dt>UNSPECIFIED</dt>
9341 * <dd>
9342 * The parent has not imposed any constraint on the child. It can be whatever size
9343 * it wants.
9344 * </dd>
9345 *
9346 * <dt>EXACTLY</dt>
9347 * <dd>
9348 * The parent has determined an exact size for the child. The child is going to be
9349 * given those bounds regardless of how big it wants to be.
9350 * </dd>
9351 *
9352 * <dt>AT_MOST</dt>
9353 * <dd>
9354 * The child can be as large as it wants up to the specified size.
9355 * </dd>
9356 * </dl>
9357 *
9358 * MeasureSpecs are implemented as ints to reduce object allocation. This class
9359 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
9360 */
9361 public static class MeasureSpec {
9362 private static final int MODE_SHIFT = 30;
9363 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
9364
9365 /**
9366 * Measure specification mode: The parent has not imposed any constraint
9367 * on the child. It can be whatever size it wants.
9368 */
9369 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
9370
9371 /**
9372 * Measure specification mode: The parent has determined an exact size
9373 * for the child. The child is going to be given those bounds regardless
9374 * of how big it wants to be.
9375 */
9376 public static final int EXACTLY = 1 << MODE_SHIFT;
9377
9378 /**
9379 * Measure specification mode: The child can be as large as it wants up
9380 * to the specified size.
9381 */
9382 public static final int AT_MOST = 2 << MODE_SHIFT;
9383
9384 /**
9385 * Creates a measure specification based on the supplied size and mode.
9386 *
9387 * The mode must always be one of the following:
9388 * <ul>
9389 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
9390 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
9391 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
9392 * </ul>
9393 *
9394 * @param size the size of the measure specification
9395 * @param mode the mode of the measure specification
9396 * @return the measure specification based on size and mode
9397 */
9398 public static int makeMeasureSpec(int size, int mode) {
9399 return size + mode;
9400 }
9401
9402 /**
9403 * Extracts the mode from the supplied measure specification.
9404 *
9405 * @param measureSpec the measure specification to extract the mode from
9406 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
9407 * {@link android.view.View.MeasureSpec#AT_MOST} or
9408 * {@link android.view.View.MeasureSpec#EXACTLY}
9409 */
9410 public static int getMode(int measureSpec) {
9411 return (measureSpec & MODE_MASK);
9412 }
9413
9414 /**
9415 * Extracts the size from the supplied measure specification.
9416 *
9417 * @param measureSpec the measure specification to extract the size from
9418 * @return the size in pixels defined in the supplied measure specification
9419 */
9420 public static int getSize(int measureSpec) {
9421 return (measureSpec & ~MODE_MASK);
9422 }
9423
9424 /**
9425 * Returns a String representation of the specified measure
9426 * specification.
9427 *
9428 * @param measureSpec the measure specification to convert to a String
9429 * @return a String with the following format: "MeasureSpec: MODE SIZE"
9430 */
9431 public static String toString(int measureSpec) {
9432 int mode = getMode(measureSpec);
9433 int size = getSize(measureSpec);
9434
9435 StringBuilder sb = new StringBuilder("MeasureSpec: ");
9436
9437 if (mode == UNSPECIFIED)
9438 sb.append("UNSPECIFIED ");
9439 else if (mode == EXACTLY)
9440 sb.append("EXACTLY ");
9441 else if (mode == AT_MOST)
9442 sb.append("AT_MOST ");
9443 else
9444 sb.append(mode).append(" ");
9445
9446 sb.append(size);
9447 return sb.toString();
9448 }
9449 }
9450
9451 class CheckForLongPress implements Runnable {
9452
9453 private int mOriginalWindowAttachCount;
9454
9455 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -07009456 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009457 && mOriginalWindowAttachCount == mWindowAttachCount) {
9458 if (performLongClick()) {
9459 mHasPerformedLongPress = true;
9460 }
9461 }
9462 }
9463
9464 public void rememberWindowAttachCount() {
9465 mOriginalWindowAttachCount = mWindowAttachCount;
9466 }
9467 }
Adam Powelle14579b2009-12-16 18:39:52 -08009468
9469 private final class CheckForTap implements Runnable {
9470 public void run() {
9471 mPrivateFlags &= ~PREPRESSED;
9472 mPrivateFlags |= PRESSED;
9473 refreshDrawableState();
9474 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
9475 postCheckForLongClick(ViewConfiguration.getTapTimeout());
9476 }
9477 }
9478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009479
Adam Powella35d7682010-03-12 14:48:13 -08009480 private final class PerformClick implements Runnable {
9481 public void run() {
9482 performClick();
9483 }
9484 }
9485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009486 /**
9487 * Interface definition for a callback to be invoked when a key event is
9488 * dispatched to this view. The callback will be invoked before the key
9489 * event is given to the view.
9490 */
9491 public interface OnKeyListener {
9492 /**
9493 * Called when a key is dispatched to a view. This allows listeners to
9494 * get a chance to respond before the target view.
9495 *
9496 * @param v The view the key has been dispatched to.
9497 * @param keyCode The code for the physical key that was pressed
9498 * @param event The KeyEvent object containing full information about
9499 * the event.
9500 * @return True if the listener has consumed the event, false otherwise.
9501 */
9502 boolean onKey(View v, int keyCode, KeyEvent event);
9503 }
9504
9505 /**
9506 * Interface definition for a callback to be invoked when a touch event is
9507 * dispatched to this view. The callback will be invoked before the touch
9508 * event is given to the view.
9509 */
9510 public interface OnTouchListener {
9511 /**
9512 * Called when a touch event is dispatched to a view. This allows listeners to
9513 * get a chance to respond before the target view.
9514 *
9515 * @param v The view the touch event has been dispatched to.
9516 * @param event The MotionEvent object containing full information about
9517 * the event.
9518 * @return True if the listener has consumed the event, false otherwise.
9519 */
9520 boolean onTouch(View v, MotionEvent event);
9521 }
9522
9523 /**
9524 * Interface definition for a callback to be invoked when a view has been clicked and held.
9525 */
9526 public interface OnLongClickListener {
9527 /**
9528 * Called when a view has been clicked and held.
9529 *
9530 * @param v The view that was clicked and held.
9531 *
9532 * return True if the callback consumed the long click, false otherwise
9533 */
9534 boolean onLongClick(View v);
9535 }
9536
9537 /**
9538 * Interface definition for a callback to be invoked when the focus state of
9539 * a view changed.
9540 */
9541 public interface OnFocusChangeListener {
9542 /**
9543 * Called when the focus state of a view has changed.
9544 *
9545 * @param v The view whose state has changed.
9546 * @param hasFocus The new focus state of v.
9547 */
9548 void onFocusChange(View v, boolean hasFocus);
9549 }
9550
9551 /**
9552 * Interface definition for a callback to be invoked when a view is clicked.
9553 */
9554 public interface OnClickListener {
9555 /**
9556 * Called when a view has been clicked.
9557 *
9558 * @param v The view that was clicked.
9559 */
9560 void onClick(View v);
9561 }
9562
9563 /**
9564 * Interface definition for a callback to be invoked when the context menu
9565 * for this view is being built.
9566 */
9567 public interface OnCreateContextMenuListener {
9568 /**
9569 * Called when the context menu for this view is being built. It is not
9570 * safe to hold onto the menu after this method returns.
9571 *
9572 * @param menu The context menu that is being built
9573 * @param v The view for which the context menu is being built
9574 * @param menuInfo Extra information about the item for which the
9575 * context menu should be shown. This information will vary
9576 * depending on the class of v.
9577 */
9578 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
9579 }
9580
9581 private final class UnsetPressedState implements Runnable {
9582 public void run() {
9583 setPressed(false);
9584 }
9585 }
9586
9587 /**
9588 * Base class for derived classes that want to save and restore their own
9589 * state in {@link android.view.View#onSaveInstanceState()}.
9590 */
9591 public static class BaseSavedState extends AbsSavedState {
9592 /**
9593 * Constructor used when reading from a parcel. Reads the state of the superclass.
9594 *
9595 * @param source
9596 */
9597 public BaseSavedState(Parcel source) {
9598 super(source);
9599 }
9600
9601 /**
9602 * Constructor called by derived classes when creating their SavedState objects
9603 *
9604 * @param superState The state of the superclass of this view
9605 */
9606 public BaseSavedState(Parcelable superState) {
9607 super(superState);
9608 }
9609
9610 public static final Parcelable.Creator<BaseSavedState> CREATOR =
9611 new Parcelable.Creator<BaseSavedState>() {
9612 public BaseSavedState createFromParcel(Parcel in) {
9613 return new BaseSavedState(in);
9614 }
9615
9616 public BaseSavedState[] newArray(int size) {
9617 return new BaseSavedState[size];
9618 }
9619 };
9620 }
9621
9622 /**
9623 * A set of information given to a view when it is attached to its parent
9624 * window.
9625 */
9626 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009627 interface Callbacks {
9628 void playSoundEffect(int effectId);
9629 boolean performHapticFeedback(int effectId, boolean always);
9630 }
9631
9632 /**
9633 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
9634 * to a Handler. This class contains the target (View) to invalidate and
9635 * the coordinates of the dirty rectangle.
9636 *
9637 * For performance purposes, this class also implements a pool of up to
9638 * POOL_LIMIT objects that get reused. This reduces memory allocations
9639 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009640 */
Romain Guyd928d682009-03-31 17:52:16 -07009641 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009642 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -07009643 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
9644 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -07009645 public InvalidateInfo newInstance() {
9646 return new InvalidateInfo();
9647 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648
Romain Guyd928d682009-03-31 17:52:16 -07009649 public void onAcquired(InvalidateInfo element) {
9650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009651
Romain Guyd928d682009-03-31 17:52:16 -07009652 public void onReleased(InvalidateInfo element) {
9653 }
9654 }, POOL_LIMIT)
9655 );
9656
9657 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009658
9659 View target;
9660
9661 int left;
9662 int top;
9663 int right;
9664 int bottom;
9665
Romain Guyd928d682009-03-31 17:52:16 -07009666 public void setNextPoolable(InvalidateInfo element) {
9667 mNext = element;
9668 }
9669
9670 public InvalidateInfo getNextPoolable() {
9671 return mNext;
9672 }
9673
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009674 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -07009675 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009676 }
9677
9678 void release() {
Romain Guyd928d682009-03-31 17:52:16 -07009679 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009680 }
9681 }
9682
9683 final IWindowSession mSession;
9684
9685 final IWindow mWindow;
9686
9687 final IBinder mWindowToken;
9688
9689 final Callbacks mRootCallbacks;
9690
9691 /**
9692 * The top view of the hierarchy.
9693 */
9694 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -07009695
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 IBinder mPanelParentWindowToken;
9697 Surface mSurface;
9698
9699 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009700 * Scale factor used by the compatibility mode
9701 */
9702 float mApplicationScale;
9703
9704 /**
9705 * Indicates whether the application is in compatibility mode
9706 */
9707 boolean mScalingRequired;
9708
9709 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 * Left position of this view's window
9711 */
9712 int mWindowLeft;
9713
9714 /**
9715 * Top position of this view's window
9716 */
9717 int mWindowTop;
9718
9719 /**
Romain Guy35b38ce2009-10-07 13:38:55 -07009720 * Indicates whether the window is translucent/transparent
9721 */
9722 boolean mTranslucentWindow;
9723
9724 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725 * For windows that are full-screen but using insets to layout inside
9726 * of the screen decorations, these are the current insets for the
9727 * content of the window.
9728 */
9729 final Rect mContentInsets = new Rect();
9730
9731 /**
9732 * For windows that are full-screen but using insets to layout inside
9733 * of the screen decorations, these are the current insets for the
9734 * actual visible parts of the window.
9735 */
9736 final Rect mVisibleInsets = new Rect();
9737
9738 /**
9739 * The internal insets given by this window. This value is
9740 * supplied by the client (through
9741 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
9742 * be given to the window manager when changed to be used in laying
9743 * out windows behind it.
9744 */
9745 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
9746 = new ViewTreeObserver.InternalInsetsInfo();
9747
9748 /**
9749 * All views in the window's hierarchy that serve as scroll containers,
9750 * used to determine if the window can be resized or must be panned
9751 * to adjust for a soft input area.
9752 */
9753 final ArrayList<View> mScrollContainers = new ArrayList<View>();
9754
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07009755 final KeyEvent.DispatcherState mKeyDispatchState
9756 = new KeyEvent.DispatcherState();
9757
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009758 /**
9759 * Indicates whether the view's window currently has the focus.
9760 */
9761 boolean mHasWindowFocus;
9762
9763 /**
9764 * The current visibility of the window.
9765 */
9766 int mWindowVisibility;
9767
9768 /**
9769 * Indicates the time at which drawing started to occur.
9770 */
9771 long mDrawingTime;
9772
9773 /**
Romain Guy5bcdff42009-05-14 21:27:18 -07009774 * Indicates whether or not ignoring the DIRTY_MASK flags.
9775 */
9776 boolean mIgnoreDirtyState;
9777
9778 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009779 * Indicates whether the view's window is currently in touch mode.
9780 */
9781 boolean mInTouchMode;
9782
9783 /**
9784 * Indicates that ViewRoot should trigger a global layout change
9785 * the next time it performs a traversal
9786 */
9787 boolean mRecomputeGlobalAttributes;
9788
9789 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009790 * Set during a traveral if any views want to keep the screen on.
9791 */
9792 boolean mKeepScreenOn;
9793
9794 /**
9795 * Set if the visibility of any views has changed.
9796 */
9797 boolean mViewVisibilityChanged;
9798
9799 /**
9800 * Set to true if a view has been scrolled.
9801 */
9802 boolean mViewScrollChanged;
9803
9804 /**
9805 * Global to the view hierarchy used as a temporary for dealing with
9806 * x/y points in the transparent region computations.
9807 */
9808 final int[] mTransparentLocation = new int[2];
9809
9810 /**
9811 * Global to the view hierarchy used as a temporary for dealing with
9812 * x/y points in the ViewGroup.invalidateChild implementation.
9813 */
9814 final int[] mInvalidateChildLocation = new int[2];
9815
Chet Haasec3aa3612010-06-17 08:50:37 -07009816
9817 /**
9818 * Global to the view hierarchy used as a temporary for dealing with
9819 * x/y location when view is transformed.
9820 */
9821 final float[] mTmpTransformLocation = new float[2];
9822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009823 /**
9824 * The view tree observer used to dispatch global events like
9825 * layout, pre-draw, touch mode change, etc.
9826 */
9827 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
9828
9829 /**
9830 * A Canvas used by the view hierarchy to perform bitmap caching.
9831 */
9832 Canvas mCanvas;
9833
9834 /**
9835 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
9836 * handler can be used to pump events in the UI events queue.
9837 */
9838 final Handler mHandler;
9839
9840 /**
9841 * Identifier for messages requesting the view to be invalidated.
9842 * Such messages should be sent to {@link #mHandler}.
9843 */
9844 static final int INVALIDATE_MSG = 0x1;
9845
9846 /**
9847 * Identifier for messages requesting the view to invalidate a region.
9848 * Such messages should be sent to {@link #mHandler}.
9849 */
9850 static final int INVALIDATE_RECT_MSG = 0x2;
9851
9852 /**
9853 * Temporary for use in computing invalidate rectangles while
9854 * calling up the hierarchy.
9855 */
9856 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -07009857
9858 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07009859 * Temporary for use in computing hit areas with transformed views
9860 */
9861 final RectF mTmpTransformRect = new RectF();
9862
9863 /**
9864 * Temporary for use in computing invalidation areas with transformed views
9865 */
9866 final float[] mTmpTransformBounds = new float[8];
9867
9868 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07009869 * Temporary list for use in collecting focusable descendents of a view.
9870 */
9871 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
9872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009873 /**
9874 * Creates a new set of attachment information with the specified
9875 * events handler and thread.
9876 *
9877 * @param handler the events handler the view must use
9878 */
9879 AttachInfo(IWindowSession session, IWindow window,
9880 Handler handler, Callbacks effectPlayer) {
9881 mSession = session;
9882 mWindow = window;
9883 mWindowToken = window.asBinder();
9884 mHandler = handler;
9885 mRootCallbacks = effectPlayer;
9886 }
9887 }
9888
9889 /**
9890 * <p>ScrollabilityCache holds various fields used by a View when scrolling
9891 * is supported. This avoids keeping too many unused fields in most
9892 * instances of View.</p>
9893 */
Mike Cleronf116bf82009-09-27 19:14:12 -07009894 private static class ScrollabilityCache implements Runnable {
9895
9896 /**
9897 * Scrollbars are not visible
9898 */
9899 public static final int OFF = 0;
9900
9901 /**
9902 * Scrollbars are visible
9903 */
9904 public static final int ON = 1;
9905
9906 /**
9907 * Scrollbars are fading away
9908 */
9909 public static final int FADING = 2;
9910
9911 public boolean fadeScrollBars;
9912
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009913 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -07009914 public int scrollBarDefaultDelayBeforeFade;
9915 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916
9917 public int scrollBarSize;
9918 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -07009919 public float[] interpolatorValues;
9920 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009921
9922 public final Paint paint;
9923 public final Matrix matrix;
9924 public Shader shader;
9925
Mike Cleronf116bf82009-09-27 19:14:12 -07009926 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
9927
9928 private final float[] mOpaque = {255.0f};
9929 private final float[] mTransparent = {0.0f};
9930
9931 /**
9932 * When fading should start. This time moves into the future every time
9933 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
9934 */
9935 public long fadeStartTime;
9936
9937
9938 /**
9939 * The current state of the scrollbars: ON, OFF, or FADING
9940 */
9941 public int state = OFF;
9942
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 private int mLastColor;
9944
Mike Cleronf116bf82009-09-27 19:14:12 -07009945 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009946 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
9947 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -07009948 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
9949 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009950
9951 paint = new Paint();
9952 matrix = new Matrix();
9953 // use use a height of 1, and then wack the matrix each time we
9954 // actually use it.
9955 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -07009956
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009957 paint.setShader(shader);
9958 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -07009959 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009960 }
Romain Guy8506ab42009-06-11 17:35:47 -07009961
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 public void setFadeColor(int color) {
9963 if (color != 0 && color != mLastColor) {
9964 mLastColor = color;
9965 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -07009966
Romain Guye55e1a72009-08-27 10:42:26 -07009967 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
9968 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -07009969
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 paint.setShader(shader);
9971 // Restore the default transfer mode (src_over)
9972 paint.setXfermode(null);
9973 }
9974 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009975
9976 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -07009977 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -07009978 if (now >= fadeStartTime) {
9979
9980 // the animation fades the scrollbars out by changing
9981 // the opacity (alpha) from fully opaque to fully
9982 // transparent
9983 int nextFrame = (int) now;
9984 int framesCount = 0;
9985
9986 Interpolator interpolator = scrollBarInterpolator;
9987
9988 // Start opaque
9989 interpolator.setKeyFrame(framesCount++, nextFrame, mOpaque);
9990
9991 // End transparent
9992 nextFrame += scrollBarFadeDuration;
9993 interpolator.setKeyFrame(framesCount, nextFrame, mTransparent);
9994
9995 state = FADING;
9996
9997 // Kick off the fade animation
9998 host.invalidate();
9999 }
10000 }
10001
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 }
10003}