blob: 4ee3a4798f70a1b64ddd00afc5f939147b66895e [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;
Jeff Brown33bbfd22011-02-24 20:55:35 -080060import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070062import android.view.accessibility.AccessibilityEvent;
63import android.view.accessibility.AccessibilityEventSource;
64import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070066import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.inputmethod.InputConnection;
69import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.ScrollBarDrawable;
71
Christopher Tatea0374192010-10-05 13:06:41 -070072import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import java.lang.reflect.InvocationTargetException;
74import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import java.util.ArrayList;
76import java.util.Arrays;
Romain Guyd90a3312009-05-06 14:54:28 -070077import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080078import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079
80/**
81 * <p>
82 * This class represents the basic building block for user interface components. A View
83 * occupies a rectangular area on the screen and is responsible for drawing and
84 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070085 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
87 * are invisible containers that hold other Views (or other ViewGroups) and define
88 * their layout properties.
89 * </p>
90 *
91 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070092 * <p>For an introduction to using this class to develop your
93 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070095 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
97 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
98 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
99 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
100 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
104 * </p>
105 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700106 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * <a name="Using"></a>
108 * <h3>Using Views</h3>
109 * <p>
110 * All of the views in a window are arranged in a single tree. You can add views
111 * either from code or by specifying a tree of views in one or more XML layout
112 * files. There are many specialized subclasses of views that act as controls or
113 * are capable of displaying text, images, or other content.
114 * </p>
115 * <p>
116 * Once you have created a tree of views, there are typically a few types of
117 * common operations you may wish to perform:
118 * <ul>
119 * <li><strong>Set properties:</strong> for example setting the text of a
120 * {@link android.widget.TextView}. The available properties and the methods
121 * that set them will vary among the different subclasses of views. Note that
122 * properties that are known at build time can be set in the XML layout
123 * files.</li>
124 * <li><strong>Set focus:</strong> The framework will handled moving focus in
125 * response to user input. To force focus to a specific view, call
126 * {@link #requestFocus}.</li>
127 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
128 * that will be notified when something interesting happens to the view. For
129 * example, all views will let you set a listener to be notified when the view
130 * gains or loses focus. You can register such a listener using
131 * {@link #setOnFocusChangeListener}. Other view subclasses offer more
132 * specialized listeners. For example, a Button exposes a listener to notify
133 * clients when the button is clicked.</li>
134 * <li><strong>Set visibility:</strong> You can hide or show views using
135 * {@link #setVisibility}.</li>
136 * </ul>
137 * </p>
138 * <p><em>
139 * Note: The Android framework is responsible for measuring, laying out and
140 * drawing views. You should not call methods that perform these actions on
141 * views yourself unless you are actually implementing a
142 * {@link android.view.ViewGroup}.
143 * </em></p>
144 *
145 * <a name="Lifecycle"></a>
146 * <h3>Implementing a Custom View</h3>
147 *
148 * <p>
149 * To implement a custom view, you will usually begin by providing overrides for
150 * some of the standard methods that the framework calls on all views. You do
151 * not need to override all of these methods. In fact, you can start by just
152 * overriding {@link #onDraw(android.graphics.Canvas)}.
153 * <table border="2" width="85%" align="center" cellpadding="5">
154 * <thead>
155 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
156 * </thead>
157 *
158 * <tbody>
159 * <tr>
160 * <td rowspan="2">Creation</td>
161 * <td>Constructors</td>
162 * <td>There is a form of the constructor that are called when the view
163 * is created from code and a form that is called when the view is
164 * inflated from a layout file. The second form should parse and apply
165 * any attributes defined in the layout file.
166 * </td>
167 * </tr>
168 * <tr>
169 * <td><code>{@link #onFinishInflate()}</code></td>
170 * <td>Called after a view and all of its children has been inflated
171 * from XML.</td>
172 * </tr>
173 *
174 * <tr>
175 * <td rowspan="3">Layout</td>
176 * <td><code>{@link #onMeasure}</code></td>
177 * <td>Called to determine the size requirements for this view and all
178 * of its children.
179 * </td>
180 * </tr>
181 * <tr>
182 * <td><code>{@link #onLayout}</code></td>
183 * <td>Called when this view should assign a size and position to all
184 * of its children.
185 * </td>
186 * </tr>
187 * <tr>
188 * <td><code>{@link #onSizeChanged}</code></td>
189 * <td>Called when the size of this view has changed.
190 * </td>
191 * </tr>
192 *
193 * <tr>
194 * <td>Drawing</td>
195 * <td><code>{@link #onDraw}</code></td>
196 * <td>Called when the view should render its content.
197 * </td>
198 * </tr>
199 *
200 * <tr>
201 * <td rowspan="4">Event processing</td>
202 * <td><code>{@link #onKeyDown}</code></td>
203 * <td>Called when a new key event occurs.
204 * </td>
205 * </tr>
206 * <tr>
207 * <td><code>{@link #onKeyUp}</code></td>
208 * <td>Called when a key up event occurs.
209 * </td>
210 * </tr>
211 * <tr>
212 * <td><code>{@link #onTrackballEvent}</code></td>
213 * <td>Called when a trackball motion event occurs.
214 * </td>
215 * </tr>
216 * <tr>
217 * <td><code>{@link #onTouchEvent}</code></td>
218 * <td>Called when a touch screen motion event occurs.
219 * </td>
220 * </tr>
221 *
222 * <tr>
223 * <td rowspan="2">Focus</td>
224 * <td><code>{@link #onFocusChanged}</code></td>
225 * <td>Called when the view gains or loses focus.
226 * </td>
227 * </tr>
228 *
229 * <tr>
230 * <td><code>{@link #onWindowFocusChanged}</code></td>
231 * <td>Called when the window containing the view gains or loses focus.
232 * </td>
233 * </tr>
234 *
235 * <tr>
236 * <td rowspan="3">Attaching</td>
237 * <td><code>{@link #onAttachedToWindow()}</code></td>
238 * <td>Called when the view is attached to a window.
239 * </td>
240 * </tr>
241 *
242 * <tr>
243 * <td><code>{@link #onDetachedFromWindow}</code></td>
244 * <td>Called when the view is detached from its window.
245 * </td>
246 * </tr>
247 *
248 * <tr>
249 * <td><code>{@link #onWindowVisibilityChanged}</code></td>
250 * <td>Called when the visibility of the window containing the view
251 * has changed.
252 * </td>
253 * </tr>
254 * </tbody>
255 *
256 * </table>
257 * </p>
258 *
259 * <a name="IDs"></a>
260 * <h3>IDs</h3>
261 * Views may have an integer id associated with them. These ids are typically
262 * assigned in the layout XML files, and are used to find specific views within
263 * the view tree. A common pattern is to:
264 * <ul>
265 * <li>Define a Button in the layout file and assign it a unique ID.
266 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700267 * &lt;Button
268 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 * android:layout_width="wrap_content"
270 * android:layout_height="wrap_content"
271 * android:text="@string/my_button_text"/&gt;
272 * </pre></li>
273 * <li>From the onCreate method of an Activity, find the Button
274 * <pre class="prettyprint">
275 * Button myButton = (Button) findViewById(R.id.my_button);
276 * </pre></li>
277 * </ul>
278 * <p>
279 * View IDs need not be unique throughout the tree, but it is good practice to
280 * ensure that they are at least unique within the part of the tree you are
281 * searching.
282 * </p>
283 *
284 * <a name="Position"></a>
285 * <h3>Position</h3>
286 * <p>
287 * The geometry of a view is that of a rectangle. A view has a location,
288 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
289 * two dimensions, expressed as a width and a height. The unit for location
290 * and dimensions is the pixel.
291 * </p>
292 *
293 * <p>
294 * It is possible to retrieve the location of a view by invoking the methods
295 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
296 * coordinate of the rectangle representing the view. The latter returns the
297 * top, or Y, coordinate of the rectangle representing the view. These methods
298 * both return the location of the view relative to its parent. For instance,
299 * when getLeft() returns 20, that means the view is located 20 pixels to the
300 * right of the left edge of its direct parent.
301 * </p>
302 *
303 * <p>
304 * In addition, several convenience methods are offered to avoid unnecessary
305 * computations, namely {@link #getRight()} and {@link #getBottom()}.
306 * These methods return the coordinates of the right and bottom edges of the
307 * rectangle representing the view. For instance, calling {@link #getRight()}
308 * is similar to the following computation: <code>getLeft() + getWidth()</code>
309 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
310 * </p>
311 *
312 * <a name="SizePaddingMargins"></a>
313 * <h3>Size, padding and margins</h3>
314 * <p>
315 * The size of a view is expressed with a width and a height. A view actually
316 * possess two pairs of width and height values.
317 * </p>
318 *
319 * <p>
320 * The first pair is known as <em>measured width</em> and
321 * <em>measured height</em>. These dimensions define how big a view wants to be
322 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
323 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
324 * and {@link #getMeasuredHeight()}.
325 * </p>
326 *
327 * <p>
328 * The second pair is simply known as <em>width</em> and <em>height</em>, or
329 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
330 * dimensions define the actual size of the view on screen, at drawing time and
331 * after layout. These values may, but do not have to, be different from the
332 * measured width and height. The width and height can be obtained by calling
333 * {@link #getWidth()} and {@link #getHeight()}.
334 * </p>
335 *
336 * <p>
337 * To measure its dimensions, a view takes into account its padding. The padding
338 * is expressed in pixels for the left, top, right and bottom parts of the view.
339 * Padding can be used to offset the content of the view by a specific amount of
340 * pixels. For instance, a left padding of 2 will push the view's content by
341 * 2 pixels to the right of the left edge. Padding can be set using the
342 * {@link #setPadding(int, int, int, int)} method and queried by calling
343 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
344 * {@link #getPaddingRight()} and {@link #getPaddingBottom()}.
345 * </p>
346 *
347 * <p>
348 * Even though a view can define a padding, it does not provide any support for
349 * margins. However, view groups provide such a support. Refer to
350 * {@link android.view.ViewGroup} and
351 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
352 * </p>
353 *
354 * <a name="Layout"></a>
355 * <h3>Layout</h3>
356 * <p>
357 * Layout is a two pass process: a measure pass and a layout pass. The measuring
358 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
359 * of the view tree. Each view pushes dimension specifications down the tree
360 * during the recursion. At the end of the measure pass, every view has stored
361 * its measurements. The second pass happens in
362 * {@link #layout(int,int,int,int)} and is also top-down. During
363 * this pass each parent is responsible for positioning all of its children
364 * using the sizes computed in the measure pass.
365 * </p>
366 *
367 * <p>
368 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
369 * {@link #getMeasuredHeight()} values must be set, along with those for all of
370 * that view's descendants. A view's measured width and measured height values
371 * must respect the constraints imposed by the view's parents. This guarantees
372 * that at the end of the measure pass, all parents accept all of their
373 * children's measurements. A parent view may call measure() more than once on
374 * its children. For example, the parent may measure each child once with
375 * unspecified dimensions to find out how big they want to be, then call
376 * measure() on them again with actual numbers if the sum of all the children's
377 * unconstrained sizes is too big or too small.
378 * </p>
379 *
380 * <p>
381 * The measure pass uses two classes to communicate dimensions. The
382 * {@link MeasureSpec} class is used by views to tell their parents how they
383 * want to be measured and positioned. The base LayoutParams class just
384 * describes how big the view wants to be for both width and height. For each
385 * dimension, it can specify one of:
386 * <ul>
387 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800388 * <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 -0800389 * (minus padding)
390 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
391 * enclose its content (plus padding).
392 * </ul>
393 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
394 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
395 * an X and Y value.
396 * </p>
397 *
398 * <p>
399 * MeasureSpecs are used to push requirements down the tree from parent to
400 * child. A MeasureSpec can be in one of three modes:
401 * <ul>
402 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
403 * of a child view. For example, a LinearLayout may call measure() on its child
404 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
405 * tall the child view wants to be given a width of 240 pixels.
406 * <li>EXACTLY: This is used by the parent to impose an exact size on the
407 * child. The child must use this size, and guarantee that all of its
408 * descendants will fit within this size.
409 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
410 * child. The child must gurantee that it and all of its descendants will fit
411 * within this size.
412 * </ul>
413 * </p>
414 *
415 * <p>
416 * To intiate a layout, call {@link #requestLayout}. This method is typically
417 * called by a view on itself when it believes that is can no longer fit within
418 * its current bounds.
419 * </p>
420 *
421 * <a name="Drawing"></a>
422 * <h3>Drawing</h3>
423 * <p>
424 * Drawing is handled by walking the tree and rendering each view that
425 * intersects the the invalid region. Because the tree is traversed in-order,
426 * this means that parents will draw before (i.e., behind) their children, with
427 * siblings drawn in the order they appear in the tree.
428 * If you set a background drawable for a View, then the View will draw it for you
429 * before calling back to its <code>onDraw()</code> method.
430 * </p>
431 *
432 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700433 * 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 -0800434 * </p>
435 *
436 * <p>
437 * To force a view to draw, call {@link #invalidate()}.
438 * </p>
439 *
440 * <a name="EventHandlingThreading"></a>
441 * <h3>Event Handling and Threading</h3>
442 * <p>
443 * The basic cycle of a view is as follows:
444 * <ol>
445 * <li>An event comes in and is dispatched to the appropriate view. The view
446 * handles the event and notifies any listeners.</li>
447 * <li>If in the course of processing the event, the view's bounds may need
448 * to be changed, the view will call {@link #requestLayout()}.</li>
449 * <li>Similarly, if in the course of processing the event the view's appearance
450 * may need to be changed, the view will call {@link #invalidate()}.</li>
451 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
452 * the framework will take care of measuring, laying out, and drawing the tree
453 * as appropriate.</li>
454 * </ol>
455 * </p>
456 *
457 * <p><em>Note: The entire view tree is single threaded. You must always be on
458 * the UI thread when calling any method on any view.</em>
459 * If you are doing work on other threads and want to update the state of a view
460 * from that thread, you should use a {@link Handler}.
461 * </p>
462 *
463 * <a name="FocusHandling"></a>
464 * <h3>Focus Handling</h3>
465 * <p>
466 * The framework will handle routine focus movement in response to user input.
467 * This includes changing the focus as views are removed or hidden, or as new
468 * views become available. Views indicate their willingness to take focus
469 * through the {@link #isFocusable} method. To change whether a view can take
470 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
471 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
472 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
473 * </p>
474 * <p>
475 * Focus movement is based on an algorithm which finds the nearest neighbor in a
476 * given direction. In rare cases, the default algorithm may not match the
477 * intended behavior of the developer. In these situations, you can provide
478 * explicit overrides by using these XML attributes in the layout file:
479 * <pre>
480 * nextFocusDown
481 * nextFocusLeft
482 * nextFocusRight
483 * nextFocusUp
484 * </pre>
485 * </p>
486 *
487 *
488 * <p>
489 * To get a particular view to take focus, call {@link #requestFocus()}.
490 * </p>
491 *
492 * <a name="TouchMode"></a>
493 * <h3>Touch Mode</h3>
494 * <p>
495 * When a user is navigating a user interface via directional keys such as a D-pad, it is
496 * necessary to give focus to actionable items such as buttons so the user can see
497 * what will take input. If the device has touch capabilities, however, and the user
498 * begins interacting with the interface by touching it, it is no longer necessary to
499 * always highlight, or give focus to, a particular view. This motivates a mode
500 * for interaction named 'touch mode'.
501 * </p>
502 * <p>
503 * For a touch capable device, once the user touches the screen, the device
504 * will enter touch mode. From this point onward, only views for which
505 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
506 * Other views that are touchable, like buttons, will not take focus when touched; they will
507 * only fire the on click listeners.
508 * </p>
509 * <p>
510 * Any time a user hits a directional key, such as a D-pad direction, the view device will
511 * exit touch mode, and find a view to take focus, so that the user may resume interacting
512 * with the user interface without touching the screen again.
513 * </p>
514 * <p>
515 * The touch mode state is maintained across {@link android.app.Activity}s. Call
516 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
517 * </p>
518 *
519 * <a name="Scrolling"></a>
520 * <h3>Scrolling</h3>
521 * <p>
522 * The framework provides basic support for views that wish to internally
523 * scroll their content. This includes keeping track of the X and Y scroll
524 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800525 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700526 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * </p>
528 *
529 * <a name="Tags"></a>
530 * <h3>Tags</h3>
531 * <p>
532 * Unlike IDs, tags are not used to identify views. Tags are essentially an
533 * extra piece of information that can be associated with a view. They are most
534 * often used as a convenience to store data related to views in the views
535 * themselves rather than by putting them in a separate structure.
536 * </p>
537 *
538 * <a name="Animation"></a>
539 * <h3>Animation</h3>
540 * <p>
541 * You can attach an {@link Animation} object to a view using
542 * {@link #setAnimation(Animation)} or
543 * {@link #startAnimation(Animation)}. The animation can alter the scale,
544 * rotation, translation and alpha of a view over time. If the animation is
545 * attached to a view that has children, the animation will affect the entire
546 * subtree rooted by that node. When an animation is started, the framework will
547 * take care of redrawing the appropriate views until the animation completes.
548 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800549 * <p>
550 * Starting with Android 3.0, the preferred way of animating views is to use the
551 * {@link android.animation} package APIs.
552 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 *
Jeff Brown85a31762010-09-01 17:01:00 -0700554 * <a name="Security"></a>
555 * <h3>Security</h3>
556 * <p>
557 * Sometimes it is essential that an application be able to verify that an action
558 * is being performed with the full knowledge and consent of the user, such as
559 * granting a permission request, making a purchase or clicking on an advertisement.
560 * Unfortunately, a malicious application could try to spoof the user into
561 * performing these actions, unaware, by concealing the intended purpose of the view.
562 * As a remedy, the framework offers a touch filtering mechanism that can be used to
563 * improve the security of views that provide access to sensitive functionality.
564 * </p><p>
565 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800566 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700567 * will discard touches that are received whenever the view's window is obscured by
568 * another visible window. As a result, the view will not receive touches whenever a
569 * toast, dialog or other window appears above the view's window.
570 * </p><p>
571 * For more fine-grained control over security, consider overriding the
572 * {@link #onFilterTouchEventForSecurity} method to implement your own security policy.
573 * See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
574 * </p>
575 *
Romain Guy171c5922011-01-06 10:04:23 -0800576 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700577 * @attr ref android.R.styleable#View_background
578 * @attr ref android.R.styleable#View_clickable
579 * @attr ref android.R.styleable#View_contentDescription
580 * @attr ref android.R.styleable#View_drawingCacheQuality
581 * @attr ref android.R.styleable#View_duplicateParentState
582 * @attr ref android.R.styleable#View_id
583 * @attr ref android.R.styleable#View_fadingEdge
584 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700585 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700587 * @attr ref android.R.styleable#View_isScrollContainer
588 * @attr ref android.R.styleable#View_focusable
589 * @attr ref android.R.styleable#View_focusableInTouchMode
590 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
591 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800592 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700593 * @attr ref android.R.styleable#View_longClickable
594 * @attr ref android.R.styleable#View_minHeight
595 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 * @attr ref android.R.styleable#View_nextFocusDown
597 * @attr ref android.R.styleable#View_nextFocusLeft
598 * @attr ref android.R.styleable#View_nextFocusRight
599 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700600 * @attr ref android.R.styleable#View_onClick
601 * @attr ref android.R.styleable#View_padding
602 * @attr ref android.R.styleable#View_paddingBottom
603 * @attr ref android.R.styleable#View_paddingLeft
604 * @attr ref android.R.styleable#View_paddingRight
605 * @attr ref android.R.styleable#View_paddingTop
606 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800607 * @attr ref android.R.styleable#View_rotation
608 * @attr ref android.R.styleable#View_rotationX
609 * @attr ref android.R.styleable#View_rotationY
610 * @attr ref android.R.styleable#View_scaleX
611 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 * @attr ref android.R.styleable#View_scrollX
613 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700614 * @attr ref android.R.styleable#View_scrollbarSize
615 * @attr ref android.R.styleable#View_scrollbarStyle
616 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700617 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
618 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800619 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
620 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 * @attr ref android.R.styleable#View_scrollbarThumbVertical
622 * @attr ref android.R.styleable#View_scrollbarTrackVertical
623 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
624 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700625 * @attr ref android.R.styleable#View_soundEffectsEnabled
626 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800627 * @attr ref android.R.styleable#View_transformPivotX
628 * @attr ref android.R.styleable#View_transformPivotY
629 * @attr ref android.R.styleable#View_translationX
630 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 *
633 * @see android.view.ViewGroup
634 */
svetoslavganov75986cf2009-05-14 22:28:01 -0700635public class View implements Drawable.Callback, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 private static final boolean DBG = false;
637
638 /**
639 * The logging tag used by this class with android.util.Log.
640 */
641 protected static final String VIEW_LOG_TAG = "View";
642
643 /**
644 * Used to mark a View that has no ID.
645 */
646 public static final int NO_ID = -1;
647
648 /**
649 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
650 * calling setFlags.
651 */
652 private static final int NOT_FOCUSABLE = 0x00000000;
653
654 /**
655 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
656 * setFlags.
657 */
658 private static final int FOCUSABLE = 0x00000001;
659
660 /**
661 * Mask for use with setFlags indicating bits used for focus.
662 */
663 private static final int FOCUSABLE_MASK = 0x00000001;
664
665 /**
666 * This view will adjust its padding to fit sytem windows (e.g. status bar)
667 */
668 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
669
670 /**
671 * This view is visible. Use with {@link #setVisibility}.
672 */
673 public static final int VISIBLE = 0x00000000;
674
675 /**
676 * This view is invisible, but it still takes up space for layout purposes.
677 * Use with {@link #setVisibility}.
678 */
679 public static final int INVISIBLE = 0x00000004;
680
681 /**
682 * This view is invisible, and it doesn't take any space for layout
683 * purposes. Use with {@link #setVisibility}.
684 */
685 public static final int GONE = 0x00000008;
686
687 /**
688 * Mask for use with setFlags indicating bits used for visibility.
689 * {@hide}
690 */
691 static final int VISIBILITY_MASK = 0x0000000C;
692
693 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
694
695 /**
696 * This view is enabled. Intrepretation varies by subclass.
697 * Use with ENABLED_MASK when calling setFlags.
698 * {@hide}
699 */
700 static final int ENABLED = 0x00000000;
701
702 /**
703 * This view is disabled. Intrepretation varies by subclass.
704 * Use with ENABLED_MASK when calling setFlags.
705 * {@hide}
706 */
707 static final int DISABLED = 0x00000020;
708
709 /**
710 * Mask for use with setFlags indicating bits used for indicating whether
711 * this view is enabled
712 * {@hide}
713 */
714 static final int ENABLED_MASK = 0x00000020;
715
716 /**
717 * This view won't draw. {@link #onDraw} won't be called and further
718 * optimizations
719 * will be performed. It is okay to have this flag set and a background.
720 * Use with DRAW_MASK when calling setFlags.
721 * {@hide}
722 */
723 static final int WILL_NOT_DRAW = 0x00000080;
724
725 /**
726 * Mask for use with setFlags indicating bits used for indicating whether
727 * this view is will draw
728 * {@hide}
729 */
730 static final int DRAW_MASK = 0x00000080;
731
732 /**
733 * <p>This view doesn't show scrollbars.</p>
734 * {@hide}
735 */
736 static final int SCROLLBARS_NONE = 0x00000000;
737
738 /**
739 * <p>This view shows horizontal scrollbars.</p>
740 * {@hide}
741 */
742 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
743
744 /**
745 * <p>This view shows vertical scrollbars.</p>
746 * {@hide}
747 */
748 static final int SCROLLBARS_VERTICAL = 0x00000200;
749
750 /**
751 * <p>Mask for use with setFlags indicating bits used for indicating which
752 * scrollbars are enabled.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_MASK = 0x00000300;
756
Jeff Brown85a31762010-09-01 17:01:00 -0700757 /**
758 * Indicates that the view should filter touches when its window is obscured.
759 * Refer to the class comments for more information about this security feature.
760 * {@hide}
761 */
762 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
763
764 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800765
766 /**
767 * <p>This view doesn't show fading edges.</p>
768 * {@hide}
769 */
770 static final int FADING_EDGE_NONE = 0x00000000;
771
772 /**
773 * <p>This view shows horizontal fading edges.</p>
774 * {@hide}
775 */
776 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
777
778 /**
779 * <p>This view shows vertical fading edges.</p>
780 * {@hide}
781 */
782 static final int FADING_EDGE_VERTICAL = 0x00002000;
783
784 /**
785 * <p>Mask for use with setFlags indicating bits used for indicating which
786 * fading edges are enabled.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_MASK = 0x00003000;
790
791 /**
792 * <p>Indicates this view can be clicked. When clickable, a View reacts
793 * to clicks by notifying the OnClickListener.<p>
794 * {@hide}
795 */
796 static final int CLICKABLE = 0x00004000;
797
798 /**
799 * <p>Indicates this view is caching its drawing into a bitmap.</p>
800 * {@hide}
801 */
802 static final int DRAWING_CACHE_ENABLED = 0x00008000;
803
804 /**
805 * <p>Indicates that no icicle should be saved for this view.<p>
806 * {@hide}
807 */
808 static final int SAVE_DISABLED = 0x000010000;
809
810 /**
811 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
812 * property.</p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED_MASK = 0x000010000;
816
817 /**
818 * <p>Indicates that no drawing cache should ever be created for this view.<p>
819 * {@hide}
820 */
821 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
822
823 /**
824 * <p>Indicates this view can take / keep focus when int touch mode.</p>
825 * {@hide}
826 */
827 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
828
829 /**
830 * <p>Enables low quality mode for the drawing cache.</p>
831 */
832 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
833
834 /**
835 * <p>Enables high quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
838
839 /**
840 * <p>Enables automatic quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
843
844 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
845 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
846 };
847
848 /**
849 * <p>Mask for use with setFlags indicating bits used for the cache
850 * quality property.</p>
851 * {@hide}
852 */
853 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
854
855 /**
856 * <p>
857 * Indicates this view can be long clicked. When long clickable, a View
858 * reacts to long clicks by notifying the OnLongClickListener or showing a
859 * context menu.
860 * </p>
861 * {@hide}
862 */
863 static final int LONG_CLICKABLE = 0x00200000;
864
865 /**
866 * <p>Indicates that this view gets its drawable states from its direct parent
867 * and ignores its original internal states.</p>
868 *
869 * @hide
870 */
871 static final int DUPLICATE_PARENT_STATE = 0x00400000;
872
873 /**
874 * The scrollbar style to display the scrollbars inside the content area,
875 * without increasing the padding. The scrollbars will be overlaid with
876 * translucency on the view's content.
877 */
878 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the padded area,
882 * increasing the padding of the view. The scrollbars will not overlap the
883 * content area of the view.
884 */
885 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
886
887 /**
888 * The scrollbar style to display the scrollbars at the edge of the view,
889 * without increasing the padding. The scrollbars will be overlaid with
890 * translucency.
891 */
892 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * increasing the padding of the view. The scrollbars will only overlap the
897 * background, if any.
898 */
899 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
900
901 /**
902 * Mask to check if the scrollbar style is overlay or inset.
903 * {@hide}
904 */
905 static final int SCROLLBARS_INSET_MASK = 0x01000000;
906
907 /**
908 * Mask to check if the scrollbar style is inside or outside.
909 * {@hide}
910 */
911 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
912
913 /**
914 * Mask for scrollbar style.
915 * {@hide}
916 */
917 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
918
919 /**
920 * View flag indicating that the screen should remain on while the
921 * window containing this view is visible to the user. This effectively
922 * takes care of automatically setting the WindowManager's
923 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
924 */
925 public static final int KEEP_SCREEN_ON = 0x04000000;
926
927 /**
928 * View flag indicating whether this view should have sound effects enabled
929 * for events such as clicking and touching.
930 */
931 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
932
933 /**
934 * View flag indicating whether this view should have haptic feedback
935 * enabled for events such as long presses.
936 */
937 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
938
939 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700940 * <p>Indicates that the view hierarchy should stop saving state when
941 * it reaches this view. If state saving is initiated immediately at
942 * the view, it will be allowed.
943 * {@hide}
944 */
945 static final int PARENT_SAVE_DISABLED = 0x20000000;
946
947 /**
948 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
949 * {@hide}
950 */
951 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
952
953 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700954 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
955 * should add all focusable Views regardless if they are focusable in touch mode.
956 */
957 public static final int FOCUSABLES_ALL = 0x00000000;
958
959 /**
960 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
961 * should add only Views focusable in touch mode.
962 */
963 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
964
965 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800966 * Use with {@link #focusSearch}. Move focus to the previous selectable
967 * item.
968 */
969 public static final int FOCUS_BACKWARD = 0x00000001;
970
971 /**
972 * Use with {@link #focusSearch}. Move focus to the next selectable
973 * item.
974 */
975 public static final int FOCUS_FORWARD = 0x00000002;
976
977 /**
978 * Use with {@link #focusSearch}. Move focus to the left.
979 */
980 public static final int FOCUS_LEFT = 0x00000011;
981
982 /**
983 * Use with {@link #focusSearch}. Move focus up.
984 */
985 public static final int FOCUS_UP = 0x00000021;
986
987 /**
988 * Use with {@link #focusSearch}. Move focus to the right.
989 */
990 public static final int FOCUS_RIGHT = 0x00000042;
991
992 /**
993 * Use with {@link #focusSearch}. Move focus down.
994 */
995 public static final int FOCUS_DOWN = 0x00000082;
996
997 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -0800998 * Bits of {@link #getMeasuredWidthAndState()} and
999 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1000 */
1001 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1002
1003 /**
1004 * Bits of {@link #getMeasuredWidthAndState()} and
1005 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1006 */
1007 public static final int MEASURED_STATE_MASK = 0xff000000;
1008
1009 /**
1010 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1011 * for functions that combine both width and height into a single int,
1012 * such as {@link #getMeasuredState()} and the childState argument of
1013 * {@link #resolveSizeAndState(int, int, int)}.
1014 */
1015 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1016
1017 /**
1018 * Bit of {@link #getMeasuredWidthAndState()} and
1019 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1020 * is smaller that the space the view would like to have.
1021 */
1022 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1023
1024 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 * Base View state sets
1026 */
1027 // Singles
1028 /**
1029 * Indicates the view has no states set. States are used with
1030 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1031 * view depending on its state.
1032 *
1033 * @see android.graphics.drawable.Drawable
1034 * @see #getDrawableState()
1035 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001036 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 /**
1038 * Indicates the view is enabled. States are used with
1039 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1040 * view depending on its state.
1041 *
1042 * @see android.graphics.drawable.Drawable
1043 * @see #getDrawableState()
1044 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001045 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 /**
1047 * Indicates the view is focused. States are used with
1048 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1049 * view depending on its state.
1050 *
1051 * @see android.graphics.drawable.Drawable
1052 * @see #getDrawableState()
1053 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001054 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001055 /**
1056 * Indicates the view is selected. States are used with
1057 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1058 * view depending on its state.
1059 *
1060 * @see android.graphics.drawable.Drawable
1061 * @see #getDrawableState()
1062 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001063 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 /**
1065 * Indicates the view is pressed. States are used with
1066 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1067 * view depending on its state.
1068 *
1069 * @see android.graphics.drawable.Drawable
1070 * @see #getDrawableState()
1071 * @hide
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view's window has focus. States are used with
1076 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1077 * view depending on its state.
1078 *
1079 * @see android.graphics.drawable.Drawable
1080 * @see #getDrawableState()
1081 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001082 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 // Doubles
1084 /**
1085 * Indicates the view is enabled and has the focus.
1086 *
1087 * @see #ENABLED_STATE_SET
1088 * @see #FOCUSED_STATE_SET
1089 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001090 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 /**
1092 * Indicates the view is enabled and selected.
1093 *
1094 * @see #ENABLED_STATE_SET
1095 * @see #SELECTED_STATE_SET
1096 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001097 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 /**
1099 * Indicates the view is enabled and that its window has focus.
1100 *
1101 * @see #ENABLED_STATE_SET
1102 * @see #WINDOW_FOCUSED_STATE_SET
1103 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001104 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 /**
1106 * Indicates the view is focused and selected.
1107 *
1108 * @see #FOCUSED_STATE_SET
1109 * @see #SELECTED_STATE_SET
1110 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001111 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 /**
1113 * Indicates the view has the focus and that its window has the focus.
1114 *
1115 * @see #FOCUSED_STATE_SET
1116 * @see #WINDOW_FOCUSED_STATE_SET
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is selected and that its window has the focus.
1121 *
1122 * @see #SELECTED_STATE_SET
1123 * @see #WINDOW_FOCUSED_STATE_SET
1124 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001125 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 // Triples
1127 /**
1128 * Indicates the view is enabled, focused and selected.
1129 *
1130 * @see #ENABLED_STATE_SET
1131 * @see #FOCUSED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled, focused and its window has the focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #FOCUSED_STATE_SET
1140 * @see #WINDOW_FOCUSED_STATE_SET
1141 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001142 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 /**
1144 * Indicates the view is enabled, selected and its window has the focus.
1145 *
1146 * @see #ENABLED_STATE_SET
1147 * @see #SELECTED_STATE_SET
1148 * @see #WINDOW_FOCUSED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is focused, selected and its window has the focus.
1153 *
1154 * @see #FOCUSED_STATE_SET
1155 * @see #SELECTED_STATE_SET
1156 * @see #WINDOW_FOCUSED_STATE_SET
1157 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001158 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001159 /**
1160 * Indicates the view is enabled, focused, selected and its window
1161 * has the focus.
1162 *
1163 * @see #ENABLED_STATE_SET
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 * @see #WINDOW_FOCUSED_STATE_SET
1167 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001168 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 /**
1170 * Indicates the view is pressed and its window has the focus.
1171 *
1172 * @see #PRESSED_STATE_SET
1173 * @see #WINDOW_FOCUSED_STATE_SET
1174 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001175 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 /**
1177 * Indicates the view is pressed and selected.
1178 *
1179 * @see #PRESSED_STATE_SET
1180 * @see #SELECTED_STATE_SET
1181 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001182 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 /**
1184 * Indicates the view is pressed, selected and its window has the focus.
1185 *
1186 * @see #PRESSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 * @see #WINDOW_FOCUSED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is pressed and focused.
1193 *
1194 * @see #PRESSED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is pressed, focused and its window has the focus.
1200 *
1201 * @see #PRESSED_STATE_SET
1202 * @see #FOCUSED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed, focused and selected.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is pressed, focused, selected and its window has the focus.
1216 *
1217 * @see #PRESSED_STATE_SET
1218 * @see #FOCUSED_STATE_SET
1219 * @see #SELECTED_STATE_SET
1220 * @see #WINDOW_FOCUSED_STATE_SET
1221 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001222 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 /**
1224 * Indicates the view is pressed and enabled.
1225 *
1226 * @see #PRESSED_STATE_SET
1227 * @see #ENABLED_STATE_SET
1228 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001229 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 /**
1231 * Indicates the view is pressed, enabled and its window has the focus.
1232 *
1233 * @see #PRESSED_STATE_SET
1234 * @see #ENABLED_STATE_SET
1235 * @see #WINDOW_FOCUSED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, enabled and selected.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #ENABLED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed, enabled, selected and its window has the
1248 * focus.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #ENABLED_STATE_SET
1252 * @see #SELECTED_STATE_SET
1253 * @see #WINDOW_FOCUSED_STATE_SET
1254 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001255 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 /**
1257 * Indicates the view is pressed, enabled and focused.
1258 *
1259 * @see #PRESSED_STATE_SET
1260 * @see #ENABLED_STATE_SET
1261 * @see #FOCUSED_STATE_SET
1262 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001263 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 /**
1265 * Indicates the view is pressed, enabled, focused and its window has the
1266 * focus.
1267 *
1268 * @see #PRESSED_STATE_SET
1269 * @see #ENABLED_STATE_SET
1270 * @see #FOCUSED_STATE_SET
1271 * @see #WINDOW_FOCUSED_STATE_SET
1272 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001273 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 /**
1275 * Indicates the view is pressed, enabled, focused and selected.
1276 *
1277 * @see #PRESSED_STATE_SET
1278 * @see #ENABLED_STATE_SET
1279 * @see #SELECTED_STATE_SET
1280 * @see #FOCUSED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, focused, selected and its window
1285 * has the focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #FOCUSED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294
1295 /**
1296 * The order here is very important to {@link #getDrawableState()}
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 private static final int[][] VIEW_STATE_SETS;
1299
Romain Guyb051e892010-09-28 19:09:36 -07001300 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1301 static final int VIEW_STATE_SELECTED = 1 << 1;
1302 static final int VIEW_STATE_FOCUSED = 1 << 2;
1303 static final int VIEW_STATE_ENABLED = 1 << 3;
1304 static final int VIEW_STATE_PRESSED = 1 << 4;
1305 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001306 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001307 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001308 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1309 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310
1311 static final int[] VIEW_STATE_IDS = new int[] {
1312 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1313 R.attr.state_selected, VIEW_STATE_SELECTED,
1314 R.attr.state_focused, VIEW_STATE_FOCUSED,
1315 R.attr.state_enabled, VIEW_STATE_ENABLED,
1316 R.attr.state_pressed, VIEW_STATE_PRESSED,
1317 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001318 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001319 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001320 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1321 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 };
1323
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001324 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001325 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1326 throw new IllegalStateException(
1327 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1328 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001330 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001331 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001332 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001333 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001334 orderedIds[i * 2] = viewState;
1335 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001336 }
1337 }
1338 }
Romain Guyb051e892010-09-28 19:09:36 -07001339 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1340 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1341 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001342 int numBits = Integer.bitCount(i);
1343 int[] set = new int[numBits];
1344 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001345 for (int j = 0; j < orderedIds.length; j += 2) {
1346 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347 set[pos++] = orderedIds[j];
1348 }
1349 }
1350 VIEW_STATE_SETS[i] = set;
1351 }
1352
1353 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1354 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1355 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1356 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1357 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1358 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1359 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1360 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1361 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1362 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1363 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1364 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1365 | VIEW_STATE_FOCUSED];
1366 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1367 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1368 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1369 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1370 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1371 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1372 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1373 | VIEW_STATE_ENABLED];
1374 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1375 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1376 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1377 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1378 | VIEW_STATE_ENABLED];
1379 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1380 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1381 | VIEW_STATE_ENABLED];
1382 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1383 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1384 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1385
1386 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1387 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1388 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1389 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1390 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1391 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1392 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1393 | VIEW_STATE_PRESSED];
1394 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1395 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1396 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1398 | VIEW_STATE_PRESSED];
1399 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1400 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1401 | VIEW_STATE_PRESSED];
1402 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1403 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1404 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1405 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1406 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1407 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1408 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1409 | VIEW_STATE_PRESSED];
1410 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1411 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1412 | VIEW_STATE_PRESSED];
1413 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1415 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1416 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1418 | VIEW_STATE_PRESSED];
1419 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1421 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1422 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1424 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1425 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1427 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1428 | VIEW_STATE_PRESSED];
1429 }
1430
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001432 * Temporary Rect currently for use in setBackground(). This will probably
1433 * be extended in the future to hold our own class with more than just
1434 * a Rect. :)
1435 */
1436 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001437
1438 /**
1439 * Map used to store views' tags.
1440 */
1441 private static WeakHashMap<View, SparseArray<Object>> sTags;
1442
1443 /**
1444 * Lock used to access sTags.
1445 */
1446 private static final Object sTagsLock = new Object();
1447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001448 /**
1449 * The animation currently associated with this view.
1450 * @hide
1451 */
1452 protected Animation mCurrentAnimation = null;
1453
1454 /**
1455 * Width as measured during measure pass.
1456 * {@hide}
1457 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001458 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001459 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001460
1461 /**
1462 * Height as measured during measure pass.
1463 * {@hide}
1464 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001465 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001466 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467
1468 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001469 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1470 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1471 * its display list. This flag, used only when hw accelerated, allows us to clear the
1472 * flag while retaining this information until it's needed (at getDisplayList() time and
1473 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1474 *
1475 * {@hide}
1476 */
1477 boolean mRecreateDisplayList = false;
1478
1479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001480 * The view's identifier.
1481 * {@hide}
1482 *
1483 * @see #setId(int)
1484 * @see #getId()
1485 */
1486 @ViewDebug.ExportedProperty(resolveId = true)
1487 int mID = NO_ID;
1488
1489 /**
1490 * The view's tag.
1491 * {@hide}
1492 *
1493 * @see #setTag(Object)
1494 * @see #getTag()
1495 */
1496 protected Object mTag;
1497
1498 // for mPrivateFlags:
1499 /** {@hide} */
1500 static final int WANTS_FOCUS = 0x00000001;
1501 /** {@hide} */
1502 static final int FOCUSED = 0x00000002;
1503 /** {@hide} */
1504 static final int SELECTED = 0x00000004;
1505 /** {@hide} */
1506 static final int IS_ROOT_NAMESPACE = 0x00000008;
1507 /** {@hide} */
1508 static final int HAS_BOUNDS = 0x00000010;
1509 /** {@hide} */
1510 static final int DRAWN = 0x00000020;
1511 /**
1512 * When this flag is set, this view is running an animation on behalf of its
1513 * children and should therefore not cancel invalidate requests, even if they
1514 * lie outside of this view's bounds.
1515 *
1516 * {@hide}
1517 */
1518 static final int DRAW_ANIMATION = 0x00000040;
1519 /** {@hide} */
1520 static final int SKIP_DRAW = 0x00000080;
1521 /** {@hide} */
1522 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1523 /** {@hide} */
1524 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1525 /** {@hide} */
1526 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1527 /** {@hide} */
1528 static final int MEASURED_DIMENSION_SET = 0x00000800;
1529 /** {@hide} */
1530 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001531 /** {@hide} */
1532 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533
1534 private static final int PRESSED = 0x00004000;
1535
1536 /** {@hide} */
1537 static final int DRAWING_CACHE_VALID = 0x00008000;
1538 /**
1539 * Flag used to indicate that this view should be drawn once more (and only once
1540 * more) after its animation has completed.
1541 * {@hide}
1542 */
1543 static final int ANIMATION_STARTED = 0x00010000;
1544
1545 private static final int SAVE_STATE_CALLED = 0x00020000;
1546
1547 /**
1548 * Indicates that the View returned true when onSetAlpha() was called and that
1549 * the alpha must be restored.
1550 * {@hide}
1551 */
1552 static final int ALPHA_SET = 0x00040000;
1553
1554 /**
1555 * Set by {@link #setScrollContainer(boolean)}.
1556 */
1557 static final int SCROLL_CONTAINER = 0x00080000;
1558
1559 /**
1560 * Set by {@link #setScrollContainer(boolean)}.
1561 */
1562 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1563
1564 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001565 * View flag indicating whether this view was invalidated (fully or partially.)
1566 *
1567 * @hide
1568 */
1569 static final int DIRTY = 0x00200000;
1570
1571 /**
1572 * View flag indicating whether this view was invalidated by an opaque
1573 * invalidate request.
1574 *
1575 * @hide
1576 */
1577 static final int DIRTY_OPAQUE = 0x00400000;
1578
1579 /**
1580 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1581 *
1582 * @hide
1583 */
1584 static final int DIRTY_MASK = 0x00600000;
1585
1586 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001587 * Indicates whether the background is opaque.
1588 *
1589 * @hide
1590 */
1591 static final int OPAQUE_BACKGROUND = 0x00800000;
1592
1593 /**
1594 * Indicates whether the scrollbars are opaque.
1595 *
1596 * @hide
1597 */
1598 static final int OPAQUE_SCROLLBARS = 0x01000000;
1599
1600 /**
1601 * Indicates whether the view is opaque.
1602 *
1603 * @hide
1604 */
1605 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001606
Adam Powelle14579b2009-12-16 18:39:52 -08001607 /**
1608 * Indicates a prepressed state;
1609 * the short time between ACTION_DOWN and recognizing
1610 * a 'real' press. Prepressed is used to recognize quick taps
1611 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001612 *
Adam Powelle14579b2009-12-16 18:39:52 -08001613 * @hide
1614 */
1615 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001616
Adam Powellc9fbaab2010-02-16 17:16:19 -08001617 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001618 * Indicates whether the view is temporarily detached.
1619 *
1620 * @hide
1621 */
1622 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001623
Adam Powell8568c3a2010-04-19 14:26:11 -07001624 /**
1625 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001626 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001627 * @hide
1628 */
1629 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001630
1631 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001632 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1633 * @hide
1634 */
1635 private static final int HOVERED = 0x10000000;
1636
1637 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001638 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1639 * for transform operations
1640 *
1641 * @hide
1642 */
Adam Powellf37df072010-09-17 16:22:49 -07001643 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001644
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001645 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001646 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001647
Chet Haasefd2b0022010-08-06 13:08:56 -07001648 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001649 * Indicates that this view was specifically invalidated, not just dirtied because some
1650 * child view was invalidated. The flag is used to determine when we need to recreate
1651 * a view's display list (as opposed to just returning a reference to its existing
1652 * display list).
1653 *
1654 * @hide
1655 */
1656 static final int INVALIDATED = 0x80000000;
1657
Christopher Tate3d4bf172011-03-28 16:16:46 -07001658 /* Masks for mPrivateFlags2 */
1659
1660 /**
1661 * Indicates that this view has reported that it can accept the current drag's content.
1662 * Cleared when the drag operation concludes.
1663 * @hide
1664 */
1665 static final int DRAG_CAN_ACCEPT = 0x00000001;
1666
1667 /**
1668 * Indicates that this view is currently directly under the drag location in a
1669 * drag-and-drop operation involving content that it can accept. Cleared when
1670 * the drag exits the view, or when the drag operation concludes.
1671 * @hide
1672 */
1673 static final int DRAG_HOVERED = 0x00000002;
1674
1675 /* End of masks for mPrivateFlags2 */
1676
1677 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1678
Chet Haasedaf98e92011-01-10 14:10:36 -08001679 /**
Adam Powell637d3372010-08-25 14:37:03 -07001680 * Always allow a user to over-scroll this view, provided it is a
1681 * view that can scroll.
1682 *
1683 * @see #getOverScrollMode()
1684 * @see #setOverScrollMode(int)
1685 */
1686 public static final int OVER_SCROLL_ALWAYS = 0;
1687
1688 /**
1689 * Allow a user to over-scroll this view only if the content is large
1690 * enough to meaningfully scroll, provided it is a view that can scroll.
1691 *
1692 * @see #getOverScrollMode()
1693 * @see #setOverScrollMode(int)
1694 */
1695 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1696
1697 /**
1698 * Never allow a user to over-scroll this view.
1699 *
1700 * @see #getOverScrollMode()
1701 * @see #setOverScrollMode(int)
1702 */
1703 public static final int OVER_SCROLL_NEVER = 2;
1704
1705 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001706 * View has requested the status bar to be visible (the default).
1707 *
Joe Malin32736f02011-01-19 16:14:20 -08001708 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001709 */
1710 public static final int STATUS_BAR_VISIBLE = 0;
1711
1712 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001713 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001714 *
Joe Malin32736f02011-01-19 16:14:20 -08001715 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001716 */
1717 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1718
1719 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001720 * @hide
1721 *
1722 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1723 * out of the public fields to keep the undefined bits out of the developer's way.
1724 *
1725 * Flag to make the status bar not expandable. Unless you also
1726 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1727 */
1728 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1729
1730 /**
1731 * @hide
1732 *
1733 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1734 * out of the public fields to keep the undefined bits out of the developer's way.
1735 *
1736 * Flag to hide notification icons and scrolling ticker text.
1737 */
1738 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1739
1740 /**
1741 * @hide
1742 *
1743 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1744 * out of the public fields to keep the undefined bits out of the developer's way.
1745 *
1746 * Flag to disable incoming notification alerts. This will not block
1747 * icons, but it will block sound, vibrating and other visual or aural notifications.
1748 */
1749 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
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 *
1757 * Flag to hide only the scrolling ticker. Note that
1758 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1759 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1760 */
1761 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1762
1763 /**
1764 * @hide
1765 *
1766 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1767 * out of the public fields to keep the undefined bits out of the developer's way.
1768 *
1769 * Flag to hide the center system info area.
1770 */
1771 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1772
1773 /**
1774 * @hide
1775 *
1776 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1777 * out of the public fields to keep the undefined bits out of the developer's way.
1778 *
1779 * Flag to hide only the navigation buttons. Don't use this
1780 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001781 *
1782 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001783 */
1784 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1785
1786 /**
1787 * @hide
1788 *
1789 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1790 * out of the public fields to keep the undefined bits out of the developer's way.
1791 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001792 * Flag to hide only the back button. Don't use this
1793 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1794 */
1795 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1796
1797 /**
1798 * @hide
1799 *
1800 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1801 * out of the public fields to keep the undefined bits out of the developer's way.
1802 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001803 * Flag to hide only the clock. You might use this if your activity has
1804 * its own clock making the status bar's clock redundant.
1805 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001806 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1807
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001808 /**
1809 * @hide
1810 */
1811 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001812
1813 /**
Adam Powell637d3372010-08-25 14:37:03 -07001814 * Controls the over-scroll mode for this view.
1815 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1816 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1817 * and {@link #OVER_SCROLL_NEVER}.
1818 */
1819 private int mOverScrollMode;
1820
1821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 * The parent this view is attached to.
1823 * {@hide}
1824 *
1825 * @see #getParent()
1826 */
1827 protected ViewParent mParent;
1828
1829 /**
1830 * {@hide}
1831 */
1832 AttachInfo mAttachInfo;
1833
1834 /**
1835 * {@hide}
1836 */
Romain Guy809a7f62009-05-14 15:44:42 -07001837 @ViewDebug.ExportedProperty(flagMapping = {
1838 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1839 name = "FORCE_LAYOUT"),
1840 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1841 name = "LAYOUT_REQUIRED"),
1842 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001843 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001844 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1845 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1846 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1847 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1848 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001850 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851
1852 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001853 * This view's request for the visibility of the status bar.
1854 * @hide
1855 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001856 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001857 int mSystemUiVisibility;
1858
1859 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 * Count of how many windows this view has been attached to.
1861 */
1862 int mWindowAttachCount;
1863
1864 /**
1865 * The layout parameters associated with this view and used by the parent
1866 * {@link android.view.ViewGroup} to determine how this view should be
1867 * laid out.
1868 * {@hide}
1869 */
1870 protected ViewGroup.LayoutParams mLayoutParams;
1871
1872 /**
1873 * The view flags hold various views states.
1874 * {@hide}
1875 */
1876 @ViewDebug.ExportedProperty
1877 int mViewFlags;
1878
1879 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001880 * The transform matrix for the View. This transform is calculated internally
1881 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1882 * is used by default. Do *not* use this variable directly; instead call
1883 * getMatrix(), which will automatically recalculate the matrix if necessary
1884 * to get the correct matrix based on the latest rotation and scale properties.
1885 */
1886 private final Matrix mMatrix = new Matrix();
1887
1888 /**
1889 * The transform matrix for the View. This transform is calculated internally
1890 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1891 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001892 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001893 * to get the correct matrix based on the latest rotation and scale properties.
1894 */
1895 private Matrix mInverseMatrix;
1896
1897 /**
1898 * An internal variable that tracks whether we need to recalculate the
1899 * transform matrix, based on whether the rotation or scaleX/Y properties
1900 * have changed since the matrix was last calculated.
1901 */
Chet Haasea00f3862011-02-22 06:34:40 -08001902 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001903
1904 /**
1905 * An internal variable that tracks whether we need to recalculate the
1906 * transform matrix, based on whether the rotation or scaleX/Y properties
1907 * have changed since the matrix was last calculated.
1908 */
1909 private boolean mInverseMatrixDirty = true;
1910
1911 /**
1912 * A variable that tracks whether we need to recalculate the
1913 * transform matrix, based on whether the rotation or scaleX/Y properties
1914 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001915 * is only valid after a call to updateMatrix() or to a function that
1916 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001917 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001918 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001919
1920 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001921 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1922 */
1923 private Camera mCamera = null;
1924
1925 /**
1926 * This matrix is used when computing the matrix for 3D rotations.
1927 */
1928 private Matrix matrix3D = null;
1929
1930 /**
1931 * These prev values are used to recalculate a centered pivot point when necessary. The
1932 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1933 * set), so thes values are only used then as well.
1934 */
1935 private int mPrevWidth = -1;
1936 private int mPrevHeight = -1;
1937
Joe Malin32736f02011-01-19 16:14:20 -08001938 private boolean mLastIsOpaque;
1939
Chet Haasefd2b0022010-08-06 13:08:56 -07001940 /**
1941 * Convenience value to check for float values that are close enough to zero to be considered
1942 * zero.
1943 */
Romain Guy2542d192010-08-18 11:47:12 -07001944 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001945
1946 /**
1947 * The degrees rotation around the vertical axis through the pivot point.
1948 */
1949 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001950 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001951
1952 /**
1953 * The degrees rotation around the horizontal axis through the pivot point.
1954 */
1955 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001956 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The degrees rotation around the pivot point.
1960 */
1961 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001962 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001963
1964 /**
Chet Haasedf030d22010-07-30 17:22:38 -07001965 * The amount of translation of the object away from its left property (post-layout).
1966 */
1967 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001968 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001969
1970 /**
1971 * The amount of translation of the object away from its top property (post-layout).
1972 */
1973 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001974 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07001975
1976 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001977 * The amount of scale in the x direction around the pivot point. A
1978 * value of 1 means no scaling is applied.
1979 */
1980 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001981 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * The amount of scale in the y direction around the pivot point. A
1985 * value of 1 means no scaling is applied.
1986 */
1987 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001988 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001989
1990 /**
1991 * The amount of scale in the x direction around the pivot point. A
1992 * value of 1 means no scaling is applied.
1993 */
1994 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001995 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07001996
1997 /**
1998 * The amount of scale in the y direction around the pivot point. A
1999 * value of 1 means no scaling is applied.
2000 */
2001 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002002 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002003
2004 /**
2005 * The opacity of the View. This is a value from 0 to 1, where 0 means
2006 * completely transparent and 1 means completely opaque.
2007 */
2008 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002009 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002010
2011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002012 * The distance in pixels from the left edge of this view's parent
2013 * to the left edge of this view.
2014 * {@hide}
2015 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002016 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 protected int mLeft;
2018 /**
2019 * The distance in pixels from the left edge of this view's parent
2020 * to the right edge of this view.
2021 * {@hide}
2022 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002023 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 protected int mRight;
2025 /**
2026 * The distance in pixels from the top edge of this view's parent
2027 * to the top edge of this view.
2028 * {@hide}
2029 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002030 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 protected int mTop;
2032 /**
2033 * The distance in pixels from the top edge of this view's parent
2034 * to the bottom edge of this view.
2035 * {@hide}
2036 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002037 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002038 protected int mBottom;
2039
2040 /**
2041 * The offset, in pixels, by which the content of this view is scrolled
2042 * horizontally.
2043 * {@hide}
2044 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002045 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 protected int mScrollX;
2047 /**
2048 * The offset, in pixels, by which the content of this view is scrolled
2049 * vertically.
2050 * {@hide}
2051 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002052 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002053 protected int mScrollY;
2054
2055 /**
2056 * The left padding in pixels, that is the distance in pixels between the
2057 * left edge of this view and the left edge of its content.
2058 * {@hide}
2059 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002060 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 protected int mPaddingLeft;
2062 /**
2063 * The right padding in pixels, that is the distance in pixels between the
2064 * right edge of this view and the right edge of its content.
2065 * {@hide}
2066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002067 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 protected int mPaddingRight;
2069 /**
2070 * The top padding in pixels, that is the distance in pixels between the
2071 * top edge of this view and the top edge of its content.
2072 * {@hide}
2073 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002074 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 protected int mPaddingTop;
2076 /**
2077 * The bottom padding in pixels, that is the distance in pixels between the
2078 * bottom edge of this view and the bottom edge of its content.
2079 * {@hide}
2080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002081 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 protected int mPaddingBottom;
2083
2084 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002085 * Briefly describes the view and is primarily used for accessibility support.
2086 */
2087 private CharSequence mContentDescription;
2088
2089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 * Cache the paddingRight set by the user to append to the scrollbar's size.
2091 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002092 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 int mUserPaddingRight;
2094
2095 /**
2096 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2097 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002098 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 int mUserPaddingBottom;
2100
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002101 /**
Adam Powell20232d02010-12-08 21:08:53 -08002102 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2103 */
2104 @ViewDebug.ExportedProperty(category = "padding")
2105 int mUserPaddingLeft;
2106
2107 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002108 * @hide
2109 */
2110 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2111 /**
2112 * @hide
2113 */
2114 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115
2116 private Resources mResources = null;
2117
2118 private Drawable mBGDrawable;
2119
2120 private int mBackgroundResource;
2121 private boolean mBackgroundSizeChanged;
2122
2123 /**
2124 * Listener used to dispatch focus change events.
2125 * This field should be made private, so it is hidden from the SDK.
2126 * {@hide}
2127 */
2128 protected OnFocusChangeListener mOnFocusChangeListener;
2129
2130 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002131 * Listeners for layout change events.
2132 */
2133 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2134
2135 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002136 * Listeners for attach events.
2137 */
2138 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2139
2140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 * Listener used to dispatch click events.
2142 * This field should be made private, so it is hidden from the SDK.
2143 * {@hide}
2144 */
2145 protected OnClickListener mOnClickListener;
2146
2147 /**
2148 * Listener used to dispatch long click events.
2149 * This field should be made private, so it is hidden from the SDK.
2150 * {@hide}
2151 */
2152 protected OnLongClickListener mOnLongClickListener;
2153
2154 /**
2155 * Listener used to build the context menu.
2156 * This field should be made private, so it is hidden from the SDK.
2157 * {@hide}
2158 */
2159 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2160
2161 private OnKeyListener mOnKeyListener;
2162
2163 private OnTouchListener mOnTouchListener;
2164
Jeff Brown33bbfd22011-02-24 20:55:35 -08002165 private OnGenericMotionListener mOnGenericMotionListener;
2166
Chris Tate32affef2010-10-18 15:29:21 -07002167 private OnDragListener mOnDragListener;
2168
Joe Onorato664644d2011-01-23 17:53:23 -08002169 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2170
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 /**
2172 * The application environment this view lives in.
2173 * This field should be made private, so it is hidden from the SDK.
2174 * {@hide}
2175 */
2176 protected Context mContext;
2177
2178 private ScrollabilityCache mScrollCache;
2179
2180 private int[] mDrawableState = null;
2181
Romain Guy0211a0a2011-02-14 16:34:59 -08002182 /**
2183 * Set to true when drawing cache is enabled and cannot be created.
2184 *
2185 * @hide
2186 */
2187 public boolean mCachingFailed;
2188
Romain Guy02890fd2010-08-06 17:58:44 -07002189 private Bitmap mDrawingCache;
2190 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002191 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002192 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193
2194 /**
2195 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2196 * the user may specify which view to go to next.
2197 */
2198 private int mNextFocusLeftId = View.NO_ID;
2199
2200 /**
2201 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2202 * the user may specify which view to go to next.
2203 */
2204 private int mNextFocusRightId = View.NO_ID;
2205
2206 /**
2207 * When this view has focus and the next focus is {@link #FOCUS_UP},
2208 * the user may specify which view to go to next.
2209 */
2210 private int mNextFocusUpId = View.NO_ID;
2211
2212 /**
2213 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2214 * the user may specify which view to go to next.
2215 */
2216 private int mNextFocusDownId = View.NO_ID;
2217
Jeff Brown4e6319b2010-12-13 10:36:51 -08002218 /**
2219 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2220 * the user may specify which view to go to next.
2221 */
2222 int mNextFocusForwardId = View.NO_ID;
2223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002225 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002226 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228 private UnsetPressedState mUnsetPressedState;
2229
2230 /**
2231 * Whether the long press's action has been invoked. The tap's action is invoked on the
2232 * up event while a long press is invoked as soon as the long press duration is reached, so
2233 * a long press could be performed before the tap is checked, in which case the tap's action
2234 * should not be invoked.
2235 */
2236 private boolean mHasPerformedLongPress;
2237
2238 /**
2239 * The minimum height of the view. We'll try our best to have the height
2240 * of this view to at least this amount.
2241 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002242 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002243 private int mMinHeight;
2244
2245 /**
2246 * The minimum width of the view. We'll try our best to have the width
2247 * of this view to at least this amount.
2248 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002249 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002250 private int mMinWidth;
2251
2252 /**
2253 * The delegate to handle touch events that are physically in this view
2254 * but should be handled by another view.
2255 */
2256 private TouchDelegate mTouchDelegate = null;
2257
2258 /**
2259 * Solid color to use as a background when creating the drawing cache. Enables
2260 * the cache to use 16 bit bitmaps instead of 32 bit.
2261 */
2262 private int mDrawingCacheBackgroundColor = 0;
2263
2264 /**
2265 * Special tree observer used when mAttachInfo is null.
2266 */
2267 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002268
Adam Powelle14579b2009-12-16 18:39:52 -08002269 /**
2270 * Cache the touch slop from the context that created the view.
2271 */
2272 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002274 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002275 * Object that handles automatic animation of view properties.
2276 */
2277 private ViewPropertyAnimator mAnimator = null;
2278
2279 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002280 * Flag indicating that a drag can cross window boundaries. When
2281 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2282 * with this flag set, all visible applications will be able to participate
2283 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002284 *
2285 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002286 */
2287 public static final int DRAG_FLAG_GLOBAL = 1;
2288
2289 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002290 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2291 */
2292 private float mVerticalScrollFactor;
2293
2294 /**
Adam Powell20232d02010-12-08 21:08:53 -08002295 * Position of the vertical scroll bar.
2296 */
2297 private int mVerticalScrollbarPosition;
2298
2299 /**
2300 * Position the scroll bar at the default position as determined by the system.
2301 */
2302 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2303
2304 /**
2305 * Position the scroll bar along the left edge.
2306 */
2307 public static final int SCROLLBAR_POSITION_LEFT = 1;
2308
2309 /**
2310 * Position the scroll bar along the right edge.
2311 */
2312 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2313
2314 /**
Romain Guy171c5922011-01-06 10:04:23 -08002315 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002316 *
2317 * @see #getLayerType()
2318 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002319 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002320 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002321 */
2322 public static final int LAYER_TYPE_NONE = 0;
2323
2324 /**
2325 * <p>Indicates that the view has a software layer. A software layer is backed
2326 * by a bitmap and causes the view to be rendered using Android's software
2327 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002328 *
Romain Guy171c5922011-01-06 10:04:23 -08002329 * <p>Software layers have various usages:</p>
2330 * <p>When the application is not using hardware acceleration, a software layer
2331 * is useful to apply a specific color filter and/or blending mode and/or
2332 * translucency to a view and all its children.</p>
2333 * <p>When the application is using hardware acceleration, a software layer
2334 * is useful to render drawing primitives not supported by the hardware
2335 * accelerated pipeline. It can also be used to cache a complex view tree
2336 * into a texture and reduce the complexity of drawing operations. For instance,
2337 * when animating a complex view tree with a translation, a software layer can
2338 * be used to render the view tree only once.</p>
2339 * <p>Software layers should be avoided when the affected view tree updates
2340 * often. Every update will require to re-render the software layer, which can
2341 * potentially be slow (particularly when hardware acceleration is turned on
2342 * since the layer will have to be uploaded into a hardware texture after every
2343 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002344 *
2345 * @see #getLayerType()
2346 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002347 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002348 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002349 */
2350 public static final int LAYER_TYPE_SOFTWARE = 1;
2351
2352 /**
2353 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2354 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2355 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2356 * rendering pipeline, but only if hardware acceleration is turned on for the
2357 * view hierarchy. When hardware acceleration is turned off, hardware layers
2358 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002359 *
Romain Guy171c5922011-01-06 10:04:23 -08002360 * <p>A hardware layer is useful to apply a specific color filter and/or
2361 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002362 * <p>A hardware layer can be used to cache a complex view tree into a
2363 * texture and reduce the complexity of drawing operations. For instance,
2364 * when animating a complex view tree with a translation, a hardware layer can
2365 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002366 * <p>A hardware layer can also be used to increase the rendering quality when
2367 * rotation transformations are applied on a view. It can also be used to
2368 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002369 *
2370 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002371 * @see #setLayerType(int, android.graphics.Paint)
2372 * @see #LAYER_TYPE_NONE
2373 * @see #LAYER_TYPE_SOFTWARE
2374 */
2375 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002376
Romain Guy3aaff3a2011-01-12 14:18:47 -08002377 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2378 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2379 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2380 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2381 })
Romain Guy171c5922011-01-06 10:04:23 -08002382 int mLayerType = LAYER_TYPE_NONE;
2383 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002384 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002385
2386 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002387 * Consistency verifier for debugging purposes.
2388 * @hide
2389 */
2390 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2391 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2392 new InputEventConsistencyVerifier(this, 0) : null;
2393
2394 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002395 * Simple constructor to use when creating a view from code.
2396 *
2397 * @param context The Context the view is running in, through which it can
2398 * access the current theme, resources, etc.
2399 */
2400 public View(Context context) {
2401 mContext = context;
2402 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002403 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002404 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002405 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002406 }
2407
2408 /**
2409 * Constructor that is called when inflating a view from XML. This is called
2410 * when a view is being constructed from an XML file, supplying attributes
2411 * that were specified in the XML file. This version uses a default style of
2412 * 0, so the only attribute values applied are those in the Context's Theme
2413 * and the given AttributeSet.
2414 *
2415 * <p>
2416 * The method onFinishInflate() will be called after all children have been
2417 * added.
2418 *
2419 * @param context The Context the view is running in, through which it can
2420 * access the current theme, resources, etc.
2421 * @param attrs The attributes of the XML tag that is inflating the view.
2422 * @see #View(Context, AttributeSet, int)
2423 */
2424 public View(Context context, AttributeSet attrs) {
2425 this(context, attrs, 0);
2426 }
2427
2428 /**
2429 * Perform inflation from XML and apply a class-specific base style. This
2430 * constructor of View allows subclasses to use their own base style when
2431 * they are inflating. For example, a Button class's constructor would call
2432 * this version of the super class constructor and supply
2433 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2434 * the theme's button style to modify all of the base view attributes (in
2435 * particular its background) as well as the Button class's attributes.
2436 *
2437 * @param context The Context the view is running in, through which it can
2438 * access the current theme, resources, etc.
2439 * @param attrs The attributes of the XML tag that is inflating the view.
2440 * @param defStyle The default style to apply to this view. If 0, no style
2441 * will be applied (beyond what is included in the theme). This may
2442 * either be an attribute resource, whose value will be retrieved
2443 * from the current theme, or an explicit style resource.
2444 * @see #View(Context, AttributeSet)
2445 */
2446 public View(Context context, AttributeSet attrs, int defStyle) {
2447 this(context);
2448
2449 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2450 defStyle, 0);
2451
2452 Drawable background = null;
2453
2454 int leftPadding = -1;
2455 int topPadding = -1;
2456 int rightPadding = -1;
2457 int bottomPadding = -1;
2458
2459 int padding = -1;
2460
2461 int viewFlagValues = 0;
2462 int viewFlagMasks = 0;
2463
2464 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002466 int x = 0;
2467 int y = 0;
2468
Chet Haase73066682010-11-29 15:55:32 -08002469 float tx = 0;
2470 float ty = 0;
2471 float rotation = 0;
2472 float rotationX = 0;
2473 float rotationY = 0;
2474 float sx = 1f;
2475 float sy = 1f;
2476 boolean transformSet = false;
2477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2479
Adam Powell637d3372010-08-25 14:37:03 -07002480 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002481 final int N = a.getIndexCount();
2482 for (int i = 0; i < N; i++) {
2483 int attr = a.getIndex(i);
2484 switch (attr) {
2485 case com.android.internal.R.styleable.View_background:
2486 background = a.getDrawable(attr);
2487 break;
2488 case com.android.internal.R.styleable.View_padding:
2489 padding = a.getDimensionPixelSize(attr, -1);
2490 break;
2491 case com.android.internal.R.styleable.View_paddingLeft:
2492 leftPadding = a.getDimensionPixelSize(attr, -1);
2493 break;
2494 case com.android.internal.R.styleable.View_paddingTop:
2495 topPadding = a.getDimensionPixelSize(attr, -1);
2496 break;
2497 case com.android.internal.R.styleable.View_paddingRight:
2498 rightPadding = a.getDimensionPixelSize(attr, -1);
2499 break;
2500 case com.android.internal.R.styleable.View_paddingBottom:
2501 bottomPadding = a.getDimensionPixelSize(attr, -1);
2502 break;
2503 case com.android.internal.R.styleable.View_scrollX:
2504 x = a.getDimensionPixelOffset(attr, 0);
2505 break;
2506 case com.android.internal.R.styleable.View_scrollY:
2507 y = a.getDimensionPixelOffset(attr, 0);
2508 break;
Chet Haase73066682010-11-29 15:55:32 -08002509 case com.android.internal.R.styleable.View_alpha:
2510 setAlpha(a.getFloat(attr, 1f));
2511 break;
2512 case com.android.internal.R.styleable.View_transformPivotX:
2513 setPivotX(a.getDimensionPixelOffset(attr, 0));
2514 break;
2515 case com.android.internal.R.styleable.View_transformPivotY:
2516 setPivotY(a.getDimensionPixelOffset(attr, 0));
2517 break;
2518 case com.android.internal.R.styleable.View_translationX:
2519 tx = a.getDimensionPixelOffset(attr, 0);
2520 transformSet = true;
2521 break;
2522 case com.android.internal.R.styleable.View_translationY:
2523 ty = a.getDimensionPixelOffset(attr, 0);
2524 transformSet = true;
2525 break;
2526 case com.android.internal.R.styleable.View_rotation:
2527 rotation = a.getFloat(attr, 0);
2528 transformSet = true;
2529 break;
2530 case com.android.internal.R.styleable.View_rotationX:
2531 rotationX = a.getFloat(attr, 0);
2532 transformSet = true;
2533 break;
2534 case com.android.internal.R.styleable.View_rotationY:
2535 rotationY = a.getFloat(attr, 0);
2536 transformSet = true;
2537 break;
2538 case com.android.internal.R.styleable.View_scaleX:
2539 sx = a.getFloat(attr, 1f);
2540 transformSet = true;
2541 break;
2542 case com.android.internal.R.styleable.View_scaleY:
2543 sy = a.getFloat(attr, 1f);
2544 transformSet = true;
2545 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002546 case com.android.internal.R.styleable.View_id:
2547 mID = a.getResourceId(attr, NO_ID);
2548 break;
2549 case com.android.internal.R.styleable.View_tag:
2550 mTag = a.getText(attr);
2551 break;
2552 case com.android.internal.R.styleable.View_fitsSystemWindows:
2553 if (a.getBoolean(attr, false)) {
2554 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2555 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2556 }
2557 break;
2558 case com.android.internal.R.styleable.View_focusable:
2559 if (a.getBoolean(attr, false)) {
2560 viewFlagValues |= FOCUSABLE;
2561 viewFlagMasks |= FOCUSABLE_MASK;
2562 }
2563 break;
2564 case com.android.internal.R.styleable.View_focusableInTouchMode:
2565 if (a.getBoolean(attr, false)) {
2566 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2567 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2568 }
2569 break;
2570 case com.android.internal.R.styleable.View_clickable:
2571 if (a.getBoolean(attr, false)) {
2572 viewFlagValues |= CLICKABLE;
2573 viewFlagMasks |= CLICKABLE;
2574 }
2575 break;
2576 case com.android.internal.R.styleable.View_longClickable:
2577 if (a.getBoolean(attr, false)) {
2578 viewFlagValues |= LONG_CLICKABLE;
2579 viewFlagMasks |= LONG_CLICKABLE;
2580 }
2581 break;
2582 case com.android.internal.R.styleable.View_saveEnabled:
2583 if (!a.getBoolean(attr, true)) {
2584 viewFlagValues |= SAVE_DISABLED;
2585 viewFlagMasks |= SAVE_DISABLED_MASK;
2586 }
2587 break;
2588 case com.android.internal.R.styleable.View_duplicateParentState:
2589 if (a.getBoolean(attr, false)) {
2590 viewFlagValues |= DUPLICATE_PARENT_STATE;
2591 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2592 }
2593 break;
2594 case com.android.internal.R.styleable.View_visibility:
2595 final int visibility = a.getInt(attr, 0);
2596 if (visibility != 0) {
2597 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2598 viewFlagMasks |= VISIBILITY_MASK;
2599 }
2600 break;
2601 case com.android.internal.R.styleable.View_drawingCacheQuality:
2602 final int cacheQuality = a.getInt(attr, 0);
2603 if (cacheQuality != 0) {
2604 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2605 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2606 }
2607 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002608 case com.android.internal.R.styleable.View_contentDescription:
2609 mContentDescription = a.getString(attr);
2610 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002611 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2612 if (!a.getBoolean(attr, true)) {
2613 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2614 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2615 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002616 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002617 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2618 if (!a.getBoolean(attr, true)) {
2619 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2620 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2621 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002622 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002623 case R.styleable.View_scrollbars:
2624 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2625 if (scrollbars != SCROLLBARS_NONE) {
2626 viewFlagValues |= scrollbars;
2627 viewFlagMasks |= SCROLLBARS_MASK;
2628 initializeScrollbars(a);
2629 }
2630 break;
2631 case R.styleable.View_fadingEdge:
2632 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2633 if (fadingEdge != FADING_EDGE_NONE) {
2634 viewFlagValues |= fadingEdge;
2635 viewFlagMasks |= FADING_EDGE_MASK;
2636 initializeFadingEdge(a);
2637 }
2638 break;
2639 case R.styleable.View_scrollbarStyle:
2640 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2641 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2642 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2643 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2644 }
2645 break;
2646 case R.styleable.View_isScrollContainer:
2647 setScrollContainer = true;
2648 if (a.getBoolean(attr, false)) {
2649 setScrollContainer(true);
2650 }
2651 break;
2652 case com.android.internal.R.styleable.View_keepScreenOn:
2653 if (a.getBoolean(attr, false)) {
2654 viewFlagValues |= KEEP_SCREEN_ON;
2655 viewFlagMasks |= KEEP_SCREEN_ON;
2656 }
2657 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002658 case R.styleable.View_filterTouchesWhenObscured:
2659 if (a.getBoolean(attr, false)) {
2660 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2661 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2662 }
2663 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664 case R.styleable.View_nextFocusLeft:
2665 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2666 break;
2667 case R.styleable.View_nextFocusRight:
2668 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2669 break;
2670 case R.styleable.View_nextFocusUp:
2671 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2672 break;
2673 case R.styleable.View_nextFocusDown:
2674 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2675 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002676 case R.styleable.View_nextFocusForward:
2677 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2678 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002679 case R.styleable.View_minWidth:
2680 mMinWidth = a.getDimensionPixelSize(attr, 0);
2681 break;
2682 case R.styleable.View_minHeight:
2683 mMinHeight = a.getDimensionPixelSize(attr, 0);
2684 break;
Romain Guy9a817362009-05-01 10:57:14 -07002685 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002686 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002687 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002688 + "be used within a restricted context");
2689 }
2690
Romain Guy9a817362009-05-01 10:57:14 -07002691 final String handlerName = a.getString(attr);
2692 if (handlerName != null) {
2693 setOnClickListener(new OnClickListener() {
2694 private Method mHandler;
2695
2696 public void onClick(View v) {
2697 if (mHandler == null) {
2698 try {
2699 mHandler = getContext().getClass().getMethod(handlerName,
2700 View.class);
2701 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002702 int id = getId();
2703 String idText = id == NO_ID ? "" : " with id '"
2704 + getContext().getResources().getResourceEntryName(
2705 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002706 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002707 handlerName + "(View) in the activity "
2708 + getContext().getClass() + " for onClick handler"
2709 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002710 }
2711 }
2712
2713 try {
2714 mHandler.invoke(getContext(), View.this);
2715 } catch (IllegalAccessException e) {
2716 throw new IllegalStateException("Could not execute non "
2717 + "public method of the activity", e);
2718 } catch (InvocationTargetException e) {
2719 throw new IllegalStateException("Could not execute "
2720 + "method of the activity", e);
2721 }
2722 }
2723 });
2724 }
2725 break;
Adam Powell637d3372010-08-25 14:37:03 -07002726 case R.styleable.View_overScrollMode:
2727 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2728 break;
Adam Powell20232d02010-12-08 21:08:53 -08002729 case R.styleable.View_verticalScrollbarPosition:
2730 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2731 break;
Romain Guy171c5922011-01-06 10:04:23 -08002732 case R.styleable.View_layerType:
2733 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2734 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 }
2736 }
2737
Adam Powell637d3372010-08-25 14:37:03 -07002738 setOverScrollMode(overScrollMode);
2739
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002740 if (background != null) {
2741 setBackgroundDrawable(background);
2742 }
2743
2744 if (padding >= 0) {
2745 leftPadding = padding;
2746 topPadding = padding;
2747 rightPadding = padding;
2748 bottomPadding = padding;
2749 }
2750
2751 // If the user specified the padding (either with android:padding or
2752 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2753 // use the default padding or the padding from the background drawable
2754 // (stored at this point in mPadding*)
2755 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2756 topPadding >= 0 ? topPadding : mPaddingTop,
2757 rightPadding >= 0 ? rightPadding : mPaddingRight,
2758 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2759
2760 if (viewFlagMasks != 0) {
2761 setFlags(viewFlagValues, viewFlagMasks);
2762 }
2763
2764 // Needs to be called after mViewFlags is set
2765 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2766 recomputePadding();
2767 }
2768
2769 if (x != 0 || y != 0) {
2770 scrollTo(x, y);
2771 }
2772
Chet Haase73066682010-11-29 15:55:32 -08002773 if (transformSet) {
2774 setTranslationX(tx);
2775 setTranslationY(ty);
2776 setRotation(rotation);
2777 setRotationX(rotationX);
2778 setRotationY(rotationY);
2779 setScaleX(sx);
2780 setScaleY(sy);
2781 }
2782
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002783 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2784 setScrollContainer(true);
2785 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002786
2787 computeOpaqueFlags();
2788
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002789 a.recycle();
2790 }
2791
2792 /**
2793 * Non-public constructor for use in testing
2794 */
2795 View() {
2796 }
2797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002798 /**
2799 * <p>
2800 * Initializes the fading edges from a given set of styled attributes. This
2801 * method should be called by subclasses that need fading edges and when an
2802 * instance of these subclasses is created programmatically rather than
2803 * being inflated from XML. This method is automatically called when the XML
2804 * is inflated.
2805 * </p>
2806 *
2807 * @param a the styled attributes set to initialize the fading edges from
2808 */
2809 protected void initializeFadingEdge(TypedArray a) {
2810 initScrollCache();
2811
2812 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2813 R.styleable.View_fadingEdgeLength,
2814 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2815 }
2816
2817 /**
2818 * Returns the size of the vertical faded edges used to indicate that more
2819 * content in this view is visible.
2820 *
2821 * @return The size in pixels of the vertical faded edge or 0 if vertical
2822 * faded edges are not enabled for this view.
2823 * @attr ref android.R.styleable#View_fadingEdgeLength
2824 */
2825 public int getVerticalFadingEdgeLength() {
2826 if (isVerticalFadingEdgeEnabled()) {
2827 ScrollabilityCache cache = mScrollCache;
2828 if (cache != null) {
2829 return cache.fadingEdgeLength;
2830 }
2831 }
2832 return 0;
2833 }
2834
2835 /**
2836 * Set the size of the faded edge used to indicate that more content in this
2837 * view is available. Will not change whether the fading edge is enabled; use
2838 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2839 * to enable the fading edge for the vertical or horizontal fading edges.
2840 *
2841 * @param length The size in pixels of the faded edge used to indicate that more
2842 * content in this view is visible.
2843 */
2844 public void setFadingEdgeLength(int length) {
2845 initScrollCache();
2846 mScrollCache.fadingEdgeLength = length;
2847 }
2848
2849 /**
2850 * Returns the size of the horizontal faded edges used to indicate that more
2851 * content in this view is visible.
2852 *
2853 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2854 * faded edges are not enabled for this view.
2855 * @attr ref android.R.styleable#View_fadingEdgeLength
2856 */
2857 public int getHorizontalFadingEdgeLength() {
2858 if (isHorizontalFadingEdgeEnabled()) {
2859 ScrollabilityCache cache = mScrollCache;
2860 if (cache != null) {
2861 return cache.fadingEdgeLength;
2862 }
2863 }
2864 return 0;
2865 }
2866
2867 /**
2868 * Returns the width of the vertical scrollbar.
2869 *
2870 * @return The width in pixels of the vertical scrollbar or 0 if there
2871 * is no vertical scrollbar.
2872 */
2873 public int getVerticalScrollbarWidth() {
2874 ScrollabilityCache cache = mScrollCache;
2875 if (cache != null) {
2876 ScrollBarDrawable scrollBar = cache.scrollBar;
2877 if (scrollBar != null) {
2878 int size = scrollBar.getSize(true);
2879 if (size <= 0) {
2880 size = cache.scrollBarSize;
2881 }
2882 return size;
2883 }
2884 return 0;
2885 }
2886 return 0;
2887 }
2888
2889 /**
2890 * Returns the height of the horizontal scrollbar.
2891 *
2892 * @return The height in pixels of the horizontal scrollbar or 0 if
2893 * there is no horizontal scrollbar.
2894 */
2895 protected int getHorizontalScrollbarHeight() {
2896 ScrollabilityCache cache = mScrollCache;
2897 if (cache != null) {
2898 ScrollBarDrawable scrollBar = cache.scrollBar;
2899 if (scrollBar != null) {
2900 int size = scrollBar.getSize(false);
2901 if (size <= 0) {
2902 size = cache.scrollBarSize;
2903 }
2904 return size;
2905 }
2906 return 0;
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * <p>
2913 * Initializes the scrollbars from a given set of styled attributes. This
2914 * method should be called by subclasses that need scrollbars and when an
2915 * instance of these subclasses is created programmatically rather than
2916 * being inflated from XML. This method is automatically called when the XML
2917 * is inflated.
2918 * </p>
2919 *
2920 * @param a the styled attributes set to initialize the scrollbars from
2921 */
2922 protected void initializeScrollbars(TypedArray a) {
2923 initScrollCache();
2924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002926
Mike Cleronf116bf82009-09-27 19:14:12 -07002927 if (scrollabilityCache.scrollBar == null) {
2928 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2929 }
Joe Malin32736f02011-01-19 16:14:20 -08002930
Romain Guy8bda2482010-03-02 11:42:11 -08002931 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002932
Mike Cleronf116bf82009-09-27 19:14:12 -07002933 if (!fadeScrollbars) {
2934 scrollabilityCache.state = ScrollabilityCache.ON;
2935 }
2936 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002937
2938
Mike Cleronf116bf82009-09-27 19:14:12 -07002939 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2940 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2941 .getScrollBarFadeDuration());
2942 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2943 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2944 ViewConfiguration.getScrollDefaultDelay());
2945
Joe Malin32736f02011-01-19 16:14:20 -08002946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002947 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2948 com.android.internal.R.styleable.View_scrollbarSize,
2949 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2950
2951 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2952 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2953
2954 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2955 if (thumb != null) {
2956 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
2957 }
2958
2959 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
2960 false);
2961 if (alwaysDraw) {
2962 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
2963 }
2964
2965 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
2966 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
2967
2968 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
2969 if (thumb != null) {
2970 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
2971 }
2972
2973 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
2974 false);
2975 if (alwaysDraw) {
2976 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
2977 }
2978
2979 // Re-apply user/background padding so that scrollbar(s) get added
2980 recomputePadding();
2981 }
2982
2983 /**
2984 * <p>
2985 * Initalizes the scrollability cache if necessary.
2986 * </p>
2987 */
2988 private void initScrollCache() {
2989 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07002990 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 }
2992 }
2993
2994 /**
Adam Powell20232d02010-12-08 21:08:53 -08002995 * Set the position of the vertical scroll bar. Should be one of
2996 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
2997 * {@link #SCROLLBAR_POSITION_RIGHT}.
2998 *
2999 * @param position Where the vertical scroll bar should be positioned.
3000 */
3001 public void setVerticalScrollbarPosition(int position) {
3002 if (mVerticalScrollbarPosition != position) {
3003 mVerticalScrollbarPosition = position;
3004 computeOpaqueFlags();
3005 recomputePadding();
3006 }
3007 }
3008
3009 /**
3010 * @return The position where the vertical scroll bar will show, if applicable.
3011 * @see #setVerticalScrollbarPosition(int)
3012 */
3013 public int getVerticalScrollbarPosition() {
3014 return mVerticalScrollbarPosition;
3015 }
3016
3017 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 * Register a callback to be invoked when focus of this view changed.
3019 *
3020 * @param l The callback that will run.
3021 */
3022 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3023 mOnFocusChangeListener = l;
3024 }
3025
3026 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003027 * Add a listener that will be called when the bounds of the view change due to
3028 * layout processing.
3029 *
3030 * @param listener The listener that will be called when layout bounds change.
3031 */
3032 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3033 if (mOnLayoutChangeListeners == null) {
3034 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3035 }
3036 mOnLayoutChangeListeners.add(listener);
3037 }
3038
3039 /**
3040 * Remove a listener for layout changes.
3041 *
3042 * @param listener The listener for layout bounds change.
3043 */
3044 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3045 if (mOnLayoutChangeListeners == null) {
3046 return;
3047 }
3048 mOnLayoutChangeListeners.remove(listener);
3049 }
3050
3051 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003052 * Add a listener for attach state changes.
3053 *
3054 * This listener will be called whenever this view is attached or detached
3055 * from a window. Remove the listener using
3056 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3057 *
3058 * @param listener Listener to attach
3059 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3060 */
3061 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3062 if (mOnAttachStateChangeListeners == null) {
3063 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3064 }
3065 mOnAttachStateChangeListeners.add(listener);
3066 }
3067
3068 /**
3069 * Remove a listener for attach state changes. The listener will receive no further
3070 * notification of window attach/detach events.
3071 *
3072 * @param listener Listener to remove
3073 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3074 */
3075 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3076 if (mOnAttachStateChangeListeners == null) {
3077 return;
3078 }
3079 mOnAttachStateChangeListeners.remove(listener);
3080 }
3081
3082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003083 * Returns the focus-change callback registered for this view.
3084 *
3085 * @return The callback, or null if one is not registered.
3086 */
3087 public OnFocusChangeListener getOnFocusChangeListener() {
3088 return mOnFocusChangeListener;
3089 }
3090
3091 /**
3092 * Register a callback to be invoked when this view is clicked. If this view is not
3093 * clickable, it becomes clickable.
3094 *
3095 * @param l The callback that will run
3096 *
3097 * @see #setClickable(boolean)
3098 */
3099 public void setOnClickListener(OnClickListener l) {
3100 if (!isClickable()) {
3101 setClickable(true);
3102 }
3103 mOnClickListener = l;
3104 }
3105
3106 /**
3107 * Register a callback to be invoked when this view is clicked and held. If this view is not
3108 * long clickable, it becomes long clickable.
3109 *
3110 * @param l The callback that will run
3111 *
3112 * @see #setLongClickable(boolean)
3113 */
3114 public void setOnLongClickListener(OnLongClickListener l) {
3115 if (!isLongClickable()) {
3116 setLongClickable(true);
3117 }
3118 mOnLongClickListener = l;
3119 }
3120
3121 /**
3122 * Register a callback to be invoked when the context menu for this view is
3123 * being built. If this view is not long clickable, it becomes long clickable.
3124 *
3125 * @param l The callback that will run
3126 *
3127 */
3128 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3129 if (!isLongClickable()) {
3130 setLongClickable(true);
3131 }
3132 mOnCreateContextMenuListener = l;
3133 }
3134
3135 /**
3136 * Call this view's OnClickListener, if it is defined.
3137 *
3138 * @return True there was an assigned OnClickListener that was called, false
3139 * otherwise is returned.
3140 */
3141 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003142 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003144 if (mOnClickListener != null) {
3145 playSoundEffect(SoundEffectConstants.CLICK);
3146 mOnClickListener.onClick(this);
3147 return true;
3148 }
3149
3150 return false;
3151 }
3152
3153 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003154 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3155 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003156 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003157 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003158 */
3159 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003160 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003162 boolean handled = false;
3163 if (mOnLongClickListener != null) {
3164 handled = mOnLongClickListener.onLongClick(View.this);
3165 }
3166 if (!handled) {
3167 handled = showContextMenu();
3168 }
3169 if (handled) {
3170 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3171 }
3172 return handled;
3173 }
3174
3175 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003176 * Performs button-related actions during a touch down event.
3177 *
3178 * @param event The event.
3179 * @return True if the down was consumed.
3180 *
3181 * @hide
3182 */
3183 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3184 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3185 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3186 return true;
3187 }
3188 }
3189 return false;
3190 }
3191
3192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003193 * Bring up the context menu for this view.
3194 *
3195 * @return Whether a context menu was displayed.
3196 */
3197 public boolean showContextMenu() {
3198 return getParent().showContextMenuForChild(this);
3199 }
3200
3201 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003202 * Bring up the context menu for this view, referring to the item under the specified point.
3203 *
3204 * @param x The referenced x coordinate.
3205 * @param y The referenced y coordinate.
3206 * @param metaState The keyboard modifiers that were pressed.
3207 * @return Whether a context menu was displayed.
3208 *
3209 * @hide
3210 */
3211 public boolean showContextMenu(float x, float y, int metaState) {
3212 return showContextMenu();
3213 }
3214
3215 /**
Adam Powell6e346362010-07-23 10:18:23 -07003216 * Start an action mode.
3217 *
3218 * @param callback Callback that will control the lifecycle of the action mode
3219 * @return The new action mode if it is started, null otherwise
3220 *
3221 * @see ActionMode
3222 */
3223 public ActionMode startActionMode(ActionMode.Callback callback) {
3224 return getParent().startActionModeForChild(this, callback);
3225 }
3226
3227 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003228 * Register a callback to be invoked when a key is pressed in this view.
3229 * @param l the key listener to attach to this view
3230 */
3231 public void setOnKeyListener(OnKeyListener l) {
3232 mOnKeyListener = l;
3233 }
3234
3235 /**
3236 * Register a callback to be invoked when a touch event is sent to this view.
3237 * @param l the touch listener to attach to this view
3238 */
3239 public void setOnTouchListener(OnTouchListener l) {
3240 mOnTouchListener = l;
3241 }
3242
3243 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003244 * Register a callback to be invoked when a generic motion event is sent to this view.
3245 * @param l the generic motion listener to attach to this view
3246 */
3247 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3248 mOnGenericMotionListener = l;
3249 }
3250
3251 /**
Joe Malin32736f02011-01-19 16:14:20 -08003252 * Register a drag event listener callback object for this View. The parameter is
3253 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3254 * View, the system calls the
3255 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3256 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003257 */
3258 public void setOnDragListener(OnDragListener l) {
3259 mOnDragListener = l;
3260 }
3261
3262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3264 *
3265 * Note: this does not check whether this {@link View} should get focus, it just
3266 * gives it focus no matter what. It should only be called internally by framework
3267 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3268 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003269 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3270 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003271 * focus moved when requestFocus() is called. It may not always
3272 * apply, in which case use the default View.FOCUS_DOWN.
3273 * @param previouslyFocusedRect The rectangle of the view that had focus
3274 * prior in this View's coordinate system.
3275 */
3276 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3277 if (DBG) {
3278 System.out.println(this + " requestFocus()");
3279 }
3280
3281 if ((mPrivateFlags & FOCUSED) == 0) {
3282 mPrivateFlags |= FOCUSED;
3283
3284 if (mParent != null) {
3285 mParent.requestChildFocus(this, this);
3286 }
3287
3288 onFocusChanged(true, direction, previouslyFocusedRect);
3289 refreshDrawableState();
3290 }
3291 }
3292
3293 /**
3294 * Request that a rectangle of this view be visible on the screen,
3295 * scrolling if necessary just enough.
3296 *
3297 * <p>A View should call this if it maintains some notion of which part
3298 * of its content is interesting. For example, a text editing view
3299 * should call this when its cursor moves.
3300 *
3301 * @param rectangle The rectangle.
3302 * @return Whether any parent scrolled.
3303 */
3304 public boolean requestRectangleOnScreen(Rect rectangle) {
3305 return requestRectangleOnScreen(rectangle, false);
3306 }
3307
3308 /**
3309 * Request that a rectangle of this view be visible on the screen,
3310 * scrolling if necessary just enough.
3311 *
3312 * <p>A View should call this if it maintains some notion of which part
3313 * of its content is interesting. For example, a text editing view
3314 * should call this when its cursor moves.
3315 *
3316 * <p>When <code>immediate</code> is set to true, scrolling will not be
3317 * animated.
3318 *
3319 * @param rectangle The rectangle.
3320 * @param immediate True to forbid animated scrolling, false otherwise
3321 * @return Whether any parent scrolled.
3322 */
3323 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3324 View child = this;
3325 ViewParent parent = mParent;
3326 boolean scrolled = false;
3327 while (parent != null) {
3328 scrolled |= parent.requestChildRectangleOnScreen(child,
3329 rectangle, immediate);
3330
3331 // offset rect so next call has the rectangle in the
3332 // coordinate system of its direct child.
3333 rectangle.offset(child.getLeft(), child.getTop());
3334 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3335
3336 if (!(parent instanceof View)) {
3337 break;
3338 }
Romain Guy8506ab42009-06-11 17:35:47 -07003339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003340 child = (View) parent;
3341 parent = child.getParent();
3342 }
3343 return scrolled;
3344 }
3345
3346 /**
3347 * Called when this view wants to give up focus. This will cause
3348 * {@link #onFocusChanged} to be called.
3349 */
3350 public void clearFocus() {
3351 if (DBG) {
3352 System.out.println(this + " clearFocus()");
3353 }
3354
3355 if ((mPrivateFlags & FOCUSED) != 0) {
3356 mPrivateFlags &= ~FOCUSED;
3357
3358 if (mParent != null) {
3359 mParent.clearChildFocus(this);
3360 }
3361
3362 onFocusChanged(false, 0, null);
3363 refreshDrawableState();
3364 }
3365 }
3366
3367 /**
3368 * Called to clear the focus of a view that is about to be removed.
3369 * Doesn't call clearChildFocus, which prevents this view from taking
3370 * focus again before it has been removed from the parent
3371 */
3372 void clearFocusForRemoval() {
3373 if ((mPrivateFlags & FOCUSED) != 0) {
3374 mPrivateFlags &= ~FOCUSED;
3375
3376 onFocusChanged(false, 0, null);
3377 refreshDrawableState();
3378 }
3379 }
3380
3381 /**
3382 * Called internally by the view system when a new view is getting focus.
3383 * This is what clears the old focus.
3384 */
3385 void unFocus() {
3386 if (DBG) {
3387 System.out.println(this + " unFocus()");
3388 }
3389
3390 if ((mPrivateFlags & FOCUSED) != 0) {
3391 mPrivateFlags &= ~FOCUSED;
3392
3393 onFocusChanged(false, 0, null);
3394 refreshDrawableState();
3395 }
3396 }
3397
3398 /**
3399 * Returns true if this view has focus iteself, or is the ancestor of the
3400 * view that has focus.
3401 *
3402 * @return True if this view has or contains focus, false otherwise.
3403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003404 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003405 public boolean hasFocus() {
3406 return (mPrivateFlags & FOCUSED) != 0;
3407 }
3408
3409 /**
3410 * Returns true if this view is focusable or if it contains a reachable View
3411 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3412 * is a View whose parents do not block descendants focus.
3413 *
3414 * Only {@link #VISIBLE} views are considered focusable.
3415 *
3416 * @return True if the view is focusable or if the view contains a focusable
3417 * View, false otherwise.
3418 *
3419 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3420 */
3421 public boolean hasFocusable() {
3422 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3423 }
3424
3425 /**
3426 * Called by the view system when the focus state of this view changes.
3427 * When the focus change event is caused by directional navigation, direction
3428 * and previouslyFocusedRect provide insight into where the focus is coming from.
3429 * When overriding, be sure to call up through to the super class so that
3430 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003431 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003432 * @param gainFocus True if the View has focus; false otherwise.
3433 * @param direction The direction focus has moved when requestFocus()
3434 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003435 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3436 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3437 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003438 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3439 * system, of the previously focused view. If applicable, this will be
3440 * passed in as finer grained information about where the focus is coming
3441 * from (in addition to direction). Will be <code>null</code> otherwise.
3442 */
3443 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003444 if (gainFocus) {
3445 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3446 }
3447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003448 InputMethodManager imm = InputMethodManager.peekInstance();
3449 if (!gainFocus) {
3450 if (isPressed()) {
3451 setPressed(false);
3452 }
3453 if (imm != null && mAttachInfo != null
3454 && mAttachInfo.mHasWindowFocus) {
3455 imm.focusOut(this);
3456 }
Romain Guya2431d02009-04-30 16:30:00 -07003457 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003458 } else if (imm != null && mAttachInfo != null
3459 && mAttachInfo.mHasWindowFocus) {
3460 imm.focusIn(this);
3461 }
Romain Guy8506ab42009-06-11 17:35:47 -07003462
Romain Guy0fd89bf2011-01-26 15:41:30 -08003463 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 if (mOnFocusChangeListener != null) {
3465 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3466 }
Joe Malin32736f02011-01-19 16:14:20 -08003467
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003468 if (mAttachInfo != null) {
3469 mAttachInfo.mKeyDispatchState.reset(this);
3470 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 }
3472
3473 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003474 * {@inheritDoc}
3475 */
3476 public void sendAccessibilityEvent(int eventType) {
3477 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3478 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3479 }
3480 }
3481
3482 /**
3483 * {@inheritDoc}
3484 */
3485 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003486 if (!isShown()) {
3487 return;
3488 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003489
3490 // Populate these here since they are related to the View that
3491 // sends the event and should not be modified while dispatching
3492 // to descendants.
svetoslavganov75986cf2009-05-14 22:28:01 -07003493 event.setClassName(getClass().getName());
3494 event.setPackageName(getContext().getPackageName());
3495 event.setEnabled(isEnabled());
3496 event.setContentDescription(mContentDescription);
3497
3498 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3499 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3500 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3501 event.setItemCount(focusablesTempList.size());
3502 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3503 focusablesTempList.clear();
3504 }
3505
3506 dispatchPopulateAccessibilityEvent(event);
3507
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003508 // In the beginning we called #isShown(), so we know that getParent() is not null.
3509 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003510 }
3511
3512 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003513 * Dispatches an {@link AccessibilityEvent} to the {@link View} children to be populated.
3514 * This method first calls {@link #onPopulateAccessibilityEvent(AccessibilityEvent)}
3515 * on this view allowing it to populate information about itself and also decide
3516 * whether to intercept the population i.e. to prevent its children from populating
3517 * the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07003518 *
3519 * @param event The event.
3520 *
3521 * @return True if the event population was completed.
3522 */
3523 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003524 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003525 return false;
3526 }
3527
3528 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003529 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3530 * giving a chance to this View to populate the accessibility evnet with
3531 * information about itself.
3532 *
3533 * @param event The accessibility event which to populate.
3534 */
3535 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3536
3537 }
3538
3539 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003540 * Gets the {@link View} description. It briefly describes the view and is
3541 * primarily used for accessibility support. Set this property to enable
3542 * better accessibility support for your application. This is especially
3543 * true for views that do not have textual representation (For example,
3544 * ImageButton).
3545 *
3546 * @return The content descriptiopn.
3547 *
3548 * @attr ref android.R.styleable#View_contentDescription
3549 */
3550 public CharSequence getContentDescription() {
3551 return mContentDescription;
3552 }
3553
3554 /**
3555 * Sets the {@link View} description. It briefly describes the view and is
3556 * primarily used for accessibility support. Set this property to enable
3557 * better accessibility support for your application. This is especially
3558 * true for views that do not have textual representation (For example,
3559 * ImageButton).
3560 *
3561 * @param contentDescription The content description.
3562 *
3563 * @attr ref android.R.styleable#View_contentDescription
3564 */
3565 public void setContentDescription(CharSequence contentDescription) {
3566 mContentDescription = contentDescription;
3567 }
3568
3569 /**
Romain Guya2431d02009-04-30 16:30:00 -07003570 * Invoked whenever this view loses focus, either by losing window focus or by losing
3571 * focus within its window. This method can be used to clear any state tied to the
3572 * focus. For instance, if a button is held pressed with the trackball and the window
3573 * loses focus, this method can be used to cancel the press.
3574 *
3575 * Subclasses of View overriding this method should always call super.onFocusLost().
3576 *
3577 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003578 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003579 *
3580 * @hide pending API council approval
3581 */
3582 protected void onFocusLost() {
3583 resetPressedState();
3584 }
3585
3586 private void resetPressedState() {
3587 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3588 return;
3589 }
3590
3591 if (isPressed()) {
3592 setPressed(false);
3593
3594 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003595 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003596 }
3597 }
3598 }
3599
3600 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003601 * Returns true if this view has focus
3602 *
3603 * @return True if this view has focus, false otherwise.
3604 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003605 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003606 public boolean isFocused() {
3607 return (mPrivateFlags & FOCUSED) != 0;
3608 }
3609
3610 /**
3611 * Find the view in the hierarchy rooted at this view that currently has
3612 * focus.
3613 *
3614 * @return The view that currently has focus, or null if no focused view can
3615 * be found.
3616 */
3617 public View findFocus() {
3618 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3619 }
3620
3621 /**
3622 * Change whether this view is one of the set of scrollable containers in
3623 * its window. This will be used to determine whether the window can
3624 * resize or must pan when a soft input area is open -- scrollable
3625 * containers allow the window to use resize mode since the container
3626 * will appropriately shrink.
3627 */
3628 public void setScrollContainer(boolean isScrollContainer) {
3629 if (isScrollContainer) {
3630 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3631 mAttachInfo.mScrollContainers.add(this);
3632 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3633 }
3634 mPrivateFlags |= SCROLL_CONTAINER;
3635 } else {
3636 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3637 mAttachInfo.mScrollContainers.remove(this);
3638 }
3639 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3640 }
3641 }
3642
3643 /**
3644 * Returns the quality of the drawing cache.
3645 *
3646 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3647 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3648 *
3649 * @see #setDrawingCacheQuality(int)
3650 * @see #setDrawingCacheEnabled(boolean)
3651 * @see #isDrawingCacheEnabled()
3652 *
3653 * @attr ref android.R.styleable#View_drawingCacheQuality
3654 */
3655 public int getDrawingCacheQuality() {
3656 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3657 }
3658
3659 /**
3660 * Set the drawing cache quality of this view. This value is used only when the
3661 * drawing cache is enabled
3662 *
3663 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3664 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3665 *
3666 * @see #getDrawingCacheQuality()
3667 * @see #setDrawingCacheEnabled(boolean)
3668 * @see #isDrawingCacheEnabled()
3669 *
3670 * @attr ref android.R.styleable#View_drawingCacheQuality
3671 */
3672 public void setDrawingCacheQuality(int quality) {
3673 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3674 }
3675
3676 /**
3677 * Returns whether the screen should remain on, corresponding to the current
3678 * value of {@link #KEEP_SCREEN_ON}.
3679 *
3680 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3681 *
3682 * @see #setKeepScreenOn(boolean)
3683 *
3684 * @attr ref android.R.styleable#View_keepScreenOn
3685 */
3686 public boolean getKeepScreenOn() {
3687 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3688 }
3689
3690 /**
3691 * Controls whether the screen should remain on, modifying the
3692 * value of {@link #KEEP_SCREEN_ON}.
3693 *
3694 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3695 *
3696 * @see #getKeepScreenOn()
3697 *
3698 * @attr ref android.R.styleable#View_keepScreenOn
3699 */
3700 public void setKeepScreenOn(boolean keepScreenOn) {
3701 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3702 }
3703
3704 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003705 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3706 * @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 -08003707 *
3708 * @attr ref android.R.styleable#View_nextFocusLeft
3709 */
3710 public int getNextFocusLeftId() {
3711 return mNextFocusLeftId;
3712 }
3713
3714 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003715 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3716 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3717 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003718 *
3719 * @attr ref android.R.styleable#View_nextFocusLeft
3720 */
3721 public void setNextFocusLeftId(int nextFocusLeftId) {
3722 mNextFocusLeftId = nextFocusLeftId;
3723 }
3724
3725 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003726 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3727 * @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 -08003728 *
3729 * @attr ref android.R.styleable#View_nextFocusRight
3730 */
3731 public int getNextFocusRightId() {
3732 return mNextFocusRightId;
3733 }
3734
3735 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003736 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3737 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3738 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 *
3740 * @attr ref android.R.styleable#View_nextFocusRight
3741 */
3742 public void setNextFocusRightId(int nextFocusRightId) {
3743 mNextFocusRightId = nextFocusRightId;
3744 }
3745
3746 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003747 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3748 * @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 -08003749 *
3750 * @attr ref android.R.styleable#View_nextFocusUp
3751 */
3752 public int getNextFocusUpId() {
3753 return mNextFocusUpId;
3754 }
3755
3756 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003757 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3758 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3759 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003760 *
3761 * @attr ref android.R.styleable#View_nextFocusUp
3762 */
3763 public void setNextFocusUpId(int nextFocusUpId) {
3764 mNextFocusUpId = nextFocusUpId;
3765 }
3766
3767 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003768 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3769 * @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 -08003770 *
3771 * @attr ref android.R.styleable#View_nextFocusDown
3772 */
3773 public int getNextFocusDownId() {
3774 return mNextFocusDownId;
3775 }
3776
3777 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003778 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3779 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3780 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003781 *
3782 * @attr ref android.R.styleable#View_nextFocusDown
3783 */
3784 public void setNextFocusDownId(int nextFocusDownId) {
3785 mNextFocusDownId = nextFocusDownId;
3786 }
3787
3788 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003789 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3790 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3791 *
3792 * @attr ref android.R.styleable#View_nextFocusForward
3793 */
3794 public int getNextFocusForwardId() {
3795 return mNextFocusForwardId;
3796 }
3797
3798 /**
3799 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3800 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3801 * decide automatically.
3802 *
3803 * @attr ref android.R.styleable#View_nextFocusForward
3804 */
3805 public void setNextFocusForwardId(int nextFocusForwardId) {
3806 mNextFocusForwardId = nextFocusForwardId;
3807 }
3808
3809 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003810 * Returns the visibility of this view and all of its ancestors
3811 *
3812 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3813 */
3814 public boolean isShown() {
3815 View current = this;
3816 //noinspection ConstantConditions
3817 do {
3818 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3819 return false;
3820 }
3821 ViewParent parent = current.mParent;
3822 if (parent == null) {
3823 return false; // We are not attached to the view root
3824 }
3825 if (!(parent instanceof View)) {
3826 return true;
3827 }
3828 current = (View) parent;
3829 } while (current != null);
3830
3831 return false;
3832 }
3833
3834 /**
3835 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3836 * is set
3837 *
3838 * @param insets Insets for system windows
3839 *
3840 * @return True if this view applied the insets, false otherwise
3841 */
3842 protected boolean fitSystemWindows(Rect insets) {
3843 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3844 mPaddingLeft = insets.left;
3845 mPaddingTop = insets.top;
3846 mPaddingRight = insets.right;
3847 mPaddingBottom = insets.bottom;
3848 requestLayout();
3849 return true;
3850 }
3851 return false;
3852 }
3853
3854 /**
3855 * Returns the visibility status for this view.
3856 *
3857 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3858 * @attr ref android.R.styleable#View_visibility
3859 */
3860 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003861 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3862 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3863 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003864 })
3865 public int getVisibility() {
3866 return mViewFlags & VISIBILITY_MASK;
3867 }
3868
3869 /**
3870 * Set the enabled state of this view.
3871 *
3872 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3873 * @attr ref android.R.styleable#View_visibility
3874 */
3875 @RemotableViewMethod
3876 public void setVisibility(int visibility) {
3877 setFlags(visibility, VISIBILITY_MASK);
3878 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3879 }
3880
3881 /**
3882 * Returns the enabled status for this view. The interpretation of the
3883 * enabled state varies by subclass.
3884 *
3885 * @return True if this view is enabled, false otherwise.
3886 */
3887 @ViewDebug.ExportedProperty
3888 public boolean isEnabled() {
3889 return (mViewFlags & ENABLED_MASK) == ENABLED;
3890 }
3891
3892 /**
3893 * Set the enabled state of this view. The interpretation of the enabled
3894 * state varies by subclass.
3895 *
3896 * @param enabled True if this view is enabled, false otherwise.
3897 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003898 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003899 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003900 if (enabled == isEnabled()) return;
3901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003902 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3903
3904 /*
3905 * The View most likely has to change its appearance, so refresh
3906 * the drawable state.
3907 */
3908 refreshDrawableState();
3909
3910 // Invalidate too, since the default behavior for views is to be
3911 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003912 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913 }
3914
3915 /**
3916 * Set whether this view can receive the focus.
3917 *
3918 * Setting this to false will also ensure that this view is not focusable
3919 * in touch mode.
3920 *
3921 * @param focusable If true, this view can receive the focus.
3922 *
3923 * @see #setFocusableInTouchMode(boolean)
3924 * @attr ref android.R.styleable#View_focusable
3925 */
3926 public void setFocusable(boolean focusable) {
3927 if (!focusable) {
3928 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3929 }
3930 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3931 }
3932
3933 /**
3934 * Set whether this view can receive focus while in touch mode.
3935 *
3936 * Setting this to true will also ensure that this view is focusable.
3937 *
3938 * @param focusableInTouchMode If true, this view can receive the focus while
3939 * in touch mode.
3940 *
3941 * @see #setFocusable(boolean)
3942 * @attr ref android.R.styleable#View_focusableInTouchMode
3943 */
3944 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3945 // Focusable in touch mode should always be set before the focusable flag
3946 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3947 // which, in touch mode, will not successfully request focus on this view
3948 // because the focusable in touch mode flag is not set
3949 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3950 if (focusableInTouchMode) {
3951 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3952 }
3953 }
3954
3955 /**
3956 * Set whether this view should have sound effects enabled for events such as
3957 * clicking and touching.
3958 *
3959 * <p>You may wish to disable sound effects for a view if you already play sounds,
3960 * for instance, a dial key that plays dtmf tones.
3961 *
3962 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3963 * @see #isSoundEffectsEnabled()
3964 * @see #playSoundEffect(int)
3965 * @attr ref android.R.styleable#View_soundEffectsEnabled
3966 */
3967 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3968 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3969 }
3970
3971 /**
3972 * @return whether this view should have sound effects enabled for events such as
3973 * clicking and touching.
3974 *
3975 * @see #setSoundEffectsEnabled(boolean)
3976 * @see #playSoundEffect(int)
3977 * @attr ref android.R.styleable#View_soundEffectsEnabled
3978 */
3979 @ViewDebug.ExportedProperty
3980 public boolean isSoundEffectsEnabled() {
3981 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3982 }
3983
3984 /**
3985 * Set whether this view should have haptic feedback for events such as
3986 * long presses.
3987 *
3988 * <p>You may wish to disable haptic feedback if your view already controls
3989 * its own haptic feedback.
3990 *
3991 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3992 * @see #isHapticFeedbackEnabled()
3993 * @see #performHapticFeedback(int)
3994 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3995 */
3996 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3997 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3998 }
3999
4000 /**
4001 * @return whether this view should have haptic feedback enabled for events
4002 * long presses.
4003 *
4004 * @see #setHapticFeedbackEnabled(boolean)
4005 * @see #performHapticFeedback(int)
4006 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4007 */
4008 @ViewDebug.ExportedProperty
4009 public boolean isHapticFeedbackEnabled() {
4010 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4011 }
4012
4013 /**
4014 * If this view doesn't do any drawing on its own, set this flag to
4015 * allow further optimizations. By default, this flag is not set on
4016 * View, but could be set on some View subclasses such as ViewGroup.
4017 *
4018 * Typically, if you override {@link #onDraw} you should clear this flag.
4019 *
4020 * @param willNotDraw whether or not this View draw on its own
4021 */
4022 public void setWillNotDraw(boolean willNotDraw) {
4023 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4024 }
4025
4026 /**
4027 * Returns whether or not this View draws on its own.
4028 *
4029 * @return true if this view has nothing to draw, false otherwise
4030 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004031 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004032 public boolean willNotDraw() {
4033 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4034 }
4035
4036 /**
4037 * When a View's drawing cache is enabled, drawing is redirected to an
4038 * offscreen bitmap. Some views, like an ImageView, must be able to
4039 * bypass this mechanism if they already draw a single bitmap, to avoid
4040 * unnecessary usage of the memory.
4041 *
4042 * @param willNotCacheDrawing true if this view does not cache its
4043 * drawing, false otherwise
4044 */
4045 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4046 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4047 }
4048
4049 /**
4050 * Returns whether or not this View can cache its drawing or not.
4051 *
4052 * @return true if this view does not cache its drawing, false otherwise
4053 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004054 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004055 public boolean willNotCacheDrawing() {
4056 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4057 }
4058
4059 /**
4060 * Indicates whether this view reacts to click events or not.
4061 *
4062 * @return true if the view is clickable, false otherwise
4063 *
4064 * @see #setClickable(boolean)
4065 * @attr ref android.R.styleable#View_clickable
4066 */
4067 @ViewDebug.ExportedProperty
4068 public boolean isClickable() {
4069 return (mViewFlags & CLICKABLE) == CLICKABLE;
4070 }
4071
4072 /**
4073 * Enables or disables click events for this view. When a view
4074 * is clickable it will change its state to "pressed" on every click.
4075 * Subclasses should set the view clickable to visually react to
4076 * user's clicks.
4077 *
4078 * @param clickable true to make the view clickable, false otherwise
4079 *
4080 * @see #isClickable()
4081 * @attr ref android.R.styleable#View_clickable
4082 */
4083 public void setClickable(boolean clickable) {
4084 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4085 }
4086
4087 /**
4088 * Indicates whether this view reacts to long click events or not.
4089 *
4090 * @return true if the view is long clickable, false otherwise
4091 *
4092 * @see #setLongClickable(boolean)
4093 * @attr ref android.R.styleable#View_longClickable
4094 */
4095 public boolean isLongClickable() {
4096 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4097 }
4098
4099 /**
4100 * Enables or disables long click events for this view. When a view is long
4101 * clickable it reacts to the user holding down the button for a longer
4102 * duration than a tap. This event can either launch the listener or a
4103 * context menu.
4104 *
4105 * @param longClickable true to make the view long clickable, false otherwise
4106 * @see #isLongClickable()
4107 * @attr ref android.R.styleable#View_longClickable
4108 */
4109 public void setLongClickable(boolean longClickable) {
4110 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4111 }
4112
4113 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004114 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004115 *
4116 * @see #isClickable()
4117 * @see #setClickable(boolean)
4118 *
4119 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4120 * the View's internal state from a previously set "pressed" state.
4121 */
4122 public void setPressed(boolean pressed) {
4123 if (pressed) {
4124 mPrivateFlags |= PRESSED;
4125 } else {
4126 mPrivateFlags &= ~PRESSED;
4127 }
4128 refreshDrawableState();
4129 dispatchSetPressed(pressed);
4130 }
4131
4132 /**
4133 * Dispatch setPressed to all of this View's children.
4134 *
4135 * @see #setPressed(boolean)
4136 *
4137 * @param pressed The new pressed state
4138 */
4139 protected void dispatchSetPressed(boolean pressed) {
4140 }
4141
4142 /**
4143 * Indicates whether the view is currently in pressed state. Unless
4144 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4145 * the pressed state.
4146 *
4147 * @see #setPressed
4148 * @see #isClickable()
4149 * @see #setClickable(boolean)
4150 *
4151 * @return true if the view is currently pressed, false otherwise
4152 */
4153 public boolean isPressed() {
4154 return (mPrivateFlags & PRESSED) == PRESSED;
4155 }
4156
4157 /**
4158 * Indicates whether this view will save its state (that is,
4159 * whether its {@link #onSaveInstanceState} method will be called).
4160 *
4161 * @return Returns true if the view state saving is enabled, else false.
4162 *
4163 * @see #setSaveEnabled(boolean)
4164 * @attr ref android.R.styleable#View_saveEnabled
4165 */
4166 public boolean isSaveEnabled() {
4167 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4168 }
4169
4170 /**
4171 * Controls whether the saving of this view's state is
4172 * enabled (that is, whether its {@link #onSaveInstanceState} method
4173 * will be called). Note that even if freezing is enabled, the
4174 * view still must have an id assigned to it (via {@link #setId setId()})
4175 * for its state to be saved. This flag can only disable the
4176 * saving of this view; any child views may still have their state saved.
4177 *
4178 * @param enabled Set to false to <em>disable</em> state saving, or true
4179 * (the default) to allow it.
4180 *
4181 * @see #isSaveEnabled()
4182 * @see #setId(int)
4183 * @see #onSaveInstanceState()
4184 * @attr ref android.R.styleable#View_saveEnabled
4185 */
4186 public void setSaveEnabled(boolean enabled) {
4187 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4188 }
4189
Jeff Brown85a31762010-09-01 17:01:00 -07004190 /**
4191 * Gets whether the framework should discard touches when the view's
4192 * window is obscured by another visible window.
4193 * Refer to the {@link View} security documentation for more details.
4194 *
4195 * @return True if touch filtering is enabled.
4196 *
4197 * @see #setFilterTouchesWhenObscured(boolean)
4198 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4199 */
4200 @ViewDebug.ExportedProperty
4201 public boolean getFilterTouchesWhenObscured() {
4202 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4203 }
4204
4205 /**
4206 * Sets whether the framework should discard touches when the view's
4207 * window is obscured by another visible window.
4208 * Refer to the {@link View} security documentation for more details.
4209 *
4210 * @param enabled True if touch filtering should be enabled.
4211 *
4212 * @see #getFilterTouchesWhenObscured
4213 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4214 */
4215 public void setFilterTouchesWhenObscured(boolean enabled) {
4216 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4217 FILTER_TOUCHES_WHEN_OBSCURED);
4218 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004219
4220 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004221 * Indicates whether the entire hierarchy under this view will save its
4222 * state when a state saving traversal occurs from its parent. The default
4223 * is true; if false, these views will not be saved unless
4224 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4225 *
4226 * @return Returns true if the view state saving from parent is enabled, else false.
4227 *
4228 * @see #setSaveFromParentEnabled(boolean)
4229 */
4230 public boolean isSaveFromParentEnabled() {
4231 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4232 }
4233
4234 /**
4235 * Controls whether the entire hierarchy under this view will save its
4236 * state when a state saving traversal occurs from its parent. The default
4237 * is true; if false, these views will not be saved unless
4238 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4239 *
4240 * @param enabled Set to false to <em>disable</em> state saving, or true
4241 * (the default) to allow it.
4242 *
4243 * @see #isSaveFromParentEnabled()
4244 * @see #setId(int)
4245 * @see #onSaveInstanceState()
4246 */
4247 public void setSaveFromParentEnabled(boolean enabled) {
4248 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4249 }
4250
4251
4252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004253 * Returns whether this View is able to take focus.
4254 *
4255 * @return True if this view can take focus, or false otherwise.
4256 * @attr ref android.R.styleable#View_focusable
4257 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004258 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004259 public final boolean isFocusable() {
4260 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4261 }
4262
4263 /**
4264 * When a view is focusable, it may not want to take focus when in touch mode.
4265 * For example, a button would like focus when the user is navigating via a D-pad
4266 * so that the user can click on it, but once the user starts touching the screen,
4267 * the button shouldn't take focus
4268 * @return Whether the view is focusable in touch mode.
4269 * @attr ref android.R.styleable#View_focusableInTouchMode
4270 */
4271 @ViewDebug.ExportedProperty
4272 public final boolean isFocusableInTouchMode() {
4273 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4274 }
4275
4276 /**
4277 * Find the nearest view in the specified direction that can take focus.
4278 * This does not actually give focus to that view.
4279 *
4280 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4281 *
4282 * @return The nearest focusable in the specified direction, or null if none
4283 * can be found.
4284 */
4285 public View focusSearch(int direction) {
4286 if (mParent != null) {
4287 return mParent.focusSearch(this, direction);
4288 } else {
4289 return null;
4290 }
4291 }
4292
4293 /**
4294 * This method is the last chance for the focused view and its ancestors to
4295 * respond to an arrow key. This is called when the focused view did not
4296 * consume the key internally, nor could the view system find a new view in
4297 * the requested direction to give focus to.
4298 *
4299 * @param focused The currently focused view.
4300 * @param direction The direction focus wants to move. One of FOCUS_UP,
4301 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4302 * @return True if the this view consumed this unhandled move.
4303 */
4304 public boolean dispatchUnhandledMove(View focused, int direction) {
4305 return false;
4306 }
4307
4308 /**
4309 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004310 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004311 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004312 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4313 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004314 * @return The user specified next view, or null if there is none.
4315 */
4316 View findUserSetNextFocus(View root, int direction) {
4317 switch (direction) {
4318 case FOCUS_LEFT:
4319 if (mNextFocusLeftId == View.NO_ID) return null;
4320 return findViewShouldExist(root, mNextFocusLeftId);
4321 case FOCUS_RIGHT:
4322 if (mNextFocusRightId == View.NO_ID) return null;
4323 return findViewShouldExist(root, mNextFocusRightId);
4324 case FOCUS_UP:
4325 if (mNextFocusUpId == View.NO_ID) return null;
4326 return findViewShouldExist(root, mNextFocusUpId);
4327 case FOCUS_DOWN:
4328 if (mNextFocusDownId == View.NO_ID) return null;
4329 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004330 case FOCUS_FORWARD:
4331 if (mNextFocusForwardId == View.NO_ID) return null;
4332 return findViewShouldExist(root, mNextFocusForwardId);
4333 case FOCUS_BACKWARD: {
4334 final int id = mID;
4335 return root.findViewByPredicate(new Predicate<View>() {
4336 @Override
4337 public boolean apply(View t) {
4338 return t.mNextFocusForwardId == id;
4339 }
4340 });
4341 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004342 }
4343 return null;
4344 }
4345
4346 private static View findViewShouldExist(View root, int childViewId) {
4347 View result = root.findViewById(childViewId);
4348 if (result == null) {
4349 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4350 + "by user for id " + childViewId);
4351 }
4352 return result;
4353 }
4354
4355 /**
4356 * Find and return all focusable views that are descendants of this view,
4357 * possibly including this view if it is focusable itself.
4358 *
4359 * @param direction The direction of the focus
4360 * @return A list of focusable views
4361 */
4362 public ArrayList<View> getFocusables(int direction) {
4363 ArrayList<View> result = new ArrayList<View>(24);
4364 addFocusables(result, direction);
4365 return result;
4366 }
4367
4368 /**
4369 * Add any focusable views that are descendants of this view (possibly
4370 * including this view if it is focusable itself) to views. If we are in touch mode,
4371 * only add views that are also focusable in touch mode.
4372 *
4373 * @param views Focusable views found so far
4374 * @param direction The direction of the focus
4375 */
4376 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004377 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004379
svetoslavganov75986cf2009-05-14 22:28:01 -07004380 /**
4381 * Adds any focusable views that are descendants of this view (possibly
4382 * including this view if it is focusable itself) to views. This method
4383 * adds all focusable views regardless if we are in touch mode or
4384 * only views focusable in touch mode if we are in touch mode depending on
4385 * the focusable mode paramater.
4386 *
4387 * @param views Focusable views found so far or null if all we are interested is
4388 * the number of focusables.
4389 * @param direction The direction of the focus.
4390 * @param focusableMode The type of focusables to be added.
4391 *
4392 * @see #FOCUSABLES_ALL
4393 * @see #FOCUSABLES_TOUCH_MODE
4394 */
4395 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4396 if (!isFocusable()) {
4397 return;
4398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004399
svetoslavganov75986cf2009-05-14 22:28:01 -07004400 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4401 isInTouchMode() && !isFocusableInTouchMode()) {
4402 return;
4403 }
4404
4405 if (views != null) {
4406 views.add(this);
4407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 }
4409
4410 /**
4411 * Find and return all touchable views that are descendants of this view,
4412 * possibly including this view if it is touchable itself.
4413 *
4414 * @return A list of touchable views
4415 */
4416 public ArrayList<View> getTouchables() {
4417 ArrayList<View> result = new ArrayList<View>();
4418 addTouchables(result);
4419 return result;
4420 }
4421
4422 /**
4423 * Add any touchable views that are descendants of this view (possibly
4424 * including this view if it is touchable itself) to views.
4425 *
4426 * @param views Touchable views found so far
4427 */
4428 public void addTouchables(ArrayList<View> views) {
4429 final int viewFlags = mViewFlags;
4430
4431 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4432 && (viewFlags & ENABLED_MASK) == ENABLED) {
4433 views.add(this);
4434 }
4435 }
4436
4437 /**
4438 * Call this to try to give focus to a specific view or to one of its
4439 * descendants.
4440 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004441 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4442 * false), or if it is focusable and it is not focusable in touch mode
4443 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004444 *
4445 * See also {@link #focusSearch}, which is what you call to say that you
4446 * have focus, and you want your parent to look for the next one.
4447 *
4448 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4449 * {@link #FOCUS_DOWN} and <code>null</code>.
4450 *
4451 * @return Whether this view or one of its descendants actually took focus.
4452 */
4453 public final boolean requestFocus() {
4454 return requestFocus(View.FOCUS_DOWN);
4455 }
4456
4457
4458 /**
4459 * Call this to try to give focus to a specific view or to one of its
4460 * descendants and give it a hint about what direction focus is heading.
4461 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004462 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4463 * false), or if it is focusable and it is not focusable in touch mode
4464 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004465 *
4466 * See also {@link #focusSearch}, which is what you call to say that you
4467 * have focus, and you want your parent to look for the next one.
4468 *
4469 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4470 * <code>null</code> set for the previously focused rectangle.
4471 *
4472 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4473 * @return Whether this view or one of its descendants actually took focus.
4474 */
4475 public final boolean requestFocus(int direction) {
4476 return requestFocus(direction, null);
4477 }
4478
4479 /**
4480 * Call this to try to give focus to a specific view or to one of its descendants
4481 * and give it hints about the direction and a specific rectangle that the focus
4482 * is coming from. The rectangle can help give larger views a finer grained hint
4483 * about where focus is coming from, and therefore, where to show selection, or
4484 * forward focus change internally.
4485 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004486 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4487 * false), or if it is focusable and it is not focusable in touch mode
4488 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004489 *
4490 * A View will not take focus if it is not visible.
4491 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004492 * A View will not take focus if one of its parents has
4493 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4494 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004495 *
4496 * See also {@link #focusSearch}, which is what you call to say that you
4497 * have focus, and you want your parent to look for the next one.
4498 *
4499 * You may wish to override this method if your custom {@link View} has an internal
4500 * {@link View} that it wishes to forward the request to.
4501 *
4502 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4503 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4504 * to give a finer grained hint about where focus is coming from. May be null
4505 * if there is no hint.
4506 * @return Whether this view or one of its descendants actually took focus.
4507 */
4508 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4509 // need to be focusable
4510 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4511 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4512 return false;
4513 }
4514
4515 // need to be focusable in touch mode if in touch mode
4516 if (isInTouchMode() &&
4517 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4518 return false;
4519 }
4520
4521 // need to not have any parents blocking us
4522 if (hasAncestorThatBlocksDescendantFocus()) {
4523 return false;
4524 }
4525
4526 handleFocusGainInternal(direction, previouslyFocusedRect);
4527 return true;
4528 }
4529
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004530 /** Gets the ViewAncestor, or null if not attached. */
4531 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004532 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004533 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004534 }
4535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004536 /**
4537 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4538 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4539 * touch mode to request focus when they are touched.
4540 *
4541 * @return Whether this view or one of its descendants actually took focus.
4542 *
4543 * @see #isInTouchMode()
4544 *
4545 */
4546 public final boolean requestFocusFromTouch() {
4547 // Leave touch mode if we need to
4548 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004549 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004550 if (viewRoot != null) {
4551 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004552 }
4553 }
4554 return requestFocus(View.FOCUS_DOWN);
4555 }
4556
4557 /**
4558 * @return Whether any ancestor of this view blocks descendant focus.
4559 */
4560 private boolean hasAncestorThatBlocksDescendantFocus() {
4561 ViewParent ancestor = mParent;
4562 while (ancestor instanceof ViewGroup) {
4563 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4564 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4565 return true;
4566 } else {
4567 ancestor = vgAncestor.getParent();
4568 }
4569 }
4570 return false;
4571 }
4572
4573 /**
Romain Guya440b002010-02-24 15:57:54 -08004574 * @hide
4575 */
4576 public void dispatchStartTemporaryDetach() {
4577 onStartTemporaryDetach();
4578 }
4579
4580 /**
4581 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4583 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004584 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004585 */
4586 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004587 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004588 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004589 }
4590
4591 /**
4592 * @hide
4593 */
4594 public void dispatchFinishTemporaryDetach() {
4595 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004596 }
Romain Guy8506ab42009-06-11 17:35:47 -07004597
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004598 /**
4599 * Called after {@link #onStartTemporaryDetach} when the container is done
4600 * changing the view.
4601 */
4602 public void onFinishTemporaryDetach() {
4603 }
Romain Guy8506ab42009-06-11 17:35:47 -07004604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004605 /**
4606 * capture information of this view for later analysis: developement only
4607 * check dynamic switch to make sure we only dump view
4608 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4609 */
4610 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004611 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 return;
4613 }
4614 ViewDebug.dumpCapturedView(subTag, v);
4615 }
4616
4617 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004618 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4619 * for this view's window. Returns null if the view is not currently attached
4620 * to the window. Normally you will not need to use this directly, but
4621 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4622 */
4623 public KeyEvent.DispatcherState getKeyDispatcherState() {
4624 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4625 }
Joe Malin32736f02011-01-19 16:14:20 -08004626
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004628 * Dispatch a key event before it is processed by any input method
4629 * associated with the view hierarchy. This can be used to intercept
4630 * key events in special situations before the IME consumes them; a
4631 * typical example would be handling the BACK key to update the application's
4632 * UI instead of allowing the IME to see it and close itself.
4633 *
4634 * @param event The key event to be dispatched.
4635 * @return True if the event was handled, false otherwise.
4636 */
4637 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4638 return onKeyPreIme(event.getKeyCode(), event);
4639 }
4640
4641 /**
4642 * Dispatch a key event to the next view on the focus path. This path runs
4643 * from the top of the view tree down to the currently focused view. If this
4644 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4645 * the next node down the focus path. This method also fires any key
4646 * listeners.
4647 *
4648 * @param event The key event to be dispatched.
4649 * @return True if the event was handled, false otherwise.
4650 */
4651 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004652 if (mInputEventConsistencyVerifier != null) {
4653 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4654 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004655
Romain Guyf607bdc2010-09-10 19:20:06 -07004656 //noinspection SimplifiableIfStatement,deprecation
Joe Onorato43a17652011-04-06 19:22:23 -07004657 if (false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004658 captureViewInfo("captureViewKeyEvent", this);
4659 }
4660
Jeff Brown21bc5c92011-02-28 18:27:14 -08004661 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004662 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004663 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4664 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4665 return true;
4666 }
4667
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004668 if (event.dispatch(this, mAttachInfo != null
4669 ? mAttachInfo.mKeyDispatchState : null, this)) {
4670 return true;
4671 }
4672
4673 if (mInputEventConsistencyVerifier != null) {
4674 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4675 }
4676 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 }
4678
4679 /**
4680 * Dispatches a key shortcut event.
4681 *
4682 * @param event The key event to be dispatched.
4683 * @return True if the event was handled by the view, false otherwise.
4684 */
4685 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4686 return onKeyShortcut(event.getKeyCode(), event);
4687 }
4688
4689 /**
4690 * Pass the touch screen motion event down to the target view, or this
4691 * view if it is the target.
4692 *
4693 * @param event The motion event to be dispatched.
4694 * @return True if the event was handled by the view, false otherwise.
4695 */
4696 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004697 if (mInputEventConsistencyVerifier != null) {
4698 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4699 }
4700
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004701 if (onFilterTouchEventForSecurity(event)) {
4702 //noinspection SimplifiableIfStatement
4703 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4704 mOnTouchListener.onTouch(this, event)) {
4705 return true;
4706 }
4707
4708 if (onTouchEvent(event)) {
4709 return true;
4710 }
Jeff Brown85a31762010-09-01 17:01:00 -07004711 }
4712
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004713 if (mInputEventConsistencyVerifier != null) {
4714 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004715 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004716 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 }
4718
4719 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004720 * Filter the touch event to apply security policies.
4721 *
4722 * @param event The motion event to be filtered.
4723 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004724 *
Jeff Brown85a31762010-09-01 17:01:00 -07004725 * @see #getFilterTouchesWhenObscured
4726 */
4727 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004728 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004729 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4730 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4731 // Window is obscured, drop this touch.
4732 return false;
4733 }
4734 return true;
4735 }
4736
4737 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004738 * Pass a trackball motion event down to the focused view.
4739 *
4740 * @param event The motion event to be dispatched.
4741 * @return True if the event was handled by the view, false otherwise.
4742 */
4743 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004744 if (mInputEventConsistencyVerifier != null) {
4745 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4746 }
4747
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004748 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004749 if (onTrackballEvent(event)) {
4750 return true;
4751 }
4752
4753 if (mInputEventConsistencyVerifier != null) {
4754 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4755 }
4756 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004757 }
4758
4759 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004760 * Dispatch a generic motion event.
4761 * <p>
4762 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4763 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004764 * delivered to the focused view. Hover events are handled specially and are delivered
4765 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004766 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004767 *
4768 * @param event The motion event to be dispatched.
4769 * @return True if the event was handled by the view, false otherwise.
4770 */
4771 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004772 if (mInputEventConsistencyVerifier != null) {
4773 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4774 }
4775
Jeff Browna032cc02011-03-07 16:56:21 -08004776 final int source = event.getSource();
4777 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4778 final int action = event.getAction();
4779 if (action == MotionEvent.ACTION_HOVER_ENTER
4780 || action == MotionEvent.ACTION_HOVER_MOVE
4781 || action == MotionEvent.ACTION_HOVER_EXIT) {
4782 if (dispatchHoverEvent(event)) {
4783 return true;
4784 }
4785 } else if (dispatchGenericPointerEvent(event)) {
4786 return true;
4787 }
4788 } else if (dispatchGenericFocusedEvent(event)) {
4789 return true;
4790 }
4791
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004792 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004793 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4794 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4795 return true;
4796 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004797
4798 if (onGenericMotionEvent(event)) {
4799 return true;
4800 }
4801
4802 if (mInputEventConsistencyVerifier != null) {
4803 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4804 }
4805 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004806 }
4807
4808 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004809 * Dispatch a hover event.
4810 * <p>
4811 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4812 * </p>
4813 *
4814 * @param event The motion event to be dispatched.
4815 * @return True if the event was handled by the view, false otherwise.
4816 * @hide
4817 */
4818 protected boolean dispatchHoverEvent(MotionEvent event) {
4819 return onHoverEvent(event);
4820 }
4821
4822 /**
4823 * Dispatch a generic motion event to the view under the first pointer.
4824 * <p>
4825 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4826 * </p>
4827 *
4828 * @param event The motion event to be dispatched.
4829 * @return True if the event was handled by the view, false otherwise.
4830 * @hide
4831 */
4832 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4833 return false;
4834 }
4835
4836 /**
4837 * Dispatch a generic motion event to the currently focused view.
4838 * <p>
4839 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4840 * </p>
4841 *
4842 * @param event The motion event to be dispatched.
4843 * @return True if the event was handled by the view, false otherwise.
4844 * @hide
4845 */
4846 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4847 return false;
4848 }
4849
4850 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004851 * Dispatch a pointer event.
4852 * <p>
4853 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4854 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4855 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4856 * and should not be expected to handle other pointing device features.
4857 * </p>
4858 *
4859 * @param event The motion event to be dispatched.
4860 * @return True if the event was handled by the view, false otherwise.
4861 * @hide
4862 */
4863 public final boolean dispatchPointerEvent(MotionEvent event) {
4864 if (event.isTouchEvent()) {
4865 return dispatchTouchEvent(event);
4866 } else {
4867 return dispatchGenericMotionEvent(event);
4868 }
4869 }
4870
4871 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004872 * Called when the window containing this view gains or loses window focus.
4873 * ViewGroups should override to route to their children.
4874 *
4875 * @param hasFocus True if the window containing this view now has focus,
4876 * false otherwise.
4877 */
4878 public void dispatchWindowFocusChanged(boolean hasFocus) {
4879 onWindowFocusChanged(hasFocus);
4880 }
4881
4882 /**
4883 * Called when the window containing this view gains or loses focus. Note
4884 * that this is separate from view focus: to receive key events, both
4885 * your view and its window must have focus. If a window is displayed
4886 * on top of yours that takes input focus, then your own window will lose
4887 * focus but the view focus will remain unchanged.
4888 *
4889 * @param hasWindowFocus True if the window containing this view now has
4890 * focus, false otherwise.
4891 */
4892 public void onWindowFocusChanged(boolean hasWindowFocus) {
4893 InputMethodManager imm = InputMethodManager.peekInstance();
4894 if (!hasWindowFocus) {
4895 if (isPressed()) {
4896 setPressed(false);
4897 }
4898 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4899 imm.focusOut(this);
4900 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004901 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004902 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004903 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004904 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4905 imm.focusIn(this);
4906 }
4907 refreshDrawableState();
4908 }
4909
4910 /**
4911 * Returns true if this view is in a window that currently has window focus.
4912 * Note that this is not the same as the view itself having focus.
4913 *
4914 * @return True if this view is in a window that currently has window focus.
4915 */
4916 public boolean hasWindowFocus() {
4917 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4918 }
4919
4920 /**
Adam Powell326d8082009-12-09 15:10:07 -08004921 * Dispatch a view visibility change down the view hierarchy.
4922 * ViewGroups should override to route to their children.
4923 * @param changedView The view whose visibility changed. Could be 'this' or
4924 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004925 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4926 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004927 */
4928 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4929 onVisibilityChanged(changedView, visibility);
4930 }
4931
4932 /**
4933 * Called when the visibility of the view or an ancestor of the view is changed.
4934 * @param changedView The view whose visibility changed. Could be 'this' or
4935 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004936 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4937 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004938 */
4939 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004940 if (visibility == VISIBLE) {
4941 if (mAttachInfo != null) {
4942 initialAwakenScrollBars();
4943 } else {
4944 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4945 }
4946 }
Adam Powell326d8082009-12-09 15:10:07 -08004947 }
4948
4949 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004950 * Dispatch a hint about whether this view is displayed. For instance, when
4951 * a View moves out of the screen, it might receives a display hint indicating
4952 * the view is not displayed. Applications should not <em>rely</em> on this hint
4953 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004954 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004955 * @param hint A hint about whether or not this view is displayed:
4956 * {@link #VISIBLE} or {@link #INVISIBLE}.
4957 */
4958 public void dispatchDisplayHint(int hint) {
4959 onDisplayHint(hint);
4960 }
4961
4962 /**
4963 * Gives this view a hint about whether is displayed or not. For instance, when
4964 * a View moves out of the screen, it might receives a display hint indicating
4965 * the view is not displayed. Applications should not <em>rely</em> on this hint
4966 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004967 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004968 * @param hint A hint about whether or not this view is displayed:
4969 * {@link #VISIBLE} or {@link #INVISIBLE}.
4970 */
4971 protected void onDisplayHint(int hint) {
4972 }
4973
4974 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004975 * Dispatch a window visibility change down the view hierarchy.
4976 * ViewGroups should override to route to their children.
4977 *
4978 * @param visibility The new visibility of the window.
4979 *
4980 * @see #onWindowVisibilityChanged
4981 */
4982 public void dispatchWindowVisibilityChanged(int visibility) {
4983 onWindowVisibilityChanged(visibility);
4984 }
4985
4986 /**
4987 * Called when the window containing has change its visibility
4988 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4989 * that this tells you whether or not your window is being made visible
4990 * to the window manager; this does <em>not</em> tell you whether or not
4991 * your window is obscured by other windows on the screen, even if it
4992 * is itself visible.
4993 *
4994 * @param visibility The new visibility of the window.
4995 */
4996 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004997 if (visibility == VISIBLE) {
4998 initialAwakenScrollBars();
4999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005000 }
5001
5002 /**
5003 * Returns the current visibility of the window this view is attached to
5004 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5005 *
5006 * @return Returns the current visibility of the view's window.
5007 */
5008 public int getWindowVisibility() {
5009 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5010 }
5011
5012 /**
5013 * Retrieve the overall visible display size in which the window this view is
5014 * attached to has been positioned in. This takes into account screen
5015 * decorations above the window, for both cases where the window itself
5016 * is being position inside of them or the window is being placed under
5017 * then and covered insets are used for the window to position its content
5018 * inside. In effect, this tells you the available area where content can
5019 * be placed and remain visible to users.
5020 *
5021 * <p>This function requires an IPC back to the window manager to retrieve
5022 * the requested information, so should not be used in performance critical
5023 * code like drawing.
5024 *
5025 * @param outRect Filled in with the visible display frame. If the view
5026 * is not attached to a window, this is simply the raw display size.
5027 */
5028 public void getWindowVisibleDisplayFrame(Rect outRect) {
5029 if (mAttachInfo != null) {
5030 try {
5031 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5032 } catch (RemoteException e) {
5033 return;
5034 }
5035 // XXX This is really broken, and probably all needs to be done
5036 // in the window manager, and we need to know more about whether
5037 // we want the area behind or in front of the IME.
5038 final Rect insets = mAttachInfo.mVisibleInsets;
5039 outRect.left += insets.left;
5040 outRect.top += insets.top;
5041 outRect.right -= insets.right;
5042 outRect.bottom -= insets.bottom;
5043 return;
5044 }
5045 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005046 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005047 }
5048
5049 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005050 * Dispatch a notification about a resource configuration change down
5051 * the view hierarchy.
5052 * ViewGroups should override to route to their children.
5053 *
5054 * @param newConfig The new resource configuration.
5055 *
5056 * @see #onConfigurationChanged
5057 */
5058 public void dispatchConfigurationChanged(Configuration newConfig) {
5059 onConfigurationChanged(newConfig);
5060 }
5061
5062 /**
5063 * Called when the current configuration of the resources being used
5064 * by the application have changed. You can use this to decide when
5065 * to reload resources that can changed based on orientation and other
5066 * configuration characterstics. You only need to use this if you are
5067 * not relying on the normal {@link android.app.Activity} mechanism of
5068 * recreating the activity instance upon a configuration change.
5069 *
5070 * @param newConfig The new resource configuration.
5071 */
5072 protected void onConfigurationChanged(Configuration newConfig) {
5073 }
5074
5075 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005076 * Private function to aggregate all per-view attributes in to the view
5077 * root.
5078 */
5079 void dispatchCollectViewAttributes(int visibility) {
5080 performCollectViewAttributes(visibility);
5081 }
5082
5083 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005084 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005085 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5086 mAttachInfo.mKeepScreenOn = true;
5087 }
5088 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5089 if (mOnSystemUiVisibilityChangeListener != null) {
5090 mAttachInfo.mHasSystemUiListeners = true;
5091 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 }
5093 }
5094
5095 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005096 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005098 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5099 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 ai.mRecomputeGlobalAttributes = true;
5101 }
5102 }
5103 }
5104
5105 /**
5106 * Returns whether the device is currently in touch mode. Touch mode is entered
5107 * once the user begins interacting with the device by touch, and affects various
5108 * things like whether focus is always visible to the user.
5109 *
5110 * @return Whether the device is in touch mode.
5111 */
5112 @ViewDebug.ExportedProperty
5113 public boolean isInTouchMode() {
5114 if (mAttachInfo != null) {
5115 return mAttachInfo.mInTouchMode;
5116 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005117 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005118 }
5119 }
5120
5121 /**
5122 * Returns the context the view is running in, through which it can
5123 * access the current theme, resources, etc.
5124 *
5125 * @return The view's Context.
5126 */
5127 @ViewDebug.CapturedViewProperty
5128 public final Context getContext() {
5129 return mContext;
5130 }
5131
5132 /**
5133 * Handle a key event before it is processed by any input method
5134 * associated with the view hierarchy. This can be used to intercept
5135 * key events in special situations before the IME consumes them; a
5136 * typical example would be handling the BACK key to update the application's
5137 * UI instead of allowing the IME to see it and close itself.
5138 *
5139 * @param keyCode The value in event.getKeyCode().
5140 * @param event Description of the key event.
5141 * @return If you handled the event, return true. If you want to allow the
5142 * event to be handled by the next receiver, return false.
5143 */
5144 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5145 return false;
5146 }
5147
5148 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005149 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5150 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005151 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5152 * is released, if the view is enabled and clickable.
5153 *
5154 * @param keyCode A key code that represents the button pressed, from
5155 * {@link android.view.KeyEvent}.
5156 * @param event The KeyEvent object that defines the button action.
5157 */
5158 public boolean onKeyDown(int keyCode, KeyEvent event) {
5159 boolean result = false;
5160
5161 switch (keyCode) {
5162 case KeyEvent.KEYCODE_DPAD_CENTER:
5163 case KeyEvent.KEYCODE_ENTER: {
5164 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5165 return true;
5166 }
5167 // Long clickable items don't necessarily have to be clickable
5168 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5169 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5170 (event.getRepeatCount() == 0)) {
5171 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005172 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 return true;
5174 }
5175 break;
5176 }
5177 }
5178 return result;
5179 }
5180
5181 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005182 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5183 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5184 * the event).
5185 */
5186 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5187 return false;
5188 }
5189
5190 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005191 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5192 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5194 * {@link KeyEvent#KEYCODE_ENTER} is released.
5195 *
5196 * @param keyCode A key code that represents the button pressed, from
5197 * {@link android.view.KeyEvent}.
5198 * @param event The KeyEvent object that defines the button action.
5199 */
5200 public boolean onKeyUp(int keyCode, KeyEvent event) {
5201 boolean result = false;
5202
5203 switch (keyCode) {
5204 case KeyEvent.KEYCODE_DPAD_CENTER:
5205 case KeyEvent.KEYCODE_ENTER: {
5206 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5207 return true;
5208 }
5209 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5210 setPressed(false);
5211
5212 if (!mHasPerformedLongPress) {
5213 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005214 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005215
5216 result = performClick();
5217 }
5218 }
5219 break;
5220 }
5221 }
5222 return result;
5223 }
5224
5225 /**
5226 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5227 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5228 * the event).
5229 *
5230 * @param keyCode A key code that represents the button pressed, from
5231 * {@link android.view.KeyEvent}.
5232 * @param repeatCount The number of times the action was made.
5233 * @param event The KeyEvent object that defines the button action.
5234 */
5235 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5236 return false;
5237 }
5238
5239 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005240 * Called on the focused view when a key shortcut event is not handled.
5241 * Override this method to implement local key shortcuts for the View.
5242 * Key shortcuts can also be implemented by setting the
5243 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 *
5245 * @param keyCode The value in event.getKeyCode().
5246 * @param event Description of the key event.
5247 * @return If you handled the event, return true. If you want to allow the
5248 * event to be handled by the next receiver, return false.
5249 */
5250 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5251 return false;
5252 }
5253
5254 /**
5255 * Check whether the called view is a text editor, in which case it
5256 * would make sense to automatically display a soft input window for
5257 * it. Subclasses should override this if they implement
5258 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005259 * a call on that method would return a non-null InputConnection, and
5260 * they are really a first-class editor that the user would normally
5261 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005262 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005263 * <p>The default implementation always returns false. This does
5264 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5265 * will not be called or the user can not otherwise perform edits on your
5266 * view; it is just a hint to the system that this is not the primary
5267 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005268 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005269 * @return Returns true if this view is a text editor, else false.
5270 */
5271 public boolean onCheckIsTextEditor() {
5272 return false;
5273 }
Romain Guy8506ab42009-06-11 17:35:47 -07005274
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005275 /**
5276 * Create a new InputConnection for an InputMethod to interact
5277 * with the view. The default implementation returns null, since it doesn't
5278 * support input methods. You can override this to implement such support.
5279 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005280 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005281 * <p>When implementing this, you probably also want to implement
5282 * {@link #onCheckIsTextEditor()} to indicate you will return a
5283 * non-null InputConnection.
5284 *
5285 * @param outAttrs Fill in with attribute information about the connection.
5286 */
5287 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5288 return null;
5289 }
5290
5291 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005292 * Called by the {@link android.view.inputmethod.InputMethodManager}
5293 * when a view who is not the current
5294 * input connection target is trying to make a call on the manager. The
5295 * default implementation returns false; you can override this to return
5296 * true for certain views if you are performing InputConnection proxying
5297 * to them.
5298 * @param view The View that is making the InputMethodManager call.
5299 * @return Return true to allow the call, false to reject.
5300 */
5301 public boolean checkInputConnectionProxy(View view) {
5302 return false;
5303 }
Romain Guy8506ab42009-06-11 17:35:47 -07005304
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005306 * Show the context menu for this view. It is not safe to hold on to the
5307 * menu after returning from this method.
5308 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005309 * You should normally not overload this method. Overload
5310 * {@link #onCreateContextMenu(ContextMenu)} or define an
5311 * {@link OnCreateContextMenuListener} to add items to the context menu.
5312 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005313 * @param menu The context menu to populate
5314 */
5315 public void createContextMenu(ContextMenu menu) {
5316 ContextMenuInfo menuInfo = getContextMenuInfo();
5317
5318 // Sets the current menu info so all items added to menu will have
5319 // my extra info set.
5320 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5321
5322 onCreateContextMenu(menu);
5323 if (mOnCreateContextMenuListener != null) {
5324 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5325 }
5326
5327 // Clear the extra information so subsequent items that aren't mine don't
5328 // have my extra info.
5329 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5330
5331 if (mParent != null) {
5332 mParent.createContextMenu(menu);
5333 }
5334 }
5335
5336 /**
5337 * Views should implement this if they have extra information to associate
5338 * with the context menu. The return result is supplied as a parameter to
5339 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5340 * callback.
5341 *
5342 * @return Extra information about the item for which the context menu
5343 * should be shown. This information will vary across different
5344 * subclasses of View.
5345 */
5346 protected ContextMenuInfo getContextMenuInfo() {
5347 return null;
5348 }
5349
5350 /**
5351 * Views should implement this if the view itself is going to add items to
5352 * the context menu.
5353 *
5354 * @param menu the context menu to populate
5355 */
5356 protected void onCreateContextMenu(ContextMenu menu) {
5357 }
5358
5359 /**
5360 * Implement this method to handle trackball motion events. The
5361 * <em>relative</em> movement of the trackball since the last event
5362 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5363 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5364 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5365 * they will often be fractional values, representing the more fine-grained
5366 * movement information available from a trackball).
5367 *
5368 * @param event The motion event.
5369 * @return True if the event was handled, false otherwise.
5370 */
5371 public boolean onTrackballEvent(MotionEvent event) {
5372 return false;
5373 }
5374
5375 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005376 * Implement this method to handle generic motion events.
5377 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005378 * Generic motion events describe joystick movements, mouse hovers, track pad
5379 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005380 * {@link MotionEvent#getSource() source} of the motion event specifies
5381 * the class of input that was received. Implementations of this method
5382 * must examine the bits in the source before processing the event.
5383 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005384 * </p><p>
5385 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5386 * are delivered to the view under the pointer. All other generic motion events are
5387 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005388 * </p>
5389 * <code>
5390 * public boolean onGenericMotionEvent(MotionEvent event) {
5391 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005392 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5393 * // process the joystick movement...
5394 * return true;
5395 * }
5396 * }
5397 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5398 * switch (event.getAction()) {
5399 * case MotionEvent.ACTION_HOVER_MOVE:
5400 * // process the mouse hover movement...
5401 * return true;
5402 * case MotionEvent.ACTION_SCROLL:
5403 * // process the scroll wheel movement...
5404 * return true;
5405 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005406 * }
5407 * return super.onGenericMotionEvent(event);
5408 * }
5409 * </code>
5410 *
5411 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005412 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005413 */
5414 public boolean onGenericMotionEvent(MotionEvent event) {
5415 return false;
5416 }
5417
5418 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005419 * Implement this method to handle hover events.
5420 * <p>
5421 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5422 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5423 * </p><p>
5424 * The view receives hover enter as the pointer enters the bounds of the view and hover
5425 * exit as the pointer exits the bound of the view or just before the pointer goes down
5426 * (which implies that {@link #onTouchEvent} will be called soon).
5427 * </p><p>
5428 * If the view would like to handle the hover event itself and prevent its children
5429 * from receiving hover, it should return true from this method. If this method returns
5430 * true and a child has already received a hover enter event, the child will
5431 * automatically receive a hover exit event.
5432 * </p><p>
5433 * The default implementation sets the hovered state of the view if the view is
5434 * clickable.
5435 * </p>
5436 *
5437 * @param event The motion event that describes the hover.
5438 * @return True if this view handled the hover event and does not want its children
5439 * to receive the hover event.
5440 */
5441 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005442 switch (event.getAction()) {
5443 case MotionEvent.ACTION_HOVER_ENTER:
5444 setHovered(true);
5445 break;
5446
5447 case MotionEvent.ACTION_HOVER_EXIT:
5448 setHovered(false);
5449 break;
5450 }
5451
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005452 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005453 }
5454
5455 /**
5456 * Returns true if the view is currently hovered.
5457 *
5458 * @return True if the view is currently hovered.
5459 */
5460 public boolean isHovered() {
5461 return (mPrivateFlags & HOVERED) != 0;
5462 }
5463
5464 /**
5465 * Sets whether the view is currently hovered.
5466 *
5467 * @param hovered True if the view is hovered.
5468 */
5469 public void setHovered(boolean hovered) {
5470 if (hovered) {
5471 if ((mPrivateFlags & HOVERED) == 0) {
5472 mPrivateFlags |= HOVERED;
5473 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005474 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005475 }
5476 } else {
5477 if ((mPrivateFlags & HOVERED) != 0) {
5478 mPrivateFlags &= ~HOVERED;
5479 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005480 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005481 }
5482 }
5483 }
5484
5485 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005486 * Implement this method to handle touch screen motion events.
5487 *
5488 * @param event The motion event.
5489 * @return True if the event was handled, false otherwise.
5490 */
5491 public boolean onTouchEvent(MotionEvent event) {
5492 final int viewFlags = mViewFlags;
5493
5494 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005495 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5496 mPrivateFlags &= ~PRESSED;
5497 refreshDrawableState();
5498 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005499 // A disabled view that is clickable still consumes the touch
5500 // events, it just doesn't respond to them.
5501 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5502 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5503 }
5504
5505 if (mTouchDelegate != null) {
5506 if (mTouchDelegate.onTouchEvent(event)) {
5507 return true;
5508 }
5509 }
5510
5511 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5512 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5513 switch (event.getAction()) {
5514 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005515 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5516 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 // take focus if we don't have it already and we should in
5518 // touch mode.
5519 boolean focusTaken = false;
5520 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5521 focusTaken = requestFocus();
5522 }
5523
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005524 if (prepressed) {
5525 // The button is being released before we actually
5526 // showed it as pressed. Make it show the pressed
5527 // state now (before scheduling the click) to ensure
5528 // the user sees it.
5529 mPrivateFlags |= PRESSED;
5530 refreshDrawableState();
5531 }
Joe Malin32736f02011-01-19 16:14:20 -08005532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005533 if (!mHasPerformedLongPress) {
5534 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005535 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536
5537 // Only perform take click actions if we were in the pressed state
5538 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005539 // Use a Runnable and post this rather than calling
5540 // performClick directly. This lets other visual state
5541 // of the view update before click actions start.
5542 if (mPerformClick == null) {
5543 mPerformClick = new PerformClick();
5544 }
5545 if (!post(mPerformClick)) {
5546 performClick();
5547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005548 }
5549 }
5550
5551 if (mUnsetPressedState == null) {
5552 mUnsetPressedState = new UnsetPressedState();
5553 }
5554
Adam Powelle14579b2009-12-16 18:39:52 -08005555 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005556 postDelayed(mUnsetPressedState,
5557 ViewConfiguration.getPressedStateDuration());
5558 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005559 // If the post failed, unpress right now
5560 mUnsetPressedState.run();
5561 }
Adam Powelle14579b2009-12-16 18:39:52 -08005562 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 }
5564 break;
5565
5566 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005567 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005568
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07005569 if (performButtonActionOnTouchDown(event)) {
5570 break;
5571 }
5572
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005573 // Walk up the hierarchy to determine if we're inside a scrolling container.
5574 boolean isInScrollingContainer = false;
5575 ViewParent p = getParent();
5576 while (p != null && p instanceof ViewGroup) {
5577 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5578 isInScrollingContainer = true;
5579 break;
5580 }
5581 p = p.getParent();
5582 }
5583
5584 // For views inside a scrolling container, delay the pressed feedback for
5585 // a short period in case this is a scroll.
5586 if (isInScrollingContainer) {
5587 mPrivateFlags |= PREPRESSED;
5588 if (mPendingCheckForTap == null) {
5589 mPendingCheckForTap = new CheckForTap();
5590 }
5591 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5592 } else {
5593 // Not inside a scrolling container, so show the feedback right away
5594 mPrivateFlags |= PRESSED;
5595 refreshDrawableState();
5596 checkForLongClick(0);
5597 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005598 break;
5599
5600 case MotionEvent.ACTION_CANCEL:
5601 mPrivateFlags &= ~PRESSED;
5602 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005603 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005604 break;
5605
5606 case MotionEvent.ACTION_MOVE:
5607 final int x = (int) event.getX();
5608 final int y = (int) event.getY();
5609
5610 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005611 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005613 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005614 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005615 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005616 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005617
5618 // Need to switch from pressed to not pressed
5619 mPrivateFlags &= ~PRESSED;
5620 refreshDrawableState();
5621 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005622 }
5623 break;
5624 }
5625 return true;
5626 }
5627
5628 return false;
5629 }
5630
5631 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005632 * Remove the longpress detection timer.
5633 */
5634 private void removeLongPressCallback() {
5635 if (mPendingCheckForLongPress != null) {
5636 removeCallbacks(mPendingCheckForLongPress);
5637 }
5638 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005639
5640 /**
5641 * Remove the pending click action
5642 */
5643 private void removePerformClickCallback() {
5644 if (mPerformClick != null) {
5645 removeCallbacks(mPerformClick);
5646 }
5647 }
5648
Adam Powelle14579b2009-12-16 18:39:52 -08005649 /**
Romain Guya440b002010-02-24 15:57:54 -08005650 * Remove the prepress detection timer.
5651 */
5652 private void removeUnsetPressCallback() {
5653 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5654 setPressed(false);
5655 removeCallbacks(mUnsetPressedState);
5656 }
5657 }
5658
5659 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005660 * Remove the tap detection timer.
5661 */
5662 private void removeTapCallback() {
5663 if (mPendingCheckForTap != null) {
5664 mPrivateFlags &= ~PREPRESSED;
5665 removeCallbacks(mPendingCheckForTap);
5666 }
5667 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005668
5669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005670 * Cancels a pending long press. Your subclass can use this if you
5671 * want the context menu to come up if the user presses and holds
5672 * at the same place, but you don't want it to come up if they press
5673 * and then move around enough to cause scrolling.
5674 */
5675 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005676 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005677
5678 /*
5679 * The prepressed state handled by the tap callback is a display
5680 * construct, but the tap callback will post a long press callback
5681 * less its own timeout. Remove it here.
5682 */
5683 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005684 }
5685
5686 /**
5687 * Sets the TouchDelegate for this View.
5688 */
5689 public void setTouchDelegate(TouchDelegate delegate) {
5690 mTouchDelegate = delegate;
5691 }
5692
5693 /**
5694 * Gets the TouchDelegate for this View.
5695 */
5696 public TouchDelegate getTouchDelegate() {
5697 return mTouchDelegate;
5698 }
5699
5700 /**
5701 * Set flags controlling behavior of this view.
5702 *
5703 * @param flags Constant indicating the value which should be set
5704 * @param mask Constant indicating the bit range that should be changed
5705 */
5706 void setFlags(int flags, int mask) {
5707 int old = mViewFlags;
5708 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5709
5710 int changed = mViewFlags ^ old;
5711 if (changed == 0) {
5712 return;
5713 }
5714 int privateFlags = mPrivateFlags;
5715
5716 /* Check if the FOCUSABLE bit has changed */
5717 if (((changed & FOCUSABLE_MASK) != 0) &&
5718 ((privateFlags & HAS_BOUNDS) !=0)) {
5719 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5720 && ((privateFlags & FOCUSED) != 0)) {
5721 /* Give up focus if we are no longer focusable */
5722 clearFocus();
5723 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5724 && ((privateFlags & FOCUSED) == 0)) {
5725 /*
5726 * Tell the view system that we are now available to take focus
5727 * if no one else already has it.
5728 */
5729 if (mParent != null) mParent.focusableViewAvailable(this);
5730 }
5731 }
5732
5733 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5734 if ((changed & VISIBILITY_MASK) != 0) {
5735 /*
5736 * If this view is becoming visible, set the DRAWN flag so that
5737 * the next invalidate() will not be skipped.
5738 */
5739 mPrivateFlags |= DRAWN;
5740
5741 needGlobalAttributesUpdate(true);
5742
5743 // a view becoming visible is worth notifying the parent
5744 // about in case nothing has focus. even if this specific view
5745 // isn't focusable, it may contain something that is, so let
5746 // the root view try to give this focus if nothing else does.
5747 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5748 mParent.focusableViewAvailable(this);
5749 }
5750 }
5751 }
5752
5753 /* Check if the GONE bit has changed */
5754 if ((changed & GONE) != 0) {
5755 needGlobalAttributesUpdate(false);
5756 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005757 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005758
Romain Guyecd80ee2009-12-03 17:13:02 -08005759 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5760 if (hasFocus()) clearFocus();
5761 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005762 }
5763 if (mAttachInfo != null) {
5764 mAttachInfo.mViewVisibilityChanged = true;
5765 }
5766 }
5767
5768 /* Check if the VISIBLE bit has changed */
5769 if ((changed & INVISIBLE) != 0) {
5770 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005771 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005772
5773 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5774 // root view becoming invisible shouldn't clear focus
5775 if (getRootView() != this) {
5776 clearFocus();
5777 }
5778 }
5779 if (mAttachInfo != null) {
5780 mAttachInfo.mViewVisibilityChanged = true;
5781 }
5782 }
5783
Adam Powell326d8082009-12-09 15:10:07 -08005784 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005785 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005786 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5787 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005788 }
Adam Powell326d8082009-12-09 15:10:07 -08005789 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5790 }
5791
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5793 destroyDrawingCache();
5794 }
5795
5796 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5797 destroyDrawingCache();
5798 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005799 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005800 }
5801
5802 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5803 destroyDrawingCache();
5804 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5805 }
5806
5807 if ((changed & DRAW_MASK) != 0) {
5808 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5809 if (mBGDrawable != null) {
5810 mPrivateFlags &= ~SKIP_DRAW;
5811 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5812 } else {
5813 mPrivateFlags |= SKIP_DRAW;
5814 }
5815 } else {
5816 mPrivateFlags &= ~SKIP_DRAW;
5817 }
5818 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005819 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005820 }
5821
5822 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005823 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005824 mParent.recomputeViewAttributes(this);
5825 }
5826 }
5827 }
5828
5829 /**
5830 * Change the view's z order in the tree, so it's on top of other sibling
5831 * views
5832 */
5833 public void bringToFront() {
5834 if (mParent != null) {
5835 mParent.bringChildToFront(this);
5836 }
5837 }
5838
5839 /**
5840 * This is called in response to an internal scroll in this view (i.e., the
5841 * view scrolled its own contents). This is typically as a result of
5842 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5843 * called.
5844 *
5845 * @param l Current horizontal scroll origin.
5846 * @param t Current vertical scroll origin.
5847 * @param oldl Previous horizontal scroll origin.
5848 * @param oldt Previous vertical scroll origin.
5849 */
5850 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5851 mBackgroundSizeChanged = true;
5852
5853 final AttachInfo ai = mAttachInfo;
5854 if (ai != null) {
5855 ai.mViewScrollChanged = true;
5856 }
5857 }
5858
5859 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005860 * Interface definition for a callback to be invoked when the layout bounds of a view
5861 * changes due to layout processing.
5862 */
5863 public interface OnLayoutChangeListener {
5864 /**
5865 * Called when the focus state of a view has changed.
5866 *
5867 * @param v The view whose state has changed.
5868 * @param left The new value of the view's left property.
5869 * @param top The new value of the view's top property.
5870 * @param right The new value of the view's right property.
5871 * @param bottom The new value of the view's bottom property.
5872 * @param oldLeft The previous value of the view's left property.
5873 * @param oldTop The previous value of the view's top property.
5874 * @param oldRight The previous value of the view's right property.
5875 * @param oldBottom The previous value of the view's bottom property.
5876 */
5877 void onLayoutChange(View v, int left, int top, int right, int bottom,
5878 int oldLeft, int oldTop, int oldRight, int oldBottom);
5879 }
5880
5881 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005882 * This is called during layout when the size of this view has changed. If
5883 * you were just added to the view hierarchy, you're called with the old
5884 * values of 0.
5885 *
5886 * @param w Current width of this view.
5887 * @param h Current height of this view.
5888 * @param oldw Old width of this view.
5889 * @param oldh Old height of this view.
5890 */
5891 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5892 }
5893
5894 /**
5895 * Called by draw to draw the child views. This may be overridden
5896 * by derived classes to gain control just before its children are drawn
5897 * (but after its own view has been drawn).
5898 * @param canvas the canvas on which to draw the view
5899 */
5900 protected void dispatchDraw(Canvas canvas) {
5901 }
5902
5903 /**
5904 * Gets the parent of this view. Note that the parent is a
5905 * ViewParent and not necessarily a View.
5906 *
5907 * @return Parent of this view.
5908 */
5909 public final ViewParent getParent() {
5910 return mParent;
5911 }
5912
5913 /**
5914 * Return the scrolled left position of this view. This is the left edge of
5915 * the displayed part of your view. You do not need to draw any pixels
5916 * farther left, since those are outside of the frame of your view on
5917 * screen.
5918 *
5919 * @return The left edge of the displayed part of your view, in pixels.
5920 */
5921 public final int getScrollX() {
5922 return mScrollX;
5923 }
5924
5925 /**
5926 * Return the scrolled top position of this view. This is the top edge of
5927 * the displayed part of your view. You do not need to draw any pixels above
5928 * it, since those are outside of the frame of your view on screen.
5929 *
5930 * @return The top edge of the displayed part of your view, in pixels.
5931 */
5932 public final int getScrollY() {
5933 return mScrollY;
5934 }
5935
5936 /**
5937 * Return the width of the your view.
5938 *
5939 * @return The width of your view, in pixels.
5940 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005941 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005942 public final int getWidth() {
5943 return mRight - mLeft;
5944 }
5945
5946 /**
5947 * Return the height of your view.
5948 *
5949 * @return The height of your view, in pixels.
5950 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005951 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005952 public final int getHeight() {
5953 return mBottom - mTop;
5954 }
5955
5956 /**
5957 * Return the visible drawing bounds of your view. Fills in the output
5958 * rectangle with the values from getScrollX(), getScrollY(),
5959 * getWidth(), and getHeight().
5960 *
5961 * @param outRect The (scrolled) drawing bounds of the view.
5962 */
5963 public void getDrawingRect(Rect outRect) {
5964 outRect.left = mScrollX;
5965 outRect.top = mScrollY;
5966 outRect.right = mScrollX + (mRight - mLeft);
5967 outRect.bottom = mScrollY + (mBottom - mTop);
5968 }
5969
5970 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005971 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5972 * raw width component (that is the result is masked by
5973 * {@link #MEASURED_SIZE_MASK}).
5974 *
5975 * @return The raw measured width of this view.
5976 */
5977 public final int getMeasuredWidth() {
5978 return mMeasuredWidth & MEASURED_SIZE_MASK;
5979 }
5980
5981 /**
5982 * Return the full width measurement information for this view as computed
5983 * by the most recent call to {@link #measure}. This result is a bit mask
5984 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005985 * This should be used during measurement and layout calculations only. Use
5986 * {@link #getWidth()} to see how wide a view is after layout.
5987 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005988 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005989 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005990 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005991 return mMeasuredWidth;
5992 }
5993
5994 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005995 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5996 * raw width component (that is the result is masked by
5997 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005998 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005999 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 */
6001 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006002 return mMeasuredHeight & MEASURED_SIZE_MASK;
6003 }
6004
6005 /**
6006 * Return the full height measurement information for this view as computed
6007 * by the most recent call to {@link #measure}. This result is a bit mask
6008 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6009 * This should be used during measurement and layout calculations only. Use
6010 * {@link #getHeight()} to see how wide a view is after layout.
6011 *
6012 * @return The measured width of this view as a bit mask.
6013 */
6014 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006015 return mMeasuredHeight;
6016 }
6017
6018 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006019 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6020 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6021 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6022 * and the height component is at the shifted bits
6023 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6024 */
6025 public final int getMeasuredState() {
6026 return (mMeasuredWidth&MEASURED_STATE_MASK)
6027 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6028 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6029 }
6030
6031 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006032 * The transform matrix of this view, which is calculated based on the current
6033 * roation, scale, and pivot properties.
6034 *
6035 * @see #getRotation()
6036 * @see #getScaleX()
6037 * @see #getScaleY()
6038 * @see #getPivotX()
6039 * @see #getPivotY()
6040 * @return The current transform matrix for the view
6041 */
6042 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006043 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006044 return mMatrix;
6045 }
6046
6047 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006048 * Utility function to determine if the value is far enough away from zero to be
6049 * considered non-zero.
6050 * @param value A floating point value to check for zero-ness
6051 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6052 */
6053 private static boolean nonzero(float value) {
6054 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6055 }
6056
6057 /**
Jeff Brown86671742010-09-30 20:00:15 -07006058 * Returns true if the transform matrix is the identity matrix.
6059 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006060 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006061 * @return True if the transform matrix is the identity matrix, false otherwise.
6062 */
Jeff Brown86671742010-09-30 20:00:15 -07006063 final boolean hasIdentityMatrix() {
6064 updateMatrix();
6065 return mMatrixIsIdentity;
6066 }
6067
6068 /**
6069 * Recomputes the transform matrix if necessary.
6070 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006071 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006072 if (mMatrixDirty) {
6073 // transform-related properties have changed since the last time someone
6074 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006075
6076 // Figure out if we need to update the pivot point
6077 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006078 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006079 mPrevWidth = mRight - mLeft;
6080 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006081 mPivotX = mPrevWidth / 2f;
6082 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006083 }
6084 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006085 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006086 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6087 mMatrix.setTranslate(mTranslationX, mTranslationY);
6088 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6089 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6090 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006091 if (mCamera == null) {
6092 mCamera = new Camera();
6093 matrix3D = new Matrix();
6094 }
6095 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006096 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006097 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006098 mCamera.getMatrix(matrix3D);
6099 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006100 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006101 mMatrix.postConcat(matrix3D);
6102 mCamera.restore();
6103 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006104 mMatrixDirty = false;
6105 mMatrixIsIdentity = mMatrix.isIdentity();
6106 mInverseMatrixDirty = true;
6107 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006108 }
6109
6110 /**
6111 * Utility method to retrieve the inverse of the current mMatrix property.
6112 * We cache the matrix to avoid recalculating it when transform properties
6113 * have not changed.
6114 *
6115 * @return The inverse of the current matrix of this view.
6116 */
Jeff Brown86671742010-09-30 20:00:15 -07006117 final Matrix getInverseMatrix() {
6118 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006119 if (mInverseMatrixDirty) {
6120 if (mInverseMatrix == null) {
6121 mInverseMatrix = new Matrix();
6122 }
6123 mMatrix.invert(mInverseMatrix);
6124 mInverseMatrixDirty = false;
6125 }
6126 return mInverseMatrix;
6127 }
6128
6129 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006130 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6131 * views are drawn) from the camera to this view. The camera's distance
6132 * affects 3D transformations, for instance rotations around the X and Y
6133 * axis. If the rotationX or rotationY properties are changed and this view is
6134 * large (more than half the size of the screen), it is recommended to always
6135 * use a camera distance that's greater than the height (X axis rotation) or
6136 * the width (Y axis rotation) of this view.</p>
6137 *
6138 * <p>The distance of the camera from the view plane can have an affect on the
6139 * perspective distortion of the view when it is rotated around the x or y axis.
6140 * For example, a large distance will result in a large viewing angle, and there
6141 * will not be much perspective distortion of the view as it rotates. A short
6142 * distance may cause much more perspective distortion upon rotation, and can
6143 * also result in some drawing artifacts if the rotated view ends up partially
6144 * behind the camera (which is why the recommendation is to use a distance at
6145 * least as far as the size of the view, if the view is to be rotated.)</p>
6146 *
6147 * <p>The distance is expressed in "depth pixels." The default distance depends
6148 * on the screen density. For instance, on a medium density display, the
6149 * default distance is 1280. On a high density display, the default distance
6150 * is 1920.</p>
6151 *
6152 * <p>If you want to specify a distance that leads to visually consistent
6153 * results across various densities, use the following formula:</p>
6154 * <pre>
6155 * float scale = context.getResources().getDisplayMetrics().density;
6156 * view.setCameraDistance(distance * scale);
6157 * </pre>
6158 *
6159 * <p>The density scale factor of a high density display is 1.5,
6160 * and 1920 = 1280 * 1.5.</p>
6161 *
6162 * @param distance The distance in "depth pixels", if negative the opposite
6163 * value is used
6164 *
6165 * @see #setRotationX(float)
6166 * @see #setRotationY(float)
6167 */
6168 public void setCameraDistance(float distance) {
6169 invalidateParentCaches();
6170 invalidate(false);
6171
6172 final float dpi = mResources.getDisplayMetrics().densityDpi;
6173 if (mCamera == null) {
6174 mCamera = new Camera();
6175 matrix3D = new Matrix();
6176 }
6177
6178 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6179 mMatrixDirty = true;
6180
6181 invalidate(false);
6182 }
6183
6184 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006185 * The degrees that the view is rotated around the pivot point.
6186 *
Romain Guya5364ee2011-02-24 14:46:04 -08006187 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006188 * @see #getPivotX()
6189 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006190 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006191 * @return The degrees of rotation.
6192 */
6193 public float getRotation() {
6194 return mRotation;
6195 }
6196
6197 /**
Chet Haase897247b2010-09-09 14:54:47 -07006198 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6199 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006200 *
6201 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006202 *
6203 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006204 * @see #getPivotX()
6205 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006206 * @see #setRotationX(float)
6207 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006208 *
6209 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006210 */
6211 public void setRotation(float rotation) {
6212 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006213 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006214 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006215 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006216 mRotation = rotation;
6217 mMatrixDirty = true;
6218 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006219 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006220 }
6221 }
6222
6223 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006224 * The degrees that the view is rotated around the vertical axis through the pivot point.
6225 *
6226 * @see #getPivotX()
6227 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006228 * @see #setRotationY(float)
6229 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006230 * @return The degrees of Y rotation.
6231 */
6232 public float getRotationY() {
6233 return mRotationY;
6234 }
6235
6236 /**
Chet Haase897247b2010-09-09 14:54:47 -07006237 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6238 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6239 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006240 *
6241 * When rotating large views, it is recommended to adjust the camera distance
6242 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006243 *
6244 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006245 *
6246 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006247 * @see #getPivotX()
6248 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006249 * @see #setRotation(float)
6250 * @see #setRotationX(float)
6251 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006252 *
6253 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006254 */
6255 public void setRotationY(float rotationY) {
6256 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006257 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006258 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006259 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006260 mRotationY = rotationY;
6261 mMatrixDirty = true;
6262 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006263 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006264 }
6265 }
6266
6267 /**
6268 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6269 *
6270 * @see #getPivotX()
6271 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006272 * @see #setRotationX(float)
6273 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006274 * @return The degrees of X rotation.
6275 */
6276 public float getRotationX() {
6277 return mRotationX;
6278 }
6279
6280 /**
Chet Haase897247b2010-09-09 14:54:47 -07006281 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6282 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6283 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006284 *
6285 * When rotating large views, it is recommended to adjust the camera distance
6286 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006287 *
6288 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006289 *
6290 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006291 * @see #getPivotX()
6292 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006293 * @see #setRotation(float)
6294 * @see #setRotationY(float)
6295 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006296 *
6297 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006298 */
6299 public void setRotationX(float rotationX) {
6300 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006301 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006302 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006303 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006304 mRotationX = rotationX;
6305 mMatrixDirty = true;
6306 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006307 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006308 }
6309 }
6310
6311 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006312 * The amount that the view is scaled in x around the pivot point, as a proportion of
6313 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6314 *
Joe Onorato93162322010-09-16 15:42:01 -04006315 * <p>By default, this is 1.0f.
6316 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006317 * @see #getPivotX()
6318 * @see #getPivotY()
6319 * @return The scaling factor.
6320 */
6321 public float getScaleX() {
6322 return mScaleX;
6323 }
6324
6325 /**
6326 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6327 * the view's unscaled width. A value of 1 means that no scaling is applied.
6328 *
6329 * @param scaleX The scaling factor.
6330 * @see #getPivotX()
6331 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006332 *
6333 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006334 */
6335 public void setScaleX(float scaleX) {
6336 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006337 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006338 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006339 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006340 mScaleX = scaleX;
6341 mMatrixDirty = true;
6342 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006343 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006344 }
6345 }
6346
6347 /**
6348 * The amount that the view is scaled in y around the pivot point, as a proportion of
6349 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6350 *
Joe Onorato93162322010-09-16 15:42:01 -04006351 * <p>By default, this is 1.0f.
6352 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006353 * @see #getPivotX()
6354 * @see #getPivotY()
6355 * @return The scaling factor.
6356 */
6357 public float getScaleY() {
6358 return mScaleY;
6359 }
6360
6361 /**
6362 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6363 * the view's unscaled width. A value of 1 means that no scaling is applied.
6364 *
6365 * @param scaleY The scaling factor.
6366 * @see #getPivotX()
6367 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006368 *
6369 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006370 */
6371 public void setScaleY(float scaleY) {
6372 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006373 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006374 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006375 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006376 mScaleY = scaleY;
6377 mMatrixDirty = true;
6378 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006379 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006380 }
6381 }
6382
6383 /**
6384 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6385 * and {@link #setScaleX(float) scaled}.
6386 *
6387 * @see #getRotation()
6388 * @see #getScaleX()
6389 * @see #getScaleY()
6390 * @see #getPivotY()
6391 * @return The x location of the pivot point.
6392 */
6393 public float getPivotX() {
6394 return mPivotX;
6395 }
6396
6397 /**
6398 * Sets the x location of the point around which the view is
6399 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006400 * By default, the pivot point is centered on the object.
6401 * Setting this property disables this behavior and causes the view to use only the
6402 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006403 *
6404 * @param pivotX The x location of the pivot point.
6405 * @see #getRotation()
6406 * @see #getScaleX()
6407 * @see #getScaleY()
6408 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006409 *
6410 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006411 */
6412 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006413 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006414 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006415 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006416 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006417 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006418 mPivotX = pivotX;
6419 mMatrixDirty = true;
6420 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006421 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006422 }
6423 }
6424
6425 /**
6426 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6427 * and {@link #setScaleY(float) scaled}.
6428 *
6429 * @see #getRotation()
6430 * @see #getScaleX()
6431 * @see #getScaleY()
6432 * @see #getPivotY()
6433 * @return The y location of the pivot point.
6434 */
6435 public float getPivotY() {
6436 return mPivotY;
6437 }
6438
6439 /**
6440 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006441 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6442 * Setting this property disables this behavior and causes the view to use only the
6443 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006444 *
6445 * @param pivotY The y location of the pivot point.
6446 * @see #getRotation()
6447 * @see #getScaleX()
6448 * @see #getScaleY()
6449 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006450 *
6451 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006452 */
6453 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006454 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006455 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006456 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006457 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006458 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006459 mPivotY = pivotY;
6460 mMatrixDirty = true;
6461 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006462 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006463 }
6464 }
6465
6466 /**
6467 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6468 * completely transparent and 1 means the view is completely opaque.
6469 *
Joe Onorato93162322010-09-16 15:42:01 -04006470 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006471 * @return The opacity of the view.
6472 */
6473 public float getAlpha() {
6474 return mAlpha;
6475 }
6476
6477 /**
Romain Guy171c5922011-01-06 10:04:23 -08006478 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6479 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006480 *
Romain Guy171c5922011-01-06 10:04:23 -08006481 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6482 * responsible for applying the opacity itself. Otherwise, calling this method is
6483 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006484 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006485 *
6486 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006487 *
Joe Malin32736f02011-01-19 16:14:20 -08006488 * @see #setLayerType(int, android.graphics.Paint)
6489 *
Chet Haase73066682010-11-29 15:55:32 -08006490 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006491 */
6492 public void setAlpha(float alpha) {
6493 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006494 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006495 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006496 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006497 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006498 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006499 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006500 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006501 invalidate(false);
6502 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006503 }
6504
6505 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006506 * Faster version of setAlpha() which performs the same steps except there are
6507 * no calls to invalidate(). The caller of this function should perform proper invalidation
6508 * on the parent and this object. The return value indicates whether the subclass handles
6509 * alpha (the return value for onSetAlpha()).
6510 *
6511 * @param alpha The new value for the alpha property
6512 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6513 */
6514 boolean setAlphaNoInvalidation(float alpha) {
6515 mAlpha = alpha;
6516 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6517 if (subclassHandlesAlpha) {
6518 mPrivateFlags |= ALPHA_SET;
6519 } else {
6520 mPrivateFlags &= ~ALPHA_SET;
6521 }
6522 return subclassHandlesAlpha;
6523 }
6524
6525 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006526 * Top position of this view relative to its parent.
6527 *
6528 * @return The top of this view, in pixels.
6529 */
6530 @ViewDebug.CapturedViewProperty
6531 public final int getTop() {
6532 return mTop;
6533 }
6534
6535 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006536 * Sets the top position of this view relative to its parent. This method is meant to be called
6537 * by the layout system and should not generally be called otherwise, because the property
6538 * may be changed at any time by the layout.
6539 *
6540 * @param top The top of this view, in pixels.
6541 */
6542 public final void setTop(int top) {
6543 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006544 updateMatrix();
6545 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006546 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006547 int minTop;
6548 int yLoc;
6549 if (top < mTop) {
6550 minTop = top;
6551 yLoc = top - mTop;
6552 } else {
6553 minTop = mTop;
6554 yLoc = 0;
6555 }
Chet Haasee9140a72011-02-16 16:23:29 -08006556 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006557 }
6558 } else {
6559 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006560 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006561 }
6562
Chet Haaseed032702010-10-01 14:05:54 -07006563 int width = mRight - mLeft;
6564 int oldHeight = mBottom - mTop;
6565
Chet Haase21cd1382010-09-01 17:42:29 -07006566 mTop = top;
6567
Chet Haaseed032702010-10-01 14:05:54 -07006568 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6569
Chet Haase21cd1382010-09-01 17:42:29 -07006570 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006571 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6572 // A change in dimension means an auto-centered pivot point changes, too
6573 mMatrixDirty = true;
6574 }
Chet Haase21cd1382010-09-01 17:42:29 -07006575 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006576 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006577 }
Chet Haase55dbb652010-12-21 20:15:08 -08006578 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006579 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006580 }
6581 }
6582
6583 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006584 * Bottom position of this view relative to its parent.
6585 *
6586 * @return The bottom of this view, in pixels.
6587 */
6588 @ViewDebug.CapturedViewProperty
6589 public final int getBottom() {
6590 return mBottom;
6591 }
6592
6593 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006594 * True if this view has changed since the last time being drawn.
6595 *
6596 * @return The dirty state of this view.
6597 */
6598 public boolean isDirty() {
6599 return (mPrivateFlags & DIRTY_MASK) != 0;
6600 }
6601
6602 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006603 * Sets the bottom position of this view relative to its parent. This method is meant to be
6604 * called by the layout system and should not generally be called otherwise, because the
6605 * property may be changed at any time by the layout.
6606 *
6607 * @param bottom The bottom of this view, in pixels.
6608 */
6609 public final void setBottom(int bottom) {
6610 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006611 updateMatrix();
6612 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006613 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006614 int maxBottom;
6615 if (bottom < mBottom) {
6616 maxBottom = mBottom;
6617 } else {
6618 maxBottom = bottom;
6619 }
Chet Haasee9140a72011-02-16 16:23:29 -08006620 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006621 }
6622 } else {
6623 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006624 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006625 }
6626
Chet Haaseed032702010-10-01 14:05:54 -07006627 int width = mRight - mLeft;
6628 int oldHeight = mBottom - mTop;
6629
Chet Haase21cd1382010-09-01 17:42:29 -07006630 mBottom = bottom;
6631
Chet Haaseed032702010-10-01 14:05:54 -07006632 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6633
Chet Haase21cd1382010-09-01 17:42:29 -07006634 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006635 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6636 // A change in dimension means an auto-centered pivot point changes, too
6637 mMatrixDirty = true;
6638 }
Chet Haase21cd1382010-09-01 17:42:29 -07006639 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006640 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006641 }
Chet Haase55dbb652010-12-21 20:15:08 -08006642 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006643 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006644 }
6645 }
6646
6647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006648 * Left position of this view relative to its parent.
6649 *
6650 * @return The left edge of this view, in pixels.
6651 */
6652 @ViewDebug.CapturedViewProperty
6653 public final int getLeft() {
6654 return mLeft;
6655 }
6656
6657 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006658 * Sets the left position of this view relative to its parent. This method is meant to be called
6659 * by the layout system and should not generally be called otherwise, because the property
6660 * may be changed at any time by the layout.
6661 *
6662 * @param left The bottom of this view, in pixels.
6663 */
6664 public final void setLeft(int left) {
6665 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006666 updateMatrix();
6667 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006668 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006669 int minLeft;
6670 int xLoc;
6671 if (left < mLeft) {
6672 minLeft = left;
6673 xLoc = left - mLeft;
6674 } else {
6675 minLeft = mLeft;
6676 xLoc = 0;
6677 }
Chet Haasee9140a72011-02-16 16:23:29 -08006678 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006679 }
6680 } else {
6681 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006682 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006683 }
6684
Chet Haaseed032702010-10-01 14:05:54 -07006685 int oldWidth = mRight - mLeft;
6686 int height = mBottom - mTop;
6687
Chet Haase21cd1382010-09-01 17:42:29 -07006688 mLeft = left;
6689
Chet Haaseed032702010-10-01 14:05:54 -07006690 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6691
Chet Haase21cd1382010-09-01 17:42:29 -07006692 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006693 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6694 // A change in dimension means an auto-centered pivot point changes, too
6695 mMatrixDirty = true;
6696 }
Chet Haase21cd1382010-09-01 17:42:29 -07006697 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006698 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006699 }
Chet Haase55dbb652010-12-21 20:15:08 -08006700 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006701 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006702 }
6703 }
6704
6705 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006706 * Right position of this view relative to its parent.
6707 *
6708 * @return The right edge of this view, in pixels.
6709 */
6710 @ViewDebug.CapturedViewProperty
6711 public final int getRight() {
6712 return mRight;
6713 }
6714
6715 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006716 * Sets the right position of this view relative to its parent. This method is meant to be called
6717 * by the layout system and should not generally be called otherwise, because the property
6718 * may be changed at any time by the layout.
6719 *
6720 * @param right The bottom of this view, in pixels.
6721 */
6722 public final void setRight(int right) {
6723 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006724 updateMatrix();
6725 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006726 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006727 int maxRight;
6728 if (right < mRight) {
6729 maxRight = mRight;
6730 } else {
6731 maxRight = right;
6732 }
Chet Haasee9140a72011-02-16 16:23:29 -08006733 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006734 }
6735 } else {
6736 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006737 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006738 }
6739
Chet Haaseed032702010-10-01 14:05:54 -07006740 int oldWidth = mRight - mLeft;
6741 int height = mBottom - mTop;
6742
Chet Haase21cd1382010-09-01 17:42:29 -07006743 mRight = right;
6744
Chet Haaseed032702010-10-01 14:05:54 -07006745 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6746
Chet Haase21cd1382010-09-01 17:42:29 -07006747 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006748 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6749 // A change in dimension means an auto-centered pivot point changes, too
6750 mMatrixDirty = true;
6751 }
Chet Haase21cd1382010-09-01 17:42:29 -07006752 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006753 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006754 }
Chet Haase55dbb652010-12-21 20:15:08 -08006755 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006756 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006757 }
6758 }
6759
6760 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006761 * The visual x position of this view, in pixels. This is equivalent to the
6762 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006763 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006764 *
Chet Haasedf030d22010-07-30 17:22:38 -07006765 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006766 */
Chet Haasedf030d22010-07-30 17:22:38 -07006767 public float getX() {
6768 return mLeft + mTranslationX;
6769 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006770
Chet Haasedf030d22010-07-30 17:22:38 -07006771 /**
6772 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6773 * {@link #setTranslationX(float) translationX} property to be the difference between
6774 * the x value passed in and the current {@link #getLeft() left} property.
6775 *
6776 * @param x The visual x position of this view, in pixels.
6777 */
6778 public void setX(float x) {
6779 setTranslationX(x - mLeft);
6780 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006781
Chet Haasedf030d22010-07-30 17:22:38 -07006782 /**
6783 * The visual y position of this view, in pixels. This is equivalent to the
6784 * {@link #setTranslationY(float) translationY} property plus the current
6785 * {@link #getTop() top} property.
6786 *
6787 * @return The visual y position of this view, in pixels.
6788 */
6789 public float getY() {
6790 return mTop + mTranslationY;
6791 }
6792
6793 /**
6794 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6795 * {@link #setTranslationY(float) translationY} property to be the difference between
6796 * the y value passed in and the current {@link #getTop() top} property.
6797 *
6798 * @param y The visual y position of this view, in pixels.
6799 */
6800 public void setY(float y) {
6801 setTranslationY(y - mTop);
6802 }
6803
6804
6805 /**
6806 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6807 * This position is post-layout, in addition to wherever the object's
6808 * layout placed it.
6809 *
6810 * @return The horizontal position of this view relative to its left position, in pixels.
6811 */
6812 public float getTranslationX() {
6813 return mTranslationX;
6814 }
6815
6816 /**
6817 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6818 * This effectively positions the object post-layout, in addition to wherever the object's
6819 * layout placed it.
6820 *
6821 * @param translationX The horizontal position of this view relative to its left position,
6822 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006823 *
6824 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006825 */
6826 public void setTranslationX(float translationX) {
6827 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006828 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006829 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006830 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006831 mTranslationX = translationX;
6832 mMatrixDirty = true;
6833 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006834 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006835 }
6836 }
6837
6838 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006839 * The horizontal location of this view relative to its {@link #getTop() top} position.
6840 * This position is post-layout, in addition to wherever the object's
6841 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006842 *
Chet Haasedf030d22010-07-30 17:22:38 -07006843 * @return The vertical position of this view relative to its top position,
6844 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006845 */
Chet Haasedf030d22010-07-30 17:22:38 -07006846 public float getTranslationY() {
6847 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006848 }
6849
6850 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006851 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6852 * This effectively positions the object post-layout, in addition to wherever the object's
6853 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006854 *
Chet Haasedf030d22010-07-30 17:22:38 -07006855 * @param translationY The vertical position of this view relative to its top position,
6856 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006857 *
6858 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006859 */
Chet Haasedf030d22010-07-30 17:22:38 -07006860 public void setTranslationY(float translationY) {
6861 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006862 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006863 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006864 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006865 mTranslationY = translationY;
6866 mMatrixDirty = true;
6867 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006868 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006869 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006870 }
6871
6872 /**
Romain Guyda489792011-02-03 01:05:15 -08006873 * @hide
6874 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006875 public void setFastTranslationX(float x) {
6876 mTranslationX = x;
6877 mMatrixDirty = true;
6878 }
6879
6880 /**
6881 * @hide
6882 */
6883 public void setFastTranslationY(float y) {
6884 mTranslationY = y;
6885 mMatrixDirty = true;
6886 }
6887
6888 /**
6889 * @hide
6890 */
Romain Guyda489792011-02-03 01:05:15 -08006891 public void setFastX(float x) {
6892 mTranslationX = x - mLeft;
6893 mMatrixDirty = true;
6894 }
6895
6896 /**
6897 * @hide
6898 */
6899 public void setFastY(float y) {
6900 mTranslationY = y - mTop;
6901 mMatrixDirty = true;
6902 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006903
Romain Guyda489792011-02-03 01:05:15 -08006904 /**
6905 * @hide
6906 */
6907 public void setFastScaleX(float x) {
6908 mScaleX = x;
6909 mMatrixDirty = true;
6910 }
6911
6912 /**
6913 * @hide
6914 */
6915 public void setFastScaleY(float y) {
6916 mScaleY = y;
6917 mMatrixDirty = true;
6918 }
6919
6920 /**
6921 * @hide
6922 */
6923 public void setFastAlpha(float alpha) {
6924 mAlpha = alpha;
6925 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006926
Romain Guyda489792011-02-03 01:05:15 -08006927 /**
6928 * @hide
6929 */
6930 public void setFastRotationY(float y) {
6931 mRotationY = y;
6932 mMatrixDirty = true;
6933 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006934
Romain Guyda489792011-02-03 01:05:15 -08006935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006936 * Hit rectangle in parent's coordinates
6937 *
6938 * @param outRect The hit rectangle of the view.
6939 */
6940 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006941 updateMatrix();
6942 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006943 outRect.set(mLeft, mTop, mRight, mBottom);
6944 } else {
6945 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006946 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006947 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006948 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6949 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006950 }
6951 }
6952
6953 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006954 * Determines whether the given point, in local coordinates is inside the view.
6955 */
6956 /*package*/ final boolean pointInView(float localX, float localY) {
6957 return localX >= 0 && localX < (mRight - mLeft)
6958 && localY >= 0 && localY < (mBottom - mTop);
6959 }
6960
6961 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006962 * Utility method to determine whether the given point, in local coordinates,
6963 * is inside the view, where the area of the view is expanded by the slop factor.
6964 * This method is called while processing touch-move events to determine if the event
6965 * is still within the view.
6966 */
6967 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006968 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006969 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006970 }
6971
6972 /**
6973 * When a view has focus and the user navigates away from it, the next view is searched for
6974 * starting from the rectangle filled in by this method.
6975 *
6976 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6977 * view maintains some idea of internal selection, such as a cursor, or a selected row
6978 * or column, you should override this method and fill in a more specific rectangle.
6979 *
6980 * @param r The rectangle to fill in, in this view's coordinates.
6981 */
6982 public void getFocusedRect(Rect r) {
6983 getDrawingRect(r);
6984 }
6985
6986 /**
6987 * If some part of this view is not clipped by any of its parents, then
6988 * return that area in r in global (root) coordinates. To convert r to local
6989 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6990 * -globalOffset.y)) If the view is completely clipped or translated out,
6991 * return false.
6992 *
6993 * @param r If true is returned, r holds the global coordinates of the
6994 * visible portion of this view.
6995 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6996 * between this view and its root. globalOffet may be null.
6997 * @return true if r is non-empty (i.e. part of the view is visible at the
6998 * root level.
6999 */
7000 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7001 int width = mRight - mLeft;
7002 int height = mBottom - mTop;
7003 if (width > 0 && height > 0) {
7004 r.set(0, 0, width, height);
7005 if (globalOffset != null) {
7006 globalOffset.set(-mScrollX, -mScrollY);
7007 }
7008 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7009 }
7010 return false;
7011 }
7012
7013 public final boolean getGlobalVisibleRect(Rect r) {
7014 return getGlobalVisibleRect(r, null);
7015 }
7016
7017 public final boolean getLocalVisibleRect(Rect r) {
7018 Point offset = new Point();
7019 if (getGlobalVisibleRect(r, offset)) {
7020 r.offset(-offset.x, -offset.y); // make r local
7021 return true;
7022 }
7023 return false;
7024 }
7025
7026 /**
7027 * Offset this view's vertical location by the specified number of pixels.
7028 *
7029 * @param offset the number of pixels to offset the view by
7030 */
7031 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007032 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007033 updateMatrix();
7034 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007035 final ViewParent p = mParent;
7036 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007037 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007038 int minTop;
7039 int maxBottom;
7040 int yLoc;
7041 if (offset < 0) {
7042 minTop = mTop + offset;
7043 maxBottom = mBottom;
7044 yLoc = offset;
7045 } else {
7046 minTop = mTop;
7047 maxBottom = mBottom + offset;
7048 yLoc = 0;
7049 }
7050 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7051 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007052 }
7053 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007054 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007055 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007056
Chet Haasec3aa3612010-06-17 08:50:37 -07007057 mTop += offset;
7058 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007059
Chet Haasec3aa3612010-06-17 08:50:37 -07007060 if (!mMatrixIsIdentity) {
7061 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007062 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007063 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007064 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007066 }
7067
7068 /**
7069 * Offset this view's horizontal location by the specified amount of pixels.
7070 *
7071 * @param offset the numer of pixels to offset the view by
7072 */
7073 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007074 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007075 updateMatrix();
7076 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007077 final ViewParent p = mParent;
7078 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007079 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007080 int minLeft;
7081 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007082 if (offset < 0) {
7083 minLeft = mLeft + offset;
7084 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007085 } else {
7086 minLeft = mLeft;
7087 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007088 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007090 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007091 }
7092 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007093 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007094 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007095
Chet Haasec3aa3612010-06-17 08:50:37 -07007096 mLeft += offset;
7097 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007098
Chet Haasec3aa3612010-06-17 08:50:37 -07007099 if (!mMatrixIsIdentity) {
7100 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007101 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007102 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007103 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007105 }
7106
7107 /**
7108 * Get the LayoutParams associated with this view. All views should have
7109 * layout parameters. These supply parameters to the <i>parent</i> of this
7110 * view specifying how it should be arranged. There are many subclasses of
7111 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7112 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007113 *
7114 * This method may return null if this View is not attached to a parent
7115 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7116 * was not invoked successfully. When a View is attached to a parent
7117 * ViewGroup, this method must not return null.
7118 *
7119 * @return The LayoutParams associated with this view, or null if no
7120 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007121 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007122 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007123 public ViewGroup.LayoutParams getLayoutParams() {
7124 return mLayoutParams;
7125 }
7126
7127 /**
7128 * Set the layout parameters associated with this view. These supply
7129 * parameters to the <i>parent</i> of this view specifying how it should be
7130 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7131 * correspond to the different subclasses of ViewGroup that are responsible
7132 * for arranging their children.
7133 *
Romain Guy01c174b2011-02-22 11:51:06 -08007134 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007135 */
7136 public void setLayoutParams(ViewGroup.LayoutParams params) {
7137 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007138 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007139 }
7140 mLayoutParams = params;
7141 requestLayout();
7142 }
7143
7144 /**
7145 * Set the scrolled position of your view. This will cause a call to
7146 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7147 * invalidated.
7148 * @param x the x position to scroll to
7149 * @param y the y position to scroll to
7150 */
7151 public void scrollTo(int x, int y) {
7152 if (mScrollX != x || mScrollY != y) {
7153 int oldX = mScrollX;
7154 int oldY = mScrollY;
7155 mScrollX = x;
7156 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007157 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007158 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007159 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007160 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007162 }
7163 }
7164
7165 /**
7166 * Move the scrolled position of your view. This will cause a call to
7167 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7168 * invalidated.
7169 * @param x the amount of pixels to scroll by horizontally
7170 * @param y the amount of pixels to scroll by vertically
7171 */
7172 public void scrollBy(int x, int y) {
7173 scrollTo(mScrollX + x, mScrollY + y);
7174 }
7175
7176 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007177 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7178 * animation to fade the scrollbars out after a default delay. If a subclass
7179 * provides animated scrolling, the start delay should equal the duration
7180 * of the scrolling animation.</p>
7181 *
7182 * <p>The animation starts only if at least one of the scrollbars is
7183 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7184 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7185 * this method returns true, and false otherwise. If the animation is
7186 * started, this method calls {@link #invalidate()}; in that case the
7187 * caller should not call {@link #invalidate()}.</p>
7188 *
7189 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007190 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007191 *
7192 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7193 * and {@link #scrollTo(int, int)}.</p>
7194 *
7195 * @return true if the animation is played, false otherwise
7196 *
7197 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007198 * @see #scrollBy(int, int)
7199 * @see #scrollTo(int, int)
7200 * @see #isHorizontalScrollBarEnabled()
7201 * @see #isVerticalScrollBarEnabled()
7202 * @see #setHorizontalScrollBarEnabled(boolean)
7203 * @see #setVerticalScrollBarEnabled(boolean)
7204 */
7205 protected boolean awakenScrollBars() {
7206 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007207 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007208 }
7209
7210 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007211 * Trigger the scrollbars to draw.
7212 * This method differs from awakenScrollBars() only in its default duration.
7213 * initialAwakenScrollBars() will show the scroll bars for longer than
7214 * usual to give the user more of a chance to notice them.
7215 *
7216 * @return true if the animation is played, false otherwise.
7217 */
7218 private boolean initialAwakenScrollBars() {
7219 return mScrollCache != null &&
7220 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7221 }
7222
7223 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007224 * <p>
7225 * Trigger the scrollbars to draw. When invoked this method starts an
7226 * animation to fade the scrollbars out after a fixed delay. If a subclass
7227 * provides animated scrolling, the start delay should equal the duration of
7228 * the scrolling animation.
7229 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007230 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007231 * <p>
7232 * The animation starts only if at least one of the scrollbars is enabled,
7233 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7234 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7235 * this method returns true, and false otherwise. If the animation is
7236 * started, this method calls {@link #invalidate()}; in that case the caller
7237 * should not call {@link #invalidate()}.
7238 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007239 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007240 * <p>
7241 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007242 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007243 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007244 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007245 * @param startDelay the delay, in milliseconds, after which the animation
7246 * should start; when the delay is 0, the animation starts
7247 * immediately
7248 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007249 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007250 * @see #scrollBy(int, int)
7251 * @see #scrollTo(int, int)
7252 * @see #isHorizontalScrollBarEnabled()
7253 * @see #isVerticalScrollBarEnabled()
7254 * @see #setHorizontalScrollBarEnabled(boolean)
7255 * @see #setVerticalScrollBarEnabled(boolean)
7256 */
7257 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007258 return awakenScrollBars(startDelay, true);
7259 }
Joe Malin32736f02011-01-19 16:14:20 -08007260
Mike Cleron290947b2009-09-29 18:34:32 -07007261 /**
7262 * <p>
7263 * Trigger the scrollbars to draw. When invoked this method starts an
7264 * animation to fade the scrollbars out after a fixed delay. If a subclass
7265 * provides animated scrolling, the start delay should equal the duration of
7266 * the scrolling animation.
7267 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007268 *
Mike Cleron290947b2009-09-29 18:34:32 -07007269 * <p>
7270 * The animation starts only if at least one of the scrollbars is enabled,
7271 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7272 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7273 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007274 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007275 * is set to true; in that case the caller
7276 * should not call {@link #invalidate()}.
7277 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007278 *
Mike Cleron290947b2009-09-29 18:34:32 -07007279 * <p>
7280 * This method should be invoked everytime a subclass directly updates the
7281 * scroll parameters.
7282 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007283 *
Mike Cleron290947b2009-09-29 18:34:32 -07007284 * @param startDelay the delay, in milliseconds, after which the animation
7285 * should start; when the delay is 0, the animation starts
7286 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007287 *
Mike Cleron290947b2009-09-29 18:34:32 -07007288 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007289 *
Mike Cleron290947b2009-09-29 18:34:32 -07007290 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007291 *
Mike Cleron290947b2009-09-29 18:34:32 -07007292 * @see #scrollBy(int, int)
7293 * @see #scrollTo(int, int)
7294 * @see #isHorizontalScrollBarEnabled()
7295 * @see #isVerticalScrollBarEnabled()
7296 * @see #setHorizontalScrollBarEnabled(boolean)
7297 * @see #setVerticalScrollBarEnabled(boolean)
7298 */
7299 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007300 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007301
Mike Cleronf116bf82009-09-27 19:14:12 -07007302 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7303 return false;
7304 }
7305
7306 if (scrollCache.scrollBar == null) {
7307 scrollCache.scrollBar = new ScrollBarDrawable();
7308 }
7309
7310 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7311
Mike Cleron290947b2009-09-29 18:34:32 -07007312 if (invalidate) {
7313 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007314 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007315 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007316
7317 if (scrollCache.state == ScrollabilityCache.OFF) {
7318 // FIXME: this is copied from WindowManagerService.
7319 // We should get this value from the system when it
7320 // is possible to do so.
7321 final int KEY_REPEAT_FIRST_DELAY = 750;
7322 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7323 }
7324
7325 // Tell mScrollCache when we should start fading. This may
7326 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007327 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007328 scrollCache.fadeStartTime = fadeStartTime;
7329 scrollCache.state = ScrollabilityCache.ON;
7330
7331 // Schedule our fader to run, unscheduling any old ones first
7332 if (mAttachInfo != null) {
7333 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7334 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7335 }
7336
7337 return true;
7338 }
7339
7340 return false;
7341 }
7342
7343 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007344 * Mark the the area defined by dirty as needing to be drawn. If the view is
7345 * visible, {@link #onDraw} will be called at some point in the future.
7346 * This must be called from a UI thread. To call from a non-UI thread, call
7347 * {@link #postInvalidate()}.
7348 *
7349 * WARNING: This method is destructive to dirty.
7350 * @param dirty the rectangle representing the bounds of the dirty region
7351 */
7352 public void invalidate(Rect dirty) {
7353 if (ViewDebug.TRACE_HIERARCHY) {
7354 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7355 }
7356
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007357 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007358 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7359 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007360 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007361 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007362 final ViewParent p = mParent;
7363 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007364 //noinspection PointlessBooleanExpression,ConstantConditions
7365 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7366 if (p != null && ai != null && ai.mHardwareAccelerated) {
7367 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007368 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007369 p.invalidateChild(this, null);
7370 return;
7371 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007372 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007373 if (p != null && ai != null) {
7374 final int scrollX = mScrollX;
7375 final int scrollY = mScrollY;
7376 final Rect r = ai.mTmpInvalRect;
7377 r.set(dirty.left - scrollX, dirty.top - scrollY,
7378 dirty.right - scrollX, dirty.bottom - scrollY);
7379 mParent.invalidateChild(this, r);
7380 }
7381 }
7382 }
7383
7384 /**
7385 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7386 * The coordinates of the dirty rect are relative to the view.
7387 * If the view is visible, {@link #onDraw} will be called at some point
7388 * in the future. This must be called from a UI thread. To call
7389 * from a non-UI thread, call {@link #postInvalidate()}.
7390 * @param l the left position of the dirty region
7391 * @param t the top position of the dirty region
7392 * @param r the right position of the dirty region
7393 * @param b the bottom position of the dirty region
7394 */
7395 public void invalidate(int l, int t, int r, int b) {
7396 if (ViewDebug.TRACE_HIERARCHY) {
7397 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7398 }
7399
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007400 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007401 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7402 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007403 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007404 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007405 final ViewParent p = mParent;
7406 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007407 //noinspection PointlessBooleanExpression,ConstantConditions
7408 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7409 if (p != null && ai != null && ai.mHardwareAccelerated) {
7410 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007411 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007412 p.invalidateChild(this, null);
7413 return;
7414 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007415 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007416 if (p != null && ai != null && l < r && t < b) {
7417 final int scrollX = mScrollX;
7418 final int scrollY = mScrollY;
7419 final Rect tmpr = ai.mTmpInvalRect;
7420 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7421 p.invalidateChild(this, tmpr);
7422 }
7423 }
7424 }
7425
7426 /**
7427 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7428 * be called at some point in the future. This must be called from a
7429 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7430 */
7431 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007432 invalidate(true);
7433 }
Joe Malin32736f02011-01-19 16:14:20 -08007434
Chet Haaseed032702010-10-01 14:05:54 -07007435 /**
7436 * This is where the invalidate() work actually happens. A full invalidate()
7437 * causes the drawing cache to be invalidated, but this function can be called with
7438 * invalidateCache set to false to skip that invalidation step for cases that do not
7439 * need it (for example, a component that remains at the same dimensions with the same
7440 * content).
7441 *
7442 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7443 * well. This is usually true for a full invalidate, but may be set to false if the
7444 * View's contents or dimensions have not changed.
7445 */
Romain Guy849d0a32011-02-01 17:20:48 -08007446 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007447 if (ViewDebug.TRACE_HIERARCHY) {
7448 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7449 }
7450
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007451 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007452 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007453 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7454 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007455 mPrivateFlags &= ~DRAWN;
7456 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007457 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007458 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7459 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007460 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007461 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007462 //noinspection PointlessBooleanExpression,ConstantConditions
7463 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7464 if (p != null && ai != null && ai.mHardwareAccelerated) {
7465 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007466 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007467 p.invalidateChild(this, null);
7468 return;
7469 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007470 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007471
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007472 if (p != null && ai != null) {
7473 final Rect r = ai.mTmpInvalRect;
7474 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7475 // Don't call invalidate -- we don't want to internally scroll
7476 // our own bounds
7477 p.invalidateChild(this, r);
7478 }
7479 }
7480 }
7481
7482 /**
Romain Guyda489792011-02-03 01:05:15 -08007483 * @hide
7484 */
7485 public void fastInvalidate() {
7486 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7487 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7488 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7489 if (mParent instanceof View) {
7490 ((View) mParent).mPrivateFlags |= INVALIDATED;
7491 }
7492 mPrivateFlags &= ~DRAWN;
7493 mPrivateFlags |= INVALIDATED;
7494 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007495 if (mParent != null && mAttachInfo != null) {
7496 if (mAttachInfo.mHardwareAccelerated) {
7497 mParent.invalidateChild(this, null);
7498 } else {
7499 final Rect r = mAttachInfo.mTmpInvalRect;
7500 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7501 // Don't call invalidate -- we don't want to internally scroll
7502 // our own bounds
7503 mParent.invalidateChild(this, r);
7504 }
Romain Guyda489792011-02-03 01:05:15 -08007505 }
7506 }
7507 }
7508
7509 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007510 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007511 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7512 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007513 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7514 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007515 *
7516 * @hide
7517 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007518 protected void invalidateParentCaches() {
7519 if (mParent instanceof View) {
7520 ((View) mParent).mPrivateFlags |= INVALIDATED;
7521 }
7522 }
Joe Malin32736f02011-01-19 16:14:20 -08007523
Romain Guy0fd89bf2011-01-26 15:41:30 -08007524 /**
7525 * Used to indicate that the parent of this view should be invalidated. This functionality
7526 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7527 * which is necessary when various parent-managed properties of the view change, such as
7528 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7529 * an invalidation event to the parent.
7530 *
7531 * @hide
7532 */
7533 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007534 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007535 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007536 }
7537 }
7538
7539 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007540 * Indicates whether this View is opaque. An opaque View guarantees that it will
7541 * draw all the pixels overlapping its bounds using a fully opaque color.
7542 *
7543 * Subclasses of View should override this method whenever possible to indicate
7544 * whether an instance is opaque. Opaque Views are treated in a special way by
7545 * the View hierarchy, possibly allowing it to perform optimizations during
7546 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007547 *
Romain Guy24443ea2009-05-11 11:56:30 -07007548 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007549 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007550 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007551 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007552 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7553 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007554 }
7555
Adam Powell20232d02010-12-08 21:08:53 -08007556 /**
7557 * @hide
7558 */
7559 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007560 // Opaque if:
7561 // - Has a background
7562 // - Background is opaque
7563 // - Doesn't have scrollbars or scrollbars are inside overlay
7564
7565 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7566 mPrivateFlags |= OPAQUE_BACKGROUND;
7567 } else {
7568 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7569 }
7570
7571 final int flags = mViewFlags;
7572 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7573 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7574 mPrivateFlags |= OPAQUE_SCROLLBARS;
7575 } else {
7576 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7577 }
7578 }
7579
7580 /**
7581 * @hide
7582 */
7583 protected boolean hasOpaqueScrollbars() {
7584 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007585 }
7586
7587 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007588 * @return A handler associated with the thread running the View. This
7589 * handler can be used to pump events in the UI events queue.
7590 */
7591 public Handler getHandler() {
7592 if (mAttachInfo != null) {
7593 return mAttachInfo.mHandler;
7594 }
7595 return null;
7596 }
7597
7598 /**
7599 * Causes the Runnable to be added to the message queue.
7600 * The runnable will be run on the user interface thread.
7601 *
7602 * @param action The Runnable that will be executed.
7603 *
7604 * @return Returns true if the Runnable was successfully placed in to the
7605 * message queue. Returns false on failure, usually because the
7606 * looper processing the message queue is exiting.
7607 */
7608 public boolean post(Runnable action) {
7609 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007610 AttachInfo attachInfo = mAttachInfo;
7611 if (attachInfo != null) {
7612 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007613 } else {
7614 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007615 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007616 return true;
7617 }
7618
7619 return handler.post(action);
7620 }
7621
7622 /**
7623 * Causes the Runnable to be added to the message queue, to be run
7624 * after the specified amount of time elapses.
7625 * The runnable will be run on the user interface thread.
7626 *
7627 * @param action The Runnable that will be executed.
7628 * @param delayMillis The delay (in milliseconds) until the Runnable
7629 * will be executed.
7630 *
7631 * @return true if the Runnable was successfully placed in to the
7632 * message queue. Returns false on failure, usually because the
7633 * looper processing the message queue is exiting. Note that a
7634 * result of true does not mean the Runnable will be processed --
7635 * if the looper is quit before the delivery time of the message
7636 * occurs then the message will be dropped.
7637 */
7638 public boolean postDelayed(Runnable action, long delayMillis) {
7639 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007640 AttachInfo attachInfo = mAttachInfo;
7641 if (attachInfo != null) {
7642 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007643 } else {
7644 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007645 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007646 return true;
7647 }
7648
7649 return handler.postDelayed(action, delayMillis);
7650 }
7651
7652 /**
7653 * Removes the specified Runnable from the message queue.
7654 *
7655 * @param action The Runnable to remove from the message handling queue
7656 *
7657 * @return true if this view could ask the Handler to remove the Runnable,
7658 * false otherwise. When the returned value is true, the Runnable
7659 * may or may not have been actually removed from the message queue
7660 * (for instance, if the Runnable was not in the queue already.)
7661 */
7662 public boolean removeCallbacks(Runnable action) {
7663 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007664 AttachInfo attachInfo = mAttachInfo;
7665 if (attachInfo != null) {
7666 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007667 } else {
7668 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007669 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007670 return true;
7671 }
7672
7673 handler.removeCallbacks(action);
7674 return true;
7675 }
7676
7677 /**
7678 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7679 * Use this to invalidate the View from a non-UI thread.
7680 *
7681 * @see #invalidate()
7682 */
7683 public void postInvalidate() {
7684 postInvalidateDelayed(0);
7685 }
7686
7687 /**
7688 * Cause an invalidate of the specified area to happen on a subsequent cycle
7689 * through the event loop. Use this to invalidate the View from a non-UI thread.
7690 *
7691 * @param left The left coordinate of the rectangle to invalidate.
7692 * @param top The top coordinate of the rectangle to invalidate.
7693 * @param right The right coordinate of the rectangle to invalidate.
7694 * @param bottom The bottom coordinate of the rectangle to invalidate.
7695 *
7696 * @see #invalidate(int, int, int, int)
7697 * @see #invalidate(Rect)
7698 */
7699 public void postInvalidate(int left, int top, int right, int bottom) {
7700 postInvalidateDelayed(0, left, top, right, bottom);
7701 }
7702
7703 /**
7704 * Cause an invalidate to happen on a subsequent cycle through the event
7705 * loop. Waits for the specified amount of time.
7706 *
7707 * @param delayMilliseconds the duration in milliseconds to delay the
7708 * invalidation by
7709 */
7710 public void postInvalidateDelayed(long delayMilliseconds) {
7711 // We try only with the AttachInfo because there's no point in invalidating
7712 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007713 AttachInfo attachInfo = mAttachInfo;
7714 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 Message msg = Message.obtain();
7716 msg.what = AttachInfo.INVALIDATE_MSG;
7717 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007718 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007719 }
7720 }
7721
7722 /**
7723 * Cause an invalidate of the specified area to happen on a subsequent cycle
7724 * through the event loop. Waits for the specified amount of time.
7725 *
7726 * @param delayMilliseconds the duration in milliseconds to delay the
7727 * invalidation by
7728 * @param left The left coordinate of the rectangle to invalidate.
7729 * @param top The top coordinate of the rectangle to invalidate.
7730 * @param right The right coordinate of the rectangle to invalidate.
7731 * @param bottom The bottom coordinate of the rectangle to invalidate.
7732 */
7733 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7734 int right, int bottom) {
7735
7736 // We try only with the AttachInfo because there's no point in invalidating
7737 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007738 AttachInfo attachInfo = mAttachInfo;
7739 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007740 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7741 info.target = this;
7742 info.left = left;
7743 info.top = top;
7744 info.right = right;
7745 info.bottom = bottom;
7746
7747 final Message msg = Message.obtain();
7748 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7749 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007750 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007751 }
7752 }
7753
7754 /**
7755 * Called by a parent to request that a child update its values for mScrollX
7756 * and mScrollY if necessary. This will typically be done if the child is
7757 * animating a scroll using a {@link android.widget.Scroller Scroller}
7758 * object.
7759 */
7760 public void computeScroll() {
7761 }
7762
7763 /**
7764 * <p>Indicate whether the horizontal edges are faded when the view is
7765 * scrolled horizontally.</p>
7766 *
7767 * @return true if the horizontal edges should are faded on scroll, false
7768 * otherwise
7769 *
7770 * @see #setHorizontalFadingEdgeEnabled(boolean)
7771 * @attr ref android.R.styleable#View_fadingEdge
7772 */
7773 public boolean isHorizontalFadingEdgeEnabled() {
7774 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7775 }
7776
7777 /**
7778 * <p>Define whether the horizontal edges should be faded when this view
7779 * is scrolled horizontally.</p>
7780 *
7781 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7782 * be faded when the view is scrolled
7783 * horizontally
7784 *
7785 * @see #isHorizontalFadingEdgeEnabled()
7786 * @attr ref android.R.styleable#View_fadingEdge
7787 */
7788 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7789 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7790 if (horizontalFadingEdgeEnabled) {
7791 initScrollCache();
7792 }
7793
7794 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7795 }
7796 }
7797
7798 /**
7799 * <p>Indicate whether the vertical edges are faded when the view is
7800 * scrolled horizontally.</p>
7801 *
7802 * @return true if the vertical edges should are faded on scroll, false
7803 * otherwise
7804 *
7805 * @see #setVerticalFadingEdgeEnabled(boolean)
7806 * @attr ref android.R.styleable#View_fadingEdge
7807 */
7808 public boolean isVerticalFadingEdgeEnabled() {
7809 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7810 }
7811
7812 /**
7813 * <p>Define whether the vertical edges should be faded when this view
7814 * is scrolled vertically.</p>
7815 *
7816 * @param verticalFadingEdgeEnabled true if the vertical edges should
7817 * be faded when the view is scrolled
7818 * vertically
7819 *
7820 * @see #isVerticalFadingEdgeEnabled()
7821 * @attr ref android.R.styleable#View_fadingEdge
7822 */
7823 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7824 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7825 if (verticalFadingEdgeEnabled) {
7826 initScrollCache();
7827 }
7828
7829 mViewFlags ^= FADING_EDGE_VERTICAL;
7830 }
7831 }
7832
7833 /**
7834 * Returns the strength, or intensity, of the top faded edge. The strength is
7835 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7836 * returns 0.0 or 1.0 but no value in between.
7837 *
7838 * Subclasses should override this method to provide a smoother fade transition
7839 * when scrolling occurs.
7840 *
7841 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7842 */
7843 protected float getTopFadingEdgeStrength() {
7844 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7845 }
7846
7847 /**
7848 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7849 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7850 * returns 0.0 or 1.0 but no value in between.
7851 *
7852 * Subclasses should override this method to provide a smoother fade transition
7853 * when scrolling occurs.
7854 *
7855 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7856 */
7857 protected float getBottomFadingEdgeStrength() {
7858 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7859 computeVerticalScrollRange() ? 1.0f : 0.0f;
7860 }
7861
7862 /**
7863 * Returns the strength, or intensity, of the left faded edge. The strength is
7864 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7865 * returns 0.0 or 1.0 but no value in between.
7866 *
7867 * Subclasses should override this method to provide a smoother fade transition
7868 * when scrolling occurs.
7869 *
7870 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7871 */
7872 protected float getLeftFadingEdgeStrength() {
7873 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7874 }
7875
7876 /**
7877 * Returns the strength, or intensity, of the right faded edge. The strength is
7878 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7879 * returns 0.0 or 1.0 but no value in between.
7880 *
7881 * Subclasses should override this method to provide a smoother fade transition
7882 * when scrolling occurs.
7883 *
7884 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7885 */
7886 protected float getRightFadingEdgeStrength() {
7887 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7888 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7889 }
7890
7891 /**
7892 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7893 * scrollbar is not drawn by default.</p>
7894 *
7895 * @return true if the horizontal scrollbar should be painted, false
7896 * otherwise
7897 *
7898 * @see #setHorizontalScrollBarEnabled(boolean)
7899 */
7900 public boolean isHorizontalScrollBarEnabled() {
7901 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7902 }
7903
7904 /**
7905 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7906 * scrollbar is not drawn by default.</p>
7907 *
7908 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7909 * be painted
7910 *
7911 * @see #isHorizontalScrollBarEnabled()
7912 */
7913 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7914 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7915 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007916 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007917 recomputePadding();
7918 }
7919 }
7920
7921 /**
7922 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7923 * scrollbar is not drawn by default.</p>
7924 *
7925 * @return true if the vertical scrollbar should be painted, false
7926 * otherwise
7927 *
7928 * @see #setVerticalScrollBarEnabled(boolean)
7929 */
7930 public boolean isVerticalScrollBarEnabled() {
7931 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7932 }
7933
7934 /**
7935 * <p>Define whether the vertical scrollbar should be drawn or not. The
7936 * scrollbar is not drawn by default.</p>
7937 *
7938 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7939 * be painted
7940 *
7941 * @see #isVerticalScrollBarEnabled()
7942 */
7943 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7944 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7945 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007946 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007947 recomputePadding();
7948 }
7949 }
7950
Adam Powell20232d02010-12-08 21:08:53 -08007951 /**
7952 * @hide
7953 */
7954 protected void recomputePadding() {
7955 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007956 }
Joe Malin32736f02011-01-19 16:14:20 -08007957
Mike Cleronfe81d382009-09-28 14:22:16 -07007958 /**
7959 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007960 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007961 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007962 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007963 */
7964 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7965 initScrollCache();
7966 final ScrollabilityCache scrollabilityCache = mScrollCache;
7967 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007968 if (fadeScrollbars) {
7969 scrollabilityCache.state = ScrollabilityCache.OFF;
7970 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007971 scrollabilityCache.state = ScrollabilityCache.ON;
7972 }
7973 }
Joe Malin32736f02011-01-19 16:14:20 -08007974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975 /**
Joe Malin32736f02011-01-19 16:14:20 -08007976 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007977 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007978 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007979 * @return true if scrollbar fading is enabled
7980 */
7981 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007982 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007983 }
Joe Malin32736f02011-01-19 16:14:20 -08007984
Mike Cleron52f0a642009-09-28 18:21:37 -07007985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7987 * inset. When inset, they add to the padding of the view. And the scrollbars
7988 * can be drawn inside the padding area or on the edge of the view. For example,
7989 * if a view has a background drawable and you want to draw the scrollbars
7990 * inside the padding specified by the drawable, you can use
7991 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7992 * appear at the edge of the view, ignoring the padding, then you can use
7993 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7994 * @param style the style of the scrollbars. Should be one of
7995 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7996 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7997 * @see #SCROLLBARS_INSIDE_OVERLAY
7998 * @see #SCROLLBARS_INSIDE_INSET
7999 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8000 * @see #SCROLLBARS_OUTSIDE_INSET
8001 */
8002 public void setScrollBarStyle(int style) {
8003 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8004 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008005 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 recomputePadding();
8007 }
8008 }
8009
8010 /**
8011 * <p>Returns the current scrollbar style.</p>
8012 * @return the current scrollbar style
8013 * @see #SCROLLBARS_INSIDE_OVERLAY
8014 * @see #SCROLLBARS_INSIDE_INSET
8015 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8016 * @see #SCROLLBARS_OUTSIDE_INSET
8017 */
8018 public int getScrollBarStyle() {
8019 return mViewFlags & SCROLLBARS_STYLE_MASK;
8020 }
8021
8022 /**
8023 * <p>Compute the horizontal range that the horizontal scrollbar
8024 * represents.</p>
8025 *
8026 * <p>The range is expressed in arbitrary units that must be the same as the
8027 * units used by {@link #computeHorizontalScrollExtent()} and
8028 * {@link #computeHorizontalScrollOffset()}.</p>
8029 *
8030 * <p>The default range is the drawing width of this view.</p>
8031 *
8032 * @return the total horizontal range represented by the horizontal
8033 * scrollbar
8034 *
8035 * @see #computeHorizontalScrollExtent()
8036 * @see #computeHorizontalScrollOffset()
8037 * @see android.widget.ScrollBarDrawable
8038 */
8039 protected int computeHorizontalScrollRange() {
8040 return getWidth();
8041 }
8042
8043 /**
8044 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8045 * within the horizontal range. This value is used to compute the position
8046 * of the thumb within the scrollbar's track.</p>
8047 *
8048 * <p>The range is expressed in arbitrary units that must be the same as the
8049 * units used by {@link #computeHorizontalScrollRange()} and
8050 * {@link #computeHorizontalScrollExtent()}.</p>
8051 *
8052 * <p>The default offset is the scroll offset of this view.</p>
8053 *
8054 * @return the horizontal offset of the scrollbar's thumb
8055 *
8056 * @see #computeHorizontalScrollRange()
8057 * @see #computeHorizontalScrollExtent()
8058 * @see android.widget.ScrollBarDrawable
8059 */
8060 protected int computeHorizontalScrollOffset() {
8061 return mScrollX;
8062 }
8063
8064 /**
8065 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8066 * within the horizontal range. This value is used to compute the length
8067 * of the thumb within the scrollbar's track.</p>
8068 *
8069 * <p>The range is expressed in arbitrary units that must be the same as the
8070 * units used by {@link #computeHorizontalScrollRange()} and
8071 * {@link #computeHorizontalScrollOffset()}.</p>
8072 *
8073 * <p>The default extent is the drawing width of this view.</p>
8074 *
8075 * @return the horizontal extent of the scrollbar's thumb
8076 *
8077 * @see #computeHorizontalScrollRange()
8078 * @see #computeHorizontalScrollOffset()
8079 * @see android.widget.ScrollBarDrawable
8080 */
8081 protected int computeHorizontalScrollExtent() {
8082 return getWidth();
8083 }
8084
8085 /**
8086 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8087 *
8088 * <p>The range is expressed in arbitrary units that must be the same as the
8089 * units used by {@link #computeVerticalScrollExtent()} and
8090 * {@link #computeVerticalScrollOffset()}.</p>
8091 *
8092 * @return the total vertical range represented by the vertical scrollbar
8093 *
8094 * <p>The default range is the drawing height of this view.</p>
8095 *
8096 * @see #computeVerticalScrollExtent()
8097 * @see #computeVerticalScrollOffset()
8098 * @see android.widget.ScrollBarDrawable
8099 */
8100 protected int computeVerticalScrollRange() {
8101 return getHeight();
8102 }
8103
8104 /**
8105 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8106 * within the horizontal range. This value is used to compute the position
8107 * of the thumb within the scrollbar's track.</p>
8108 *
8109 * <p>The range is expressed in arbitrary units that must be the same as the
8110 * units used by {@link #computeVerticalScrollRange()} and
8111 * {@link #computeVerticalScrollExtent()}.</p>
8112 *
8113 * <p>The default offset is the scroll offset of this view.</p>
8114 *
8115 * @return the vertical offset of the scrollbar's thumb
8116 *
8117 * @see #computeVerticalScrollRange()
8118 * @see #computeVerticalScrollExtent()
8119 * @see android.widget.ScrollBarDrawable
8120 */
8121 protected int computeVerticalScrollOffset() {
8122 return mScrollY;
8123 }
8124
8125 /**
8126 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8127 * within the vertical range. This value is used to compute the length
8128 * of the thumb within the scrollbar's track.</p>
8129 *
8130 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008131 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008132 * {@link #computeVerticalScrollOffset()}.</p>
8133 *
8134 * <p>The default extent is the drawing height of this view.</p>
8135 *
8136 * @return the vertical extent of the scrollbar's thumb
8137 *
8138 * @see #computeVerticalScrollRange()
8139 * @see #computeVerticalScrollOffset()
8140 * @see android.widget.ScrollBarDrawable
8141 */
8142 protected int computeVerticalScrollExtent() {
8143 return getHeight();
8144 }
8145
8146 /**
8147 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8148 * scrollbars are painted only if they have been awakened first.</p>
8149 *
8150 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008151 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008152 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008153 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008154 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 // scrollbars are drawn only when the animation is running
8156 final ScrollabilityCache cache = mScrollCache;
8157 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008158
Mike Cleronf116bf82009-09-27 19:14:12 -07008159 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008160
Mike Cleronf116bf82009-09-27 19:14:12 -07008161 if (state == ScrollabilityCache.OFF) {
8162 return;
8163 }
Joe Malin32736f02011-01-19 16:14:20 -08008164
Mike Cleronf116bf82009-09-27 19:14:12 -07008165 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008166
Mike Cleronf116bf82009-09-27 19:14:12 -07008167 if (state == ScrollabilityCache.FADING) {
8168 // We're fading -- get our fade interpolation
8169 if (cache.interpolatorValues == null) {
8170 cache.interpolatorValues = new float[1];
8171 }
Joe Malin32736f02011-01-19 16:14:20 -08008172
Mike Cleronf116bf82009-09-27 19:14:12 -07008173 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008174
Mike Cleronf116bf82009-09-27 19:14:12 -07008175 // Stops the animation if we're done
8176 if (cache.scrollBarInterpolator.timeToValues(values) ==
8177 Interpolator.Result.FREEZE_END) {
8178 cache.state = ScrollabilityCache.OFF;
8179 } else {
8180 cache.scrollBar.setAlpha(Math.round(values[0]));
8181 }
Joe Malin32736f02011-01-19 16:14:20 -08008182
8183 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008184 // drawing. We only want this when we're fading so that
8185 // we prevent excessive redraws
8186 invalidate = true;
8187 } else {
8188 // We're just on -- but we may have been fading before so
8189 // reset alpha
8190 cache.scrollBar.setAlpha(255);
8191 }
8192
Joe Malin32736f02011-01-19 16:14:20 -08008193
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008194 final int viewFlags = mViewFlags;
8195
8196 final boolean drawHorizontalScrollBar =
8197 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8198 final boolean drawVerticalScrollBar =
8199 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8200 && !isVerticalScrollBarHidden();
8201
8202 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8203 final int width = mRight - mLeft;
8204 final int height = mBottom - mTop;
8205
8206 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207
Mike Reede8853fc2009-09-04 14:01:48 -04008208 final int scrollX = mScrollX;
8209 final int scrollY = mScrollY;
8210 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8211
Mike Cleronf116bf82009-09-27 19:14:12 -07008212 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008213
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008214 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008215 int size = scrollBar.getSize(false);
8216 if (size <= 0) {
8217 size = cache.scrollBarSize;
8218 }
8219
Mike Cleronf116bf82009-09-27 19:14:12 -07008220 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008221 computeHorizontalScrollOffset(),
8222 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008223 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008224 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008225 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008226 left = scrollX + (mPaddingLeft & inside);
8227 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8228 bottom = top + size;
8229 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8230 if (invalidate) {
8231 invalidate(left, top, right, bottom);
8232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 }
8234
8235 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008236 int size = scrollBar.getSize(true);
8237 if (size <= 0) {
8238 size = cache.scrollBarSize;
8239 }
8240
Mike Reede8853fc2009-09-04 14:01:48 -04008241 scrollBar.setParameters(computeVerticalScrollRange(),
8242 computeVerticalScrollOffset(),
8243 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008244 switch (mVerticalScrollbarPosition) {
8245 default:
8246 case SCROLLBAR_POSITION_DEFAULT:
8247 case SCROLLBAR_POSITION_RIGHT:
8248 left = scrollX + width - size - (mUserPaddingRight & inside);
8249 break;
8250 case SCROLLBAR_POSITION_LEFT:
8251 left = scrollX + (mUserPaddingLeft & inside);
8252 break;
8253 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008254 top = scrollY + (mPaddingTop & inside);
8255 right = left + size;
8256 bottom = scrollY + height - (mUserPaddingBottom & inside);
8257 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8258 if (invalidate) {
8259 invalidate(left, top, right, bottom);
8260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 }
8262 }
8263 }
8264 }
Romain Guy8506ab42009-06-11 17:35:47 -07008265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008266 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008267 * 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 -08008268 * FastScroller is visible.
8269 * @return whether to temporarily hide the vertical scrollbar
8270 * @hide
8271 */
8272 protected boolean isVerticalScrollBarHidden() {
8273 return false;
8274 }
8275
8276 /**
8277 * <p>Draw the horizontal scrollbar if
8278 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8279 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008280 * @param canvas the canvas on which to draw the scrollbar
8281 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 *
8283 * @see #isHorizontalScrollBarEnabled()
8284 * @see #computeHorizontalScrollRange()
8285 * @see #computeHorizontalScrollExtent()
8286 * @see #computeHorizontalScrollOffset()
8287 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008288 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008289 */
Romain Guy8fb95422010-08-17 18:38:51 -07008290 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8291 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008292 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008293 scrollBar.draw(canvas);
8294 }
Mike Reede8853fc2009-09-04 14:01:48 -04008295
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008297 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8298 * returns true.</p>
8299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008300 * @param canvas the canvas on which to draw the scrollbar
8301 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008302 *
8303 * @see #isVerticalScrollBarEnabled()
8304 * @see #computeVerticalScrollRange()
8305 * @see #computeVerticalScrollExtent()
8306 * @see #computeVerticalScrollOffset()
8307 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008308 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 */
Romain Guy8fb95422010-08-17 18:38:51 -07008310 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8311 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008312 scrollBar.setBounds(l, t, r, b);
8313 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 }
8315
8316 /**
8317 * Implement this to do your drawing.
8318 *
8319 * @param canvas the canvas on which the background will be drawn
8320 */
8321 protected void onDraw(Canvas canvas) {
8322 }
8323
8324 /*
8325 * Caller is responsible for calling requestLayout if necessary.
8326 * (This allows addViewInLayout to not request a new layout.)
8327 */
8328 void assignParent(ViewParent parent) {
8329 if (mParent == null) {
8330 mParent = parent;
8331 } else if (parent == null) {
8332 mParent = null;
8333 } else {
8334 throw new RuntimeException("view " + this + " being added, but"
8335 + " it already has a parent");
8336 }
8337 }
8338
8339 /**
8340 * This is called when the view is attached to a window. At this point it
8341 * has a Surface and will start drawing. Note that this function is
8342 * guaranteed to be called before {@link #onDraw}, however it may be called
8343 * any time before the first onDraw -- including before or after
8344 * {@link #onMeasure}.
8345 *
8346 * @see #onDetachedFromWindow()
8347 */
8348 protected void onAttachedToWindow() {
8349 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8350 mParent.requestTransparentRegion(this);
8351 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008352 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8353 initialAwakenScrollBars();
8354 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8355 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008356 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 }
8358
8359 /**
8360 * This is called when the view is detached from a window. At this point it
8361 * no longer has a surface for drawing.
8362 *
8363 * @see #onAttachedToWindow()
8364 */
8365 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008366 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008367
Romain Guya440b002010-02-24 15:57:54 -08008368 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008369 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008370 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008372 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008373
8374 if (mHardwareLayer != null) {
8375 mHardwareLayer.destroy();
8376 mHardwareLayer = null;
8377 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008378
Chet Haasedaf98e92011-01-10 14:10:36 -08008379 if (mDisplayList != null) {
8380 mDisplayList.invalidate();
8381 }
8382
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008383 if (mAttachInfo != null) {
8384 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8385 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8386 }
8387
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008388 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008389 }
8390
8391 /**
8392 * @return The number of times this view has been attached to a window
8393 */
8394 protected int getWindowAttachCount() {
8395 return mWindowAttachCount;
8396 }
8397
8398 /**
8399 * Retrieve a unique token identifying the window this view is attached to.
8400 * @return Return the window's token for use in
8401 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8402 */
8403 public IBinder getWindowToken() {
8404 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8405 }
8406
8407 /**
8408 * Retrieve a unique token identifying the top-level "real" window of
8409 * the window that this view is attached to. That is, this is like
8410 * {@link #getWindowToken}, except if the window this view in is a panel
8411 * window (attached to another containing window), then the token of
8412 * the containing window is returned instead.
8413 *
8414 * @return Returns the associated window token, either
8415 * {@link #getWindowToken()} or the containing window's token.
8416 */
8417 public IBinder getApplicationWindowToken() {
8418 AttachInfo ai = mAttachInfo;
8419 if (ai != null) {
8420 IBinder appWindowToken = ai.mPanelParentWindowToken;
8421 if (appWindowToken == null) {
8422 appWindowToken = ai.mWindowToken;
8423 }
8424 return appWindowToken;
8425 }
8426 return null;
8427 }
8428
8429 /**
8430 * Retrieve private session object this view hierarchy is using to
8431 * communicate with the window manager.
8432 * @return the session object to communicate with the window manager
8433 */
8434 /*package*/ IWindowSession getWindowSession() {
8435 return mAttachInfo != null ? mAttachInfo.mSession : null;
8436 }
8437
8438 /**
8439 * @param info the {@link android.view.View.AttachInfo} to associated with
8440 * this view
8441 */
8442 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8443 //System.out.println("Attached! " + this);
8444 mAttachInfo = info;
8445 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008446 // We will need to evaluate the drawable state at least once.
8447 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 if (mFloatingTreeObserver != null) {
8449 info.mTreeObserver.merge(mFloatingTreeObserver);
8450 mFloatingTreeObserver = null;
8451 }
8452 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8453 mAttachInfo.mScrollContainers.add(this);
8454 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8455 }
8456 performCollectViewAttributes(visibility);
8457 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008458
8459 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8460 mOnAttachStateChangeListeners;
8461 if (listeners != null && listeners.size() > 0) {
8462 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8463 // perform the dispatching. The iterator is a safe guard against listeners that
8464 // could mutate the list by calling the various add/remove methods. This prevents
8465 // the array from being modified while we iterate it.
8466 for (OnAttachStateChangeListener listener : listeners) {
8467 listener.onViewAttachedToWindow(this);
8468 }
8469 }
8470
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008471 int vis = info.mWindowVisibility;
8472 if (vis != GONE) {
8473 onWindowVisibilityChanged(vis);
8474 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008475 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8476 // If nobody has evaluated the drawable state yet, then do it now.
8477 refreshDrawableState();
8478 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008479 }
8480
8481 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008482 AttachInfo info = mAttachInfo;
8483 if (info != null) {
8484 int vis = info.mWindowVisibility;
8485 if (vis != GONE) {
8486 onWindowVisibilityChanged(GONE);
8487 }
8488 }
8489
8490 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008491
Adam Powell4afd62b2011-02-18 15:02:18 -08008492 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8493 mOnAttachStateChangeListeners;
8494 if (listeners != null && listeners.size() > 0) {
8495 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8496 // perform the dispatching. The iterator is a safe guard against listeners that
8497 // could mutate the list by calling the various add/remove methods. This prevents
8498 // the array from being modified while we iterate it.
8499 for (OnAttachStateChangeListener listener : listeners) {
8500 listener.onViewDetachedFromWindow(this);
8501 }
8502 }
8503
Romain Guy01d5edc2011-01-28 11:28:53 -08008504 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 mAttachInfo.mScrollContainers.remove(this);
8506 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8507 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008509 mAttachInfo = null;
8510 }
8511
8512 /**
8513 * Store this view hierarchy's frozen state into the given container.
8514 *
8515 * @param container The SparseArray in which to save the view's state.
8516 *
8517 * @see #restoreHierarchyState
8518 * @see #dispatchSaveInstanceState
8519 * @see #onSaveInstanceState
8520 */
8521 public void saveHierarchyState(SparseArray<Parcelable> container) {
8522 dispatchSaveInstanceState(container);
8523 }
8524
8525 /**
8526 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8527 * May be overridden to modify how freezing happens to a view's children; for example, some
8528 * views may want to not store state for their children.
8529 *
8530 * @param container The SparseArray in which to save the view's state.
8531 *
8532 * @see #dispatchRestoreInstanceState
8533 * @see #saveHierarchyState
8534 * @see #onSaveInstanceState
8535 */
8536 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8537 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8538 mPrivateFlags &= ~SAVE_STATE_CALLED;
8539 Parcelable state = onSaveInstanceState();
8540 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8541 throw new IllegalStateException(
8542 "Derived class did not call super.onSaveInstanceState()");
8543 }
8544 if (state != null) {
8545 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8546 // + ": " + state);
8547 container.put(mID, state);
8548 }
8549 }
8550 }
8551
8552 /**
8553 * Hook allowing a view to generate a representation of its internal state
8554 * that can later be used to create a new instance with that same state.
8555 * This state should only contain information that is not persistent or can
8556 * not be reconstructed later. For example, you will never store your
8557 * current position on screen because that will be computed again when a
8558 * new instance of the view is placed in its view hierarchy.
8559 * <p>
8560 * Some examples of things you may store here: the current cursor position
8561 * in a text view (but usually not the text itself since that is stored in a
8562 * content provider or other persistent storage), the currently selected
8563 * item in a list view.
8564 *
8565 * @return Returns a Parcelable object containing the view's current dynamic
8566 * state, or null if there is nothing interesting to save. The
8567 * default implementation returns null.
8568 * @see #onRestoreInstanceState
8569 * @see #saveHierarchyState
8570 * @see #dispatchSaveInstanceState
8571 * @see #setSaveEnabled(boolean)
8572 */
8573 protected Parcelable onSaveInstanceState() {
8574 mPrivateFlags |= SAVE_STATE_CALLED;
8575 return BaseSavedState.EMPTY_STATE;
8576 }
8577
8578 /**
8579 * Restore this view hierarchy's frozen state from the given container.
8580 *
8581 * @param container The SparseArray which holds previously frozen states.
8582 *
8583 * @see #saveHierarchyState
8584 * @see #dispatchRestoreInstanceState
8585 * @see #onRestoreInstanceState
8586 */
8587 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8588 dispatchRestoreInstanceState(container);
8589 }
8590
8591 /**
8592 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8593 * children. May be overridden to modify how restoreing happens to a view's children; for
8594 * example, some views may want to not store state for their children.
8595 *
8596 * @param container The SparseArray which holds previously saved state.
8597 *
8598 * @see #dispatchSaveInstanceState
8599 * @see #restoreHierarchyState
8600 * @see #onRestoreInstanceState
8601 */
8602 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8603 if (mID != NO_ID) {
8604 Parcelable state = container.get(mID);
8605 if (state != null) {
8606 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8607 // + ": " + state);
8608 mPrivateFlags &= ~SAVE_STATE_CALLED;
8609 onRestoreInstanceState(state);
8610 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8611 throw new IllegalStateException(
8612 "Derived class did not call super.onRestoreInstanceState()");
8613 }
8614 }
8615 }
8616 }
8617
8618 /**
8619 * Hook allowing a view to re-apply a representation of its internal state that had previously
8620 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8621 * null state.
8622 *
8623 * @param state The frozen state that had previously been returned by
8624 * {@link #onSaveInstanceState}.
8625 *
8626 * @see #onSaveInstanceState
8627 * @see #restoreHierarchyState
8628 * @see #dispatchRestoreInstanceState
8629 */
8630 protected void onRestoreInstanceState(Parcelable state) {
8631 mPrivateFlags |= SAVE_STATE_CALLED;
8632 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008633 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8634 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008635 + "when two views of different type have the same id in the same hierarchy. "
8636 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008637 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008638 }
8639 }
8640
8641 /**
8642 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8643 *
8644 * @return the drawing start time in milliseconds
8645 */
8646 public long getDrawingTime() {
8647 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8648 }
8649
8650 /**
8651 * <p>Enables or disables the duplication of the parent's state into this view. When
8652 * duplication is enabled, this view gets its drawable state from its parent rather
8653 * than from its own internal properties.</p>
8654 *
8655 * <p>Note: in the current implementation, setting this property to true after the
8656 * view was added to a ViewGroup might have no effect at all. This property should
8657 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8658 *
8659 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8660 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008661 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008662 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8663 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008664 *
8665 * @param enabled True to enable duplication of the parent's drawable state, false
8666 * to disable it.
8667 *
8668 * @see #getDrawableState()
8669 * @see #isDuplicateParentStateEnabled()
8670 */
8671 public void setDuplicateParentStateEnabled(boolean enabled) {
8672 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8673 }
8674
8675 /**
8676 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8677 *
8678 * @return True if this view's drawable state is duplicated from the parent,
8679 * false otherwise
8680 *
8681 * @see #getDrawableState()
8682 * @see #setDuplicateParentStateEnabled(boolean)
8683 */
8684 public boolean isDuplicateParentStateEnabled() {
8685 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8686 }
8687
8688 /**
Romain Guy171c5922011-01-06 10:04:23 -08008689 * <p>Specifies the type of layer backing this view. The layer can be
8690 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8691 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008692 *
Romain Guy171c5922011-01-06 10:04:23 -08008693 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8694 * instance that controls how the layer is composed on screen. The following
8695 * properties of the paint are taken into account when composing the layer:</p>
8696 * <ul>
8697 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8698 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8699 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8700 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008701 *
Romain Guy171c5922011-01-06 10:04:23 -08008702 * <p>If this view has an alpha value set to < 1.0 by calling
8703 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8704 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8705 * equivalent to setting a hardware layer on this view and providing a paint with
8706 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008707 *
Romain Guy171c5922011-01-06 10:04:23 -08008708 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8709 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8710 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008711 *
Romain Guy171c5922011-01-06 10:04:23 -08008712 * @param layerType The ype of layer to use with this view, must be one of
8713 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8714 * {@link #LAYER_TYPE_HARDWARE}
8715 * @param paint The paint used to compose the layer. This argument is optional
8716 * and can be null. It is ignored when the layer type is
8717 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008718 *
8719 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008720 * @see #LAYER_TYPE_NONE
8721 * @see #LAYER_TYPE_SOFTWARE
8722 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008723 * @see #setAlpha(float)
8724 *
Romain Guy171c5922011-01-06 10:04:23 -08008725 * @attr ref android.R.styleable#View_layerType
8726 */
8727 public void setLayerType(int layerType, Paint paint) {
8728 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008729 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008730 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8731 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008732
Romain Guyd6cd5722011-01-17 14:42:41 -08008733 if (layerType == mLayerType) {
8734 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8735 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008736 invalidateParentCaches();
8737 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008738 }
8739 return;
8740 }
Romain Guy171c5922011-01-06 10:04:23 -08008741
8742 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008743 switch (mLayerType) {
8744 case LAYER_TYPE_SOFTWARE:
8745 if (mDrawingCache != null) {
8746 mDrawingCache.recycle();
8747 mDrawingCache = null;
8748 }
Joe Malin32736f02011-01-19 16:14:20 -08008749
Romain Guy6c319ca2011-01-11 14:29:25 -08008750 if (mUnscaledDrawingCache != null) {
8751 mUnscaledDrawingCache.recycle();
8752 mUnscaledDrawingCache = null;
8753 }
8754 break;
8755 case LAYER_TYPE_HARDWARE:
8756 if (mHardwareLayer != null) {
8757 mHardwareLayer.destroy();
8758 mHardwareLayer = null;
8759 }
8760 break;
8761 default:
8762 break;
Romain Guy171c5922011-01-06 10:04:23 -08008763 }
8764
8765 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008766 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8767 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8768 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008769
Romain Guy0fd89bf2011-01-26 15:41:30 -08008770 invalidateParentCaches();
8771 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008772 }
8773
8774 /**
8775 * Indicates what type of layer is currently associated with this view. By default
8776 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8777 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8778 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008779 *
Romain Guy171c5922011-01-06 10:04:23 -08008780 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8781 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008782 *
8783 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008784 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008785 * @see #LAYER_TYPE_NONE
8786 * @see #LAYER_TYPE_SOFTWARE
8787 * @see #LAYER_TYPE_HARDWARE
8788 */
8789 public int getLayerType() {
8790 return mLayerType;
8791 }
Joe Malin32736f02011-01-19 16:14:20 -08008792
Romain Guy6c319ca2011-01-11 14:29:25 -08008793 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008794 * Forces this view's layer to be created and this view to be rendered
8795 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8796 * invoking this method will have no effect.
8797 *
8798 * This method can for instance be used to render a view into its layer before
8799 * starting an animation. If this view is complex, rendering into the layer
8800 * before starting the animation will avoid skipping frames.
8801 *
8802 * @throws IllegalStateException If this view is not attached to a window
8803 *
8804 * @see #setLayerType(int, android.graphics.Paint)
8805 */
8806 public void buildLayer() {
8807 if (mLayerType == LAYER_TYPE_NONE) return;
8808
8809 if (mAttachInfo == null) {
8810 throw new IllegalStateException("This view must be attached to a window first");
8811 }
8812
8813 switch (mLayerType) {
8814 case LAYER_TYPE_HARDWARE:
8815 getHardwareLayer();
8816 break;
8817 case LAYER_TYPE_SOFTWARE:
8818 buildDrawingCache(true);
8819 break;
8820 }
8821 }
8822
8823 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008824 * <p>Returns a hardware layer that can be used to draw this view again
8825 * without executing its draw method.</p>
8826 *
8827 * @return A HardwareLayer ready to render, or null if an error occurred.
8828 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008829 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008830 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8831 return null;
8832 }
8833
8834 final int width = mRight - mLeft;
8835 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008836
Romain Guy6c319ca2011-01-11 14:29:25 -08008837 if (width == 0 || height == 0) {
8838 return null;
8839 }
8840
8841 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8842 if (mHardwareLayer == null) {
8843 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8844 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008845 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008846 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8847 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008848 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008849 }
8850
Romain Guy5e7f7662011-01-24 22:35:56 -08008851 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8852 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8853 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008854 try {
8855 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008856 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008857 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008858
Chet Haase88172fe2011-03-07 17:36:33 -08008859 final int restoreCount = canvas.save();
8860
Romain Guy6c319ca2011-01-11 14:29:25 -08008861 computeScroll();
8862 canvas.translate(-mScrollX, -mScrollY);
8863
Romain Guy6c319ca2011-01-11 14:29:25 -08008864 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008865
Romain Guy6c319ca2011-01-11 14:29:25 -08008866 // Fast path for layouts with no backgrounds
8867 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8868 mPrivateFlags &= ~DIRTY_MASK;
8869 dispatchDraw(canvas);
8870 } else {
8871 draw(canvas);
8872 }
Joe Malin32736f02011-01-19 16:14:20 -08008873
Chet Haase88172fe2011-03-07 17:36:33 -08008874 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008875 } finally {
8876 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008877 mHardwareLayer.end(currentCanvas);
8878 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008879 }
8880 }
8881
8882 return mHardwareLayer;
8883 }
Romain Guy171c5922011-01-06 10:04:23 -08008884
8885 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008886 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8887 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8888 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8889 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8890 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8891 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008892 *
Romain Guy171c5922011-01-06 10:04:23 -08008893 * <p>Enabling the drawing cache is similar to
8894 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008895 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8896 * drawing cache has no effect on rendering because the system uses a different mechanism
8897 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8898 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8899 * for information on how to enable software and hardware layers.</p>
8900 *
8901 * <p>This API can be used to manually generate
8902 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8903 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008904 *
8905 * @param enabled true to enable the drawing cache, false otherwise
8906 *
8907 * @see #isDrawingCacheEnabled()
8908 * @see #getDrawingCache()
8909 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008910 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008911 */
8912 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008913 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008914 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8915 }
8916
8917 /**
8918 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8919 *
8920 * @return true if the drawing cache is enabled
8921 *
8922 * @see #setDrawingCacheEnabled(boolean)
8923 * @see #getDrawingCache()
8924 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008925 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008926 public boolean isDrawingCacheEnabled() {
8927 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8928 }
8929
8930 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008931 * Debugging utility which recursively outputs the dirty state of a view and its
8932 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008933 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008934 * @hide
8935 */
Romain Guy676b1732011-02-14 14:45:33 -08008936 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008937 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8938 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8939 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8940 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8941 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8942 if (clear) {
8943 mPrivateFlags &= clearMask;
8944 }
8945 if (this instanceof ViewGroup) {
8946 ViewGroup parent = (ViewGroup) this;
8947 final int count = parent.getChildCount();
8948 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008949 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008950 child.outputDirtyFlags(indent + " ", clear, clearMask);
8951 }
8952 }
8953 }
8954
8955 /**
8956 * This method is used by ViewGroup to cause its children to restore or recreate their
8957 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8958 * to recreate its own display list, which would happen if it went through the normal
8959 * draw/dispatchDraw mechanisms.
8960 *
8961 * @hide
8962 */
8963 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008964
8965 /**
8966 * A view that is not attached or hardware accelerated cannot create a display list.
8967 * This method checks these conditions and returns the appropriate result.
8968 *
8969 * @return true if view has the ability to create a display list, false otherwise.
8970 *
8971 * @hide
8972 */
8973 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008974 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008975 }
Joe Malin32736f02011-01-19 16:14:20 -08008976
Chet Haasedaf98e92011-01-10 14:10:36 -08008977 /**
Romain Guyb051e892010-09-28 19:09:36 -07008978 * <p>Returns a display list that can be used to draw this view again
8979 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008980 *
Romain Guyb051e892010-09-28 19:09:36 -07008981 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008982 *
8983 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008984 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008985 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008986 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008987 return null;
8988 }
8989
Chet Haasedaf98e92011-01-10 14:10:36 -08008990 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8991 mDisplayList == null || !mDisplayList.isValid() ||
8992 mRecreateDisplayList)) {
8993 // Don't need to recreate the display list, just need to tell our
8994 // children to restore/recreate theirs
8995 if (mDisplayList != null && mDisplayList.isValid() &&
8996 !mRecreateDisplayList) {
8997 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8998 mPrivateFlags &= ~DIRTY_MASK;
8999 dispatchGetDisplayList();
9000
9001 return mDisplayList;
9002 }
9003
9004 // If we got here, we're recreating it. Mark it as such to ensure that
9005 // we copy in child display lists into ours in drawChild()
9006 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009007 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009008 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9009 // If we're creating a new display list, make sure our parent gets invalidated
9010 // since they will need to recreate their display list to account for this
9011 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009012 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009013 }
Romain Guyb051e892010-09-28 19:09:36 -07009014
9015 final HardwareCanvas canvas = mDisplayList.start();
9016 try {
9017 int width = mRight - mLeft;
9018 int height = mBottom - mTop;
9019
9020 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009021 // The dirty rect should always be null for a display list
9022 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009023
Chet Haase88172fe2011-03-07 17:36:33 -08009024 final int restoreCount = canvas.save();
9025
Chet Haasedaf98e92011-01-10 14:10:36 -08009026 computeScroll();
9027 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009028 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009029
Romain Guyb051e892010-09-28 19:09:36 -07009030 // Fast path for layouts with no backgrounds
9031 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9032 mPrivateFlags &= ~DIRTY_MASK;
9033 dispatchDraw(canvas);
9034 } else {
9035 draw(canvas);
9036 }
Joe Malin32736f02011-01-19 16:14:20 -08009037
Chet Haase88172fe2011-03-07 17:36:33 -08009038 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009039 } finally {
9040 canvas.onPostDraw();
9041
9042 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009043 }
Chet Haase77785f92011-01-25 23:22:09 -08009044 } else {
9045 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9046 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009047 }
9048
9049 return mDisplayList;
9050 }
9051
9052 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009053 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009054 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009055 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009056 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009057 * @see #getDrawingCache(boolean)
9058 */
9059 public Bitmap getDrawingCache() {
9060 return getDrawingCache(false);
9061 }
9062
9063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009064 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9065 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9066 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9067 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9068 * request the drawing cache by calling this method and draw it on screen if the
9069 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009070 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009071 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9072 * this method will create a bitmap of the same size as this view. Because this bitmap
9073 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9074 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9075 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9076 * size than the view. This implies that your application must be able to handle this
9077 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009078 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009079 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9080 * the current density of the screen when the application is in compatibility
9081 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009082 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009083 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009084 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009085 * @see #setDrawingCacheEnabled(boolean)
9086 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009087 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009088 * @see #destroyDrawingCache()
9089 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009090 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009091 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9092 return null;
9093 }
9094 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009095 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009096 }
Romain Guy02890fd2010-08-06 17:58:44 -07009097 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098 }
9099
9100 /**
9101 * <p>Frees the resources used by the drawing cache. If you call
9102 * {@link #buildDrawingCache()} manually without calling
9103 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9104 * should cleanup the cache with this method afterwards.</p>
9105 *
9106 * @see #setDrawingCacheEnabled(boolean)
9107 * @see #buildDrawingCache()
9108 * @see #getDrawingCache()
9109 */
9110 public void destroyDrawingCache() {
9111 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009112 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113 mDrawingCache = null;
9114 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009115 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009116 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009117 mUnscaledDrawingCache = null;
9118 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009119 }
9120
9121 /**
9122 * Setting a solid background color for the drawing cache's bitmaps will improve
9123 * perfromance and memory usage. Note, though that this should only be used if this
9124 * view will always be drawn on top of a solid color.
9125 *
9126 * @param color The background color to use for the drawing cache's bitmap
9127 *
9128 * @see #setDrawingCacheEnabled(boolean)
9129 * @see #buildDrawingCache()
9130 * @see #getDrawingCache()
9131 */
9132 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009133 if (color != mDrawingCacheBackgroundColor) {
9134 mDrawingCacheBackgroundColor = color;
9135 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 }
9138
9139 /**
9140 * @see #setDrawingCacheBackgroundColor(int)
9141 *
9142 * @return The background color to used for the drawing cache's bitmap
9143 */
9144 public int getDrawingCacheBackgroundColor() {
9145 return mDrawingCacheBackgroundColor;
9146 }
9147
9148 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009149 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009150 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009151 * @see #buildDrawingCache(boolean)
9152 */
9153 public void buildDrawingCache() {
9154 buildDrawingCache(false);
9155 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009156
Romain Guyfbd8f692009-06-26 14:51:58 -07009157 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9159 *
9160 * <p>If you call {@link #buildDrawingCache()} manually without calling
9161 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9162 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009163 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009164 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9165 * this method will create a bitmap of the same size as this view. Because this bitmap
9166 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9167 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9168 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9169 * size than the view. This implies that your application must be able to handle this
9170 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009171 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009172 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9173 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009174 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009175 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009176 *
9177 * @see #getDrawingCache()
9178 * @see #destroyDrawingCache()
9179 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009180 public void buildDrawingCache(boolean autoScale) {
9181 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009182 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009183 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009184
9185 if (ViewDebug.TRACE_HIERARCHY) {
9186 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9187 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009188
Romain Guy8506ab42009-06-11 17:35:47 -07009189 int width = mRight - mLeft;
9190 int height = mBottom - mTop;
9191
9192 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009193 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009194
Romain Guye1123222009-06-29 14:24:56 -07009195 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009196 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9197 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009199
9200 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009201 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009202 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009203
9204 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009205 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009206 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009207 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9208 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009209 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009210 return;
9211 }
9212
9213 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009214 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215
9216 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009217 Bitmap.Config quality;
9218 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009219 // Never pick ARGB_4444 because it looks awful
9220 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009221 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9222 case DRAWING_CACHE_QUALITY_AUTO:
9223 quality = Bitmap.Config.ARGB_8888;
9224 break;
9225 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009226 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009227 break;
9228 case DRAWING_CACHE_QUALITY_HIGH:
9229 quality = Bitmap.Config.ARGB_8888;
9230 break;
9231 default:
9232 quality = Bitmap.Config.ARGB_8888;
9233 break;
9234 }
9235 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009236 // Optimization for translucent windows
9237 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009238 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009239 }
9240
9241 // Try to cleanup memory
9242 if (bitmap != null) bitmap.recycle();
9243
9244 try {
9245 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009246 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009247 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009248 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009249 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009250 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009251 }
Adam Powell26153a32010-11-08 15:22:27 -08009252 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 } catch (OutOfMemoryError e) {
9254 // If there is not enough memory to create the bitmap cache, just
9255 // ignore the issue as bitmap caches are not required to draw the
9256 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009257 if (autoScale) {
9258 mDrawingCache = null;
9259 } else {
9260 mUnscaledDrawingCache = null;
9261 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009262 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263 return;
9264 }
9265
9266 clear = drawingCacheBackgroundColor != 0;
9267 }
9268
9269 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009270 if (attachInfo != null) {
9271 canvas = attachInfo.mCanvas;
9272 if (canvas == null) {
9273 canvas = new Canvas();
9274 }
9275 canvas.setBitmap(bitmap);
9276 // Temporarily clobber the cached Canvas in case one of our children
9277 // is also using a drawing cache. Without this, the children would
9278 // steal the canvas by attaching their own bitmap to it and bad, bad
9279 // thing would happen (invisible views, corrupted drawings, etc.)
9280 attachInfo.mCanvas = null;
9281 } else {
9282 // This case should hopefully never or seldom happen
9283 canvas = new Canvas(bitmap);
9284 }
9285
9286 if (clear) {
9287 bitmap.eraseColor(drawingCacheBackgroundColor);
9288 }
9289
9290 computeScroll();
9291 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009292
Romain Guye1123222009-06-29 14:24:56 -07009293 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009294 final float scale = attachInfo.mApplicationScale;
9295 canvas.scale(scale, scale);
9296 }
Joe Malin32736f02011-01-19 16:14:20 -08009297
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009298 canvas.translate(-mScrollX, -mScrollY);
9299
Romain Guy5bcdff42009-05-14 21:27:18 -07009300 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009301 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9302 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009303 mPrivateFlags |= DRAWING_CACHE_VALID;
9304 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009305
9306 // Fast path for layouts with no backgrounds
9307 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9308 if (ViewDebug.TRACE_HIERARCHY) {
9309 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9310 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009311 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009312 dispatchDraw(canvas);
9313 } else {
9314 draw(canvas);
9315 }
9316
9317 canvas.restoreToCount(restoreCount);
9318
9319 if (attachInfo != null) {
9320 // Restore the cached Canvas for our siblings
9321 attachInfo.mCanvas = canvas;
9322 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009323 }
9324 }
9325
9326 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009327 * Create a snapshot of the view into a bitmap. We should probably make
9328 * some form of this public, but should think about the API.
9329 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009330 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009331 int width = mRight - mLeft;
9332 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009333
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009334 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009335 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009336 width = (int) ((width * scale) + 0.5f);
9337 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009338
Romain Guy8c11e312009-09-14 15:15:30 -07009339 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009340 if (bitmap == null) {
9341 throw new OutOfMemoryError();
9342 }
9343
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009344 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009345
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009346 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009347 if (attachInfo != null) {
9348 canvas = attachInfo.mCanvas;
9349 if (canvas == null) {
9350 canvas = new Canvas();
9351 }
9352 canvas.setBitmap(bitmap);
9353 // Temporarily clobber the cached Canvas in case one of our children
9354 // is also using a drawing cache. Without this, the children would
9355 // steal the canvas by attaching their own bitmap to it and bad, bad
9356 // things would happen (invisible views, corrupted drawings, etc.)
9357 attachInfo.mCanvas = null;
9358 } else {
9359 // This case should hopefully never or seldom happen
9360 canvas = new Canvas(bitmap);
9361 }
9362
Romain Guy5bcdff42009-05-14 21:27:18 -07009363 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009364 bitmap.eraseColor(backgroundColor);
9365 }
9366
9367 computeScroll();
9368 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009369 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009370 canvas.translate(-mScrollX, -mScrollY);
9371
Romain Guy5bcdff42009-05-14 21:27:18 -07009372 // Temporarily remove the dirty mask
9373 int flags = mPrivateFlags;
9374 mPrivateFlags &= ~DIRTY_MASK;
9375
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009376 // Fast path for layouts with no backgrounds
9377 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9378 dispatchDraw(canvas);
9379 } else {
9380 draw(canvas);
9381 }
9382
Romain Guy5bcdff42009-05-14 21:27:18 -07009383 mPrivateFlags = flags;
9384
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009385 canvas.restoreToCount(restoreCount);
9386
9387 if (attachInfo != null) {
9388 // Restore the cached Canvas for our siblings
9389 attachInfo.mCanvas = canvas;
9390 }
Romain Guy8506ab42009-06-11 17:35:47 -07009391
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009392 return bitmap;
9393 }
9394
9395 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009396 * Indicates whether this View is currently in edit mode. A View is usually
9397 * in edit mode when displayed within a developer tool. For instance, if
9398 * this View is being drawn by a visual user interface builder, this method
9399 * should return true.
9400 *
9401 * Subclasses should check the return value of this method to provide
9402 * different behaviors if their normal behavior might interfere with the
9403 * host environment. For instance: the class spawns a thread in its
9404 * constructor, the drawing code relies on device-specific features, etc.
9405 *
9406 * This method is usually checked in the drawing code of custom widgets.
9407 *
9408 * @return True if this View is in edit mode, false otherwise.
9409 */
9410 public boolean isInEditMode() {
9411 return false;
9412 }
9413
9414 /**
9415 * If the View draws content inside its padding and enables fading edges,
9416 * it needs to support padding offsets. Padding offsets are added to the
9417 * fading edges to extend the length of the fade so that it covers pixels
9418 * drawn inside the padding.
9419 *
9420 * Subclasses of this class should override this method if they need
9421 * to draw content inside the padding.
9422 *
9423 * @return True if padding offset must be applied, false otherwise.
9424 *
9425 * @see #getLeftPaddingOffset()
9426 * @see #getRightPaddingOffset()
9427 * @see #getTopPaddingOffset()
9428 * @see #getBottomPaddingOffset()
9429 *
9430 * @since CURRENT
9431 */
9432 protected boolean isPaddingOffsetRequired() {
9433 return false;
9434 }
9435
9436 /**
9437 * Amount by which to extend the left fading region. Called only when
9438 * {@link #isPaddingOffsetRequired()} returns true.
9439 *
9440 * @return The left padding offset in pixels.
9441 *
9442 * @see #isPaddingOffsetRequired()
9443 *
9444 * @since CURRENT
9445 */
9446 protected int getLeftPaddingOffset() {
9447 return 0;
9448 }
9449
9450 /**
9451 * Amount by which to extend the right fading region. Called only when
9452 * {@link #isPaddingOffsetRequired()} returns true.
9453 *
9454 * @return The right padding offset in pixels.
9455 *
9456 * @see #isPaddingOffsetRequired()
9457 *
9458 * @since CURRENT
9459 */
9460 protected int getRightPaddingOffset() {
9461 return 0;
9462 }
9463
9464 /**
9465 * Amount by which to extend the top fading region. Called only when
9466 * {@link #isPaddingOffsetRequired()} returns true.
9467 *
9468 * @return The top padding offset in pixels.
9469 *
9470 * @see #isPaddingOffsetRequired()
9471 *
9472 * @since CURRENT
9473 */
9474 protected int getTopPaddingOffset() {
9475 return 0;
9476 }
9477
9478 /**
9479 * Amount by which to extend the bottom fading region. Called only when
9480 * {@link #isPaddingOffsetRequired()} returns true.
9481 *
9482 * @return The bottom padding offset in pixels.
9483 *
9484 * @see #isPaddingOffsetRequired()
9485 *
9486 * @since CURRENT
9487 */
9488 protected int getBottomPaddingOffset() {
9489 return 0;
9490 }
9491
9492 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009493 * <p>Indicates whether this view is attached to an hardware accelerated
9494 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009495 *
Romain Guy2bffd262010-09-12 17:40:02 -07009496 * <p>Even if this method returns true, it does not mean that every call
9497 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9498 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9499 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9500 * window is hardware accelerated,
9501 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9502 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009503 *
Romain Guy2bffd262010-09-12 17:40:02 -07009504 * @return True if the view is attached to a window and the window is
9505 * hardware accelerated; false in any other case.
9506 */
9507 public boolean isHardwareAccelerated() {
9508 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9509 }
Joe Malin32736f02011-01-19 16:14:20 -08009510
Romain Guy2bffd262010-09-12 17:40:02 -07009511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009512 * Manually render this view (and all of its children) to the given Canvas.
9513 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009514 * called. When implementing a view, implement {@link #onDraw} instead of
9515 * overriding this method. If you do need to override this method, call
9516 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 *
9518 * @param canvas The Canvas to which the View is rendered.
9519 */
9520 public void draw(Canvas canvas) {
9521 if (ViewDebug.TRACE_HIERARCHY) {
9522 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9523 }
9524
Romain Guy5bcdff42009-05-14 21:27:18 -07009525 final int privateFlags = mPrivateFlags;
9526 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9527 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9528 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009530 /*
9531 * Draw traversal performs several drawing steps which must be executed
9532 * in the appropriate order:
9533 *
9534 * 1. Draw the background
9535 * 2. If necessary, save the canvas' layers to prepare for fading
9536 * 3. Draw view's content
9537 * 4. Draw children
9538 * 5. If necessary, draw the fading edges and restore layers
9539 * 6. Draw decorations (scrollbars for instance)
9540 */
9541
9542 // Step 1, draw the background, if needed
9543 int saveCount;
9544
Romain Guy24443ea2009-05-11 11:56:30 -07009545 if (!dirtyOpaque) {
9546 final Drawable background = mBGDrawable;
9547 if (background != null) {
9548 final int scrollX = mScrollX;
9549 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009550
Romain Guy24443ea2009-05-11 11:56:30 -07009551 if (mBackgroundSizeChanged) {
9552 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9553 mBackgroundSizeChanged = false;
9554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009555
Romain Guy24443ea2009-05-11 11:56:30 -07009556 if ((scrollX | scrollY) == 0) {
9557 background.draw(canvas);
9558 } else {
9559 canvas.translate(scrollX, scrollY);
9560 background.draw(canvas);
9561 canvas.translate(-scrollX, -scrollY);
9562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009563 }
9564 }
9565
9566 // skip step 2 & 5 if possible (common case)
9567 final int viewFlags = mViewFlags;
9568 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9569 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9570 if (!verticalEdges && !horizontalEdges) {
9571 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009572 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009573
9574 // Step 4, draw the children
9575 dispatchDraw(canvas);
9576
9577 // Step 6, draw decorations (scrollbars)
9578 onDrawScrollBars(canvas);
9579
9580 // we're done...
9581 return;
9582 }
9583
9584 /*
9585 * Here we do the full fledged routine...
9586 * (this is an uncommon case where speed matters less,
9587 * this is why we repeat some of the tests that have been
9588 * done above)
9589 */
9590
9591 boolean drawTop = false;
9592 boolean drawBottom = false;
9593 boolean drawLeft = false;
9594 boolean drawRight = false;
9595
9596 float topFadeStrength = 0.0f;
9597 float bottomFadeStrength = 0.0f;
9598 float leftFadeStrength = 0.0f;
9599 float rightFadeStrength = 0.0f;
9600
9601 // Step 2, save the canvas' layers
9602 int paddingLeft = mPaddingLeft;
9603 int paddingTop = mPaddingTop;
9604
9605 final boolean offsetRequired = isPaddingOffsetRequired();
9606 if (offsetRequired) {
9607 paddingLeft += getLeftPaddingOffset();
9608 paddingTop += getTopPaddingOffset();
9609 }
9610
9611 int left = mScrollX + paddingLeft;
9612 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9613 int top = mScrollY + paddingTop;
9614 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9615
9616 if (offsetRequired) {
9617 right += getRightPaddingOffset();
9618 bottom += getBottomPaddingOffset();
9619 }
9620
9621 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009622 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9623 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009624
9625 // clip the fade length if top and bottom fades overlap
9626 // overlapping fades produce odd-looking artifacts
9627 if (verticalEdges && (top + length > bottom - length)) {
9628 length = (bottom - top) / 2;
9629 }
9630
9631 // also clip horizontal fades if necessary
9632 if (horizontalEdges && (left + length > right - length)) {
9633 length = (right - left) / 2;
9634 }
9635
9636 if (verticalEdges) {
9637 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009638 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009639 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009640 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009641 }
9642
9643 if (horizontalEdges) {
9644 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009645 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009646 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009647 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009648 }
9649
9650 saveCount = canvas.getSaveCount();
9651
9652 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009653 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009654 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9655
9656 if (drawTop) {
9657 canvas.saveLayer(left, top, right, top + length, null, flags);
9658 }
9659
9660 if (drawBottom) {
9661 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9662 }
9663
9664 if (drawLeft) {
9665 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9666 }
9667
9668 if (drawRight) {
9669 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9670 }
9671 } else {
9672 scrollabilityCache.setFadeColor(solidColor);
9673 }
9674
9675 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009676 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009677
9678 // Step 4, draw the children
9679 dispatchDraw(canvas);
9680
9681 // Step 5, draw the fade effect and restore layers
9682 final Paint p = scrollabilityCache.paint;
9683 final Matrix matrix = scrollabilityCache.matrix;
9684 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009685
9686 if (drawTop) {
9687 matrix.setScale(1, fadeHeight * topFadeStrength);
9688 matrix.postTranslate(left, top);
9689 fade.setLocalMatrix(matrix);
9690 canvas.drawRect(left, top, right, top + length, p);
9691 }
9692
9693 if (drawBottom) {
9694 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9695 matrix.postRotate(180);
9696 matrix.postTranslate(left, bottom);
9697 fade.setLocalMatrix(matrix);
9698 canvas.drawRect(left, bottom - length, right, bottom, p);
9699 }
9700
9701 if (drawLeft) {
9702 matrix.setScale(1, fadeHeight * leftFadeStrength);
9703 matrix.postRotate(-90);
9704 matrix.postTranslate(left, top);
9705 fade.setLocalMatrix(matrix);
9706 canvas.drawRect(left, top, left + length, bottom, p);
9707 }
9708
9709 if (drawRight) {
9710 matrix.setScale(1, fadeHeight * rightFadeStrength);
9711 matrix.postRotate(90);
9712 matrix.postTranslate(right, top);
9713 fade.setLocalMatrix(matrix);
9714 canvas.drawRect(right - length, top, right, bottom, p);
9715 }
9716
9717 canvas.restoreToCount(saveCount);
9718
9719 // Step 6, draw decorations (scrollbars)
9720 onDrawScrollBars(canvas);
9721 }
9722
9723 /**
9724 * Override this if your view is known to always be drawn on top of a solid color background,
9725 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9726 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9727 * should be set to 0xFF.
9728 *
9729 * @see #setVerticalFadingEdgeEnabled
9730 * @see #setHorizontalFadingEdgeEnabled
9731 *
9732 * @return The known solid color background for this view, or 0 if the color may vary
9733 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009734 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009735 public int getSolidColor() {
9736 return 0;
9737 }
9738
9739 /**
9740 * Build a human readable string representation of the specified view flags.
9741 *
9742 * @param flags the view flags to convert to a string
9743 * @return a String representing the supplied flags
9744 */
9745 private static String printFlags(int flags) {
9746 String output = "";
9747 int numFlags = 0;
9748 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9749 output += "TAKES_FOCUS";
9750 numFlags++;
9751 }
9752
9753 switch (flags & VISIBILITY_MASK) {
9754 case INVISIBLE:
9755 if (numFlags > 0) {
9756 output += " ";
9757 }
9758 output += "INVISIBLE";
9759 // USELESS HERE numFlags++;
9760 break;
9761 case GONE:
9762 if (numFlags > 0) {
9763 output += " ";
9764 }
9765 output += "GONE";
9766 // USELESS HERE numFlags++;
9767 break;
9768 default:
9769 break;
9770 }
9771 return output;
9772 }
9773
9774 /**
9775 * Build a human readable string representation of the specified private
9776 * view flags.
9777 *
9778 * @param privateFlags the private view flags to convert to a string
9779 * @return a String representing the supplied flags
9780 */
9781 private static String printPrivateFlags(int privateFlags) {
9782 String output = "";
9783 int numFlags = 0;
9784
9785 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9786 output += "WANTS_FOCUS";
9787 numFlags++;
9788 }
9789
9790 if ((privateFlags & FOCUSED) == FOCUSED) {
9791 if (numFlags > 0) {
9792 output += " ";
9793 }
9794 output += "FOCUSED";
9795 numFlags++;
9796 }
9797
9798 if ((privateFlags & SELECTED) == SELECTED) {
9799 if (numFlags > 0) {
9800 output += " ";
9801 }
9802 output += "SELECTED";
9803 numFlags++;
9804 }
9805
9806 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9807 if (numFlags > 0) {
9808 output += " ";
9809 }
9810 output += "IS_ROOT_NAMESPACE";
9811 numFlags++;
9812 }
9813
9814 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9815 if (numFlags > 0) {
9816 output += " ";
9817 }
9818 output += "HAS_BOUNDS";
9819 numFlags++;
9820 }
9821
9822 if ((privateFlags & DRAWN) == DRAWN) {
9823 if (numFlags > 0) {
9824 output += " ";
9825 }
9826 output += "DRAWN";
9827 // USELESS HERE numFlags++;
9828 }
9829 return output;
9830 }
9831
9832 /**
9833 * <p>Indicates whether or not this view's layout will be requested during
9834 * the next hierarchy layout pass.</p>
9835 *
9836 * @return true if the layout will be forced during next layout pass
9837 */
9838 public boolean isLayoutRequested() {
9839 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9840 }
9841
9842 /**
9843 * Assign a size and position to a view and all of its
9844 * descendants
9845 *
9846 * <p>This is the second phase of the layout mechanism.
9847 * (The first is measuring). In this phase, each parent calls
9848 * layout on all of its children to position them.
9849 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009850 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009851 *
Chet Haase9c087442011-01-12 16:20:16 -08009852 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009853 * Derived classes with children should override
9854 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009855 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009856 *
9857 * @param l Left position, relative to parent
9858 * @param t Top position, relative to parent
9859 * @param r Right position, relative to parent
9860 * @param b Bottom position, relative to parent
9861 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009862 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009863 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009864 int oldL = mLeft;
9865 int oldT = mTop;
9866 int oldB = mBottom;
9867 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 boolean changed = setFrame(l, t, r, b);
9869 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9870 if (ViewDebug.TRACE_HIERARCHY) {
9871 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9872 }
9873
9874 onLayout(changed, l, t, r, b);
9875 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009876
9877 if (mOnLayoutChangeListeners != null) {
9878 ArrayList<OnLayoutChangeListener> listenersCopy =
9879 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9880 int numListeners = listenersCopy.size();
9881 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009882 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009883 }
9884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009885 }
9886 mPrivateFlags &= ~FORCE_LAYOUT;
9887 }
9888
9889 /**
9890 * Called from layout when this view should
9891 * assign a size and position to each of its children.
9892 *
9893 * Derived classes with children should override
9894 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009895 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009896 * @param changed This is a new size or position for this view
9897 * @param left Left position, relative to parent
9898 * @param top Top position, relative to parent
9899 * @param right Right position, relative to parent
9900 * @param bottom Bottom position, relative to parent
9901 */
9902 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9903 }
9904
9905 /**
9906 * Assign a size and position to this view.
9907 *
9908 * This is called from layout.
9909 *
9910 * @param left Left position, relative to parent
9911 * @param top Top position, relative to parent
9912 * @param right Right position, relative to parent
9913 * @param bottom Bottom position, relative to parent
9914 * @return true if the new size and position are different than the
9915 * previous ones
9916 * {@hide}
9917 */
9918 protected boolean setFrame(int left, int top, int right, int bottom) {
9919 boolean changed = false;
9920
9921 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009922 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009923 + right + "," + bottom + ")");
9924 }
9925
9926 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9927 changed = true;
9928
9929 // Remember our drawn bit
9930 int drawn = mPrivateFlags & DRAWN;
9931
9932 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009933 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009934
9935
9936 int oldWidth = mRight - mLeft;
9937 int oldHeight = mBottom - mTop;
9938
9939 mLeft = left;
9940 mTop = top;
9941 mRight = right;
9942 mBottom = bottom;
9943
9944 mPrivateFlags |= HAS_BOUNDS;
9945
9946 int newWidth = right - left;
9947 int newHeight = bottom - top;
9948
9949 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009950 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9951 // A change in dimension means an auto-centered pivot point changes, too
9952 mMatrixDirty = true;
9953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009954 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9955 }
9956
9957 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9958 // If we are visible, force the DRAWN bit to on so that
9959 // this invalidate will go through (at least to our parent).
9960 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009961 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009962 // the DRAWN bit.
9963 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009964 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009965 // parent display list may need to be recreated based on a change in the bounds
9966 // of any child
9967 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009968 }
9969
9970 // Reset drawn bit to original value (invalidate turns it off)
9971 mPrivateFlags |= drawn;
9972
9973 mBackgroundSizeChanged = true;
9974 }
9975 return changed;
9976 }
9977
9978 /**
9979 * Finalize inflating a view from XML. This is called as the last phase
9980 * of inflation, after all child views have been added.
9981 *
9982 * <p>Even if the subclass overrides onFinishInflate, they should always be
9983 * sure to call the super method, so that we get called.
9984 */
9985 protected void onFinishInflate() {
9986 }
9987
9988 /**
9989 * Returns the resources associated with this view.
9990 *
9991 * @return Resources object.
9992 */
9993 public Resources getResources() {
9994 return mResources;
9995 }
9996
9997 /**
9998 * Invalidates the specified Drawable.
9999 *
10000 * @param drawable the drawable to invalidate
10001 */
10002 public void invalidateDrawable(Drawable drawable) {
10003 if (verifyDrawable(drawable)) {
10004 final Rect dirty = drawable.getBounds();
10005 final int scrollX = mScrollX;
10006 final int scrollY = mScrollY;
10007
10008 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10009 dirty.right + scrollX, dirty.bottom + scrollY);
10010 }
10011 }
10012
10013 /**
10014 * Schedules an action on a drawable to occur at a specified time.
10015 *
10016 * @param who the recipient of the action
10017 * @param what the action to run on the drawable
10018 * @param when the time at which the action must occur. Uses the
10019 * {@link SystemClock#uptimeMillis} timebase.
10020 */
10021 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10022 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10023 mAttachInfo.mHandler.postAtTime(what, who, when);
10024 }
10025 }
10026
10027 /**
10028 * Cancels a scheduled action on a drawable.
10029 *
10030 * @param who the recipient of the action
10031 * @param what the action to cancel
10032 */
10033 public void unscheduleDrawable(Drawable who, Runnable what) {
10034 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10035 mAttachInfo.mHandler.removeCallbacks(what, who);
10036 }
10037 }
10038
10039 /**
10040 * Unschedule any events associated with the given Drawable. This can be
10041 * used when selecting a new Drawable into a view, so that the previous
10042 * one is completely unscheduled.
10043 *
10044 * @param who The Drawable to unschedule.
10045 *
10046 * @see #drawableStateChanged
10047 */
10048 public void unscheduleDrawable(Drawable who) {
10049 if (mAttachInfo != null) {
10050 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10051 }
10052 }
10053
10054 /**
10055 * If your view subclass is displaying its own Drawable objects, it should
10056 * override this function and return true for any Drawable it is
10057 * displaying. This allows animations for those drawables to be
10058 * scheduled.
10059 *
10060 * <p>Be sure to call through to the super class when overriding this
10061 * function.
10062 *
10063 * @param who The Drawable to verify. Return true if it is one you are
10064 * displaying, else return the result of calling through to the
10065 * super class.
10066 *
10067 * @return boolean If true than the Drawable is being displayed in the
10068 * view; else false and it is not allowed to animate.
10069 *
10070 * @see #unscheduleDrawable
10071 * @see #drawableStateChanged
10072 */
10073 protected boolean verifyDrawable(Drawable who) {
10074 return who == mBGDrawable;
10075 }
10076
10077 /**
10078 * This function is called whenever the state of the view changes in such
10079 * a way that it impacts the state of drawables being shown.
10080 *
10081 * <p>Be sure to call through to the superclass when overriding this
10082 * function.
10083 *
10084 * @see Drawable#setState
10085 */
10086 protected void drawableStateChanged() {
10087 Drawable d = mBGDrawable;
10088 if (d != null && d.isStateful()) {
10089 d.setState(getDrawableState());
10090 }
10091 }
10092
10093 /**
10094 * Call this to force a view to update its drawable state. This will cause
10095 * drawableStateChanged to be called on this view. Views that are interested
10096 * in the new state should call getDrawableState.
10097 *
10098 * @see #drawableStateChanged
10099 * @see #getDrawableState
10100 */
10101 public void refreshDrawableState() {
10102 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10103 drawableStateChanged();
10104
10105 ViewParent parent = mParent;
10106 if (parent != null) {
10107 parent.childDrawableStateChanged(this);
10108 }
10109 }
10110
10111 /**
10112 * Return an array of resource IDs of the drawable states representing the
10113 * current state of the view.
10114 *
10115 * @return The current drawable state
10116 *
10117 * @see Drawable#setState
10118 * @see #drawableStateChanged
10119 * @see #onCreateDrawableState
10120 */
10121 public final int[] getDrawableState() {
10122 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10123 return mDrawableState;
10124 } else {
10125 mDrawableState = onCreateDrawableState(0);
10126 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10127 return mDrawableState;
10128 }
10129 }
10130
10131 /**
10132 * Generate the new {@link android.graphics.drawable.Drawable} state for
10133 * this view. This is called by the view
10134 * system when the cached Drawable state is determined to be invalid. To
10135 * retrieve the current state, you should use {@link #getDrawableState}.
10136 *
10137 * @param extraSpace if non-zero, this is the number of extra entries you
10138 * would like in the returned array in which you can place your own
10139 * states.
10140 *
10141 * @return Returns an array holding the current {@link Drawable} state of
10142 * the view.
10143 *
10144 * @see #mergeDrawableStates
10145 */
10146 protected int[] onCreateDrawableState(int extraSpace) {
10147 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10148 mParent instanceof View) {
10149 return ((View) mParent).onCreateDrawableState(extraSpace);
10150 }
10151
10152 int[] drawableState;
10153
10154 int privateFlags = mPrivateFlags;
10155
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010156 int viewStateIndex = 0;
10157 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10158 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10159 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010160 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010161 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10162 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010163 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10164 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010165 // This is set if HW acceleration is requested, even if the current
10166 // process doesn't allow it. This is just to allow app preview
10167 // windows to better match their app.
10168 viewStateIndex |= VIEW_STATE_ACCELERATED;
10169 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010170 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171
Christopher Tate3d4bf172011-03-28 16:16:46 -070010172 final int privateFlags2 = mPrivateFlags2;
10173 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10174 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010176 drawableState = VIEW_STATE_SETS[viewStateIndex];
10177
10178 //noinspection ConstantIfStatement
10179 if (false) {
10180 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10181 Log.i("View", toString()
10182 + " pressed=" + ((privateFlags & PRESSED) != 0)
10183 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10184 + " fo=" + hasFocus()
10185 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010186 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010187 + ": " + Arrays.toString(drawableState));
10188 }
10189
10190 if (extraSpace == 0) {
10191 return drawableState;
10192 }
10193
10194 final int[] fullState;
10195 if (drawableState != null) {
10196 fullState = new int[drawableState.length + extraSpace];
10197 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10198 } else {
10199 fullState = new int[extraSpace];
10200 }
10201
10202 return fullState;
10203 }
10204
10205 /**
10206 * Merge your own state values in <var>additionalState</var> into the base
10207 * state values <var>baseState</var> that were returned by
10208 * {@link #onCreateDrawableState}.
10209 *
10210 * @param baseState The base state values returned by
10211 * {@link #onCreateDrawableState}, which will be modified to also hold your
10212 * own additional state values.
10213 *
10214 * @param additionalState The additional state values you would like
10215 * added to <var>baseState</var>; this array is not modified.
10216 *
10217 * @return As a convenience, the <var>baseState</var> array you originally
10218 * passed into the function is returned.
10219 *
10220 * @see #onCreateDrawableState
10221 */
10222 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10223 final int N = baseState.length;
10224 int i = N - 1;
10225 while (i >= 0 && baseState[i] == 0) {
10226 i--;
10227 }
10228 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10229 return baseState;
10230 }
10231
10232 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010233 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10234 * on all Drawable objects associated with this view.
10235 */
10236 public void jumpDrawablesToCurrentState() {
10237 if (mBGDrawable != null) {
10238 mBGDrawable.jumpToCurrentState();
10239 }
10240 }
10241
10242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010243 * Sets the background color for this view.
10244 * @param color the color of the background
10245 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010246 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010247 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010248 if (mBGDrawable instanceof ColorDrawable) {
10249 ((ColorDrawable) mBGDrawable).setColor(color);
10250 } else {
10251 setBackgroundDrawable(new ColorDrawable(color));
10252 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010253 }
10254
10255 /**
10256 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010257 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010258 * @param resid The identifier of the resource.
10259 * @attr ref android.R.styleable#View_background
10260 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010261 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010262 public void setBackgroundResource(int resid) {
10263 if (resid != 0 && resid == mBackgroundResource) {
10264 return;
10265 }
10266
10267 Drawable d= null;
10268 if (resid != 0) {
10269 d = mResources.getDrawable(resid);
10270 }
10271 setBackgroundDrawable(d);
10272
10273 mBackgroundResource = resid;
10274 }
10275
10276 /**
10277 * Set the background to a given Drawable, or remove the background. If the
10278 * background has padding, this View's padding is set to the background's
10279 * padding. However, when a background is removed, this View's padding isn't
10280 * touched. If setting the padding is desired, please use
10281 * {@link #setPadding(int, int, int, int)}.
10282 *
10283 * @param d The Drawable to use as the background, or null to remove the
10284 * background
10285 */
10286 public void setBackgroundDrawable(Drawable d) {
10287 boolean requestLayout = false;
10288
10289 mBackgroundResource = 0;
10290
10291 /*
10292 * Regardless of whether we're setting a new background or not, we want
10293 * to clear the previous drawable.
10294 */
10295 if (mBGDrawable != null) {
10296 mBGDrawable.setCallback(null);
10297 unscheduleDrawable(mBGDrawable);
10298 }
10299
10300 if (d != null) {
10301 Rect padding = sThreadLocal.get();
10302 if (padding == null) {
10303 padding = new Rect();
10304 sThreadLocal.set(padding);
10305 }
10306 if (d.getPadding(padding)) {
10307 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10308 }
10309
10310 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10311 // if it has a different minimum size, we should layout again
10312 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10313 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10314 requestLayout = true;
10315 }
10316
10317 d.setCallback(this);
10318 if (d.isStateful()) {
10319 d.setState(getDrawableState());
10320 }
10321 d.setVisible(getVisibility() == VISIBLE, false);
10322 mBGDrawable = d;
10323
10324 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10325 mPrivateFlags &= ~SKIP_DRAW;
10326 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10327 requestLayout = true;
10328 }
10329 } else {
10330 /* Remove the background */
10331 mBGDrawable = null;
10332
10333 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10334 /*
10335 * This view ONLY drew the background before and we're removing
10336 * the background, so now it won't draw anything
10337 * (hence we SKIP_DRAW)
10338 */
10339 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10340 mPrivateFlags |= SKIP_DRAW;
10341 }
10342
10343 /*
10344 * When the background is set, we try to apply its padding to this
10345 * View. When the background is removed, we don't touch this View's
10346 * padding. This is noted in the Javadocs. Hence, we don't need to
10347 * requestLayout(), the invalidate() below is sufficient.
10348 */
10349
10350 // The old background's minimum size could have affected this
10351 // View's layout, so let's requestLayout
10352 requestLayout = true;
10353 }
10354
Romain Guy8f1344f52009-05-15 16:03:59 -070010355 computeOpaqueFlags();
10356
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010357 if (requestLayout) {
10358 requestLayout();
10359 }
10360
10361 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010362 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010363 }
10364
10365 /**
10366 * Gets the background drawable
10367 * @return The drawable used as the background for this view, if any.
10368 */
10369 public Drawable getBackground() {
10370 return mBGDrawable;
10371 }
10372
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010373 /**
10374 * Sets the padding. The view may add on the space required to display
10375 * the scrollbars, depending on the style and visibility of the scrollbars.
10376 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10377 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10378 * from the values set in this call.
10379 *
10380 * @attr ref android.R.styleable#View_padding
10381 * @attr ref android.R.styleable#View_paddingBottom
10382 * @attr ref android.R.styleable#View_paddingLeft
10383 * @attr ref android.R.styleable#View_paddingRight
10384 * @attr ref android.R.styleable#View_paddingTop
10385 * @param left the left padding in pixels
10386 * @param top the top padding in pixels
10387 * @param right the right padding in pixels
10388 * @param bottom the bottom padding in pixels
10389 */
10390 public void setPadding(int left, int top, int right, int bottom) {
10391 boolean changed = false;
10392
Adam Powell20232d02010-12-08 21:08:53 -080010393 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010394 mUserPaddingRight = right;
10395 mUserPaddingBottom = bottom;
10396
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010397 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010398
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010399 // Common case is there are no scroll bars.
10400 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010401 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010402 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10403 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010404 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010405 switch (mVerticalScrollbarPosition) {
10406 case SCROLLBAR_POSITION_DEFAULT:
10407 case SCROLLBAR_POSITION_RIGHT:
10408 right += offset;
10409 break;
10410 case SCROLLBAR_POSITION_LEFT:
10411 left += offset;
10412 break;
10413 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010414 }
Adam Powell20232d02010-12-08 21:08:53 -080010415 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010416 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10417 ? 0 : getHorizontalScrollbarHeight();
10418 }
10419 }
Romain Guy8506ab42009-06-11 17:35:47 -070010420
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010421 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010422 changed = true;
10423 mPaddingLeft = left;
10424 }
10425 if (mPaddingTop != top) {
10426 changed = true;
10427 mPaddingTop = top;
10428 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010429 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010431 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010432 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010433 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010434 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010435 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436 }
10437
10438 if (changed) {
10439 requestLayout();
10440 }
10441 }
10442
10443 /**
10444 * Returns the top padding of this view.
10445 *
10446 * @return the top padding in pixels
10447 */
10448 public int getPaddingTop() {
10449 return mPaddingTop;
10450 }
10451
10452 /**
10453 * Returns the bottom padding of this view. If there are inset and enabled
10454 * scrollbars, this value may include the space required to display the
10455 * scrollbars as well.
10456 *
10457 * @return the bottom padding in pixels
10458 */
10459 public int getPaddingBottom() {
10460 return mPaddingBottom;
10461 }
10462
10463 /**
10464 * Returns the left padding of this view. If there are inset and enabled
10465 * scrollbars, this value may include the space required to display the
10466 * scrollbars as well.
10467 *
10468 * @return the left padding in pixels
10469 */
10470 public int getPaddingLeft() {
10471 return mPaddingLeft;
10472 }
10473
10474 /**
10475 * Returns the right padding of this view. If there are inset and enabled
10476 * scrollbars, this value may include the space required to display the
10477 * scrollbars as well.
10478 *
10479 * @return the right padding in pixels
10480 */
10481 public int getPaddingRight() {
10482 return mPaddingRight;
10483 }
10484
10485 /**
10486 * Changes the selection state of this view. A view can be selected or not.
10487 * Note that selection is not the same as focus. Views are typically
10488 * selected in the context of an AdapterView like ListView or GridView;
10489 * the selected view is the view that is highlighted.
10490 *
10491 * @param selected true if the view must be selected, false otherwise
10492 */
10493 public void setSelected(boolean selected) {
10494 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10495 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010496 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010497 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010498 refreshDrawableState();
10499 dispatchSetSelected(selected);
10500 }
10501 }
10502
10503 /**
10504 * Dispatch setSelected to all of this View's children.
10505 *
10506 * @see #setSelected(boolean)
10507 *
10508 * @param selected The new selected state
10509 */
10510 protected void dispatchSetSelected(boolean selected) {
10511 }
10512
10513 /**
10514 * Indicates the selection state of this view.
10515 *
10516 * @return true if the view is selected, false otherwise
10517 */
10518 @ViewDebug.ExportedProperty
10519 public boolean isSelected() {
10520 return (mPrivateFlags & SELECTED) != 0;
10521 }
10522
10523 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010524 * Changes the activated state of this view. A view can be activated or not.
10525 * Note that activation is not the same as selection. Selection is
10526 * a transient property, representing the view (hierarchy) the user is
10527 * currently interacting with. Activation is a longer-term state that the
10528 * user can move views in and out of. For example, in a list view with
10529 * single or multiple selection enabled, the views in the current selection
10530 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10531 * here.) The activated state is propagated down to children of the view it
10532 * is set on.
10533 *
10534 * @param activated true if the view must be activated, false otherwise
10535 */
10536 public void setActivated(boolean activated) {
10537 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10538 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010539 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010540 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010541 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010542 }
10543 }
10544
10545 /**
10546 * Dispatch setActivated to all of this View's children.
10547 *
10548 * @see #setActivated(boolean)
10549 *
10550 * @param activated The new activated state
10551 */
10552 protected void dispatchSetActivated(boolean activated) {
10553 }
10554
10555 /**
10556 * Indicates the activation state of this view.
10557 *
10558 * @return true if the view is activated, false otherwise
10559 */
10560 @ViewDebug.ExportedProperty
10561 public boolean isActivated() {
10562 return (mPrivateFlags & ACTIVATED) != 0;
10563 }
10564
10565 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10567 * observer can be used to get notifications when global events, like
10568 * layout, happen.
10569 *
10570 * The returned ViewTreeObserver observer is not guaranteed to remain
10571 * valid for the lifetime of this View. If the caller of this method keeps
10572 * a long-lived reference to ViewTreeObserver, it should always check for
10573 * the return value of {@link ViewTreeObserver#isAlive()}.
10574 *
10575 * @return The ViewTreeObserver for this view's hierarchy.
10576 */
10577 public ViewTreeObserver getViewTreeObserver() {
10578 if (mAttachInfo != null) {
10579 return mAttachInfo.mTreeObserver;
10580 }
10581 if (mFloatingTreeObserver == null) {
10582 mFloatingTreeObserver = new ViewTreeObserver();
10583 }
10584 return mFloatingTreeObserver;
10585 }
10586
10587 /**
10588 * <p>Finds the topmost view in the current view hierarchy.</p>
10589 *
10590 * @return the topmost view containing this view
10591 */
10592 public View getRootView() {
10593 if (mAttachInfo != null) {
10594 final View v = mAttachInfo.mRootView;
10595 if (v != null) {
10596 return v;
10597 }
10598 }
Romain Guy8506ab42009-06-11 17:35:47 -070010599
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010600 View parent = this;
10601
10602 while (parent.mParent != null && parent.mParent instanceof View) {
10603 parent = (View) parent.mParent;
10604 }
10605
10606 return parent;
10607 }
10608
10609 /**
10610 * <p>Computes the coordinates of this view on the screen. The argument
10611 * must be an array of two integers. After the method returns, the array
10612 * contains the x and y location in that order.</p>
10613 *
10614 * @param location an array of two integers in which to hold the coordinates
10615 */
10616 public void getLocationOnScreen(int[] location) {
10617 getLocationInWindow(location);
10618
10619 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010620 if (info != null) {
10621 location[0] += info.mWindowLeft;
10622 location[1] += info.mWindowTop;
10623 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010624 }
10625
10626 /**
10627 * <p>Computes the coordinates of this view in its window. The argument
10628 * must be an array of two integers. After the method returns, the array
10629 * contains the x and y location in that order.</p>
10630 *
10631 * @param location an array of two integers in which to hold the coordinates
10632 */
10633 public void getLocationInWindow(int[] location) {
10634 if (location == null || location.length < 2) {
10635 throw new IllegalArgumentException("location must be an array of "
10636 + "two integers");
10637 }
10638
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010639 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10640 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010641
10642 ViewParent viewParent = mParent;
10643 while (viewParent instanceof View) {
10644 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010645 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10646 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 viewParent = view.mParent;
10648 }
Romain Guy8506ab42009-06-11 17:35:47 -070010649
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010650 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010651 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010652 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010653 location[1] -= vr.mCurScrollY;
10654 }
10655 }
10656
10657 /**
10658 * {@hide}
10659 * @param id the id of the view to be found
10660 * @return the view of the specified id, null if cannot be found
10661 */
10662 protected View findViewTraversal(int id) {
10663 if (id == mID) {
10664 return this;
10665 }
10666 return null;
10667 }
10668
10669 /**
10670 * {@hide}
10671 * @param tag the tag of the view to be found
10672 * @return the view of specified tag, null if cannot be found
10673 */
10674 protected View findViewWithTagTraversal(Object tag) {
10675 if (tag != null && tag.equals(mTag)) {
10676 return this;
10677 }
10678 return null;
10679 }
10680
10681 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010682 * {@hide}
10683 * @param predicate The predicate to evaluate.
10684 * @return The first view that matches the predicate or null.
10685 */
10686 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10687 if (predicate.apply(this)) {
10688 return this;
10689 }
10690 return null;
10691 }
10692
10693 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010694 * Look for a child view with the given id. If this view has the given
10695 * id, return this view.
10696 *
10697 * @param id The id to search for.
10698 * @return The view that has the given id in the hierarchy or null
10699 */
10700 public final View findViewById(int id) {
10701 if (id < 0) {
10702 return null;
10703 }
10704 return findViewTraversal(id);
10705 }
10706
10707 /**
10708 * Look for a child view with the given tag. If this view has the given
10709 * tag, return this view.
10710 *
10711 * @param tag The tag to search for, using "tag.equals(getTag())".
10712 * @return The View that has the given tag in the hierarchy or null
10713 */
10714 public final View findViewWithTag(Object tag) {
10715 if (tag == null) {
10716 return null;
10717 }
10718 return findViewWithTagTraversal(tag);
10719 }
10720
10721 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010722 * {@hide}
10723 * Look for a child view that matches the specified predicate.
10724 * If this view matches the predicate, return this view.
10725 *
10726 * @param predicate The predicate to evaluate.
10727 * @return The first view that matches the predicate or null.
10728 */
10729 public final View findViewByPredicate(Predicate<View> predicate) {
10730 return findViewByPredicateTraversal(predicate);
10731 }
10732
10733 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010734 * Sets the identifier for this view. The identifier does not have to be
10735 * unique in this view's hierarchy. The identifier should be a positive
10736 * number.
10737 *
10738 * @see #NO_ID
10739 * @see #getId
10740 * @see #findViewById
10741 *
10742 * @param id a number used to identify the view
10743 *
10744 * @attr ref android.R.styleable#View_id
10745 */
10746 public void setId(int id) {
10747 mID = id;
10748 }
10749
10750 /**
10751 * {@hide}
10752 *
10753 * @param isRoot true if the view belongs to the root namespace, false
10754 * otherwise
10755 */
10756 public void setIsRootNamespace(boolean isRoot) {
10757 if (isRoot) {
10758 mPrivateFlags |= IS_ROOT_NAMESPACE;
10759 } else {
10760 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10761 }
10762 }
10763
10764 /**
10765 * {@hide}
10766 *
10767 * @return true if the view belongs to the root namespace, false otherwise
10768 */
10769 public boolean isRootNamespace() {
10770 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10771 }
10772
10773 /**
10774 * Returns this view's identifier.
10775 *
10776 * @return a positive integer used to identify the view or {@link #NO_ID}
10777 * if the view has no ID
10778 *
10779 * @see #setId
10780 * @see #findViewById
10781 * @attr ref android.R.styleable#View_id
10782 */
10783 @ViewDebug.CapturedViewProperty
10784 public int getId() {
10785 return mID;
10786 }
10787
10788 /**
10789 * Returns this view's tag.
10790 *
10791 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010792 *
10793 * @see #setTag(Object)
10794 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010795 */
10796 @ViewDebug.ExportedProperty
10797 public Object getTag() {
10798 return mTag;
10799 }
10800
10801 /**
10802 * Sets the tag associated with this view. A tag can be used to mark
10803 * a view in its hierarchy and does not have to be unique within the
10804 * hierarchy. Tags can also be used to store data within a view without
10805 * resorting to another data structure.
10806 *
10807 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010808 *
10809 * @see #getTag()
10810 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010811 */
10812 public void setTag(final Object tag) {
10813 mTag = tag;
10814 }
10815
10816 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010817 * Returns the tag associated with this view and the specified key.
10818 *
10819 * @param key The key identifying the tag
10820 *
10821 * @return the Object stored in this view as a tag
10822 *
10823 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010824 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010825 */
10826 public Object getTag(int key) {
10827 SparseArray<Object> tags = null;
10828 synchronized (sTagsLock) {
10829 if (sTags != null) {
10830 tags = sTags.get(this);
10831 }
10832 }
10833
10834 if (tags != null) return tags.get(key);
10835 return null;
10836 }
10837
10838 /**
10839 * Sets a tag associated with this view and a key. A tag can be used
10840 * to mark a view in its hierarchy and does not have to be unique within
10841 * the hierarchy. Tags can also be used to store data within a view
10842 * without resorting to another data structure.
10843 *
10844 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010845 * application to ensure it is unique (see the <a
10846 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10847 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010848 * the Android framework or not associated with any package will cause
10849 * an {@link IllegalArgumentException} to be thrown.
10850 *
10851 * @param key The key identifying the tag
10852 * @param tag An Object to tag the view with
10853 *
10854 * @throws IllegalArgumentException If they specified key is not valid
10855 *
10856 * @see #setTag(Object)
10857 * @see #getTag(int)
10858 */
10859 public void setTag(int key, final Object tag) {
10860 // If the package id is 0x00 or 0x01, it's either an undefined package
10861 // or a framework id
10862 if ((key >>> 24) < 2) {
10863 throw new IllegalArgumentException("The key must be an application-specific "
10864 + "resource id.");
10865 }
10866
10867 setTagInternal(this, key, tag);
10868 }
10869
10870 /**
10871 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10872 * framework id.
10873 *
10874 * @hide
10875 */
10876 public void setTagInternal(int key, Object tag) {
10877 if ((key >>> 24) != 0x1) {
10878 throw new IllegalArgumentException("The key must be a framework-specific "
10879 + "resource id.");
10880 }
10881
Romain Guy8506ab42009-06-11 17:35:47 -070010882 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010883 }
10884
10885 private static void setTagInternal(View view, int key, Object tag) {
10886 SparseArray<Object> tags = null;
10887 synchronized (sTagsLock) {
10888 if (sTags == null) {
10889 sTags = new WeakHashMap<View, SparseArray<Object>>();
10890 } else {
10891 tags = sTags.get(view);
10892 }
10893 }
10894
10895 if (tags == null) {
10896 tags = new SparseArray<Object>(2);
10897 synchronized (sTagsLock) {
10898 sTags.put(view, tags);
10899 }
10900 }
10901
10902 tags.put(key, tag);
10903 }
10904
10905 /**
Romain Guy13922e02009-05-12 17:56:14 -070010906 * @param consistency The type of consistency. See ViewDebug for more information.
10907 *
10908 * @hide
10909 */
10910 protected boolean dispatchConsistencyCheck(int consistency) {
10911 return onConsistencyCheck(consistency);
10912 }
10913
10914 /**
10915 * Method that subclasses should implement to check their consistency. The type of
10916 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010917 *
Romain Guy13922e02009-05-12 17:56:14 -070010918 * @param consistency The type of consistency. See ViewDebug for more information.
10919 *
10920 * @throws IllegalStateException if the view is in an inconsistent state.
10921 *
10922 * @hide
10923 */
10924 protected boolean onConsistencyCheck(int consistency) {
10925 boolean result = true;
10926
10927 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10928 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10929
10930 if (checkLayout) {
10931 if (getParent() == null) {
10932 result = false;
10933 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10934 "View " + this + " does not have a parent.");
10935 }
10936
10937 if (mAttachInfo == null) {
10938 result = false;
10939 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10940 "View " + this + " is not attached to a window.");
10941 }
10942 }
10943
10944 if (checkDrawing) {
10945 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10946 // from their draw() method
10947
10948 if ((mPrivateFlags & DRAWN) != DRAWN &&
10949 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10950 result = false;
10951 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10952 "View " + this + " was invalidated but its drawing cache is valid.");
10953 }
10954 }
10955
10956 return result;
10957 }
10958
10959 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 * Prints information about this view in the log output, with the tag
10961 * {@link #VIEW_LOG_TAG}.
10962 *
10963 * @hide
10964 */
10965 public void debug() {
10966 debug(0);
10967 }
10968
10969 /**
10970 * Prints information about this view in the log output, with the tag
10971 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10972 * indentation defined by the <code>depth</code>.
10973 *
10974 * @param depth the indentation level
10975 *
10976 * @hide
10977 */
10978 protected void debug(int depth) {
10979 String output = debugIndent(depth - 1);
10980
10981 output += "+ " + this;
10982 int id = getId();
10983 if (id != -1) {
10984 output += " (id=" + id + ")";
10985 }
10986 Object tag = getTag();
10987 if (tag != null) {
10988 output += " (tag=" + tag + ")";
10989 }
10990 Log.d(VIEW_LOG_TAG, output);
10991
10992 if ((mPrivateFlags & FOCUSED) != 0) {
10993 output = debugIndent(depth) + " FOCUSED";
10994 Log.d(VIEW_LOG_TAG, output);
10995 }
10996
10997 output = debugIndent(depth);
10998 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10999 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11000 + "} ";
11001 Log.d(VIEW_LOG_TAG, output);
11002
11003 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11004 || mPaddingBottom != 0) {
11005 output = debugIndent(depth);
11006 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11007 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11008 Log.d(VIEW_LOG_TAG, output);
11009 }
11010
11011 output = debugIndent(depth);
11012 output += "mMeasureWidth=" + mMeasuredWidth +
11013 " mMeasureHeight=" + mMeasuredHeight;
11014 Log.d(VIEW_LOG_TAG, output);
11015
11016 output = debugIndent(depth);
11017 if (mLayoutParams == null) {
11018 output += "BAD! no layout params";
11019 } else {
11020 output = mLayoutParams.debug(output);
11021 }
11022 Log.d(VIEW_LOG_TAG, output);
11023
11024 output = debugIndent(depth);
11025 output += "flags={";
11026 output += View.printFlags(mViewFlags);
11027 output += "}";
11028 Log.d(VIEW_LOG_TAG, output);
11029
11030 output = debugIndent(depth);
11031 output += "privateFlags={";
11032 output += View.printPrivateFlags(mPrivateFlags);
11033 output += "}";
11034 Log.d(VIEW_LOG_TAG, output);
11035 }
11036
11037 /**
11038 * Creates an string of whitespaces used for indentation.
11039 *
11040 * @param depth the indentation level
11041 * @return a String containing (depth * 2 + 3) * 2 white spaces
11042 *
11043 * @hide
11044 */
11045 protected static String debugIndent(int depth) {
11046 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11047 for (int i = 0; i < (depth * 2) + 3; i++) {
11048 spaces.append(' ').append(' ');
11049 }
11050 return spaces.toString();
11051 }
11052
11053 /**
11054 * <p>Return the offset of the widget's text baseline from the widget's top
11055 * boundary. If this widget does not support baseline alignment, this
11056 * method returns -1. </p>
11057 *
11058 * @return the offset of the baseline within the widget's bounds or -1
11059 * if baseline alignment is not supported
11060 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011061 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011062 public int getBaseline() {
11063 return -1;
11064 }
11065
11066 /**
11067 * Call this when something has changed which has invalidated the
11068 * layout of this view. This will schedule a layout pass of the view
11069 * tree.
11070 */
11071 public void requestLayout() {
11072 if (ViewDebug.TRACE_HIERARCHY) {
11073 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11074 }
11075
11076 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011077 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011078
11079 if (mParent != null && !mParent.isLayoutRequested()) {
11080 mParent.requestLayout();
11081 }
11082 }
11083
11084 /**
11085 * Forces this view to be laid out during the next layout pass.
11086 * This method does not call requestLayout() or forceLayout()
11087 * on the parent.
11088 */
11089 public void forceLayout() {
11090 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011091 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011092 }
11093
11094 /**
11095 * <p>
11096 * This is called to find out how big a view should be. The parent
11097 * supplies constraint information in the width and height parameters.
11098 * </p>
11099 *
11100 * <p>
11101 * The actual mesurement work of a view is performed in
11102 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11103 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11104 * </p>
11105 *
11106 *
11107 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11108 * parent
11109 * @param heightMeasureSpec Vertical space requirements as imposed by the
11110 * parent
11111 *
11112 * @see #onMeasure(int, int)
11113 */
11114 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11115 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11116 widthMeasureSpec != mOldWidthMeasureSpec ||
11117 heightMeasureSpec != mOldHeightMeasureSpec) {
11118
11119 // first clears the measured dimension flag
11120 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11121
11122 if (ViewDebug.TRACE_HIERARCHY) {
11123 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11124 }
11125
11126 // measure ourselves, this should set the measured dimension flag back
11127 onMeasure(widthMeasureSpec, heightMeasureSpec);
11128
11129 // flag not set, setMeasuredDimension() was not invoked, we raise
11130 // an exception to warn the developer
11131 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11132 throw new IllegalStateException("onMeasure() did not set the"
11133 + " measured dimension by calling"
11134 + " setMeasuredDimension()");
11135 }
11136
11137 mPrivateFlags |= LAYOUT_REQUIRED;
11138 }
11139
11140 mOldWidthMeasureSpec = widthMeasureSpec;
11141 mOldHeightMeasureSpec = heightMeasureSpec;
11142 }
11143
11144 /**
11145 * <p>
11146 * Measure the view and its content to determine the measured width and the
11147 * measured height. This method is invoked by {@link #measure(int, int)} and
11148 * should be overriden by subclasses to provide accurate and efficient
11149 * measurement of their contents.
11150 * </p>
11151 *
11152 * <p>
11153 * <strong>CONTRACT:</strong> When overriding this method, you
11154 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11155 * measured width and height of this view. Failure to do so will trigger an
11156 * <code>IllegalStateException</code>, thrown by
11157 * {@link #measure(int, int)}. Calling the superclass'
11158 * {@link #onMeasure(int, int)} is a valid use.
11159 * </p>
11160 *
11161 * <p>
11162 * The base class implementation of measure defaults to the background size,
11163 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11164 * override {@link #onMeasure(int, int)} to provide better measurements of
11165 * their content.
11166 * </p>
11167 *
11168 * <p>
11169 * If this method is overridden, it is the subclass's responsibility to make
11170 * sure the measured height and width are at least the view's minimum height
11171 * and width ({@link #getSuggestedMinimumHeight()} and
11172 * {@link #getSuggestedMinimumWidth()}).
11173 * </p>
11174 *
11175 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11176 * The requirements are encoded with
11177 * {@link android.view.View.MeasureSpec}.
11178 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11179 * The requirements are encoded with
11180 * {@link android.view.View.MeasureSpec}.
11181 *
11182 * @see #getMeasuredWidth()
11183 * @see #getMeasuredHeight()
11184 * @see #setMeasuredDimension(int, int)
11185 * @see #getSuggestedMinimumHeight()
11186 * @see #getSuggestedMinimumWidth()
11187 * @see android.view.View.MeasureSpec#getMode(int)
11188 * @see android.view.View.MeasureSpec#getSize(int)
11189 */
11190 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11191 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11192 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11193 }
11194
11195 /**
11196 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11197 * measured width and measured height. Failing to do so will trigger an
11198 * exception at measurement time.</p>
11199 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011200 * @param measuredWidth The measured width of this view. May be a complex
11201 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11202 * {@link #MEASURED_STATE_TOO_SMALL}.
11203 * @param measuredHeight The measured height of this view. May be a complex
11204 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11205 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011206 */
11207 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11208 mMeasuredWidth = measuredWidth;
11209 mMeasuredHeight = measuredHeight;
11210
11211 mPrivateFlags |= MEASURED_DIMENSION_SET;
11212 }
11213
11214 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011215 * Merge two states as returned by {@link #getMeasuredState()}.
11216 * @param curState The current state as returned from a view or the result
11217 * of combining multiple views.
11218 * @param newState The new view state to combine.
11219 * @return Returns a new integer reflecting the combination of the two
11220 * states.
11221 */
11222 public static int combineMeasuredStates(int curState, int newState) {
11223 return curState | newState;
11224 }
11225
11226 /**
11227 * Version of {@link #resolveSizeAndState(int, int, int)}
11228 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11229 */
11230 public static int resolveSize(int size, int measureSpec) {
11231 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11232 }
11233
11234 /**
11235 * Utility to reconcile a desired size and state, with constraints imposed
11236 * by a MeasureSpec. Will take the desired size, unless a different size
11237 * is imposed by the constraints. The returned value is a compound integer,
11238 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11239 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11240 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011241 *
11242 * @param size How big the view wants to be
11243 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011244 * @return Size information bit mask as defined by
11245 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011246 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011247 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011248 int result = size;
11249 int specMode = MeasureSpec.getMode(measureSpec);
11250 int specSize = MeasureSpec.getSize(measureSpec);
11251 switch (specMode) {
11252 case MeasureSpec.UNSPECIFIED:
11253 result = size;
11254 break;
11255 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011256 if (specSize < size) {
11257 result = specSize | MEASURED_STATE_TOO_SMALL;
11258 } else {
11259 result = size;
11260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 break;
11262 case MeasureSpec.EXACTLY:
11263 result = specSize;
11264 break;
11265 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011266 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 }
11268
11269 /**
11270 * Utility to return a default size. Uses the supplied size if the
11271 * MeasureSpec imposed no contraints. Will get larger if allowed
11272 * by the MeasureSpec.
11273 *
11274 * @param size Default size for this view
11275 * @param measureSpec Constraints imposed by the parent
11276 * @return The size this view should be.
11277 */
11278 public static int getDefaultSize(int size, int measureSpec) {
11279 int result = size;
11280 int specMode = MeasureSpec.getMode(measureSpec);
11281 int specSize = MeasureSpec.getSize(measureSpec);
11282
11283 switch (specMode) {
11284 case MeasureSpec.UNSPECIFIED:
11285 result = size;
11286 break;
11287 case MeasureSpec.AT_MOST:
11288 case MeasureSpec.EXACTLY:
11289 result = specSize;
11290 break;
11291 }
11292 return result;
11293 }
11294
11295 /**
11296 * Returns the suggested minimum height that the view should use. This
11297 * returns the maximum of the view's minimum height
11298 * and the background's minimum height
11299 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11300 * <p>
11301 * When being used in {@link #onMeasure(int, int)}, the caller should still
11302 * ensure the returned height is within the requirements of the parent.
11303 *
11304 * @return The suggested minimum height of the view.
11305 */
11306 protected int getSuggestedMinimumHeight() {
11307 int suggestedMinHeight = mMinHeight;
11308
11309 if (mBGDrawable != null) {
11310 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11311 if (suggestedMinHeight < bgMinHeight) {
11312 suggestedMinHeight = bgMinHeight;
11313 }
11314 }
11315
11316 return suggestedMinHeight;
11317 }
11318
11319 /**
11320 * Returns the suggested minimum width that the view should use. This
11321 * returns the maximum of the view's minimum width)
11322 * and the background's minimum width
11323 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11324 * <p>
11325 * When being used in {@link #onMeasure(int, int)}, the caller should still
11326 * ensure the returned width is within the requirements of the parent.
11327 *
11328 * @return The suggested minimum width of the view.
11329 */
11330 protected int getSuggestedMinimumWidth() {
11331 int suggestedMinWidth = mMinWidth;
11332
11333 if (mBGDrawable != null) {
11334 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11335 if (suggestedMinWidth < bgMinWidth) {
11336 suggestedMinWidth = bgMinWidth;
11337 }
11338 }
11339
11340 return suggestedMinWidth;
11341 }
11342
11343 /**
11344 * Sets the minimum height of the view. It is not guaranteed the view will
11345 * be able to achieve this minimum height (for example, if its parent layout
11346 * constrains it with less available height).
11347 *
11348 * @param minHeight The minimum height the view will try to be.
11349 */
11350 public void setMinimumHeight(int minHeight) {
11351 mMinHeight = minHeight;
11352 }
11353
11354 /**
11355 * Sets the minimum width of the view. It is not guaranteed the view will
11356 * be able to achieve this minimum width (for example, if its parent layout
11357 * constrains it with less available width).
11358 *
11359 * @param minWidth The minimum width the view will try to be.
11360 */
11361 public void setMinimumWidth(int minWidth) {
11362 mMinWidth = minWidth;
11363 }
11364
11365 /**
11366 * Get the animation currently associated with this view.
11367 *
11368 * @return The animation that is currently playing or
11369 * scheduled to play for this view.
11370 */
11371 public Animation getAnimation() {
11372 return mCurrentAnimation;
11373 }
11374
11375 /**
11376 * Start the specified animation now.
11377 *
11378 * @param animation the animation to start now
11379 */
11380 public void startAnimation(Animation animation) {
11381 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11382 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011383 invalidateParentCaches();
11384 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011385 }
11386
11387 /**
11388 * Cancels any animations for this view.
11389 */
11390 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011391 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011392 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011393 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011395 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 }
11397
11398 /**
11399 * Sets the next animation to play for this view.
11400 * If you want the animation to play immediately, use
11401 * startAnimation. This method provides allows fine-grained
11402 * control over the start time and invalidation, but you
11403 * must make sure that 1) the animation has a start time set, and
11404 * 2) the view will be invalidated when the animation is supposed to
11405 * start.
11406 *
11407 * @param animation The next animation, or null.
11408 */
11409 public void setAnimation(Animation animation) {
11410 mCurrentAnimation = animation;
11411 if (animation != null) {
11412 animation.reset();
11413 }
11414 }
11415
11416 /**
11417 * Invoked by a parent ViewGroup to notify the start of the animation
11418 * currently associated with this view. If you override this method,
11419 * always call super.onAnimationStart();
11420 *
11421 * @see #setAnimation(android.view.animation.Animation)
11422 * @see #getAnimation()
11423 */
11424 protected void onAnimationStart() {
11425 mPrivateFlags |= ANIMATION_STARTED;
11426 }
11427
11428 /**
11429 * Invoked by a parent ViewGroup to notify the end of the animation
11430 * currently associated with this view. If you override this method,
11431 * always call super.onAnimationEnd();
11432 *
11433 * @see #setAnimation(android.view.animation.Animation)
11434 * @see #getAnimation()
11435 */
11436 protected void onAnimationEnd() {
11437 mPrivateFlags &= ~ANIMATION_STARTED;
11438 }
11439
11440 /**
11441 * Invoked if there is a Transform that involves alpha. Subclass that can
11442 * draw themselves with the specified alpha should return true, and then
11443 * respect that alpha when their onDraw() is called. If this returns false
11444 * then the view may be redirected to draw into an offscreen buffer to
11445 * fulfill the request, which will look fine, but may be slower than if the
11446 * subclass handles it internally. The default implementation returns false.
11447 *
11448 * @param alpha The alpha (0..255) to apply to the view's drawing
11449 * @return true if the view can draw with the specified alpha.
11450 */
11451 protected boolean onSetAlpha(int alpha) {
11452 return false;
11453 }
11454
11455 /**
11456 * This is used by the RootView to perform an optimization when
11457 * the view hierarchy contains one or several SurfaceView.
11458 * SurfaceView is always considered transparent, but its children are not,
11459 * therefore all View objects remove themselves from the global transparent
11460 * region (passed as a parameter to this function).
11461 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011462 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 *
11464 * @return Returns true if the effective visibility of the view at this
11465 * point is opaque, regardless of the transparent region; returns false
11466 * if it is possible for underlying windows to be seen behind the view.
11467 *
11468 * {@hide}
11469 */
11470 public boolean gatherTransparentRegion(Region region) {
11471 final AttachInfo attachInfo = mAttachInfo;
11472 if (region != null && attachInfo != null) {
11473 final int pflags = mPrivateFlags;
11474 if ((pflags & SKIP_DRAW) == 0) {
11475 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11476 // remove it from the transparent region.
11477 final int[] location = attachInfo.mTransparentLocation;
11478 getLocationInWindow(location);
11479 region.op(location[0], location[1], location[0] + mRight - mLeft,
11480 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11481 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11482 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11483 // exists, so we remove the background drawable's non-transparent
11484 // parts from this transparent region.
11485 applyDrawableToTransparentRegion(mBGDrawable, region);
11486 }
11487 }
11488 return true;
11489 }
11490
11491 /**
11492 * Play a sound effect for this view.
11493 *
11494 * <p>The framework will play sound effects for some built in actions, such as
11495 * clicking, but you may wish to play these effects in your widget,
11496 * for instance, for internal navigation.
11497 *
11498 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11499 * {@link #isSoundEffectsEnabled()} is true.
11500 *
11501 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11502 */
11503 public void playSoundEffect(int soundConstant) {
11504 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11505 return;
11506 }
11507 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11508 }
11509
11510 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011511 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011512 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011513 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 *
11515 * <p>The framework will provide haptic feedback for some built in actions,
11516 * such as long presses, but you may wish to provide feedback for your
11517 * own widget.
11518 *
11519 * <p>The feedback will only be performed if
11520 * {@link #isHapticFeedbackEnabled()} is true.
11521 *
11522 * @param feedbackConstant One of the constants defined in
11523 * {@link HapticFeedbackConstants}
11524 */
11525 public boolean performHapticFeedback(int feedbackConstant) {
11526 return performHapticFeedback(feedbackConstant, 0);
11527 }
11528
11529 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011530 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011531 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011532 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011533 *
11534 * @param feedbackConstant One of the constants defined in
11535 * {@link HapticFeedbackConstants}
11536 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11537 */
11538 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11539 if (mAttachInfo == null) {
11540 return false;
11541 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011542 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011543 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011544 && !isHapticFeedbackEnabled()) {
11545 return false;
11546 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011547 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11548 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549 }
11550
11551 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011552 * Request that the visibility of the status bar be changed.
11553 */
11554 public void setSystemUiVisibility(int visibility) {
11555 if (visibility != mSystemUiVisibility) {
11556 mSystemUiVisibility = visibility;
11557 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11558 mParent.recomputeViewAttributes(this);
11559 }
11560 }
11561 }
11562
11563 /**
11564 * Returns the status bar visibility that this view has requested.
11565 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011566 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011567 return mSystemUiVisibility;
11568 }
11569
11570 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11571 mOnSystemUiVisibilityChangeListener = l;
11572 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11573 mParent.recomputeViewAttributes(this);
11574 }
11575 }
11576
11577 /**
11578 */
11579 public void dispatchSystemUiVisibilityChanged(int visibility) {
11580 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011581 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011582 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011583 }
11584 }
11585
11586 /**
Joe Malin32736f02011-01-19 16:14:20 -080011587 * Creates an image that the system displays during the drag and drop
11588 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11589 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11590 * appearance as the given View. The default also positions the center of the drag shadow
11591 * directly under the touch point. If no View is provided (the constructor with no parameters
11592 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11593 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11594 * default is an invisible drag shadow.
11595 * <p>
11596 * You are not required to use the View you provide to the constructor as the basis of the
11597 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11598 * anything you want as the drag shadow.
11599 * </p>
11600 * <p>
11601 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11602 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11603 * size and position of the drag shadow. It uses this data to construct a
11604 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11605 * so that your application can draw the shadow image in the Canvas.
11606 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011607 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011608 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011609 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011610
11611 /**
Joe Malin32736f02011-01-19 16:14:20 -080011612 * Constructs a shadow image builder based on a View. By default, the resulting drag
11613 * shadow will have the same appearance and dimensions as the View, with the touch point
11614 * over the center of the View.
11615 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011616 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011617 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011618 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011619 }
11620
Christopher Tate17ed60c2011-01-18 12:50:26 -080011621 /**
11622 * Construct a shadow builder object with no associated View. This
11623 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11624 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11625 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011626 * reference to any View object. If they are not overridden, then the result is an
11627 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011628 */
11629 public DragShadowBuilder() {
11630 mView = new WeakReference<View>(null);
11631 }
11632
11633 /**
11634 * Returns the View object that had been passed to the
11635 * {@link #View.DragShadowBuilder(View)}
11636 * constructor. If that View parameter was {@code null} or if the
11637 * {@link #View.DragShadowBuilder()}
11638 * constructor was used to instantiate the builder object, this method will return
11639 * null.
11640 *
11641 * @return The View object associate with this builder object.
11642 */
Chris Tate6b391282010-10-14 15:48:59 -070011643 final public View getView() {
11644 return mView.get();
11645 }
11646
Christopher Tate2c095f32010-10-04 14:13:40 -070011647 /**
Joe Malin32736f02011-01-19 16:14:20 -080011648 * Provides the metrics for the shadow image. These include the dimensions of
11649 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011650 * be centered under the touch location while dragging.
11651 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011652 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011653 * same as the dimensions of the View itself and centers the shadow under
11654 * the touch point.
11655 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011656 *
Joe Malin32736f02011-01-19 16:14:20 -080011657 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11658 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11659 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11660 * image.
11661 *
11662 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11663 * shadow image that should be underneath the touch point during the drag and drop
11664 * operation. Your application must set {@link android.graphics.Point#x} to the
11665 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011666 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011667 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011668 final View view = mView.get();
11669 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011670 shadowSize.set(view.getWidth(), view.getHeight());
11671 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011672 } else {
11673 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11674 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011675 }
11676
11677 /**
Joe Malin32736f02011-01-19 16:14:20 -080011678 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11679 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011680 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011681 *
Joe Malin32736f02011-01-19 16:14:20 -080011682 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011683 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011684 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011685 final View view = mView.get();
11686 if (view != null) {
11687 view.draw(canvas);
11688 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011689 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011690 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011691 }
11692 }
11693
11694 /**
Joe Malin32736f02011-01-19 16:14:20 -080011695 * Starts a drag and drop operation. When your application calls this method, it passes a
11696 * {@link android.view.View.DragShadowBuilder} object to the system. The
11697 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11698 * to get metrics for the drag shadow, and then calls the object's
11699 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11700 * <p>
11701 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11702 * drag events to all the View objects in your application that are currently visible. It does
11703 * this either by calling the View object's drag listener (an implementation of
11704 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11705 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11706 * Both are passed a {@link android.view.DragEvent} object that has a
11707 * {@link android.view.DragEvent#getAction()} value of
11708 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11709 * </p>
11710 * <p>
11711 * Your application can invoke startDrag() on any attached View object. The View object does not
11712 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11713 * be related to the View the user selected for dragging.
11714 * </p>
11715 * @param data A {@link android.content.ClipData} object pointing to the data to be
11716 * transferred by the drag and drop operation.
11717 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11718 * drag shadow.
11719 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11720 * drop operation. This Object is put into every DragEvent object sent by the system during the
11721 * current drag.
11722 * <p>
11723 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11724 * to the target Views. For example, it can contain flags that differentiate between a
11725 * a copy operation and a move operation.
11726 * </p>
11727 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11728 * so the parameter should be set to 0.
11729 * @return {@code true} if the method completes successfully, or
11730 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11731 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011732 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011733 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011734 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011735 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011736 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011737 }
11738 boolean okay = false;
11739
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011740 Point shadowSize = new Point();
11741 Point shadowTouchPoint = new Point();
11742 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011743
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011744 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11745 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11746 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011747 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011748
Chris Tatea32dcf72010-10-14 12:13:50 -070011749 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011750 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11751 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011752 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011753 Surface surface = new Surface();
11754 try {
11755 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011756 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011757 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011758 + " surface=" + surface);
11759 if (token != null) {
11760 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011761 try {
Chris Tate6b391282010-10-14 15:48:59 -070011762 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011763 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011764 } finally {
11765 surface.unlockCanvasAndPost(canvas);
11766 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011767
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011768 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080011769
11770 // Cache the local state object for delivery with DragEvents
11771 root.setLocalDragState(myLocalState);
11772
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011773 // repurpose 'shadowSize' for the last touch point
11774 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011775
Christopher Tatea53146c2010-09-07 11:57:52 -070011776 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011777 shadowSize.x, shadowSize.y,
11778 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011779 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011780 }
11781 } catch (Exception e) {
11782 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11783 surface.destroy();
11784 }
11785
11786 return okay;
11787 }
11788
Christopher Tatea53146c2010-09-07 11:57:52 -070011789 /**
Joe Malin32736f02011-01-19 16:14:20 -080011790 * Handles drag events sent by the system following a call to
11791 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11792 *<p>
11793 * When the system calls this method, it passes a
11794 * {@link android.view.DragEvent} object. A call to
11795 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11796 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11797 * operation.
11798 * @param event The {@link android.view.DragEvent} sent by the system.
11799 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11800 * in DragEvent, indicating the type of drag event represented by this object.
11801 * @return {@code true} if the method was successful, otherwise {@code false}.
11802 * <p>
11803 * The method should return {@code true} in response to an action type of
11804 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11805 * operation.
11806 * </p>
11807 * <p>
11808 * The method should also return {@code true} in response to an action type of
11809 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11810 * {@code false} if it didn't.
11811 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011812 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011813 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011814 return false;
11815 }
11816
11817 /**
Joe Malin32736f02011-01-19 16:14:20 -080011818 * Detects if this View is enabled and has a drag event listener.
11819 * If both are true, then it calls the drag event listener with the
11820 * {@link android.view.DragEvent} it received. If the drag event listener returns
11821 * {@code true}, then dispatchDragEvent() returns {@code true}.
11822 * <p>
11823 * For all other cases, the method calls the
11824 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11825 * method and returns its result.
11826 * </p>
11827 * <p>
11828 * This ensures that a drag event is always consumed, even if the View does not have a drag
11829 * event listener. However, if the View has a listener and the listener returns true, then
11830 * onDragEvent() is not called.
11831 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011832 */
11833 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011834 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011835 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11836 && mOnDragListener.onDrag(this, event)) {
11837 return true;
11838 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011839 return onDragEvent(event);
11840 }
11841
Christopher Tate3d4bf172011-03-28 16:16:46 -070011842 boolean canAcceptDrag() {
11843 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11844 }
11845
Christopher Tatea53146c2010-09-07 11:57:52 -070011846 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011847 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11848 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011849 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011850 */
11851 public void onCloseSystemDialogs(String reason) {
11852 }
Joe Malin32736f02011-01-19 16:14:20 -080011853
Dianne Hackbornffa42482009-09-23 22:20:11 -070011854 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011855 * Given a Drawable whose bounds have been set to draw into this view,
11856 * update a Region being computed for {@link #gatherTransparentRegion} so
11857 * that any non-transparent parts of the Drawable are removed from the
11858 * given transparent region.
11859 *
11860 * @param dr The Drawable whose transparency is to be applied to the region.
11861 * @param region A Region holding the current transparency information,
11862 * where any parts of the region that are set are considered to be
11863 * transparent. On return, this region will be modified to have the
11864 * transparency information reduced by the corresponding parts of the
11865 * Drawable that are not transparent.
11866 * {@hide}
11867 */
11868 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11869 if (DBG) {
11870 Log.i("View", "Getting transparent region for: " + this);
11871 }
11872 final Region r = dr.getTransparentRegion();
11873 final Rect db = dr.getBounds();
11874 final AttachInfo attachInfo = mAttachInfo;
11875 if (r != null && attachInfo != null) {
11876 final int w = getRight()-getLeft();
11877 final int h = getBottom()-getTop();
11878 if (db.left > 0) {
11879 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11880 r.op(0, 0, db.left, h, Region.Op.UNION);
11881 }
11882 if (db.right < w) {
11883 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11884 r.op(db.right, 0, w, h, Region.Op.UNION);
11885 }
11886 if (db.top > 0) {
11887 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11888 r.op(0, 0, w, db.top, Region.Op.UNION);
11889 }
11890 if (db.bottom < h) {
11891 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11892 r.op(0, db.bottom, w, h, Region.Op.UNION);
11893 }
11894 final int[] location = attachInfo.mTransparentLocation;
11895 getLocationInWindow(location);
11896 r.translate(location[0], location[1]);
11897 region.op(r, Region.Op.INTERSECT);
11898 } else {
11899 region.op(db, Region.Op.DIFFERENCE);
11900 }
11901 }
11902
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011903 private void checkForLongClick(int delayOffset) {
11904 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11905 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011906
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011907 if (mPendingCheckForLongPress == null) {
11908 mPendingCheckForLongPress = new CheckForLongPress();
11909 }
11910 mPendingCheckForLongPress.rememberWindowAttachCount();
11911 postDelayed(mPendingCheckForLongPress,
11912 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011914 }
11915
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011916 /**
11917 * Inflate a view from an XML resource. This convenience method wraps the {@link
11918 * LayoutInflater} class, which provides a full range of options for view inflation.
11919 *
11920 * @param context The Context object for your activity or application.
11921 * @param resource The resource ID to inflate
11922 * @param root A view group that will be the parent. Used to properly inflate the
11923 * layout_* parameters.
11924 * @see LayoutInflater
11925 */
11926 public static View inflate(Context context, int resource, ViewGroup root) {
11927 LayoutInflater factory = LayoutInflater.from(context);
11928 return factory.inflate(resource, root);
11929 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011931 /**
Adam Powell637d3372010-08-25 14:37:03 -070011932 * Scroll the view with standard behavior for scrolling beyond the normal
11933 * content boundaries. Views that call this method should override
11934 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11935 * results of an over-scroll operation.
11936 *
11937 * Views can use this method to handle any touch or fling-based scrolling.
11938 *
11939 * @param deltaX Change in X in pixels
11940 * @param deltaY Change in Y in pixels
11941 * @param scrollX Current X scroll value in pixels before applying deltaX
11942 * @param scrollY Current Y scroll value in pixels before applying deltaY
11943 * @param scrollRangeX Maximum content scroll range along the X axis
11944 * @param scrollRangeY Maximum content scroll range along the Y axis
11945 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11946 * along the X axis.
11947 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11948 * along the Y axis.
11949 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11950 * @return true if scrolling was clamped to an over-scroll boundary along either
11951 * axis, false otherwise.
11952 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011953 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011954 protected boolean overScrollBy(int deltaX, int deltaY,
11955 int scrollX, int scrollY,
11956 int scrollRangeX, int scrollRangeY,
11957 int maxOverScrollX, int maxOverScrollY,
11958 boolean isTouchEvent) {
11959 final int overScrollMode = mOverScrollMode;
11960 final boolean canScrollHorizontal =
11961 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11962 final boolean canScrollVertical =
11963 computeVerticalScrollRange() > computeVerticalScrollExtent();
11964 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11965 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11966 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11967 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11968
11969 int newScrollX = scrollX + deltaX;
11970 if (!overScrollHorizontal) {
11971 maxOverScrollX = 0;
11972 }
11973
11974 int newScrollY = scrollY + deltaY;
11975 if (!overScrollVertical) {
11976 maxOverScrollY = 0;
11977 }
11978
11979 // Clamp values if at the limits and record
11980 final int left = -maxOverScrollX;
11981 final int right = maxOverScrollX + scrollRangeX;
11982 final int top = -maxOverScrollY;
11983 final int bottom = maxOverScrollY + scrollRangeY;
11984
11985 boolean clampedX = false;
11986 if (newScrollX > right) {
11987 newScrollX = right;
11988 clampedX = true;
11989 } else if (newScrollX < left) {
11990 newScrollX = left;
11991 clampedX = true;
11992 }
11993
11994 boolean clampedY = false;
11995 if (newScrollY > bottom) {
11996 newScrollY = bottom;
11997 clampedY = true;
11998 } else if (newScrollY < top) {
11999 newScrollY = top;
12000 clampedY = true;
12001 }
12002
12003 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12004
12005 return clampedX || clampedY;
12006 }
12007
12008 /**
12009 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12010 * respond to the results of an over-scroll operation.
12011 *
12012 * @param scrollX New X scroll value in pixels
12013 * @param scrollY New Y scroll value in pixels
12014 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12015 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12016 */
12017 protected void onOverScrolled(int scrollX, int scrollY,
12018 boolean clampedX, boolean clampedY) {
12019 // Intentionally empty.
12020 }
12021
12022 /**
12023 * Returns the over-scroll mode for this view. The result will be
12024 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12025 * (allow over-scrolling only if the view content is larger than the container),
12026 * or {@link #OVER_SCROLL_NEVER}.
12027 *
12028 * @return This view's over-scroll mode.
12029 */
12030 public int getOverScrollMode() {
12031 return mOverScrollMode;
12032 }
12033
12034 /**
12035 * Set the over-scroll mode for this view. Valid over-scroll modes are
12036 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12037 * (allow over-scrolling only if the view content is larger than the container),
12038 * or {@link #OVER_SCROLL_NEVER}.
12039 *
12040 * Setting the over-scroll mode of a view will have an effect only if the
12041 * view is capable of scrolling.
12042 *
12043 * @param overScrollMode The new over-scroll mode for this view.
12044 */
12045 public void setOverScrollMode(int overScrollMode) {
12046 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12047 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12048 overScrollMode != OVER_SCROLL_NEVER) {
12049 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12050 }
12051 mOverScrollMode = overScrollMode;
12052 }
12053
12054 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012055 * Gets a scale factor that determines the distance the view should scroll
12056 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12057 * @return The vertical scroll scale factor.
12058 * @hide
12059 */
12060 protected float getVerticalScrollFactor() {
12061 if (mVerticalScrollFactor == 0) {
12062 TypedValue outValue = new TypedValue();
12063 if (!mContext.getTheme().resolveAttribute(
12064 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12065 throw new IllegalStateException(
12066 "Expected theme to define listPreferredItemHeight.");
12067 }
12068 mVerticalScrollFactor = outValue.getDimension(
12069 mContext.getResources().getDisplayMetrics());
12070 }
12071 return mVerticalScrollFactor;
12072 }
12073
12074 /**
12075 * Gets a scale factor that determines the distance the view should scroll
12076 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12077 * @return The horizontal scroll scale factor.
12078 * @hide
12079 */
12080 protected float getHorizontalScrollFactor() {
12081 // TODO: Should use something else.
12082 return getVerticalScrollFactor();
12083 }
12084
12085 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012086 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12087 * Each MeasureSpec represents a requirement for either the width or the height.
12088 * A MeasureSpec is comprised of a size and a mode. There are three possible
12089 * modes:
12090 * <dl>
12091 * <dt>UNSPECIFIED</dt>
12092 * <dd>
12093 * The parent has not imposed any constraint on the child. It can be whatever size
12094 * it wants.
12095 * </dd>
12096 *
12097 * <dt>EXACTLY</dt>
12098 * <dd>
12099 * The parent has determined an exact size for the child. The child is going to be
12100 * given those bounds regardless of how big it wants to be.
12101 * </dd>
12102 *
12103 * <dt>AT_MOST</dt>
12104 * <dd>
12105 * The child can be as large as it wants up to the specified size.
12106 * </dd>
12107 * </dl>
12108 *
12109 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12110 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12111 */
12112 public static class MeasureSpec {
12113 private static final int MODE_SHIFT = 30;
12114 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12115
12116 /**
12117 * Measure specification mode: The parent has not imposed any constraint
12118 * on the child. It can be whatever size it wants.
12119 */
12120 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12121
12122 /**
12123 * Measure specification mode: The parent has determined an exact size
12124 * for the child. The child is going to be given those bounds regardless
12125 * of how big it wants to be.
12126 */
12127 public static final int EXACTLY = 1 << MODE_SHIFT;
12128
12129 /**
12130 * Measure specification mode: The child can be as large as it wants up
12131 * to the specified size.
12132 */
12133 public static final int AT_MOST = 2 << MODE_SHIFT;
12134
12135 /**
12136 * Creates a measure specification based on the supplied size and mode.
12137 *
12138 * The mode must always be one of the following:
12139 * <ul>
12140 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12141 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12142 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12143 * </ul>
12144 *
12145 * @param size the size of the measure specification
12146 * @param mode the mode of the measure specification
12147 * @return the measure specification based on size and mode
12148 */
12149 public static int makeMeasureSpec(int size, int mode) {
12150 return size + mode;
12151 }
12152
12153 /**
12154 * Extracts the mode from the supplied measure specification.
12155 *
12156 * @param measureSpec the measure specification to extract the mode from
12157 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12158 * {@link android.view.View.MeasureSpec#AT_MOST} or
12159 * {@link android.view.View.MeasureSpec#EXACTLY}
12160 */
12161 public static int getMode(int measureSpec) {
12162 return (measureSpec & MODE_MASK);
12163 }
12164
12165 /**
12166 * Extracts the size from the supplied measure specification.
12167 *
12168 * @param measureSpec the measure specification to extract the size from
12169 * @return the size in pixels defined in the supplied measure specification
12170 */
12171 public static int getSize(int measureSpec) {
12172 return (measureSpec & ~MODE_MASK);
12173 }
12174
12175 /**
12176 * Returns a String representation of the specified measure
12177 * specification.
12178 *
12179 * @param measureSpec the measure specification to convert to a String
12180 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12181 */
12182 public static String toString(int measureSpec) {
12183 int mode = getMode(measureSpec);
12184 int size = getSize(measureSpec);
12185
12186 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12187
12188 if (mode == UNSPECIFIED)
12189 sb.append("UNSPECIFIED ");
12190 else if (mode == EXACTLY)
12191 sb.append("EXACTLY ");
12192 else if (mode == AT_MOST)
12193 sb.append("AT_MOST ");
12194 else
12195 sb.append(mode).append(" ");
12196
12197 sb.append(size);
12198 return sb.toString();
12199 }
12200 }
12201
12202 class CheckForLongPress implements Runnable {
12203
12204 private int mOriginalWindowAttachCount;
12205
12206 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012207 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012208 && mOriginalWindowAttachCount == mWindowAttachCount) {
12209 if (performLongClick()) {
12210 mHasPerformedLongPress = true;
12211 }
12212 }
12213 }
12214
12215 public void rememberWindowAttachCount() {
12216 mOriginalWindowAttachCount = mWindowAttachCount;
12217 }
12218 }
Joe Malin32736f02011-01-19 16:14:20 -080012219
Adam Powelle14579b2009-12-16 18:39:52 -080012220 private final class CheckForTap implements Runnable {
12221 public void run() {
12222 mPrivateFlags &= ~PREPRESSED;
12223 mPrivateFlags |= PRESSED;
12224 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012225 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012226 }
12227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228
Adam Powella35d7682010-03-12 14:48:13 -080012229 private final class PerformClick implements Runnable {
12230 public void run() {
12231 performClick();
12232 }
12233 }
12234
Dianne Hackborn63042d62011-01-26 18:56:29 -080012235 /** @hide */
12236 public void hackTurnOffWindowResizeAnim(boolean off) {
12237 mAttachInfo.mTurnOffWindowResizeAnim = off;
12238 }
Joe Malin32736f02011-01-19 16:14:20 -080012239
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012240 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012241 * This method returns a ViewPropertyAnimator object, which can be used to animate
12242 * specific properties on this View.
12243 *
12244 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12245 */
12246 public ViewPropertyAnimator animate() {
12247 if (mAnimator == null) {
12248 mAnimator = new ViewPropertyAnimator(this);
12249 }
12250 return mAnimator;
12251 }
12252
12253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012254 * Interface definition for a callback to be invoked when a key event is
12255 * dispatched to this view. The callback will be invoked before the key
12256 * event is given to the view.
12257 */
12258 public interface OnKeyListener {
12259 /**
12260 * Called when a key is dispatched to a view. This allows listeners to
12261 * get a chance to respond before the target view.
12262 *
12263 * @param v The view the key has been dispatched to.
12264 * @param keyCode The code for the physical key that was pressed
12265 * @param event The KeyEvent object containing full information about
12266 * the event.
12267 * @return True if the listener has consumed the event, false otherwise.
12268 */
12269 boolean onKey(View v, int keyCode, KeyEvent event);
12270 }
12271
12272 /**
12273 * Interface definition for a callback to be invoked when a touch event is
12274 * dispatched to this view. The callback will be invoked before the touch
12275 * event is given to the view.
12276 */
12277 public interface OnTouchListener {
12278 /**
12279 * Called when a touch event is dispatched to a view. This allows listeners to
12280 * get a chance to respond before the target view.
12281 *
12282 * @param v The view the touch event has been dispatched to.
12283 * @param event The MotionEvent object containing full information about
12284 * the event.
12285 * @return True if the listener has consumed the event, false otherwise.
12286 */
12287 boolean onTouch(View v, MotionEvent event);
12288 }
12289
12290 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012291 * Interface definition for a callback to be invoked when a generic motion event is
12292 * dispatched to this view. The callback will be invoked before the generic motion
12293 * event is given to the view.
12294 */
12295 public interface OnGenericMotionListener {
12296 /**
12297 * Called when a generic motion event is dispatched to a view. This allows listeners to
12298 * get a chance to respond before the target view.
12299 *
12300 * @param v The view the generic motion event has been dispatched to.
12301 * @param event The MotionEvent object containing full information about
12302 * the event.
12303 * @return True if the listener has consumed the event, false otherwise.
12304 */
12305 boolean onGenericMotion(View v, MotionEvent event);
12306 }
12307
12308 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012309 * Interface definition for a callback to be invoked when a view has been clicked and held.
12310 */
12311 public interface OnLongClickListener {
12312 /**
12313 * Called when a view has been clicked and held.
12314 *
12315 * @param v The view that was clicked and held.
12316 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012317 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012318 */
12319 boolean onLongClick(View v);
12320 }
12321
12322 /**
Chris Tate32affef2010-10-18 15:29:21 -070012323 * Interface definition for a callback to be invoked when a drag is being dispatched
12324 * to this view. The callback will be invoked before the hosting view's own
12325 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12326 * onDrag(event) behavior, it should return 'false' from this callback.
12327 */
12328 public interface OnDragListener {
12329 /**
12330 * Called when a drag event is dispatched to a view. This allows listeners
12331 * to get a chance to override base View behavior.
12332 *
Joe Malin32736f02011-01-19 16:14:20 -080012333 * @param v The View that received the drag event.
12334 * @param event The {@link android.view.DragEvent} object for the drag event.
12335 * @return {@code true} if the drag event was handled successfully, or {@code false}
12336 * if the drag event was not handled. Note that {@code false} will trigger the View
12337 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012338 */
12339 boolean onDrag(View v, DragEvent event);
12340 }
12341
12342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012343 * Interface definition for a callback to be invoked when the focus state of
12344 * a view changed.
12345 */
12346 public interface OnFocusChangeListener {
12347 /**
12348 * Called when the focus state of a view has changed.
12349 *
12350 * @param v The view whose state has changed.
12351 * @param hasFocus The new focus state of v.
12352 */
12353 void onFocusChange(View v, boolean hasFocus);
12354 }
12355
12356 /**
12357 * Interface definition for a callback to be invoked when a view is clicked.
12358 */
12359 public interface OnClickListener {
12360 /**
12361 * Called when a view has been clicked.
12362 *
12363 * @param v The view that was clicked.
12364 */
12365 void onClick(View v);
12366 }
12367
12368 /**
12369 * Interface definition for a callback to be invoked when the context menu
12370 * for this view is being built.
12371 */
12372 public interface OnCreateContextMenuListener {
12373 /**
12374 * Called when the context menu for this view is being built. It is not
12375 * safe to hold onto the menu after this method returns.
12376 *
12377 * @param menu The context menu that is being built
12378 * @param v The view for which the context menu is being built
12379 * @param menuInfo Extra information about the item for which the
12380 * context menu should be shown. This information will vary
12381 * depending on the class of v.
12382 */
12383 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12384 }
12385
Joe Onorato664644d2011-01-23 17:53:23 -080012386 /**
12387 * Interface definition for a callback to be invoked when the status bar changes
12388 * visibility.
12389 *
12390 * @see #setOnSystemUiVisibilityChangeListener
12391 */
12392 public interface OnSystemUiVisibilityChangeListener {
12393 /**
12394 * Called when the status bar changes visibility because of a call to
12395 * {@link #setSystemUiVisibility}.
12396 *
12397 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12398 */
12399 public void onSystemUiVisibilityChange(int visibility);
12400 }
12401
Adam Powell4afd62b2011-02-18 15:02:18 -080012402 /**
12403 * Interface definition for a callback to be invoked when this view is attached
12404 * or detached from its window.
12405 */
12406 public interface OnAttachStateChangeListener {
12407 /**
12408 * Called when the view is attached to a window.
12409 * @param v The view that was attached
12410 */
12411 public void onViewAttachedToWindow(View v);
12412 /**
12413 * Called when the view is detached from a window.
12414 * @param v The view that was detached
12415 */
12416 public void onViewDetachedFromWindow(View v);
12417 }
12418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012419 private final class UnsetPressedState implements Runnable {
12420 public void run() {
12421 setPressed(false);
12422 }
12423 }
12424
12425 /**
12426 * Base class for derived classes that want to save and restore their own
12427 * state in {@link android.view.View#onSaveInstanceState()}.
12428 */
12429 public static class BaseSavedState extends AbsSavedState {
12430 /**
12431 * Constructor used when reading from a parcel. Reads the state of the superclass.
12432 *
12433 * @param source
12434 */
12435 public BaseSavedState(Parcel source) {
12436 super(source);
12437 }
12438
12439 /**
12440 * Constructor called by derived classes when creating their SavedState objects
12441 *
12442 * @param superState The state of the superclass of this view
12443 */
12444 public BaseSavedState(Parcelable superState) {
12445 super(superState);
12446 }
12447
12448 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12449 new Parcelable.Creator<BaseSavedState>() {
12450 public BaseSavedState createFromParcel(Parcel in) {
12451 return new BaseSavedState(in);
12452 }
12453
12454 public BaseSavedState[] newArray(int size) {
12455 return new BaseSavedState[size];
12456 }
12457 };
12458 }
12459
12460 /**
12461 * A set of information given to a view when it is attached to its parent
12462 * window.
12463 */
12464 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012465 interface Callbacks {
12466 void playSoundEffect(int effectId);
12467 boolean performHapticFeedback(int effectId, boolean always);
12468 }
12469
12470 /**
12471 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12472 * to a Handler. This class contains the target (View) to invalidate and
12473 * the coordinates of the dirty rectangle.
12474 *
12475 * For performance purposes, this class also implements a pool of up to
12476 * POOL_LIMIT objects that get reused. This reduces memory allocations
12477 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012478 */
Romain Guyd928d682009-03-31 17:52:16 -070012479 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012480 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012481 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12482 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012483 public InvalidateInfo newInstance() {
12484 return new InvalidateInfo();
12485 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012486
Romain Guyd928d682009-03-31 17:52:16 -070012487 public void onAcquired(InvalidateInfo element) {
12488 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012489
Romain Guyd928d682009-03-31 17:52:16 -070012490 public void onReleased(InvalidateInfo element) {
12491 }
12492 }, POOL_LIMIT)
12493 );
12494
12495 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012496
12497 View target;
12498
12499 int left;
12500 int top;
12501 int right;
12502 int bottom;
12503
Romain Guyd928d682009-03-31 17:52:16 -070012504 public void setNextPoolable(InvalidateInfo element) {
12505 mNext = element;
12506 }
12507
12508 public InvalidateInfo getNextPoolable() {
12509 return mNext;
12510 }
12511
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012512 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012513 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012514 }
12515
12516 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012517 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012518 }
12519 }
12520
12521 final IWindowSession mSession;
12522
12523 final IWindow mWindow;
12524
12525 final IBinder mWindowToken;
12526
12527 final Callbacks mRootCallbacks;
12528
Chet Haasedaf98e92011-01-10 14:10:36 -080012529 Canvas mHardwareCanvas;
12530
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012531 /**
12532 * The top view of the hierarchy.
12533 */
12534 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012535
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012536 IBinder mPanelParentWindowToken;
12537 Surface mSurface;
12538
Romain Guyb051e892010-09-28 19:09:36 -070012539 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012540 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012541 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012542
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012543 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012544 * Scale factor used by the compatibility mode
12545 */
12546 float mApplicationScale;
12547
12548 /**
12549 * Indicates whether the application is in compatibility mode
12550 */
12551 boolean mScalingRequired;
12552
12553 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012554 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080012555 */
12556 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012557
Dianne Hackborn63042d62011-01-26 18:56:29 -080012558 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012559 * Left position of this view's window
12560 */
12561 int mWindowLeft;
12562
12563 /**
12564 * Top position of this view's window
12565 */
12566 int mWindowTop;
12567
12568 /**
Adam Powell26153a32010-11-08 15:22:27 -080012569 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012570 */
Adam Powell26153a32010-11-08 15:22:27 -080012571 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012572
12573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012574 * For windows that are full-screen but using insets to layout inside
12575 * of the screen decorations, these are the current insets for the
12576 * content of the window.
12577 */
12578 final Rect mContentInsets = new Rect();
12579
12580 /**
12581 * For windows that are full-screen but using insets to layout inside
12582 * of the screen decorations, these are the current insets for the
12583 * actual visible parts of the window.
12584 */
12585 final Rect mVisibleInsets = new Rect();
12586
12587 /**
12588 * The internal insets given by this window. This value is
12589 * supplied by the client (through
12590 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12591 * be given to the window manager when changed to be used in laying
12592 * out windows behind it.
12593 */
12594 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12595 = new ViewTreeObserver.InternalInsetsInfo();
12596
12597 /**
12598 * All views in the window's hierarchy that serve as scroll containers,
12599 * used to determine if the window can be resized or must be panned
12600 * to adjust for a soft input area.
12601 */
12602 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12603
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012604 final KeyEvent.DispatcherState mKeyDispatchState
12605 = new KeyEvent.DispatcherState();
12606
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012607 /**
12608 * Indicates whether the view's window currently has the focus.
12609 */
12610 boolean mHasWindowFocus;
12611
12612 /**
12613 * The current visibility of the window.
12614 */
12615 int mWindowVisibility;
12616
12617 /**
12618 * Indicates the time at which drawing started to occur.
12619 */
12620 long mDrawingTime;
12621
12622 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012623 * Indicates whether or not ignoring the DIRTY_MASK flags.
12624 */
12625 boolean mIgnoreDirtyState;
12626
12627 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012628 * Indicates whether the view's window is currently in touch mode.
12629 */
12630 boolean mInTouchMode;
12631
12632 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012633 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012634 * the next time it performs a traversal
12635 */
12636 boolean mRecomputeGlobalAttributes;
12637
12638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012639 * Set during a traveral if any views want to keep the screen on.
12640 */
12641 boolean mKeepScreenOn;
12642
12643 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012644 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12645 */
12646 int mSystemUiVisibility;
12647
12648 /**
12649 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12650 * attached.
12651 */
12652 boolean mHasSystemUiListeners;
12653
12654 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012655 * Set if the visibility of any views has changed.
12656 */
12657 boolean mViewVisibilityChanged;
12658
12659 /**
12660 * Set to true if a view has been scrolled.
12661 */
12662 boolean mViewScrollChanged;
12663
12664 /**
12665 * Global to the view hierarchy used as a temporary for dealing with
12666 * x/y points in the transparent region computations.
12667 */
12668 final int[] mTransparentLocation = new int[2];
12669
12670 /**
12671 * Global to the view hierarchy used as a temporary for dealing with
12672 * x/y points in the ViewGroup.invalidateChild implementation.
12673 */
12674 final int[] mInvalidateChildLocation = new int[2];
12675
Chet Haasec3aa3612010-06-17 08:50:37 -070012676
12677 /**
12678 * Global to the view hierarchy used as a temporary for dealing with
12679 * x/y location when view is transformed.
12680 */
12681 final float[] mTmpTransformLocation = new float[2];
12682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012683 /**
12684 * The view tree observer used to dispatch global events like
12685 * layout, pre-draw, touch mode change, etc.
12686 */
12687 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12688
12689 /**
12690 * A Canvas used by the view hierarchy to perform bitmap caching.
12691 */
12692 Canvas mCanvas;
12693
12694 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012695 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 * handler can be used to pump events in the UI events queue.
12697 */
12698 final Handler mHandler;
12699
12700 /**
12701 * Identifier for messages requesting the view to be invalidated.
12702 * Such messages should be sent to {@link #mHandler}.
12703 */
12704 static final int INVALIDATE_MSG = 0x1;
12705
12706 /**
12707 * Identifier for messages requesting the view to invalidate a region.
12708 * Such messages should be sent to {@link #mHandler}.
12709 */
12710 static final int INVALIDATE_RECT_MSG = 0x2;
12711
12712 /**
12713 * Temporary for use in computing invalidate rectangles while
12714 * calling up the hierarchy.
12715 */
12716 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012717
12718 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012719 * Temporary for use in computing hit areas with transformed views
12720 */
12721 final RectF mTmpTransformRect = new RectF();
12722
12723 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012724 * Temporary list for use in collecting focusable descendents of a view.
12725 */
12726 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12727
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012728 /**
12729 * Creates a new set of attachment information with the specified
12730 * events handler and thread.
12731 *
12732 * @param handler the events handler the view must use
12733 */
12734 AttachInfo(IWindowSession session, IWindow window,
12735 Handler handler, Callbacks effectPlayer) {
12736 mSession = session;
12737 mWindow = window;
12738 mWindowToken = window.asBinder();
12739 mHandler = handler;
12740 mRootCallbacks = effectPlayer;
12741 }
12742 }
12743
12744 /**
12745 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12746 * is supported. This avoids keeping too many unused fields in most
12747 * instances of View.</p>
12748 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012749 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012750
Mike Cleronf116bf82009-09-27 19:14:12 -070012751 /**
12752 * Scrollbars are not visible
12753 */
12754 public static final int OFF = 0;
12755
12756 /**
12757 * Scrollbars are visible
12758 */
12759 public static final int ON = 1;
12760
12761 /**
12762 * Scrollbars are fading away
12763 */
12764 public static final int FADING = 2;
12765
12766 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012767
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012768 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012769 public int scrollBarDefaultDelayBeforeFade;
12770 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012771
12772 public int scrollBarSize;
12773 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012774 public float[] interpolatorValues;
12775 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776
12777 public final Paint paint;
12778 public final Matrix matrix;
12779 public Shader shader;
12780
Mike Cleronf116bf82009-09-27 19:14:12 -070012781 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12782
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012783 private static final float[] OPAQUE = { 255 };
12784 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012785
Mike Cleronf116bf82009-09-27 19:14:12 -070012786 /**
12787 * When fading should start. This time moves into the future every time
12788 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12789 */
12790 public long fadeStartTime;
12791
12792
12793 /**
12794 * The current state of the scrollbars: ON, OFF, or FADING
12795 */
12796 public int state = OFF;
12797
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012798 private int mLastColor;
12799
Mike Cleronf116bf82009-09-27 19:14:12 -070012800 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012801 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12802 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012803 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12804 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012805
12806 paint = new Paint();
12807 matrix = new Matrix();
12808 // use use a height of 1, and then wack the matrix each time we
12809 // actually use it.
12810 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012811
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012812 paint.setShader(shader);
12813 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012814 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012815 }
Romain Guy8506ab42009-06-11 17:35:47 -070012816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012817 public void setFadeColor(int color) {
12818 if (color != 0 && color != mLastColor) {
12819 mLastColor = color;
12820 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012821
Romain Guye55e1a72009-08-27 10:42:26 -070012822 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12823 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012825 paint.setShader(shader);
12826 // Restore the default transfer mode (src_over)
12827 paint.setXfermode(null);
12828 }
12829 }
Joe Malin32736f02011-01-19 16:14:20 -080012830
Mike Cleronf116bf82009-09-27 19:14:12 -070012831 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012832 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012833 if (now >= fadeStartTime) {
12834
12835 // the animation fades the scrollbars out by changing
12836 // the opacity (alpha) from fully opaque to fully
12837 // transparent
12838 int nextFrame = (int) now;
12839 int framesCount = 0;
12840
12841 Interpolator interpolator = scrollBarInterpolator;
12842
12843 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012844 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012845
12846 // End transparent
12847 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012848 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012849
12850 state = FADING;
12851
12852 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012853 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012854 }
12855 }
12856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 }
12858}