blob: 0df464f93dedd9085d1ca67544382381f5eb0942 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.view;
18
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080019import com.android.internal.R;
20import com.android.internal.util.Predicate;
21import com.android.internal.view.menu.MenuBuilder;
22
Christopher Tatea53146c2010-09-07 11:57:52 -070023import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080025import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.res.Resources;
27import android.content.res.TypedArray;
28import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070029import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070031import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.graphics.LinearGradient;
33import android.graphics.Matrix;
34import android.graphics.Paint;
35import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070036import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.PorterDuff;
38import android.graphics.PorterDuffXfermode;
39import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070040import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.graphics.Region;
42import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.graphics.drawable.ColorDrawable;
44import android.graphics.drawable.Drawable;
45import android.os.Handler;
46import android.os.IBinder;
47import android.os.Message;
48import android.os.Parcel;
49import android.os.Parcelable;
50import android.os.RemoteException;
51import android.os.SystemClock;
52import android.os.SystemProperties;
53import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070055import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Pools;
59import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityEventSource;
63import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Christopher Tatea0374192010-10-05 13:06:41 -070071import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import java.lang.reflect.InvocationTargetException;
73import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import java.util.ArrayList;
75import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070076import java.util.WeakHashMap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
78/**
79 * <p>
80 * This class represents the basic building block for user interface components. A View
81 * occupies a rectangular area on the screen and is responsible for drawing and
82 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070083 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
85 * are invisible containers that hold other Views (or other ViewGroups) and define
86 * their layout properties.
87 * </p>
88 *
89 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070090 * <p>For an introduction to using this class to develop your
91 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070093 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
95 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
96 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
102 * </p>
103 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * <a name="Using"></a>
106 * <h3>Using Views</h3>
107 * <p>
108 * All of the views in a window are arranged in a single tree. You can add views
109 * either from code or by specifying a tree of views in one or more XML layout
110 * files. There are many specialized subclasses of views that act as controls or
111 * are capable of displaying text, images, or other content.
112 * </p>
113 * <p>
114 * Once you have created a tree of views, there are typically a few types of
115 * common operations you may wish to perform:
116 * <ul>
117 * <li><strong>Set properties:</strong> for example setting the text of a
118 * {@link android.widget.TextView}. The available properties and the methods
119 * that set them will vary among the different subclasses of views. Note that
120 * properties that are known at build time can be set in the XML layout
121 * files.</li>
122 * <li><strong>Set focus:</strong> The framework will handled moving focus in
123 * response to user input. To force focus to a specific view, call
124 * {@link #requestFocus}.</li>
125 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
126 * that will be notified when something interesting happens to the view. For
127 * example, all views will let you set a listener to be notified when the view
128 * gains or loses focus. You can register such a listener using
129 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
130 * specialized listeners. For example, a Button exposes a listener to notify
131 * clients when the button is clicked.</li>
132 * <li><strong>Set visibility:</strong> You can hide or show views using
133 * {@link #setVisibility}.</li>
134 * </ul>
135 * </p>
136 * <p><em>
137 * Note: The Android framework is responsible for measuring, laying out and
138 * drawing views. You should not call methods that perform these actions on
139 * views yourself unless you are actually implementing a
140 * {@link android.view.ViewGroup}.
141 * </em></p>
142 *
143 * <a name="Lifecycle"></a>
144 * <h3>Implementing a Custom View</h3>
145 *
146 * <p>
147 * To implement a custom view, you will usually begin by providing overrides for
148 * some of the standard methods that the framework calls on all views. You do
149 * not need to override all of these methods. In fact, you can start by just
150 * overriding {@link #onDraw(android.graphics.Canvas)}.
151 * <table border="2" width="85%" align="center" cellpadding="5">
152 * <thead>
153 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
154 * </thead>
155 *
156 * <tbody>
157 * <tr>
158 * <td rowspan="2">Creation</td>
159 * <td>Constructors</td>
160 * <td>There is a form of the constructor that are called when the view
161 * is created from code and a form that is called when the view is
162 * inflated from a layout file. The second form should parse and apply
163 * any attributes defined in the layout file.
164 * </td>
165 * </tr>
166 * <tr>
167 * <td><code>{@link #onFinishInflate()}</code></td>
168 * <td>Called after a view and all of its children has been inflated
169 * from XML.</td>
170 * </tr>
171 *
172 * <tr>
173 * <td rowspan="3">Layout</td>
174 * <td><code>{@link #onMeasure}</code></td>
175 * <td>Called to determine the size requirements for this view and all
176 * of its children.
177 * </td>
178 * </tr>
179 * <tr>
180 * <td><code>{@link #onLayout}</code></td>
181 * <td>Called when this view should assign a size and position to all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
186 * <td><code>{@link #onSizeChanged}</code></td>
187 * <td>Called when the size of this view has changed.
188 * </td>
189 * </tr>
190 *
191 * <tr>
192 * <td>Drawing</td>
193 * <td><code>{@link #onDraw}</code></td>
194 * <td>Called when the view should render its content.
195 * </td>
196 * </tr>
197 *
198 * <tr>
199 * <td rowspan="4">Event processing</td>
200 * <td><code>{@link #onKeyDown}</code></td>
201 * <td>Called when a new key event occurs.
202 * </td>
203 * </tr>
204 * <tr>
205 * <td><code>{@link #onKeyUp}</code></td>
206 * <td>Called when a key up event occurs.
207 * </td>
208 * </tr>
209 * <tr>
210 * <td><code>{@link #onTrackballEvent}</code></td>
211 * <td>Called when a trackball motion event occurs.
212 * </td>
213 * </tr>
214 * <tr>
215 * <td><code>{@link #onTouchEvent}</code></td>
216 * <td>Called when a touch screen motion event occurs.
217 * </td>
218 * </tr>
219 *
220 * <tr>
221 * <td rowspan="2">Focus</td>
222 * <td><code>{@link #onFocusChanged}</code></td>
223 * <td>Called when the view gains or loses focus.
224 * </td>
225 * </tr>
226 *
227 * <tr>
228 * <td><code>{@link #onWindowFocusChanged}</code></td>
229 * <td>Called when the window containing the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
234 * <td rowspan="3">Attaching</td>
235 * <td><code>{@link #onAttachedToWindow()}</code></td>
236 * <td>Called when the view is attached to a window.
237 * </td>
238 * </tr>
239 *
240 * <tr>
241 * <td><code>{@link #onDetachedFromWindow}</code></td>
242 * <td>Called when the view is detached from its window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
248 * <td>Called when the visibility of the window containing the view
249 * has changed.
250 * </td>
251 * </tr>
252 * </tbody>
253 *
254 * </table>
255 * </p>
256 *
257 * <a name="IDs"></a>
258 * <h3>IDs</h3>
259 * Views may have an integer id associated with them. These ids are typically
260 * assigned in the layout XML files, and are used to find specific views within
261 * the view tree. A common pattern is to:
262 * <ul>
263 * <li>Define a Button in the layout file and assign it a unique ID.
264 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700265 * &lt;Button
266 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 * android:layout_width="wrap_content"
268 * android:layout_height="wrap_content"
269 * android:text="@string/my_button_text"/&gt;
270 * </pre></li>
271 * <li>From the onCreate method of an Activity, find the Button
272 * <pre class="prettyprint">
273 * Button myButton = (Button) findViewById(R.id.my_button);
274 * </pre></li>
275 * </ul>
276 * <p>
277 * View IDs need not be unique throughout the tree, but it is good practice to
278 * ensure that they are at least unique within the part of the tree you are
279 * searching.
280 * </p>
281 *
282 * <a name="Position"></a>
283 * <h3>Position</h3>
284 * <p>
285 * The geometry of a view is that of a rectangle. A view has a location,
286 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
287 * two dimensions, expressed as a width and a height. The unit for location
288 * and dimensions is the pixel.
289 * </p>
290 *
291 * <p>
292 * It is possible to retrieve the location of a view by invoking the methods
293 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
294 * coordinate of the rectangle representing the view. The latter returns the
295 * top, or Y, coordinate of the rectangle representing the view. These methods
296 * both return the location of the view relative to its parent. For instance,
297 * when getLeft() returns 20, that means the view is located 20 pixels to the
298 * right of the left edge of its direct parent.
299 * </p>
300 *
301 * <p>
302 * In addition, several convenience methods are offered to avoid unnecessary
303 * computations, namely {@link #getRight()} and {@link #getBottom()}.
304 * These methods return the coordinates of the right and bottom edges of the
305 * rectangle representing the view. For instance, calling {@link #getRight()}
306 * is similar to the following computation: <code>getLeft() + getWidth()</code>
307 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
308 * </p>
309 *
310 * <a name="SizePaddingMargins"></a>
311 * <h3>Size, padding and margins</h3>
312 * <p>
313 * The size of a view is expressed with a width and a height. A view actually
314 * possess two pairs of width and height values.
315 * </p>
316 *
317 * <p>
318 * The first pair is known as <em>measured width</em> and
319 * <em>measured height</em>. These dimensions define how big a view wants to be
320 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
321 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
322 * and {@link #getMeasuredHeight()}.
323 * </p>
324 *
325 * <p>
326 * The second pair is simply known as <em>width</em> and <em>height</em>, or
327 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
328 * dimensions define the actual size of the view on screen, at drawing time and
329 * after layout. These values may, but do not have to, be different from the
330 * measured width and height. The width and height can be obtained by calling
331 * {@link #getWidth()} and {@link #getHeight()}.
332 * </p>
333 *
334 * <p>
335 * To measure its dimensions, a view takes into account its padding. The padding
336 * is expressed in pixels for the left, top, right and bottom parts of the view.
337 * Padding can be used to offset the content of the view by a specific amount of
338 * pixels. For instance, a left padding of 2 will push the view's content by
339 * 2 pixels to the right of the left edge. Padding can be set using the
340 * {@link #setPadding(int, int, int, int)} method and queried by calling
341 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
342 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
343 * </p>
344 *
345 * <p>
346 * Even though a view can define a padding, it does not provide any support for
347 * margins. However, view groups provide such a support. Refer to
348 * {@link android.view.ViewGroup} and
349 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
350 * </p>
351 *
352 * <a name="Layout"></a>
353 * <h3>Layout</h3>
354 * <p>
355 * Layout is a two pass process: a measure pass and a layout pass. The measuring
356 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
357 * of the view tree. Each view pushes dimension specifications down the tree
358 * during the recursion. At the end of the measure pass, every view has stored
359 * its measurements. The second pass happens in
360 * {@link #layout(int,int,int,int)} and is also top-down. During
361 * this pass each parent is responsible for positioning all of its children
362 * using the sizes computed in the measure pass.
363 * </p>
364 *
365 * <p>
366 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
367 * {@link #getMeasuredHeight()} values must be set, along with those for all of
368 * that view's descendants. A view's measured width and measured height values
369 * must respect the constraints imposed by the view's parents. This guarantees
370 * that at the end of the measure pass, all parents accept all of their
371 * children's measurements. A parent view may call measure() more than once on
372 * its children. For example, the parent may measure each child once with
373 * unspecified dimensions to find out how big they want to be, then call
374 * measure() on them again with actual numbers if the sum of all the children's
375 * unconstrained sizes is too big or too small.
376 * </p>
377 *
378 * <p>
379 * The measure pass uses two classes to communicate dimensions. The
380 * {@link MeasureSpec} class is used by views to tell their parents how they
381 * want to be measured and positioned. The base LayoutParams class just
382 * describes how big the view wants to be for both width and height. For each
383 * dimension, it can specify one of:
384 * <ul>
385 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800386 * <li>MATCH_PARENT, which means the view wants to be as big as its parent
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800387 * (minus padding)
388 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
389 * enclose its content (plus padding).
390 * </ul>
391 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
392 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
393 * an X and Y value.
394 * </p>
395 *
396 * <p>
397 * MeasureSpecs are used to push requirements down the tree from parent to
398 * child. A MeasureSpec can be in one of three modes:
399 * <ul>
400 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
401 * of a child view. For example, a LinearLayout may call measure() on its child
402 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
403 * tall the child view wants to be given a width of 240 pixels.
404 * <li>EXACTLY: This is used by the parent to impose an exact size on the
405 * child. The child must use this size, and guarantee that all of its
406 * descendants will fit within this size.
407 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
408 * child. The child must gurantee that it and all of its descendants will fit
409 * within this size.
410 * </ul>
411 * </p>
412 *
413 * <p>
414 * To intiate a layout, call {@link #requestLayout}. This method is typically
415 * called by a view on itself when it believes that is can no longer fit within
416 * its current bounds.
417 * </p>
418 *
419 * <a name="Drawing"></a>
420 * <h3>Drawing</h3>
421 * <p>
422 * Drawing is handled by walking the tree and rendering each view that
423 * intersects the the invalid region. Because the tree is traversed in-order,
424 * this means that parents will draw before (i.e., behind) their children, with
425 * siblings drawn in the order they appear in the tree.
426 * If you set a background drawable for a View, then the View will draw it for you
427 * before calling back to its <code>onDraw()</code> method.
428 * </p>
429 *
430 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700431 * Note that the framework will not draw views that are not in the invalid region.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 * </p>
433 *
434 * <p>
435 * To force a view to draw, call {@link #invalidate()}.
436 * </p>
437 *
438 * <a name="EventHandlingThreading"></a>
439 * <h3>Event Handling and Threading</h3>
440 * <p>
441 * The basic cycle of a view is as follows:
442 * <ol>
443 * <li>An event comes in and is dispatched to the appropriate view. The view
444 * handles the event and notifies any listeners.</li>
445 * <li>If in the course of processing the event, the view's bounds may need
446 * to be changed, the view will call {@link #requestLayout()}.</li>
447 * <li>Similarly, if in the course of processing the event the view's appearance
448 * may need to be changed, the view will call {@link #invalidate()}.</li>
449 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
450 * the framework will take care of measuring, laying out, and drawing the tree
451 * as appropriate.</li>
452 * </ol>
453 * </p>
454 *
455 * <p><em>Note: The entire view tree is single threaded. You must always be on
456 * the UI thread when calling any method on any view.</em>
457 * If you are doing work on other threads and want to update the state of a view
458 * from that thread, you should use a {@link Handler}.
459 * </p>
460 *
461 * <a name="FocusHandling"></a>
462 * <h3>Focus Handling</h3>
463 * <p>
464 * The framework will handle routine focus movement in response to user input.
465 * This includes changing the focus as views are removed or hidden, or as new
466 * views become available. Views indicate their willingness to take focus
467 * through the {@link #isFocusable} method. To change whether a view can take
468 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
469 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
470 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
471 * </p>
472 * <p>
473 * Focus movement is based on an algorithm which finds the nearest neighbor in a
474 * given direction. In rare cases, the default algorithm may not match the
475 * intended behavior of the developer. In these situations, you can provide
476 * explicit overrides by using these XML attributes in the layout file:
477 * <pre>
478 * nextFocusDown
479 * nextFocusLeft
480 * nextFocusRight
481 * nextFocusUp
482 * </pre>
483 * </p>
484 *
485 *
486 * <p>
487 * To get a particular view to take focus, call {@link #requestFocus()}.
488 * </p>
489 *
490 * <a name="TouchMode"></a>
491 * <h3>Touch Mode</h3>
492 * <p>
493 * When a user is navigating a user interface via directional keys such as a D-pad, it is
494 * necessary to give focus to actionable items such as buttons so the user can see
495 * what will take input. If the device has touch capabilities, however, and the user
496 * begins interacting with the interface by touching it, it is no longer necessary to
497 * always highlight, or give focus to, a particular view. This motivates a mode
498 * for interaction named 'touch mode'.
499 * </p>
500 * <p>
501 * For a touch capable device, once the user touches the screen, the device
502 * will enter touch mode. From this point onward, only views for which
503 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
504 * Other views that are touchable, like buttons, will not take focus when touched; they will
505 * only fire the on click listeners.
506 * </p>
507 * <p>
508 * Any time a user hits a directional key, such as a D-pad direction, the view device will
509 * exit touch mode, and find a view to take focus, so that the user may resume interacting
510 * with the user interface without touching the screen again.
511 * </p>
512 * <p>
513 * The touch mode state is maintained across {@link android.app.Activity}s. Call
514 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
515 * </p>
516 *
517 * <a name="Scrolling"></a>
518 * <h3>Scrolling</h3>
519 * <p>
520 * The framework provides basic support for views that wish to internally
521 * scroll their content. This includes keeping track of the X and Y scroll
522 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800523 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700524 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 * </p>
526 *
527 * <a name="Tags"></a>
528 * <h3>Tags</h3>
529 * <p>
530 * Unlike IDs, tags are not used to identify views. Tags are essentially an
531 * extra piece of information that can be associated with a view. They are most
532 * often used as a convenience to store data related to views in the views
533 * themselves rather than by putting them in a separate structure.
534 * </p>
535 *
536 * <a name="Animation"></a>
537 * <h3>Animation</h3>
538 * <p>
539 * You can attach an {@link Animation} object to a view using
540 * {@link #setAnimation(Animation)} or
541 * {@link #startAnimation(Animation)}. The animation can alter the scale,
542 * rotation, translation and alpha of a view over time. If the animation is
543 * attached to a view that has children, the animation will affect the entire
544 * subtree rooted by that node. When an animation is started, the framework will
545 * take care of redrawing the appropriate views until the animation completes.
546 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800547 * <p>
548 * Starting with Android 3.0, the preferred way of animating views is to use the
549 * {@link android.animation} package APIs.
550 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 *
Jeff Brown85a31762010-09-01 17:01:00 -0700552 * <a name="Security"></a>
553 * <h3>Security</h3>
554 * <p>
555 * Sometimes it is essential that an application be able to verify that an action
556 * is being performed with the full knowledge and consent of the user, such as
557 * granting a permission request, making a purchase or clicking on an advertisement.
558 * Unfortunately, a malicious application could try to spoof the user into
559 * performing these actions, unaware, by concealing the intended purpose of the view.
560 * As a remedy, the framework offers a touch filtering mechanism that can be used to
561 * improve the security of views that provide access to sensitive functionality.
562 * </p><p>
563 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800564 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700565 * will discard touches that are received whenever the view's window is obscured by
566 * another visible window. As a result, the view will not receive touches whenever a
567 * toast, dialog or other window appears above the view's window.
568 * </p><p>
569 * For more fine-grained control over security, consider overriding the
570 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
571 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
572 * </p>
573 *
Romain Guy171c5922011-01-06 10:04:23 -0800574 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700575 * @attr ref android.R.styleable#View_background
576 * @attr ref android.R.styleable#View_clickable
577 * @attr ref android.R.styleable#View_contentDescription
578 * @attr ref android.R.styleable#View_drawingCacheQuality
579 * @attr ref android.R.styleable#View_duplicateParentState
580 * @attr ref android.R.styleable#View_id
581 * @attr ref android.R.styleable#View_fadingEdge
582 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700583 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800584 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700585 * @attr ref android.R.styleable#View_isScrollContainer
586 * @attr ref android.R.styleable#View_focusable
587 * @attr ref android.R.styleable#View_focusableInTouchMode
588 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
589 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800590 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_longClickable
592 * @attr ref android.R.styleable#View_minHeight
593 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 * @attr ref android.R.styleable#View_nextFocusDown
595 * @attr ref android.R.styleable#View_nextFocusLeft
596 * @attr ref android.R.styleable#View_nextFocusRight
597 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700598 * @attr ref android.R.styleable#View_onClick
599 * @attr ref android.R.styleable#View_padding
600 * @attr ref android.R.styleable#View_paddingBottom
601 * @attr ref android.R.styleable#View_paddingLeft
602 * @attr ref android.R.styleable#View_paddingRight
603 * @attr ref android.R.styleable#View_paddingTop
604 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800605 * @attr ref android.R.styleable#View_rotation
606 * @attr ref android.R.styleable#View_rotationX
607 * @attr ref android.R.styleable#View_rotationY
608 * @attr ref android.R.styleable#View_scaleX
609 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 * @attr ref android.R.styleable#View_scrollX
611 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_scrollbarSize
613 * @attr ref android.R.styleable#View_scrollbarStyle
614 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700615 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
616 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
618 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarThumbVertical
620 * @attr ref android.R.styleable#View_scrollbarTrackVertical
621 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
622 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700623 * @attr ref android.R.styleable#View_soundEffectsEnabled
624 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800625 * @attr ref android.R.styleable#View_transformPivotX
626 * @attr ref android.R.styleable#View_transformPivotY
627 * @attr ref android.R.styleable#View_translationX
628 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800630 *
631 * @see android.view.ViewGroup
632 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700633public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634 private static final boolean DBG = false;
635
636 /**
637 * The logging tag used by this class with android.util.Log.
638 */
639 protected static final String VIEW_LOG_TAG = "View";
640
641 /**
642 * Used to mark a View that has no ID.
643 */
644 public static final int NO_ID = -1;
645
646 /**
647 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
648 * calling setFlags.
649 */
650 private static final int NOT_FOCUSABLE = 0x00000000;
651
652 /**
653 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
654 * setFlags.
655 */
656 private static final int FOCUSABLE = 0x00000001;
657
658 /**
659 * Mask for use with setFlags indicating bits used for focus.
660 */
661 private static final int FOCUSABLE_MASK = 0x00000001;
662
663 /**
664 * This view will adjust its padding to fit sytem windows (e.g. status bar)
665 */
666 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
667
668 /**
669 * This view is visible. Use with {@link #setVisibility}.
670 */
671 public static final int VISIBLE = 0x00000000;
672
673 /**
674 * This view is invisible, but it still takes up space for layout purposes.
675 * Use with {@link #setVisibility}.
676 */
677 public static final int INVISIBLE = 0x00000004;
678
679 /**
680 * This view is invisible, and it doesn't take any space for layout
681 * purposes. Use with {@link #setVisibility}.
682 */
683 public static final int GONE = 0x00000008;
684
685 /**
686 * Mask for use with setFlags indicating bits used for visibility.
687 * {@hide}
688 */
689 static final int VISIBILITY_MASK = 0x0000000C;
690
691 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
692
693 /**
694 * This view is enabled. Intrepretation varies by subclass.
695 * Use with ENABLED_MASK when calling setFlags.
696 * {@hide}
697 */
698 static final int ENABLED = 0x00000000;
699
700 /**
701 * This view is disabled. Intrepretation varies by subclass.
702 * Use with ENABLED_MASK when calling setFlags.
703 * {@hide}
704 */
705 static final int DISABLED = 0x00000020;
706
707 /**
708 * Mask for use with setFlags indicating bits used for indicating whether
709 * this view is enabled
710 * {@hide}
711 */
712 static final int ENABLED_MASK = 0x00000020;
713
714 /**
715 * This view won't draw. {@link #onDraw} won't be called and further
716 * optimizations
717 * will be performed. It is okay to have this flag set and a background.
718 * Use with DRAW_MASK when calling setFlags.
719 * {@hide}
720 */
721 static final int WILL_NOT_DRAW = 0x00000080;
722
723 /**
724 * Mask for use with setFlags indicating bits used for indicating whether
725 * this view is will draw
726 * {@hide}
727 */
728 static final int DRAW_MASK = 0x00000080;
729
730 /**
731 * <p>This view doesn't show scrollbars.</p>
732 * {@hide}
733 */
734 static final int SCROLLBARS_NONE = 0x00000000;
735
736 /**
737 * <p>This view shows horizontal scrollbars.</p>
738 * {@hide}
739 */
740 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
741
742 /**
743 * <p>This view shows vertical scrollbars.</p>
744 * {@hide}
745 */
746 static final int SCROLLBARS_VERTICAL = 0x00000200;
747
748 /**
749 * <p>Mask for use with setFlags indicating bits used for indicating which
750 * scrollbars are enabled.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_MASK = 0x00000300;
754
Jeff Brown85a31762010-09-01 17:01:00 -0700755 /**
756 * Indicates that the view should filter touches when its window is obscured.
757 * Refer to the class comments for more information about this security feature.
758 * {@hide}
759 */
760 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
761
762 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763
764 /**
765 * <p>This view doesn't show fading edges.</p>
766 * {@hide}
767 */
768 static final int FADING_EDGE_NONE = 0x00000000;
769
770 /**
771 * <p>This view shows horizontal fading edges.</p>
772 * {@hide}
773 */
774 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
775
776 /**
777 * <p>This view shows vertical fading edges.</p>
778 * {@hide}
779 */
780 static final int FADING_EDGE_VERTICAL = 0x00002000;
781
782 /**
783 * <p>Mask for use with setFlags indicating bits used for indicating which
784 * fading edges are enabled.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_MASK = 0x00003000;
788
789 /**
790 * <p>Indicates this view can be clicked. When clickable, a View reacts
791 * to clicks by notifying the OnClickListener.<p>
792 * {@hide}
793 */
794 static final int CLICKABLE = 0x00004000;
795
796 /**
797 * <p>Indicates this view is caching its drawing into a bitmap.</p>
798 * {@hide}
799 */
800 static final int DRAWING_CACHE_ENABLED = 0x00008000;
801
802 /**
803 * <p>Indicates that no icicle should be saved for this view.<p>
804 * {@hide}
805 */
806 static final int SAVE_DISABLED = 0x000010000;
807
808 /**
809 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
810 * property.</p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED_MASK = 0x000010000;
814
815 /**
816 * <p>Indicates that no drawing cache should ever be created for this view.<p>
817 * {@hide}
818 */
819 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
820
821 /**
822 * <p>Indicates this view can take / keep focus when int touch mode.</p>
823 * {@hide}
824 */
825 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
826
827 /**
828 * <p>Enables low quality mode for the drawing cache.</p>
829 */
830 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
831
832 /**
833 * <p>Enables high quality mode for the drawing cache.</p>
834 */
835 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
836
837 /**
838 * <p>Enables automatic quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
841
842 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
843 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
844 };
845
846 /**
847 * <p>Mask for use with setFlags indicating bits used for the cache
848 * quality property.</p>
849 * {@hide}
850 */
851 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
852
853 /**
854 * <p>
855 * Indicates this view can be long clicked. When long clickable, a View
856 * reacts to long clicks by notifying the OnLongClickListener or showing a
857 * context menu.
858 * </p>
859 * {@hide}
860 */
861 static final int LONG_CLICKABLE = 0x00200000;
862
863 /**
864 * <p>Indicates that this view gets its drawable states from its direct parent
865 * and ignores its original internal states.</p>
866 *
867 * @hide
868 */
869 static final int DUPLICATE_PARENT_STATE = 0x00400000;
870
871 /**
872 * The scrollbar style to display the scrollbars inside the content area,
873 * without increasing the padding. The scrollbars will be overlaid with
874 * translucency on the view's content.
875 */
876 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the padded area,
880 * increasing the padding of the view. The scrollbars will not overlap the
881 * content area of the view.
882 */
883 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
884
885 /**
886 * The scrollbar style to display the scrollbars at the edge of the view,
887 * without increasing the padding. The scrollbars will be overlaid with
888 * translucency.
889 */
890 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * increasing the padding of the view. The scrollbars will only overlap the
895 * background, if any.
896 */
897 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
898
899 /**
900 * Mask to check if the scrollbar style is overlay or inset.
901 * {@hide}
902 */
903 static final int SCROLLBARS_INSET_MASK = 0x01000000;
904
905 /**
906 * Mask to check if the scrollbar style is inside or outside.
907 * {@hide}
908 */
909 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
910
911 /**
912 * Mask for scrollbar style.
913 * {@hide}
914 */
915 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
916
917 /**
918 * View flag indicating that the screen should remain on while the
919 * window containing this view is visible to the user. This effectively
920 * takes care of automatically setting the WindowManager's
921 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
922 */
923 public static final int KEEP_SCREEN_ON = 0x04000000;
924
925 /**
926 * View flag indicating whether this view should have sound effects enabled
927 * for events such as clicking and touching.
928 */
929 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
930
931 /**
932 * View flag indicating whether this view should have haptic feedback
933 * enabled for events such as long presses.
934 */
935 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
936
937 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700938 * <p>Indicates that the view hierarchy should stop saving state when
939 * it reaches this view. If state saving is initiated immediately at
940 * the view, it will be allowed.
941 * {@hide}
942 */
943 static final int PARENT_SAVE_DISABLED = 0x20000000;
944
945 /**
946 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
947 * {@hide}
948 */
949 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
950
951 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700952 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
953 * should add all focusable Views regardless if they are focusable in touch mode.
954 */
955 public static final int FOCUSABLES_ALL = 0x00000000;
956
957 /**
958 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
959 * should add only Views focusable in touch mode.
960 */
961 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
962
963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 * Use with {@link #focusSearch}. Move focus to the previous selectable
965 * item.
966 */
967 public static final int FOCUS_BACKWARD = 0x00000001;
968
969 /**
970 * Use with {@link #focusSearch}. Move focus to the next selectable
971 * item.
972 */
973 public static final int FOCUS_FORWARD = 0x00000002;
974
975 /**
976 * Use with {@link #focusSearch}. Move focus to the left.
977 */
978 public static final int FOCUS_LEFT = 0x00000011;
979
980 /**
981 * Use with {@link #focusSearch}. Move focus up.
982 */
983 public static final int FOCUS_UP = 0x00000021;
984
985 /**
986 * Use with {@link #focusSearch}. Move focus to the right.
987 */
988 public static final int FOCUS_RIGHT = 0x00000042;
989
990 /**
991 * Use with {@link #focusSearch}. Move focus down.
992 */
993 public static final int FOCUS_DOWN = 0x00000082;
994
995 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800996 * Bits of {@link #getMeasuredWidthAndState()} and
997 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
998 */
999 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1000
1001 /**
1002 * Bits of {@link #getMeasuredWidthAndState()} and
1003 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1004 */
1005 public static final int MEASURED_STATE_MASK = 0xff000000;
1006
1007 /**
1008 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1009 * for functions that combine both width and height into a single int,
1010 * such as {@link #getMeasuredState()} and the childState argument of
1011 * {@link #resolveSizeAndState(int, int, int)}.
1012 */
1013 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1014
1015 /**
1016 * Bit of {@link #getMeasuredWidthAndState()} and
1017 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1018 * is smaller that the space the view would like to have.
1019 */
1020 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1021
1022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * Base View state sets
1024 */
1025 // Singles
1026 /**
1027 * Indicates the view has no states set. States are used with
1028 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1029 * view depending on its state.
1030 *
1031 * @see android.graphics.drawable.Drawable
1032 * @see #getDrawableState()
1033 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001034 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 /**
1036 * Indicates the view is enabled. States are used with
1037 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1038 * view depending on its state.
1039 *
1040 * @see android.graphics.drawable.Drawable
1041 * @see #getDrawableState()
1042 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001043 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 /**
1045 * Indicates the view is focused. States are used with
1046 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1047 * view depending on its state.
1048 *
1049 * @see android.graphics.drawable.Drawable
1050 * @see #getDrawableState()
1051 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001052 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 /**
1054 * Indicates the view is selected. States are used with
1055 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1056 * view depending on its state.
1057 *
1058 * @see android.graphics.drawable.Drawable
1059 * @see #getDrawableState()
1060 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001061 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 /**
1063 * Indicates the view is pressed. States are used with
1064 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1065 * view depending on its state.
1066 *
1067 * @see android.graphics.drawable.Drawable
1068 * @see #getDrawableState()
1069 * @hide
1070 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001071 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 /**
1073 * Indicates the view's window has focus. States are used with
1074 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1075 * view depending on its state.
1076 *
1077 * @see android.graphics.drawable.Drawable
1078 * @see #getDrawableState()
1079 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001080 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 // Doubles
1082 /**
1083 * Indicates the view is enabled and has the focus.
1084 *
1085 * @see #ENABLED_STATE_SET
1086 * @see #FOCUSED_STATE_SET
1087 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001088 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001089 /**
1090 * Indicates the view is enabled and selected.
1091 *
1092 * @see #ENABLED_STATE_SET
1093 * @see #SELECTED_STATE_SET
1094 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001095 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 /**
1097 * Indicates the view is enabled and that its window has focus.
1098 *
1099 * @see #ENABLED_STATE_SET
1100 * @see #WINDOW_FOCUSED_STATE_SET
1101 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001102 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001103 /**
1104 * Indicates the view is focused and selected.
1105 *
1106 * @see #FOCUSED_STATE_SET
1107 * @see #SELECTED_STATE_SET
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view has the focus and that its window has the focus.
1112 *
1113 * @see #FOCUSED_STATE_SET
1114 * @see #WINDOW_FOCUSED_STATE_SET
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is selected and that its window has the focus.
1119 *
1120 * @see #SELECTED_STATE_SET
1121 * @see #WINDOW_FOCUSED_STATE_SET
1122 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001123 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001124 // Triples
1125 /**
1126 * Indicates the view is enabled, focused and selected.
1127 *
1128 * @see #ENABLED_STATE_SET
1129 * @see #FOCUSED_STATE_SET
1130 * @see #SELECTED_STATE_SET
1131 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001132 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 /**
1134 * Indicates the view is enabled, focused and its window has the focus.
1135 *
1136 * @see #ENABLED_STATE_SET
1137 * @see #FOCUSED_STATE_SET
1138 * @see #WINDOW_FOCUSED_STATE_SET
1139 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001140 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 /**
1142 * Indicates the view is enabled, selected and its window has the focus.
1143 *
1144 * @see #ENABLED_STATE_SET
1145 * @see #SELECTED_STATE_SET
1146 * @see #WINDOW_FOCUSED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view is focused, selected and its window has the focus.
1151 *
1152 * @see #FOCUSED_STATE_SET
1153 * @see #SELECTED_STATE_SET
1154 * @see #WINDOW_FOCUSED_STATE_SET
1155 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001156 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001157 /**
1158 * Indicates the view is enabled, focused, selected and its window
1159 * has the focus.
1160 *
1161 * @see #ENABLED_STATE_SET
1162 * @see #FOCUSED_STATE_SET
1163 * @see #SELECTED_STATE_SET
1164 * @see #WINDOW_FOCUSED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view is pressed and its window has the focus.
1169 *
1170 * @see #PRESSED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is pressed and selected.
1176 *
1177 * @see #PRESSED_STATE_SET
1178 * @see #SELECTED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 /**
1182 * Indicates the view is pressed, selected and its window has the focus.
1183 *
1184 * @see #PRESSED_STATE_SET
1185 * @see #SELECTED_STATE_SET
1186 * @see #WINDOW_FOCUSED_STATE_SET
1187 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001188 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 /**
1190 * Indicates the view is pressed and focused.
1191 *
1192 * @see #PRESSED_STATE_SET
1193 * @see #FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is pressed, focused and its window has the focus.
1198 *
1199 * @see #PRESSED_STATE_SET
1200 * @see #FOCUSED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is pressed, focused and selected.
1206 *
1207 * @see #PRESSED_STATE_SET
1208 * @see #SELECTED_STATE_SET
1209 * @see #FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is pressed, focused, selected and its window has the focus.
1214 *
1215 * @see #PRESSED_STATE_SET
1216 * @see #FOCUSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 * @see #WINDOW_FOCUSED_STATE_SET
1219 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001220 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 /**
1222 * Indicates the view is pressed and enabled.
1223 *
1224 * @see #PRESSED_STATE_SET
1225 * @see #ENABLED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed, enabled and its window has the focus.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #ENABLED_STATE_SET
1233 * @see #WINDOW_FOCUSED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed, enabled and selected.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #ENABLED_STATE_SET
1241 * @see #SELECTED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed, enabled, selected and its window has the
1246 * focus.
1247 *
1248 * @see #PRESSED_STATE_SET
1249 * @see #ENABLED_STATE_SET
1250 * @see #SELECTED_STATE_SET
1251 * @see #WINDOW_FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, enabled and focused.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #ENABLED_STATE_SET
1259 * @see #FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, enabled, focused and its window has the
1264 * focus.
1265 *
1266 * @see #PRESSED_STATE_SET
1267 * @see #ENABLED_STATE_SET
1268 * @see #FOCUSED_STATE_SET
1269 * @see #WINDOW_FOCUSED_STATE_SET
1270 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001271 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 /**
1273 * Indicates the view is pressed, enabled, focused and selected.
1274 *
1275 * @see #PRESSED_STATE_SET
1276 * @see #ENABLED_STATE_SET
1277 * @see #SELECTED_STATE_SET
1278 * @see #FOCUSED_STATE_SET
1279 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001280 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001281 /**
1282 * Indicates the view is pressed, enabled, focused, selected and its window
1283 * has the focus.
1284 *
1285 * @see #PRESSED_STATE_SET
1286 * @see #ENABLED_STATE_SET
1287 * @see #SELECTED_STATE_SET
1288 * @see #FOCUSED_STATE_SET
1289 * @see #WINDOW_FOCUSED_STATE_SET
1290 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001291 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292
1293 /**
1294 * The order here is very important to {@link #getDrawableState()}
1295 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001296 private static final int[][] VIEW_STATE_SETS;
1297
Romain Guyb051e892010-09-28 19:09:36 -07001298 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1299 static final int VIEW_STATE_SELECTED = 1 << 1;
1300 static final int VIEW_STATE_FOCUSED = 1 << 2;
1301 static final int VIEW_STATE_ENABLED = 1 << 3;
1302 static final int VIEW_STATE_PRESSED = 1 << 4;
1303 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001304 static final int VIEW_STATE_ACCELERATED = 1 << 6;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001305
1306 static final int[] VIEW_STATE_IDS = new int[] {
1307 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1308 R.attr.state_selected, VIEW_STATE_SELECTED,
1309 R.attr.state_focused, VIEW_STATE_FOCUSED,
1310 R.attr.state_enabled, VIEW_STATE_ENABLED,
1311 R.attr.state_pressed, VIEW_STATE_PRESSED,
1312 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001313 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001314 };
1315
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001316 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001317 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1318 throw new IllegalStateException(
1319 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1320 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001321 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001322 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001323 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001324 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001325 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001326 orderedIds[i * 2] = viewState;
1327 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 }
1329 }
1330 }
Romain Guyb051e892010-09-28 19:09:36 -07001331 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1332 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1333 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001334 int numBits = Integer.bitCount(i);
1335 int[] set = new int[numBits];
1336 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001337 for (int j = 0; j < orderedIds.length; j += 2) {
1338 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001339 set[pos++] = orderedIds[j];
1340 }
1341 }
1342 VIEW_STATE_SETS[i] = set;
1343 }
1344
1345 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1346 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1347 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1348 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1349 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1350 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1351 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1352 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1353 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1354 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1355 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1356 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1357 | VIEW_STATE_FOCUSED];
1358 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1359 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1361 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1363 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1365 | VIEW_STATE_ENABLED];
1366 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1367 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1368 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1369 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1370 | VIEW_STATE_ENABLED];
1371 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1373 | VIEW_STATE_ENABLED];
1374 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1376 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1377
1378 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1379 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1381 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1382 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1383 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1384 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1385 | VIEW_STATE_PRESSED];
1386 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1387 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1388 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1389 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1390 | VIEW_STATE_PRESSED];
1391 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1393 | VIEW_STATE_PRESSED];
1394 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1396 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1397 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1398 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1399 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1401 | VIEW_STATE_PRESSED];
1402 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1404 | VIEW_STATE_PRESSED];
1405 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1407 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1408 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1410 | VIEW_STATE_PRESSED];
1411 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1413 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1414 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1416 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1417 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1419 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1420 | VIEW_STATE_PRESSED];
1421 }
1422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001423 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001424 * Temporary Rect currently for use in setBackground(). This will probably
1425 * be extended in the future to hold our own class with more than just
1426 * a Rect. :)
1427 */
1428 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001429
1430 /**
1431 * Map used to store views' tags.
1432 */
1433 private static WeakHashMap<View, SparseArray<Object>> sTags;
1434
1435 /**
1436 * Lock used to access sTags.
1437 */
1438 private static final Object sTagsLock = new Object();
1439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 /**
1441 * The animation currently associated with this view.
1442 * @hide
1443 */
1444 protected Animation mCurrentAnimation = null;
1445
1446 /**
1447 * Width as measured during measure pass.
1448 * {@hide}
1449 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001450 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001451 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001452
1453 /**
1454 * Height as measured during measure pass.
1455 * {@hide}
1456 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001457 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001458 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459
1460 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001461 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1462 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1463 * its display list. This flag, used only when hw accelerated, allows us to clear the
1464 * flag while retaining this information until it's needed (at getDisplayList() time and
1465 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1466 *
1467 * {@hide}
1468 */
1469 boolean mRecreateDisplayList = false;
1470
1471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001472 * The view's identifier.
1473 * {@hide}
1474 *
1475 * @see #setId(int)
1476 * @see #getId()
1477 */
1478 @ViewDebug.ExportedProperty(resolveId = true)
1479 int mID = NO_ID;
1480
1481 /**
1482 * The view's tag.
1483 * {@hide}
1484 *
1485 * @see #setTag(Object)
1486 * @see #getTag()
1487 */
1488 protected Object mTag;
1489
1490 // for mPrivateFlags:
1491 /** {@hide} */
1492 static final int WANTS_FOCUS = 0x00000001;
1493 /** {@hide} */
1494 static final int FOCUSED = 0x00000002;
1495 /** {@hide} */
1496 static final int SELECTED = 0x00000004;
1497 /** {@hide} */
1498 static final int IS_ROOT_NAMESPACE = 0x00000008;
1499 /** {@hide} */
1500 static final int HAS_BOUNDS = 0x00000010;
1501 /** {@hide} */
1502 static final int DRAWN = 0x00000020;
1503 /**
1504 * When this flag is set, this view is running an animation on behalf of its
1505 * children and should therefore not cancel invalidate requests, even if they
1506 * lie outside of this view's bounds.
1507 *
1508 * {@hide}
1509 */
1510 static final int DRAW_ANIMATION = 0x00000040;
1511 /** {@hide} */
1512 static final int SKIP_DRAW = 0x00000080;
1513 /** {@hide} */
1514 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1515 /** {@hide} */
1516 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1517 /** {@hide} */
1518 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1519 /** {@hide} */
1520 static final int MEASURED_DIMENSION_SET = 0x00000800;
1521 /** {@hide} */
1522 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001523 /** {@hide} */
1524 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 private static final int PRESSED = 0x00004000;
1527
1528 /** {@hide} */
1529 static final int DRAWING_CACHE_VALID = 0x00008000;
1530 /**
1531 * Flag used to indicate that this view should be drawn once more (and only once
1532 * more) after its animation has completed.
1533 * {@hide}
1534 */
1535 static final int ANIMATION_STARTED = 0x00010000;
1536
1537 private static final int SAVE_STATE_CALLED = 0x00020000;
1538
1539 /**
1540 * Indicates that the View returned true when onSetAlpha() was called and that
1541 * the alpha must be restored.
1542 * {@hide}
1543 */
1544 static final int ALPHA_SET = 0x00040000;
1545
1546 /**
1547 * Set by {@link #setScrollContainer(boolean)}.
1548 */
1549 static final int SCROLL_CONTAINER = 0x00080000;
1550
1551 /**
1552 * Set by {@link #setScrollContainer(boolean)}.
1553 */
1554 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1555
1556 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001557 * View flag indicating whether this view was invalidated (fully or partially.)
1558 *
1559 * @hide
1560 */
1561 static final int DIRTY = 0x00200000;
1562
1563 /**
1564 * View flag indicating whether this view was invalidated by an opaque
1565 * invalidate request.
1566 *
1567 * @hide
1568 */
1569 static final int DIRTY_OPAQUE = 0x00400000;
1570
1571 /**
1572 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1573 *
1574 * @hide
1575 */
1576 static final int DIRTY_MASK = 0x00600000;
1577
1578 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001579 * Indicates whether the background is opaque.
1580 *
1581 * @hide
1582 */
1583 static final int OPAQUE_BACKGROUND = 0x00800000;
1584
1585 /**
1586 * Indicates whether the scrollbars are opaque.
1587 *
1588 * @hide
1589 */
1590 static final int OPAQUE_SCROLLBARS = 0x01000000;
1591
1592 /**
1593 * Indicates whether the view is opaque.
1594 *
1595 * @hide
1596 */
1597 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001598
Adam Powelle14579b2009-12-16 18:39:52 -08001599 /**
1600 * Indicates a prepressed state;
1601 * the short time between ACTION_DOWN and recognizing
1602 * a 'real' press. Prepressed is used to recognize quick taps
1603 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001604 *
Adam Powelle14579b2009-12-16 18:39:52 -08001605 * @hide
1606 */
1607 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001608
Adam Powellc9fbaab2010-02-16 17:16:19 -08001609 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001610 * Indicates whether the view is temporarily detached.
1611 *
1612 * @hide
1613 */
1614 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001615
Adam Powell8568c3a2010-04-19 14:26:11 -07001616 /**
1617 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001618 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001619 * @hide
1620 */
1621 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001622
1623 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001624 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1625 * for transform operations
1626 *
1627 * @hide
1628 */
Adam Powellf37df072010-09-17 16:22:49 -07001629 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001630
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001631 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001632 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001633
Chet Haasefd2b0022010-08-06 13:08:56 -07001634 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001635 * Indicates that this view was specifically invalidated, not just dirtied because some
1636 * child view was invalidated. The flag is used to determine when we need to recreate
1637 * a view's display list (as opposed to just returning a reference to its existing
1638 * display list).
1639 *
1640 * @hide
1641 */
1642 static final int INVALIDATED = 0x80000000;
1643
1644 /**
Adam Powell637d3372010-08-25 14:37:03 -07001645 * Always allow a user to over-scroll this view, provided it is a
1646 * view that can scroll.
1647 *
1648 * @see #getOverScrollMode()
1649 * @see #setOverScrollMode(int)
1650 */
1651 public static final int OVER_SCROLL_ALWAYS = 0;
1652
1653 /**
1654 * Allow a user to over-scroll this view only if the content is large
1655 * enough to meaningfully scroll, provided it is a view that can scroll.
1656 *
1657 * @see #getOverScrollMode()
1658 * @see #setOverScrollMode(int)
1659 */
1660 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1661
1662 /**
1663 * Never allow a user to over-scroll this view.
1664 *
1665 * @see #getOverScrollMode()
1666 * @see #setOverScrollMode(int)
1667 */
1668 public static final int OVER_SCROLL_NEVER = 2;
1669
1670 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001671 * View has requested the status bar to be visible (the default).
1672 *
Joe Malin32736f02011-01-19 16:14:20 -08001673 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001674 */
1675 public static final int STATUS_BAR_VISIBLE = 0;
1676
1677 /**
1678 * View has requested the status bar to be visible (the default).
1679 *
Joe Malin32736f02011-01-19 16:14:20 -08001680 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001681 */
1682 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1683
1684 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001685 * @hide
1686 *
1687 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1688 * out of the public fields to keep the undefined bits out of the developer's way.
1689 *
1690 * Flag to make the status bar not expandable. Unless you also
1691 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1692 */
1693 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1694
1695 /**
1696 * @hide
1697 *
1698 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1699 * out of the public fields to keep the undefined bits out of the developer's way.
1700 *
1701 * Flag to hide notification icons and scrolling ticker text.
1702 */
1703 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1704
1705 /**
1706 * @hide
1707 *
1708 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1709 * out of the public fields to keep the undefined bits out of the developer's way.
1710 *
1711 * Flag to disable incoming notification alerts. This will not block
1712 * icons, but it will block sound, vibrating and other visual or aural notifications.
1713 */
1714 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1715
1716 /**
1717 * @hide
1718 *
1719 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1720 * out of the public fields to keep the undefined bits out of the developer's way.
1721 *
1722 * Flag to hide only the scrolling ticker. Note that
1723 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1724 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1725 */
1726 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1727
1728 /**
1729 * @hide
1730 *
1731 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1732 * out of the public fields to keep the undefined bits out of the developer's way.
1733 *
1734 * Flag to hide the center system info area.
1735 */
1736 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1737
1738 /**
1739 * @hide
1740 *
1741 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1742 * out of the public fields to keep the undefined bits out of the developer's way.
1743 *
1744 * Flag to hide only the navigation buttons. Don't use this
1745 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001746 *
1747 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001748 */
1749 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1750
1751 /**
1752 * @hide
1753 *
1754 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1755 * out of the public fields to keep the undefined bits out of the developer's way.
1756 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001757 * Flag to hide only the back button. Don't use this
1758 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1759 */
1760 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1761
1762 /**
1763 * @hide
1764 *
1765 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1766 * out of the public fields to keep the undefined bits out of the developer's way.
1767 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001768 * Flag to hide only the clock. You might use this if your activity has
1769 * its own clock making the status bar's clock redundant.
1770 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001771 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1772
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001773
1774 /**
1775 * @hide
1776 */
1777 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
1778
1779
1780 /**
Adam Powell637d3372010-08-25 14:37:03 -07001781 * Controls the over-scroll mode for this view.
1782 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1783 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1784 * and {@link #OVER_SCROLL_NEVER}.
1785 */
1786 private int mOverScrollMode;
1787
1788 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001789 * The parent this view is attached to.
1790 * {@hide}
1791 *
1792 * @see #getParent()
1793 */
1794 protected ViewParent mParent;
1795
1796 /**
1797 * {@hide}
1798 */
1799 AttachInfo mAttachInfo;
1800
1801 /**
1802 * {@hide}
1803 */
Romain Guy809a7f62009-05-14 15:44:42 -07001804 @ViewDebug.ExportedProperty(flagMapping = {
1805 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1806 name = "FORCE_LAYOUT"),
1807 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1808 name = "LAYOUT_REQUIRED"),
1809 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001810 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001811 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1812 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1813 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1814 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1815 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 int mPrivateFlags;
1817
1818 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001819 * This view's request for the visibility of the status bar.
1820 * @hide
1821 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001822 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001823 int mSystemUiVisibility;
1824
1825 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001826 * Count of how many windows this view has been attached to.
1827 */
1828 int mWindowAttachCount;
1829
1830 /**
1831 * The layout parameters associated with this view and used by the parent
1832 * {@link android.view.ViewGroup} to determine how this view should be
1833 * laid out.
1834 * {@hide}
1835 */
1836 protected ViewGroup.LayoutParams mLayoutParams;
1837
1838 /**
1839 * The view flags hold various views states.
1840 * {@hide}
1841 */
1842 @ViewDebug.ExportedProperty
1843 int mViewFlags;
1844
1845 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001846 * The transform matrix for the View. This transform is calculated internally
1847 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1848 * is used by default. Do *not* use this variable directly; instead call
1849 * getMatrix(), which will automatically recalculate the matrix if necessary
1850 * to get the correct matrix based on the latest rotation and scale properties.
1851 */
1852 private final Matrix mMatrix = new Matrix();
1853
1854 /**
1855 * The transform matrix for the View. This transform is calculated internally
1856 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1857 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001858 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001859 * to get the correct matrix based on the latest rotation and scale properties.
1860 */
1861 private Matrix mInverseMatrix;
1862
1863 /**
1864 * An internal variable that tracks whether we need to recalculate the
1865 * transform matrix, based on whether the rotation or scaleX/Y properties
1866 * have changed since the matrix was last calculated.
1867 */
1868 private boolean mMatrixDirty = false;
1869
1870 /**
1871 * An internal variable that tracks whether we need to recalculate the
1872 * transform matrix, based on whether the rotation or scaleX/Y properties
1873 * have changed since the matrix was last calculated.
1874 */
1875 private boolean mInverseMatrixDirty = true;
1876
1877 /**
1878 * A variable that tracks whether we need to recalculate the
1879 * transform matrix, based on whether the rotation or scaleX/Y properties
1880 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001881 * is only valid after a call to updateMatrix() or to a function that
1882 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001883 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001884 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001885
1886 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001887 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1888 */
1889 private Camera mCamera = null;
1890
1891 /**
1892 * This matrix is used when computing the matrix for 3D rotations.
1893 */
1894 private Matrix matrix3D = null;
1895
1896 /**
1897 * These prev values are used to recalculate a centered pivot point when necessary. The
1898 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1899 * set), so thes values are only used then as well.
1900 */
1901 private int mPrevWidth = -1;
1902 private int mPrevHeight = -1;
1903
Joe Malin32736f02011-01-19 16:14:20 -08001904 private boolean mLastIsOpaque;
1905
Chet Haasefd2b0022010-08-06 13:08:56 -07001906 /**
1907 * Convenience value to check for float values that are close enough to zero to be considered
1908 * zero.
1909 */
Romain Guy2542d192010-08-18 11:47:12 -07001910 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001911
1912 /**
1913 * The degrees rotation around the vertical axis through the pivot point.
1914 */
1915 @ViewDebug.ExportedProperty
1916 private float mRotationY = 0f;
1917
1918 /**
1919 * The degrees rotation around the horizontal axis through the pivot point.
1920 */
1921 @ViewDebug.ExportedProperty
1922 private float mRotationX = 0f;
1923
1924 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001925 * The degrees rotation around the pivot point.
1926 */
1927 @ViewDebug.ExportedProperty
1928 private float mRotation = 0f;
1929
1930 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001931 * The amount of translation of the object away from its left property (post-layout).
1932 */
1933 @ViewDebug.ExportedProperty
1934 private float mTranslationX = 0f;
1935
1936 /**
1937 * The amount of translation of the object away from its top property (post-layout).
1938 */
1939 @ViewDebug.ExportedProperty
1940 private float mTranslationY = 0f;
1941
1942 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001943 * The amount of scale in the x direction around the pivot point. A
1944 * value of 1 means no scaling is applied.
1945 */
1946 @ViewDebug.ExportedProperty
1947 private float mScaleX = 1f;
1948
1949 /**
1950 * The amount of scale in the y direction around the pivot point. A
1951 * value of 1 means no scaling is applied.
1952 */
1953 @ViewDebug.ExportedProperty
1954 private float mScaleY = 1f;
1955
1956 /**
1957 * The amount of scale in the x direction around the pivot point. A
1958 * value of 1 means no scaling is applied.
1959 */
1960 @ViewDebug.ExportedProperty
1961 private float mPivotX = 0f;
1962
1963 /**
1964 * The amount of scale in the y direction around the pivot point. A
1965 * value of 1 means no scaling is applied.
1966 */
1967 @ViewDebug.ExportedProperty
1968 private float mPivotY = 0f;
1969
1970 /**
1971 * The opacity of the View. This is a value from 0 to 1, where 0 means
1972 * completely transparent and 1 means completely opaque.
1973 */
1974 @ViewDebug.ExportedProperty
1975 private float mAlpha = 1f;
1976
1977 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 * The distance in pixels from the left edge of this view's parent
1979 * to the left edge of this view.
1980 * {@hide}
1981 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001982 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 protected int mLeft;
1984 /**
1985 * The distance in pixels from the left edge of this view's parent
1986 * to the right edge of this view.
1987 * {@hide}
1988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 protected int mRight;
1991 /**
1992 * The distance in pixels from the top edge of this view's parent
1993 * to the top edge of this view.
1994 * {@hide}
1995 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001996 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 protected int mTop;
1998 /**
1999 * The distance in pixels from the top edge of this view's parent
2000 * to the bottom edge of this view.
2001 * {@hide}
2002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002003 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 protected int mBottom;
2005
2006 /**
2007 * The offset, in pixels, by which the content of this view is scrolled
2008 * horizontally.
2009 * {@hide}
2010 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002011 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 protected int mScrollX;
2013 /**
2014 * The offset, in pixels, by which the content of this view is scrolled
2015 * vertically.
2016 * {@hide}
2017 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002018 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 protected int mScrollY;
2020
2021 /**
2022 * The left padding in pixels, that is the distance in pixels between the
2023 * left edge of this view and the left edge of its content.
2024 * {@hide}
2025 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002026 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 protected int mPaddingLeft;
2028 /**
2029 * The right padding in pixels, that is the distance in pixels between the
2030 * right edge of this view and the right edge of its content.
2031 * {@hide}
2032 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002033 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002034 protected int mPaddingRight;
2035 /**
2036 * The top padding in pixels, that is the distance in pixels between the
2037 * top edge of this view and the top edge of its content.
2038 * {@hide}
2039 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002040 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 protected int mPaddingTop;
2042 /**
2043 * The bottom padding in pixels, that is the distance in pixels between the
2044 * bottom edge of this view and the bottom edge of its content.
2045 * {@hide}
2046 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002047 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 protected int mPaddingBottom;
2049
2050 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002051 * Briefly describes the view and is primarily used for accessibility support.
2052 */
2053 private CharSequence mContentDescription;
2054
2055 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056 * Cache the paddingRight set by the user to append to the scrollbar's size.
2057 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002058 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002059 int mUserPaddingRight;
2060
2061 /**
2062 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2063 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002064 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002065 int mUserPaddingBottom;
2066
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002067 /**
Adam Powell20232d02010-12-08 21:08:53 -08002068 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2069 */
2070 @ViewDebug.ExportedProperty(category = "padding")
2071 int mUserPaddingLeft;
2072
2073 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002074 * @hide
2075 */
2076 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2077 /**
2078 * @hide
2079 */
2080 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002081
2082 private Resources mResources = null;
2083
2084 private Drawable mBGDrawable;
2085
2086 private int mBackgroundResource;
2087 private boolean mBackgroundSizeChanged;
2088
2089 /**
2090 * Listener used to dispatch focus change events.
2091 * This field should be made private, so it is hidden from the SDK.
2092 * {@hide}
2093 */
2094 protected OnFocusChangeListener mOnFocusChangeListener;
2095
2096 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002097 * Listeners for layout change events.
2098 */
2099 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2100
2101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002102 * Listener used to dispatch click events.
2103 * This field should be made private, so it is hidden from the SDK.
2104 * {@hide}
2105 */
2106 protected OnClickListener mOnClickListener;
2107
2108 /**
2109 * Listener used to dispatch long click events.
2110 * This field should be made private, so it is hidden from the SDK.
2111 * {@hide}
2112 */
2113 protected OnLongClickListener mOnLongClickListener;
2114
2115 /**
2116 * Listener used to build the context menu.
2117 * This field should be made private, so it is hidden from the SDK.
2118 * {@hide}
2119 */
2120 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2121
2122 private OnKeyListener mOnKeyListener;
2123
2124 private OnTouchListener mOnTouchListener;
2125
Chris Tate32affef2010-10-18 15:29:21 -07002126 private OnDragListener mOnDragListener;
2127
Joe Onorato664644d2011-01-23 17:53:23 -08002128 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 /**
2131 * The application environment this view lives in.
2132 * This field should be made private, so it is hidden from the SDK.
2133 * {@hide}
2134 */
2135 protected Context mContext;
2136
2137 private ScrollabilityCache mScrollCache;
2138
2139 private int[] mDrawableState = null;
2140
Romain Guy0211a0a2011-02-14 16:34:59 -08002141 /**
2142 * Set to true when drawing cache is enabled and cannot be created.
2143 *
2144 * @hide
2145 */
2146 public boolean mCachingFailed;
2147
Romain Guy02890fd2010-08-06 17:58:44 -07002148 private Bitmap mDrawingCache;
2149 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002150 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002151 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152
2153 /**
2154 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2155 * the user may specify which view to go to next.
2156 */
2157 private int mNextFocusLeftId = View.NO_ID;
2158
2159 /**
2160 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2161 * the user may specify which view to go to next.
2162 */
2163 private int mNextFocusRightId = View.NO_ID;
2164
2165 /**
2166 * When this view has focus and the next focus is {@link #FOCUS_UP},
2167 * the user may specify which view to go to next.
2168 */
2169 private int mNextFocusUpId = View.NO_ID;
2170
2171 /**
2172 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2173 * the user may specify which view to go to next.
2174 */
2175 private int mNextFocusDownId = View.NO_ID;
2176
Jeff Brown4e6319b2010-12-13 10:36:51 -08002177 /**
2178 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2179 * the user may specify which view to go to next.
2180 */
2181 int mNextFocusForwardId = View.NO_ID;
2182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002184 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002185 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 private UnsetPressedState mUnsetPressedState;
2188
2189 /**
2190 * Whether the long press's action has been invoked. The tap's action is invoked on the
2191 * up event while a long press is invoked as soon as the long press duration is reached, so
2192 * a long press could be performed before the tap is checked, in which case the tap's action
2193 * should not be invoked.
2194 */
2195 private boolean mHasPerformedLongPress;
2196
2197 /**
2198 * The minimum height of the view. We'll try our best to have the height
2199 * of this view to at least this amount.
2200 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002201 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 private int mMinHeight;
2203
2204 /**
2205 * The minimum width of the view. We'll try our best to have the width
2206 * of this view to at least this amount.
2207 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002208 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002209 private int mMinWidth;
2210
2211 /**
2212 * The delegate to handle touch events that are physically in this view
2213 * but should be handled by another view.
2214 */
2215 private TouchDelegate mTouchDelegate = null;
2216
2217 /**
2218 * Solid color to use as a background when creating the drawing cache. Enables
2219 * the cache to use 16 bit bitmaps instead of 32 bit.
2220 */
2221 private int mDrawingCacheBackgroundColor = 0;
2222
2223 /**
2224 * Special tree observer used when mAttachInfo is null.
2225 */
2226 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002227
Adam Powelle14579b2009-12-16 18:39:52 -08002228 /**
2229 * Cache the touch slop from the context that created the view.
2230 */
2231 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002233 /**
Christopher Tatea53146c2010-09-07 11:57:52 -07002234 * Cache drag/drop state
2235 *
2236 */
2237 boolean mCanAcceptDrop;
Christopher Tatea53146c2010-09-07 11:57:52 -07002238
2239 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002240 * Flag indicating that a drag can cross window boundaries. When
2241 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2242 * with this flag set, all visible applications will be able to participate
2243 * in the drag operation and receive the dragged content.
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002244 */
2245 public static final int DRAG_FLAG_GLOBAL = 1;
2246
2247 /**
Adam Powell20232d02010-12-08 21:08:53 -08002248 * Position of the vertical scroll bar.
2249 */
2250 private int mVerticalScrollbarPosition;
2251
2252 /**
2253 * Position the scroll bar at the default position as determined by the system.
2254 */
2255 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2256
2257 /**
2258 * Position the scroll bar along the left edge.
2259 */
2260 public static final int SCROLLBAR_POSITION_LEFT = 1;
2261
2262 /**
2263 * Position the scroll bar along the right edge.
2264 */
2265 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2266
2267 /**
Romain Guy171c5922011-01-06 10:04:23 -08002268 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002269 *
2270 * @see #getLayerType()
2271 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002272 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002273 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002274 */
2275 public static final int LAYER_TYPE_NONE = 0;
2276
2277 /**
2278 * <p>Indicates that the view has a software layer. A software layer is backed
2279 * by a bitmap and causes the view to be rendered using Android's software
2280 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002281 *
Romain Guy171c5922011-01-06 10:04:23 -08002282 * <p>Software layers have various usages:</p>
2283 * <p>When the application is not using hardware acceleration, a software layer
2284 * is useful to apply a specific color filter and/or blending mode and/or
2285 * translucency to a view and all its children.</p>
2286 * <p>When the application is using hardware acceleration, a software layer
2287 * is useful to render drawing primitives not supported by the hardware
2288 * accelerated pipeline. It can also be used to cache a complex view tree
2289 * into a texture and reduce the complexity of drawing operations. For instance,
2290 * when animating a complex view tree with a translation, a software layer can
2291 * be used to render the view tree only once.</p>
2292 * <p>Software layers should be avoided when the affected view tree updates
2293 * often. Every update will require to re-render the software layer, which can
2294 * potentially be slow (particularly when hardware acceleration is turned on
2295 * since the layer will have to be uploaded into a hardware texture after every
2296 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002297 *
2298 * @see #getLayerType()
2299 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002300 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002301 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002302 */
2303 public static final int LAYER_TYPE_SOFTWARE = 1;
2304
2305 /**
2306 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2307 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2308 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2309 * rendering pipeline, but only if hardware acceleration is turned on for the
2310 * view hierarchy. When hardware acceleration is turned off, hardware layers
2311 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002312 *
Romain Guy171c5922011-01-06 10:04:23 -08002313 * <p>A hardware layer is useful to apply a specific color filter and/or
2314 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002315 * <p>A hardware layer can be used to cache a complex view tree into a
2316 * texture and reduce the complexity of drawing operations. For instance,
2317 * when animating a complex view tree with a translation, a hardware layer can
2318 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002319 * <p>A hardware layer can also be used to increase the rendering quality when
2320 * rotation transformations are applied on a view. It can also be used to
2321 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002322 *
2323 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002324 * @see #setLayerType(int, android.graphics.Paint)
2325 * @see #LAYER_TYPE_NONE
2326 * @see #LAYER_TYPE_SOFTWARE
2327 */
2328 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002329
Romain Guy3aaff3a2011-01-12 14:18:47 -08002330 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2331 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2332 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2333 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2334 })
Romain Guy171c5922011-01-06 10:04:23 -08002335 int mLayerType = LAYER_TYPE_NONE;
2336 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002337 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002338
2339 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002340 * Simple constructor to use when creating a view from code.
2341 *
2342 * @param context The Context the view is running in, through which it can
2343 * access the current theme, resources, etc.
2344 */
2345 public View(Context context) {
2346 mContext = context;
2347 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002348 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002349 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002350 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002351 }
2352
2353 /**
2354 * Constructor that is called when inflating a view from XML. This is called
2355 * when a view is being constructed from an XML file, supplying attributes
2356 * that were specified in the XML file. This version uses a default style of
2357 * 0, so the only attribute values applied are those in the Context's Theme
2358 * and the given AttributeSet.
2359 *
2360 * <p>
2361 * The method onFinishInflate() will be called after all children have been
2362 * added.
2363 *
2364 * @param context The Context the view is running in, through which it can
2365 * access the current theme, resources, etc.
2366 * @param attrs The attributes of the XML tag that is inflating the view.
2367 * @see #View(Context, AttributeSet, int)
2368 */
2369 public View(Context context, AttributeSet attrs) {
2370 this(context, attrs, 0);
2371 }
2372
2373 /**
2374 * Perform inflation from XML and apply a class-specific base style. This
2375 * constructor of View allows subclasses to use their own base style when
2376 * they are inflating. For example, a Button class's constructor would call
2377 * this version of the super class constructor and supply
2378 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2379 * the theme's button style to modify all of the base view attributes (in
2380 * particular its background) as well as the Button class's attributes.
2381 *
2382 * @param context The Context the view is running in, through which it can
2383 * access the current theme, resources, etc.
2384 * @param attrs The attributes of the XML tag that is inflating the view.
2385 * @param defStyle The default style to apply to this view. If 0, no style
2386 * will be applied (beyond what is included in the theme). This may
2387 * either be an attribute resource, whose value will be retrieved
2388 * from the current theme, or an explicit style resource.
2389 * @see #View(Context, AttributeSet)
2390 */
2391 public View(Context context, AttributeSet attrs, int defStyle) {
2392 this(context);
2393
2394 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2395 defStyle, 0);
2396
2397 Drawable background = null;
2398
2399 int leftPadding = -1;
2400 int topPadding = -1;
2401 int rightPadding = -1;
2402 int bottomPadding = -1;
2403
2404 int padding = -1;
2405
2406 int viewFlagValues = 0;
2407 int viewFlagMasks = 0;
2408
2409 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002410
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002411 int x = 0;
2412 int y = 0;
2413
Chet Haase73066682010-11-29 15:55:32 -08002414 float tx = 0;
2415 float ty = 0;
2416 float rotation = 0;
2417 float rotationX = 0;
2418 float rotationY = 0;
2419 float sx = 1f;
2420 float sy = 1f;
2421 boolean transformSet = false;
2422
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002423 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2424
Adam Powell637d3372010-08-25 14:37:03 -07002425 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002426 final int N = a.getIndexCount();
2427 for (int i = 0; i < N; i++) {
2428 int attr = a.getIndex(i);
2429 switch (attr) {
2430 case com.android.internal.R.styleable.View_background:
2431 background = a.getDrawable(attr);
2432 break;
2433 case com.android.internal.R.styleable.View_padding:
2434 padding = a.getDimensionPixelSize(attr, -1);
2435 break;
2436 case com.android.internal.R.styleable.View_paddingLeft:
2437 leftPadding = a.getDimensionPixelSize(attr, -1);
2438 break;
2439 case com.android.internal.R.styleable.View_paddingTop:
2440 topPadding = a.getDimensionPixelSize(attr, -1);
2441 break;
2442 case com.android.internal.R.styleable.View_paddingRight:
2443 rightPadding = a.getDimensionPixelSize(attr, -1);
2444 break;
2445 case com.android.internal.R.styleable.View_paddingBottom:
2446 bottomPadding = a.getDimensionPixelSize(attr, -1);
2447 break;
2448 case com.android.internal.R.styleable.View_scrollX:
2449 x = a.getDimensionPixelOffset(attr, 0);
2450 break;
2451 case com.android.internal.R.styleable.View_scrollY:
2452 y = a.getDimensionPixelOffset(attr, 0);
2453 break;
Chet Haase73066682010-11-29 15:55:32 -08002454 case com.android.internal.R.styleable.View_alpha:
2455 setAlpha(a.getFloat(attr, 1f));
2456 break;
2457 case com.android.internal.R.styleable.View_transformPivotX:
2458 setPivotX(a.getDimensionPixelOffset(attr, 0));
2459 break;
2460 case com.android.internal.R.styleable.View_transformPivotY:
2461 setPivotY(a.getDimensionPixelOffset(attr, 0));
2462 break;
2463 case com.android.internal.R.styleable.View_translationX:
2464 tx = a.getDimensionPixelOffset(attr, 0);
2465 transformSet = true;
2466 break;
2467 case com.android.internal.R.styleable.View_translationY:
2468 ty = a.getDimensionPixelOffset(attr, 0);
2469 transformSet = true;
2470 break;
2471 case com.android.internal.R.styleable.View_rotation:
2472 rotation = a.getFloat(attr, 0);
2473 transformSet = true;
2474 break;
2475 case com.android.internal.R.styleable.View_rotationX:
2476 rotationX = a.getFloat(attr, 0);
2477 transformSet = true;
2478 break;
2479 case com.android.internal.R.styleable.View_rotationY:
2480 rotationY = a.getFloat(attr, 0);
2481 transformSet = true;
2482 break;
2483 case com.android.internal.R.styleable.View_scaleX:
2484 sx = a.getFloat(attr, 1f);
2485 transformSet = true;
2486 break;
2487 case com.android.internal.R.styleable.View_scaleY:
2488 sy = a.getFloat(attr, 1f);
2489 transformSet = true;
2490 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002491 case com.android.internal.R.styleable.View_id:
2492 mID = a.getResourceId(attr, NO_ID);
2493 break;
2494 case com.android.internal.R.styleable.View_tag:
2495 mTag = a.getText(attr);
2496 break;
2497 case com.android.internal.R.styleable.View_fitsSystemWindows:
2498 if (a.getBoolean(attr, false)) {
2499 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2500 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2501 }
2502 break;
2503 case com.android.internal.R.styleable.View_focusable:
2504 if (a.getBoolean(attr, false)) {
2505 viewFlagValues |= FOCUSABLE;
2506 viewFlagMasks |= FOCUSABLE_MASK;
2507 }
2508 break;
2509 case com.android.internal.R.styleable.View_focusableInTouchMode:
2510 if (a.getBoolean(attr, false)) {
2511 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2512 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2513 }
2514 break;
2515 case com.android.internal.R.styleable.View_clickable:
2516 if (a.getBoolean(attr, false)) {
2517 viewFlagValues |= CLICKABLE;
2518 viewFlagMasks |= CLICKABLE;
2519 }
2520 break;
2521 case com.android.internal.R.styleable.View_longClickable:
2522 if (a.getBoolean(attr, false)) {
2523 viewFlagValues |= LONG_CLICKABLE;
2524 viewFlagMasks |= LONG_CLICKABLE;
2525 }
2526 break;
2527 case com.android.internal.R.styleable.View_saveEnabled:
2528 if (!a.getBoolean(attr, true)) {
2529 viewFlagValues |= SAVE_DISABLED;
2530 viewFlagMasks |= SAVE_DISABLED_MASK;
2531 }
2532 break;
2533 case com.android.internal.R.styleable.View_duplicateParentState:
2534 if (a.getBoolean(attr, false)) {
2535 viewFlagValues |= DUPLICATE_PARENT_STATE;
2536 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2537 }
2538 break;
2539 case com.android.internal.R.styleable.View_visibility:
2540 final int visibility = a.getInt(attr, 0);
2541 if (visibility != 0) {
2542 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2543 viewFlagMasks |= VISIBILITY_MASK;
2544 }
2545 break;
2546 case com.android.internal.R.styleable.View_drawingCacheQuality:
2547 final int cacheQuality = a.getInt(attr, 0);
2548 if (cacheQuality != 0) {
2549 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2550 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2551 }
2552 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002553 case com.android.internal.R.styleable.View_contentDescription:
2554 mContentDescription = a.getString(attr);
2555 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002556 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2557 if (!a.getBoolean(attr, true)) {
2558 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2559 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2560 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002561 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002562 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2563 if (!a.getBoolean(attr, true)) {
2564 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2565 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2566 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002567 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002568 case R.styleable.View_scrollbars:
2569 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2570 if (scrollbars != SCROLLBARS_NONE) {
2571 viewFlagValues |= scrollbars;
2572 viewFlagMasks |= SCROLLBARS_MASK;
2573 initializeScrollbars(a);
2574 }
2575 break;
2576 case R.styleable.View_fadingEdge:
2577 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2578 if (fadingEdge != FADING_EDGE_NONE) {
2579 viewFlagValues |= fadingEdge;
2580 viewFlagMasks |= FADING_EDGE_MASK;
2581 initializeFadingEdge(a);
2582 }
2583 break;
2584 case R.styleable.View_scrollbarStyle:
2585 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2586 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2587 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2588 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2589 }
2590 break;
2591 case R.styleable.View_isScrollContainer:
2592 setScrollContainer = true;
2593 if (a.getBoolean(attr, false)) {
2594 setScrollContainer(true);
2595 }
2596 break;
2597 case com.android.internal.R.styleable.View_keepScreenOn:
2598 if (a.getBoolean(attr, false)) {
2599 viewFlagValues |= KEEP_SCREEN_ON;
2600 viewFlagMasks |= KEEP_SCREEN_ON;
2601 }
2602 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002603 case R.styleable.View_filterTouchesWhenObscured:
2604 if (a.getBoolean(attr, false)) {
2605 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2606 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2607 }
2608 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002609 case R.styleable.View_nextFocusLeft:
2610 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2611 break;
2612 case R.styleable.View_nextFocusRight:
2613 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2614 break;
2615 case R.styleable.View_nextFocusUp:
2616 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2617 break;
2618 case R.styleable.View_nextFocusDown:
2619 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2620 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002621 case R.styleable.View_nextFocusForward:
2622 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2623 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002624 case R.styleable.View_minWidth:
2625 mMinWidth = a.getDimensionPixelSize(attr, 0);
2626 break;
2627 case R.styleable.View_minHeight:
2628 mMinHeight = a.getDimensionPixelSize(attr, 0);
2629 break;
Romain Guy9a817362009-05-01 10:57:14 -07002630 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002631 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002632 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002633 + "be used within a restricted context");
2634 }
2635
Romain Guy9a817362009-05-01 10:57:14 -07002636 final String handlerName = a.getString(attr);
2637 if (handlerName != null) {
2638 setOnClickListener(new OnClickListener() {
2639 private Method mHandler;
2640
2641 public void onClick(View v) {
2642 if (mHandler == null) {
2643 try {
2644 mHandler = getContext().getClass().getMethod(handlerName,
2645 View.class);
2646 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002647 int id = getId();
2648 String idText = id == NO_ID ? "" : " with id '"
2649 + getContext().getResources().getResourceEntryName(
2650 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002651 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002652 handlerName + "(View) in the activity "
2653 + getContext().getClass() + " for onClick handler"
2654 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002655 }
2656 }
2657
2658 try {
2659 mHandler.invoke(getContext(), View.this);
2660 } catch (IllegalAccessException e) {
2661 throw new IllegalStateException("Could not execute non "
2662 + "public method of the activity", e);
2663 } catch (InvocationTargetException e) {
2664 throw new IllegalStateException("Could not execute "
2665 + "method of the activity", e);
2666 }
2667 }
2668 });
2669 }
2670 break;
Adam Powell637d3372010-08-25 14:37:03 -07002671 case R.styleable.View_overScrollMode:
2672 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2673 break;
Adam Powell20232d02010-12-08 21:08:53 -08002674 case R.styleable.View_verticalScrollbarPosition:
2675 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2676 break;
Romain Guy171c5922011-01-06 10:04:23 -08002677 case R.styleable.View_layerType:
2678 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2679 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002680 }
2681 }
2682
Adam Powell637d3372010-08-25 14:37:03 -07002683 setOverScrollMode(overScrollMode);
2684
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002685 if (background != null) {
2686 setBackgroundDrawable(background);
2687 }
2688
2689 if (padding >= 0) {
2690 leftPadding = padding;
2691 topPadding = padding;
2692 rightPadding = padding;
2693 bottomPadding = padding;
2694 }
2695
2696 // If the user specified the padding (either with android:padding or
2697 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2698 // use the default padding or the padding from the background drawable
2699 // (stored at this point in mPadding*)
2700 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2701 topPadding >= 0 ? topPadding : mPaddingTop,
2702 rightPadding >= 0 ? rightPadding : mPaddingRight,
2703 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2704
2705 if (viewFlagMasks != 0) {
2706 setFlags(viewFlagValues, viewFlagMasks);
2707 }
2708
2709 // Needs to be called after mViewFlags is set
2710 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2711 recomputePadding();
2712 }
2713
2714 if (x != 0 || y != 0) {
2715 scrollTo(x, y);
2716 }
2717
Chet Haase73066682010-11-29 15:55:32 -08002718 if (transformSet) {
2719 setTranslationX(tx);
2720 setTranslationY(ty);
2721 setRotation(rotation);
2722 setRotationX(rotationX);
2723 setRotationY(rotationY);
2724 setScaleX(sx);
2725 setScaleY(sy);
2726 }
2727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002728 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2729 setScrollContainer(true);
2730 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002731
2732 computeOpaqueFlags();
2733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002734 a.recycle();
2735 }
2736
2737 /**
2738 * Non-public constructor for use in testing
2739 */
2740 View() {
2741 }
2742
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002743 /**
2744 * <p>
2745 * Initializes the fading edges from a given set of styled attributes. This
2746 * method should be called by subclasses that need fading edges and when an
2747 * instance of these subclasses is created programmatically rather than
2748 * being inflated from XML. This method is automatically called when the XML
2749 * is inflated.
2750 * </p>
2751 *
2752 * @param a the styled attributes set to initialize the fading edges from
2753 */
2754 protected void initializeFadingEdge(TypedArray a) {
2755 initScrollCache();
2756
2757 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2758 R.styleable.View_fadingEdgeLength,
2759 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2760 }
2761
2762 /**
2763 * Returns the size of the vertical faded edges used to indicate that more
2764 * content in this view is visible.
2765 *
2766 * @return The size in pixels of the vertical faded edge or 0 if vertical
2767 * faded edges are not enabled for this view.
2768 * @attr ref android.R.styleable#View_fadingEdgeLength
2769 */
2770 public int getVerticalFadingEdgeLength() {
2771 if (isVerticalFadingEdgeEnabled()) {
2772 ScrollabilityCache cache = mScrollCache;
2773 if (cache != null) {
2774 return cache.fadingEdgeLength;
2775 }
2776 }
2777 return 0;
2778 }
2779
2780 /**
2781 * Set the size of the faded edge used to indicate that more content in this
2782 * view is available. Will not change whether the fading edge is enabled; use
2783 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2784 * to enable the fading edge for the vertical or horizontal fading edges.
2785 *
2786 * @param length The size in pixels of the faded edge used to indicate that more
2787 * content in this view is visible.
2788 */
2789 public void setFadingEdgeLength(int length) {
2790 initScrollCache();
2791 mScrollCache.fadingEdgeLength = length;
2792 }
2793
2794 /**
2795 * Returns the size of the horizontal faded edges used to indicate that more
2796 * content in this view is visible.
2797 *
2798 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2799 * faded edges are not enabled for this view.
2800 * @attr ref android.R.styleable#View_fadingEdgeLength
2801 */
2802 public int getHorizontalFadingEdgeLength() {
2803 if (isHorizontalFadingEdgeEnabled()) {
2804 ScrollabilityCache cache = mScrollCache;
2805 if (cache != null) {
2806 return cache.fadingEdgeLength;
2807 }
2808 }
2809 return 0;
2810 }
2811
2812 /**
2813 * Returns the width of the vertical scrollbar.
2814 *
2815 * @return The width in pixels of the vertical scrollbar or 0 if there
2816 * is no vertical scrollbar.
2817 */
2818 public int getVerticalScrollbarWidth() {
2819 ScrollabilityCache cache = mScrollCache;
2820 if (cache != null) {
2821 ScrollBarDrawable scrollBar = cache.scrollBar;
2822 if (scrollBar != null) {
2823 int size = scrollBar.getSize(true);
2824 if (size <= 0) {
2825 size = cache.scrollBarSize;
2826 }
2827 return size;
2828 }
2829 return 0;
2830 }
2831 return 0;
2832 }
2833
2834 /**
2835 * Returns the height of the horizontal scrollbar.
2836 *
2837 * @return The height in pixels of the horizontal scrollbar or 0 if
2838 * there is no horizontal scrollbar.
2839 */
2840 protected int getHorizontalScrollbarHeight() {
2841 ScrollabilityCache cache = mScrollCache;
2842 if (cache != null) {
2843 ScrollBarDrawable scrollBar = cache.scrollBar;
2844 if (scrollBar != null) {
2845 int size = scrollBar.getSize(false);
2846 if (size <= 0) {
2847 size = cache.scrollBarSize;
2848 }
2849 return size;
2850 }
2851 return 0;
2852 }
2853 return 0;
2854 }
2855
2856 /**
2857 * <p>
2858 * Initializes the scrollbars from a given set of styled attributes. This
2859 * method should be called by subclasses that need scrollbars and when an
2860 * instance of these subclasses is created programmatically rather than
2861 * being inflated from XML. This method is automatically called when the XML
2862 * is inflated.
2863 * </p>
2864 *
2865 * @param a the styled attributes set to initialize the scrollbars from
2866 */
2867 protected void initializeScrollbars(TypedArray a) {
2868 initScrollCache();
2869
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002870 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002871
Mike Cleronf116bf82009-09-27 19:14:12 -07002872 if (scrollabilityCache.scrollBar == null) {
2873 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2874 }
Joe Malin32736f02011-01-19 16:14:20 -08002875
Romain Guy8bda2482010-03-02 11:42:11 -08002876 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002877
Mike Cleronf116bf82009-09-27 19:14:12 -07002878 if (!fadeScrollbars) {
2879 scrollabilityCache.state = ScrollabilityCache.ON;
2880 }
2881 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002882
2883
Mike Cleronf116bf82009-09-27 19:14:12 -07002884 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2885 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2886 .getScrollBarFadeDuration());
2887 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2888 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2889 ViewConfiguration.getScrollDefaultDelay());
2890
Joe Malin32736f02011-01-19 16:14:20 -08002891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002892 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2893 com.android.internal.R.styleable.View_scrollbarSize,
2894 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2895
2896 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2897 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2898
2899 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2900 if (thumb != null) {
2901 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2902 }
2903
2904 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2905 false);
2906 if (alwaysDraw) {
2907 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2908 }
2909
2910 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2911 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2912
2913 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2914 if (thumb != null) {
2915 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2916 }
2917
2918 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2919 false);
2920 if (alwaysDraw) {
2921 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2922 }
2923
2924 // Re-apply user/background padding so that scrollbar(s) get added
2925 recomputePadding();
2926 }
2927
2928 /**
2929 * <p>
2930 * Initalizes the scrollability cache if necessary.
2931 * </p>
2932 */
2933 private void initScrollCache() {
2934 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002935 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002936 }
2937 }
2938
2939 /**
Adam Powell20232d02010-12-08 21:08:53 -08002940 * Set the position of the vertical scroll bar. Should be one of
2941 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2942 * {@link #SCROLLBAR_POSITION_RIGHT}.
2943 *
2944 * @param position Where the vertical scroll bar should be positioned.
2945 */
2946 public void setVerticalScrollbarPosition(int position) {
2947 if (mVerticalScrollbarPosition != position) {
2948 mVerticalScrollbarPosition = position;
2949 computeOpaqueFlags();
2950 recomputePadding();
2951 }
2952 }
2953
2954 /**
2955 * @return The position where the vertical scroll bar will show, if applicable.
2956 * @see #setVerticalScrollbarPosition(int)
2957 */
2958 public int getVerticalScrollbarPosition() {
2959 return mVerticalScrollbarPosition;
2960 }
2961
2962 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 * Register a callback to be invoked when focus of this view changed.
2964 *
2965 * @param l The callback that will run.
2966 */
2967 public void setOnFocusChangeListener(OnFocusChangeListener l) {
2968 mOnFocusChangeListener = l;
2969 }
2970
2971 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002972 * Add a listener that will be called when the bounds of the view change due to
2973 * layout processing.
2974 *
2975 * @param listener The listener that will be called when layout bounds change.
2976 */
2977 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
2978 if (mOnLayoutChangeListeners == null) {
2979 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
2980 }
2981 mOnLayoutChangeListeners.add(listener);
2982 }
2983
2984 /**
2985 * Remove a listener for layout changes.
2986 *
2987 * @param listener The listener for layout bounds change.
2988 */
2989 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
2990 if (mOnLayoutChangeListeners == null) {
2991 return;
2992 }
2993 mOnLayoutChangeListeners.remove(listener);
2994 }
2995
2996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002997 * Returns the focus-change callback registered for this view.
2998 *
2999 * @return The callback, or null if one is not registered.
3000 */
3001 public OnFocusChangeListener getOnFocusChangeListener() {
3002 return mOnFocusChangeListener;
3003 }
3004
3005 /**
3006 * Register a callback to be invoked when this view is clicked. If this view is not
3007 * clickable, it becomes clickable.
3008 *
3009 * @param l The callback that will run
3010 *
3011 * @see #setClickable(boolean)
3012 */
3013 public void setOnClickListener(OnClickListener l) {
3014 if (!isClickable()) {
3015 setClickable(true);
3016 }
3017 mOnClickListener = l;
3018 }
3019
3020 /**
3021 * Register a callback to be invoked when this view is clicked and held. If this view is not
3022 * long clickable, it becomes long clickable.
3023 *
3024 * @param l The callback that will run
3025 *
3026 * @see #setLongClickable(boolean)
3027 */
3028 public void setOnLongClickListener(OnLongClickListener l) {
3029 if (!isLongClickable()) {
3030 setLongClickable(true);
3031 }
3032 mOnLongClickListener = l;
3033 }
3034
3035 /**
3036 * Register a callback to be invoked when the context menu for this view is
3037 * being built. If this view is not long clickable, it becomes long clickable.
3038 *
3039 * @param l The callback that will run
3040 *
3041 */
3042 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3043 if (!isLongClickable()) {
3044 setLongClickable(true);
3045 }
3046 mOnCreateContextMenuListener = l;
3047 }
3048
3049 /**
3050 * Call this view's OnClickListener, if it is defined.
3051 *
3052 * @return True there was an assigned OnClickListener that was called, false
3053 * otherwise is returned.
3054 */
3055 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003056 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003058 if (mOnClickListener != null) {
3059 playSoundEffect(SoundEffectConstants.CLICK);
3060 mOnClickListener.onClick(this);
3061 return true;
3062 }
3063
3064 return false;
3065 }
3066
3067 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003068 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3069 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003070 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003071 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 */
3073 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003074 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3075
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 boolean handled = false;
3077 if (mOnLongClickListener != null) {
3078 handled = mOnLongClickListener.onLongClick(View.this);
3079 }
3080 if (!handled) {
3081 handled = showContextMenu();
3082 }
3083 if (handled) {
3084 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3085 }
3086 return handled;
3087 }
3088
3089 /**
3090 * Bring up the context menu for this view.
3091 *
3092 * @return Whether a context menu was displayed.
3093 */
3094 public boolean showContextMenu() {
3095 return getParent().showContextMenuForChild(this);
3096 }
3097
3098 /**
Adam Powell6e346362010-07-23 10:18:23 -07003099 * Start an action mode.
3100 *
3101 * @param callback Callback that will control the lifecycle of the action mode
3102 * @return The new action mode if it is started, null otherwise
3103 *
3104 * @see ActionMode
3105 */
3106 public ActionMode startActionMode(ActionMode.Callback callback) {
3107 return getParent().startActionModeForChild(this, callback);
3108 }
3109
3110 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003111 * Register a callback to be invoked when a key is pressed in this view.
3112 * @param l the key listener to attach to this view
3113 */
3114 public void setOnKeyListener(OnKeyListener l) {
3115 mOnKeyListener = l;
3116 }
3117
3118 /**
3119 * Register a callback to be invoked when a touch event is sent to this view.
3120 * @param l the touch listener to attach to this view
3121 */
3122 public void setOnTouchListener(OnTouchListener l) {
3123 mOnTouchListener = l;
3124 }
3125
3126 /**
Joe Malin32736f02011-01-19 16:14:20 -08003127 * Register a drag event listener callback object for this View. The parameter is
3128 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3129 * View, the system calls the
3130 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3131 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003132 */
3133 public void setOnDragListener(OnDragListener l) {
3134 mOnDragListener = l;
3135 }
3136
3137 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003138 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3139 *
3140 * Note: this does not check whether this {@link View} should get focus, it just
3141 * gives it focus no matter what. It should only be called internally by framework
3142 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3143 *
3144 * @param direction values are View.FOCUS_UP, View.FOCUS_DOWN,
3145 * View.FOCUS_LEFT or View.FOCUS_RIGHT. This is the direction which
3146 * focus moved when requestFocus() is called. It may not always
3147 * apply, in which case use the default View.FOCUS_DOWN.
3148 * @param previouslyFocusedRect The rectangle of the view that had focus
3149 * prior in this View's coordinate system.
3150 */
3151 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3152 if (DBG) {
3153 System.out.println(this + " requestFocus()");
3154 }
3155
3156 if ((mPrivateFlags & FOCUSED) == 0) {
3157 mPrivateFlags |= FOCUSED;
3158
3159 if (mParent != null) {
3160 mParent.requestChildFocus(this, this);
3161 }
3162
3163 onFocusChanged(true, direction, previouslyFocusedRect);
3164 refreshDrawableState();
3165 }
3166 }
3167
3168 /**
3169 * Request that a rectangle of this view be visible on the screen,
3170 * scrolling if necessary just enough.
3171 *
3172 * <p>A View should call this if it maintains some notion of which part
3173 * of its content is interesting. For example, a text editing view
3174 * should call this when its cursor moves.
3175 *
3176 * @param rectangle The rectangle.
3177 * @return Whether any parent scrolled.
3178 */
3179 public boolean requestRectangleOnScreen(Rect rectangle) {
3180 return requestRectangleOnScreen(rectangle, false);
3181 }
3182
3183 /**
3184 * Request that a rectangle of this view be visible on the screen,
3185 * scrolling if necessary just enough.
3186 *
3187 * <p>A View should call this if it maintains some notion of which part
3188 * of its content is interesting. For example, a text editing view
3189 * should call this when its cursor moves.
3190 *
3191 * <p>When <code>immediate</code> is set to true, scrolling will not be
3192 * animated.
3193 *
3194 * @param rectangle The rectangle.
3195 * @param immediate True to forbid animated scrolling, false otherwise
3196 * @return Whether any parent scrolled.
3197 */
3198 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3199 View child = this;
3200 ViewParent parent = mParent;
3201 boolean scrolled = false;
3202 while (parent != null) {
3203 scrolled |= parent.requestChildRectangleOnScreen(child,
3204 rectangle, immediate);
3205
3206 // offset rect so next call has the rectangle in the
3207 // coordinate system of its direct child.
3208 rectangle.offset(child.getLeft(), child.getTop());
3209 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3210
3211 if (!(parent instanceof View)) {
3212 break;
3213 }
Romain Guy8506ab42009-06-11 17:35:47 -07003214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003215 child = (View) parent;
3216 parent = child.getParent();
3217 }
3218 return scrolled;
3219 }
3220
3221 /**
3222 * Called when this view wants to give up focus. This will cause
3223 * {@link #onFocusChanged} to be called.
3224 */
3225 public void clearFocus() {
3226 if (DBG) {
3227 System.out.println(this + " clearFocus()");
3228 }
3229
3230 if ((mPrivateFlags & FOCUSED) != 0) {
3231 mPrivateFlags &= ~FOCUSED;
3232
3233 if (mParent != null) {
3234 mParent.clearChildFocus(this);
3235 }
3236
3237 onFocusChanged(false, 0, null);
3238 refreshDrawableState();
3239 }
3240 }
3241
3242 /**
3243 * Called to clear the focus of a view that is about to be removed.
3244 * Doesn't call clearChildFocus, which prevents this view from taking
3245 * focus again before it has been removed from the parent
3246 */
3247 void clearFocusForRemoval() {
3248 if ((mPrivateFlags & FOCUSED) != 0) {
3249 mPrivateFlags &= ~FOCUSED;
3250
3251 onFocusChanged(false, 0, null);
3252 refreshDrawableState();
3253 }
3254 }
3255
3256 /**
3257 * Called internally by the view system when a new view is getting focus.
3258 * This is what clears the old focus.
3259 */
3260 void unFocus() {
3261 if (DBG) {
3262 System.out.println(this + " unFocus()");
3263 }
3264
3265 if ((mPrivateFlags & FOCUSED) != 0) {
3266 mPrivateFlags &= ~FOCUSED;
3267
3268 onFocusChanged(false, 0, null);
3269 refreshDrawableState();
3270 }
3271 }
3272
3273 /**
3274 * Returns true if this view has focus iteself, or is the ancestor of the
3275 * view that has focus.
3276 *
3277 * @return True if this view has or contains focus, false otherwise.
3278 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003279 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003280 public boolean hasFocus() {
3281 return (mPrivateFlags & FOCUSED) != 0;
3282 }
3283
3284 /**
3285 * Returns true if this view is focusable or if it contains a reachable View
3286 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3287 * is a View whose parents do not block descendants focus.
3288 *
3289 * Only {@link #VISIBLE} views are considered focusable.
3290 *
3291 * @return True if the view is focusable or if the view contains a focusable
3292 * View, false otherwise.
3293 *
3294 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3295 */
3296 public boolean hasFocusable() {
3297 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3298 }
3299
3300 /**
3301 * Called by the view system when the focus state of this view changes.
3302 * When the focus change event is caused by directional navigation, direction
3303 * and previouslyFocusedRect provide insight into where the focus is coming from.
3304 * When overriding, be sure to call up through to the super class so that
3305 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003306 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003307 * @param gainFocus True if the View has focus; false otherwise.
3308 * @param direction The direction focus has moved when requestFocus()
3309 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003310 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3311 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3312 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003313 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3314 * system, of the previously focused view. If applicable, this will be
3315 * passed in as finer grained information about where the focus is coming
3316 * from (in addition to direction). Will be <code>null</code> otherwise.
3317 */
3318 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003319 if (gainFocus) {
3320 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3321 }
3322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003323 InputMethodManager imm = InputMethodManager.peekInstance();
3324 if (!gainFocus) {
3325 if (isPressed()) {
3326 setPressed(false);
3327 }
3328 if (imm != null && mAttachInfo != null
3329 && mAttachInfo.mHasWindowFocus) {
3330 imm.focusOut(this);
3331 }
Romain Guya2431d02009-04-30 16:30:00 -07003332 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003333 } else if (imm != null && mAttachInfo != null
3334 && mAttachInfo.mHasWindowFocus) {
3335 imm.focusIn(this);
3336 }
Romain Guy8506ab42009-06-11 17:35:47 -07003337
Romain Guy0fd89bf2011-01-26 15:41:30 -08003338 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003339 if (mOnFocusChangeListener != null) {
3340 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3341 }
Joe Malin32736f02011-01-19 16:14:20 -08003342
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003343 if (mAttachInfo != null) {
3344 mAttachInfo.mKeyDispatchState.reset(this);
3345 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003346 }
3347
3348 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003349 * {@inheritDoc}
3350 */
3351 public void sendAccessibilityEvent(int eventType) {
3352 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3353 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3354 }
3355 }
3356
3357 /**
3358 * {@inheritDoc}
3359 */
3360 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003361 if (!isShown()) {
3362 return;
3363 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003364 event.setClassName(getClass().getName());
3365 event.setPackageName(getContext().getPackageName());
3366 event.setEnabled(isEnabled());
3367 event.setContentDescription(mContentDescription);
3368
3369 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3370 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3371 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3372 event.setItemCount(focusablesTempList.size());
3373 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3374 focusablesTempList.clear();
3375 }
3376
3377 dispatchPopulateAccessibilityEvent(event);
3378
3379 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3380 }
3381
3382 /**
3383 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3384 * to be populated.
3385 *
3386 * @param event The event.
3387 *
3388 * @return True if the event population was completed.
3389 */
3390 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3391 return false;
3392 }
3393
3394 /**
3395 * Gets the {@link View} description. It briefly describes the view and is
3396 * primarily used for accessibility support. Set this property to enable
3397 * better accessibility support for your application. This is especially
3398 * true for views that do not have textual representation (For example,
3399 * ImageButton).
3400 *
3401 * @return The content descriptiopn.
3402 *
3403 * @attr ref android.R.styleable#View_contentDescription
3404 */
3405 public CharSequence getContentDescription() {
3406 return mContentDescription;
3407 }
3408
3409 /**
3410 * Sets the {@link View} description. It briefly describes the view and is
3411 * primarily used for accessibility support. Set this property to enable
3412 * better accessibility support for your application. This is especially
3413 * true for views that do not have textual representation (For example,
3414 * ImageButton).
3415 *
3416 * @param contentDescription The content description.
3417 *
3418 * @attr ref android.R.styleable#View_contentDescription
3419 */
3420 public void setContentDescription(CharSequence contentDescription) {
3421 mContentDescription = contentDescription;
3422 }
3423
3424 /**
Romain Guya2431d02009-04-30 16:30:00 -07003425 * Invoked whenever this view loses focus, either by losing window focus or by losing
3426 * focus within its window. This method can be used to clear any state tied to the
3427 * focus. For instance, if a button is held pressed with the trackball and the window
3428 * loses focus, this method can be used to cancel the press.
3429 *
3430 * Subclasses of View overriding this method should always call super.onFocusLost().
3431 *
3432 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003433 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003434 *
3435 * @hide pending API council approval
3436 */
3437 protected void onFocusLost() {
3438 resetPressedState();
3439 }
3440
3441 private void resetPressedState() {
3442 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3443 return;
3444 }
3445
3446 if (isPressed()) {
3447 setPressed(false);
3448
3449 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003450 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003451 }
3452 }
3453 }
3454
3455 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003456 * Returns true if this view has focus
3457 *
3458 * @return True if this view has focus, false otherwise.
3459 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003460 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 public boolean isFocused() {
3462 return (mPrivateFlags & FOCUSED) != 0;
3463 }
3464
3465 /**
3466 * Find the view in the hierarchy rooted at this view that currently has
3467 * focus.
3468 *
3469 * @return The view that currently has focus, or null if no focused view can
3470 * be found.
3471 */
3472 public View findFocus() {
3473 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3474 }
3475
3476 /**
3477 * Change whether this view is one of the set of scrollable containers in
3478 * its window. This will be used to determine whether the window can
3479 * resize or must pan when a soft input area is open -- scrollable
3480 * containers allow the window to use resize mode since the container
3481 * will appropriately shrink.
3482 */
3483 public void setScrollContainer(boolean isScrollContainer) {
3484 if (isScrollContainer) {
3485 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3486 mAttachInfo.mScrollContainers.add(this);
3487 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3488 }
3489 mPrivateFlags |= SCROLL_CONTAINER;
3490 } else {
3491 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3492 mAttachInfo.mScrollContainers.remove(this);
3493 }
3494 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3495 }
3496 }
3497
3498 /**
3499 * Returns the quality of the drawing cache.
3500 *
3501 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3502 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3503 *
3504 * @see #setDrawingCacheQuality(int)
3505 * @see #setDrawingCacheEnabled(boolean)
3506 * @see #isDrawingCacheEnabled()
3507 *
3508 * @attr ref android.R.styleable#View_drawingCacheQuality
3509 */
3510 public int getDrawingCacheQuality() {
3511 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3512 }
3513
3514 /**
3515 * Set the drawing cache quality of this view. This value is used only when the
3516 * drawing cache is enabled
3517 *
3518 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3519 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3520 *
3521 * @see #getDrawingCacheQuality()
3522 * @see #setDrawingCacheEnabled(boolean)
3523 * @see #isDrawingCacheEnabled()
3524 *
3525 * @attr ref android.R.styleable#View_drawingCacheQuality
3526 */
3527 public void setDrawingCacheQuality(int quality) {
3528 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3529 }
3530
3531 /**
3532 * Returns whether the screen should remain on, corresponding to the current
3533 * value of {@link #KEEP_SCREEN_ON}.
3534 *
3535 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3536 *
3537 * @see #setKeepScreenOn(boolean)
3538 *
3539 * @attr ref android.R.styleable#View_keepScreenOn
3540 */
3541 public boolean getKeepScreenOn() {
3542 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3543 }
3544
3545 /**
3546 * Controls whether the screen should remain on, modifying the
3547 * value of {@link #KEEP_SCREEN_ON}.
3548 *
3549 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3550 *
3551 * @see #getKeepScreenOn()
3552 *
3553 * @attr ref android.R.styleable#View_keepScreenOn
3554 */
3555 public void setKeepScreenOn(boolean keepScreenOn) {
3556 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3557 }
3558
3559 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003560 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3561 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003562 *
3563 * @attr ref android.R.styleable#View_nextFocusLeft
3564 */
3565 public int getNextFocusLeftId() {
3566 return mNextFocusLeftId;
3567 }
3568
3569 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003570 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3571 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3572 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003573 *
3574 * @attr ref android.R.styleable#View_nextFocusLeft
3575 */
3576 public void setNextFocusLeftId(int nextFocusLeftId) {
3577 mNextFocusLeftId = nextFocusLeftId;
3578 }
3579
3580 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003581 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3582 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003583 *
3584 * @attr ref android.R.styleable#View_nextFocusRight
3585 */
3586 public int getNextFocusRightId() {
3587 return mNextFocusRightId;
3588 }
3589
3590 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003591 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3592 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3593 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 *
3595 * @attr ref android.R.styleable#View_nextFocusRight
3596 */
3597 public void setNextFocusRightId(int nextFocusRightId) {
3598 mNextFocusRightId = nextFocusRightId;
3599 }
3600
3601 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003602 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3603 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003604 *
3605 * @attr ref android.R.styleable#View_nextFocusUp
3606 */
3607 public int getNextFocusUpId() {
3608 return mNextFocusUpId;
3609 }
3610
3611 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003612 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3613 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3614 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003615 *
3616 * @attr ref android.R.styleable#View_nextFocusUp
3617 */
3618 public void setNextFocusUpId(int nextFocusUpId) {
3619 mNextFocusUpId = nextFocusUpId;
3620 }
3621
3622 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003623 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3624 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003625 *
3626 * @attr ref android.R.styleable#View_nextFocusDown
3627 */
3628 public int getNextFocusDownId() {
3629 return mNextFocusDownId;
3630 }
3631
3632 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003633 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3634 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3635 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003636 *
3637 * @attr ref android.R.styleable#View_nextFocusDown
3638 */
3639 public void setNextFocusDownId(int nextFocusDownId) {
3640 mNextFocusDownId = nextFocusDownId;
3641 }
3642
3643 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003644 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3645 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3646 *
3647 * @attr ref android.R.styleable#View_nextFocusForward
3648 */
3649 public int getNextFocusForwardId() {
3650 return mNextFocusForwardId;
3651 }
3652
3653 /**
3654 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3655 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3656 * decide automatically.
3657 *
3658 * @attr ref android.R.styleable#View_nextFocusForward
3659 */
3660 public void setNextFocusForwardId(int nextFocusForwardId) {
3661 mNextFocusForwardId = nextFocusForwardId;
3662 }
3663
3664 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003665 * Returns the visibility of this view and all of its ancestors
3666 *
3667 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3668 */
3669 public boolean isShown() {
3670 View current = this;
3671 //noinspection ConstantConditions
3672 do {
3673 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3674 return false;
3675 }
3676 ViewParent parent = current.mParent;
3677 if (parent == null) {
3678 return false; // We are not attached to the view root
3679 }
3680 if (!(parent instanceof View)) {
3681 return true;
3682 }
3683 current = (View) parent;
3684 } while (current != null);
3685
3686 return false;
3687 }
3688
3689 /**
3690 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3691 * is set
3692 *
3693 * @param insets Insets for system windows
3694 *
3695 * @return True if this view applied the insets, false otherwise
3696 */
3697 protected boolean fitSystemWindows(Rect insets) {
3698 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3699 mPaddingLeft = insets.left;
3700 mPaddingTop = insets.top;
3701 mPaddingRight = insets.right;
3702 mPaddingBottom = insets.bottom;
3703 requestLayout();
3704 return true;
3705 }
3706 return false;
3707 }
3708
3709 /**
3710 * Returns the visibility status for this view.
3711 *
3712 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3713 * @attr ref android.R.styleable#View_visibility
3714 */
3715 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003716 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3717 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3718 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 })
3720 public int getVisibility() {
3721 return mViewFlags & VISIBILITY_MASK;
3722 }
3723
3724 /**
3725 * Set the enabled state of this view.
3726 *
3727 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3728 * @attr ref android.R.styleable#View_visibility
3729 */
3730 @RemotableViewMethod
3731 public void setVisibility(int visibility) {
3732 setFlags(visibility, VISIBILITY_MASK);
3733 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3734 }
3735
3736 /**
3737 * Returns the enabled status for this view. The interpretation of the
3738 * enabled state varies by subclass.
3739 *
3740 * @return True if this view is enabled, false otherwise.
3741 */
3742 @ViewDebug.ExportedProperty
3743 public boolean isEnabled() {
3744 return (mViewFlags & ENABLED_MASK) == ENABLED;
3745 }
3746
3747 /**
3748 * Set the enabled state of this view. The interpretation of the enabled
3749 * state varies by subclass.
3750 *
3751 * @param enabled True if this view is enabled, false otherwise.
3752 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003753 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003754 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003755 if (enabled == isEnabled()) return;
3756
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3758
3759 /*
3760 * The View most likely has to change its appearance, so refresh
3761 * the drawable state.
3762 */
3763 refreshDrawableState();
3764
3765 // Invalidate too, since the default behavior for views is to be
3766 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003767 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003768 }
3769
3770 /**
3771 * Set whether this view can receive the focus.
3772 *
3773 * Setting this to false will also ensure that this view is not focusable
3774 * in touch mode.
3775 *
3776 * @param focusable If true, this view can receive the focus.
3777 *
3778 * @see #setFocusableInTouchMode(boolean)
3779 * @attr ref android.R.styleable#View_focusable
3780 */
3781 public void setFocusable(boolean focusable) {
3782 if (!focusable) {
3783 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3784 }
3785 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3786 }
3787
3788 /**
3789 * Set whether this view can receive focus while in touch mode.
3790 *
3791 * Setting this to true will also ensure that this view is focusable.
3792 *
3793 * @param focusableInTouchMode If true, this view can receive the focus while
3794 * in touch mode.
3795 *
3796 * @see #setFocusable(boolean)
3797 * @attr ref android.R.styleable#View_focusableInTouchMode
3798 */
3799 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3800 // Focusable in touch mode should always be set before the focusable flag
3801 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3802 // which, in touch mode, will not successfully request focus on this view
3803 // because the focusable in touch mode flag is not set
3804 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3805 if (focusableInTouchMode) {
3806 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3807 }
3808 }
3809
3810 /**
3811 * Set whether this view should have sound effects enabled for events such as
3812 * clicking and touching.
3813 *
3814 * <p>You may wish to disable sound effects for a view if you already play sounds,
3815 * for instance, a dial key that plays dtmf tones.
3816 *
3817 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3818 * @see #isSoundEffectsEnabled()
3819 * @see #playSoundEffect(int)
3820 * @attr ref android.R.styleable#View_soundEffectsEnabled
3821 */
3822 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3823 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3824 }
3825
3826 /**
3827 * @return whether this view should have sound effects enabled for events such as
3828 * clicking and touching.
3829 *
3830 * @see #setSoundEffectsEnabled(boolean)
3831 * @see #playSoundEffect(int)
3832 * @attr ref android.R.styleable#View_soundEffectsEnabled
3833 */
3834 @ViewDebug.ExportedProperty
3835 public boolean isSoundEffectsEnabled() {
3836 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3837 }
3838
3839 /**
3840 * Set whether this view should have haptic feedback for events such as
3841 * long presses.
3842 *
3843 * <p>You may wish to disable haptic feedback if your view already controls
3844 * its own haptic feedback.
3845 *
3846 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3847 * @see #isHapticFeedbackEnabled()
3848 * @see #performHapticFeedback(int)
3849 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3850 */
3851 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3852 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3853 }
3854
3855 /**
3856 * @return whether this view should have haptic feedback enabled for events
3857 * long presses.
3858 *
3859 * @see #setHapticFeedbackEnabled(boolean)
3860 * @see #performHapticFeedback(int)
3861 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3862 */
3863 @ViewDebug.ExportedProperty
3864 public boolean isHapticFeedbackEnabled() {
3865 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3866 }
3867
3868 /**
3869 * If this view doesn't do any drawing on its own, set this flag to
3870 * allow further optimizations. By default, this flag is not set on
3871 * View, but could be set on some View subclasses such as ViewGroup.
3872 *
3873 * Typically, if you override {@link #onDraw} you should clear this flag.
3874 *
3875 * @param willNotDraw whether or not this View draw on its own
3876 */
3877 public void setWillNotDraw(boolean willNotDraw) {
3878 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3879 }
3880
3881 /**
3882 * Returns whether or not this View draws on its own.
3883 *
3884 * @return true if this view has nothing to draw, false otherwise
3885 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003886 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003887 public boolean willNotDraw() {
3888 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3889 }
3890
3891 /**
3892 * When a View's drawing cache is enabled, drawing is redirected to an
3893 * offscreen bitmap. Some views, like an ImageView, must be able to
3894 * bypass this mechanism if they already draw a single bitmap, to avoid
3895 * unnecessary usage of the memory.
3896 *
3897 * @param willNotCacheDrawing true if this view does not cache its
3898 * drawing, false otherwise
3899 */
3900 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3901 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3902 }
3903
3904 /**
3905 * Returns whether or not this View can cache its drawing or not.
3906 *
3907 * @return true if this view does not cache its drawing, false otherwise
3908 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003909 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003910 public boolean willNotCacheDrawing() {
3911 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
3912 }
3913
3914 /**
3915 * Indicates whether this view reacts to click events or not.
3916 *
3917 * @return true if the view is clickable, false otherwise
3918 *
3919 * @see #setClickable(boolean)
3920 * @attr ref android.R.styleable#View_clickable
3921 */
3922 @ViewDebug.ExportedProperty
3923 public boolean isClickable() {
3924 return (mViewFlags & CLICKABLE) == CLICKABLE;
3925 }
3926
3927 /**
3928 * Enables or disables click events for this view. When a view
3929 * is clickable it will change its state to "pressed" on every click.
3930 * Subclasses should set the view clickable to visually react to
3931 * user's clicks.
3932 *
3933 * @param clickable true to make the view clickable, false otherwise
3934 *
3935 * @see #isClickable()
3936 * @attr ref android.R.styleable#View_clickable
3937 */
3938 public void setClickable(boolean clickable) {
3939 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
3940 }
3941
3942 /**
3943 * Indicates whether this view reacts to long click events or not.
3944 *
3945 * @return true if the view is long clickable, false otherwise
3946 *
3947 * @see #setLongClickable(boolean)
3948 * @attr ref android.R.styleable#View_longClickable
3949 */
3950 public boolean isLongClickable() {
3951 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
3952 }
3953
3954 /**
3955 * Enables or disables long click events for this view. When a view is long
3956 * clickable it reacts to the user holding down the button for a longer
3957 * duration than a tap. This event can either launch the listener or a
3958 * context menu.
3959 *
3960 * @param longClickable true to make the view long clickable, false otherwise
3961 * @see #isLongClickable()
3962 * @attr ref android.R.styleable#View_longClickable
3963 */
3964 public void setLongClickable(boolean longClickable) {
3965 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
3966 }
3967
3968 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07003969 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003970 *
3971 * @see #isClickable()
3972 * @see #setClickable(boolean)
3973 *
3974 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
3975 * the View's internal state from a previously set "pressed" state.
3976 */
3977 public void setPressed(boolean pressed) {
3978 if (pressed) {
3979 mPrivateFlags |= PRESSED;
3980 } else {
3981 mPrivateFlags &= ~PRESSED;
3982 }
3983 refreshDrawableState();
3984 dispatchSetPressed(pressed);
3985 }
3986
3987 /**
3988 * Dispatch setPressed to all of this View's children.
3989 *
3990 * @see #setPressed(boolean)
3991 *
3992 * @param pressed The new pressed state
3993 */
3994 protected void dispatchSetPressed(boolean pressed) {
3995 }
3996
3997 /**
3998 * Indicates whether the view is currently in pressed state. Unless
3999 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4000 * the pressed state.
4001 *
4002 * @see #setPressed
4003 * @see #isClickable()
4004 * @see #setClickable(boolean)
4005 *
4006 * @return true if the view is currently pressed, false otherwise
4007 */
4008 public boolean isPressed() {
4009 return (mPrivateFlags & PRESSED) == PRESSED;
4010 }
4011
4012 /**
4013 * Indicates whether this view will save its state (that is,
4014 * whether its {@link #onSaveInstanceState} method will be called).
4015 *
4016 * @return Returns true if the view state saving is enabled, else false.
4017 *
4018 * @see #setSaveEnabled(boolean)
4019 * @attr ref android.R.styleable#View_saveEnabled
4020 */
4021 public boolean isSaveEnabled() {
4022 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4023 }
4024
4025 /**
4026 * Controls whether the saving of this view's state is
4027 * enabled (that is, whether its {@link #onSaveInstanceState} method
4028 * will be called). Note that even if freezing is enabled, the
4029 * view still must have an id assigned to it (via {@link #setId setId()})
4030 * for its state to be saved. This flag can only disable the
4031 * saving of this view; any child views may still have their state saved.
4032 *
4033 * @param enabled Set to false to <em>disable</em> state saving, or true
4034 * (the default) to allow it.
4035 *
4036 * @see #isSaveEnabled()
4037 * @see #setId(int)
4038 * @see #onSaveInstanceState()
4039 * @attr ref android.R.styleable#View_saveEnabled
4040 */
4041 public void setSaveEnabled(boolean enabled) {
4042 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4043 }
4044
Jeff Brown85a31762010-09-01 17:01:00 -07004045 /**
4046 * Gets whether the framework should discard touches when the view's
4047 * window is obscured by another visible window.
4048 * Refer to the {@link View} security documentation for more details.
4049 *
4050 * @return True if touch filtering is enabled.
4051 *
4052 * @see #setFilterTouchesWhenObscured(boolean)
4053 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4054 */
4055 @ViewDebug.ExportedProperty
4056 public boolean getFilterTouchesWhenObscured() {
4057 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4058 }
4059
4060 /**
4061 * Sets whether the framework should discard touches when the view's
4062 * window is obscured by another visible window.
4063 * Refer to the {@link View} security documentation for more details.
4064 *
4065 * @param enabled True if touch filtering should be enabled.
4066 *
4067 * @see #getFilterTouchesWhenObscured
4068 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4069 */
4070 public void setFilterTouchesWhenObscured(boolean enabled) {
4071 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4072 FILTER_TOUCHES_WHEN_OBSCURED);
4073 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004074
4075 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004076 * Indicates whether the entire hierarchy under this view will save its
4077 * state when a state saving traversal occurs from its parent. The default
4078 * is true; if false, these views will not be saved unless
4079 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4080 *
4081 * @return Returns true if the view state saving from parent is enabled, else false.
4082 *
4083 * @see #setSaveFromParentEnabled(boolean)
4084 */
4085 public boolean isSaveFromParentEnabled() {
4086 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4087 }
4088
4089 /**
4090 * Controls whether the entire hierarchy under this view will save its
4091 * state when a state saving traversal occurs from its parent. The default
4092 * is true; if false, these views will not be saved unless
4093 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4094 *
4095 * @param enabled Set to false to <em>disable</em> state saving, or true
4096 * (the default) to allow it.
4097 *
4098 * @see #isSaveFromParentEnabled()
4099 * @see #setId(int)
4100 * @see #onSaveInstanceState()
4101 */
4102 public void setSaveFromParentEnabled(boolean enabled) {
4103 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4104 }
4105
4106
4107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004108 * Returns whether this View is able to take focus.
4109 *
4110 * @return True if this view can take focus, or false otherwise.
4111 * @attr ref android.R.styleable#View_focusable
4112 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004113 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004114 public final boolean isFocusable() {
4115 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4116 }
4117
4118 /**
4119 * When a view is focusable, it may not want to take focus when in touch mode.
4120 * For example, a button would like focus when the user is navigating via a D-pad
4121 * so that the user can click on it, but once the user starts touching the screen,
4122 * the button shouldn't take focus
4123 * @return Whether the view is focusable in touch mode.
4124 * @attr ref android.R.styleable#View_focusableInTouchMode
4125 */
4126 @ViewDebug.ExportedProperty
4127 public final boolean isFocusableInTouchMode() {
4128 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4129 }
4130
4131 /**
4132 * Find the nearest view in the specified direction that can take focus.
4133 * This does not actually give focus to that view.
4134 *
4135 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4136 *
4137 * @return The nearest focusable in the specified direction, or null if none
4138 * can be found.
4139 */
4140 public View focusSearch(int direction) {
4141 if (mParent != null) {
4142 return mParent.focusSearch(this, direction);
4143 } else {
4144 return null;
4145 }
4146 }
4147
4148 /**
4149 * This method is the last chance for the focused view and its ancestors to
4150 * respond to an arrow key. This is called when the focused view did not
4151 * consume the key internally, nor could the view system find a new view in
4152 * the requested direction to give focus to.
4153 *
4154 * @param focused The currently focused view.
4155 * @param direction The direction focus wants to move. One of FOCUS_UP,
4156 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4157 * @return True if the this view consumed this unhandled move.
4158 */
4159 public boolean dispatchUnhandledMove(View focused, int direction) {
4160 return false;
4161 }
4162
4163 /**
4164 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004165 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004167 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4168 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004169 * @return The user specified next view, or null if there is none.
4170 */
4171 View findUserSetNextFocus(View root, int direction) {
4172 switch (direction) {
4173 case FOCUS_LEFT:
4174 if (mNextFocusLeftId == View.NO_ID) return null;
4175 return findViewShouldExist(root, mNextFocusLeftId);
4176 case FOCUS_RIGHT:
4177 if (mNextFocusRightId == View.NO_ID) return null;
4178 return findViewShouldExist(root, mNextFocusRightId);
4179 case FOCUS_UP:
4180 if (mNextFocusUpId == View.NO_ID) return null;
4181 return findViewShouldExist(root, mNextFocusUpId);
4182 case FOCUS_DOWN:
4183 if (mNextFocusDownId == View.NO_ID) return null;
4184 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004185 case FOCUS_FORWARD:
4186 if (mNextFocusForwardId == View.NO_ID) return null;
4187 return findViewShouldExist(root, mNextFocusForwardId);
4188 case FOCUS_BACKWARD: {
4189 final int id = mID;
4190 return root.findViewByPredicate(new Predicate<View>() {
4191 @Override
4192 public boolean apply(View t) {
4193 return t.mNextFocusForwardId == id;
4194 }
4195 });
4196 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004197 }
4198 return null;
4199 }
4200
4201 private static View findViewShouldExist(View root, int childViewId) {
4202 View result = root.findViewById(childViewId);
4203 if (result == null) {
4204 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4205 + "by user for id " + childViewId);
4206 }
4207 return result;
4208 }
4209
4210 /**
4211 * Find and return all focusable views that are descendants of this view,
4212 * possibly including this view if it is focusable itself.
4213 *
4214 * @param direction The direction of the focus
4215 * @return A list of focusable views
4216 */
4217 public ArrayList<View> getFocusables(int direction) {
4218 ArrayList<View> result = new ArrayList<View>(24);
4219 addFocusables(result, direction);
4220 return result;
4221 }
4222
4223 /**
4224 * Add any focusable views that are descendants of this view (possibly
4225 * including this view if it is focusable itself) to views. If we are in touch mode,
4226 * only add views that are also focusable in touch mode.
4227 *
4228 * @param views Focusable views found so far
4229 * @param direction The direction of the focus
4230 */
4231 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004232 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4233 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004234
svetoslavganov75986cf2009-05-14 22:28:01 -07004235 /**
4236 * Adds any focusable views that are descendants of this view (possibly
4237 * including this view if it is focusable itself) to views. This method
4238 * adds all focusable views regardless if we are in touch mode or
4239 * only views focusable in touch mode if we are in touch mode depending on
4240 * the focusable mode paramater.
4241 *
4242 * @param views Focusable views found so far or null if all we are interested is
4243 * the number of focusables.
4244 * @param direction The direction of the focus.
4245 * @param focusableMode The type of focusables to be added.
4246 *
4247 * @see #FOCUSABLES_ALL
4248 * @see #FOCUSABLES_TOUCH_MODE
4249 */
4250 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4251 if (!isFocusable()) {
4252 return;
4253 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004254
svetoslavganov75986cf2009-05-14 22:28:01 -07004255 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4256 isInTouchMode() && !isFocusableInTouchMode()) {
4257 return;
4258 }
4259
4260 if (views != null) {
4261 views.add(this);
4262 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 }
4264
4265 /**
4266 * Find and return all touchable views that are descendants of this view,
4267 * possibly including this view if it is touchable itself.
4268 *
4269 * @return A list of touchable views
4270 */
4271 public ArrayList<View> getTouchables() {
4272 ArrayList<View> result = new ArrayList<View>();
4273 addTouchables(result);
4274 return result;
4275 }
4276
4277 /**
4278 * Add any touchable views that are descendants of this view (possibly
4279 * including this view if it is touchable itself) to views.
4280 *
4281 * @param views Touchable views found so far
4282 */
4283 public void addTouchables(ArrayList<View> views) {
4284 final int viewFlags = mViewFlags;
4285
4286 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4287 && (viewFlags & ENABLED_MASK) == ENABLED) {
4288 views.add(this);
4289 }
4290 }
4291
4292 /**
4293 * Call this to try to give focus to a specific view or to one of its
4294 * descendants.
4295 *
4296 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4297 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4298 * while the device is in touch mode.
4299 *
4300 * See also {@link #focusSearch}, which is what you call to say that you
4301 * have focus, and you want your parent to look for the next one.
4302 *
4303 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4304 * {@link #FOCUS_DOWN} and <code>null</code>.
4305 *
4306 * @return Whether this view or one of its descendants actually took focus.
4307 */
4308 public final boolean requestFocus() {
4309 return requestFocus(View.FOCUS_DOWN);
4310 }
4311
4312
4313 /**
4314 * Call this to try to give focus to a specific view or to one of its
4315 * descendants and give it a hint about what direction focus is heading.
4316 *
4317 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4318 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4319 * while the device is in touch mode.
4320 *
4321 * See also {@link #focusSearch}, which is what you call to say that you
4322 * have focus, and you want your parent to look for the next one.
4323 *
4324 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4325 * <code>null</code> set for the previously focused rectangle.
4326 *
4327 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4328 * @return Whether this view or one of its descendants actually took focus.
4329 */
4330 public final boolean requestFocus(int direction) {
4331 return requestFocus(direction, null);
4332 }
4333
4334 /**
4335 * Call this to try to give focus to a specific view or to one of its descendants
4336 * and give it hints about the direction and a specific rectangle that the focus
4337 * is coming from. The rectangle can help give larger views a finer grained hint
4338 * about where focus is coming from, and therefore, where to show selection, or
4339 * forward focus change internally.
4340 *
4341 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns false),
4342 * or if it is focusable and it is not focusable in touch mode ({@link #isFocusableInTouchMode})
4343 * while the device is in touch mode.
4344 *
4345 * A View will not take focus if it is not visible.
4346 *
4347 * A View will not take focus if one of its parents has {@link android.view.ViewGroup#getDescendantFocusability()}
4348 * equal to {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
4349 *
4350 * See also {@link #focusSearch}, which is what you call to say that you
4351 * have focus, and you want your parent to look for the next one.
4352 *
4353 * You may wish to override this method if your custom {@link View} has an internal
4354 * {@link View} that it wishes to forward the request to.
4355 *
4356 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4357 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4358 * to give a finer grained hint about where focus is coming from. May be null
4359 * if there is no hint.
4360 * @return Whether this view or one of its descendants actually took focus.
4361 */
4362 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4363 // need to be focusable
4364 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4365 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4366 return false;
4367 }
4368
4369 // need to be focusable in touch mode if in touch mode
4370 if (isInTouchMode() &&
4371 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4372 return false;
4373 }
4374
4375 // need to not have any parents blocking us
4376 if (hasAncestorThatBlocksDescendantFocus()) {
4377 return false;
4378 }
4379
4380 handleFocusGainInternal(direction, previouslyFocusedRect);
4381 return true;
4382 }
4383
Christopher Tate2c095f32010-10-04 14:13:40 -07004384 /** Gets the ViewRoot, or null if not attached. */
4385 /*package*/ ViewRoot getViewRoot() {
4386 View root = getRootView();
4387 return root != null ? (ViewRoot)root.getParent() : null;
4388 }
4389
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 /**
4391 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4392 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4393 * touch mode to request focus when they are touched.
4394 *
4395 * @return Whether this view or one of its descendants actually took focus.
4396 *
4397 * @see #isInTouchMode()
4398 *
4399 */
4400 public final boolean requestFocusFromTouch() {
4401 // Leave touch mode if we need to
4402 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004403 ViewRoot viewRoot = getViewRoot();
4404 if (viewRoot != null) {
4405 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004406 }
4407 }
4408 return requestFocus(View.FOCUS_DOWN);
4409 }
4410
4411 /**
4412 * @return Whether any ancestor of this view blocks descendant focus.
4413 */
4414 private boolean hasAncestorThatBlocksDescendantFocus() {
4415 ViewParent ancestor = mParent;
4416 while (ancestor instanceof ViewGroup) {
4417 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4418 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4419 return true;
4420 } else {
4421 ancestor = vgAncestor.getParent();
4422 }
4423 }
4424 return false;
4425 }
4426
4427 /**
Romain Guya440b002010-02-24 15:57:54 -08004428 * @hide
4429 */
4430 public void dispatchStartTemporaryDetach() {
4431 onStartTemporaryDetach();
4432 }
4433
4434 /**
4435 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004436 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4437 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004438 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004439 */
4440 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004441 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004442 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004443 }
4444
4445 /**
4446 * @hide
4447 */
4448 public void dispatchFinishTemporaryDetach() {
4449 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004450 }
Romain Guy8506ab42009-06-11 17:35:47 -07004451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004452 /**
4453 * Called after {@link #onStartTemporaryDetach} when the container is done
4454 * changing the view.
4455 */
4456 public void onFinishTemporaryDetach() {
4457 }
Romain Guy8506ab42009-06-11 17:35:47 -07004458
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004459 /**
4460 * capture information of this view for later analysis: developement only
4461 * check dynamic switch to make sure we only dump view
4462 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4463 */
4464 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004465 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004466 return;
4467 }
4468 ViewDebug.dumpCapturedView(subTag, v);
4469 }
4470
4471 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004472 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4473 * for this view's window. Returns null if the view is not currently attached
4474 * to the window. Normally you will not need to use this directly, but
4475 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4476 */
4477 public KeyEvent.DispatcherState getKeyDispatcherState() {
4478 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4479 }
Joe Malin32736f02011-01-19 16:14:20 -08004480
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004481 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004482 * Dispatch a key event before it is processed by any input method
4483 * associated with the view hierarchy. This can be used to intercept
4484 * key events in special situations before the IME consumes them; a
4485 * typical example would be handling the BACK key to update the application's
4486 * UI instead of allowing the IME to see it and close itself.
4487 *
4488 * @param event The key event to be dispatched.
4489 * @return True if the event was handled, false otherwise.
4490 */
4491 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4492 return onKeyPreIme(event.getKeyCode(), event);
4493 }
4494
4495 /**
4496 * Dispatch a key event to the next view on the focus path. This path runs
4497 * from the top of the view tree down to the currently focused view. If this
4498 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4499 * the next node down the focus path. This method also fires any key
4500 * listeners.
4501 *
4502 * @param event The key event to be dispatched.
4503 * @return True if the event was handled, false otherwise.
4504 */
4505 public boolean dispatchKeyEvent(KeyEvent event) {
4506 // If any attached key listener a first crack at the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004507
Romain Guyf607bdc2010-09-10 19:20:06 -07004508 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004509 if (android.util.Config.LOGV) {
4510 captureViewInfo("captureViewKeyEvent", this);
4511 }
4512
Romain Guyf607bdc2010-09-10 19:20:06 -07004513 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004514 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4515 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4516 return true;
4517 }
4518
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004519 return event.dispatch(this, mAttachInfo != null
4520 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 }
4522
4523 /**
4524 * Dispatches a key shortcut event.
4525 *
4526 * @param event The key event to be dispatched.
4527 * @return True if the event was handled by the view, false otherwise.
4528 */
4529 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4530 return onKeyShortcut(event.getKeyCode(), event);
4531 }
4532
4533 /**
4534 * Pass the touch screen motion event down to the target view, or this
4535 * view if it is the target.
4536 *
4537 * @param event The motion event to be dispatched.
4538 * @return True if the event was handled by the view, false otherwise.
4539 */
4540 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown85a31762010-09-01 17:01:00 -07004541 if (!onFilterTouchEventForSecurity(event)) {
4542 return false;
4543 }
4544
Romain Guyf607bdc2010-09-10 19:20:06 -07004545 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004546 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4547 mOnTouchListener.onTouch(this, event)) {
4548 return true;
4549 }
4550 return onTouchEvent(event);
4551 }
4552
4553 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004554 * Filter the touch event to apply security policies.
4555 *
4556 * @param event The motion event to be filtered.
4557 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004558 *
Jeff Brown85a31762010-09-01 17:01:00 -07004559 * @see #getFilterTouchesWhenObscured
4560 */
4561 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004562 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004563 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4564 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4565 // Window is obscured, drop this touch.
4566 return false;
4567 }
4568 return true;
4569 }
4570
4571 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004572 * Pass a trackball motion event down to the focused view.
4573 *
4574 * @param event The motion event to be dispatched.
4575 * @return True if the event was handled by the view, false otherwise.
4576 */
4577 public boolean dispatchTrackballEvent(MotionEvent event) {
4578 //Log.i("view", "view=" + this + ", " + event.toString());
4579 return onTrackballEvent(event);
4580 }
4581
4582 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08004583 * Pass a generic motion event down to the focused view.
4584 *
4585 * @param event The motion event to be dispatched.
4586 * @return True if the event was handled by the view, false otherwise.
4587 */
4588 public boolean dispatchGenericMotionEvent(MotionEvent event) {
4589 return onGenericMotionEvent(event);
4590 }
4591
4592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004593 * Called when the window containing this view gains or loses window focus.
4594 * ViewGroups should override to route to their children.
4595 *
4596 * @param hasFocus True if the window containing this view now has focus,
4597 * false otherwise.
4598 */
4599 public void dispatchWindowFocusChanged(boolean hasFocus) {
4600 onWindowFocusChanged(hasFocus);
4601 }
4602
4603 /**
4604 * Called when the window containing this view gains or loses focus. Note
4605 * that this is separate from view focus: to receive key events, both
4606 * your view and its window must have focus. If a window is displayed
4607 * on top of yours that takes input focus, then your own window will lose
4608 * focus but the view focus will remain unchanged.
4609 *
4610 * @param hasWindowFocus True if the window containing this view now has
4611 * focus, false otherwise.
4612 */
4613 public void onWindowFocusChanged(boolean hasWindowFocus) {
4614 InputMethodManager imm = InputMethodManager.peekInstance();
4615 if (!hasWindowFocus) {
4616 if (isPressed()) {
4617 setPressed(false);
4618 }
4619 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4620 imm.focusOut(this);
4621 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004622 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004623 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004624 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004625 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4626 imm.focusIn(this);
4627 }
4628 refreshDrawableState();
4629 }
4630
4631 /**
4632 * Returns true if this view is in a window that currently has window focus.
4633 * Note that this is not the same as the view itself having focus.
4634 *
4635 * @return True if this view is in a window that currently has window focus.
4636 */
4637 public boolean hasWindowFocus() {
4638 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4639 }
4640
4641 /**
Adam Powell326d8082009-12-09 15:10:07 -08004642 * Dispatch a view visibility change down the view hierarchy.
4643 * ViewGroups should override to route to their children.
4644 * @param changedView The view whose visibility changed. Could be 'this' or
4645 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004646 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4647 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004648 */
4649 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4650 onVisibilityChanged(changedView, visibility);
4651 }
4652
4653 /**
4654 * Called when the visibility of the view or an ancestor of the view is changed.
4655 * @param changedView The view whose visibility changed. Could be 'this' or
4656 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004657 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4658 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004659 */
4660 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004661 if (visibility == VISIBLE) {
4662 if (mAttachInfo != null) {
4663 initialAwakenScrollBars();
4664 } else {
4665 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4666 }
4667 }
Adam Powell326d8082009-12-09 15:10:07 -08004668 }
4669
4670 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004671 * Dispatch a hint about whether this view is displayed. For instance, when
4672 * a View moves out of the screen, it might receives a display hint indicating
4673 * the view is not displayed. Applications should not <em>rely</em> on this hint
4674 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004675 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004676 * @param hint A hint about whether or not this view is displayed:
4677 * {@link #VISIBLE} or {@link #INVISIBLE}.
4678 */
4679 public void dispatchDisplayHint(int hint) {
4680 onDisplayHint(hint);
4681 }
4682
4683 /**
4684 * Gives this view a hint about whether is displayed or not. For instance, when
4685 * a View moves out of the screen, it might receives a display hint indicating
4686 * the view is not displayed. Applications should not <em>rely</em> on this hint
4687 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004688 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004689 * @param hint A hint about whether or not this view is displayed:
4690 * {@link #VISIBLE} or {@link #INVISIBLE}.
4691 */
4692 protected void onDisplayHint(int hint) {
4693 }
4694
4695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 * Dispatch a window visibility change down the view hierarchy.
4697 * ViewGroups should override to route to their children.
4698 *
4699 * @param visibility The new visibility of the window.
4700 *
4701 * @see #onWindowVisibilityChanged
4702 */
4703 public void dispatchWindowVisibilityChanged(int visibility) {
4704 onWindowVisibilityChanged(visibility);
4705 }
4706
4707 /**
4708 * Called when the window containing has change its visibility
4709 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4710 * that this tells you whether or not your window is being made visible
4711 * to the window manager; this does <em>not</em> tell you whether or not
4712 * your window is obscured by other windows on the screen, even if it
4713 * is itself visible.
4714 *
4715 * @param visibility The new visibility of the window.
4716 */
4717 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004718 if (visibility == VISIBLE) {
4719 initialAwakenScrollBars();
4720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721 }
4722
4723 /**
4724 * Returns the current visibility of the window this view is attached to
4725 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4726 *
4727 * @return Returns the current visibility of the view's window.
4728 */
4729 public int getWindowVisibility() {
4730 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4731 }
4732
4733 /**
4734 * Retrieve the overall visible display size in which the window this view is
4735 * attached to has been positioned in. This takes into account screen
4736 * decorations above the window, for both cases where the window itself
4737 * is being position inside of them or the window is being placed under
4738 * then and covered insets are used for the window to position its content
4739 * inside. In effect, this tells you the available area where content can
4740 * be placed and remain visible to users.
4741 *
4742 * <p>This function requires an IPC back to the window manager to retrieve
4743 * the requested information, so should not be used in performance critical
4744 * code like drawing.
4745 *
4746 * @param outRect Filled in with the visible display frame. If the view
4747 * is not attached to a window, this is simply the raw display size.
4748 */
4749 public void getWindowVisibleDisplayFrame(Rect outRect) {
4750 if (mAttachInfo != null) {
4751 try {
4752 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4753 } catch (RemoteException e) {
4754 return;
4755 }
4756 // XXX This is really broken, and probably all needs to be done
4757 // in the window manager, and we need to know more about whether
4758 // we want the area behind or in front of the IME.
4759 final Rect insets = mAttachInfo.mVisibleInsets;
4760 outRect.left += insets.left;
4761 outRect.top += insets.top;
4762 outRect.right -= insets.right;
4763 outRect.bottom -= insets.bottom;
4764 return;
4765 }
4766 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4767 outRect.set(0, 0, d.getWidth(), d.getHeight());
4768 }
4769
4770 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004771 * Dispatch a notification about a resource configuration change down
4772 * the view hierarchy.
4773 * ViewGroups should override to route to their children.
4774 *
4775 * @param newConfig The new resource configuration.
4776 *
4777 * @see #onConfigurationChanged
4778 */
4779 public void dispatchConfigurationChanged(Configuration newConfig) {
4780 onConfigurationChanged(newConfig);
4781 }
4782
4783 /**
4784 * Called when the current configuration of the resources being used
4785 * by the application have changed. You can use this to decide when
4786 * to reload resources that can changed based on orientation and other
4787 * configuration characterstics. You only need to use this if you are
4788 * not relying on the normal {@link android.app.Activity} mechanism of
4789 * recreating the activity instance upon a configuration change.
4790 *
4791 * @param newConfig The new resource configuration.
4792 */
4793 protected void onConfigurationChanged(Configuration newConfig) {
4794 }
4795
4796 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004797 * Private function to aggregate all per-view attributes in to the view
4798 * root.
4799 */
4800 void dispatchCollectViewAttributes(int visibility) {
4801 performCollectViewAttributes(visibility);
4802 }
4803
4804 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08004805 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004806 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
4807 mAttachInfo.mKeepScreenOn = true;
4808 }
4809 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
4810 if (mOnSystemUiVisibilityChangeListener != null) {
4811 mAttachInfo.mHasSystemUiListeners = true;
4812 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 }
4814 }
4815
4816 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08004817 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004818 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08004819 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
4820 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004821 ai.mRecomputeGlobalAttributes = true;
4822 }
4823 }
4824 }
4825
4826 /**
4827 * Returns whether the device is currently in touch mode. Touch mode is entered
4828 * once the user begins interacting with the device by touch, and affects various
4829 * things like whether focus is always visible to the user.
4830 *
4831 * @return Whether the device is in touch mode.
4832 */
4833 @ViewDebug.ExportedProperty
4834 public boolean isInTouchMode() {
4835 if (mAttachInfo != null) {
4836 return mAttachInfo.mInTouchMode;
4837 } else {
4838 return ViewRoot.isInTouchMode();
4839 }
4840 }
4841
4842 /**
4843 * Returns the context the view is running in, through which it can
4844 * access the current theme, resources, etc.
4845 *
4846 * @return The view's Context.
4847 */
4848 @ViewDebug.CapturedViewProperty
4849 public final Context getContext() {
4850 return mContext;
4851 }
4852
4853 /**
4854 * Handle a key event before it is processed by any input method
4855 * associated with the view hierarchy. This can be used to intercept
4856 * key events in special situations before the IME consumes them; a
4857 * typical example would be handling the BACK key to update the application's
4858 * UI instead of allowing the IME to see it and close itself.
4859 *
4860 * @param keyCode The value in event.getKeyCode().
4861 * @param event Description of the key event.
4862 * @return If you handled the event, return true. If you want to allow the
4863 * event to be handled by the next receiver, return false.
4864 */
4865 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
4866 return false;
4867 }
4868
4869 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004870 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
4871 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
4873 * is released, if the view is enabled and clickable.
4874 *
4875 * @param keyCode A key code that represents the button pressed, from
4876 * {@link android.view.KeyEvent}.
4877 * @param event The KeyEvent object that defines the button action.
4878 */
4879 public boolean onKeyDown(int keyCode, KeyEvent event) {
4880 boolean result = false;
4881
4882 switch (keyCode) {
4883 case KeyEvent.KEYCODE_DPAD_CENTER:
4884 case KeyEvent.KEYCODE_ENTER: {
4885 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4886 return true;
4887 }
4888 // Long clickable items don't necessarily have to be clickable
4889 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
4890 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
4891 (event.getRepeatCount() == 0)) {
4892 setPressed(true);
4893 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08004894 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 }
4896 return true;
4897 }
4898 break;
4899 }
4900 }
4901 return result;
4902 }
4903
4904 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004905 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
4906 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
4907 * the event).
4908 */
4909 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
4910 return false;
4911 }
4912
4913 /**
Jeff Brown995e7742010-12-22 16:59:36 -08004914 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
4915 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
4917 * {@link KeyEvent#KEYCODE_ENTER} is released.
4918 *
4919 * @param keyCode A key code that represents the button pressed, from
4920 * {@link android.view.KeyEvent}.
4921 * @param event The KeyEvent object that defines the button action.
4922 */
4923 public boolean onKeyUp(int keyCode, KeyEvent event) {
4924 boolean result = false;
4925
4926 switch (keyCode) {
4927 case KeyEvent.KEYCODE_DPAD_CENTER:
4928 case KeyEvent.KEYCODE_ENTER: {
4929 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4930 return true;
4931 }
4932 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
4933 setPressed(false);
4934
4935 if (!mHasPerformedLongPress) {
4936 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05004937 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004938
4939 result = performClick();
4940 }
4941 }
4942 break;
4943 }
4944 }
4945 return result;
4946 }
4947
4948 /**
4949 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
4950 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
4951 * the event).
4952 *
4953 * @param keyCode A key code that represents the button pressed, from
4954 * {@link android.view.KeyEvent}.
4955 * @param repeatCount The number of times the action was made.
4956 * @param event The KeyEvent object that defines the button action.
4957 */
4958 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
4959 return false;
4960 }
4961
4962 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08004963 * Called on the focused view when a key shortcut event is not handled.
4964 * Override this method to implement local key shortcuts for the View.
4965 * Key shortcuts can also be implemented by setting the
4966 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004967 *
4968 * @param keyCode The value in event.getKeyCode().
4969 * @param event Description of the key event.
4970 * @return If you handled the event, return true. If you want to allow the
4971 * event to be handled by the next receiver, return false.
4972 */
4973 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
4974 return false;
4975 }
4976
4977 /**
4978 * Check whether the called view is a text editor, in which case it
4979 * would make sense to automatically display a soft input window for
4980 * it. Subclasses should override this if they implement
4981 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004982 * a call on that method would return a non-null InputConnection, and
4983 * they are really a first-class editor that the user would normally
4984 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07004985 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07004986 * <p>The default implementation always returns false. This does
4987 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
4988 * will not be called or the user can not otherwise perform edits on your
4989 * view; it is just a hint to the system that this is not the primary
4990 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07004991 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004992 * @return Returns true if this view is a text editor, else false.
4993 */
4994 public boolean onCheckIsTextEditor() {
4995 return false;
4996 }
Romain Guy8506ab42009-06-11 17:35:47 -07004997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004998 /**
4999 * Create a new InputConnection for an InputMethod to interact
5000 * with the view. The default implementation returns null, since it doesn't
5001 * support input methods. You can override this to implement such support.
5002 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005003 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005004 * <p>When implementing this, you probably also want to implement
5005 * {@link #onCheckIsTextEditor()} to indicate you will return a
5006 * non-null InputConnection.
5007 *
5008 * @param outAttrs Fill in with attribute information about the connection.
5009 */
5010 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5011 return null;
5012 }
5013
5014 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005015 * Called by the {@link android.view.inputmethod.InputMethodManager}
5016 * when a view who is not the current
5017 * input connection target is trying to make a call on the manager. The
5018 * default implementation returns false; you can override this to return
5019 * true for certain views if you are performing InputConnection proxying
5020 * to them.
5021 * @param view The View that is making the InputMethodManager call.
5022 * @return Return true to allow the call, false to reject.
5023 */
5024 public boolean checkInputConnectionProxy(View view) {
5025 return false;
5026 }
Romain Guy8506ab42009-06-11 17:35:47 -07005027
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 * Show the context menu for this view. It is not safe to hold on to the
5030 * menu after returning from this method.
5031 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005032 * You should normally not overload this method. Overload
5033 * {@link #onCreateContextMenu(ContextMenu)} or define an
5034 * {@link OnCreateContextMenuListener} to add items to the context menu.
5035 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005036 * @param menu The context menu to populate
5037 */
5038 public void createContextMenu(ContextMenu menu) {
5039 ContextMenuInfo menuInfo = getContextMenuInfo();
5040
5041 // Sets the current menu info so all items added to menu will have
5042 // my extra info set.
5043 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5044
5045 onCreateContextMenu(menu);
5046 if (mOnCreateContextMenuListener != null) {
5047 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5048 }
5049
5050 // Clear the extra information so subsequent items that aren't mine don't
5051 // have my extra info.
5052 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5053
5054 if (mParent != null) {
5055 mParent.createContextMenu(menu);
5056 }
5057 }
5058
5059 /**
5060 * Views should implement this if they have extra information to associate
5061 * with the context menu. The return result is supplied as a parameter to
5062 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5063 * callback.
5064 *
5065 * @return Extra information about the item for which the context menu
5066 * should be shown. This information will vary across different
5067 * subclasses of View.
5068 */
5069 protected ContextMenuInfo getContextMenuInfo() {
5070 return null;
5071 }
5072
5073 /**
5074 * Views should implement this if the view itself is going to add items to
5075 * the context menu.
5076 *
5077 * @param menu the context menu to populate
5078 */
5079 protected void onCreateContextMenu(ContextMenu menu) {
5080 }
5081
5082 /**
5083 * Implement this method to handle trackball motion events. The
5084 * <em>relative</em> movement of the trackball since the last event
5085 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5086 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5087 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5088 * they will often be fractional values, representing the more fine-grained
5089 * movement information available from a trackball).
5090 *
5091 * @param event The motion event.
5092 * @return True if the event was handled, false otherwise.
5093 */
5094 public boolean onTrackballEvent(MotionEvent event) {
5095 return false;
5096 }
5097
5098 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005099 * Implement this method to handle generic motion events.
5100 * <p>
5101 * Generic motion events are dispatched to the focused view to describe
5102 * the motions of input devices such as joysticks. The
5103 * {@link MotionEvent#getSource() source} of the motion event specifies
5104 * the class of input that was received. Implementations of this method
5105 * must examine the bits in the source before processing the event.
5106 * The following code example shows how this is done.
5107 * </p>
5108 * <code>
5109 * public boolean onGenericMotionEvent(MotionEvent event) {
5110 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
5111 * float x = event.getX();
5112 * float y = event.getY();
5113 * // process the joystick motion
5114 * return true;
5115 * }
5116 * return super.onGenericMotionEvent(event);
5117 * }
5118 * </code>
5119 *
5120 * @param event The generic motion event being processed.
5121 *
5122 * @return Return true if you have consumed the event, false if you haven't.
5123 * The default implementation always returns false.
5124 */
5125 public boolean onGenericMotionEvent(MotionEvent event) {
5126 return false;
5127 }
5128
5129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005130 * Implement this method to handle touch screen motion events.
5131 *
5132 * @param event The motion event.
5133 * @return True if the event was handled, false otherwise.
5134 */
5135 public boolean onTouchEvent(MotionEvent event) {
5136 final int viewFlags = mViewFlags;
5137
5138 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5139 // A disabled view that is clickable still consumes the touch
5140 // events, it just doesn't respond to them.
5141 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5142 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5143 }
5144
5145 if (mTouchDelegate != null) {
5146 if (mTouchDelegate.onTouchEvent(event)) {
5147 return true;
5148 }
5149 }
5150
5151 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5152 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5153 switch (event.getAction()) {
5154 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005155 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5156 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005157 // take focus if we don't have it already and we should in
5158 // touch mode.
5159 boolean focusTaken = false;
5160 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5161 focusTaken = requestFocus();
5162 }
5163
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005164 if (prepressed) {
5165 // The button is being released before we actually
5166 // showed it as pressed. Make it show the pressed
5167 // state now (before scheduling the click) to ensure
5168 // the user sees it.
5169 mPrivateFlags |= PRESSED;
5170 refreshDrawableState();
5171 }
Joe Malin32736f02011-01-19 16:14:20 -08005172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 if (!mHasPerformedLongPress) {
5174 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005175 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005176
5177 // Only perform take click actions if we were in the pressed state
5178 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005179 // Use a Runnable and post this rather than calling
5180 // performClick directly. This lets other visual state
5181 // of the view update before click actions start.
5182 if (mPerformClick == null) {
5183 mPerformClick = new PerformClick();
5184 }
5185 if (!post(mPerformClick)) {
5186 performClick();
5187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005188 }
5189 }
5190
5191 if (mUnsetPressedState == null) {
5192 mUnsetPressedState = new UnsetPressedState();
5193 }
5194
Adam Powelle14579b2009-12-16 18:39:52 -08005195 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005196 postDelayed(mUnsetPressedState,
5197 ViewConfiguration.getPressedStateDuration());
5198 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 // If the post failed, unpress right now
5200 mUnsetPressedState.run();
5201 }
Adam Powelle14579b2009-12-16 18:39:52 -08005202 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005203 }
5204 break;
5205
5206 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005207 if (mPendingCheckForTap == null) {
5208 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005209 }
Adam Powelle14579b2009-12-16 18:39:52 -08005210 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005211 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005212 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 break;
5214
5215 case MotionEvent.ACTION_CANCEL:
5216 mPrivateFlags &= ~PRESSED;
5217 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005218 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 break;
5220
5221 case MotionEvent.ACTION_MOVE:
5222 final int x = (int) event.getX();
5223 final int y = (int) event.getY();
5224
5225 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005226 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005227 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005228 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005230 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005231 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232
5233 // Need to switch from pressed to not pressed
5234 mPrivateFlags &= ~PRESSED;
5235 refreshDrawableState();
5236 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005237 }
5238 break;
5239 }
5240 return true;
5241 }
5242
5243 return false;
5244 }
5245
5246 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005247 * Remove the longpress detection timer.
5248 */
5249 private void removeLongPressCallback() {
5250 if (mPendingCheckForLongPress != null) {
5251 removeCallbacks(mPendingCheckForLongPress);
5252 }
5253 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005254
5255 /**
5256 * Remove the pending click action
5257 */
5258 private void removePerformClickCallback() {
5259 if (mPerformClick != null) {
5260 removeCallbacks(mPerformClick);
5261 }
5262 }
5263
Adam Powelle14579b2009-12-16 18:39:52 -08005264 /**
Romain Guya440b002010-02-24 15:57:54 -08005265 * Remove the prepress detection timer.
5266 */
5267 private void removeUnsetPressCallback() {
5268 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5269 setPressed(false);
5270 removeCallbacks(mUnsetPressedState);
5271 }
5272 }
5273
5274 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005275 * Remove the tap detection timer.
5276 */
5277 private void removeTapCallback() {
5278 if (mPendingCheckForTap != null) {
5279 mPrivateFlags &= ~PREPRESSED;
5280 removeCallbacks(mPendingCheckForTap);
5281 }
5282 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005283
5284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005285 * Cancels a pending long press. Your subclass can use this if you
5286 * want the context menu to come up if the user presses and holds
5287 * at the same place, but you don't want it to come up if they press
5288 * and then move around enough to cause scrolling.
5289 */
5290 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005291 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005292
5293 /*
5294 * The prepressed state handled by the tap callback is a display
5295 * construct, but the tap callback will post a long press callback
5296 * less its own timeout. Remove it here.
5297 */
5298 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005299 }
5300
5301 /**
5302 * Sets the TouchDelegate for this View.
5303 */
5304 public void setTouchDelegate(TouchDelegate delegate) {
5305 mTouchDelegate = delegate;
5306 }
5307
5308 /**
5309 * Gets the TouchDelegate for this View.
5310 */
5311 public TouchDelegate getTouchDelegate() {
5312 return mTouchDelegate;
5313 }
5314
5315 /**
5316 * Set flags controlling behavior of this view.
5317 *
5318 * @param flags Constant indicating the value which should be set
5319 * @param mask Constant indicating the bit range that should be changed
5320 */
5321 void setFlags(int flags, int mask) {
5322 int old = mViewFlags;
5323 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5324
5325 int changed = mViewFlags ^ old;
5326 if (changed == 0) {
5327 return;
5328 }
5329 int privateFlags = mPrivateFlags;
5330
5331 /* Check if the FOCUSABLE bit has changed */
5332 if (((changed & FOCUSABLE_MASK) != 0) &&
5333 ((privateFlags & HAS_BOUNDS) !=0)) {
5334 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5335 && ((privateFlags & FOCUSED) != 0)) {
5336 /* Give up focus if we are no longer focusable */
5337 clearFocus();
5338 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5339 && ((privateFlags & FOCUSED) == 0)) {
5340 /*
5341 * Tell the view system that we are now available to take focus
5342 * if no one else already has it.
5343 */
5344 if (mParent != null) mParent.focusableViewAvailable(this);
5345 }
5346 }
5347
5348 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5349 if ((changed & VISIBILITY_MASK) != 0) {
5350 /*
5351 * If this view is becoming visible, set the DRAWN flag so that
5352 * the next invalidate() will not be skipped.
5353 */
5354 mPrivateFlags |= DRAWN;
5355
5356 needGlobalAttributesUpdate(true);
5357
5358 // a view becoming visible is worth notifying the parent
5359 // about in case nothing has focus. even if this specific view
5360 // isn't focusable, it may contain something that is, so let
5361 // the root view try to give this focus if nothing else does.
5362 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5363 mParent.focusableViewAvailable(this);
5364 }
5365 }
5366 }
5367
5368 /* Check if the GONE bit has changed */
5369 if ((changed & GONE) != 0) {
5370 needGlobalAttributesUpdate(false);
5371 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005372 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005373
Romain Guyecd80ee2009-12-03 17:13:02 -08005374 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5375 if (hasFocus()) clearFocus();
5376 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005377 }
5378 if (mAttachInfo != null) {
5379 mAttachInfo.mViewVisibilityChanged = true;
5380 }
5381 }
5382
5383 /* Check if the VISIBLE bit has changed */
5384 if ((changed & INVISIBLE) != 0) {
5385 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005386 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005387
5388 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5389 // root view becoming invisible shouldn't clear focus
5390 if (getRootView() != this) {
5391 clearFocus();
5392 }
5393 }
5394 if (mAttachInfo != null) {
5395 mAttachInfo.mViewVisibilityChanged = true;
5396 }
5397 }
5398
Adam Powell326d8082009-12-09 15:10:07 -08005399 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005400 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005401 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5402 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005403 }
Adam Powell326d8082009-12-09 15:10:07 -08005404 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5405 }
5406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005407 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5408 destroyDrawingCache();
5409 }
5410
5411 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5412 destroyDrawingCache();
5413 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005414 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005415 }
5416
5417 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5418 destroyDrawingCache();
5419 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5420 }
5421
5422 if ((changed & DRAW_MASK) != 0) {
5423 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5424 if (mBGDrawable != null) {
5425 mPrivateFlags &= ~SKIP_DRAW;
5426 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5427 } else {
5428 mPrivateFlags |= SKIP_DRAW;
5429 }
5430 } else {
5431 mPrivateFlags &= ~SKIP_DRAW;
5432 }
5433 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005434 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005435 }
5436
5437 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005438 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005439 mParent.recomputeViewAttributes(this);
5440 }
5441 }
5442 }
5443
5444 /**
5445 * Change the view's z order in the tree, so it's on top of other sibling
5446 * views
5447 */
5448 public void bringToFront() {
5449 if (mParent != null) {
5450 mParent.bringChildToFront(this);
5451 }
5452 }
5453
5454 /**
5455 * This is called in response to an internal scroll in this view (i.e., the
5456 * view scrolled its own contents). This is typically as a result of
5457 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5458 * called.
5459 *
5460 * @param l Current horizontal scroll origin.
5461 * @param t Current vertical scroll origin.
5462 * @param oldl Previous horizontal scroll origin.
5463 * @param oldt Previous vertical scroll origin.
5464 */
5465 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5466 mBackgroundSizeChanged = true;
5467
5468 final AttachInfo ai = mAttachInfo;
5469 if (ai != null) {
5470 ai.mViewScrollChanged = true;
5471 }
5472 }
5473
5474 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005475 * Interface definition for a callback to be invoked when the layout bounds of a view
5476 * changes due to layout processing.
5477 */
5478 public interface OnLayoutChangeListener {
5479 /**
5480 * Called when the focus state of a view has changed.
5481 *
5482 * @param v The view whose state has changed.
5483 * @param left The new value of the view's left property.
5484 * @param top The new value of the view's top property.
5485 * @param right The new value of the view's right property.
5486 * @param bottom The new value of the view's bottom property.
5487 * @param oldLeft The previous value of the view's left property.
5488 * @param oldTop The previous value of the view's top property.
5489 * @param oldRight The previous value of the view's right property.
5490 * @param oldBottom The previous value of the view's bottom property.
5491 */
5492 void onLayoutChange(View v, int left, int top, int right, int bottom,
5493 int oldLeft, int oldTop, int oldRight, int oldBottom);
5494 }
5495
5496 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005497 * This is called during layout when the size of this view has changed. If
5498 * you were just added to the view hierarchy, you're called with the old
5499 * values of 0.
5500 *
5501 * @param w Current width of this view.
5502 * @param h Current height of this view.
5503 * @param oldw Old width of this view.
5504 * @param oldh Old height of this view.
5505 */
5506 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5507 }
5508
5509 /**
5510 * Called by draw to draw the child views. This may be overridden
5511 * by derived classes to gain control just before its children are drawn
5512 * (but after its own view has been drawn).
5513 * @param canvas the canvas on which to draw the view
5514 */
5515 protected void dispatchDraw(Canvas canvas) {
5516 }
5517
5518 /**
5519 * Gets the parent of this view. Note that the parent is a
5520 * ViewParent and not necessarily a View.
5521 *
5522 * @return Parent of this view.
5523 */
5524 public final ViewParent getParent() {
5525 return mParent;
5526 }
5527
5528 /**
5529 * Return the scrolled left position of this view. This is the left edge of
5530 * the displayed part of your view. You do not need to draw any pixels
5531 * farther left, since those are outside of the frame of your view on
5532 * screen.
5533 *
5534 * @return The left edge of the displayed part of your view, in pixels.
5535 */
5536 public final int getScrollX() {
5537 return mScrollX;
5538 }
5539
5540 /**
5541 * Return the scrolled top position of this view. This is the top edge of
5542 * the displayed part of your view. You do not need to draw any pixels above
5543 * it, since those are outside of the frame of your view on screen.
5544 *
5545 * @return The top edge of the displayed part of your view, in pixels.
5546 */
5547 public final int getScrollY() {
5548 return mScrollY;
5549 }
5550
5551 /**
5552 * Return the width of the your view.
5553 *
5554 * @return The width of your view, in pixels.
5555 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005556 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005557 public final int getWidth() {
5558 return mRight - mLeft;
5559 }
5560
5561 /**
5562 * Return the height of your view.
5563 *
5564 * @return The height of your view, in pixels.
5565 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005566 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005567 public final int getHeight() {
5568 return mBottom - mTop;
5569 }
5570
5571 /**
5572 * Return the visible drawing bounds of your view. Fills in the output
5573 * rectangle with the values from getScrollX(), getScrollY(),
5574 * getWidth(), and getHeight().
5575 *
5576 * @param outRect The (scrolled) drawing bounds of the view.
5577 */
5578 public void getDrawingRect(Rect outRect) {
5579 outRect.left = mScrollX;
5580 outRect.top = mScrollY;
5581 outRect.right = mScrollX + (mRight - mLeft);
5582 outRect.bottom = mScrollY + (mBottom - mTop);
5583 }
5584
5585 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005586 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5587 * raw width component (that is the result is masked by
5588 * {@link #MEASURED_SIZE_MASK}).
5589 *
5590 * @return The raw measured width of this view.
5591 */
5592 public final int getMeasuredWidth() {
5593 return mMeasuredWidth & MEASURED_SIZE_MASK;
5594 }
5595
5596 /**
5597 * Return the full width measurement information for this view as computed
5598 * by the most recent call to {@link #measure}. This result is a bit mask
5599 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 * This should be used during measurement and layout calculations only. Use
5601 * {@link #getWidth()} to see how wide a view is after layout.
5602 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005603 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005605 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005606 return mMeasuredWidth;
5607 }
5608
5609 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005610 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5611 * raw width component (that is the result is masked by
5612 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005613 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005614 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005615 */
5616 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005617 return mMeasuredHeight & MEASURED_SIZE_MASK;
5618 }
5619
5620 /**
5621 * Return the full height measurement information for this view as computed
5622 * by the most recent call to {@link #measure}. This result is a bit mask
5623 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5624 * This should be used during measurement and layout calculations only. Use
5625 * {@link #getHeight()} to see how wide a view is after layout.
5626 *
5627 * @return The measured width of this view as a bit mask.
5628 */
5629 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005630 return mMeasuredHeight;
5631 }
5632
5633 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005634 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5635 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5636 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5637 * and the height component is at the shifted bits
5638 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5639 */
5640 public final int getMeasuredState() {
5641 return (mMeasuredWidth&MEASURED_STATE_MASK)
5642 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5643 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5644 }
5645
5646 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005647 * The transform matrix of this view, which is calculated based on the current
5648 * roation, scale, and pivot properties.
5649 *
5650 * @see #getRotation()
5651 * @see #getScaleX()
5652 * @see #getScaleY()
5653 * @see #getPivotX()
5654 * @see #getPivotY()
5655 * @return The current transform matrix for the view
5656 */
5657 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005658 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005659 return mMatrix;
5660 }
5661
5662 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005663 * Utility function to determine if the value is far enough away from zero to be
5664 * considered non-zero.
5665 * @param value A floating point value to check for zero-ness
5666 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5667 */
5668 private static boolean nonzero(float value) {
5669 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5670 }
5671
5672 /**
Jeff Brown86671742010-09-30 20:00:15 -07005673 * Returns true if the transform matrix is the identity matrix.
5674 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005675 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005676 * @return True if the transform matrix is the identity matrix, false otherwise.
5677 */
Jeff Brown86671742010-09-30 20:00:15 -07005678 final boolean hasIdentityMatrix() {
5679 updateMatrix();
5680 return mMatrixIsIdentity;
5681 }
5682
5683 /**
5684 * Recomputes the transform matrix if necessary.
5685 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005686 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005687 if (mMatrixDirty) {
5688 // transform-related properties have changed since the last time someone
5689 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005690
5691 // Figure out if we need to update the pivot point
5692 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005693 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005694 mPrevWidth = mRight - mLeft;
5695 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005696 mPivotX = mPrevWidth / 2f;
5697 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005698 }
5699 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005700 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005701 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5702 mMatrix.setTranslate(mTranslationX, mTranslationY);
5703 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5704 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
5705 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07005706 if (mCamera == null) {
5707 mCamera = new Camera();
5708 matrix3D = new Matrix();
5709 }
5710 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07005711 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005712 mCamera.rotateX(mRotationX);
5713 mCamera.rotateY(mRotationY);
Chet Haase897247b2010-09-09 14:54:47 -07005714 mCamera.rotateZ(-mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07005715 mCamera.getMatrix(matrix3D);
5716 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07005717 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07005718 mMatrix.postConcat(matrix3D);
5719 mCamera.restore();
5720 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005721 mMatrixDirty = false;
5722 mMatrixIsIdentity = mMatrix.isIdentity();
5723 mInverseMatrixDirty = true;
5724 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005725 }
5726
5727 /**
5728 * Utility method to retrieve the inverse of the current mMatrix property.
5729 * We cache the matrix to avoid recalculating it when transform properties
5730 * have not changed.
5731 *
5732 * @return The inverse of the current matrix of this view.
5733 */
Jeff Brown86671742010-09-30 20:00:15 -07005734 final Matrix getInverseMatrix() {
5735 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07005736 if (mInverseMatrixDirty) {
5737 if (mInverseMatrix == null) {
5738 mInverseMatrix = new Matrix();
5739 }
5740 mMatrix.invert(mInverseMatrix);
5741 mInverseMatrixDirty = false;
5742 }
5743 return mInverseMatrix;
5744 }
5745
5746 /**
5747 * The degrees that the view is rotated around the pivot point.
5748 *
5749 * @see #getPivotX()
5750 * @see #getPivotY()
5751 * @return The degrees of rotation.
5752 */
5753 public float getRotation() {
5754 return mRotation;
5755 }
5756
5757 /**
Chet Haase897247b2010-09-09 14:54:47 -07005758 * Sets the degrees that the view is rotated around the pivot point. Increasing values
5759 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07005760 *
5761 * @param rotation The degrees of rotation.
5762 * @see #getPivotX()
5763 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005764 *
5765 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07005766 */
5767 public void setRotation(float rotation) {
5768 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005769 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005770 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005771 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005772 mRotation = rotation;
5773 mMatrixDirty = true;
5774 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005775 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005776 }
5777 }
5778
5779 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005780 * The degrees that the view is rotated around the vertical axis through the pivot point.
5781 *
5782 * @see #getPivotX()
5783 * @see #getPivotY()
5784 * @return The degrees of Y rotation.
5785 */
5786 public float getRotationY() {
5787 return mRotationY;
5788 }
5789
5790 /**
Chet Haase897247b2010-09-09 14:54:47 -07005791 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
5792 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
5793 * down the y axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005794 *
5795 * @param rotationY The degrees of Y rotation.
5796 * @see #getPivotX()
5797 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005798 *
5799 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07005800 */
5801 public void setRotationY(float rotationY) {
5802 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005803 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005804 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005805 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005806 mRotationY = rotationY;
5807 mMatrixDirty = true;
5808 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005809 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005810 }
5811 }
5812
5813 /**
5814 * The degrees that the view is rotated around the horizontal axis through the pivot point.
5815 *
5816 * @see #getPivotX()
5817 * @see #getPivotY()
5818 * @return The degrees of X rotation.
5819 */
5820 public float getRotationX() {
5821 return mRotationX;
5822 }
5823
5824 /**
Chet Haase897247b2010-09-09 14:54:47 -07005825 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
5826 * Increasing values result in clockwise rotation from the viewpoint of looking down the
5827 * x axis.
Chet Haasefd2b0022010-08-06 13:08:56 -07005828 *
5829 * @param rotationX The degrees of X rotation.
5830 * @see #getPivotX()
5831 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005832 *
5833 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07005834 */
5835 public void setRotationX(float rotationX) {
5836 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005837 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07005838 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005839 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005840 mRotationX = rotationX;
5841 mMatrixDirty = true;
5842 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005843 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07005844 }
5845 }
5846
5847 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005848 * The amount that the view is scaled in x around the pivot point, as a proportion of
5849 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
5850 *
Joe Onorato93162322010-09-16 15:42:01 -04005851 * <p>By default, this is 1.0f.
5852 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005853 * @see #getPivotX()
5854 * @see #getPivotY()
5855 * @return The scaling factor.
5856 */
5857 public float getScaleX() {
5858 return mScaleX;
5859 }
5860
5861 /**
5862 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
5863 * the view's unscaled width. A value of 1 means that no scaling is applied.
5864 *
5865 * @param scaleX The scaling factor.
5866 * @see #getPivotX()
5867 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005868 *
5869 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07005870 */
5871 public void setScaleX(float scaleX) {
5872 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005873 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005874 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005875 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005876 mScaleX = scaleX;
5877 mMatrixDirty = true;
5878 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005879 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005880 }
5881 }
5882
5883 /**
5884 * The amount that the view is scaled in y around the pivot point, as a proportion of
5885 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
5886 *
Joe Onorato93162322010-09-16 15:42:01 -04005887 * <p>By default, this is 1.0f.
5888 *
Chet Haasec3aa3612010-06-17 08:50:37 -07005889 * @see #getPivotX()
5890 * @see #getPivotY()
5891 * @return The scaling factor.
5892 */
5893 public float getScaleY() {
5894 return mScaleY;
5895 }
5896
5897 /**
5898 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
5899 * the view's unscaled width. A value of 1 means that no scaling is applied.
5900 *
5901 * @param scaleY The scaling factor.
5902 * @see #getPivotX()
5903 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005904 *
5905 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07005906 */
5907 public void setScaleY(float scaleY) {
5908 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005909 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005910 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005911 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005912 mScaleY = scaleY;
5913 mMatrixDirty = true;
5914 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005915 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005916 }
5917 }
5918
5919 /**
5920 * The x location of the point around which the view is {@link #setRotation(float) rotated}
5921 * and {@link #setScaleX(float) scaled}.
5922 *
5923 * @see #getRotation()
5924 * @see #getScaleX()
5925 * @see #getScaleY()
5926 * @see #getPivotY()
5927 * @return The x location of the pivot point.
5928 */
5929 public float getPivotX() {
5930 return mPivotX;
5931 }
5932
5933 /**
5934 * Sets the x location of the point around which the view is
5935 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07005936 * By default, the pivot point is centered on the object.
5937 * Setting this property disables this behavior and causes the view to use only the
5938 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07005939 *
5940 * @param pivotX The x location of the pivot point.
5941 * @see #getRotation()
5942 * @see #getScaleX()
5943 * @see #getScaleY()
5944 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005945 *
5946 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07005947 */
5948 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005949 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07005950 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005951 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005952 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005953 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005954 mPivotX = pivotX;
5955 mMatrixDirty = true;
5956 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005957 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005958 }
5959 }
5960
5961 /**
5962 * The y location of the point around which the view is {@link #setRotation(float) rotated}
5963 * and {@link #setScaleY(float) scaled}.
5964 *
5965 * @see #getRotation()
5966 * @see #getScaleX()
5967 * @see #getScaleY()
5968 * @see #getPivotY()
5969 * @return The y location of the pivot point.
5970 */
5971 public float getPivotY() {
5972 return mPivotY;
5973 }
5974
5975 /**
5976 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07005977 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
5978 * Setting this property disables this behavior and causes the view to use only the
5979 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07005980 *
5981 * @param pivotY The y location of the pivot point.
5982 * @see #getRotation()
5983 * @see #getScaleX()
5984 * @see #getScaleY()
5985 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08005986 *
5987 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07005988 */
5989 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005990 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07005991 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005992 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07005993 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07005994 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005995 mPivotY = pivotY;
5996 mMatrixDirty = true;
5997 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07005998 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07005999 }
6000 }
6001
6002 /**
6003 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6004 * completely transparent and 1 means the view is completely opaque.
6005 *
Joe Onorato93162322010-09-16 15:42:01 -04006006 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006007 * @return The opacity of the view.
6008 */
6009 public float getAlpha() {
6010 return mAlpha;
6011 }
6012
6013 /**
Romain Guy171c5922011-01-06 10:04:23 -08006014 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6015 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006016 *
Romain Guy171c5922011-01-06 10:04:23 -08006017 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6018 * responsible for applying the opacity itself. Otherwise, calling this method is
6019 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006020 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006021 *
6022 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006023 *
Joe Malin32736f02011-01-19 16:14:20 -08006024 * @see #setLayerType(int, android.graphics.Paint)
6025 *
Chet Haase73066682010-11-29 15:55:32 -08006026 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006027 */
6028 public void setAlpha(float alpha) {
6029 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006030 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006031 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006032 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006033 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006034 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006035 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006036 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006037 invalidate(false);
6038 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006039 }
6040
6041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006042 * Top position of this view relative to its parent.
6043 *
6044 * @return The top of this view, in pixels.
6045 */
6046 @ViewDebug.CapturedViewProperty
6047 public final int getTop() {
6048 return mTop;
6049 }
6050
6051 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006052 * Sets the top position of this view relative to its parent. This method is meant to be called
6053 * by the layout system and should not generally be called otherwise, because the property
6054 * may be changed at any time by the layout.
6055 *
6056 * @param top The top of this view, in pixels.
6057 */
6058 public final void setTop(int top) {
6059 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006060 updateMatrix();
6061 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006062 final ViewParent p = mParent;
6063 if (p != null && mAttachInfo != null) {
6064 final Rect r = mAttachInfo.mTmpInvalRect;
6065 int minTop;
6066 int yLoc;
6067 if (top < mTop) {
6068 minTop = top;
6069 yLoc = top - mTop;
6070 } else {
6071 minTop = mTop;
6072 yLoc = 0;
6073 }
6074 r.set(0, yLoc, mRight - mLeft, mBottom - minTop);
6075 p.invalidateChild(this, r);
6076 }
6077 } else {
6078 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006079 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006080 }
6081
Chet Haaseed032702010-10-01 14:05:54 -07006082 int width = mRight - mLeft;
6083 int oldHeight = mBottom - mTop;
6084
Chet Haase21cd1382010-09-01 17:42:29 -07006085 mTop = top;
6086
Chet Haaseed032702010-10-01 14:05:54 -07006087 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6088
Chet Haase21cd1382010-09-01 17:42:29 -07006089 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006090 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6091 // A change in dimension means an auto-centered pivot point changes, too
6092 mMatrixDirty = true;
6093 }
Chet Haase21cd1382010-09-01 17:42:29 -07006094 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006095 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006096 }
Chet Haase55dbb652010-12-21 20:15:08 -08006097 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006098 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006099 }
6100 }
6101
6102 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006103 * Bottom position of this view relative to its parent.
6104 *
6105 * @return The bottom of this view, in pixels.
6106 */
6107 @ViewDebug.CapturedViewProperty
6108 public final int getBottom() {
6109 return mBottom;
6110 }
6111
6112 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006113 * True if this view has changed since the last time being drawn.
6114 *
6115 * @return The dirty state of this view.
6116 */
6117 public boolean isDirty() {
6118 return (mPrivateFlags & DIRTY_MASK) != 0;
6119 }
6120
6121 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006122 * Sets the bottom position of this view relative to its parent. This method is meant to be
6123 * called by the layout system and should not generally be called otherwise, because the
6124 * property may be changed at any time by the layout.
6125 *
6126 * @param bottom The bottom of this view, in pixels.
6127 */
6128 public final void setBottom(int bottom) {
6129 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006130 updateMatrix();
6131 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006132 final ViewParent p = mParent;
6133 if (p != null && mAttachInfo != null) {
6134 final Rect r = mAttachInfo.mTmpInvalRect;
6135 int maxBottom;
6136 if (bottom < mBottom) {
6137 maxBottom = mBottom;
6138 } else {
6139 maxBottom = bottom;
6140 }
6141 r.set(0, 0, mRight - mLeft, maxBottom - mTop);
6142 p.invalidateChild(this, r);
6143 }
6144 } else {
6145 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006146 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006147 }
6148
Chet Haaseed032702010-10-01 14:05:54 -07006149 int width = mRight - mLeft;
6150 int oldHeight = mBottom - mTop;
6151
Chet Haase21cd1382010-09-01 17:42:29 -07006152 mBottom = bottom;
6153
Chet Haaseed032702010-10-01 14:05:54 -07006154 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6155
Chet Haase21cd1382010-09-01 17:42:29 -07006156 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006157 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6158 // A change in dimension means an auto-centered pivot point changes, too
6159 mMatrixDirty = true;
6160 }
Chet Haase21cd1382010-09-01 17:42:29 -07006161 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006162 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006163 }
Chet Haase55dbb652010-12-21 20:15:08 -08006164 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006165 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006166 }
6167 }
6168
6169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 * Left position of this view relative to its parent.
6171 *
6172 * @return The left edge of this view, in pixels.
6173 */
6174 @ViewDebug.CapturedViewProperty
6175 public final int getLeft() {
6176 return mLeft;
6177 }
6178
6179 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006180 * Sets the left position of this view relative to its parent. This method is meant to be called
6181 * by the layout system and should not generally be called otherwise, because the property
6182 * may be changed at any time by the layout.
6183 *
6184 * @param left The bottom of this view, in pixels.
6185 */
6186 public final void setLeft(int left) {
6187 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006188 updateMatrix();
6189 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006190 final ViewParent p = mParent;
6191 if (p != null && mAttachInfo != null) {
6192 final Rect r = mAttachInfo.mTmpInvalRect;
6193 int minLeft;
6194 int xLoc;
6195 if (left < mLeft) {
6196 minLeft = left;
6197 xLoc = left - mLeft;
6198 } else {
6199 minLeft = mLeft;
6200 xLoc = 0;
6201 }
6202 r.set(xLoc, 0, mRight - minLeft, mBottom - mTop);
6203 p.invalidateChild(this, r);
6204 }
6205 } else {
6206 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006207 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006208 }
6209
Chet Haaseed032702010-10-01 14:05:54 -07006210 int oldWidth = mRight - mLeft;
6211 int height = mBottom - mTop;
6212
Chet Haase21cd1382010-09-01 17:42:29 -07006213 mLeft = left;
6214
Chet Haaseed032702010-10-01 14:05:54 -07006215 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6216
Chet Haase21cd1382010-09-01 17:42:29 -07006217 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006218 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6219 // A change in dimension means an auto-centered pivot point changes, too
6220 mMatrixDirty = true;
6221 }
Chet Haase21cd1382010-09-01 17:42:29 -07006222 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006223 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006224 }
Chet Haase55dbb652010-12-21 20:15:08 -08006225 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006226 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006227 }
6228 }
6229
6230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006231 * Right position of this view relative to its parent.
6232 *
6233 * @return The right edge of this view, in pixels.
6234 */
6235 @ViewDebug.CapturedViewProperty
6236 public final int getRight() {
6237 return mRight;
6238 }
6239
6240 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006241 * Sets the right position of this view relative to its parent. This method is meant to be called
6242 * by the layout system and should not generally be called otherwise, because the property
6243 * may be changed at any time by the layout.
6244 *
6245 * @param right The bottom of this view, in pixels.
6246 */
6247 public final void setRight(int right) {
6248 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006249 updateMatrix();
6250 if (mMatrixIsIdentity) {
Chet Haase21cd1382010-09-01 17:42:29 -07006251 final ViewParent p = mParent;
6252 if (p != null && mAttachInfo != null) {
6253 final Rect r = mAttachInfo.mTmpInvalRect;
6254 int maxRight;
6255 if (right < mRight) {
6256 maxRight = mRight;
6257 } else {
6258 maxRight = right;
6259 }
6260 r.set(0, 0, maxRight - mLeft, mBottom - mTop);
6261 p.invalidateChild(this, r);
6262 }
6263 } else {
6264 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006265 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006266 }
6267
Chet Haaseed032702010-10-01 14:05:54 -07006268 int oldWidth = mRight - mLeft;
6269 int height = mBottom - mTop;
6270
Chet Haase21cd1382010-09-01 17:42:29 -07006271 mRight = right;
6272
Chet Haaseed032702010-10-01 14:05:54 -07006273 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6274
Chet Haase21cd1382010-09-01 17:42:29 -07006275 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006276 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6277 // A change in dimension means an auto-centered pivot point changes, too
6278 mMatrixDirty = true;
6279 }
Chet Haase21cd1382010-09-01 17:42:29 -07006280 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006281 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006282 }
Chet Haase55dbb652010-12-21 20:15:08 -08006283 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006284 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006285 }
6286 }
6287
6288 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006289 * The visual x position of this view, in pixels. This is equivalent to the
6290 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006291 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006292 *
Chet Haasedf030d22010-07-30 17:22:38 -07006293 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006294 */
Chet Haasedf030d22010-07-30 17:22:38 -07006295 public float getX() {
6296 return mLeft + mTranslationX;
6297 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006298
Chet Haasedf030d22010-07-30 17:22:38 -07006299 /**
6300 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6301 * {@link #setTranslationX(float) translationX} property to be the difference between
6302 * the x value passed in and the current {@link #getLeft() left} property.
6303 *
6304 * @param x The visual x position of this view, in pixels.
6305 */
6306 public void setX(float x) {
6307 setTranslationX(x - mLeft);
6308 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006309
Chet Haasedf030d22010-07-30 17:22:38 -07006310 /**
6311 * The visual y position of this view, in pixels. This is equivalent to the
6312 * {@link #setTranslationY(float) translationY} property plus the current
6313 * {@link #getTop() top} property.
6314 *
6315 * @return The visual y position of this view, in pixels.
6316 */
6317 public float getY() {
6318 return mTop + mTranslationY;
6319 }
6320
6321 /**
6322 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6323 * {@link #setTranslationY(float) translationY} property to be the difference between
6324 * the y value passed in and the current {@link #getTop() top} property.
6325 *
6326 * @param y The visual y position of this view, in pixels.
6327 */
6328 public void setY(float y) {
6329 setTranslationY(y - mTop);
6330 }
6331
6332
6333 /**
6334 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6335 * This position is post-layout, in addition to wherever the object's
6336 * layout placed it.
6337 *
6338 * @return The horizontal position of this view relative to its left position, in pixels.
6339 */
6340 public float getTranslationX() {
6341 return mTranslationX;
6342 }
6343
6344 /**
6345 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6346 * This effectively positions the object post-layout, in addition to wherever the object's
6347 * layout placed it.
6348 *
6349 * @param translationX The horizontal position of this view relative to its left position,
6350 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006351 *
6352 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006353 */
6354 public void setTranslationX(float translationX) {
6355 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006356 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006357 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006358 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006359 mTranslationX = translationX;
6360 mMatrixDirty = true;
6361 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006362 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006363 }
6364 }
6365
6366 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006367 * The horizontal location of this view relative to its {@link #getTop() top} position.
6368 * This position is post-layout, in addition to wherever the object's
6369 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006370 *
Chet Haasedf030d22010-07-30 17:22:38 -07006371 * @return The vertical position of this view relative to its top position,
6372 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006373 */
Chet Haasedf030d22010-07-30 17:22:38 -07006374 public float getTranslationY() {
6375 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006376 }
6377
6378 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006379 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6380 * This effectively positions the object post-layout, in addition to wherever the object's
6381 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006382 *
Chet Haasedf030d22010-07-30 17:22:38 -07006383 * @param translationY The vertical position of this view relative to its top position,
6384 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006385 *
6386 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006387 */
Chet Haasedf030d22010-07-30 17:22:38 -07006388 public void setTranslationY(float translationY) {
6389 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006390 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006391 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006392 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006393 mTranslationY = translationY;
6394 mMatrixDirty = true;
6395 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006396 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006397 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006398 }
6399
6400 /**
Romain Guyda489792011-02-03 01:05:15 -08006401 * @hide
6402 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006403 public void setFastTranslationX(float x) {
6404 mTranslationX = x;
6405 mMatrixDirty = true;
6406 }
6407
6408 /**
6409 * @hide
6410 */
6411 public void setFastTranslationY(float y) {
6412 mTranslationY = y;
6413 mMatrixDirty = true;
6414 }
6415
6416 /**
6417 * @hide
6418 */
Romain Guyda489792011-02-03 01:05:15 -08006419 public void setFastX(float x) {
6420 mTranslationX = x - mLeft;
6421 mMatrixDirty = true;
6422 }
6423
6424 /**
6425 * @hide
6426 */
6427 public void setFastY(float y) {
6428 mTranslationY = y - mTop;
6429 mMatrixDirty = true;
6430 }
6431
6432 /**
6433 * @hide
6434 */
6435 public void setFastScaleX(float x) {
6436 mScaleX = x;
6437 mMatrixDirty = true;
6438 }
6439
6440 /**
6441 * @hide
6442 */
6443 public void setFastScaleY(float y) {
6444 mScaleY = y;
6445 mMatrixDirty = true;
6446 }
6447
6448 /**
6449 * @hide
6450 */
6451 public void setFastAlpha(float alpha) {
6452 mAlpha = alpha;
6453 }
6454
6455 /**
6456 * @hide
6457 */
6458 public void setFastRotationY(float y) {
6459 mRotationY = y;
6460 mMatrixDirty = true;
6461 }
6462
6463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 * Hit rectangle in parent's coordinates
6465 *
6466 * @param outRect The hit rectangle of the view.
6467 */
6468 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006469 updateMatrix();
6470 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006471 outRect.set(mLeft, mTop, mRight, mBottom);
6472 } else {
6473 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006474 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006475 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006476 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6477 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006478 }
6479 }
6480
6481 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006482 * Determines whether the given point, in local coordinates is inside the view.
6483 */
6484 /*package*/ final boolean pointInView(float localX, float localY) {
6485 return localX >= 0 && localX < (mRight - mLeft)
6486 && localY >= 0 && localY < (mBottom - mTop);
6487 }
6488
6489 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006490 * Utility method to determine whether the given point, in local coordinates,
6491 * is inside the view, where the area of the view is expanded by the slop factor.
6492 * This method is called while processing touch-move events to determine if the event
6493 * is still within the view.
6494 */
6495 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006496 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006497 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006498 }
6499
6500 /**
6501 * When a view has focus and the user navigates away from it, the next view is searched for
6502 * starting from the rectangle filled in by this method.
6503 *
6504 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6505 * view maintains some idea of internal selection, such as a cursor, or a selected row
6506 * or column, you should override this method and fill in a more specific rectangle.
6507 *
6508 * @param r The rectangle to fill in, in this view's coordinates.
6509 */
6510 public void getFocusedRect(Rect r) {
6511 getDrawingRect(r);
6512 }
6513
6514 /**
6515 * If some part of this view is not clipped by any of its parents, then
6516 * return that area in r in global (root) coordinates. To convert r to local
6517 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6518 * -globalOffset.y)) If the view is completely clipped or translated out,
6519 * return false.
6520 *
6521 * @param r If true is returned, r holds the global coordinates of the
6522 * visible portion of this view.
6523 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6524 * between this view and its root. globalOffet may be null.
6525 * @return true if r is non-empty (i.e. part of the view is visible at the
6526 * root level.
6527 */
6528 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6529 int width = mRight - mLeft;
6530 int height = mBottom - mTop;
6531 if (width > 0 && height > 0) {
6532 r.set(0, 0, width, height);
6533 if (globalOffset != null) {
6534 globalOffset.set(-mScrollX, -mScrollY);
6535 }
6536 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6537 }
6538 return false;
6539 }
6540
6541 public final boolean getGlobalVisibleRect(Rect r) {
6542 return getGlobalVisibleRect(r, null);
6543 }
6544
6545 public final boolean getLocalVisibleRect(Rect r) {
6546 Point offset = new Point();
6547 if (getGlobalVisibleRect(r, offset)) {
6548 r.offset(-offset.x, -offset.y); // make r local
6549 return true;
6550 }
6551 return false;
6552 }
6553
6554 /**
6555 * Offset this view's vertical location by the specified number of pixels.
6556 *
6557 * @param offset the number of pixels to offset the view by
6558 */
6559 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006560 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006561 updateMatrix();
6562 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006563 final ViewParent p = mParent;
6564 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006565 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006566 int minTop;
6567 int maxBottom;
6568 int yLoc;
6569 if (offset < 0) {
6570 minTop = mTop + offset;
6571 maxBottom = mBottom;
6572 yLoc = offset;
6573 } else {
6574 minTop = mTop;
6575 maxBottom = mBottom + offset;
6576 yLoc = 0;
6577 }
6578 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6579 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006580 }
6581 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006582 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006583 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006584
Chet Haasec3aa3612010-06-17 08:50:37 -07006585 mTop += offset;
6586 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006587
Chet Haasec3aa3612010-06-17 08:50:37 -07006588 if (!mMatrixIsIdentity) {
6589 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006590 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006591 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006592 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006593 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006594 }
6595
6596 /**
6597 * Offset this view's horizontal location by the specified amount of pixels.
6598 *
6599 * @param offset the numer of pixels to offset the view by
6600 */
6601 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006602 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006603 updateMatrix();
6604 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006605 final ViewParent p = mParent;
6606 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006607 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006608 int minLeft;
6609 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006610 if (offset < 0) {
6611 minLeft = mLeft + offset;
6612 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006613 } else {
6614 minLeft = mLeft;
6615 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006616 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006617 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07006618 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006619 }
6620 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006621 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006622 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006623
Chet Haasec3aa3612010-06-17 08:50:37 -07006624 mLeft += offset;
6625 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006626
Chet Haasec3aa3612010-06-17 08:50:37 -07006627 if (!mMatrixIsIdentity) {
6628 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006629 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006630 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006631 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006632 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006633 }
6634
6635 /**
6636 * Get the LayoutParams associated with this view. All views should have
6637 * layout parameters. These supply parameters to the <i>parent</i> of this
6638 * view specifying how it should be arranged. There are many subclasses of
6639 * ViewGroup.LayoutParams, and these correspond to the different subclasses
6640 * of ViewGroup that are responsible for arranging their children.
6641 * @return The LayoutParams associated with this view
6642 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07006643 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006644 public ViewGroup.LayoutParams getLayoutParams() {
6645 return mLayoutParams;
6646 }
6647
6648 /**
6649 * Set the layout parameters associated with this view. These supply
6650 * parameters to the <i>parent</i> of this view specifying how it should be
6651 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
6652 * correspond to the different subclasses of ViewGroup that are responsible
6653 * for arranging their children.
6654 *
6655 * @param params the layout parameters for this view
6656 */
6657 public void setLayoutParams(ViewGroup.LayoutParams params) {
6658 if (params == null) {
6659 throw new NullPointerException("params == null");
6660 }
6661 mLayoutParams = params;
6662 requestLayout();
6663 }
6664
6665 /**
6666 * Set the scrolled position of your view. This will cause a call to
6667 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6668 * invalidated.
6669 * @param x the x position to scroll to
6670 * @param y the y position to scroll to
6671 */
6672 public void scrollTo(int x, int y) {
6673 if (mScrollX != x || mScrollY != y) {
6674 int oldX = mScrollX;
6675 int oldY = mScrollY;
6676 mScrollX = x;
6677 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006678 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006679 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07006680 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006681 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006682 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006683 }
6684 }
6685
6686 /**
6687 * Move the scrolled position of your view. This will cause a call to
6688 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6689 * invalidated.
6690 * @param x the amount of pixels to scroll by horizontally
6691 * @param y the amount of pixels to scroll by vertically
6692 */
6693 public void scrollBy(int x, int y) {
6694 scrollTo(mScrollX + x, mScrollY + y);
6695 }
6696
6697 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006698 * <p>Trigger the scrollbars to draw. When invoked this method starts an
6699 * animation to fade the scrollbars out after a default delay. If a subclass
6700 * provides animated scrolling, the start delay should equal the duration
6701 * of the scrolling animation.</p>
6702 *
6703 * <p>The animation starts only if at least one of the scrollbars is
6704 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
6705 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6706 * this method returns true, and false otherwise. If the animation is
6707 * started, this method calls {@link #invalidate()}; in that case the
6708 * caller should not call {@link #invalidate()}.</p>
6709 *
6710 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07006711 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07006712 *
6713 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
6714 * and {@link #scrollTo(int, int)}.</p>
6715 *
6716 * @return true if the animation is played, false otherwise
6717 *
6718 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07006719 * @see #scrollBy(int, int)
6720 * @see #scrollTo(int, int)
6721 * @see #isHorizontalScrollBarEnabled()
6722 * @see #isVerticalScrollBarEnabled()
6723 * @see #setHorizontalScrollBarEnabled(boolean)
6724 * @see #setVerticalScrollBarEnabled(boolean)
6725 */
6726 protected boolean awakenScrollBars() {
6727 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07006728 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07006729 }
6730
6731 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07006732 * Trigger the scrollbars to draw.
6733 * This method differs from awakenScrollBars() only in its default duration.
6734 * initialAwakenScrollBars() will show the scroll bars for longer than
6735 * usual to give the user more of a chance to notice them.
6736 *
6737 * @return true if the animation is played, false otherwise.
6738 */
6739 private boolean initialAwakenScrollBars() {
6740 return mScrollCache != null &&
6741 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
6742 }
6743
6744 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07006745 * <p>
6746 * Trigger the scrollbars to draw. When invoked this method starts an
6747 * animation to fade the scrollbars out after a fixed delay. If a subclass
6748 * provides animated scrolling, the start delay should equal the duration of
6749 * the scrolling animation.
6750 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006751 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006752 * <p>
6753 * The animation starts only if at least one of the scrollbars is enabled,
6754 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6755 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6756 * this method returns true, and false otherwise. If the animation is
6757 * started, this method calls {@link #invalidate()}; in that case the caller
6758 * should not call {@link #invalidate()}.
6759 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006760 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006761 * <p>
6762 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07006763 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07006764 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006765 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006766 * @param startDelay the delay, in milliseconds, after which the animation
6767 * should start; when the delay is 0, the animation starts
6768 * immediately
6769 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006770 *
Mike Cleronf116bf82009-09-27 19:14:12 -07006771 * @see #scrollBy(int, int)
6772 * @see #scrollTo(int, int)
6773 * @see #isHorizontalScrollBarEnabled()
6774 * @see #isVerticalScrollBarEnabled()
6775 * @see #setHorizontalScrollBarEnabled(boolean)
6776 * @see #setVerticalScrollBarEnabled(boolean)
6777 */
6778 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07006779 return awakenScrollBars(startDelay, true);
6780 }
Joe Malin32736f02011-01-19 16:14:20 -08006781
Mike Cleron290947b2009-09-29 18:34:32 -07006782 /**
6783 * <p>
6784 * Trigger the scrollbars to draw. When invoked this method starts an
6785 * animation to fade the scrollbars out after a fixed delay. If a subclass
6786 * provides animated scrolling, the start delay should equal the duration of
6787 * the scrolling animation.
6788 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006789 *
Mike Cleron290947b2009-09-29 18:34:32 -07006790 * <p>
6791 * The animation starts only if at least one of the scrollbars is enabled,
6792 * as specified by {@link #isHorizontalScrollBarEnabled()} and
6793 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
6794 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08006795 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07006796 * is set to true; in that case the caller
6797 * should not call {@link #invalidate()}.
6798 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006799 *
Mike Cleron290947b2009-09-29 18:34:32 -07006800 * <p>
6801 * This method should be invoked everytime a subclass directly updates the
6802 * scroll parameters.
6803 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08006804 *
Mike Cleron290947b2009-09-29 18:34:32 -07006805 * @param startDelay the delay, in milliseconds, after which the animation
6806 * should start; when the delay is 0, the animation starts
6807 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08006808 *
Mike Cleron290947b2009-09-29 18:34:32 -07006809 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08006810 *
Mike Cleron290947b2009-09-29 18:34:32 -07006811 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08006812 *
Mike Cleron290947b2009-09-29 18:34:32 -07006813 * @see #scrollBy(int, int)
6814 * @see #scrollTo(int, int)
6815 * @see #isHorizontalScrollBarEnabled()
6816 * @see #isVerticalScrollBarEnabled()
6817 * @see #setHorizontalScrollBarEnabled(boolean)
6818 * @see #setVerticalScrollBarEnabled(boolean)
6819 */
6820 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07006821 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08006822
Mike Cleronf116bf82009-09-27 19:14:12 -07006823 if (scrollCache == null || !scrollCache.fadeScrollBars) {
6824 return false;
6825 }
6826
6827 if (scrollCache.scrollBar == null) {
6828 scrollCache.scrollBar = new ScrollBarDrawable();
6829 }
6830
6831 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
6832
Mike Cleron290947b2009-09-29 18:34:32 -07006833 if (invalidate) {
6834 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08006835 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07006836 }
Mike Cleronf116bf82009-09-27 19:14:12 -07006837
6838 if (scrollCache.state == ScrollabilityCache.OFF) {
6839 // FIXME: this is copied from WindowManagerService.
6840 // We should get this value from the system when it
6841 // is possible to do so.
6842 final int KEY_REPEAT_FIRST_DELAY = 750;
6843 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
6844 }
6845
6846 // Tell mScrollCache when we should start fading. This may
6847 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07006848 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07006849 scrollCache.fadeStartTime = fadeStartTime;
6850 scrollCache.state = ScrollabilityCache.ON;
6851
6852 // Schedule our fader to run, unscheduling any old ones first
6853 if (mAttachInfo != null) {
6854 mAttachInfo.mHandler.removeCallbacks(scrollCache);
6855 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
6856 }
6857
6858 return true;
6859 }
6860
6861 return false;
6862 }
6863
6864 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006865 * Mark the the area defined by dirty as needing to be drawn. If the view is
6866 * visible, {@link #onDraw} will be called at some point in the future.
6867 * This must be called from a UI thread. To call from a non-UI thread, call
6868 * {@link #postInvalidate()}.
6869 *
6870 * WARNING: This method is destructive to dirty.
6871 * @param dirty the rectangle representing the bounds of the dirty region
6872 */
6873 public void invalidate(Rect dirty) {
6874 if (ViewDebug.TRACE_HIERARCHY) {
6875 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6876 }
6877
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006878 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006879 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6880 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006881 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006882 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006883 final ViewParent p = mParent;
6884 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006885 //noinspection PointlessBooleanExpression,ConstantConditions
6886 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6887 if (p != null && ai != null && ai.mHardwareAccelerated) {
6888 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6889 // with a null dirty rect, which tells the ViewRoot to redraw everything
6890 p.invalidateChild(this, null);
6891 return;
6892 }
Romain Guyaf636eb2010-12-09 17:47:21 -08006893 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006894 if (p != null && ai != null) {
6895 final int scrollX = mScrollX;
6896 final int scrollY = mScrollY;
6897 final Rect r = ai.mTmpInvalRect;
6898 r.set(dirty.left - scrollX, dirty.top - scrollY,
6899 dirty.right - scrollX, dirty.bottom - scrollY);
6900 mParent.invalidateChild(this, r);
6901 }
6902 }
6903 }
6904
6905 /**
6906 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
6907 * The coordinates of the dirty rect are relative to the view.
6908 * If the view is visible, {@link #onDraw} will be called at some point
6909 * in the future. This must be called from a UI thread. To call
6910 * from a non-UI thread, call {@link #postInvalidate()}.
6911 * @param l the left position of the dirty region
6912 * @param t the top position of the dirty region
6913 * @param r the right position of the dirty region
6914 * @param b the bottom position of the dirty region
6915 */
6916 public void invalidate(int l, int t, int r, int b) {
6917 if (ViewDebug.TRACE_HIERARCHY) {
6918 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6919 }
6920
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006921 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08006922 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
6923 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006924 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08006925 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926 final ViewParent p = mParent;
6927 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08006928 //noinspection PointlessBooleanExpression,ConstantConditions
6929 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6930 if (p != null && ai != null && ai.mHardwareAccelerated) {
6931 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6932 // with a null dirty rect, which tells the ViewRoot to redraw everything
6933 p.invalidateChild(this, null);
6934 return;
6935 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08006936 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006937 if (p != null && ai != null && l < r && t < b) {
6938 final int scrollX = mScrollX;
6939 final int scrollY = mScrollY;
6940 final Rect tmpr = ai.mTmpInvalRect;
6941 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
6942 p.invalidateChild(this, tmpr);
6943 }
6944 }
6945 }
6946
6947 /**
6948 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
6949 * be called at some point in the future. This must be called from a
6950 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
6951 */
6952 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07006953 invalidate(true);
6954 }
Joe Malin32736f02011-01-19 16:14:20 -08006955
Chet Haaseed032702010-10-01 14:05:54 -07006956 /**
6957 * This is where the invalidate() work actually happens. A full invalidate()
6958 * causes the drawing cache to be invalidated, but this function can be called with
6959 * invalidateCache set to false to skip that invalidation step for cases that do not
6960 * need it (for example, a component that remains at the same dimensions with the same
6961 * content).
6962 *
6963 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
6964 * well. This is usually true for a full invalidate, but may be set to false if the
6965 * View's contents or dimensions have not changed.
6966 */
Romain Guy849d0a32011-02-01 17:20:48 -08006967 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006968 if (ViewDebug.TRACE_HIERARCHY) {
6969 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
6970 }
6971
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006972 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08006973 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08006974 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
6975 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07006976 mPrivateFlags &= ~DRAWN;
6977 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08006978 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07006979 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006981 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07006982 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08006983 //noinspection PointlessBooleanExpression,ConstantConditions
6984 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
6985 if (p != null && ai != null && ai.mHardwareAccelerated) {
6986 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
6987 // with a null dirty rect, which tells the ViewRoot to redraw everything
6988 p.invalidateChild(this, null);
6989 return;
6990 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08006991 }
Michael Jurkaebefea42010-11-15 16:04:01 -08006992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006993 if (p != null && ai != null) {
6994 final Rect r = ai.mTmpInvalRect;
6995 r.set(0, 0, mRight - mLeft, mBottom - mTop);
6996 // Don't call invalidate -- we don't want to internally scroll
6997 // our own bounds
6998 p.invalidateChild(this, r);
6999 }
7000 }
7001 }
7002
7003 /**
Romain Guyda489792011-02-03 01:05:15 -08007004 * @hide
7005 */
7006 public void fastInvalidate() {
7007 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7008 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7009 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7010 if (mParent instanceof View) {
7011 ((View) mParent).mPrivateFlags |= INVALIDATED;
7012 }
7013 mPrivateFlags &= ~DRAWN;
7014 mPrivateFlags |= INVALIDATED;
7015 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7016 if (mParent != null && mAttachInfo != null && mAttachInfo.mHardwareAccelerated) {
7017 mParent.invalidateChild(this, null);
7018 }
7019 }
7020 }
7021
7022 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007023 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007024 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7025 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007026 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7027 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007028 *
7029 * @hide
7030 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007031 protected void invalidateParentCaches() {
7032 if (mParent instanceof View) {
7033 ((View) mParent).mPrivateFlags |= INVALIDATED;
7034 }
7035 }
Joe Malin32736f02011-01-19 16:14:20 -08007036
Romain Guy0fd89bf2011-01-26 15:41:30 -08007037 /**
7038 * Used to indicate that the parent of this view should be invalidated. This functionality
7039 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7040 * which is necessary when various parent-managed properties of the view change, such as
7041 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7042 * an invalidation event to the parent.
7043 *
7044 * @hide
7045 */
7046 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007047 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007048 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007049 }
7050 }
7051
7052 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007053 * Indicates whether this View is opaque. An opaque View guarantees that it will
7054 * draw all the pixels overlapping its bounds using a fully opaque color.
7055 *
7056 * Subclasses of View should override this method whenever possible to indicate
7057 * whether an instance is opaque. Opaque Views are treated in a special way by
7058 * the View hierarchy, possibly allowing it to perform optimizations during
7059 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007060 *
Romain Guy24443ea2009-05-11 11:56:30 -07007061 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007062 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007063 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007064 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007065 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7066 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007067 }
7068
Adam Powell20232d02010-12-08 21:08:53 -08007069 /**
7070 * @hide
7071 */
7072 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007073 // Opaque if:
7074 // - Has a background
7075 // - Background is opaque
7076 // - Doesn't have scrollbars or scrollbars are inside overlay
7077
7078 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7079 mPrivateFlags |= OPAQUE_BACKGROUND;
7080 } else {
7081 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7082 }
7083
7084 final int flags = mViewFlags;
7085 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7086 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7087 mPrivateFlags |= OPAQUE_SCROLLBARS;
7088 } else {
7089 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7090 }
7091 }
7092
7093 /**
7094 * @hide
7095 */
7096 protected boolean hasOpaqueScrollbars() {
7097 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007098 }
7099
7100 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007101 * @return A handler associated with the thread running the View. This
7102 * handler can be used to pump events in the UI events queue.
7103 */
7104 public Handler getHandler() {
7105 if (mAttachInfo != null) {
7106 return mAttachInfo.mHandler;
7107 }
7108 return null;
7109 }
7110
7111 /**
7112 * Causes the Runnable to be added to the message queue.
7113 * The runnable will be run on the user interface thread.
7114 *
7115 * @param action The Runnable that will be executed.
7116 *
7117 * @return Returns true if the Runnable was successfully placed in to the
7118 * message queue. Returns false on failure, usually because the
7119 * looper processing the message queue is exiting.
7120 */
7121 public boolean post(Runnable action) {
7122 Handler handler;
7123 if (mAttachInfo != null) {
7124 handler = mAttachInfo.mHandler;
7125 } else {
7126 // Assume that post will succeed later
7127 ViewRoot.getRunQueue().post(action);
7128 return true;
7129 }
7130
7131 return handler.post(action);
7132 }
7133
7134 /**
7135 * Causes the Runnable to be added to the message queue, to be run
7136 * after the specified amount of time elapses.
7137 * The runnable will be run on the user interface thread.
7138 *
7139 * @param action The Runnable that will be executed.
7140 * @param delayMillis The delay (in milliseconds) until the Runnable
7141 * will be executed.
7142 *
7143 * @return true if the Runnable was successfully placed in to the
7144 * message queue. Returns false on failure, usually because the
7145 * looper processing the message queue is exiting. Note that a
7146 * result of true does not mean the Runnable will be processed --
7147 * if the looper is quit before the delivery time of the message
7148 * occurs then the message will be dropped.
7149 */
7150 public boolean postDelayed(Runnable action, long delayMillis) {
7151 Handler handler;
7152 if (mAttachInfo != null) {
7153 handler = mAttachInfo.mHandler;
7154 } else {
7155 // Assume that post will succeed later
7156 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7157 return true;
7158 }
7159
7160 return handler.postDelayed(action, delayMillis);
7161 }
7162
7163 /**
7164 * Removes the specified Runnable from the message queue.
7165 *
7166 * @param action The Runnable to remove from the message handling queue
7167 *
7168 * @return true if this view could ask the Handler to remove the Runnable,
7169 * false otherwise. When the returned value is true, the Runnable
7170 * may or may not have been actually removed from the message queue
7171 * (for instance, if the Runnable was not in the queue already.)
7172 */
7173 public boolean removeCallbacks(Runnable action) {
7174 Handler handler;
7175 if (mAttachInfo != null) {
7176 handler = mAttachInfo.mHandler;
7177 } else {
7178 // Assume that post will succeed later
7179 ViewRoot.getRunQueue().removeCallbacks(action);
7180 return true;
7181 }
7182
7183 handler.removeCallbacks(action);
7184 return true;
7185 }
7186
7187 /**
7188 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7189 * Use this to invalidate the View from a non-UI thread.
7190 *
7191 * @see #invalidate()
7192 */
7193 public void postInvalidate() {
7194 postInvalidateDelayed(0);
7195 }
7196
7197 /**
7198 * Cause an invalidate of the specified area to happen on a subsequent cycle
7199 * through the event loop. Use this to invalidate the View from a non-UI thread.
7200 *
7201 * @param left The left coordinate of the rectangle to invalidate.
7202 * @param top The top coordinate of the rectangle to invalidate.
7203 * @param right The right coordinate of the rectangle to invalidate.
7204 * @param bottom The bottom coordinate of the rectangle to invalidate.
7205 *
7206 * @see #invalidate(int, int, int, int)
7207 * @see #invalidate(Rect)
7208 */
7209 public void postInvalidate(int left, int top, int right, int bottom) {
7210 postInvalidateDelayed(0, left, top, right, bottom);
7211 }
7212
7213 /**
7214 * Cause an invalidate to happen on a subsequent cycle through the event
7215 * loop. Waits for the specified amount of time.
7216 *
7217 * @param delayMilliseconds the duration in milliseconds to delay the
7218 * invalidation by
7219 */
7220 public void postInvalidateDelayed(long delayMilliseconds) {
7221 // We try only with the AttachInfo because there's no point in invalidating
7222 // if we are not attached to our window
7223 if (mAttachInfo != null) {
7224 Message msg = Message.obtain();
7225 msg.what = AttachInfo.INVALIDATE_MSG;
7226 msg.obj = this;
7227 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7228 }
7229 }
7230
7231 /**
7232 * Cause an invalidate of the specified area to happen on a subsequent cycle
7233 * through the event loop. Waits for the specified amount of time.
7234 *
7235 * @param delayMilliseconds the duration in milliseconds to delay the
7236 * invalidation by
7237 * @param left The left coordinate of the rectangle to invalidate.
7238 * @param top The top coordinate of the rectangle to invalidate.
7239 * @param right The right coordinate of the rectangle to invalidate.
7240 * @param bottom The bottom coordinate of the rectangle to invalidate.
7241 */
7242 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7243 int right, int bottom) {
7244
7245 // We try only with the AttachInfo because there's no point in invalidating
7246 // if we are not attached to our window
7247 if (mAttachInfo != null) {
7248 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7249 info.target = this;
7250 info.left = left;
7251 info.top = top;
7252 info.right = right;
7253 info.bottom = bottom;
7254
7255 final Message msg = Message.obtain();
7256 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7257 msg.obj = info;
7258 mAttachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
7259 }
7260 }
7261
7262 /**
7263 * Called by a parent to request that a child update its values for mScrollX
7264 * and mScrollY if necessary. This will typically be done if the child is
7265 * animating a scroll using a {@link android.widget.Scroller Scroller}
7266 * object.
7267 */
7268 public void computeScroll() {
7269 }
7270
7271 /**
7272 * <p>Indicate whether the horizontal edges are faded when the view is
7273 * scrolled horizontally.</p>
7274 *
7275 * @return true if the horizontal edges should are faded on scroll, false
7276 * otherwise
7277 *
7278 * @see #setHorizontalFadingEdgeEnabled(boolean)
7279 * @attr ref android.R.styleable#View_fadingEdge
7280 */
7281 public boolean isHorizontalFadingEdgeEnabled() {
7282 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7283 }
7284
7285 /**
7286 * <p>Define whether the horizontal edges should be faded when this view
7287 * is scrolled horizontally.</p>
7288 *
7289 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7290 * be faded when the view is scrolled
7291 * horizontally
7292 *
7293 * @see #isHorizontalFadingEdgeEnabled()
7294 * @attr ref android.R.styleable#View_fadingEdge
7295 */
7296 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7297 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7298 if (horizontalFadingEdgeEnabled) {
7299 initScrollCache();
7300 }
7301
7302 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7303 }
7304 }
7305
7306 /**
7307 * <p>Indicate whether the vertical edges are faded when the view is
7308 * scrolled horizontally.</p>
7309 *
7310 * @return true if the vertical edges should are faded on scroll, false
7311 * otherwise
7312 *
7313 * @see #setVerticalFadingEdgeEnabled(boolean)
7314 * @attr ref android.R.styleable#View_fadingEdge
7315 */
7316 public boolean isVerticalFadingEdgeEnabled() {
7317 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7318 }
7319
7320 /**
7321 * <p>Define whether the vertical edges should be faded when this view
7322 * is scrolled vertically.</p>
7323 *
7324 * @param verticalFadingEdgeEnabled true if the vertical edges should
7325 * be faded when the view is scrolled
7326 * vertically
7327 *
7328 * @see #isVerticalFadingEdgeEnabled()
7329 * @attr ref android.R.styleable#View_fadingEdge
7330 */
7331 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7332 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7333 if (verticalFadingEdgeEnabled) {
7334 initScrollCache();
7335 }
7336
7337 mViewFlags ^= FADING_EDGE_VERTICAL;
7338 }
7339 }
7340
7341 /**
7342 * Returns the strength, or intensity, of the top faded edge. The strength is
7343 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7344 * returns 0.0 or 1.0 but no value in between.
7345 *
7346 * Subclasses should override this method to provide a smoother fade transition
7347 * when scrolling occurs.
7348 *
7349 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7350 */
7351 protected float getTopFadingEdgeStrength() {
7352 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7353 }
7354
7355 /**
7356 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7357 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7358 * returns 0.0 or 1.0 but no value in between.
7359 *
7360 * Subclasses should override this method to provide a smoother fade transition
7361 * when scrolling occurs.
7362 *
7363 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7364 */
7365 protected float getBottomFadingEdgeStrength() {
7366 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7367 computeVerticalScrollRange() ? 1.0f : 0.0f;
7368 }
7369
7370 /**
7371 * Returns the strength, or intensity, of the left faded edge. The strength is
7372 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7373 * returns 0.0 or 1.0 but no value in between.
7374 *
7375 * Subclasses should override this method to provide a smoother fade transition
7376 * when scrolling occurs.
7377 *
7378 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7379 */
7380 protected float getLeftFadingEdgeStrength() {
7381 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7382 }
7383
7384 /**
7385 * Returns the strength, or intensity, of the right faded edge. The strength is
7386 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7387 * returns 0.0 or 1.0 but no value in between.
7388 *
7389 * Subclasses should override this method to provide a smoother fade transition
7390 * when scrolling occurs.
7391 *
7392 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7393 */
7394 protected float getRightFadingEdgeStrength() {
7395 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7396 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7397 }
7398
7399 /**
7400 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7401 * scrollbar is not drawn by default.</p>
7402 *
7403 * @return true if the horizontal scrollbar should be painted, false
7404 * otherwise
7405 *
7406 * @see #setHorizontalScrollBarEnabled(boolean)
7407 */
7408 public boolean isHorizontalScrollBarEnabled() {
7409 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7410 }
7411
7412 /**
7413 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7414 * scrollbar is not drawn by default.</p>
7415 *
7416 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7417 * be painted
7418 *
7419 * @see #isHorizontalScrollBarEnabled()
7420 */
7421 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7422 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7423 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007424 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007425 recomputePadding();
7426 }
7427 }
7428
7429 /**
7430 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7431 * scrollbar is not drawn by default.</p>
7432 *
7433 * @return true if the vertical scrollbar should be painted, false
7434 * otherwise
7435 *
7436 * @see #setVerticalScrollBarEnabled(boolean)
7437 */
7438 public boolean isVerticalScrollBarEnabled() {
7439 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7440 }
7441
7442 /**
7443 * <p>Define whether the vertical scrollbar should be drawn or not. The
7444 * scrollbar is not drawn by default.</p>
7445 *
7446 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7447 * be painted
7448 *
7449 * @see #isVerticalScrollBarEnabled()
7450 */
7451 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7452 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7453 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007454 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007455 recomputePadding();
7456 }
7457 }
7458
Adam Powell20232d02010-12-08 21:08:53 -08007459 /**
7460 * @hide
7461 */
7462 protected void recomputePadding() {
7463 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 }
Joe Malin32736f02011-01-19 16:14:20 -08007465
Mike Cleronfe81d382009-09-28 14:22:16 -07007466 /**
7467 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007468 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007469 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007470 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007471 */
7472 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7473 initScrollCache();
7474 final ScrollabilityCache scrollabilityCache = mScrollCache;
7475 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007476 if (fadeScrollbars) {
7477 scrollabilityCache.state = ScrollabilityCache.OFF;
7478 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007479 scrollabilityCache.state = ScrollabilityCache.ON;
7480 }
7481 }
Joe Malin32736f02011-01-19 16:14:20 -08007482
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007483 /**
Joe Malin32736f02011-01-19 16:14:20 -08007484 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007485 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007486 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007487 * @return true if scrollbar fading is enabled
7488 */
7489 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007490 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007491 }
Joe Malin32736f02011-01-19 16:14:20 -08007492
Mike Cleron52f0a642009-09-28 18:21:37 -07007493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007494 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7495 * inset. When inset, they add to the padding of the view. And the scrollbars
7496 * can be drawn inside the padding area or on the edge of the view. For example,
7497 * if a view has a background drawable and you want to draw the scrollbars
7498 * inside the padding specified by the drawable, you can use
7499 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7500 * appear at the edge of the view, ignoring the padding, then you can use
7501 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7502 * @param style the style of the scrollbars. Should be one of
7503 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7504 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7505 * @see #SCROLLBARS_INSIDE_OVERLAY
7506 * @see #SCROLLBARS_INSIDE_INSET
7507 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7508 * @see #SCROLLBARS_OUTSIDE_INSET
7509 */
7510 public void setScrollBarStyle(int style) {
7511 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7512 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007513 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007514 recomputePadding();
7515 }
7516 }
7517
7518 /**
7519 * <p>Returns the current scrollbar style.</p>
7520 * @return the current scrollbar style
7521 * @see #SCROLLBARS_INSIDE_OVERLAY
7522 * @see #SCROLLBARS_INSIDE_INSET
7523 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7524 * @see #SCROLLBARS_OUTSIDE_INSET
7525 */
7526 public int getScrollBarStyle() {
7527 return mViewFlags & SCROLLBARS_STYLE_MASK;
7528 }
7529
7530 /**
7531 * <p>Compute the horizontal range that the horizontal scrollbar
7532 * represents.</p>
7533 *
7534 * <p>The range is expressed in arbitrary units that must be the same as the
7535 * units used by {@link #computeHorizontalScrollExtent()} and
7536 * {@link #computeHorizontalScrollOffset()}.</p>
7537 *
7538 * <p>The default range is the drawing width of this view.</p>
7539 *
7540 * @return the total horizontal range represented by the horizontal
7541 * scrollbar
7542 *
7543 * @see #computeHorizontalScrollExtent()
7544 * @see #computeHorizontalScrollOffset()
7545 * @see android.widget.ScrollBarDrawable
7546 */
7547 protected int computeHorizontalScrollRange() {
7548 return getWidth();
7549 }
7550
7551 /**
7552 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7553 * within the horizontal range. This value is used to compute the position
7554 * of the thumb within the scrollbar's track.</p>
7555 *
7556 * <p>The range is expressed in arbitrary units that must be the same as the
7557 * units used by {@link #computeHorizontalScrollRange()} and
7558 * {@link #computeHorizontalScrollExtent()}.</p>
7559 *
7560 * <p>The default offset is the scroll offset of this view.</p>
7561 *
7562 * @return the horizontal offset of the scrollbar's thumb
7563 *
7564 * @see #computeHorizontalScrollRange()
7565 * @see #computeHorizontalScrollExtent()
7566 * @see android.widget.ScrollBarDrawable
7567 */
7568 protected int computeHorizontalScrollOffset() {
7569 return mScrollX;
7570 }
7571
7572 /**
7573 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7574 * within the horizontal range. This value is used to compute the length
7575 * of the thumb within the scrollbar's track.</p>
7576 *
7577 * <p>The range is expressed in arbitrary units that must be the same as the
7578 * units used by {@link #computeHorizontalScrollRange()} and
7579 * {@link #computeHorizontalScrollOffset()}.</p>
7580 *
7581 * <p>The default extent is the drawing width of this view.</p>
7582 *
7583 * @return the horizontal extent of the scrollbar's thumb
7584 *
7585 * @see #computeHorizontalScrollRange()
7586 * @see #computeHorizontalScrollOffset()
7587 * @see android.widget.ScrollBarDrawable
7588 */
7589 protected int computeHorizontalScrollExtent() {
7590 return getWidth();
7591 }
7592
7593 /**
7594 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7595 *
7596 * <p>The range is expressed in arbitrary units that must be the same as the
7597 * units used by {@link #computeVerticalScrollExtent()} and
7598 * {@link #computeVerticalScrollOffset()}.</p>
7599 *
7600 * @return the total vertical range represented by the vertical scrollbar
7601 *
7602 * <p>The default range is the drawing height of this view.</p>
7603 *
7604 * @see #computeVerticalScrollExtent()
7605 * @see #computeVerticalScrollOffset()
7606 * @see android.widget.ScrollBarDrawable
7607 */
7608 protected int computeVerticalScrollRange() {
7609 return getHeight();
7610 }
7611
7612 /**
7613 * <p>Compute the vertical offset of the vertical scrollbar's thumb
7614 * within the horizontal range. This value is used to compute the position
7615 * of the thumb within the scrollbar's track.</p>
7616 *
7617 * <p>The range is expressed in arbitrary units that must be the same as the
7618 * units used by {@link #computeVerticalScrollRange()} and
7619 * {@link #computeVerticalScrollExtent()}.</p>
7620 *
7621 * <p>The default offset is the scroll offset of this view.</p>
7622 *
7623 * @return the vertical offset of the scrollbar's thumb
7624 *
7625 * @see #computeVerticalScrollRange()
7626 * @see #computeVerticalScrollExtent()
7627 * @see android.widget.ScrollBarDrawable
7628 */
7629 protected int computeVerticalScrollOffset() {
7630 return mScrollY;
7631 }
7632
7633 /**
7634 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
7635 * within the vertical range. This value is used to compute the length
7636 * of the thumb within the scrollbar's track.</p>
7637 *
7638 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08007639 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007640 * {@link #computeVerticalScrollOffset()}.</p>
7641 *
7642 * <p>The default extent is the drawing height of this view.</p>
7643 *
7644 * @return the vertical extent of the scrollbar's thumb
7645 *
7646 * @see #computeVerticalScrollRange()
7647 * @see #computeVerticalScrollOffset()
7648 * @see android.widget.ScrollBarDrawable
7649 */
7650 protected int computeVerticalScrollExtent() {
7651 return getHeight();
7652 }
7653
7654 /**
7655 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
7656 * scrollbars are painted only if they have been awakened first.</p>
7657 *
7658 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08007659 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007660 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08007662 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007663 // scrollbars are drawn only when the animation is running
7664 final ScrollabilityCache cache = mScrollCache;
7665 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08007666
Mike Cleronf116bf82009-09-27 19:14:12 -07007667 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08007668
Mike Cleronf116bf82009-09-27 19:14:12 -07007669 if (state == ScrollabilityCache.OFF) {
7670 return;
7671 }
Joe Malin32736f02011-01-19 16:14:20 -08007672
Mike Cleronf116bf82009-09-27 19:14:12 -07007673 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08007674
Mike Cleronf116bf82009-09-27 19:14:12 -07007675 if (state == ScrollabilityCache.FADING) {
7676 // We're fading -- get our fade interpolation
7677 if (cache.interpolatorValues == null) {
7678 cache.interpolatorValues = new float[1];
7679 }
Joe Malin32736f02011-01-19 16:14:20 -08007680
Mike Cleronf116bf82009-09-27 19:14:12 -07007681 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08007682
Mike Cleronf116bf82009-09-27 19:14:12 -07007683 // Stops the animation if we're done
7684 if (cache.scrollBarInterpolator.timeToValues(values) ==
7685 Interpolator.Result.FREEZE_END) {
7686 cache.state = ScrollabilityCache.OFF;
7687 } else {
7688 cache.scrollBar.setAlpha(Math.round(values[0]));
7689 }
Joe Malin32736f02011-01-19 16:14:20 -08007690
7691 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07007692 // drawing. We only want this when we're fading so that
7693 // we prevent excessive redraws
7694 invalidate = true;
7695 } else {
7696 // We're just on -- but we may have been fading before so
7697 // reset alpha
7698 cache.scrollBar.setAlpha(255);
7699 }
7700
Joe Malin32736f02011-01-19 16:14:20 -08007701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007702 final int viewFlags = mViewFlags;
7703
7704 final boolean drawHorizontalScrollBar =
7705 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7706 final boolean drawVerticalScrollBar =
7707 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
7708 && !isVerticalScrollBarHidden();
7709
7710 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
7711 final int width = mRight - mLeft;
7712 final int height = mBottom - mTop;
7713
7714 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715
Mike Reede8853fc2009-09-04 14:01:48 -04007716 final int scrollX = mScrollX;
7717 final int scrollY = mScrollY;
7718 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
7719
Mike Cleronf116bf82009-09-27 19:14:12 -07007720 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08007721
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007723 int size = scrollBar.getSize(false);
7724 if (size <= 0) {
7725 size = cache.scrollBarSize;
7726 }
7727
Mike Cleronf116bf82009-09-27 19:14:12 -07007728 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04007729 computeHorizontalScrollOffset(),
7730 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04007731 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07007732 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08007733 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07007734 left = scrollX + (mPaddingLeft & inside);
7735 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
7736 bottom = top + size;
7737 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
7738 if (invalidate) {
7739 invalidate(left, top, right, bottom);
7740 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007741 }
7742
7743 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08007744 int size = scrollBar.getSize(true);
7745 if (size <= 0) {
7746 size = cache.scrollBarSize;
7747 }
7748
Mike Reede8853fc2009-09-04 14:01:48 -04007749 scrollBar.setParameters(computeVerticalScrollRange(),
7750 computeVerticalScrollOffset(),
7751 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08007752 switch (mVerticalScrollbarPosition) {
7753 default:
7754 case SCROLLBAR_POSITION_DEFAULT:
7755 case SCROLLBAR_POSITION_RIGHT:
7756 left = scrollX + width - size - (mUserPaddingRight & inside);
7757 break;
7758 case SCROLLBAR_POSITION_LEFT:
7759 left = scrollX + (mUserPaddingLeft & inside);
7760 break;
7761 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007762 top = scrollY + (mPaddingTop & inside);
7763 right = left + size;
7764 bottom = scrollY + height - (mUserPaddingBottom & inside);
7765 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
7766 if (invalidate) {
7767 invalidate(left, top, right, bottom);
7768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007769 }
7770 }
7771 }
7772 }
Romain Guy8506ab42009-06-11 17:35:47 -07007773
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007774 /**
Romain Guy8506ab42009-06-11 17:35:47 -07007775 * 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 -08007776 * FastScroller is visible.
7777 * @return whether to temporarily hide the vertical scrollbar
7778 * @hide
7779 */
7780 protected boolean isVerticalScrollBarHidden() {
7781 return false;
7782 }
7783
7784 /**
7785 * <p>Draw the horizontal scrollbar if
7786 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
7787 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 * @param canvas the canvas on which to draw the scrollbar
7789 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007790 *
7791 * @see #isHorizontalScrollBarEnabled()
7792 * @see #computeHorizontalScrollRange()
7793 * @see #computeHorizontalScrollExtent()
7794 * @see #computeHorizontalScrollOffset()
7795 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07007796 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007797 */
Romain Guy8fb95422010-08-17 18:38:51 -07007798 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
7799 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007800 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007801 scrollBar.draw(canvas);
7802 }
Mike Reede8853fc2009-09-04 14:01:48 -04007803
Mike Reed4d6fe5f2009-09-03 13:29:05 -04007804 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007805 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
7806 * returns true.</p>
7807 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007808 * @param canvas the canvas on which to draw the scrollbar
7809 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007810 *
7811 * @see #isVerticalScrollBarEnabled()
7812 * @see #computeVerticalScrollRange()
7813 * @see #computeVerticalScrollExtent()
7814 * @see #computeVerticalScrollOffset()
7815 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04007816 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007817 */
Romain Guy8fb95422010-08-17 18:38:51 -07007818 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
7819 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04007820 scrollBar.setBounds(l, t, r, b);
7821 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007822 }
7823
7824 /**
7825 * Implement this to do your drawing.
7826 *
7827 * @param canvas the canvas on which the background will be drawn
7828 */
7829 protected void onDraw(Canvas canvas) {
7830 }
7831
7832 /*
7833 * Caller is responsible for calling requestLayout if necessary.
7834 * (This allows addViewInLayout to not request a new layout.)
7835 */
7836 void assignParent(ViewParent parent) {
7837 if (mParent == null) {
7838 mParent = parent;
7839 } else if (parent == null) {
7840 mParent = null;
7841 } else {
7842 throw new RuntimeException("view " + this + " being added, but"
7843 + " it already has a parent");
7844 }
7845 }
7846
7847 /**
7848 * This is called when the view is attached to a window. At this point it
7849 * has a Surface and will start drawing. Note that this function is
7850 * guaranteed to be called before {@link #onDraw}, however it may be called
7851 * any time before the first onDraw -- including before or after
7852 * {@link #onMeasure}.
7853 *
7854 * @see #onDetachedFromWindow()
7855 */
7856 protected void onAttachedToWindow() {
7857 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
7858 mParent.requestTransparentRegion(this);
7859 }
Adam Powell8568c3a2010-04-19 14:26:11 -07007860 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
7861 initialAwakenScrollBars();
7862 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
7863 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08007864 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007865 }
7866
7867 /**
7868 * This is called when the view is detached from a window. At this point it
7869 * no longer has a surface for drawing.
7870 *
7871 * @see #onAttachedToWindow()
7872 */
7873 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08007874 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08007875
Romain Guya440b002010-02-24 15:57:54 -08007876 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05007877 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08007878 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08007879
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007880 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08007881
7882 if (mHardwareLayer != null) {
7883 mHardwareLayer.destroy();
7884 mHardwareLayer = null;
7885 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007886
Chet Haasedaf98e92011-01-10 14:10:36 -08007887 if (mDisplayList != null) {
7888 mDisplayList.invalidate();
7889 }
7890
Romain Guy8dd5b1e2011-01-14 17:28:51 -08007891 if (mAttachInfo != null) {
7892 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
7893 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
7894 }
7895
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08007896 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007897 }
7898
7899 /**
7900 * @return The number of times this view has been attached to a window
7901 */
7902 protected int getWindowAttachCount() {
7903 return mWindowAttachCount;
7904 }
7905
7906 /**
7907 * Retrieve a unique token identifying the window this view is attached to.
7908 * @return Return the window's token for use in
7909 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
7910 */
7911 public IBinder getWindowToken() {
7912 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
7913 }
7914
7915 /**
7916 * Retrieve a unique token identifying the top-level "real" window of
7917 * the window that this view is attached to. That is, this is like
7918 * {@link #getWindowToken}, except if the window this view in is a panel
7919 * window (attached to another containing window), then the token of
7920 * the containing window is returned instead.
7921 *
7922 * @return Returns the associated window token, either
7923 * {@link #getWindowToken()} or the containing window's token.
7924 */
7925 public IBinder getApplicationWindowToken() {
7926 AttachInfo ai = mAttachInfo;
7927 if (ai != null) {
7928 IBinder appWindowToken = ai.mPanelParentWindowToken;
7929 if (appWindowToken == null) {
7930 appWindowToken = ai.mWindowToken;
7931 }
7932 return appWindowToken;
7933 }
7934 return null;
7935 }
7936
7937 /**
7938 * Retrieve private session object this view hierarchy is using to
7939 * communicate with the window manager.
7940 * @return the session object to communicate with the window manager
7941 */
7942 /*package*/ IWindowSession getWindowSession() {
7943 return mAttachInfo != null ? mAttachInfo.mSession : null;
7944 }
7945
7946 /**
7947 * @param info the {@link android.view.View.AttachInfo} to associated with
7948 * this view
7949 */
7950 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
7951 //System.out.println("Attached! " + this);
7952 mAttachInfo = info;
7953 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08007954 // We will need to evaluate the drawable state at least once.
7955 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 if (mFloatingTreeObserver != null) {
7957 info.mTreeObserver.merge(mFloatingTreeObserver);
7958 mFloatingTreeObserver = null;
7959 }
7960 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
7961 mAttachInfo.mScrollContainers.add(this);
7962 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
7963 }
7964 performCollectViewAttributes(visibility);
7965 onAttachedToWindow();
7966 int vis = info.mWindowVisibility;
7967 if (vis != GONE) {
7968 onWindowVisibilityChanged(vis);
7969 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08007970 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
7971 // If nobody has evaluated the drawable state yet, then do it now.
7972 refreshDrawableState();
7973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007974 }
7975
7976 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007977 AttachInfo info = mAttachInfo;
7978 if (info != null) {
7979 int vis = info.mWindowVisibility;
7980 if (vis != GONE) {
7981 onWindowVisibilityChanged(GONE);
7982 }
7983 }
7984
7985 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08007986
7987 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 mAttachInfo.mScrollContainers.remove(this);
7989 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
7990 }
Romain Guy01d5edc2011-01-28 11:28:53 -08007991
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992 mAttachInfo = null;
7993 }
7994
7995 /**
7996 * Store this view hierarchy's frozen state into the given container.
7997 *
7998 * @param container The SparseArray in which to save the view's state.
7999 *
8000 * @see #restoreHierarchyState
8001 * @see #dispatchSaveInstanceState
8002 * @see #onSaveInstanceState
8003 */
8004 public void saveHierarchyState(SparseArray<Parcelable> container) {
8005 dispatchSaveInstanceState(container);
8006 }
8007
8008 /**
8009 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8010 * May be overridden to modify how freezing happens to a view's children; for example, some
8011 * views may want to not store state for their children.
8012 *
8013 * @param container The SparseArray in which to save the view's state.
8014 *
8015 * @see #dispatchRestoreInstanceState
8016 * @see #saveHierarchyState
8017 * @see #onSaveInstanceState
8018 */
8019 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8020 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8021 mPrivateFlags &= ~SAVE_STATE_CALLED;
8022 Parcelable state = onSaveInstanceState();
8023 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8024 throw new IllegalStateException(
8025 "Derived class did not call super.onSaveInstanceState()");
8026 }
8027 if (state != null) {
8028 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8029 // + ": " + state);
8030 container.put(mID, state);
8031 }
8032 }
8033 }
8034
8035 /**
8036 * Hook allowing a view to generate a representation of its internal state
8037 * that can later be used to create a new instance with that same state.
8038 * This state should only contain information that is not persistent or can
8039 * not be reconstructed later. For example, you will never store your
8040 * current position on screen because that will be computed again when a
8041 * new instance of the view is placed in its view hierarchy.
8042 * <p>
8043 * Some examples of things you may store here: the current cursor position
8044 * in a text view (but usually not the text itself since that is stored in a
8045 * content provider or other persistent storage), the currently selected
8046 * item in a list view.
8047 *
8048 * @return Returns a Parcelable object containing the view's current dynamic
8049 * state, or null if there is nothing interesting to save. The
8050 * default implementation returns null.
8051 * @see #onRestoreInstanceState
8052 * @see #saveHierarchyState
8053 * @see #dispatchSaveInstanceState
8054 * @see #setSaveEnabled(boolean)
8055 */
8056 protected Parcelable onSaveInstanceState() {
8057 mPrivateFlags |= SAVE_STATE_CALLED;
8058 return BaseSavedState.EMPTY_STATE;
8059 }
8060
8061 /**
8062 * Restore this view hierarchy's frozen state from the given container.
8063 *
8064 * @param container The SparseArray which holds previously frozen states.
8065 *
8066 * @see #saveHierarchyState
8067 * @see #dispatchRestoreInstanceState
8068 * @see #onRestoreInstanceState
8069 */
8070 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8071 dispatchRestoreInstanceState(container);
8072 }
8073
8074 /**
8075 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8076 * children. May be overridden to modify how restoreing happens to a view's children; for
8077 * example, some views may want to not store state for their children.
8078 *
8079 * @param container The SparseArray which holds previously saved state.
8080 *
8081 * @see #dispatchSaveInstanceState
8082 * @see #restoreHierarchyState
8083 * @see #onRestoreInstanceState
8084 */
8085 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8086 if (mID != NO_ID) {
8087 Parcelable state = container.get(mID);
8088 if (state != null) {
8089 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8090 // + ": " + state);
8091 mPrivateFlags &= ~SAVE_STATE_CALLED;
8092 onRestoreInstanceState(state);
8093 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8094 throw new IllegalStateException(
8095 "Derived class did not call super.onRestoreInstanceState()");
8096 }
8097 }
8098 }
8099 }
8100
8101 /**
8102 * Hook allowing a view to re-apply a representation of its internal state that had previously
8103 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8104 * null state.
8105 *
8106 * @param state The frozen state that had previously been returned by
8107 * {@link #onSaveInstanceState}.
8108 *
8109 * @see #onSaveInstanceState
8110 * @see #restoreHierarchyState
8111 * @see #dispatchRestoreInstanceState
8112 */
8113 protected void onRestoreInstanceState(Parcelable state) {
8114 mPrivateFlags |= SAVE_STATE_CALLED;
8115 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008116 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8117 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008118 + "when two views of different type have the same id in the same hierarchy. "
8119 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008120 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008121 }
8122 }
8123
8124 /**
8125 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8126 *
8127 * @return the drawing start time in milliseconds
8128 */
8129 public long getDrawingTime() {
8130 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8131 }
8132
8133 /**
8134 * <p>Enables or disables the duplication of the parent's state into this view. When
8135 * duplication is enabled, this view gets its drawable state from its parent rather
8136 * than from its own internal properties.</p>
8137 *
8138 * <p>Note: in the current implementation, setting this property to true after the
8139 * view was added to a ViewGroup might have no effect at all. This property should
8140 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8141 *
8142 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8143 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008144 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008145 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8146 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008147 *
8148 * @param enabled True to enable duplication of the parent's drawable state, false
8149 * to disable it.
8150 *
8151 * @see #getDrawableState()
8152 * @see #isDuplicateParentStateEnabled()
8153 */
8154 public void setDuplicateParentStateEnabled(boolean enabled) {
8155 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8156 }
8157
8158 /**
8159 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8160 *
8161 * @return True if this view's drawable state is duplicated from the parent,
8162 * false otherwise
8163 *
8164 * @see #getDrawableState()
8165 * @see #setDuplicateParentStateEnabled(boolean)
8166 */
8167 public boolean isDuplicateParentStateEnabled() {
8168 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8169 }
8170
8171 /**
Romain Guy171c5922011-01-06 10:04:23 -08008172 * <p>Specifies the type of layer backing this view. The layer can be
8173 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8174 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008175 *
Romain Guy171c5922011-01-06 10:04:23 -08008176 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8177 * instance that controls how the layer is composed on screen. The following
8178 * properties of the paint are taken into account when composing the layer:</p>
8179 * <ul>
8180 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8181 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8182 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8183 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008184 *
Romain Guy171c5922011-01-06 10:04:23 -08008185 * <p>If this view has an alpha value set to < 1.0 by calling
8186 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8187 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8188 * equivalent to setting a hardware layer on this view and providing a paint with
8189 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008190 *
Romain Guy171c5922011-01-06 10:04:23 -08008191 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8192 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8193 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008194 *
Romain Guy171c5922011-01-06 10:04:23 -08008195 * @param layerType The ype of layer to use with this view, must be one of
8196 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8197 * {@link #LAYER_TYPE_HARDWARE}
8198 * @param paint The paint used to compose the layer. This argument is optional
8199 * and can be null. It is ignored when the layer type is
8200 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008201 *
8202 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008203 * @see #LAYER_TYPE_NONE
8204 * @see #LAYER_TYPE_SOFTWARE
8205 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008206 * @see #setAlpha(float)
8207 *
Romain Guy171c5922011-01-06 10:04:23 -08008208 * @attr ref android.R.styleable#View_layerType
8209 */
8210 public void setLayerType(int layerType, Paint paint) {
8211 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008212 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008213 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8214 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008215
Romain Guyd6cd5722011-01-17 14:42:41 -08008216 if (layerType == mLayerType) {
8217 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8218 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008219 invalidateParentCaches();
8220 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008221 }
8222 return;
8223 }
Romain Guy171c5922011-01-06 10:04:23 -08008224
8225 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008226 switch (mLayerType) {
8227 case LAYER_TYPE_SOFTWARE:
8228 if (mDrawingCache != null) {
8229 mDrawingCache.recycle();
8230 mDrawingCache = null;
8231 }
Joe Malin32736f02011-01-19 16:14:20 -08008232
Romain Guy6c319ca2011-01-11 14:29:25 -08008233 if (mUnscaledDrawingCache != null) {
8234 mUnscaledDrawingCache.recycle();
8235 mUnscaledDrawingCache = null;
8236 }
8237 break;
8238 case LAYER_TYPE_HARDWARE:
8239 if (mHardwareLayer != null) {
8240 mHardwareLayer.destroy();
8241 mHardwareLayer = null;
8242 }
8243 break;
8244 default:
8245 break;
Romain Guy171c5922011-01-06 10:04:23 -08008246 }
8247
8248 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008249 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8250 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8251 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008252
Romain Guy0fd89bf2011-01-26 15:41:30 -08008253 invalidateParentCaches();
8254 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008255 }
8256
8257 /**
8258 * Indicates what type of layer is currently associated with this view. By default
8259 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8260 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8261 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008262 *
Romain Guy171c5922011-01-06 10:04:23 -08008263 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8264 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008265 *
8266 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08008267 * @see #LAYER_TYPE_NONE
8268 * @see #LAYER_TYPE_SOFTWARE
8269 * @see #LAYER_TYPE_HARDWARE
8270 */
8271 public int getLayerType() {
8272 return mLayerType;
8273 }
Joe Malin32736f02011-01-19 16:14:20 -08008274
Romain Guy6c319ca2011-01-11 14:29:25 -08008275 /**
8276 * <p>Returns a hardware layer that can be used to draw this view again
8277 * without executing its draw method.</p>
8278 *
8279 * @return A HardwareLayer ready to render, or null if an error occurred.
8280 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008281 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008282 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8283 return null;
8284 }
8285
8286 final int width = mRight - mLeft;
8287 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008288
Romain Guy6c319ca2011-01-11 14:29:25 -08008289 if (width == 0 || height == 0) {
8290 return null;
8291 }
8292
8293 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8294 if (mHardwareLayer == null) {
8295 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8296 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008297 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008298 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8299 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008300 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008301 }
8302
Romain Guy5e7f7662011-01-24 22:35:56 -08008303 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8304 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8305 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008306 try {
8307 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008308 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008309 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008310
Romain Guy4f09f542011-01-26 22:41:43 -08008311 final int restoreCount = canvas.save();
8312
Romain Guy6c319ca2011-01-11 14:29:25 -08008313 computeScroll();
8314 canvas.translate(-mScrollX, -mScrollY);
8315
Romain Guy6c319ca2011-01-11 14:29:25 -08008316 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008317
Romain Guy6c319ca2011-01-11 14:29:25 -08008318 // Fast path for layouts with no backgrounds
8319 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8320 mPrivateFlags &= ~DIRTY_MASK;
8321 dispatchDraw(canvas);
8322 } else {
8323 draw(canvas);
8324 }
Joe Malin32736f02011-01-19 16:14:20 -08008325
Romain Guy6c319ca2011-01-11 14:29:25 -08008326 canvas.restoreToCount(restoreCount);
8327 } finally {
8328 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008329 mHardwareLayer.end(currentCanvas);
8330 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008331 }
8332 }
8333
8334 return mHardwareLayer;
8335 }
Romain Guy171c5922011-01-06 10:04:23 -08008336
8337 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008338 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8339 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8340 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8341 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8342 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8343 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008344 *
Romain Guy171c5922011-01-06 10:04:23 -08008345 * <p>Enabling the drawing cache is similar to
8346 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008347 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8348 * drawing cache has no effect on rendering because the system uses a different mechanism
8349 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8350 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8351 * for information on how to enable software and hardware layers.</p>
8352 *
8353 * <p>This API can be used to manually generate
8354 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8355 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008356 *
8357 * @param enabled true to enable the drawing cache, false otherwise
8358 *
8359 * @see #isDrawingCacheEnabled()
8360 * @see #getDrawingCache()
8361 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008362 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 */
8364 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008365 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008366 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8367 }
8368
8369 /**
8370 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8371 *
8372 * @return true if the drawing cache is enabled
8373 *
8374 * @see #setDrawingCacheEnabled(boolean)
8375 * @see #getDrawingCache()
8376 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008377 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 public boolean isDrawingCacheEnabled() {
8379 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8380 }
8381
8382 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008383 * Debugging utility which recursively outputs the dirty state of a view and its
8384 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008385 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008386 * @hide
8387 */
Romain Guy676b1732011-02-14 14:45:33 -08008388 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008389 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8390 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8391 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8392 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8393 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8394 if (clear) {
8395 mPrivateFlags &= clearMask;
8396 }
8397 if (this instanceof ViewGroup) {
8398 ViewGroup parent = (ViewGroup) this;
8399 final int count = parent.getChildCount();
8400 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008401 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008402 child.outputDirtyFlags(indent + " ", clear, clearMask);
8403 }
8404 }
8405 }
8406
8407 /**
8408 * This method is used by ViewGroup to cause its children to restore or recreate their
8409 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8410 * to recreate its own display list, which would happen if it went through the normal
8411 * draw/dispatchDraw mechanisms.
8412 *
8413 * @hide
8414 */
8415 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008416
8417 /**
8418 * A view that is not attached or hardware accelerated cannot create a display list.
8419 * This method checks these conditions and returns the appropriate result.
8420 *
8421 * @return true if view has the ability to create a display list, false otherwise.
8422 *
8423 * @hide
8424 */
8425 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008426 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008427 }
Joe Malin32736f02011-01-19 16:14:20 -08008428
Chet Haasedaf98e92011-01-10 14:10:36 -08008429 /**
Romain Guyb051e892010-09-28 19:09:36 -07008430 * <p>Returns a display list that can be used to draw this view again
8431 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008432 *
Romain Guyb051e892010-09-28 19:09:36 -07008433 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008434 *
8435 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008436 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008437 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008438 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008439 return null;
8440 }
8441
Chet Haasedaf98e92011-01-10 14:10:36 -08008442 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8443 mDisplayList == null || !mDisplayList.isValid() ||
8444 mRecreateDisplayList)) {
8445 // Don't need to recreate the display list, just need to tell our
8446 // children to restore/recreate theirs
8447 if (mDisplayList != null && mDisplayList.isValid() &&
8448 !mRecreateDisplayList) {
8449 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8450 mPrivateFlags &= ~DIRTY_MASK;
8451 dispatchGetDisplayList();
8452
8453 return mDisplayList;
8454 }
8455
8456 // If we got here, we're recreating it. Mark it as such to ensure that
8457 // we copy in child display lists into ours in drawChild()
8458 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008459 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008460 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8461 // If we're creating a new display list, make sure our parent gets invalidated
8462 // since they will need to recreate their display list to account for this
8463 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008464 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008465 }
Romain Guyb051e892010-09-28 19:09:36 -07008466
8467 final HardwareCanvas canvas = mDisplayList.start();
8468 try {
8469 int width = mRight - mLeft;
8470 int height = mBottom - mTop;
8471
8472 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008473 // The dirty rect should always be null for a display list
8474 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008475
8476 final int restoreCount = canvas.save();
8477
Chet Haasedaf98e92011-01-10 14:10:36 -08008478 computeScroll();
8479 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008480 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008481
Romain Guyb051e892010-09-28 19:09:36 -07008482 // Fast path for layouts with no backgrounds
8483 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8484 mPrivateFlags &= ~DIRTY_MASK;
8485 dispatchDraw(canvas);
8486 } else {
8487 draw(canvas);
8488 }
Joe Malin32736f02011-01-19 16:14:20 -08008489
Romain Guyb051e892010-09-28 19:09:36 -07008490 canvas.restoreToCount(restoreCount);
8491 } finally {
8492 canvas.onPostDraw();
8493
8494 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008495 }
Chet Haase77785f92011-01-25 23:22:09 -08008496 } else {
8497 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8498 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008499 }
8500
8501 return mDisplayList;
8502 }
8503
8504 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008505 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008506 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008507 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008508 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008509 * @see #getDrawingCache(boolean)
8510 */
8511 public Bitmap getDrawingCache() {
8512 return getDrawingCache(false);
8513 }
8514
8515 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008516 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8517 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8518 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8519 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8520 * request the drawing cache by calling this method and draw it on screen if the
8521 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008522 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008523 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8524 * this method will create a bitmap of the same size as this view. Because this bitmap
8525 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8526 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8527 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8528 * size than the view. This implies that your application must be able to handle this
8529 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008530 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008531 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8532 * the current density of the screen when the application is in compatibility
8533 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008534 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008535 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008536 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 * @see #setDrawingCacheEnabled(boolean)
8538 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008539 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008540 * @see #destroyDrawingCache()
8541 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008542 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008543 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
8544 return null;
8545 }
8546 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008547 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 }
Romain Guy02890fd2010-08-06 17:58:44 -07008549 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008550 }
8551
8552 /**
8553 * <p>Frees the resources used by the drawing cache. If you call
8554 * {@link #buildDrawingCache()} manually without calling
8555 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8556 * should cleanup the cache with this method afterwards.</p>
8557 *
8558 * @see #setDrawingCacheEnabled(boolean)
8559 * @see #buildDrawingCache()
8560 * @see #getDrawingCache()
8561 */
8562 public void destroyDrawingCache() {
8563 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008564 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008565 mDrawingCache = null;
8566 }
Romain Guyfbd8f692009-06-26 14:51:58 -07008567 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07008568 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07008569 mUnscaledDrawingCache = null;
8570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008571 }
8572
8573 /**
8574 * Setting a solid background color for the drawing cache's bitmaps will improve
8575 * perfromance and memory usage. Note, though that this should only be used if this
8576 * view will always be drawn on top of a solid color.
8577 *
8578 * @param color The background color to use for the drawing cache's bitmap
8579 *
8580 * @see #setDrawingCacheEnabled(boolean)
8581 * @see #buildDrawingCache()
8582 * @see #getDrawingCache()
8583 */
8584 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08008585 if (color != mDrawingCacheBackgroundColor) {
8586 mDrawingCacheBackgroundColor = color;
8587 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008589 }
8590
8591 /**
8592 * @see #setDrawingCacheBackgroundColor(int)
8593 *
8594 * @return The background color to used for the drawing cache's bitmap
8595 */
8596 public int getDrawingCacheBackgroundColor() {
8597 return mDrawingCacheBackgroundColor;
8598 }
8599
8600 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008601 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008602 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008603 * @see #buildDrawingCache(boolean)
8604 */
8605 public void buildDrawingCache() {
8606 buildDrawingCache(false);
8607 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008608
Romain Guyfbd8f692009-06-26 14:51:58 -07008609 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008610 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
8611 *
8612 * <p>If you call {@link #buildDrawingCache()} manually without calling
8613 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
8614 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008615 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008616 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8617 * this method will create a bitmap of the same size as this view. Because this bitmap
8618 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8619 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8620 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8621 * size than the view. This implies that your application must be able to handle this
8622 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008623 *
Romain Guy0d9275e2010-10-26 14:22:30 -07008624 * <p>You should avoid calling this method when hardware acceleration is enabled. If
8625 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08008626 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07008627 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008628 *
8629 * @see #getDrawingCache()
8630 * @see #destroyDrawingCache()
8631 */
Romain Guyfbd8f692009-06-26 14:51:58 -07008632 public void buildDrawingCache(boolean autoScale) {
8633 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07008634 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008635 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636
8637 if (ViewDebug.TRACE_HIERARCHY) {
8638 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
8639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008640
Romain Guy8506ab42009-06-11 17:35:47 -07008641 int width = mRight - mLeft;
8642 int height = mBottom - mTop;
8643
8644 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07008645 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07008646
Romain Guye1123222009-06-29 14:24:56 -07008647 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008648 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
8649 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07008650 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008651
8652 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07008653 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08008654 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008655
8656 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07008657 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08008658 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008659 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
8660 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08008661 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008662 return;
8663 }
8664
8665 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07008666 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008667
8668 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008669 Bitmap.Config quality;
8670 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08008671 // Never pick ARGB_4444 because it looks awful
8672 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008673 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
8674 case DRAWING_CACHE_QUALITY_AUTO:
8675 quality = Bitmap.Config.ARGB_8888;
8676 break;
8677 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08008678 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008679 break;
8680 case DRAWING_CACHE_QUALITY_HIGH:
8681 quality = Bitmap.Config.ARGB_8888;
8682 break;
8683 default:
8684 quality = Bitmap.Config.ARGB_8888;
8685 break;
8686 }
8687 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07008688 // Optimization for translucent windows
8689 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08008690 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008691 }
8692
8693 // Try to cleanup memory
8694 if (bitmap != null) bitmap.recycle();
8695
8696 try {
8697 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07008698 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07008699 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07008700 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008701 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07008702 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07008703 }
Adam Powell26153a32010-11-08 15:22:27 -08008704 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008705 } catch (OutOfMemoryError e) {
8706 // If there is not enough memory to create the bitmap cache, just
8707 // ignore the issue as bitmap caches are not required to draw the
8708 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07008709 if (autoScale) {
8710 mDrawingCache = null;
8711 } else {
8712 mUnscaledDrawingCache = null;
8713 }
Romain Guy0211a0a2011-02-14 16:34:59 -08008714 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008715 return;
8716 }
8717
8718 clear = drawingCacheBackgroundColor != 0;
8719 }
8720
8721 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008722 if (attachInfo != null) {
8723 canvas = attachInfo.mCanvas;
8724 if (canvas == null) {
8725 canvas = new Canvas();
8726 }
8727 canvas.setBitmap(bitmap);
8728 // Temporarily clobber the cached Canvas in case one of our children
8729 // is also using a drawing cache. Without this, the children would
8730 // steal the canvas by attaching their own bitmap to it and bad, bad
8731 // thing would happen (invisible views, corrupted drawings, etc.)
8732 attachInfo.mCanvas = null;
8733 } else {
8734 // This case should hopefully never or seldom happen
8735 canvas = new Canvas(bitmap);
8736 }
8737
8738 if (clear) {
8739 bitmap.eraseColor(drawingCacheBackgroundColor);
8740 }
8741
8742 computeScroll();
8743 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08008744
Romain Guye1123222009-06-29 14:24:56 -07008745 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07008746 final float scale = attachInfo.mApplicationScale;
8747 canvas.scale(scale, scale);
8748 }
Joe Malin32736f02011-01-19 16:14:20 -08008749
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 canvas.translate(-mScrollX, -mScrollY);
8751
Romain Guy5bcdff42009-05-14 21:27:18 -07008752 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08008753 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
8754 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07008755 mPrivateFlags |= DRAWING_CACHE_VALID;
8756 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008757
8758 // Fast path for layouts with no backgrounds
8759 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8760 if (ViewDebug.TRACE_HIERARCHY) {
8761 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8762 }
Romain Guy5bcdff42009-05-14 21:27:18 -07008763 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008764 dispatchDraw(canvas);
8765 } else {
8766 draw(canvas);
8767 }
8768
8769 canvas.restoreToCount(restoreCount);
8770
8771 if (attachInfo != null) {
8772 // Restore the cached Canvas for our siblings
8773 attachInfo.mCanvas = canvas;
8774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008775 }
8776 }
8777
8778 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008779 * Create a snapshot of the view into a bitmap. We should probably make
8780 * some form of this public, but should think about the API.
8781 */
Romain Guy223ff5c2010-03-02 17:07:47 -08008782 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008783 int width = mRight - mLeft;
8784 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008785
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008786 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07008787 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008788 width = (int) ((width * scale) + 0.5f);
8789 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08008790
Romain Guy8c11e312009-09-14 15:15:30 -07008791 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008792 if (bitmap == null) {
8793 throw new OutOfMemoryError();
8794 }
8795
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008796 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08008797
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008798 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008799 if (attachInfo != null) {
8800 canvas = attachInfo.mCanvas;
8801 if (canvas == null) {
8802 canvas = new Canvas();
8803 }
8804 canvas.setBitmap(bitmap);
8805 // Temporarily clobber the cached Canvas in case one of our children
8806 // is also using a drawing cache. Without this, the children would
8807 // steal the canvas by attaching their own bitmap to it and bad, bad
8808 // things would happen (invisible views, corrupted drawings, etc.)
8809 attachInfo.mCanvas = null;
8810 } else {
8811 // This case should hopefully never or seldom happen
8812 canvas = new Canvas(bitmap);
8813 }
8814
Romain Guy5bcdff42009-05-14 21:27:18 -07008815 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008816 bitmap.eraseColor(backgroundColor);
8817 }
8818
8819 computeScroll();
8820 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07008821 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008822 canvas.translate(-mScrollX, -mScrollY);
8823
Romain Guy5bcdff42009-05-14 21:27:18 -07008824 // Temporarily remove the dirty mask
8825 int flags = mPrivateFlags;
8826 mPrivateFlags &= ~DIRTY_MASK;
8827
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008828 // Fast path for layouts with no backgrounds
8829 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8830 dispatchDraw(canvas);
8831 } else {
8832 draw(canvas);
8833 }
8834
Romain Guy5bcdff42009-05-14 21:27:18 -07008835 mPrivateFlags = flags;
8836
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008837 canvas.restoreToCount(restoreCount);
8838
8839 if (attachInfo != null) {
8840 // Restore the cached Canvas for our siblings
8841 attachInfo.mCanvas = canvas;
8842 }
Romain Guy8506ab42009-06-11 17:35:47 -07008843
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07008844 return bitmap;
8845 }
8846
8847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 * Indicates whether this View is currently in edit mode. A View is usually
8849 * in edit mode when displayed within a developer tool. For instance, if
8850 * this View is being drawn by a visual user interface builder, this method
8851 * should return true.
8852 *
8853 * Subclasses should check the return value of this method to provide
8854 * different behaviors if their normal behavior might interfere with the
8855 * host environment. For instance: the class spawns a thread in its
8856 * constructor, the drawing code relies on device-specific features, etc.
8857 *
8858 * This method is usually checked in the drawing code of custom widgets.
8859 *
8860 * @return True if this View is in edit mode, false otherwise.
8861 */
8862 public boolean isInEditMode() {
8863 return false;
8864 }
8865
8866 /**
8867 * If the View draws content inside its padding and enables fading edges,
8868 * it needs to support padding offsets. Padding offsets are added to the
8869 * fading edges to extend the length of the fade so that it covers pixels
8870 * drawn inside the padding.
8871 *
8872 * Subclasses of this class should override this method if they need
8873 * to draw content inside the padding.
8874 *
8875 * @return True if padding offset must be applied, false otherwise.
8876 *
8877 * @see #getLeftPaddingOffset()
8878 * @see #getRightPaddingOffset()
8879 * @see #getTopPaddingOffset()
8880 * @see #getBottomPaddingOffset()
8881 *
8882 * @since CURRENT
8883 */
8884 protected boolean isPaddingOffsetRequired() {
8885 return false;
8886 }
8887
8888 /**
8889 * Amount by which to extend the left fading region. Called only when
8890 * {@link #isPaddingOffsetRequired()} returns true.
8891 *
8892 * @return The left padding offset in pixels.
8893 *
8894 * @see #isPaddingOffsetRequired()
8895 *
8896 * @since CURRENT
8897 */
8898 protected int getLeftPaddingOffset() {
8899 return 0;
8900 }
8901
8902 /**
8903 * Amount by which to extend the right fading region. Called only when
8904 * {@link #isPaddingOffsetRequired()} returns true.
8905 *
8906 * @return The right padding offset in pixels.
8907 *
8908 * @see #isPaddingOffsetRequired()
8909 *
8910 * @since CURRENT
8911 */
8912 protected int getRightPaddingOffset() {
8913 return 0;
8914 }
8915
8916 /**
8917 * Amount by which to extend the top fading region. Called only when
8918 * {@link #isPaddingOffsetRequired()} returns true.
8919 *
8920 * @return The top padding offset in pixels.
8921 *
8922 * @see #isPaddingOffsetRequired()
8923 *
8924 * @since CURRENT
8925 */
8926 protected int getTopPaddingOffset() {
8927 return 0;
8928 }
8929
8930 /**
8931 * Amount by which to extend the bottom fading region. Called only when
8932 * {@link #isPaddingOffsetRequired()} returns true.
8933 *
8934 * @return The bottom padding offset in pixels.
8935 *
8936 * @see #isPaddingOffsetRequired()
8937 *
8938 * @since CURRENT
8939 */
8940 protected int getBottomPaddingOffset() {
8941 return 0;
8942 }
8943
8944 /**
Romain Guy2bffd262010-09-12 17:40:02 -07008945 * <p>Indicates whether this view is attached to an hardware accelerated
8946 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008947 *
Romain Guy2bffd262010-09-12 17:40:02 -07008948 * <p>Even if this method returns true, it does not mean that every call
8949 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
8950 * accelerated {@link android.graphics.Canvas}. For instance, if this view
8951 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
8952 * window is hardware accelerated,
8953 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
8954 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008955 *
Romain Guy2bffd262010-09-12 17:40:02 -07008956 * @return True if the view is attached to a window and the window is
8957 * hardware accelerated; false in any other case.
8958 */
8959 public boolean isHardwareAccelerated() {
8960 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
8961 }
Joe Malin32736f02011-01-19 16:14:20 -08008962
Romain Guy2bffd262010-09-12 17:40:02 -07008963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008964 * Manually render this view (and all of its children) to the given Canvas.
8965 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08008966 * called. When implementing a view, implement {@link #onDraw} instead of
8967 * overriding this method. If you do need to override this method, call
8968 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 *
8970 * @param canvas The Canvas to which the View is rendered.
8971 */
8972 public void draw(Canvas canvas) {
8973 if (ViewDebug.TRACE_HIERARCHY) {
8974 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
8975 }
8976
Romain Guy5bcdff42009-05-14 21:27:18 -07008977 final int privateFlags = mPrivateFlags;
8978 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
8979 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
8980 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07008981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008982 /*
8983 * Draw traversal performs several drawing steps which must be executed
8984 * in the appropriate order:
8985 *
8986 * 1. Draw the background
8987 * 2. If necessary, save the canvas' layers to prepare for fading
8988 * 3. Draw view's content
8989 * 4. Draw children
8990 * 5. If necessary, draw the fading edges and restore layers
8991 * 6. Draw decorations (scrollbars for instance)
8992 */
8993
8994 // Step 1, draw the background, if needed
8995 int saveCount;
8996
Romain Guy24443ea2009-05-11 11:56:30 -07008997 if (!dirtyOpaque) {
8998 final Drawable background = mBGDrawable;
8999 if (background != null) {
9000 final int scrollX = mScrollX;
9001 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009002
Romain Guy24443ea2009-05-11 11:56:30 -07009003 if (mBackgroundSizeChanged) {
9004 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9005 mBackgroundSizeChanged = false;
9006 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009007
Romain Guy24443ea2009-05-11 11:56:30 -07009008 if ((scrollX | scrollY) == 0) {
9009 background.draw(canvas);
9010 } else {
9011 canvas.translate(scrollX, scrollY);
9012 background.draw(canvas);
9013 canvas.translate(-scrollX, -scrollY);
9014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009015 }
9016 }
9017
9018 // skip step 2 & 5 if possible (common case)
9019 final int viewFlags = mViewFlags;
9020 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9021 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9022 if (!verticalEdges && !horizontalEdges) {
9023 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009024 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009025
9026 // Step 4, draw the children
9027 dispatchDraw(canvas);
9028
9029 // Step 6, draw decorations (scrollbars)
9030 onDrawScrollBars(canvas);
9031
9032 // we're done...
9033 return;
9034 }
9035
9036 /*
9037 * Here we do the full fledged routine...
9038 * (this is an uncommon case where speed matters less,
9039 * this is why we repeat some of the tests that have been
9040 * done above)
9041 */
9042
9043 boolean drawTop = false;
9044 boolean drawBottom = false;
9045 boolean drawLeft = false;
9046 boolean drawRight = false;
9047
9048 float topFadeStrength = 0.0f;
9049 float bottomFadeStrength = 0.0f;
9050 float leftFadeStrength = 0.0f;
9051 float rightFadeStrength = 0.0f;
9052
9053 // Step 2, save the canvas' layers
9054 int paddingLeft = mPaddingLeft;
9055 int paddingTop = mPaddingTop;
9056
9057 final boolean offsetRequired = isPaddingOffsetRequired();
9058 if (offsetRequired) {
9059 paddingLeft += getLeftPaddingOffset();
9060 paddingTop += getTopPaddingOffset();
9061 }
9062
9063 int left = mScrollX + paddingLeft;
9064 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9065 int top = mScrollY + paddingTop;
9066 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9067
9068 if (offsetRequired) {
9069 right += getRightPaddingOffset();
9070 bottom += getBottomPaddingOffset();
9071 }
9072
9073 final ScrollabilityCache scrollabilityCache = mScrollCache;
9074 int length = scrollabilityCache.fadingEdgeLength;
9075
9076 // clip the fade length if top and bottom fades overlap
9077 // overlapping fades produce odd-looking artifacts
9078 if (verticalEdges && (top + length > bottom - length)) {
9079 length = (bottom - top) / 2;
9080 }
9081
9082 // also clip horizontal fades if necessary
9083 if (horizontalEdges && (left + length > right - length)) {
9084 length = (right - left) / 2;
9085 }
9086
9087 if (verticalEdges) {
9088 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009089 drawTop = topFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009090 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009091 drawBottom = bottomFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009092 }
9093
9094 if (horizontalEdges) {
9095 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009096 drawLeft = leftFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guydac5f9f2010-07-08 11:40:54 -07009098 drawRight = rightFadeStrength > 0.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009099 }
9100
9101 saveCount = canvas.getSaveCount();
9102
9103 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009104 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009105 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9106
9107 if (drawTop) {
9108 canvas.saveLayer(left, top, right, top + length, null, flags);
9109 }
9110
9111 if (drawBottom) {
9112 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9113 }
9114
9115 if (drawLeft) {
9116 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9117 }
9118
9119 if (drawRight) {
9120 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9121 }
9122 } else {
9123 scrollabilityCache.setFadeColor(solidColor);
9124 }
9125
9126 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009127 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009128
9129 // Step 4, draw the children
9130 dispatchDraw(canvas);
9131
9132 // Step 5, draw the fade effect and restore layers
9133 final Paint p = scrollabilityCache.paint;
9134 final Matrix matrix = scrollabilityCache.matrix;
9135 final Shader fade = scrollabilityCache.shader;
9136 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9137
9138 if (drawTop) {
9139 matrix.setScale(1, fadeHeight * topFadeStrength);
9140 matrix.postTranslate(left, top);
9141 fade.setLocalMatrix(matrix);
9142 canvas.drawRect(left, top, right, top + length, p);
9143 }
9144
9145 if (drawBottom) {
9146 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9147 matrix.postRotate(180);
9148 matrix.postTranslate(left, bottom);
9149 fade.setLocalMatrix(matrix);
9150 canvas.drawRect(left, bottom - length, right, bottom, p);
9151 }
9152
9153 if (drawLeft) {
9154 matrix.setScale(1, fadeHeight * leftFadeStrength);
9155 matrix.postRotate(-90);
9156 matrix.postTranslate(left, top);
9157 fade.setLocalMatrix(matrix);
9158 canvas.drawRect(left, top, left + length, bottom, p);
9159 }
9160
9161 if (drawRight) {
9162 matrix.setScale(1, fadeHeight * rightFadeStrength);
9163 matrix.postRotate(90);
9164 matrix.postTranslate(right, top);
9165 fade.setLocalMatrix(matrix);
9166 canvas.drawRect(right - length, top, right, bottom, p);
9167 }
9168
9169 canvas.restoreToCount(saveCount);
9170
9171 // Step 6, draw decorations (scrollbars)
9172 onDrawScrollBars(canvas);
9173 }
9174
9175 /**
9176 * Override this if your view is known to always be drawn on top of a solid color background,
9177 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9178 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9179 * should be set to 0xFF.
9180 *
9181 * @see #setVerticalFadingEdgeEnabled
9182 * @see #setHorizontalFadingEdgeEnabled
9183 *
9184 * @return The known solid color background for this view, or 0 if the color may vary
9185 */
9186 public int getSolidColor() {
9187 return 0;
9188 }
9189
9190 /**
9191 * Build a human readable string representation of the specified view flags.
9192 *
9193 * @param flags the view flags to convert to a string
9194 * @return a String representing the supplied flags
9195 */
9196 private static String printFlags(int flags) {
9197 String output = "";
9198 int numFlags = 0;
9199 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9200 output += "TAKES_FOCUS";
9201 numFlags++;
9202 }
9203
9204 switch (flags & VISIBILITY_MASK) {
9205 case INVISIBLE:
9206 if (numFlags > 0) {
9207 output += " ";
9208 }
9209 output += "INVISIBLE";
9210 // USELESS HERE numFlags++;
9211 break;
9212 case GONE:
9213 if (numFlags > 0) {
9214 output += " ";
9215 }
9216 output += "GONE";
9217 // USELESS HERE numFlags++;
9218 break;
9219 default:
9220 break;
9221 }
9222 return output;
9223 }
9224
9225 /**
9226 * Build a human readable string representation of the specified private
9227 * view flags.
9228 *
9229 * @param privateFlags the private view flags to convert to a string
9230 * @return a String representing the supplied flags
9231 */
9232 private static String printPrivateFlags(int privateFlags) {
9233 String output = "";
9234 int numFlags = 0;
9235
9236 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9237 output += "WANTS_FOCUS";
9238 numFlags++;
9239 }
9240
9241 if ((privateFlags & FOCUSED) == FOCUSED) {
9242 if (numFlags > 0) {
9243 output += " ";
9244 }
9245 output += "FOCUSED";
9246 numFlags++;
9247 }
9248
9249 if ((privateFlags & SELECTED) == SELECTED) {
9250 if (numFlags > 0) {
9251 output += " ";
9252 }
9253 output += "SELECTED";
9254 numFlags++;
9255 }
9256
9257 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9258 if (numFlags > 0) {
9259 output += " ";
9260 }
9261 output += "IS_ROOT_NAMESPACE";
9262 numFlags++;
9263 }
9264
9265 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9266 if (numFlags > 0) {
9267 output += " ";
9268 }
9269 output += "HAS_BOUNDS";
9270 numFlags++;
9271 }
9272
9273 if ((privateFlags & DRAWN) == DRAWN) {
9274 if (numFlags > 0) {
9275 output += " ";
9276 }
9277 output += "DRAWN";
9278 // USELESS HERE numFlags++;
9279 }
9280 return output;
9281 }
9282
9283 /**
9284 * <p>Indicates whether or not this view's layout will be requested during
9285 * the next hierarchy layout pass.</p>
9286 *
9287 * @return true if the layout will be forced during next layout pass
9288 */
9289 public boolean isLayoutRequested() {
9290 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9291 }
9292
9293 /**
9294 * Assign a size and position to a view and all of its
9295 * descendants
9296 *
9297 * <p>This is the second phase of the layout mechanism.
9298 * (The first is measuring). In this phase, each parent calls
9299 * layout on all of its children to position them.
9300 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009301 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009302 *
Chet Haase9c087442011-01-12 16:20:16 -08009303 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009304 * Derived classes with children should override
9305 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009306 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009307 *
9308 * @param l Left position, relative to parent
9309 * @param t Top position, relative to parent
9310 * @param r Right position, relative to parent
9311 * @param b Bottom position, relative to parent
9312 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009313 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009314 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009315 int oldL = mLeft;
9316 int oldT = mTop;
9317 int oldB = mBottom;
9318 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009319 boolean changed = setFrame(l, t, r, b);
9320 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9321 if (ViewDebug.TRACE_HIERARCHY) {
9322 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9323 }
9324
9325 onLayout(changed, l, t, r, b);
9326 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009327
9328 if (mOnLayoutChangeListeners != null) {
9329 ArrayList<OnLayoutChangeListener> listenersCopy =
9330 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9331 int numListeners = listenersCopy.size();
9332 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009333 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009334 }
9335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009336 }
9337 mPrivateFlags &= ~FORCE_LAYOUT;
9338 }
9339
9340 /**
9341 * Called from layout when this view should
9342 * assign a size and position to each of its children.
9343 *
9344 * Derived classes with children should override
9345 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009346 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009347 * @param changed This is a new size or position for this view
9348 * @param left Left position, relative to parent
9349 * @param top Top position, relative to parent
9350 * @param right Right position, relative to parent
9351 * @param bottom Bottom position, relative to parent
9352 */
9353 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9354 }
9355
9356 /**
9357 * Assign a size and position to this view.
9358 *
9359 * This is called from layout.
9360 *
9361 * @param left Left position, relative to parent
9362 * @param top Top position, relative to parent
9363 * @param right Right position, relative to parent
9364 * @param bottom Bottom position, relative to parent
9365 * @return true if the new size and position are different than the
9366 * previous ones
9367 * {@hide}
9368 */
9369 protected boolean setFrame(int left, int top, int right, int bottom) {
9370 boolean changed = false;
9371
9372 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009373 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009374 + right + "," + bottom + ")");
9375 }
9376
9377 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9378 changed = true;
9379
9380 // Remember our drawn bit
9381 int drawn = mPrivateFlags & DRAWN;
9382
9383 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009384 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009385
9386
9387 int oldWidth = mRight - mLeft;
9388 int oldHeight = mBottom - mTop;
9389
9390 mLeft = left;
9391 mTop = top;
9392 mRight = right;
9393 mBottom = bottom;
9394
9395 mPrivateFlags |= HAS_BOUNDS;
9396
9397 int newWidth = right - left;
9398 int newHeight = bottom - top;
9399
9400 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009401 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9402 // A change in dimension means an auto-centered pivot point changes, too
9403 mMatrixDirty = true;
9404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009405 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9406 }
9407
9408 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9409 // If we are visible, force the DRAWN bit to on so that
9410 // this invalidate will go through (at least to our parent).
9411 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009412 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 // the DRAWN bit.
9414 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009415 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009416 // parent display list may need to be recreated based on a change in the bounds
9417 // of any child
9418 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009419 }
9420
9421 // Reset drawn bit to original value (invalidate turns it off)
9422 mPrivateFlags |= drawn;
9423
9424 mBackgroundSizeChanged = true;
9425 }
9426 return changed;
9427 }
9428
9429 /**
9430 * Finalize inflating a view from XML. This is called as the last phase
9431 * of inflation, after all child views have been added.
9432 *
9433 * <p>Even if the subclass overrides onFinishInflate, they should always be
9434 * sure to call the super method, so that we get called.
9435 */
9436 protected void onFinishInflate() {
9437 }
9438
9439 /**
9440 * Returns the resources associated with this view.
9441 *
9442 * @return Resources object.
9443 */
9444 public Resources getResources() {
9445 return mResources;
9446 }
9447
9448 /**
9449 * Invalidates the specified Drawable.
9450 *
9451 * @param drawable the drawable to invalidate
9452 */
9453 public void invalidateDrawable(Drawable drawable) {
9454 if (verifyDrawable(drawable)) {
9455 final Rect dirty = drawable.getBounds();
9456 final int scrollX = mScrollX;
9457 final int scrollY = mScrollY;
9458
9459 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9460 dirty.right + scrollX, dirty.bottom + scrollY);
9461 }
9462 }
9463
9464 /**
9465 * Schedules an action on a drawable to occur at a specified time.
9466 *
9467 * @param who the recipient of the action
9468 * @param what the action to run on the drawable
9469 * @param when the time at which the action must occur. Uses the
9470 * {@link SystemClock#uptimeMillis} timebase.
9471 */
9472 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9473 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9474 mAttachInfo.mHandler.postAtTime(what, who, when);
9475 }
9476 }
9477
9478 /**
9479 * Cancels a scheduled action on a drawable.
9480 *
9481 * @param who the recipient of the action
9482 * @param what the action to cancel
9483 */
9484 public void unscheduleDrawable(Drawable who, Runnable what) {
9485 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9486 mAttachInfo.mHandler.removeCallbacks(what, who);
9487 }
9488 }
9489
9490 /**
9491 * Unschedule any events associated with the given Drawable. This can be
9492 * used when selecting a new Drawable into a view, so that the previous
9493 * one is completely unscheduled.
9494 *
9495 * @param who The Drawable to unschedule.
9496 *
9497 * @see #drawableStateChanged
9498 */
9499 public void unscheduleDrawable(Drawable who) {
9500 if (mAttachInfo != null) {
9501 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9502 }
9503 }
9504
9505 /**
9506 * If your view subclass is displaying its own Drawable objects, it should
9507 * override this function and return true for any Drawable it is
9508 * displaying. This allows animations for those drawables to be
9509 * scheduled.
9510 *
9511 * <p>Be sure to call through to the super class when overriding this
9512 * function.
9513 *
9514 * @param who The Drawable to verify. Return true if it is one you are
9515 * displaying, else return the result of calling through to the
9516 * super class.
9517 *
9518 * @return boolean If true than the Drawable is being displayed in the
9519 * view; else false and it is not allowed to animate.
9520 *
9521 * @see #unscheduleDrawable
9522 * @see #drawableStateChanged
9523 */
9524 protected boolean verifyDrawable(Drawable who) {
9525 return who == mBGDrawable;
9526 }
9527
9528 /**
9529 * This function is called whenever the state of the view changes in such
9530 * a way that it impacts the state of drawables being shown.
9531 *
9532 * <p>Be sure to call through to the superclass when overriding this
9533 * function.
9534 *
9535 * @see Drawable#setState
9536 */
9537 protected void drawableStateChanged() {
9538 Drawable d = mBGDrawable;
9539 if (d != null && d.isStateful()) {
9540 d.setState(getDrawableState());
9541 }
9542 }
9543
9544 /**
9545 * Call this to force a view to update its drawable state. This will cause
9546 * drawableStateChanged to be called on this view. Views that are interested
9547 * in the new state should call getDrawableState.
9548 *
9549 * @see #drawableStateChanged
9550 * @see #getDrawableState
9551 */
9552 public void refreshDrawableState() {
9553 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
9554 drawableStateChanged();
9555
9556 ViewParent parent = mParent;
9557 if (parent != null) {
9558 parent.childDrawableStateChanged(this);
9559 }
9560 }
9561
9562 /**
9563 * Return an array of resource IDs of the drawable states representing the
9564 * current state of the view.
9565 *
9566 * @return The current drawable state
9567 *
9568 * @see Drawable#setState
9569 * @see #drawableStateChanged
9570 * @see #onCreateDrawableState
9571 */
9572 public final int[] getDrawableState() {
9573 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
9574 return mDrawableState;
9575 } else {
9576 mDrawableState = onCreateDrawableState(0);
9577 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
9578 return mDrawableState;
9579 }
9580 }
9581
9582 /**
9583 * Generate the new {@link android.graphics.drawable.Drawable} state for
9584 * this view. This is called by the view
9585 * system when the cached Drawable state is determined to be invalid. To
9586 * retrieve the current state, you should use {@link #getDrawableState}.
9587 *
9588 * @param extraSpace if non-zero, this is the number of extra entries you
9589 * would like in the returned array in which you can place your own
9590 * states.
9591 *
9592 * @return Returns an array holding the current {@link Drawable} state of
9593 * the view.
9594 *
9595 * @see #mergeDrawableStates
9596 */
9597 protected int[] onCreateDrawableState(int extraSpace) {
9598 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
9599 mParent instanceof View) {
9600 return ((View) mParent).onCreateDrawableState(extraSpace);
9601 }
9602
9603 int[] drawableState;
9604
9605 int privateFlags = mPrivateFlags;
9606
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009607 int viewStateIndex = 0;
9608 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
9609 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
9610 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -07009611 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009612 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
9613 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009614 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
9615 // This is set if HW acceleration is requested, even if the current
9616 // process doesn't allow it. This is just to allow app preview
9617 // windows to better match their app.
9618 viewStateIndex |= VIEW_STATE_ACCELERATED;
9619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009620
9621 drawableState = VIEW_STATE_SETS[viewStateIndex];
9622
9623 //noinspection ConstantIfStatement
9624 if (false) {
9625 Log.i("View", "drawableStateIndex=" + viewStateIndex);
9626 Log.i("View", toString()
9627 + " pressed=" + ((privateFlags & PRESSED) != 0)
9628 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
9629 + " fo=" + hasFocus()
9630 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009631 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009632 + ": " + Arrays.toString(drawableState));
9633 }
9634
9635 if (extraSpace == 0) {
9636 return drawableState;
9637 }
9638
9639 final int[] fullState;
9640 if (drawableState != null) {
9641 fullState = new int[drawableState.length + extraSpace];
9642 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
9643 } else {
9644 fullState = new int[extraSpace];
9645 }
9646
9647 return fullState;
9648 }
9649
9650 /**
9651 * Merge your own state values in <var>additionalState</var> into the base
9652 * state values <var>baseState</var> that were returned by
9653 * {@link #onCreateDrawableState}.
9654 *
9655 * @param baseState The base state values returned by
9656 * {@link #onCreateDrawableState}, which will be modified to also hold your
9657 * own additional state values.
9658 *
9659 * @param additionalState The additional state values you would like
9660 * added to <var>baseState</var>; this array is not modified.
9661 *
9662 * @return As a convenience, the <var>baseState</var> array you originally
9663 * passed into the function is returned.
9664 *
9665 * @see #onCreateDrawableState
9666 */
9667 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
9668 final int N = baseState.length;
9669 int i = N - 1;
9670 while (i >= 0 && baseState[i] == 0) {
9671 i--;
9672 }
9673 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
9674 return baseState;
9675 }
9676
9677 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -07009678 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
9679 * on all Drawable objects associated with this view.
9680 */
9681 public void jumpDrawablesToCurrentState() {
9682 if (mBGDrawable != null) {
9683 mBGDrawable.jumpToCurrentState();
9684 }
9685 }
9686
9687 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009688 * Sets the background color for this view.
9689 * @param color the color of the background
9690 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009691 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -07009693 if (mBGDrawable instanceof ColorDrawable) {
9694 ((ColorDrawable) mBGDrawable).setColor(color);
9695 } else {
9696 setBackgroundDrawable(new ColorDrawable(color));
9697 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 }
9699
9700 /**
9701 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -07009702 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009703 * @param resid The identifier of the resource.
9704 * @attr ref android.R.styleable#View_background
9705 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +00009706 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009707 public void setBackgroundResource(int resid) {
9708 if (resid != 0 && resid == mBackgroundResource) {
9709 return;
9710 }
9711
9712 Drawable d= null;
9713 if (resid != 0) {
9714 d = mResources.getDrawable(resid);
9715 }
9716 setBackgroundDrawable(d);
9717
9718 mBackgroundResource = resid;
9719 }
9720
9721 /**
9722 * Set the background to a given Drawable, or remove the background. If the
9723 * background has padding, this View's padding is set to the background's
9724 * padding. However, when a background is removed, this View's padding isn't
9725 * touched. If setting the padding is desired, please use
9726 * {@link #setPadding(int, int, int, int)}.
9727 *
9728 * @param d The Drawable to use as the background, or null to remove the
9729 * background
9730 */
9731 public void setBackgroundDrawable(Drawable d) {
9732 boolean requestLayout = false;
9733
9734 mBackgroundResource = 0;
9735
9736 /*
9737 * Regardless of whether we're setting a new background or not, we want
9738 * to clear the previous drawable.
9739 */
9740 if (mBGDrawable != null) {
9741 mBGDrawable.setCallback(null);
9742 unscheduleDrawable(mBGDrawable);
9743 }
9744
9745 if (d != null) {
9746 Rect padding = sThreadLocal.get();
9747 if (padding == null) {
9748 padding = new Rect();
9749 sThreadLocal.set(padding);
9750 }
9751 if (d.getPadding(padding)) {
9752 setPadding(padding.left, padding.top, padding.right, padding.bottom);
9753 }
9754
9755 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
9756 // if it has a different minimum size, we should layout again
9757 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
9758 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
9759 requestLayout = true;
9760 }
9761
9762 d.setCallback(this);
9763 if (d.isStateful()) {
9764 d.setState(getDrawableState());
9765 }
9766 d.setVisible(getVisibility() == VISIBLE, false);
9767 mBGDrawable = d;
9768
9769 if ((mPrivateFlags & SKIP_DRAW) != 0) {
9770 mPrivateFlags &= ~SKIP_DRAW;
9771 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
9772 requestLayout = true;
9773 }
9774 } else {
9775 /* Remove the background */
9776 mBGDrawable = null;
9777
9778 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
9779 /*
9780 * This view ONLY drew the background before and we're removing
9781 * the background, so now it won't draw anything
9782 * (hence we SKIP_DRAW)
9783 */
9784 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
9785 mPrivateFlags |= SKIP_DRAW;
9786 }
9787
9788 /*
9789 * When the background is set, we try to apply its padding to this
9790 * View. When the background is removed, we don't touch this View's
9791 * padding. This is noted in the Javadocs. Hence, we don't need to
9792 * requestLayout(), the invalidate() below is sufficient.
9793 */
9794
9795 // The old background's minimum size could have affected this
9796 // View's layout, so let's requestLayout
9797 requestLayout = true;
9798 }
9799
Romain Guy8f1344f52009-05-15 16:03:59 -07009800 computeOpaqueFlags();
9801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009802 if (requestLayout) {
9803 requestLayout();
9804 }
9805
9806 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009807 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009808 }
9809
9810 /**
9811 * Gets the background drawable
9812 * @return The drawable used as the background for this view, if any.
9813 */
9814 public Drawable getBackground() {
9815 return mBGDrawable;
9816 }
9817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009818 /**
9819 * Sets the padding. The view may add on the space required to display
9820 * the scrollbars, depending on the style and visibility of the scrollbars.
9821 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
9822 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
9823 * from the values set in this call.
9824 *
9825 * @attr ref android.R.styleable#View_padding
9826 * @attr ref android.R.styleable#View_paddingBottom
9827 * @attr ref android.R.styleable#View_paddingLeft
9828 * @attr ref android.R.styleable#View_paddingRight
9829 * @attr ref android.R.styleable#View_paddingTop
9830 * @param left the left padding in pixels
9831 * @param top the top padding in pixels
9832 * @param right the right padding in pixels
9833 * @param bottom the bottom padding in pixels
9834 */
9835 public void setPadding(int left, int top, int right, int bottom) {
9836 boolean changed = false;
9837
Adam Powell20232d02010-12-08 21:08:53 -08009838 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009839 mUserPaddingRight = right;
9840 mUserPaddingBottom = bottom;
9841
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009842 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -07009843
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009844 // Common case is there are no scroll bars.
9845 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009846 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -08009847 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
9848 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009849 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -08009850 switch (mVerticalScrollbarPosition) {
9851 case SCROLLBAR_POSITION_DEFAULT:
9852 case SCROLLBAR_POSITION_RIGHT:
9853 right += offset;
9854 break;
9855 case SCROLLBAR_POSITION_LEFT:
9856 left += offset;
9857 break;
9858 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009859 }
Adam Powell20232d02010-12-08 21:08:53 -08009860 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009861 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
9862 ? 0 : getHorizontalScrollbarHeight();
9863 }
9864 }
Romain Guy8506ab42009-06-11 17:35:47 -07009865
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009866 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009867 changed = true;
9868 mPaddingLeft = left;
9869 }
9870 if (mPaddingTop != top) {
9871 changed = true;
9872 mPaddingTop = top;
9873 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009874 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009875 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009876 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009877 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009878 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009879 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07009880 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009881 }
9882
9883 if (changed) {
9884 requestLayout();
9885 }
9886 }
9887
9888 /**
9889 * Returns the top padding of this view.
9890 *
9891 * @return the top padding in pixels
9892 */
9893 public int getPaddingTop() {
9894 return mPaddingTop;
9895 }
9896
9897 /**
9898 * Returns the bottom padding of this view. If there are inset and enabled
9899 * scrollbars, this value may include the space required to display the
9900 * scrollbars as well.
9901 *
9902 * @return the bottom padding in pixels
9903 */
9904 public int getPaddingBottom() {
9905 return mPaddingBottom;
9906 }
9907
9908 /**
9909 * Returns the left padding of this view. If there are inset and enabled
9910 * scrollbars, this value may include the space required to display the
9911 * scrollbars as well.
9912 *
9913 * @return the left padding in pixels
9914 */
9915 public int getPaddingLeft() {
9916 return mPaddingLeft;
9917 }
9918
9919 /**
9920 * Returns the right padding of this view. If there are inset and enabled
9921 * scrollbars, this value may include the space required to display the
9922 * scrollbars as well.
9923 *
9924 * @return the right padding in pixels
9925 */
9926 public int getPaddingRight() {
9927 return mPaddingRight;
9928 }
9929
9930 /**
9931 * Changes the selection state of this view. A view can be selected or not.
9932 * Note that selection is not the same as focus. Views are typically
9933 * selected in the context of an AdapterView like ListView or GridView;
9934 * the selected view is the view that is highlighted.
9935 *
9936 * @param selected true if the view must be selected, false otherwise
9937 */
9938 public void setSelected(boolean selected) {
9939 if (((mPrivateFlags & SELECTED) != 0) != selected) {
9940 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -07009941 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -08009942 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 refreshDrawableState();
9944 dispatchSetSelected(selected);
9945 }
9946 }
9947
9948 /**
9949 * Dispatch setSelected to all of this View's children.
9950 *
9951 * @see #setSelected(boolean)
9952 *
9953 * @param selected The new selected state
9954 */
9955 protected void dispatchSetSelected(boolean selected) {
9956 }
9957
9958 /**
9959 * Indicates the selection state of this view.
9960 *
9961 * @return true if the view is selected, false otherwise
9962 */
9963 @ViewDebug.ExportedProperty
9964 public boolean isSelected() {
9965 return (mPrivateFlags & SELECTED) != 0;
9966 }
9967
9968 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009969 * Changes the activated state of this view. A view can be activated or not.
9970 * Note that activation is not the same as selection. Selection is
9971 * a transient property, representing the view (hierarchy) the user is
9972 * currently interacting with. Activation is a longer-term state that the
9973 * user can move views in and out of. For example, in a list view with
9974 * single or multiple selection enabled, the views in the current selection
9975 * set are activated. (Um, yeah, we are deeply sorry about the terminology
9976 * here.) The activated state is propagated down to children of the view it
9977 * is set on.
9978 *
9979 * @param activated true if the view must be activated, false otherwise
9980 */
9981 public void setActivated(boolean activated) {
9982 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
9983 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -08009984 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009985 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -07009986 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07009987 }
9988 }
9989
9990 /**
9991 * Dispatch setActivated to all of this View's children.
9992 *
9993 * @see #setActivated(boolean)
9994 *
9995 * @param activated The new activated state
9996 */
9997 protected void dispatchSetActivated(boolean activated) {
9998 }
9999
10000 /**
10001 * Indicates the activation state of this view.
10002 *
10003 * @return true if the view is activated, false otherwise
10004 */
10005 @ViewDebug.ExportedProperty
10006 public boolean isActivated() {
10007 return (mPrivateFlags & ACTIVATED) != 0;
10008 }
10009
10010 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10012 * observer can be used to get notifications when global events, like
10013 * layout, happen.
10014 *
10015 * The returned ViewTreeObserver observer is not guaranteed to remain
10016 * valid for the lifetime of this View. If the caller of this method keeps
10017 * a long-lived reference to ViewTreeObserver, it should always check for
10018 * the return value of {@link ViewTreeObserver#isAlive()}.
10019 *
10020 * @return The ViewTreeObserver for this view's hierarchy.
10021 */
10022 public ViewTreeObserver getViewTreeObserver() {
10023 if (mAttachInfo != null) {
10024 return mAttachInfo.mTreeObserver;
10025 }
10026 if (mFloatingTreeObserver == null) {
10027 mFloatingTreeObserver = new ViewTreeObserver();
10028 }
10029 return mFloatingTreeObserver;
10030 }
10031
10032 /**
10033 * <p>Finds the topmost view in the current view hierarchy.</p>
10034 *
10035 * @return the topmost view containing this view
10036 */
10037 public View getRootView() {
10038 if (mAttachInfo != null) {
10039 final View v = mAttachInfo.mRootView;
10040 if (v != null) {
10041 return v;
10042 }
10043 }
Romain Guy8506ab42009-06-11 17:35:47 -070010044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 View parent = this;
10046
10047 while (parent.mParent != null && parent.mParent instanceof View) {
10048 parent = (View) parent.mParent;
10049 }
10050
10051 return parent;
10052 }
10053
10054 /**
10055 * <p>Computes the coordinates of this view on the screen. The argument
10056 * must be an array of two integers. After the method returns, the array
10057 * contains the x and y location in that order.</p>
10058 *
10059 * @param location an array of two integers in which to hold the coordinates
10060 */
10061 public void getLocationOnScreen(int[] location) {
10062 getLocationInWindow(location);
10063
10064 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010065 if (info != null) {
10066 location[0] += info.mWindowLeft;
10067 location[1] += info.mWindowTop;
10068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 }
10070
10071 /**
10072 * <p>Computes the coordinates of this view in its window. The argument
10073 * must be an array of two integers. After the method returns, the array
10074 * contains the x and y location in that order.</p>
10075 *
10076 * @param location an array of two integers in which to hold the coordinates
10077 */
10078 public void getLocationInWindow(int[] location) {
10079 if (location == null || location.length < 2) {
10080 throw new IllegalArgumentException("location must be an array of "
10081 + "two integers");
10082 }
10083
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010084 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10085 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010086
10087 ViewParent viewParent = mParent;
10088 while (viewParent instanceof View) {
10089 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010090 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10091 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010092 viewParent = view.mParent;
10093 }
Romain Guy8506ab42009-06-11 17:35:47 -070010094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010095 if (viewParent instanceof ViewRoot) {
10096 // *cough*
10097 final ViewRoot vr = (ViewRoot)viewParent;
10098 location[1] -= vr.mCurScrollY;
10099 }
10100 }
10101
10102 /**
10103 * {@hide}
10104 * @param id the id of the view to be found
10105 * @return the view of the specified id, null if cannot be found
10106 */
10107 protected View findViewTraversal(int id) {
10108 if (id == mID) {
10109 return this;
10110 }
10111 return null;
10112 }
10113
10114 /**
10115 * {@hide}
10116 * @param tag the tag of the view to be found
10117 * @return the view of specified tag, null if cannot be found
10118 */
10119 protected View findViewWithTagTraversal(Object tag) {
10120 if (tag != null && tag.equals(mTag)) {
10121 return this;
10122 }
10123 return null;
10124 }
10125
10126 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010127 * {@hide}
10128 * @param predicate The predicate to evaluate.
10129 * @return The first view that matches the predicate or null.
10130 */
10131 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10132 if (predicate.apply(this)) {
10133 return this;
10134 }
10135 return null;
10136 }
10137
10138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010139 * Look for a child view with the given id. If this view has the given
10140 * id, return this view.
10141 *
10142 * @param id The id to search for.
10143 * @return The view that has the given id in the hierarchy or null
10144 */
10145 public final View findViewById(int id) {
10146 if (id < 0) {
10147 return null;
10148 }
10149 return findViewTraversal(id);
10150 }
10151
10152 /**
10153 * Look for a child view with the given tag. If this view has the given
10154 * tag, return this view.
10155 *
10156 * @param tag The tag to search for, using "tag.equals(getTag())".
10157 * @return The View that has the given tag in the hierarchy or null
10158 */
10159 public final View findViewWithTag(Object tag) {
10160 if (tag == null) {
10161 return null;
10162 }
10163 return findViewWithTagTraversal(tag);
10164 }
10165
10166 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010167 * {@hide}
10168 * Look for a child view that matches the specified predicate.
10169 * If this view matches the predicate, return this view.
10170 *
10171 * @param predicate The predicate to evaluate.
10172 * @return The first view that matches the predicate or null.
10173 */
10174 public final View findViewByPredicate(Predicate<View> predicate) {
10175 return findViewByPredicateTraversal(predicate);
10176 }
10177
10178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010179 * Sets the identifier for this view. The identifier does not have to be
10180 * unique in this view's hierarchy. The identifier should be a positive
10181 * number.
10182 *
10183 * @see #NO_ID
10184 * @see #getId
10185 * @see #findViewById
10186 *
10187 * @param id a number used to identify the view
10188 *
10189 * @attr ref android.R.styleable#View_id
10190 */
10191 public void setId(int id) {
10192 mID = id;
10193 }
10194
10195 /**
10196 * {@hide}
10197 *
10198 * @param isRoot true if the view belongs to the root namespace, false
10199 * otherwise
10200 */
10201 public void setIsRootNamespace(boolean isRoot) {
10202 if (isRoot) {
10203 mPrivateFlags |= IS_ROOT_NAMESPACE;
10204 } else {
10205 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10206 }
10207 }
10208
10209 /**
10210 * {@hide}
10211 *
10212 * @return true if the view belongs to the root namespace, false otherwise
10213 */
10214 public boolean isRootNamespace() {
10215 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10216 }
10217
10218 /**
10219 * Returns this view's identifier.
10220 *
10221 * @return a positive integer used to identify the view or {@link #NO_ID}
10222 * if the view has no ID
10223 *
10224 * @see #setId
10225 * @see #findViewById
10226 * @attr ref android.R.styleable#View_id
10227 */
10228 @ViewDebug.CapturedViewProperty
10229 public int getId() {
10230 return mID;
10231 }
10232
10233 /**
10234 * Returns this view's tag.
10235 *
10236 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010237 *
10238 * @see #setTag(Object)
10239 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010240 */
10241 @ViewDebug.ExportedProperty
10242 public Object getTag() {
10243 return mTag;
10244 }
10245
10246 /**
10247 * Sets the tag associated with this view. A tag can be used to mark
10248 * a view in its hierarchy and does not have to be unique within the
10249 * hierarchy. Tags can also be used to store data within a view without
10250 * resorting to another data structure.
10251 *
10252 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010253 *
10254 * @see #getTag()
10255 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010256 */
10257 public void setTag(final Object tag) {
10258 mTag = tag;
10259 }
10260
10261 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010262 * Returns the tag associated with this view and the specified key.
10263 *
10264 * @param key The key identifying the tag
10265 *
10266 * @return the Object stored in this view as a tag
10267 *
10268 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010269 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010270 */
10271 public Object getTag(int key) {
10272 SparseArray<Object> tags = null;
10273 synchronized (sTagsLock) {
10274 if (sTags != null) {
10275 tags = sTags.get(this);
10276 }
10277 }
10278
10279 if (tags != null) return tags.get(key);
10280 return null;
10281 }
10282
10283 /**
10284 * Sets a tag associated with this view and a key. A tag can be used
10285 * to mark a view in its hierarchy and does not have to be unique within
10286 * the hierarchy. Tags can also be used to store data within a view
10287 * without resorting to another data structure.
10288 *
10289 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010290 * application to ensure it is unique (see the <a
10291 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10292 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010293 * the Android framework or not associated with any package will cause
10294 * an {@link IllegalArgumentException} to be thrown.
10295 *
10296 * @param key The key identifying the tag
10297 * @param tag An Object to tag the view with
10298 *
10299 * @throws IllegalArgumentException If they specified key is not valid
10300 *
10301 * @see #setTag(Object)
10302 * @see #getTag(int)
10303 */
10304 public void setTag(int key, final Object tag) {
10305 // If the package id is 0x00 or 0x01, it's either an undefined package
10306 // or a framework id
10307 if ((key >>> 24) < 2) {
10308 throw new IllegalArgumentException("The key must be an application-specific "
10309 + "resource id.");
10310 }
10311
10312 setTagInternal(this, key, tag);
10313 }
10314
10315 /**
10316 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10317 * framework id.
10318 *
10319 * @hide
10320 */
10321 public void setTagInternal(int key, Object tag) {
10322 if ((key >>> 24) != 0x1) {
10323 throw new IllegalArgumentException("The key must be a framework-specific "
10324 + "resource id.");
10325 }
10326
Romain Guy8506ab42009-06-11 17:35:47 -070010327 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010328 }
10329
10330 private static void setTagInternal(View view, int key, Object tag) {
10331 SparseArray<Object> tags = null;
10332 synchronized (sTagsLock) {
10333 if (sTags == null) {
10334 sTags = new WeakHashMap<View, SparseArray<Object>>();
10335 } else {
10336 tags = sTags.get(view);
10337 }
10338 }
10339
10340 if (tags == null) {
10341 tags = new SparseArray<Object>(2);
10342 synchronized (sTagsLock) {
10343 sTags.put(view, tags);
10344 }
10345 }
10346
10347 tags.put(key, tag);
10348 }
10349
10350 /**
Romain Guy13922e02009-05-12 17:56:14 -070010351 * @param consistency The type of consistency. See ViewDebug for more information.
10352 *
10353 * @hide
10354 */
10355 protected boolean dispatchConsistencyCheck(int consistency) {
10356 return onConsistencyCheck(consistency);
10357 }
10358
10359 /**
10360 * Method that subclasses should implement to check their consistency. The type of
10361 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010362 *
Romain Guy13922e02009-05-12 17:56:14 -070010363 * @param consistency The type of consistency. See ViewDebug for more information.
10364 *
10365 * @throws IllegalStateException if the view is in an inconsistent state.
10366 *
10367 * @hide
10368 */
10369 protected boolean onConsistencyCheck(int consistency) {
10370 boolean result = true;
10371
10372 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10373 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10374
10375 if (checkLayout) {
10376 if (getParent() == null) {
10377 result = false;
10378 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10379 "View " + this + " does not have a parent.");
10380 }
10381
10382 if (mAttachInfo == null) {
10383 result = false;
10384 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10385 "View " + this + " is not attached to a window.");
10386 }
10387 }
10388
10389 if (checkDrawing) {
10390 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10391 // from their draw() method
10392
10393 if ((mPrivateFlags & DRAWN) != DRAWN &&
10394 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10395 result = false;
10396 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10397 "View " + this + " was invalidated but its drawing cache is valid.");
10398 }
10399 }
10400
10401 return result;
10402 }
10403
10404 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 * Prints information about this view in the log output, with the tag
10406 * {@link #VIEW_LOG_TAG}.
10407 *
10408 * @hide
10409 */
10410 public void debug() {
10411 debug(0);
10412 }
10413
10414 /**
10415 * Prints information about this view in the log output, with the tag
10416 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10417 * indentation defined by the <code>depth</code>.
10418 *
10419 * @param depth the indentation level
10420 *
10421 * @hide
10422 */
10423 protected void debug(int depth) {
10424 String output = debugIndent(depth - 1);
10425
10426 output += "+ " + this;
10427 int id = getId();
10428 if (id != -1) {
10429 output += " (id=" + id + ")";
10430 }
10431 Object tag = getTag();
10432 if (tag != null) {
10433 output += " (tag=" + tag + ")";
10434 }
10435 Log.d(VIEW_LOG_TAG, output);
10436
10437 if ((mPrivateFlags & FOCUSED) != 0) {
10438 output = debugIndent(depth) + " FOCUSED";
10439 Log.d(VIEW_LOG_TAG, output);
10440 }
10441
10442 output = debugIndent(depth);
10443 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10444 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10445 + "} ";
10446 Log.d(VIEW_LOG_TAG, output);
10447
10448 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10449 || mPaddingBottom != 0) {
10450 output = debugIndent(depth);
10451 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10452 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10453 Log.d(VIEW_LOG_TAG, output);
10454 }
10455
10456 output = debugIndent(depth);
10457 output += "mMeasureWidth=" + mMeasuredWidth +
10458 " mMeasureHeight=" + mMeasuredHeight;
10459 Log.d(VIEW_LOG_TAG, output);
10460
10461 output = debugIndent(depth);
10462 if (mLayoutParams == null) {
10463 output += "BAD! no layout params";
10464 } else {
10465 output = mLayoutParams.debug(output);
10466 }
10467 Log.d(VIEW_LOG_TAG, output);
10468
10469 output = debugIndent(depth);
10470 output += "flags={";
10471 output += View.printFlags(mViewFlags);
10472 output += "}";
10473 Log.d(VIEW_LOG_TAG, output);
10474
10475 output = debugIndent(depth);
10476 output += "privateFlags={";
10477 output += View.printPrivateFlags(mPrivateFlags);
10478 output += "}";
10479 Log.d(VIEW_LOG_TAG, output);
10480 }
10481
10482 /**
10483 * Creates an string of whitespaces used for indentation.
10484 *
10485 * @param depth the indentation level
10486 * @return a String containing (depth * 2 + 3) * 2 white spaces
10487 *
10488 * @hide
10489 */
10490 protected static String debugIndent(int depth) {
10491 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10492 for (int i = 0; i < (depth * 2) + 3; i++) {
10493 spaces.append(' ').append(' ');
10494 }
10495 return spaces.toString();
10496 }
10497
10498 /**
10499 * <p>Return the offset of the widget's text baseline from the widget's top
10500 * boundary. If this widget does not support baseline alignment, this
10501 * method returns -1. </p>
10502 *
10503 * @return the offset of the baseline within the widget's bounds or -1
10504 * if baseline alignment is not supported
10505 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010506 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010507 public int getBaseline() {
10508 return -1;
10509 }
10510
10511 /**
10512 * Call this when something has changed which has invalidated the
10513 * layout of this view. This will schedule a layout pass of the view
10514 * tree.
10515 */
10516 public void requestLayout() {
10517 if (ViewDebug.TRACE_HIERARCHY) {
10518 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10519 }
10520
10521 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010522 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010523
10524 if (mParent != null && !mParent.isLayoutRequested()) {
10525 mParent.requestLayout();
10526 }
10527 }
10528
10529 /**
10530 * Forces this view to be laid out during the next layout pass.
10531 * This method does not call requestLayout() or forceLayout()
10532 * on the parent.
10533 */
10534 public void forceLayout() {
10535 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010536 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 }
10538
10539 /**
10540 * <p>
10541 * This is called to find out how big a view should be. The parent
10542 * supplies constraint information in the width and height parameters.
10543 * </p>
10544 *
10545 * <p>
10546 * The actual mesurement work of a view is performed in
10547 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
10548 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
10549 * </p>
10550 *
10551 *
10552 * @param widthMeasureSpec Horizontal space requirements as imposed by the
10553 * parent
10554 * @param heightMeasureSpec Vertical space requirements as imposed by the
10555 * parent
10556 *
10557 * @see #onMeasure(int, int)
10558 */
10559 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
10560 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
10561 widthMeasureSpec != mOldWidthMeasureSpec ||
10562 heightMeasureSpec != mOldHeightMeasureSpec) {
10563
10564 // first clears the measured dimension flag
10565 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
10566
10567 if (ViewDebug.TRACE_HIERARCHY) {
10568 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
10569 }
10570
10571 // measure ourselves, this should set the measured dimension flag back
10572 onMeasure(widthMeasureSpec, heightMeasureSpec);
10573
10574 // flag not set, setMeasuredDimension() was not invoked, we raise
10575 // an exception to warn the developer
10576 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
10577 throw new IllegalStateException("onMeasure() did not set the"
10578 + " measured dimension by calling"
10579 + " setMeasuredDimension()");
10580 }
10581
10582 mPrivateFlags |= LAYOUT_REQUIRED;
10583 }
10584
10585 mOldWidthMeasureSpec = widthMeasureSpec;
10586 mOldHeightMeasureSpec = heightMeasureSpec;
10587 }
10588
10589 /**
10590 * <p>
10591 * Measure the view and its content to determine the measured width and the
10592 * measured height. This method is invoked by {@link #measure(int, int)} and
10593 * should be overriden by subclasses to provide accurate and efficient
10594 * measurement of their contents.
10595 * </p>
10596 *
10597 * <p>
10598 * <strong>CONTRACT:</strong> When overriding this method, you
10599 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
10600 * measured width and height of this view. Failure to do so will trigger an
10601 * <code>IllegalStateException</code>, thrown by
10602 * {@link #measure(int, int)}. Calling the superclass'
10603 * {@link #onMeasure(int, int)} is a valid use.
10604 * </p>
10605 *
10606 * <p>
10607 * The base class implementation of measure defaults to the background size,
10608 * unless a larger size is allowed by the MeasureSpec. Subclasses should
10609 * override {@link #onMeasure(int, int)} to provide better measurements of
10610 * their content.
10611 * </p>
10612 *
10613 * <p>
10614 * If this method is overridden, it is the subclass's responsibility to make
10615 * sure the measured height and width are at least the view's minimum height
10616 * and width ({@link #getSuggestedMinimumHeight()} and
10617 * {@link #getSuggestedMinimumWidth()}).
10618 * </p>
10619 *
10620 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
10621 * The requirements are encoded with
10622 * {@link android.view.View.MeasureSpec}.
10623 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
10624 * The requirements are encoded with
10625 * {@link android.view.View.MeasureSpec}.
10626 *
10627 * @see #getMeasuredWidth()
10628 * @see #getMeasuredHeight()
10629 * @see #setMeasuredDimension(int, int)
10630 * @see #getSuggestedMinimumHeight()
10631 * @see #getSuggestedMinimumWidth()
10632 * @see android.view.View.MeasureSpec#getMode(int)
10633 * @see android.view.View.MeasureSpec#getSize(int)
10634 */
10635 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
10636 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
10637 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
10638 }
10639
10640 /**
10641 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
10642 * measured width and measured height. Failing to do so will trigger an
10643 * exception at measurement time.</p>
10644 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080010645 * @param measuredWidth The measured width of this view. May be a complex
10646 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10647 * {@link #MEASURED_STATE_TOO_SMALL}.
10648 * @param measuredHeight The measured height of this view. May be a complex
10649 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
10650 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 */
10652 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
10653 mMeasuredWidth = measuredWidth;
10654 mMeasuredHeight = measuredHeight;
10655
10656 mPrivateFlags |= MEASURED_DIMENSION_SET;
10657 }
10658
10659 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080010660 * Merge two states as returned by {@link #getMeasuredState()}.
10661 * @param curState The current state as returned from a view or the result
10662 * of combining multiple views.
10663 * @param newState The new view state to combine.
10664 * @return Returns a new integer reflecting the combination of the two
10665 * states.
10666 */
10667 public static int combineMeasuredStates(int curState, int newState) {
10668 return curState | newState;
10669 }
10670
10671 /**
10672 * Version of {@link #resolveSizeAndState(int, int, int)}
10673 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
10674 */
10675 public static int resolveSize(int size, int measureSpec) {
10676 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
10677 }
10678
10679 /**
10680 * Utility to reconcile a desired size and state, with constraints imposed
10681 * by a MeasureSpec. Will take the desired size, unless a different size
10682 * is imposed by the constraints. The returned value is a compound integer,
10683 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
10684 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
10685 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686 *
10687 * @param size How big the view wants to be
10688 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080010689 * @return Size information bit mask as defined by
10690 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080010692 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010693 int result = size;
10694 int specMode = MeasureSpec.getMode(measureSpec);
10695 int specSize = MeasureSpec.getSize(measureSpec);
10696 switch (specMode) {
10697 case MeasureSpec.UNSPECIFIED:
10698 result = size;
10699 break;
10700 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080010701 if (specSize < size) {
10702 result = specSize | MEASURED_STATE_TOO_SMALL;
10703 } else {
10704 result = size;
10705 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010706 break;
10707 case MeasureSpec.EXACTLY:
10708 result = specSize;
10709 break;
10710 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080010711 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010712 }
10713
10714 /**
10715 * Utility to return a default size. Uses the supplied size if the
10716 * MeasureSpec imposed no contraints. Will get larger if allowed
10717 * by the MeasureSpec.
10718 *
10719 * @param size Default size for this view
10720 * @param measureSpec Constraints imposed by the parent
10721 * @return The size this view should be.
10722 */
10723 public static int getDefaultSize(int size, int measureSpec) {
10724 int result = size;
10725 int specMode = MeasureSpec.getMode(measureSpec);
10726 int specSize = MeasureSpec.getSize(measureSpec);
10727
10728 switch (specMode) {
10729 case MeasureSpec.UNSPECIFIED:
10730 result = size;
10731 break;
10732 case MeasureSpec.AT_MOST:
10733 case MeasureSpec.EXACTLY:
10734 result = specSize;
10735 break;
10736 }
10737 return result;
10738 }
10739
10740 /**
10741 * Returns the suggested minimum height that the view should use. This
10742 * returns the maximum of the view's minimum height
10743 * and the background's minimum height
10744 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
10745 * <p>
10746 * When being used in {@link #onMeasure(int, int)}, the caller should still
10747 * ensure the returned height is within the requirements of the parent.
10748 *
10749 * @return The suggested minimum height of the view.
10750 */
10751 protected int getSuggestedMinimumHeight() {
10752 int suggestedMinHeight = mMinHeight;
10753
10754 if (mBGDrawable != null) {
10755 final int bgMinHeight = mBGDrawable.getMinimumHeight();
10756 if (suggestedMinHeight < bgMinHeight) {
10757 suggestedMinHeight = bgMinHeight;
10758 }
10759 }
10760
10761 return suggestedMinHeight;
10762 }
10763
10764 /**
10765 * Returns the suggested minimum width that the view should use. This
10766 * returns the maximum of the view's minimum width)
10767 * and the background's minimum width
10768 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
10769 * <p>
10770 * When being used in {@link #onMeasure(int, int)}, the caller should still
10771 * ensure the returned width is within the requirements of the parent.
10772 *
10773 * @return The suggested minimum width of the view.
10774 */
10775 protected int getSuggestedMinimumWidth() {
10776 int suggestedMinWidth = mMinWidth;
10777
10778 if (mBGDrawable != null) {
10779 final int bgMinWidth = mBGDrawable.getMinimumWidth();
10780 if (suggestedMinWidth < bgMinWidth) {
10781 suggestedMinWidth = bgMinWidth;
10782 }
10783 }
10784
10785 return suggestedMinWidth;
10786 }
10787
10788 /**
10789 * Sets the minimum height of the view. It is not guaranteed the view will
10790 * be able to achieve this minimum height (for example, if its parent layout
10791 * constrains it with less available height).
10792 *
10793 * @param minHeight The minimum height the view will try to be.
10794 */
10795 public void setMinimumHeight(int minHeight) {
10796 mMinHeight = minHeight;
10797 }
10798
10799 /**
10800 * Sets the minimum width of the view. It is not guaranteed the view will
10801 * be able to achieve this minimum width (for example, if its parent layout
10802 * constrains it with less available width).
10803 *
10804 * @param minWidth The minimum width the view will try to be.
10805 */
10806 public void setMinimumWidth(int minWidth) {
10807 mMinWidth = minWidth;
10808 }
10809
10810 /**
10811 * Get the animation currently associated with this view.
10812 *
10813 * @return The animation that is currently playing or
10814 * scheduled to play for this view.
10815 */
10816 public Animation getAnimation() {
10817 return mCurrentAnimation;
10818 }
10819
10820 /**
10821 * Start the specified animation now.
10822 *
10823 * @param animation the animation to start now
10824 */
10825 public void startAnimation(Animation animation) {
10826 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
10827 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010828 invalidateParentCaches();
10829 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010830 }
10831
10832 /**
10833 * Cancels any animations for this view.
10834 */
10835 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080010836 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080010837 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080010838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010839 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010840 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010841 }
10842
10843 /**
10844 * Sets the next animation to play for this view.
10845 * If you want the animation to play immediately, use
10846 * startAnimation. This method provides allows fine-grained
10847 * control over the start time and invalidation, but you
10848 * must make sure that 1) the animation has a start time set, and
10849 * 2) the view will be invalidated when the animation is supposed to
10850 * start.
10851 *
10852 * @param animation The next animation, or null.
10853 */
10854 public void setAnimation(Animation animation) {
10855 mCurrentAnimation = animation;
10856 if (animation != null) {
10857 animation.reset();
10858 }
10859 }
10860
10861 /**
10862 * Invoked by a parent ViewGroup to notify the start of the animation
10863 * currently associated with this view. If you override this method,
10864 * always call super.onAnimationStart();
10865 *
10866 * @see #setAnimation(android.view.animation.Animation)
10867 * @see #getAnimation()
10868 */
10869 protected void onAnimationStart() {
10870 mPrivateFlags |= ANIMATION_STARTED;
10871 }
10872
10873 /**
10874 * Invoked by a parent ViewGroup to notify the end of the animation
10875 * currently associated with this view. If you override this method,
10876 * always call super.onAnimationEnd();
10877 *
10878 * @see #setAnimation(android.view.animation.Animation)
10879 * @see #getAnimation()
10880 */
10881 protected void onAnimationEnd() {
10882 mPrivateFlags &= ~ANIMATION_STARTED;
10883 }
10884
10885 /**
10886 * Invoked if there is a Transform that involves alpha. Subclass that can
10887 * draw themselves with the specified alpha should return true, and then
10888 * respect that alpha when their onDraw() is called. If this returns false
10889 * then the view may be redirected to draw into an offscreen buffer to
10890 * fulfill the request, which will look fine, but may be slower than if the
10891 * subclass handles it internally. The default implementation returns false.
10892 *
10893 * @param alpha The alpha (0..255) to apply to the view's drawing
10894 * @return true if the view can draw with the specified alpha.
10895 */
10896 protected boolean onSetAlpha(int alpha) {
10897 return false;
10898 }
10899
10900 /**
10901 * This is used by the RootView to perform an optimization when
10902 * the view hierarchy contains one or several SurfaceView.
10903 * SurfaceView is always considered transparent, but its children are not,
10904 * therefore all View objects remove themselves from the global transparent
10905 * region (passed as a parameter to this function).
10906 *
10907 * @param region The transparent region for this ViewRoot (window).
10908 *
10909 * @return Returns true if the effective visibility of the view at this
10910 * point is opaque, regardless of the transparent region; returns false
10911 * if it is possible for underlying windows to be seen behind the view.
10912 *
10913 * {@hide}
10914 */
10915 public boolean gatherTransparentRegion(Region region) {
10916 final AttachInfo attachInfo = mAttachInfo;
10917 if (region != null && attachInfo != null) {
10918 final int pflags = mPrivateFlags;
10919 if ((pflags & SKIP_DRAW) == 0) {
10920 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
10921 // remove it from the transparent region.
10922 final int[] location = attachInfo.mTransparentLocation;
10923 getLocationInWindow(location);
10924 region.op(location[0], location[1], location[0] + mRight - mLeft,
10925 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
10926 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
10927 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
10928 // exists, so we remove the background drawable's non-transparent
10929 // parts from this transparent region.
10930 applyDrawableToTransparentRegion(mBGDrawable, region);
10931 }
10932 }
10933 return true;
10934 }
10935
10936 /**
10937 * Play a sound effect for this view.
10938 *
10939 * <p>The framework will play sound effects for some built in actions, such as
10940 * clicking, but you may wish to play these effects in your widget,
10941 * for instance, for internal navigation.
10942 *
10943 * <p>The sound effect will only be played if sound effects are enabled by the user, and
10944 * {@link #isSoundEffectsEnabled()} is true.
10945 *
10946 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
10947 */
10948 public void playSoundEffect(int soundConstant) {
10949 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
10950 return;
10951 }
10952 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
10953 }
10954
10955 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010956 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070010957 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010958 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010959 *
10960 * <p>The framework will provide haptic feedback for some built in actions,
10961 * such as long presses, but you may wish to provide feedback for your
10962 * own widget.
10963 *
10964 * <p>The feedback will only be performed if
10965 * {@link #isHapticFeedbackEnabled()} is true.
10966 *
10967 * @param feedbackConstant One of the constants defined in
10968 * {@link HapticFeedbackConstants}
10969 */
10970 public boolean performHapticFeedback(int feedbackConstant) {
10971 return performHapticFeedback(feedbackConstant, 0);
10972 }
10973
10974 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010975 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070010976 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070010977 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010978 *
10979 * @param feedbackConstant One of the constants defined in
10980 * {@link HapticFeedbackConstants}
10981 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
10982 */
10983 public boolean performHapticFeedback(int feedbackConstant, int flags) {
10984 if (mAttachInfo == null) {
10985 return false;
10986 }
Romain Guyf607bdc2010-09-10 19:20:06 -070010987 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070010988 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010989 && !isHapticFeedbackEnabled()) {
10990 return false;
10991 }
Romain Guy812ccbe2010-06-01 14:07:24 -070010992 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
10993 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994 }
10995
10996 /**
Joe Onorato664644d2011-01-23 17:53:23 -080010997 * Request that the visibility of the status bar be changed.
10998 */
10999 public void setSystemUiVisibility(int visibility) {
11000 if (visibility != mSystemUiVisibility) {
11001 mSystemUiVisibility = visibility;
11002 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11003 mParent.recomputeViewAttributes(this);
11004 }
11005 }
11006 }
11007
11008 /**
11009 * Returns the status bar visibility that this view has requested.
11010 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011011 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011012 return mSystemUiVisibility;
11013 }
11014
11015 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11016 mOnSystemUiVisibilityChangeListener = l;
11017 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11018 mParent.recomputeViewAttributes(this);
11019 }
11020 }
11021
11022 /**
11023 */
11024 public void dispatchSystemUiVisibilityChanged(int visibility) {
11025 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011026 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011027 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011028 }
11029 }
11030
11031 /**
Joe Malin32736f02011-01-19 16:14:20 -080011032 * Creates an image that the system displays during the drag and drop
11033 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11034 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11035 * appearance as the given View. The default also positions the center of the drag shadow
11036 * directly under the touch point. If no View is provided (the constructor with no parameters
11037 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11038 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11039 * default is an invisible drag shadow.
11040 * <p>
11041 * You are not required to use the View you provide to the constructor as the basis of the
11042 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11043 * anything you want as the drag shadow.
11044 * </p>
11045 * <p>
11046 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11047 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11048 * size and position of the drag shadow. It uses this data to construct a
11049 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11050 * so that your application can draw the shadow image in the Canvas.
11051 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011052 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011053 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011054 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011055
11056 /**
Joe Malin32736f02011-01-19 16:14:20 -080011057 * Constructs a shadow image builder based on a View. By default, the resulting drag
11058 * shadow will have the same appearance and dimensions as the View, with the touch point
11059 * over the center of the View.
11060 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011061 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011062 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011063 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011064 }
11065
Christopher Tate17ed60c2011-01-18 12:50:26 -080011066 /**
11067 * Construct a shadow builder object with no associated View. This
11068 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11069 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11070 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011071 * reference to any View object. If they are not overridden, then the result is an
11072 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011073 */
11074 public DragShadowBuilder() {
11075 mView = new WeakReference<View>(null);
11076 }
11077
11078 /**
11079 * Returns the View object that had been passed to the
11080 * {@link #View.DragShadowBuilder(View)}
11081 * constructor. If that View parameter was {@code null} or if the
11082 * {@link #View.DragShadowBuilder()}
11083 * constructor was used to instantiate the builder object, this method will return
11084 * null.
11085 *
11086 * @return The View object associate with this builder object.
11087 */
Chris Tate6b391282010-10-14 15:48:59 -070011088 final public View getView() {
11089 return mView.get();
11090 }
11091
Christopher Tate2c095f32010-10-04 14:13:40 -070011092 /**
Joe Malin32736f02011-01-19 16:14:20 -080011093 * Provides the metrics for the shadow image. These include the dimensions of
11094 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011095 * be centered under the touch location while dragging.
11096 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011097 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011098 * same as the dimensions of the View itself and centers the shadow under
11099 * the touch point.
11100 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011101 *
Joe Malin32736f02011-01-19 16:14:20 -080011102 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11103 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11104 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11105 * image.
11106 *
11107 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11108 * shadow image that should be underneath the touch point during the drag and drop
11109 * operation. Your application must set {@link android.graphics.Point#x} to the
11110 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011111 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011112 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011113 final View view = mView.get();
11114 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011115 shadowSize.set(view.getWidth(), view.getHeight());
11116 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011117 } else {
11118 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11119 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011120 }
11121
11122 /**
Joe Malin32736f02011-01-19 16:14:20 -080011123 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11124 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011125 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011126 *
Joe Malin32736f02011-01-19 16:14:20 -080011127 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011128 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011129 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011130 final View view = mView.get();
11131 if (view != null) {
11132 view.draw(canvas);
11133 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011134 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011135 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011136 }
11137 }
11138
11139 /**
Joe Malin32736f02011-01-19 16:14:20 -080011140 * Starts a drag and drop operation. When your application calls this method, it passes a
11141 * {@link android.view.View.DragShadowBuilder} object to the system. The
11142 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11143 * to get metrics for the drag shadow, and then calls the object's
11144 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11145 * <p>
11146 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11147 * drag events to all the View objects in your application that are currently visible. It does
11148 * this either by calling the View object's drag listener (an implementation of
11149 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11150 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11151 * Both are passed a {@link android.view.DragEvent} object that has a
11152 * {@link android.view.DragEvent#getAction()} value of
11153 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11154 * </p>
11155 * <p>
11156 * Your application can invoke startDrag() on any attached View object. The View object does not
11157 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11158 * be related to the View the user selected for dragging.
11159 * </p>
11160 * @param data A {@link android.content.ClipData} object pointing to the data to be
11161 * transferred by the drag and drop operation.
11162 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11163 * drag shadow.
11164 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11165 * drop operation. This Object is put into every DragEvent object sent by the system during the
11166 * current drag.
11167 * <p>
11168 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11169 * to the target Views. For example, it can contain flags that differentiate between a
11170 * a copy operation and a move operation.
11171 * </p>
11172 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11173 * so the parameter should be set to 0.
11174 * @return {@code true} if the method completes successfully, or
11175 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11176 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011177 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011178 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011179 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011180 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011181 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011182 }
11183 boolean okay = false;
11184
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011185 Point shadowSize = new Point();
11186 Point shadowTouchPoint = new Point();
11187 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011188
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011189 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11190 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11191 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011192 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011193
Chris Tatea32dcf72010-10-14 12:13:50 -070011194 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011195 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11196 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011197 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011198 Surface surface = new Surface();
11199 try {
11200 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011201 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011202 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011203 + " surface=" + surface);
11204 if (token != null) {
11205 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011206 try {
Chris Tate6b391282010-10-14 15:48:59 -070011207 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011208 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011209 } finally {
11210 surface.unlockCanvasAndPost(canvas);
11211 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011212
Christopher Tate407b4e92010-11-30 17:14:08 -080011213 final ViewRoot root = getViewRoot();
11214
11215 // Cache the local state object for delivery with DragEvents
11216 root.setLocalDragState(myLocalState);
11217
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011218 // repurpose 'shadowSize' for the last touch point
11219 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011220
Christopher Tatea53146c2010-09-07 11:57:52 -070011221 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011222 shadowSize.x, shadowSize.y,
11223 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011224 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011225 }
11226 } catch (Exception e) {
11227 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11228 surface.destroy();
11229 }
11230
11231 return okay;
11232 }
11233
Christopher Tatea53146c2010-09-07 11:57:52 -070011234 /**
Joe Malin32736f02011-01-19 16:14:20 -080011235 * Handles drag events sent by the system following a call to
11236 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11237 *<p>
11238 * When the system calls this method, it passes a
11239 * {@link android.view.DragEvent} object. A call to
11240 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11241 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11242 * operation.
11243 * @param event The {@link android.view.DragEvent} sent by the system.
11244 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11245 * in DragEvent, indicating the type of drag event represented by this object.
11246 * @return {@code true} if the method was successful, otherwise {@code false}.
11247 * <p>
11248 * The method should return {@code true} in response to an action type of
11249 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11250 * operation.
11251 * </p>
11252 * <p>
11253 * The method should also return {@code true} in response to an action type of
11254 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11255 * {@code false} if it didn't.
11256 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011257 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011258 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011259 return false;
11260 }
11261
11262 /**
Joe Malin32736f02011-01-19 16:14:20 -080011263 * Detects if this View is enabled and has a drag event listener.
11264 * If both are true, then it calls the drag event listener with the
11265 * {@link android.view.DragEvent} it received. If the drag event listener returns
11266 * {@code true}, then dispatchDragEvent() returns {@code true}.
11267 * <p>
11268 * For all other cases, the method calls the
11269 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11270 * method and returns its result.
11271 * </p>
11272 * <p>
11273 * This ensures that a drag event is always consumed, even if the View does not have a drag
11274 * event listener. However, if the View has a listener and the listener returns true, then
11275 * onDragEvent() is not called.
11276 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011277 */
11278 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011279 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011280 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11281 && mOnDragListener.onDrag(this, event)) {
11282 return true;
11283 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011284 return onDragEvent(event);
11285 }
11286
11287 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011288 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11289 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011290 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011291 */
11292 public void onCloseSystemDialogs(String reason) {
11293 }
Joe Malin32736f02011-01-19 16:14:20 -080011294
Dianne Hackbornffa42482009-09-23 22:20:11 -070011295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 * Given a Drawable whose bounds have been set to draw into this view,
11297 * update a Region being computed for {@link #gatherTransparentRegion} so
11298 * that any non-transparent parts of the Drawable are removed from the
11299 * given transparent region.
11300 *
11301 * @param dr The Drawable whose transparency is to be applied to the region.
11302 * @param region A Region holding the current transparency information,
11303 * where any parts of the region that are set are considered to be
11304 * transparent. On return, this region will be modified to have the
11305 * transparency information reduced by the corresponding parts of the
11306 * Drawable that are not transparent.
11307 * {@hide}
11308 */
11309 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11310 if (DBG) {
11311 Log.i("View", "Getting transparent region for: " + this);
11312 }
11313 final Region r = dr.getTransparentRegion();
11314 final Rect db = dr.getBounds();
11315 final AttachInfo attachInfo = mAttachInfo;
11316 if (r != null && attachInfo != null) {
11317 final int w = getRight()-getLeft();
11318 final int h = getBottom()-getTop();
11319 if (db.left > 0) {
11320 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11321 r.op(0, 0, db.left, h, Region.Op.UNION);
11322 }
11323 if (db.right < w) {
11324 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11325 r.op(db.right, 0, w, h, Region.Op.UNION);
11326 }
11327 if (db.top > 0) {
11328 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11329 r.op(0, 0, w, db.top, Region.Op.UNION);
11330 }
11331 if (db.bottom < h) {
11332 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11333 r.op(0, db.bottom, w, h, Region.Op.UNION);
11334 }
11335 final int[] location = attachInfo.mTransparentLocation;
11336 getLocationInWindow(location);
11337 r.translate(location[0], location[1]);
11338 region.op(r, Region.Op.INTERSECT);
11339 } else {
11340 region.op(db, Region.Op.DIFFERENCE);
11341 }
11342 }
11343
Adam Powelle14579b2009-12-16 18:39:52 -080011344 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011345 mHasPerformedLongPress = false;
11346
11347 if (mPendingCheckForLongPress == null) {
11348 mPendingCheckForLongPress = new CheckForLongPress();
11349 }
11350 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011351 postDelayed(mPendingCheckForLongPress,
11352 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011353 }
11354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011355 /**
11356 * Inflate a view from an XML resource. This convenience method wraps the {@link
11357 * LayoutInflater} class, which provides a full range of options for view inflation.
11358 *
11359 * @param context The Context object for your activity or application.
11360 * @param resource The resource ID to inflate
11361 * @param root A view group that will be the parent. Used to properly inflate the
11362 * layout_* parameters.
11363 * @see LayoutInflater
11364 */
11365 public static View inflate(Context context, int resource, ViewGroup root) {
11366 LayoutInflater factory = LayoutInflater.from(context);
11367 return factory.inflate(resource, root);
11368 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011369
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011370 /**
Adam Powell637d3372010-08-25 14:37:03 -070011371 * Scroll the view with standard behavior for scrolling beyond the normal
11372 * content boundaries. Views that call this method should override
11373 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11374 * results of an over-scroll operation.
11375 *
11376 * Views can use this method to handle any touch or fling-based scrolling.
11377 *
11378 * @param deltaX Change in X in pixels
11379 * @param deltaY Change in Y in pixels
11380 * @param scrollX Current X scroll value in pixels before applying deltaX
11381 * @param scrollY Current Y scroll value in pixels before applying deltaY
11382 * @param scrollRangeX Maximum content scroll range along the X axis
11383 * @param scrollRangeY Maximum content scroll range along the Y axis
11384 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11385 * along the X axis.
11386 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11387 * along the Y axis.
11388 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11389 * @return true if scrolling was clamped to an over-scroll boundary along either
11390 * axis, false otherwise.
11391 */
11392 protected boolean overScrollBy(int deltaX, int deltaY,
11393 int scrollX, int scrollY,
11394 int scrollRangeX, int scrollRangeY,
11395 int maxOverScrollX, int maxOverScrollY,
11396 boolean isTouchEvent) {
11397 final int overScrollMode = mOverScrollMode;
11398 final boolean canScrollHorizontal =
11399 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11400 final boolean canScrollVertical =
11401 computeVerticalScrollRange() > computeVerticalScrollExtent();
11402 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11403 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11404 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11405 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11406
11407 int newScrollX = scrollX + deltaX;
11408 if (!overScrollHorizontal) {
11409 maxOverScrollX = 0;
11410 }
11411
11412 int newScrollY = scrollY + deltaY;
11413 if (!overScrollVertical) {
11414 maxOverScrollY = 0;
11415 }
11416
11417 // Clamp values if at the limits and record
11418 final int left = -maxOverScrollX;
11419 final int right = maxOverScrollX + scrollRangeX;
11420 final int top = -maxOverScrollY;
11421 final int bottom = maxOverScrollY + scrollRangeY;
11422
11423 boolean clampedX = false;
11424 if (newScrollX > right) {
11425 newScrollX = right;
11426 clampedX = true;
11427 } else if (newScrollX < left) {
11428 newScrollX = left;
11429 clampedX = true;
11430 }
11431
11432 boolean clampedY = false;
11433 if (newScrollY > bottom) {
11434 newScrollY = bottom;
11435 clampedY = true;
11436 } else if (newScrollY < top) {
11437 newScrollY = top;
11438 clampedY = true;
11439 }
11440
11441 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11442
11443 return clampedX || clampedY;
11444 }
11445
11446 /**
11447 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11448 * respond to the results of an over-scroll operation.
11449 *
11450 * @param scrollX New X scroll value in pixels
11451 * @param scrollY New Y scroll value in pixels
11452 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11453 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11454 */
11455 protected void onOverScrolled(int scrollX, int scrollY,
11456 boolean clampedX, boolean clampedY) {
11457 // Intentionally empty.
11458 }
11459
11460 /**
11461 * Returns the over-scroll mode for this view. The result will be
11462 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11463 * (allow over-scrolling only if the view content is larger than the container),
11464 * or {@link #OVER_SCROLL_NEVER}.
11465 *
11466 * @return This view's over-scroll mode.
11467 */
11468 public int getOverScrollMode() {
11469 return mOverScrollMode;
11470 }
11471
11472 /**
11473 * Set the over-scroll mode for this view. Valid over-scroll modes are
11474 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11475 * (allow over-scrolling only if the view content is larger than the container),
11476 * or {@link #OVER_SCROLL_NEVER}.
11477 *
11478 * Setting the over-scroll mode of a view will have an effect only if the
11479 * view is capable of scrolling.
11480 *
11481 * @param overScrollMode The new over-scroll mode for this view.
11482 */
11483 public void setOverScrollMode(int overScrollMode) {
11484 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11485 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11486 overScrollMode != OVER_SCROLL_NEVER) {
11487 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11488 }
11489 mOverScrollMode = overScrollMode;
11490 }
11491
11492 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011493 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11494 * Each MeasureSpec represents a requirement for either the width or the height.
11495 * A MeasureSpec is comprised of a size and a mode. There are three possible
11496 * modes:
11497 * <dl>
11498 * <dt>UNSPECIFIED</dt>
11499 * <dd>
11500 * The parent has not imposed any constraint on the child. It can be whatever size
11501 * it wants.
11502 * </dd>
11503 *
11504 * <dt>EXACTLY</dt>
11505 * <dd>
11506 * The parent has determined an exact size for the child. The child is going to be
11507 * given those bounds regardless of how big it wants to be.
11508 * </dd>
11509 *
11510 * <dt>AT_MOST</dt>
11511 * <dd>
11512 * The child can be as large as it wants up to the specified size.
11513 * </dd>
11514 * </dl>
11515 *
11516 * MeasureSpecs are implemented as ints to reduce object allocation. This class
11517 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
11518 */
11519 public static class MeasureSpec {
11520 private static final int MODE_SHIFT = 30;
11521 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
11522
11523 /**
11524 * Measure specification mode: The parent has not imposed any constraint
11525 * on the child. It can be whatever size it wants.
11526 */
11527 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
11528
11529 /**
11530 * Measure specification mode: The parent has determined an exact size
11531 * for the child. The child is going to be given those bounds regardless
11532 * of how big it wants to be.
11533 */
11534 public static final int EXACTLY = 1 << MODE_SHIFT;
11535
11536 /**
11537 * Measure specification mode: The child can be as large as it wants up
11538 * to the specified size.
11539 */
11540 public static final int AT_MOST = 2 << MODE_SHIFT;
11541
11542 /**
11543 * Creates a measure specification based on the supplied size and mode.
11544 *
11545 * The mode must always be one of the following:
11546 * <ul>
11547 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
11548 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
11549 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
11550 * </ul>
11551 *
11552 * @param size the size of the measure specification
11553 * @param mode the mode of the measure specification
11554 * @return the measure specification based on size and mode
11555 */
11556 public static int makeMeasureSpec(int size, int mode) {
11557 return size + mode;
11558 }
11559
11560 /**
11561 * Extracts the mode from the supplied measure specification.
11562 *
11563 * @param measureSpec the measure specification to extract the mode from
11564 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
11565 * {@link android.view.View.MeasureSpec#AT_MOST} or
11566 * {@link android.view.View.MeasureSpec#EXACTLY}
11567 */
11568 public static int getMode(int measureSpec) {
11569 return (measureSpec & MODE_MASK);
11570 }
11571
11572 /**
11573 * Extracts the size from the supplied measure specification.
11574 *
11575 * @param measureSpec the measure specification to extract the size from
11576 * @return the size in pixels defined in the supplied measure specification
11577 */
11578 public static int getSize(int measureSpec) {
11579 return (measureSpec & ~MODE_MASK);
11580 }
11581
11582 /**
11583 * Returns a String representation of the specified measure
11584 * specification.
11585 *
11586 * @param measureSpec the measure specification to convert to a String
11587 * @return a String with the following format: "MeasureSpec: MODE SIZE"
11588 */
11589 public static String toString(int measureSpec) {
11590 int mode = getMode(measureSpec);
11591 int size = getSize(measureSpec);
11592
11593 StringBuilder sb = new StringBuilder("MeasureSpec: ");
11594
11595 if (mode == UNSPECIFIED)
11596 sb.append("UNSPECIFIED ");
11597 else if (mode == EXACTLY)
11598 sb.append("EXACTLY ");
11599 else if (mode == AT_MOST)
11600 sb.append("AT_MOST ");
11601 else
11602 sb.append(mode).append(" ");
11603
11604 sb.append(size);
11605 return sb.toString();
11606 }
11607 }
11608
11609 class CheckForLongPress implements Runnable {
11610
11611 private int mOriginalWindowAttachCount;
11612
11613 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070011614 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011615 && mOriginalWindowAttachCount == mWindowAttachCount) {
11616 if (performLongClick()) {
11617 mHasPerformedLongPress = true;
11618 }
11619 }
11620 }
11621
11622 public void rememberWindowAttachCount() {
11623 mOriginalWindowAttachCount = mWindowAttachCount;
11624 }
11625 }
Joe Malin32736f02011-01-19 16:14:20 -080011626
Adam Powelle14579b2009-12-16 18:39:52 -080011627 private final class CheckForTap implements Runnable {
11628 public void run() {
11629 mPrivateFlags &= ~PREPRESSED;
11630 mPrivateFlags |= PRESSED;
11631 refreshDrawableState();
11632 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11633 postCheckForLongClick(ViewConfiguration.getTapTimeout());
11634 }
11635 }
11636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011637
Adam Powella35d7682010-03-12 14:48:13 -080011638 private final class PerformClick implements Runnable {
11639 public void run() {
11640 performClick();
11641 }
11642 }
11643
Dianne Hackborn63042d62011-01-26 18:56:29 -080011644 /** @hide */
11645 public void hackTurnOffWindowResizeAnim(boolean off) {
11646 mAttachInfo.mTurnOffWindowResizeAnim = off;
11647 }
Joe Malin32736f02011-01-19 16:14:20 -080011648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 /**
11650 * Interface definition for a callback to be invoked when a key event is
11651 * dispatched to this view. The callback will be invoked before the key
11652 * event is given to the view.
11653 */
11654 public interface OnKeyListener {
11655 /**
11656 * Called when a key is dispatched to a view. This allows listeners to
11657 * get a chance to respond before the target view.
11658 *
11659 * @param v The view the key has been dispatched to.
11660 * @param keyCode The code for the physical key that was pressed
11661 * @param event The KeyEvent object containing full information about
11662 * the event.
11663 * @return True if the listener has consumed the event, false otherwise.
11664 */
11665 boolean onKey(View v, int keyCode, KeyEvent event);
11666 }
11667
11668 /**
11669 * Interface definition for a callback to be invoked when a touch event is
11670 * dispatched to this view. The callback will be invoked before the touch
11671 * event is given to the view.
11672 */
11673 public interface OnTouchListener {
11674 /**
11675 * Called when a touch event is dispatched to a view. This allows listeners to
11676 * get a chance to respond before the target view.
11677 *
11678 * @param v The view the touch event has been dispatched to.
11679 * @param event The MotionEvent object containing full information about
11680 * the event.
11681 * @return True if the listener has consumed the event, false otherwise.
11682 */
11683 boolean onTouch(View v, MotionEvent event);
11684 }
11685
11686 /**
11687 * Interface definition for a callback to be invoked when a view has been clicked and held.
11688 */
11689 public interface OnLongClickListener {
11690 /**
11691 * Called when a view has been clicked and held.
11692 *
11693 * @param v The view that was clicked and held.
11694 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080011695 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011696 */
11697 boolean onLongClick(View v);
11698 }
11699
11700 /**
Chris Tate32affef2010-10-18 15:29:21 -070011701 * Interface definition for a callback to be invoked when a drag is being dispatched
11702 * to this view. The callback will be invoked before the hosting view's own
11703 * onDrag(event) method. If the listener wants to fall back to the hosting view's
11704 * onDrag(event) behavior, it should return 'false' from this callback.
11705 */
11706 public interface OnDragListener {
11707 /**
11708 * Called when a drag event is dispatched to a view. This allows listeners
11709 * to get a chance to override base View behavior.
11710 *
Joe Malin32736f02011-01-19 16:14:20 -080011711 * @param v The View that received the drag event.
11712 * @param event The {@link android.view.DragEvent} object for the drag event.
11713 * @return {@code true} if the drag event was handled successfully, or {@code false}
11714 * if the drag event was not handled. Note that {@code false} will trigger the View
11715 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070011716 */
11717 boolean onDrag(View v, DragEvent event);
11718 }
11719
11720 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011721 * Interface definition for a callback to be invoked when the focus state of
11722 * a view changed.
11723 */
11724 public interface OnFocusChangeListener {
11725 /**
11726 * Called when the focus state of a view has changed.
11727 *
11728 * @param v The view whose state has changed.
11729 * @param hasFocus The new focus state of v.
11730 */
11731 void onFocusChange(View v, boolean hasFocus);
11732 }
11733
11734 /**
11735 * Interface definition for a callback to be invoked when a view is clicked.
11736 */
11737 public interface OnClickListener {
11738 /**
11739 * Called when a view has been clicked.
11740 *
11741 * @param v The view that was clicked.
11742 */
11743 void onClick(View v);
11744 }
11745
11746 /**
11747 * Interface definition for a callback to be invoked when the context menu
11748 * for this view is being built.
11749 */
11750 public interface OnCreateContextMenuListener {
11751 /**
11752 * Called when the context menu for this view is being built. It is not
11753 * safe to hold onto the menu after this method returns.
11754 *
11755 * @param menu The context menu that is being built
11756 * @param v The view for which the context menu is being built
11757 * @param menuInfo Extra information about the item for which the
11758 * context menu should be shown. This information will vary
11759 * depending on the class of v.
11760 */
11761 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
11762 }
11763
Joe Onorato664644d2011-01-23 17:53:23 -080011764 /**
11765 * Interface definition for a callback to be invoked when the status bar changes
11766 * visibility.
11767 *
11768 * @see #setOnSystemUiVisibilityChangeListener
11769 */
11770 public interface OnSystemUiVisibilityChangeListener {
11771 /**
11772 * Called when the status bar changes visibility because of a call to
11773 * {@link #setSystemUiVisibility}.
11774 *
11775 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
11776 */
11777 public void onSystemUiVisibilityChange(int visibility);
11778 }
11779
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011780 private final class UnsetPressedState implements Runnable {
11781 public void run() {
11782 setPressed(false);
11783 }
11784 }
11785
11786 /**
11787 * Base class for derived classes that want to save and restore their own
11788 * state in {@link android.view.View#onSaveInstanceState()}.
11789 */
11790 public static class BaseSavedState extends AbsSavedState {
11791 /**
11792 * Constructor used when reading from a parcel. Reads the state of the superclass.
11793 *
11794 * @param source
11795 */
11796 public BaseSavedState(Parcel source) {
11797 super(source);
11798 }
11799
11800 /**
11801 * Constructor called by derived classes when creating their SavedState objects
11802 *
11803 * @param superState The state of the superclass of this view
11804 */
11805 public BaseSavedState(Parcelable superState) {
11806 super(superState);
11807 }
11808
11809 public static final Parcelable.Creator<BaseSavedState> CREATOR =
11810 new Parcelable.Creator<BaseSavedState>() {
11811 public BaseSavedState createFromParcel(Parcel in) {
11812 return new BaseSavedState(in);
11813 }
11814
11815 public BaseSavedState[] newArray(int size) {
11816 return new BaseSavedState[size];
11817 }
11818 };
11819 }
11820
11821 /**
11822 * A set of information given to a view when it is attached to its parent
11823 * window.
11824 */
11825 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011826 interface Callbacks {
11827 void playSoundEffect(int effectId);
11828 boolean performHapticFeedback(int effectId, boolean always);
11829 }
11830
11831 /**
11832 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
11833 * to a Handler. This class contains the target (View) to invalidate and
11834 * the coordinates of the dirty rectangle.
11835 *
11836 * For performance purposes, this class also implements a pool of up to
11837 * POOL_LIMIT objects that get reused. This reduces memory allocations
11838 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011839 */
Romain Guyd928d682009-03-31 17:52:16 -070011840 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011841 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070011842 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
11843 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070011844 public InvalidateInfo newInstance() {
11845 return new InvalidateInfo();
11846 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011847
Romain Guyd928d682009-03-31 17:52:16 -070011848 public void onAcquired(InvalidateInfo element) {
11849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011850
Romain Guyd928d682009-03-31 17:52:16 -070011851 public void onReleased(InvalidateInfo element) {
11852 }
11853 }, POOL_LIMIT)
11854 );
11855
11856 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011857
11858 View target;
11859
11860 int left;
11861 int top;
11862 int right;
11863 int bottom;
11864
Romain Guyd928d682009-03-31 17:52:16 -070011865 public void setNextPoolable(InvalidateInfo element) {
11866 mNext = element;
11867 }
11868
11869 public InvalidateInfo getNextPoolable() {
11870 return mNext;
11871 }
11872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070011874 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011875 }
11876
11877 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070011878 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011879 }
11880 }
11881
11882 final IWindowSession mSession;
11883
11884 final IWindow mWindow;
11885
11886 final IBinder mWindowToken;
11887
11888 final Callbacks mRootCallbacks;
11889
Chet Haasedaf98e92011-01-10 14:10:36 -080011890 Canvas mHardwareCanvas;
11891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011892 /**
11893 * The top view of the hierarchy.
11894 */
11895 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070011896
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011897 IBinder mPanelParentWindowToken;
11898 Surface mSurface;
11899
Romain Guyb051e892010-09-28 19:09:36 -070011900 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011901 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070011902 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080011903
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011904 /**
Romain Guy8506ab42009-06-11 17:35:47 -070011905 * Scale factor used by the compatibility mode
11906 */
11907 float mApplicationScale;
11908
11909 /**
11910 * Indicates whether the application is in compatibility mode
11911 */
11912 boolean mScalingRequired;
11913
11914 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080011915 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
11916 */
11917 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080011918
Dianne Hackborn63042d62011-01-26 18:56:29 -080011919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 * Left position of this view's window
11921 */
11922 int mWindowLeft;
11923
11924 /**
11925 * Top position of this view's window
11926 */
11927 int mWindowTop;
11928
11929 /**
Adam Powell26153a32010-11-08 15:22:27 -080011930 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070011931 */
Adam Powell26153a32010-11-08 15:22:27 -080011932 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070011933
11934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011935 * For windows that are full-screen but using insets to layout inside
11936 * of the screen decorations, these are the current insets for the
11937 * content of the window.
11938 */
11939 final Rect mContentInsets = new Rect();
11940
11941 /**
11942 * For windows that are full-screen but using insets to layout inside
11943 * of the screen decorations, these are the current insets for the
11944 * actual visible parts of the window.
11945 */
11946 final Rect mVisibleInsets = new Rect();
11947
11948 /**
11949 * The internal insets given by this window. This value is
11950 * supplied by the client (through
11951 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
11952 * be given to the window manager when changed to be used in laying
11953 * out windows behind it.
11954 */
11955 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
11956 = new ViewTreeObserver.InternalInsetsInfo();
11957
11958 /**
11959 * All views in the window's hierarchy that serve as scroll containers,
11960 * used to determine if the window can be resized or must be panned
11961 * to adjust for a soft input area.
11962 */
11963 final ArrayList<View> mScrollContainers = new ArrayList<View>();
11964
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070011965 final KeyEvent.DispatcherState mKeyDispatchState
11966 = new KeyEvent.DispatcherState();
11967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011968 /**
11969 * Indicates whether the view's window currently has the focus.
11970 */
11971 boolean mHasWindowFocus;
11972
11973 /**
11974 * The current visibility of the window.
11975 */
11976 int mWindowVisibility;
11977
11978 /**
11979 * Indicates the time at which drawing started to occur.
11980 */
11981 long mDrawingTime;
11982
11983 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070011984 * Indicates whether or not ignoring the DIRTY_MASK flags.
11985 */
11986 boolean mIgnoreDirtyState;
11987
11988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011989 * Indicates whether the view's window is currently in touch mode.
11990 */
11991 boolean mInTouchMode;
11992
11993 /**
11994 * Indicates that ViewRoot should trigger a global layout change
11995 * the next time it performs a traversal
11996 */
11997 boolean mRecomputeGlobalAttributes;
11998
11999 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012000 * Set during a traveral if any views want to keep the screen on.
12001 */
12002 boolean mKeepScreenOn;
12003
12004 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012005 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12006 */
12007 int mSystemUiVisibility;
12008
12009 /**
12010 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12011 * attached.
12012 */
12013 boolean mHasSystemUiListeners;
12014
12015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012016 * Set if the visibility of any views has changed.
12017 */
12018 boolean mViewVisibilityChanged;
12019
12020 /**
12021 * Set to true if a view has been scrolled.
12022 */
12023 boolean mViewScrollChanged;
12024
12025 /**
12026 * Global to the view hierarchy used as a temporary for dealing with
12027 * x/y points in the transparent region computations.
12028 */
12029 final int[] mTransparentLocation = new int[2];
12030
12031 /**
12032 * Global to the view hierarchy used as a temporary for dealing with
12033 * x/y points in the ViewGroup.invalidateChild implementation.
12034 */
12035 final int[] mInvalidateChildLocation = new int[2];
12036
Chet Haasec3aa3612010-06-17 08:50:37 -070012037
12038 /**
12039 * Global to the view hierarchy used as a temporary for dealing with
12040 * x/y location when view is transformed.
12041 */
12042 final float[] mTmpTransformLocation = new float[2];
12043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012044 /**
12045 * The view tree observer used to dispatch global events like
12046 * layout, pre-draw, touch mode change, etc.
12047 */
12048 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12049
12050 /**
12051 * A Canvas used by the view hierarchy to perform bitmap caching.
12052 */
12053 Canvas mCanvas;
12054
12055 /**
12056 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12057 * handler can be used to pump events in the UI events queue.
12058 */
12059 final Handler mHandler;
12060
12061 /**
12062 * Identifier for messages requesting the view to be invalidated.
12063 * Such messages should be sent to {@link #mHandler}.
12064 */
12065 static final int INVALIDATE_MSG = 0x1;
12066
12067 /**
12068 * Identifier for messages requesting the view to invalidate a region.
12069 * Such messages should be sent to {@link #mHandler}.
12070 */
12071 static final int INVALIDATE_RECT_MSG = 0x2;
12072
12073 /**
12074 * Temporary for use in computing invalidate rectangles while
12075 * calling up the hierarchy.
12076 */
12077 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012078
12079 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012080 * Temporary for use in computing hit areas with transformed views
12081 */
12082 final RectF mTmpTransformRect = new RectF();
12083
12084 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012085 * Temporary list for use in collecting focusable descendents of a view.
12086 */
12087 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012089 /**
12090 * Creates a new set of attachment information with the specified
12091 * events handler and thread.
12092 *
12093 * @param handler the events handler the view must use
12094 */
12095 AttachInfo(IWindowSession session, IWindow window,
12096 Handler handler, Callbacks effectPlayer) {
12097 mSession = session;
12098 mWindow = window;
12099 mWindowToken = window.asBinder();
12100 mHandler = handler;
12101 mRootCallbacks = effectPlayer;
12102 }
12103 }
12104
12105 /**
12106 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12107 * is supported. This avoids keeping too many unused fields in most
12108 * instances of View.</p>
12109 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012110 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012111
Mike Cleronf116bf82009-09-27 19:14:12 -070012112 /**
12113 * Scrollbars are not visible
12114 */
12115 public static final int OFF = 0;
12116
12117 /**
12118 * Scrollbars are visible
12119 */
12120 public static final int ON = 1;
12121
12122 /**
12123 * Scrollbars are fading away
12124 */
12125 public static final int FADING = 2;
12126
12127 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012129 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012130 public int scrollBarDefaultDelayBeforeFade;
12131 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012132
12133 public int scrollBarSize;
12134 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012135 public float[] interpolatorValues;
12136 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137
12138 public final Paint paint;
12139 public final Matrix matrix;
12140 public Shader shader;
12141
Mike Cleronf116bf82009-09-27 19:14:12 -070012142 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12143
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012144 private static final float[] OPAQUE = { 255 };
12145 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012146
Mike Cleronf116bf82009-09-27 19:14:12 -070012147 /**
12148 * When fading should start. This time moves into the future every time
12149 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12150 */
12151 public long fadeStartTime;
12152
12153
12154 /**
12155 * The current state of the scrollbars: ON, OFF, or FADING
12156 */
12157 public int state = OFF;
12158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012159 private int mLastColor;
12160
Mike Cleronf116bf82009-09-27 19:14:12 -070012161 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012162 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12163 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012164 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12165 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012166
12167 paint = new Paint();
12168 matrix = new Matrix();
12169 // use use a height of 1, and then wack the matrix each time we
12170 // actually use it.
12171 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 paint.setShader(shader);
12174 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012175 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012176 }
Romain Guy8506ab42009-06-11 17:35:47 -070012177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012178 public void setFadeColor(int color) {
12179 if (color != 0 && color != mLastColor) {
12180 mLastColor = color;
12181 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012182
Romain Guye55e1a72009-08-27 10:42:26 -070012183 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12184 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012186 paint.setShader(shader);
12187 // Restore the default transfer mode (src_over)
12188 paint.setXfermode(null);
12189 }
12190 }
Joe Malin32736f02011-01-19 16:14:20 -080012191
Mike Cleronf116bf82009-09-27 19:14:12 -070012192 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012193 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012194 if (now >= fadeStartTime) {
12195
12196 // the animation fades the scrollbars out by changing
12197 // the opacity (alpha) from fully opaque to fully
12198 // transparent
12199 int nextFrame = (int) now;
12200 int framesCount = 0;
12201
12202 Interpolator interpolator = scrollBarInterpolator;
12203
12204 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012205 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012206
12207 // End transparent
12208 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012209 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012210
12211 state = FADING;
12212
12213 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012214 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012215 }
12216 }
12217
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012218 }
12219}