blob: e329e97d3dd3c8e212ecd39d3c4b05db3cf27224 [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 /**
3176 * Bring up the context menu for this view.
3177 *
3178 * @return Whether a context menu was displayed.
3179 */
3180 public boolean showContextMenu() {
3181 return getParent().showContextMenuForChild(this);
3182 }
3183
3184 /**
Adam Powell6e346362010-07-23 10:18:23 -07003185 * Start an action mode.
3186 *
3187 * @param callback Callback that will control the lifecycle of the action mode
3188 * @return The new action mode if it is started, null otherwise
3189 *
3190 * @see ActionMode
3191 */
3192 public ActionMode startActionMode(ActionMode.Callback callback) {
3193 return getParent().startActionModeForChild(this, callback);
3194 }
3195
3196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 * Register a callback to be invoked when a key is pressed in this view.
3198 * @param l the key listener to attach to this view
3199 */
3200 public void setOnKeyListener(OnKeyListener l) {
3201 mOnKeyListener = l;
3202 }
3203
3204 /**
3205 * Register a callback to be invoked when a touch event is sent to this view.
3206 * @param l the touch listener to attach to this view
3207 */
3208 public void setOnTouchListener(OnTouchListener l) {
3209 mOnTouchListener = l;
3210 }
3211
3212 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003213 * Register a callback to be invoked when a generic motion event is sent to this view.
3214 * @param l the generic motion listener to attach to this view
3215 */
3216 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3217 mOnGenericMotionListener = l;
3218 }
3219
3220 /**
Joe Malin32736f02011-01-19 16:14:20 -08003221 * Register a drag event listener callback object for this View. The parameter is
3222 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3223 * View, the system calls the
3224 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3225 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003226 */
3227 public void setOnDragListener(OnDragListener l) {
3228 mOnDragListener = l;
3229 }
3230
3231 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003232 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3233 *
3234 * Note: this does not check whether this {@link View} should get focus, it just
3235 * gives it focus no matter what. It should only be called internally by framework
3236 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3237 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003238 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3239 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003240 * focus moved when requestFocus() is called. It may not always
3241 * apply, in which case use the default View.FOCUS_DOWN.
3242 * @param previouslyFocusedRect The rectangle of the view that had focus
3243 * prior in this View's coordinate system.
3244 */
3245 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3246 if (DBG) {
3247 System.out.println(this + " requestFocus()");
3248 }
3249
3250 if ((mPrivateFlags & FOCUSED) == 0) {
3251 mPrivateFlags |= FOCUSED;
3252
3253 if (mParent != null) {
3254 mParent.requestChildFocus(this, this);
3255 }
3256
3257 onFocusChanged(true, direction, previouslyFocusedRect);
3258 refreshDrawableState();
3259 }
3260 }
3261
3262 /**
3263 * Request that a rectangle of this view be visible on the screen,
3264 * scrolling if necessary just enough.
3265 *
3266 * <p>A View should call this if it maintains some notion of which part
3267 * of its content is interesting. For example, a text editing view
3268 * should call this when its cursor moves.
3269 *
3270 * @param rectangle The rectangle.
3271 * @return Whether any parent scrolled.
3272 */
3273 public boolean requestRectangleOnScreen(Rect rectangle) {
3274 return requestRectangleOnScreen(rectangle, false);
3275 }
3276
3277 /**
3278 * Request that a rectangle of this view be visible on the screen,
3279 * scrolling if necessary just enough.
3280 *
3281 * <p>A View should call this if it maintains some notion of which part
3282 * of its content is interesting. For example, a text editing view
3283 * should call this when its cursor moves.
3284 *
3285 * <p>When <code>immediate</code> is set to true, scrolling will not be
3286 * animated.
3287 *
3288 * @param rectangle The rectangle.
3289 * @param immediate True to forbid animated scrolling, false otherwise
3290 * @return Whether any parent scrolled.
3291 */
3292 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3293 View child = this;
3294 ViewParent parent = mParent;
3295 boolean scrolled = false;
3296 while (parent != null) {
3297 scrolled |= parent.requestChildRectangleOnScreen(child,
3298 rectangle, immediate);
3299
3300 // offset rect so next call has the rectangle in the
3301 // coordinate system of its direct child.
3302 rectangle.offset(child.getLeft(), child.getTop());
3303 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3304
3305 if (!(parent instanceof View)) {
3306 break;
3307 }
Romain Guy8506ab42009-06-11 17:35:47 -07003308
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003309 child = (View) parent;
3310 parent = child.getParent();
3311 }
3312 return scrolled;
3313 }
3314
3315 /**
3316 * Called when this view wants to give up focus. This will cause
3317 * {@link #onFocusChanged} to be called.
3318 */
3319 public void clearFocus() {
3320 if (DBG) {
3321 System.out.println(this + " clearFocus()");
3322 }
3323
3324 if ((mPrivateFlags & FOCUSED) != 0) {
3325 mPrivateFlags &= ~FOCUSED;
3326
3327 if (mParent != null) {
3328 mParent.clearChildFocus(this);
3329 }
3330
3331 onFocusChanged(false, 0, null);
3332 refreshDrawableState();
3333 }
3334 }
3335
3336 /**
3337 * Called to clear the focus of a view that is about to be removed.
3338 * Doesn't call clearChildFocus, which prevents this view from taking
3339 * focus again before it has been removed from the parent
3340 */
3341 void clearFocusForRemoval() {
3342 if ((mPrivateFlags & FOCUSED) != 0) {
3343 mPrivateFlags &= ~FOCUSED;
3344
3345 onFocusChanged(false, 0, null);
3346 refreshDrawableState();
3347 }
3348 }
3349
3350 /**
3351 * Called internally by the view system when a new view is getting focus.
3352 * This is what clears the old focus.
3353 */
3354 void unFocus() {
3355 if (DBG) {
3356 System.out.println(this + " unFocus()");
3357 }
3358
3359 if ((mPrivateFlags & FOCUSED) != 0) {
3360 mPrivateFlags &= ~FOCUSED;
3361
3362 onFocusChanged(false, 0, null);
3363 refreshDrawableState();
3364 }
3365 }
3366
3367 /**
3368 * Returns true if this view has focus iteself, or is the ancestor of the
3369 * view that has focus.
3370 *
3371 * @return True if this view has or contains focus, false otherwise.
3372 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003373 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003374 public boolean hasFocus() {
3375 return (mPrivateFlags & FOCUSED) != 0;
3376 }
3377
3378 /**
3379 * Returns true if this view is focusable or if it contains a reachable View
3380 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3381 * is a View whose parents do not block descendants focus.
3382 *
3383 * Only {@link #VISIBLE} views are considered focusable.
3384 *
3385 * @return True if the view is focusable or if the view contains a focusable
3386 * View, false otherwise.
3387 *
3388 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3389 */
3390 public boolean hasFocusable() {
3391 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3392 }
3393
3394 /**
3395 * Called by the view system when the focus state of this view changes.
3396 * When the focus change event is caused by directional navigation, direction
3397 * and previouslyFocusedRect provide insight into where the focus is coming from.
3398 * When overriding, be sure to call up through to the super class so that
3399 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003400 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003401 * @param gainFocus True if the View has focus; false otherwise.
3402 * @param direction The direction focus has moved when requestFocus()
3403 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003404 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3405 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3406 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003407 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3408 * system, of the previously focused view. If applicable, this will be
3409 * passed in as finer grained information about where the focus is coming
3410 * from (in addition to direction). Will be <code>null</code> otherwise.
3411 */
3412 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003413 if (gainFocus) {
3414 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3415 }
3416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003417 InputMethodManager imm = InputMethodManager.peekInstance();
3418 if (!gainFocus) {
3419 if (isPressed()) {
3420 setPressed(false);
3421 }
3422 if (imm != null && mAttachInfo != null
3423 && mAttachInfo.mHasWindowFocus) {
3424 imm.focusOut(this);
3425 }
Romain Guya2431d02009-04-30 16:30:00 -07003426 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003427 } else if (imm != null && mAttachInfo != null
3428 && mAttachInfo.mHasWindowFocus) {
3429 imm.focusIn(this);
3430 }
Romain Guy8506ab42009-06-11 17:35:47 -07003431
Romain Guy0fd89bf2011-01-26 15:41:30 -08003432 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003433 if (mOnFocusChangeListener != null) {
3434 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3435 }
Joe Malin32736f02011-01-19 16:14:20 -08003436
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003437 if (mAttachInfo != null) {
3438 mAttachInfo.mKeyDispatchState.reset(this);
3439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 }
3441
3442 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003443 * {@inheritDoc}
3444 */
3445 public void sendAccessibilityEvent(int eventType) {
3446 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3447 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3448 }
3449 }
3450
3451 /**
3452 * {@inheritDoc}
3453 */
3454 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003455 if (!isShown()) {
3456 return;
3457 }
svetoslavganov75986cf2009-05-14 22:28:01 -07003458 event.setClassName(getClass().getName());
3459 event.setPackageName(getContext().getPackageName());
3460 event.setEnabled(isEnabled());
3461 event.setContentDescription(mContentDescription);
3462
3463 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3464 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3465 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3466 event.setItemCount(focusablesTempList.size());
3467 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3468 focusablesTempList.clear();
3469 }
3470
3471 dispatchPopulateAccessibilityEvent(event);
3472
3473 AccessibilityManager.getInstance(mContext).sendAccessibilityEvent(event);
3474 }
3475
3476 /**
3477 * Dispatches an {@link AccessibilityEvent} to the {@link View} children
3478 * to be populated.
3479 *
3480 * @param event The event.
3481 *
3482 * @return True if the event population was completed.
3483 */
3484 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3485 return false;
3486 }
3487
3488 /**
3489 * Gets the {@link View} description. It briefly describes the view and is
3490 * primarily used for accessibility support. Set this property to enable
3491 * better accessibility support for your application. This is especially
3492 * true for views that do not have textual representation (For example,
3493 * ImageButton).
3494 *
3495 * @return The content descriptiopn.
3496 *
3497 * @attr ref android.R.styleable#View_contentDescription
3498 */
3499 public CharSequence getContentDescription() {
3500 return mContentDescription;
3501 }
3502
3503 /**
3504 * Sets the {@link View} description. It briefly describes the view and is
3505 * primarily used for accessibility support. Set this property to enable
3506 * better accessibility support for your application. This is especially
3507 * true for views that do not have textual representation (For example,
3508 * ImageButton).
3509 *
3510 * @param contentDescription The content description.
3511 *
3512 * @attr ref android.R.styleable#View_contentDescription
3513 */
3514 public void setContentDescription(CharSequence contentDescription) {
3515 mContentDescription = contentDescription;
3516 }
3517
3518 /**
Romain Guya2431d02009-04-30 16:30:00 -07003519 * Invoked whenever this view loses focus, either by losing window focus or by losing
3520 * focus within its window. This method can be used to clear any state tied to the
3521 * focus. For instance, if a button is held pressed with the trackball and the window
3522 * loses focus, this method can be used to cancel the press.
3523 *
3524 * Subclasses of View overriding this method should always call super.onFocusLost().
3525 *
3526 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003527 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003528 *
3529 * @hide pending API council approval
3530 */
3531 protected void onFocusLost() {
3532 resetPressedState();
3533 }
3534
3535 private void resetPressedState() {
3536 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3537 return;
3538 }
3539
3540 if (isPressed()) {
3541 setPressed(false);
3542
3543 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003544 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003545 }
3546 }
3547 }
3548
3549 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003550 * Returns true if this view has focus
3551 *
3552 * @return True if this view has focus, false otherwise.
3553 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003554 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003555 public boolean isFocused() {
3556 return (mPrivateFlags & FOCUSED) != 0;
3557 }
3558
3559 /**
3560 * Find the view in the hierarchy rooted at this view that currently has
3561 * focus.
3562 *
3563 * @return The view that currently has focus, or null if no focused view can
3564 * be found.
3565 */
3566 public View findFocus() {
3567 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3568 }
3569
3570 /**
3571 * Change whether this view is one of the set of scrollable containers in
3572 * its window. This will be used to determine whether the window can
3573 * resize or must pan when a soft input area is open -- scrollable
3574 * containers allow the window to use resize mode since the container
3575 * will appropriately shrink.
3576 */
3577 public void setScrollContainer(boolean isScrollContainer) {
3578 if (isScrollContainer) {
3579 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3580 mAttachInfo.mScrollContainers.add(this);
3581 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3582 }
3583 mPrivateFlags |= SCROLL_CONTAINER;
3584 } else {
3585 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3586 mAttachInfo.mScrollContainers.remove(this);
3587 }
3588 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3589 }
3590 }
3591
3592 /**
3593 * Returns the quality of the drawing cache.
3594 *
3595 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3596 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3597 *
3598 * @see #setDrawingCacheQuality(int)
3599 * @see #setDrawingCacheEnabled(boolean)
3600 * @see #isDrawingCacheEnabled()
3601 *
3602 * @attr ref android.R.styleable#View_drawingCacheQuality
3603 */
3604 public int getDrawingCacheQuality() {
3605 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3606 }
3607
3608 /**
3609 * Set the drawing cache quality of this view. This value is used only when the
3610 * drawing cache is enabled
3611 *
3612 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3613 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3614 *
3615 * @see #getDrawingCacheQuality()
3616 * @see #setDrawingCacheEnabled(boolean)
3617 * @see #isDrawingCacheEnabled()
3618 *
3619 * @attr ref android.R.styleable#View_drawingCacheQuality
3620 */
3621 public void setDrawingCacheQuality(int quality) {
3622 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3623 }
3624
3625 /**
3626 * Returns whether the screen should remain on, corresponding to the current
3627 * value of {@link #KEEP_SCREEN_ON}.
3628 *
3629 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3630 *
3631 * @see #setKeepScreenOn(boolean)
3632 *
3633 * @attr ref android.R.styleable#View_keepScreenOn
3634 */
3635 public boolean getKeepScreenOn() {
3636 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3637 }
3638
3639 /**
3640 * Controls whether the screen should remain on, modifying the
3641 * value of {@link #KEEP_SCREEN_ON}.
3642 *
3643 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3644 *
3645 * @see #getKeepScreenOn()
3646 *
3647 * @attr ref android.R.styleable#View_keepScreenOn
3648 */
3649 public void setKeepScreenOn(boolean keepScreenOn) {
3650 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3651 }
3652
3653 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003654 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3655 * @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 -08003656 *
3657 * @attr ref android.R.styleable#View_nextFocusLeft
3658 */
3659 public int getNextFocusLeftId() {
3660 return mNextFocusLeftId;
3661 }
3662
3663 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003664 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3665 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3666 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003667 *
3668 * @attr ref android.R.styleable#View_nextFocusLeft
3669 */
3670 public void setNextFocusLeftId(int nextFocusLeftId) {
3671 mNextFocusLeftId = nextFocusLeftId;
3672 }
3673
3674 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003675 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3676 * @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 -08003677 *
3678 * @attr ref android.R.styleable#View_nextFocusRight
3679 */
3680 public int getNextFocusRightId() {
3681 return mNextFocusRightId;
3682 }
3683
3684 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003685 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3686 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3687 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003688 *
3689 * @attr ref android.R.styleable#View_nextFocusRight
3690 */
3691 public void setNextFocusRightId(int nextFocusRightId) {
3692 mNextFocusRightId = nextFocusRightId;
3693 }
3694
3695 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003696 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3697 * @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 -08003698 *
3699 * @attr ref android.R.styleable#View_nextFocusUp
3700 */
3701 public int getNextFocusUpId() {
3702 return mNextFocusUpId;
3703 }
3704
3705 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003706 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3707 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3708 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 *
3710 * @attr ref android.R.styleable#View_nextFocusUp
3711 */
3712 public void setNextFocusUpId(int nextFocusUpId) {
3713 mNextFocusUpId = nextFocusUpId;
3714 }
3715
3716 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003717 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3718 * @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 -08003719 *
3720 * @attr ref android.R.styleable#View_nextFocusDown
3721 */
3722 public int getNextFocusDownId() {
3723 return mNextFocusDownId;
3724 }
3725
3726 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003727 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3728 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3729 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003730 *
3731 * @attr ref android.R.styleable#View_nextFocusDown
3732 */
3733 public void setNextFocusDownId(int nextFocusDownId) {
3734 mNextFocusDownId = nextFocusDownId;
3735 }
3736
3737 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003738 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3739 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3740 *
3741 * @attr ref android.R.styleable#View_nextFocusForward
3742 */
3743 public int getNextFocusForwardId() {
3744 return mNextFocusForwardId;
3745 }
3746
3747 /**
3748 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3749 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3750 * decide automatically.
3751 *
3752 * @attr ref android.R.styleable#View_nextFocusForward
3753 */
3754 public void setNextFocusForwardId(int nextFocusForwardId) {
3755 mNextFocusForwardId = nextFocusForwardId;
3756 }
3757
3758 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003759 * Returns the visibility of this view and all of its ancestors
3760 *
3761 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3762 */
3763 public boolean isShown() {
3764 View current = this;
3765 //noinspection ConstantConditions
3766 do {
3767 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3768 return false;
3769 }
3770 ViewParent parent = current.mParent;
3771 if (parent == null) {
3772 return false; // We are not attached to the view root
3773 }
3774 if (!(parent instanceof View)) {
3775 return true;
3776 }
3777 current = (View) parent;
3778 } while (current != null);
3779
3780 return false;
3781 }
3782
3783 /**
3784 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3785 * is set
3786 *
3787 * @param insets Insets for system windows
3788 *
3789 * @return True if this view applied the insets, false otherwise
3790 */
3791 protected boolean fitSystemWindows(Rect insets) {
3792 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3793 mPaddingLeft = insets.left;
3794 mPaddingTop = insets.top;
3795 mPaddingRight = insets.right;
3796 mPaddingBottom = insets.bottom;
3797 requestLayout();
3798 return true;
3799 }
3800 return false;
3801 }
3802
3803 /**
3804 * Returns the visibility status for this view.
3805 *
3806 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3807 * @attr ref android.R.styleable#View_visibility
3808 */
3809 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003810 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3811 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3812 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003813 })
3814 public int getVisibility() {
3815 return mViewFlags & VISIBILITY_MASK;
3816 }
3817
3818 /**
3819 * Set the enabled state of this view.
3820 *
3821 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3822 * @attr ref android.R.styleable#View_visibility
3823 */
3824 @RemotableViewMethod
3825 public void setVisibility(int visibility) {
3826 setFlags(visibility, VISIBILITY_MASK);
3827 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3828 }
3829
3830 /**
3831 * Returns the enabled status for this view. The interpretation of the
3832 * enabled state varies by subclass.
3833 *
3834 * @return True if this view is enabled, false otherwise.
3835 */
3836 @ViewDebug.ExportedProperty
3837 public boolean isEnabled() {
3838 return (mViewFlags & ENABLED_MASK) == ENABLED;
3839 }
3840
3841 /**
3842 * Set the enabled state of this view. The interpretation of the enabled
3843 * state varies by subclass.
3844 *
3845 * @param enabled True if this view is enabled, false otherwise.
3846 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003847 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003848 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003849 if (enabled == isEnabled()) return;
3850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003851 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3852
3853 /*
3854 * The View most likely has to change its appearance, so refresh
3855 * the drawable state.
3856 */
3857 refreshDrawableState();
3858
3859 // Invalidate too, since the default behavior for views is to be
3860 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003861 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003862 }
3863
3864 /**
3865 * Set whether this view can receive the focus.
3866 *
3867 * Setting this to false will also ensure that this view is not focusable
3868 * in touch mode.
3869 *
3870 * @param focusable If true, this view can receive the focus.
3871 *
3872 * @see #setFocusableInTouchMode(boolean)
3873 * @attr ref android.R.styleable#View_focusable
3874 */
3875 public void setFocusable(boolean focusable) {
3876 if (!focusable) {
3877 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3878 }
3879 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3880 }
3881
3882 /**
3883 * Set whether this view can receive focus while in touch mode.
3884 *
3885 * Setting this to true will also ensure that this view is focusable.
3886 *
3887 * @param focusableInTouchMode If true, this view can receive the focus while
3888 * in touch mode.
3889 *
3890 * @see #setFocusable(boolean)
3891 * @attr ref android.R.styleable#View_focusableInTouchMode
3892 */
3893 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3894 // Focusable in touch mode should always be set before the focusable flag
3895 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3896 // which, in touch mode, will not successfully request focus on this view
3897 // because the focusable in touch mode flag is not set
3898 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3899 if (focusableInTouchMode) {
3900 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3901 }
3902 }
3903
3904 /**
3905 * Set whether this view should have sound effects enabled for events such as
3906 * clicking and touching.
3907 *
3908 * <p>You may wish to disable sound effects for a view if you already play sounds,
3909 * for instance, a dial key that plays dtmf tones.
3910 *
3911 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3912 * @see #isSoundEffectsEnabled()
3913 * @see #playSoundEffect(int)
3914 * @attr ref android.R.styleable#View_soundEffectsEnabled
3915 */
3916 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3917 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3918 }
3919
3920 /**
3921 * @return whether this view should have sound effects enabled for events such as
3922 * clicking and touching.
3923 *
3924 * @see #setSoundEffectsEnabled(boolean)
3925 * @see #playSoundEffect(int)
3926 * @attr ref android.R.styleable#View_soundEffectsEnabled
3927 */
3928 @ViewDebug.ExportedProperty
3929 public boolean isSoundEffectsEnabled() {
3930 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3931 }
3932
3933 /**
3934 * Set whether this view should have haptic feedback for events such as
3935 * long presses.
3936 *
3937 * <p>You may wish to disable haptic feedback if your view already controls
3938 * its own haptic feedback.
3939 *
3940 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
3941 * @see #isHapticFeedbackEnabled()
3942 * @see #performHapticFeedback(int)
3943 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3944 */
3945 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
3946 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
3947 }
3948
3949 /**
3950 * @return whether this view should have haptic feedback enabled for events
3951 * long presses.
3952 *
3953 * @see #setHapticFeedbackEnabled(boolean)
3954 * @see #performHapticFeedback(int)
3955 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
3956 */
3957 @ViewDebug.ExportedProperty
3958 public boolean isHapticFeedbackEnabled() {
3959 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
3960 }
3961
3962 /**
3963 * If this view doesn't do any drawing on its own, set this flag to
3964 * allow further optimizations. By default, this flag is not set on
3965 * View, but could be set on some View subclasses such as ViewGroup.
3966 *
3967 * Typically, if you override {@link #onDraw} you should clear this flag.
3968 *
3969 * @param willNotDraw whether or not this View draw on its own
3970 */
3971 public void setWillNotDraw(boolean willNotDraw) {
3972 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
3973 }
3974
3975 /**
3976 * Returns whether or not this View draws on its own.
3977 *
3978 * @return true if this view has nothing to draw, false otherwise
3979 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003980 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003981 public boolean willNotDraw() {
3982 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
3983 }
3984
3985 /**
3986 * When a View's drawing cache is enabled, drawing is redirected to an
3987 * offscreen bitmap. Some views, like an ImageView, must be able to
3988 * bypass this mechanism if they already draw a single bitmap, to avoid
3989 * unnecessary usage of the memory.
3990 *
3991 * @param willNotCacheDrawing true if this view does not cache its
3992 * drawing, false otherwise
3993 */
3994 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
3995 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
3996 }
3997
3998 /**
3999 * Returns whether or not this View can cache its drawing or not.
4000 *
4001 * @return true if this view does not cache its drawing, false otherwise
4002 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004003 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004004 public boolean willNotCacheDrawing() {
4005 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4006 }
4007
4008 /**
4009 * Indicates whether this view reacts to click events or not.
4010 *
4011 * @return true if the view is clickable, false otherwise
4012 *
4013 * @see #setClickable(boolean)
4014 * @attr ref android.R.styleable#View_clickable
4015 */
4016 @ViewDebug.ExportedProperty
4017 public boolean isClickable() {
4018 return (mViewFlags & CLICKABLE) == CLICKABLE;
4019 }
4020
4021 /**
4022 * Enables or disables click events for this view. When a view
4023 * is clickable it will change its state to "pressed" on every click.
4024 * Subclasses should set the view clickable to visually react to
4025 * user's clicks.
4026 *
4027 * @param clickable true to make the view clickable, false otherwise
4028 *
4029 * @see #isClickable()
4030 * @attr ref android.R.styleable#View_clickable
4031 */
4032 public void setClickable(boolean clickable) {
4033 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4034 }
4035
4036 /**
4037 * Indicates whether this view reacts to long click events or not.
4038 *
4039 * @return true if the view is long clickable, false otherwise
4040 *
4041 * @see #setLongClickable(boolean)
4042 * @attr ref android.R.styleable#View_longClickable
4043 */
4044 public boolean isLongClickable() {
4045 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4046 }
4047
4048 /**
4049 * Enables or disables long click events for this view. When a view is long
4050 * clickable it reacts to the user holding down the button for a longer
4051 * duration than a tap. This event can either launch the listener or a
4052 * context menu.
4053 *
4054 * @param longClickable true to make the view long clickable, false otherwise
4055 * @see #isLongClickable()
4056 * @attr ref android.R.styleable#View_longClickable
4057 */
4058 public void setLongClickable(boolean longClickable) {
4059 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4060 }
4061
4062 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004063 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004064 *
4065 * @see #isClickable()
4066 * @see #setClickable(boolean)
4067 *
4068 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4069 * the View's internal state from a previously set "pressed" state.
4070 */
4071 public void setPressed(boolean pressed) {
4072 if (pressed) {
4073 mPrivateFlags |= PRESSED;
4074 } else {
4075 mPrivateFlags &= ~PRESSED;
4076 }
4077 refreshDrawableState();
4078 dispatchSetPressed(pressed);
4079 }
4080
4081 /**
4082 * Dispatch setPressed to all of this View's children.
4083 *
4084 * @see #setPressed(boolean)
4085 *
4086 * @param pressed The new pressed state
4087 */
4088 protected void dispatchSetPressed(boolean pressed) {
4089 }
4090
4091 /**
4092 * Indicates whether the view is currently in pressed state. Unless
4093 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4094 * the pressed state.
4095 *
4096 * @see #setPressed
4097 * @see #isClickable()
4098 * @see #setClickable(boolean)
4099 *
4100 * @return true if the view is currently pressed, false otherwise
4101 */
4102 public boolean isPressed() {
4103 return (mPrivateFlags & PRESSED) == PRESSED;
4104 }
4105
4106 /**
4107 * Indicates whether this view will save its state (that is,
4108 * whether its {@link #onSaveInstanceState} method will be called).
4109 *
4110 * @return Returns true if the view state saving is enabled, else false.
4111 *
4112 * @see #setSaveEnabled(boolean)
4113 * @attr ref android.R.styleable#View_saveEnabled
4114 */
4115 public boolean isSaveEnabled() {
4116 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4117 }
4118
4119 /**
4120 * Controls whether the saving of this view's state is
4121 * enabled (that is, whether its {@link #onSaveInstanceState} method
4122 * will be called). Note that even if freezing is enabled, the
4123 * view still must have an id assigned to it (via {@link #setId setId()})
4124 * for its state to be saved. This flag can only disable the
4125 * saving of this view; any child views may still have their state saved.
4126 *
4127 * @param enabled Set to false to <em>disable</em> state saving, or true
4128 * (the default) to allow it.
4129 *
4130 * @see #isSaveEnabled()
4131 * @see #setId(int)
4132 * @see #onSaveInstanceState()
4133 * @attr ref android.R.styleable#View_saveEnabled
4134 */
4135 public void setSaveEnabled(boolean enabled) {
4136 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4137 }
4138
Jeff Brown85a31762010-09-01 17:01:00 -07004139 /**
4140 * Gets whether the framework should discard touches when the view's
4141 * window is obscured by another visible window.
4142 * Refer to the {@link View} security documentation for more details.
4143 *
4144 * @return True if touch filtering is enabled.
4145 *
4146 * @see #setFilterTouchesWhenObscured(boolean)
4147 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4148 */
4149 @ViewDebug.ExportedProperty
4150 public boolean getFilterTouchesWhenObscured() {
4151 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4152 }
4153
4154 /**
4155 * Sets whether the framework should discard touches when the view's
4156 * window is obscured by another visible window.
4157 * Refer to the {@link View} security documentation for more details.
4158 *
4159 * @param enabled True if touch filtering should be enabled.
4160 *
4161 * @see #getFilterTouchesWhenObscured
4162 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4163 */
4164 public void setFilterTouchesWhenObscured(boolean enabled) {
4165 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4166 FILTER_TOUCHES_WHEN_OBSCURED);
4167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004168
4169 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004170 * Indicates whether the entire hierarchy under this view will save its
4171 * state when a state saving traversal occurs from its parent. The default
4172 * is true; if false, these views will not be saved unless
4173 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4174 *
4175 * @return Returns true if the view state saving from parent is enabled, else false.
4176 *
4177 * @see #setSaveFromParentEnabled(boolean)
4178 */
4179 public boolean isSaveFromParentEnabled() {
4180 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4181 }
4182
4183 /**
4184 * Controls whether the entire hierarchy under this view will save its
4185 * state when a state saving traversal occurs from its parent. The default
4186 * is true; if false, these views will not be saved unless
4187 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4188 *
4189 * @param enabled Set to false to <em>disable</em> state saving, or true
4190 * (the default) to allow it.
4191 *
4192 * @see #isSaveFromParentEnabled()
4193 * @see #setId(int)
4194 * @see #onSaveInstanceState()
4195 */
4196 public void setSaveFromParentEnabled(boolean enabled) {
4197 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4198 }
4199
4200
4201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004202 * Returns whether this View is able to take focus.
4203 *
4204 * @return True if this view can take focus, or false otherwise.
4205 * @attr ref android.R.styleable#View_focusable
4206 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004207 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 public final boolean isFocusable() {
4209 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4210 }
4211
4212 /**
4213 * When a view is focusable, it may not want to take focus when in touch mode.
4214 * For example, a button would like focus when the user is navigating via a D-pad
4215 * so that the user can click on it, but once the user starts touching the screen,
4216 * the button shouldn't take focus
4217 * @return Whether the view is focusable in touch mode.
4218 * @attr ref android.R.styleable#View_focusableInTouchMode
4219 */
4220 @ViewDebug.ExportedProperty
4221 public final boolean isFocusableInTouchMode() {
4222 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4223 }
4224
4225 /**
4226 * Find the nearest view in the specified direction that can take focus.
4227 * This does not actually give focus to that view.
4228 *
4229 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4230 *
4231 * @return The nearest focusable in the specified direction, or null if none
4232 * can be found.
4233 */
4234 public View focusSearch(int direction) {
4235 if (mParent != null) {
4236 return mParent.focusSearch(this, direction);
4237 } else {
4238 return null;
4239 }
4240 }
4241
4242 /**
4243 * This method is the last chance for the focused view and its ancestors to
4244 * respond to an arrow key. This is called when the focused view did not
4245 * consume the key internally, nor could the view system find a new view in
4246 * the requested direction to give focus to.
4247 *
4248 * @param focused The currently focused view.
4249 * @param direction The direction focus wants to move. One of FOCUS_UP,
4250 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4251 * @return True if the this view consumed this unhandled move.
4252 */
4253 public boolean dispatchUnhandledMove(View focused, int direction) {
4254 return false;
4255 }
4256
4257 /**
4258 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004259 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004260 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004261 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4262 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004263 * @return The user specified next view, or null if there is none.
4264 */
4265 View findUserSetNextFocus(View root, int direction) {
4266 switch (direction) {
4267 case FOCUS_LEFT:
4268 if (mNextFocusLeftId == View.NO_ID) return null;
4269 return findViewShouldExist(root, mNextFocusLeftId);
4270 case FOCUS_RIGHT:
4271 if (mNextFocusRightId == View.NO_ID) return null;
4272 return findViewShouldExist(root, mNextFocusRightId);
4273 case FOCUS_UP:
4274 if (mNextFocusUpId == View.NO_ID) return null;
4275 return findViewShouldExist(root, mNextFocusUpId);
4276 case FOCUS_DOWN:
4277 if (mNextFocusDownId == View.NO_ID) return null;
4278 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004279 case FOCUS_FORWARD:
4280 if (mNextFocusForwardId == View.NO_ID) return null;
4281 return findViewShouldExist(root, mNextFocusForwardId);
4282 case FOCUS_BACKWARD: {
4283 final int id = mID;
4284 return root.findViewByPredicate(new Predicate<View>() {
4285 @Override
4286 public boolean apply(View t) {
4287 return t.mNextFocusForwardId == id;
4288 }
4289 });
4290 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 }
4292 return null;
4293 }
4294
4295 private static View findViewShouldExist(View root, int childViewId) {
4296 View result = root.findViewById(childViewId);
4297 if (result == null) {
4298 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4299 + "by user for id " + childViewId);
4300 }
4301 return result;
4302 }
4303
4304 /**
4305 * Find and return all focusable views that are descendants of this view,
4306 * possibly including this view if it is focusable itself.
4307 *
4308 * @param direction The direction of the focus
4309 * @return A list of focusable views
4310 */
4311 public ArrayList<View> getFocusables(int direction) {
4312 ArrayList<View> result = new ArrayList<View>(24);
4313 addFocusables(result, direction);
4314 return result;
4315 }
4316
4317 /**
4318 * Add any focusable views that are descendants of this view (possibly
4319 * including this view if it is focusable itself) to views. If we are in touch mode,
4320 * only add views that are also focusable in touch mode.
4321 *
4322 * @param views Focusable views found so far
4323 * @param direction The direction of the focus
4324 */
4325 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004326 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004328
svetoslavganov75986cf2009-05-14 22:28:01 -07004329 /**
4330 * Adds any focusable views that are descendants of this view (possibly
4331 * including this view if it is focusable itself) to views. This method
4332 * adds all focusable views regardless if we are in touch mode or
4333 * only views focusable in touch mode if we are in touch mode depending on
4334 * the focusable mode paramater.
4335 *
4336 * @param views Focusable views found so far or null if all we are interested is
4337 * the number of focusables.
4338 * @param direction The direction of the focus.
4339 * @param focusableMode The type of focusables to be added.
4340 *
4341 * @see #FOCUSABLES_ALL
4342 * @see #FOCUSABLES_TOUCH_MODE
4343 */
4344 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4345 if (!isFocusable()) {
4346 return;
4347 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004348
svetoslavganov75986cf2009-05-14 22:28:01 -07004349 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4350 isInTouchMode() && !isFocusableInTouchMode()) {
4351 return;
4352 }
4353
4354 if (views != null) {
4355 views.add(this);
4356 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004357 }
4358
4359 /**
4360 * Find and return all touchable views that are descendants of this view,
4361 * possibly including this view if it is touchable itself.
4362 *
4363 * @return A list of touchable views
4364 */
4365 public ArrayList<View> getTouchables() {
4366 ArrayList<View> result = new ArrayList<View>();
4367 addTouchables(result);
4368 return result;
4369 }
4370
4371 /**
4372 * Add any touchable views that are descendants of this view (possibly
4373 * including this view if it is touchable itself) to views.
4374 *
4375 * @param views Touchable views found so far
4376 */
4377 public void addTouchables(ArrayList<View> views) {
4378 final int viewFlags = mViewFlags;
4379
4380 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4381 && (viewFlags & ENABLED_MASK) == ENABLED) {
4382 views.add(this);
4383 }
4384 }
4385
4386 /**
4387 * Call this to try to give focus to a specific view or to one of its
4388 * descendants.
4389 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004390 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4391 * false), or if it is focusable and it is not focusable in touch mode
4392 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 *
4394 * See also {@link #focusSearch}, which is what you call to say that you
4395 * have focus, and you want your parent to look for the next one.
4396 *
4397 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4398 * {@link #FOCUS_DOWN} and <code>null</code>.
4399 *
4400 * @return Whether this view or one of its descendants actually took focus.
4401 */
4402 public final boolean requestFocus() {
4403 return requestFocus(View.FOCUS_DOWN);
4404 }
4405
4406
4407 /**
4408 * Call this to try to give focus to a specific view or to one of its
4409 * descendants and give it a hint about what direction focus is heading.
4410 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004411 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4412 * false), or if it is focusable and it is not focusable in touch mode
4413 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004414 *
4415 * See also {@link #focusSearch}, which is what you call to say that you
4416 * have focus, and you want your parent to look for the next one.
4417 *
4418 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4419 * <code>null</code> set for the previously focused rectangle.
4420 *
4421 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4422 * @return Whether this view or one of its descendants actually took focus.
4423 */
4424 public final boolean requestFocus(int direction) {
4425 return requestFocus(direction, null);
4426 }
4427
4428 /**
4429 * Call this to try to give focus to a specific view or to one of its descendants
4430 * and give it hints about the direction and a specific rectangle that the focus
4431 * is coming from. The rectangle can help give larger views a finer grained hint
4432 * about where focus is coming from, and therefore, where to show selection, or
4433 * forward focus change internally.
4434 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004435 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4436 * false), or if it is focusable and it is not focusable in touch mode
4437 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004438 *
4439 * A View will not take focus if it is not visible.
4440 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004441 * A View will not take focus if one of its parents has
4442 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4443 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
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 * You may wish to override this method if your custom {@link View} has an internal
4449 * {@link View} that it wishes to forward the request to.
4450 *
4451 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4452 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4453 * to give a finer grained hint about where focus is coming from. May be null
4454 * if there is no hint.
4455 * @return Whether this view or one of its descendants actually took focus.
4456 */
4457 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4458 // need to be focusable
4459 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4460 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4461 return false;
4462 }
4463
4464 // need to be focusable in touch mode if in touch mode
4465 if (isInTouchMode() &&
4466 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4467 return false;
4468 }
4469
4470 // need to not have any parents blocking us
4471 if (hasAncestorThatBlocksDescendantFocus()) {
4472 return false;
4473 }
4474
4475 handleFocusGainInternal(direction, previouslyFocusedRect);
4476 return true;
4477 }
4478
Christopher Tate2c095f32010-10-04 14:13:40 -07004479 /** Gets the ViewRoot, or null if not attached. */
4480 /*package*/ ViewRoot getViewRoot() {
4481 View root = getRootView();
4482 return root != null ? (ViewRoot)root.getParent() : null;
4483 }
4484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004485 /**
4486 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4487 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4488 * touch mode to request focus when they are touched.
4489 *
4490 * @return Whether this view or one of its descendants actually took focus.
4491 *
4492 * @see #isInTouchMode()
4493 *
4494 */
4495 public final boolean requestFocusFromTouch() {
4496 // Leave touch mode if we need to
4497 if (isInTouchMode()) {
Christopher Tate2c095f32010-10-04 14:13:40 -07004498 ViewRoot viewRoot = getViewRoot();
4499 if (viewRoot != null) {
4500 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004501 }
4502 }
4503 return requestFocus(View.FOCUS_DOWN);
4504 }
4505
4506 /**
4507 * @return Whether any ancestor of this view blocks descendant focus.
4508 */
4509 private boolean hasAncestorThatBlocksDescendantFocus() {
4510 ViewParent ancestor = mParent;
4511 while (ancestor instanceof ViewGroup) {
4512 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4513 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4514 return true;
4515 } else {
4516 ancestor = vgAncestor.getParent();
4517 }
4518 }
4519 return false;
4520 }
4521
4522 /**
Romain Guya440b002010-02-24 15:57:54 -08004523 * @hide
4524 */
4525 public void dispatchStartTemporaryDetach() {
4526 onStartTemporaryDetach();
4527 }
4528
4529 /**
4530 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004531 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4532 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004533 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 */
4535 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004536 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004537 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004538 }
4539
4540 /**
4541 * @hide
4542 */
4543 public void dispatchFinishTemporaryDetach() {
4544 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004545 }
Romain Guy8506ab42009-06-11 17:35:47 -07004546
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004547 /**
4548 * Called after {@link #onStartTemporaryDetach} when the container is done
4549 * changing the view.
4550 */
4551 public void onFinishTemporaryDetach() {
4552 }
Romain Guy8506ab42009-06-11 17:35:47 -07004553
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004554 /**
4555 * capture information of this view for later analysis: developement only
4556 * check dynamic switch to make sure we only dump view
4557 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4558 */
4559 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004560 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004561 return;
4562 }
4563 ViewDebug.dumpCapturedView(subTag, v);
4564 }
4565
4566 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004567 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4568 * for this view's window. Returns null if the view is not currently attached
4569 * to the window. Normally you will not need to use this directly, but
4570 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4571 */
4572 public KeyEvent.DispatcherState getKeyDispatcherState() {
4573 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4574 }
Joe Malin32736f02011-01-19 16:14:20 -08004575
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004576 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004577 * Dispatch a key event before it is processed by any input method
4578 * associated with the view hierarchy. This can be used to intercept
4579 * key events in special situations before the IME consumes them; a
4580 * typical example would be handling the BACK key to update the application's
4581 * UI instead of allowing the IME to see it and close itself.
4582 *
4583 * @param event The key event to be dispatched.
4584 * @return True if the event was handled, false otherwise.
4585 */
4586 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4587 return onKeyPreIme(event.getKeyCode(), event);
4588 }
4589
4590 /**
4591 * Dispatch a key event to the next view on the focus path. This path runs
4592 * from the top of the view tree down to the currently focused view. If this
4593 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4594 * the next node down the focus path. This method also fires any key
4595 * listeners.
4596 *
4597 * @param event The key event to be dispatched.
4598 * @return True if the event was handled, false otherwise.
4599 */
4600 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004601 if (mInputEventConsistencyVerifier != null) {
4602 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4603 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004604
Romain Guyf607bdc2010-09-10 19:20:06 -07004605 //noinspection SimplifiableIfStatement,deprecation
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004606 if (android.util.Config.LOGV) {
4607 captureViewInfo("captureViewKeyEvent", this);
4608 }
4609
Jeff Brown21bc5c92011-02-28 18:27:14 -08004610 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004611 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004612 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4613 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4614 return true;
4615 }
4616
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004617 return event.dispatch(this, mAttachInfo != null
4618 ? mAttachInfo.mKeyDispatchState : null, this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004619 }
4620
4621 /**
4622 * Dispatches a key shortcut event.
4623 *
4624 * @param event The key event to be dispatched.
4625 * @return True if the event was handled by the view, false otherwise.
4626 */
4627 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4628 return onKeyShortcut(event.getKeyCode(), event);
4629 }
4630
4631 /**
4632 * Pass the touch screen motion event down to the target view, or this
4633 * view if it is the target.
4634 *
4635 * @param event The motion event to be dispatched.
4636 * @return True if the event was handled by the view, false otherwise.
4637 */
4638 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004639 if (mInputEventConsistencyVerifier != null) {
4640 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4641 }
4642
Jeff Brown85a31762010-09-01 17:01:00 -07004643 if (!onFilterTouchEventForSecurity(event)) {
4644 return false;
4645 }
4646
Romain Guyf607bdc2010-09-10 19:20:06 -07004647 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004648 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4649 mOnTouchListener.onTouch(this, event)) {
4650 return true;
4651 }
4652 return onTouchEvent(event);
4653 }
4654
4655 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004656 * Filter the touch event to apply security policies.
4657 *
4658 * @param event The motion event to be filtered.
4659 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004660 *
Jeff Brown85a31762010-09-01 17:01:00 -07004661 * @see #getFilterTouchesWhenObscured
4662 */
4663 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004664 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004665 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4666 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4667 // Window is obscured, drop this touch.
4668 return false;
4669 }
4670 return true;
4671 }
4672
4673 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004674 * Pass a trackball motion event down to the focused view.
4675 *
4676 * @param event The motion event to be dispatched.
4677 * @return True if the event was handled by the view, false otherwise.
4678 */
4679 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004680 if (mInputEventConsistencyVerifier != null) {
4681 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4682 }
4683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 //Log.i("view", "view=" + this + ", " + event.toString());
4685 return onTrackballEvent(event);
4686 }
4687
4688 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004689 * Dispatch a generic motion event.
4690 * <p>
4691 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4692 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004693 * delivered to the focused view. Hover events are handled specially and are delivered
4694 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004695 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004696 *
4697 * @param event The motion event to be dispatched.
4698 * @return True if the event was handled by the view, false otherwise.
4699 */
4700 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004701 if (mInputEventConsistencyVerifier != null) {
4702 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4703 }
4704
Jeff Browna032cc02011-03-07 16:56:21 -08004705 final int source = event.getSource();
4706 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4707 final int action = event.getAction();
4708 if (action == MotionEvent.ACTION_HOVER_ENTER
4709 || action == MotionEvent.ACTION_HOVER_MOVE
4710 || action == MotionEvent.ACTION_HOVER_EXIT) {
4711 if (dispatchHoverEvent(event)) {
4712 return true;
4713 }
4714 } else if (dispatchGenericPointerEvent(event)) {
4715 return true;
4716 }
4717 } else if (dispatchGenericFocusedEvent(event)) {
4718 return true;
4719 }
4720
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004721 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004722 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4723 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4724 return true;
4725 }
Jeff Browncb1404e2011-01-15 18:14:15 -08004726 return onGenericMotionEvent(event);
4727 }
4728
4729 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004730 * Dispatch a hover event.
4731 * <p>
4732 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4733 * </p>
4734 *
4735 * @param event The motion event to be dispatched.
4736 * @return True if the event was handled by the view, false otherwise.
4737 * @hide
4738 */
4739 protected boolean dispatchHoverEvent(MotionEvent event) {
4740 return onHoverEvent(event);
4741 }
4742
4743 /**
4744 * Dispatch a generic motion event to the view under the first pointer.
4745 * <p>
4746 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4747 * </p>
4748 *
4749 * @param event The motion event to be dispatched.
4750 * @return True if the event was handled by the view, false otherwise.
4751 * @hide
4752 */
4753 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4754 return false;
4755 }
4756
4757 /**
4758 * Dispatch a generic motion event to the currently focused view.
4759 * <p>
4760 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4761 * </p>
4762 *
4763 * @param event The motion event to be dispatched.
4764 * @return True if the event was handled by the view, false otherwise.
4765 * @hide
4766 */
4767 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4768 return false;
4769 }
4770
4771 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004772 * Dispatch a pointer event.
4773 * <p>
4774 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4775 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4776 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4777 * and should not be expected to handle other pointing device features.
4778 * </p>
4779 *
4780 * @param event The motion event to be dispatched.
4781 * @return True if the event was handled by the view, false otherwise.
4782 * @hide
4783 */
4784 public final boolean dispatchPointerEvent(MotionEvent event) {
4785 if (event.isTouchEvent()) {
4786 return dispatchTouchEvent(event);
4787 } else {
4788 return dispatchGenericMotionEvent(event);
4789 }
4790 }
4791
4792 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004793 * Called when the window containing this view gains or loses window focus.
4794 * ViewGroups should override to route to their children.
4795 *
4796 * @param hasFocus True if the window containing this view now has focus,
4797 * false otherwise.
4798 */
4799 public void dispatchWindowFocusChanged(boolean hasFocus) {
4800 onWindowFocusChanged(hasFocus);
4801 }
4802
4803 /**
4804 * Called when the window containing this view gains or loses focus. Note
4805 * that this is separate from view focus: to receive key events, both
4806 * your view and its window must have focus. If a window is displayed
4807 * on top of yours that takes input focus, then your own window will lose
4808 * focus but the view focus will remain unchanged.
4809 *
4810 * @param hasWindowFocus True if the window containing this view now has
4811 * focus, false otherwise.
4812 */
4813 public void onWindowFocusChanged(boolean hasWindowFocus) {
4814 InputMethodManager imm = InputMethodManager.peekInstance();
4815 if (!hasWindowFocus) {
4816 if (isPressed()) {
4817 setPressed(false);
4818 }
4819 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4820 imm.focusOut(this);
4821 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004822 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004823 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004824 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004825 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4826 imm.focusIn(this);
4827 }
4828 refreshDrawableState();
4829 }
4830
4831 /**
4832 * Returns true if this view is in a window that currently has window focus.
4833 * Note that this is not the same as the view itself having focus.
4834 *
4835 * @return True if this view is in a window that currently has window focus.
4836 */
4837 public boolean hasWindowFocus() {
4838 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4839 }
4840
4841 /**
Adam Powell326d8082009-12-09 15:10:07 -08004842 * Dispatch a view visibility change down the view hierarchy.
4843 * ViewGroups should override to route to their children.
4844 * @param changedView The view whose visibility changed. Could be 'this' or
4845 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004846 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4847 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004848 */
4849 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4850 onVisibilityChanged(changedView, visibility);
4851 }
4852
4853 /**
4854 * Called when the visibility of the view or an ancestor of the view is changed.
4855 * @param changedView The view whose visibility changed. Could be 'this' or
4856 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004857 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4858 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004859 */
4860 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004861 if (visibility == VISIBLE) {
4862 if (mAttachInfo != null) {
4863 initialAwakenScrollBars();
4864 } else {
4865 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4866 }
4867 }
Adam Powell326d8082009-12-09 15:10:07 -08004868 }
4869
4870 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004871 * Dispatch a hint about whether this view is displayed. For instance, when
4872 * a View moves out of the screen, it might receives a display hint indicating
4873 * the view is not displayed. Applications should not <em>rely</em> on this hint
4874 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004875 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004876 * @param hint A hint about whether or not this view is displayed:
4877 * {@link #VISIBLE} or {@link #INVISIBLE}.
4878 */
4879 public void dispatchDisplayHint(int hint) {
4880 onDisplayHint(hint);
4881 }
4882
4883 /**
4884 * Gives this view a hint about whether is displayed or not. For instance, when
4885 * a View moves out of the screen, it might receives a display hint indicating
4886 * the view is not displayed. Applications should not <em>rely</em> on this hint
4887 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08004888 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08004889 * @param hint A hint about whether or not this view is displayed:
4890 * {@link #VISIBLE} or {@link #INVISIBLE}.
4891 */
4892 protected void onDisplayHint(int hint) {
4893 }
4894
4895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 * Dispatch a window visibility change down the view hierarchy.
4897 * ViewGroups should override to route to their children.
4898 *
4899 * @param visibility The new visibility of the window.
4900 *
4901 * @see #onWindowVisibilityChanged
4902 */
4903 public void dispatchWindowVisibilityChanged(int visibility) {
4904 onWindowVisibilityChanged(visibility);
4905 }
4906
4907 /**
4908 * Called when the window containing has change its visibility
4909 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
4910 * that this tells you whether or not your window is being made visible
4911 * to the window manager; this does <em>not</em> tell you whether or not
4912 * your window is obscured by other windows on the screen, even if it
4913 * is itself visible.
4914 *
4915 * @param visibility The new visibility of the window.
4916 */
4917 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004918 if (visibility == VISIBLE) {
4919 initialAwakenScrollBars();
4920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004921 }
4922
4923 /**
4924 * Returns the current visibility of the window this view is attached to
4925 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
4926 *
4927 * @return Returns the current visibility of the view's window.
4928 */
4929 public int getWindowVisibility() {
4930 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
4931 }
4932
4933 /**
4934 * Retrieve the overall visible display size in which the window this view is
4935 * attached to has been positioned in. This takes into account screen
4936 * decorations above the window, for both cases where the window itself
4937 * is being position inside of them or the window is being placed under
4938 * then and covered insets are used for the window to position its content
4939 * inside. In effect, this tells you the available area where content can
4940 * be placed and remain visible to users.
4941 *
4942 * <p>This function requires an IPC back to the window manager to retrieve
4943 * the requested information, so should not be used in performance critical
4944 * code like drawing.
4945 *
4946 * @param outRect Filled in with the visible display frame. If the view
4947 * is not attached to a window, this is simply the raw display size.
4948 */
4949 public void getWindowVisibleDisplayFrame(Rect outRect) {
4950 if (mAttachInfo != null) {
4951 try {
4952 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
4953 } catch (RemoteException e) {
4954 return;
4955 }
4956 // XXX This is really broken, and probably all needs to be done
4957 // in the window manager, and we need to know more about whether
4958 // we want the area behind or in front of the IME.
4959 final Rect insets = mAttachInfo.mVisibleInsets;
4960 outRect.left += insets.left;
4961 outRect.top += insets.top;
4962 outRect.right -= insets.right;
4963 outRect.bottom -= insets.bottom;
4964 return;
4965 }
4966 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
4967 outRect.set(0, 0, d.getWidth(), d.getHeight());
4968 }
4969
4970 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08004971 * Dispatch a notification about a resource configuration change down
4972 * the view hierarchy.
4973 * ViewGroups should override to route to their children.
4974 *
4975 * @param newConfig The new resource configuration.
4976 *
4977 * @see #onConfigurationChanged
4978 */
4979 public void dispatchConfigurationChanged(Configuration newConfig) {
4980 onConfigurationChanged(newConfig);
4981 }
4982
4983 /**
4984 * Called when the current configuration of the resources being used
4985 * by the application have changed. You can use this to decide when
4986 * to reload resources that can changed based on orientation and other
4987 * configuration characterstics. You only need to use this if you are
4988 * not relying on the normal {@link android.app.Activity} mechanism of
4989 * recreating the activity instance upon a configuration change.
4990 *
4991 * @param newConfig The new resource configuration.
4992 */
4993 protected void onConfigurationChanged(Configuration newConfig) {
4994 }
4995
4996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004997 * Private function to aggregate all per-view attributes in to the view
4998 * root.
4999 */
5000 void dispatchCollectViewAttributes(int visibility) {
5001 performCollectViewAttributes(visibility);
5002 }
5003
5004 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005005 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005006 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5007 mAttachInfo.mKeepScreenOn = true;
5008 }
5009 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5010 if (mOnSystemUiVisibilityChangeListener != null) {
5011 mAttachInfo.mHasSystemUiListeners = true;
5012 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 }
5014 }
5015
5016 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005017 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005018 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005019 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5020 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005021 ai.mRecomputeGlobalAttributes = true;
5022 }
5023 }
5024 }
5025
5026 /**
5027 * Returns whether the device is currently in touch mode. Touch mode is entered
5028 * once the user begins interacting with the device by touch, and affects various
5029 * things like whether focus is always visible to the user.
5030 *
5031 * @return Whether the device is in touch mode.
5032 */
5033 @ViewDebug.ExportedProperty
5034 public boolean isInTouchMode() {
5035 if (mAttachInfo != null) {
5036 return mAttachInfo.mInTouchMode;
5037 } else {
5038 return ViewRoot.isInTouchMode();
5039 }
5040 }
5041
5042 /**
5043 * Returns the context the view is running in, through which it can
5044 * access the current theme, resources, etc.
5045 *
5046 * @return The view's Context.
5047 */
5048 @ViewDebug.CapturedViewProperty
5049 public final Context getContext() {
5050 return mContext;
5051 }
5052
5053 /**
5054 * Handle a key event before it is processed by any input method
5055 * associated with the view hierarchy. This can be used to intercept
5056 * key events in special situations before the IME consumes them; a
5057 * typical example would be handling the BACK key to update the application's
5058 * UI instead of allowing the IME to see it and close itself.
5059 *
5060 * @param keyCode The value in event.getKeyCode().
5061 * @param event Description of the key event.
5062 * @return If you handled the event, return true. If you want to allow the
5063 * event to be handled by the next receiver, return false.
5064 */
5065 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5066 return false;
5067 }
5068
5069 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005070 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5071 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005072 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5073 * is released, if the view is enabled and clickable.
5074 *
5075 * @param keyCode A key code that represents the button pressed, from
5076 * {@link android.view.KeyEvent}.
5077 * @param event The KeyEvent object that defines the button action.
5078 */
5079 public boolean onKeyDown(int keyCode, KeyEvent event) {
5080 boolean result = false;
5081
5082 switch (keyCode) {
5083 case KeyEvent.KEYCODE_DPAD_CENTER:
5084 case KeyEvent.KEYCODE_ENTER: {
5085 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5086 return true;
5087 }
5088 // Long clickable items don't necessarily have to be clickable
5089 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5090 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5091 (event.getRepeatCount() == 0)) {
5092 setPressed(true);
5093 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
Adam Powelle14579b2009-12-16 18:39:52 -08005094 postCheckForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 }
5096 return true;
5097 }
5098 break;
5099 }
5100 }
5101 return result;
5102 }
5103
5104 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005105 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5106 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5107 * the event).
5108 */
5109 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5110 return false;
5111 }
5112
5113 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005114 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5115 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5117 * {@link KeyEvent#KEYCODE_ENTER} is released.
5118 *
5119 * @param keyCode A key code that represents the button pressed, from
5120 * {@link android.view.KeyEvent}.
5121 * @param event The KeyEvent object that defines the button action.
5122 */
5123 public boolean onKeyUp(int keyCode, KeyEvent event) {
5124 boolean result = false;
5125
5126 switch (keyCode) {
5127 case KeyEvent.KEYCODE_DPAD_CENTER:
5128 case KeyEvent.KEYCODE_ENTER: {
5129 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5130 return true;
5131 }
5132 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5133 setPressed(false);
5134
5135 if (!mHasPerformedLongPress) {
5136 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005137 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005138
5139 result = performClick();
5140 }
5141 }
5142 break;
5143 }
5144 }
5145 return result;
5146 }
5147
5148 /**
5149 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5150 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5151 * the event).
5152 *
5153 * @param keyCode A key code that represents the button pressed, from
5154 * {@link android.view.KeyEvent}.
5155 * @param repeatCount The number of times the action was made.
5156 * @param event The KeyEvent object that defines the button action.
5157 */
5158 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5159 return false;
5160 }
5161
5162 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005163 * Called on the focused view when a key shortcut event is not handled.
5164 * Override this method to implement local key shortcuts for the View.
5165 * Key shortcuts can also be implemented by setting the
5166 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 *
5168 * @param keyCode The value in event.getKeyCode().
5169 * @param event Description of the key event.
5170 * @return If you handled the event, return true. If you want to allow the
5171 * event to be handled by the next receiver, return false.
5172 */
5173 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5174 return false;
5175 }
5176
5177 /**
5178 * Check whether the called view is a text editor, in which case it
5179 * would make sense to automatically display a soft input window for
5180 * it. Subclasses should override this if they implement
5181 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005182 * a call on that method would return a non-null InputConnection, and
5183 * they are really a first-class editor that the user would normally
5184 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005185 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005186 * <p>The default implementation always returns false. This does
5187 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5188 * will not be called or the user can not otherwise perform edits on your
5189 * view; it is just a hint to the system that this is not the primary
5190 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005191 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005192 * @return Returns true if this view is a text editor, else false.
5193 */
5194 public boolean onCheckIsTextEditor() {
5195 return false;
5196 }
Romain Guy8506ab42009-06-11 17:35:47 -07005197
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005198 /**
5199 * Create a new InputConnection for an InputMethod to interact
5200 * with the view. The default implementation returns null, since it doesn't
5201 * support input methods. You can override this to implement such support.
5202 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005203 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005204 * <p>When implementing this, you probably also want to implement
5205 * {@link #onCheckIsTextEditor()} to indicate you will return a
5206 * non-null InputConnection.
5207 *
5208 * @param outAttrs Fill in with attribute information about the connection.
5209 */
5210 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5211 return null;
5212 }
5213
5214 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005215 * Called by the {@link android.view.inputmethod.InputMethodManager}
5216 * when a view who is not the current
5217 * input connection target is trying to make a call on the manager. The
5218 * default implementation returns false; you can override this to return
5219 * true for certain views if you are performing InputConnection proxying
5220 * to them.
5221 * @param view The View that is making the InputMethodManager call.
5222 * @return Return true to allow the call, false to reject.
5223 */
5224 public boolean checkInputConnectionProxy(View view) {
5225 return false;
5226 }
Romain Guy8506ab42009-06-11 17:35:47 -07005227
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005228 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229 * Show the context menu for this view. It is not safe to hold on to the
5230 * menu after returning from this method.
5231 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005232 * You should normally not overload this method. Overload
5233 * {@link #onCreateContextMenu(ContextMenu)} or define an
5234 * {@link OnCreateContextMenuListener} to add items to the context menu.
5235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005236 * @param menu The context menu to populate
5237 */
5238 public void createContextMenu(ContextMenu menu) {
5239 ContextMenuInfo menuInfo = getContextMenuInfo();
5240
5241 // Sets the current menu info so all items added to menu will have
5242 // my extra info set.
5243 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5244
5245 onCreateContextMenu(menu);
5246 if (mOnCreateContextMenuListener != null) {
5247 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5248 }
5249
5250 // Clear the extra information so subsequent items that aren't mine don't
5251 // have my extra info.
5252 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5253
5254 if (mParent != null) {
5255 mParent.createContextMenu(menu);
5256 }
5257 }
5258
5259 /**
5260 * Views should implement this if they have extra information to associate
5261 * with the context menu. The return result is supplied as a parameter to
5262 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5263 * callback.
5264 *
5265 * @return Extra information about the item for which the context menu
5266 * should be shown. This information will vary across different
5267 * subclasses of View.
5268 */
5269 protected ContextMenuInfo getContextMenuInfo() {
5270 return null;
5271 }
5272
5273 /**
5274 * Views should implement this if the view itself is going to add items to
5275 * the context menu.
5276 *
5277 * @param menu the context menu to populate
5278 */
5279 protected void onCreateContextMenu(ContextMenu menu) {
5280 }
5281
5282 /**
5283 * Implement this method to handle trackball motion events. The
5284 * <em>relative</em> movement of the trackball since the last event
5285 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5286 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5287 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5288 * they will often be fractional values, representing the more fine-grained
5289 * movement information available from a trackball).
5290 *
5291 * @param event The motion event.
5292 * @return True if the event was handled, false otherwise.
5293 */
5294 public boolean onTrackballEvent(MotionEvent event) {
5295 return false;
5296 }
5297
5298 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005299 * Implement this method to handle generic motion events.
5300 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005301 * Generic motion events describe joystick movements, mouse hovers, track pad
5302 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005303 * {@link MotionEvent#getSource() source} of the motion event specifies
5304 * the class of input that was received. Implementations of this method
5305 * must examine the bits in the source before processing the event.
5306 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005307 * </p><p>
5308 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5309 * are delivered to the view under the pointer. All other generic motion events are
5310 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005311 * </p>
5312 * <code>
5313 * public boolean onGenericMotionEvent(MotionEvent event) {
5314 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005315 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5316 * // process the joystick movement...
5317 * return true;
5318 * }
5319 * }
5320 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5321 * switch (event.getAction()) {
5322 * case MotionEvent.ACTION_HOVER_MOVE:
5323 * // process the mouse hover movement...
5324 * return true;
5325 * case MotionEvent.ACTION_SCROLL:
5326 * // process the scroll wheel movement...
5327 * return true;
5328 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005329 * }
5330 * return super.onGenericMotionEvent(event);
5331 * }
5332 * </code>
5333 *
5334 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005335 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005336 */
5337 public boolean onGenericMotionEvent(MotionEvent event) {
5338 return false;
5339 }
5340
5341 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005342 * Implement this method to handle hover events.
5343 * <p>
5344 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5345 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5346 * </p><p>
5347 * The view receives hover enter as the pointer enters the bounds of the view and hover
5348 * exit as the pointer exits the bound of the view or just before the pointer goes down
5349 * (which implies that {@link #onTouchEvent} will be called soon).
5350 * </p><p>
5351 * If the view would like to handle the hover event itself and prevent its children
5352 * from receiving hover, it should return true from this method. If this method returns
5353 * true and a child has already received a hover enter event, the child will
5354 * automatically receive a hover exit event.
5355 * </p><p>
5356 * The default implementation sets the hovered state of the view if the view is
5357 * clickable.
5358 * </p>
5359 *
5360 * @param event The motion event that describes the hover.
5361 * @return True if this view handled the hover event and does not want its children
5362 * to receive the hover event.
5363 */
5364 public boolean onHoverEvent(MotionEvent event) {
5365 final int viewFlags = mViewFlags;
5366
5367 if (((viewFlags & CLICKABLE) != CLICKABLE &&
5368 (viewFlags & LONG_CLICKABLE) != LONG_CLICKABLE)) {
5369 // Nothing to do if the view is not clickable.
5370 return false;
5371 }
5372
5373 if ((viewFlags & ENABLED_MASK) == DISABLED) {
5374 // A disabled view that is clickable still consumes the hover events, it just doesn't
5375 // respond to them.
5376 return true;
5377 }
5378
5379 switch (event.getAction()) {
5380 case MotionEvent.ACTION_HOVER_ENTER:
5381 setHovered(true);
5382 break;
5383
5384 case MotionEvent.ACTION_HOVER_EXIT:
5385 setHovered(false);
5386 break;
5387 }
5388
5389 return true;
5390 }
5391
5392 /**
5393 * Returns true if the view is currently hovered.
5394 *
5395 * @return True if the view is currently hovered.
5396 */
5397 public boolean isHovered() {
5398 return (mPrivateFlags & HOVERED) != 0;
5399 }
5400
5401 /**
5402 * Sets whether the view is currently hovered.
5403 *
5404 * @param hovered True if the view is hovered.
5405 */
5406 public void setHovered(boolean hovered) {
5407 if (hovered) {
5408 if ((mPrivateFlags & HOVERED) == 0) {
5409 mPrivateFlags |= HOVERED;
5410 refreshDrawableState();
5411 }
5412 } else {
5413 if ((mPrivateFlags & HOVERED) != 0) {
5414 mPrivateFlags &= ~HOVERED;
5415 refreshDrawableState();
5416 }
5417 }
5418 }
5419
5420 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005421 * Implement this method to handle touch screen motion events.
5422 *
5423 * @param event The motion event.
5424 * @return True if the event was handled, false otherwise.
5425 */
5426 public boolean onTouchEvent(MotionEvent event) {
5427 final int viewFlags = mViewFlags;
5428
5429 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005430 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5431 mPrivateFlags &= ~PRESSED;
5432 refreshDrawableState();
5433 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005434 // A disabled view that is clickable still consumes the touch
5435 // events, it just doesn't respond to them.
5436 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5437 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5438 }
5439
5440 if (mTouchDelegate != null) {
5441 if (mTouchDelegate.onTouchEvent(event)) {
5442 return true;
5443 }
5444 }
5445
5446 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5447 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5448 switch (event.getAction()) {
5449 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005450 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5451 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005452 // take focus if we don't have it already and we should in
5453 // touch mode.
5454 boolean focusTaken = false;
5455 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5456 focusTaken = requestFocus();
5457 }
5458
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005459 if (prepressed) {
5460 // The button is being released before we actually
5461 // showed it as pressed. Make it show the pressed
5462 // state now (before scheduling the click) to ensure
5463 // the user sees it.
5464 mPrivateFlags |= PRESSED;
5465 refreshDrawableState();
5466 }
Joe Malin32736f02011-01-19 16:14:20 -08005467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005468 if (!mHasPerformedLongPress) {
5469 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005470 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005471
5472 // Only perform take click actions if we were in the pressed state
5473 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005474 // Use a Runnable and post this rather than calling
5475 // performClick directly. This lets other visual state
5476 // of the view update before click actions start.
5477 if (mPerformClick == null) {
5478 mPerformClick = new PerformClick();
5479 }
5480 if (!post(mPerformClick)) {
5481 performClick();
5482 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005483 }
5484 }
5485
5486 if (mUnsetPressedState == null) {
5487 mUnsetPressedState = new UnsetPressedState();
5488 }
5489
Adam Powelle14579b2009-12-16 18:39:52 -08005490 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005491 postDelayed(mUnsetPressedState,
5492 ViewConfiguration.getPressedStateDuration());
5493 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005494 // If the post failed, unpress right now
5495 mUnsetPressedState.run();
5496 }
Adam Powelle14579b2009-12-16 18:39:52 -08005497 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005498 }
5499 break;
5500
5501 case MotionEvent.ACTION_DOWN:
Adam Powelle14579b2009-12-16 18:39:52 -08005502 if (mPendingCheckForTap == null) {
5503 mPendingCheckForTap = new CheckForTap();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 }
Adam Powelle14579b2009-12-16 18:39:52 -08005505 mPrivateFlags |= PREPRESSED;
Adam Powell3b023392010-03-11 16:30:28 -08005506 mHasPerformedLongPress = false;
Adam Powelle14579b2009-12-16 18:39:52 -08005507 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005508 break;
5509
5510 case MotionEvent.ACTION_CANCEL:
5511 mPrivateFlags &= ~PRESSED;
5512 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005513 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005514 break;
5515
5516 case MotionEvent.ACTION_MOVE:
5517 final int x = (int) event.getX();
5518 final int y = (int) event.getY();
5519
5520 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005521 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005522 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005523 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005524 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005525 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005526 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005527
5528 // Need to switch from pressed to not pressed
5529 mPrivateFlags &= ~PRESSED;
5530 refreshDrawableState();
5531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005532 }
5533 break;
5534 }
5535 return true;
5536 }
5537
5538 return false;
5539 }
5540
5541 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005542 * Remove the longpress detection timer.
5543 */
5544 private void removeLongPressCallback() {
5545 if (mPendingCheckForLongPress != null) {
5546 removeCallbacks(mPendingCheckForLongPress);
5547 }
5548 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005549
5550 /**
5551 * Remove the pending click action
5552 */
5553 private void removePerformClickCallback() {
5554 if (mPerformClick != null) {
5555 removeCallbacks(mPerformClick);
5556 }
5557 }
5558
Adam Powelle14579b2009-12-16 18:39:52 -08005559 /**
Romain Guya440b002010-02-24 15:57:54 -08005560 * Remove the prepress detection timer.
5561 */
5562 private void removeUnsetPressCallback() {
5563 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5564 setPressed(false);
5565 removeCallbacks(mUnsetPressedState);
5566 }
5567 }
5568
5569 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005570 * Remove the tap detection timer.
5571 */
5572 private void removeTapCallback() {
5573 if (mPendingCheckForTap != null) {
5574 mPrivateFlags &= ~PREPRESSED;
5575 removeCallbacks(mPendingCheckForTap);
5576 }
5577 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005578
5579 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005580 * Cancels a pending long press. Your subclass can use this if you
5581 * want the context menu to come up if the user presses and holds
5582 * at the same place, but you don't want it to come up if they press
5583 * and then move around enough to cause scrolling.
5584 */
5585 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005586 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005587
5588 /*
5589 * The prepressed state handled by the tap callback is a display
5590 * construct, but the tap callback will post a long press callback
5591 * less its own timeout. Remove it here.
5592 */
5593 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005594 }
5595
5596 /**
5597 * Sets the TouchDelegate for this View.
5598 */
5599 public void setTouchDelegate(TouchDelegate delegate) {
5600 mTouchDelegate = delegate;
5601 }
5602
5603 /**
5604 * Gets the TouchDelegate for this View.
5605 */
5606 public TouchDelegate getTouchDelegate() {
5607 return mTouchDelegate;
5608 }
5609
5610 /**
5611 * Set flags controlling behavior of this view.
5612 *
5613 * @param flags Constant indicating the value which should be set
5614 * @param mask Constant indicating the bit range that should be changed
5615 */
5616 void setFlags(int flags, int mask) {
5617 int old = mViewFlags;
5618 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5619
5620 int changed = mViewFlags ^ old;
5621 if (changed == 0) {
5622 return;
5623 }
5624 int privateFlags = mPrivateFlags;
5625
5626 /* Check if the FOCUSABLE bit has changed */
5627 if (((changed & FOCUSABLE_MASK) != 0) &&
5628 ((privateFlags & HAS_BOUNDS) !=0)) {
5629 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5630 && ((privateFlags & FOCUSED) != 0)) {
5631 /* Give up focus if we are no longer focusable */
5632 clearFocus();
5633 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5634 && ((privateFlags & FOCUSED) == 0)) {
5635 /*
5636 * Tell the view system that we are now available to take focus
5637 * if no one else already has it.
5638 */
5639 if (mParent != null) mParent.focusableViewAvailable(this);
5640 }
5641 }
5642
5643 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5644 if ((changed & VISIBILITY_MASK) != 0) {
5645 /*
5646 * If this view is becoming visible, set the DRAWN flag so that
5647 * the next invalidate() will not be skipped.
5648 */
5649 mPrivateFlags |= DRAWN;
5650
5651 needGlobalAttributesUpdate(true);
5652
5653 // a view becoming visible is worth notifying the parent
5654 // about in case nothing has focus. even if this specific view
5655 // isn't focusable, it may contain something that is, so let
5656 // the root view try to give this focus if nothing else does.
5657 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5658 mParent.focusableViewAvailable(this);
5659 }
5660 }
5661 }
5662
5663 /* Check if the GONE bit has changed */
5664 if ((changed & GONE) != 0) {
5665 needGlobalAttributesUpdate(false);
5666 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005667 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005668
Romain Guyecd80ee2009-12-03 17:13:02 -08005669 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5670 if (hasFocus()) clearFocus();
5671 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005672 }
5673 if (mAttachInfo != null) {
5674 mAttachInfo.mViewVisibilityChanged = true;
5675 }
5676 }
5677
5678 /* Check if the VISIBLE bit has changed */
5679 if ((changed & INVISIBLE) != 0) {
5680 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005681 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005682
5683 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5684 // root view becoming invisible shouldn't clear focus
5685 if (getRootView() != this) {
5686 clearFocus();
5687 }
5688 }
5689 if (mAttachInfo != null) {
5690 mAttachInfo.mViewVisibilityChanged = true;
5691 }
5692 }
5693
Adam Powell326d8082009-12-09 15:10:07 -08005694 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005695 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005696 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5697 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005698 }
Adam Powell326d8082009-12-09 15:10:07 -08005699 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5700 }
5701
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005702 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5703 destroyDrawingCache();
5704 }
5705
5706 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5707 destroyDrawingCache();
5708 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005709 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005710 }
5711
5712 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5713 destroyDrawingCache();
5714 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5715 }
5716
5717 if ((changed & DRAW_MASK) != 0) {
5718 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5719 if (mBGDrawable != null) {
5720 mPrivateFlags &= ~SKIP_DRAW;
5721 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5722 } else {
5723 mPrivateFlags |= SKIP_DRAW;
5724 }
5725 } else {
5726 mPrivateFlags &= ~SKIP_DRAW;
5727 }
5728 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005729 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005730 }
5731
5732 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005733 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005734 mParent.recomputeViewAttributes(this);
5735 }
5736 }
5737 }
5738
5739 /**
5740 * Change the view's z order in the tree, so it's on top of other sibling
5741 * views
5742 */
5743 public void bringToFront() {
5744 if (mParent != null) {
5745 mParent.bringChildToFront(this);
5746 }
5747 }
5748
5749 /**
5750 * This is called in response to an internal scroll in this view (i.e., the
5751 * view scrolled its own contents). This is typically as a result of
5752 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5753 * called.
5754 *
5755 * @param l Current horizontal scroll origin.
5756 * @param t Current vertical scroll origin.
5757 * @param oldl Previous horizontal scroll origin.
5758 * @param oldt Previous vertical scroll origin.
5759 */
5760 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5761 mBackgroundSizeChanged = true;
5762
5763 final AttachInfo ai = mAttachInfo;
5764 if (ai != null) {
5765 ai.mViewScrollChanged = true;
5766 }
5767 }
5768
5769 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005770 * Interface definition for a callback to be invoked when the layout bounds of a view
5771 * changes due to layout processing.
5772 */
5773 public interface OnLayoutChangeListener {
5774 /**
5775 * Called when the focus state of a view has changed.
5776 *
5777 * @param v The view whose state has changed.
5778 * @param left The new value of the view's left property.
5779 * @param top The new value of the view's top property.
5780 * @param right The new value of the view's right property.
5781 * @param bottom The new value of the view's bottom property.
5782 * @param oldLeft The previous value of the view's left property.
5783 * @param oldTop The previous value of the view's top property.
5784 * @param oldRight The previous value of the view's right property.
5785 * @param oldBottom The previous value of the view's bottom property.
5786 */
5787 void onLayoutChange(View v, int left, int top, int right, int bottom,
5788 int oldLeft, int oldTop, int oldRight, int oldBottom);
5789 }
5790
5791 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005792 * This is called during layout when the size of this view has changed. If
5793 * you were just added to the view hierarchy, you're called with the old
5794 * values of 0.
5795 *
5796 * @param w Current width of this view.
5797 * @param h Current height of this view.
5798 * @param oldw Old width of this view.
5799 * @param oldh Old height of this view.
5800 */
5801 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5802 }
5803
5804 /**
5805 * Called by draw to draw the child views. This may be overridden
5806 * by derived classes to gain control just before its children are drawn
5807 * (but after its own view has been drawn).
5808 * @param canvas the canvas on which to draw the view
5809 */
5810 protected void dispatchDraw(Canvas canvas) {
5811 }
5812
5813 /**
5814 * Gets the parent of this view. Note that the parent is a
5815 * ViewParent and not necessarily a View.
5816 *
5817 * @return Parent of this view.
5818 */
5819 public final ViewParent getParent() {
5820 return mParent;
5821 }
5822
5823 /**
5824 * Return the scrolled left position of this view. This is the left edge of
5825 * the displayed part of your view. You do not need to draw any pixels
5826 * farther left, since those are outside of the frame of your view on
5827 * screen.
5828 *
5829 * @return The left edge of the displayed part of your view, in pixels.
5830 */
5831 public final int getScrollX() {
5832 return mScrollX;
5833 }
5834
5835 /**
5836 * Return the scrolled top position of this view. This is the top edge of
5837 * the displayed part of your view. You do not need to draw any pixels above
5838 * it, since those are outside of the frame of your view on screen.
5839 *
5840 * @return The top edge of the displayed part of your view, in pixels.
5841 */
5842 public final int getScrollY() {
5843 return mScrollY;
5844 }
5845
5846 /**
5847 * Return the width of the your view.
5848 *
5849 * @return The width of your view, in pixels.
5850 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005851 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005852 public final int getWidth() {
5853 return mRight - mLeft;
5854 }
5855
5856 /**
5857 * Return the height of your view.
5858 *
5859 * @return The height of your view, in pixels.
5860 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005861 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005862 public final int getHeight() {
5863 return mBottom - mTop;
5864 }
5865
5866 /**
5867 * Return the visible drawing bounds of your view. Fills in the output
5868 * rectangle with the values from getScrollX(), getScrollY(),
5869 * getWidth(), and getHeight().
5870 *
5871 * @param outRect The (scrolled) drawing bounds of the view.
5872 */
5873 public void getDrawingRect(Rect outRect) {
5874 outRect.left = mScrollX;
5875 outRect.top = mScrollY;
5876 outRect.right = mScrollX + (mRight - mLeft);
5877 outRect.bottom = mScrollY + (mBottom - mTop);
5878 }
5879
5880 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005881 * Like {@link #getMeasuredWidthAndState()}, but only returns the
5882 * raw width component (that is the result is masked by
5883 * {@link #MEASURED_SIZE_MASK}).
5884 *
5885 * @return The raw measured width of this view.
5886 */
5887 public final int getMeasuredWidth() {
5888 return mMeasuredWidth & MEASURED_SIZE_MASK;
5889 }
5890
5891 /**
5892 * Return the full width measurement information for this view as computed
5893 * by the most recent call to {@link #measure}. This result is a bit mask
5894 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005895 * This should be used during measurement and layout calculations only. Use
5896 * {@link #getWidth()} to see how wide a view is after layout.
5897 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005898 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005899 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08005900 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 return mMeasuredWidth;
5902 }
5903
5904 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005905 * Like {@link #getMeasuredHeightAndState()}, but only returns the
5906 * raw width component (that is the result is masked by
5907 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005908 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08005909 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005910 */
5911 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08005912 return mMeasuredHeight & MEASURED_SIZE_MASK;
5913 }
5914
5915 /**
5916 * Return the full height measurement information for this view as computed
5917 * by the most recent call to {@link #measure}. This result is a bit mask
5918 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
5919 * This should be used during measurement and layout calculations only. Use
5920 * {@link #getHeight()} to see how wide a view is after layout.
5921 *
5922 * @return The measured width of this view as a bit mask.
5923 */
5924 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005925 return mMeasuredHeight;
5926 }
5927
5928 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08005929 * Return only the state bits of {@link #getMeasuredWidthAndState()}
5930 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
5931 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
5932 * and the height component is at the shifted bits
5933 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
5934 */
5935 public final int getMeasuredState() {
5936 return (mMeasuredWidth&MEASURED_STATE_MASK)
5937 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
5938 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
5939 }
5940
5941 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07005942 * The transform matrix of this view, which is calculated based on the current
5943 * roation, scale, and pivot properties.
5944 *
5945 * @see #getRotation()
5946 * @see #getScaleX()
5947 * @see #getScaleY()
5948 * @see #getPivotX()
5949 * @see #getPivotY()
5950 * @return The current transform matrix for the view
5951 */
5952 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07005953 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07005954 return mMatrix;
5955 }
5956
5957 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07005958 * Utility function to determine if the value is far enough away from zero to be
5959 * considered non-zero.
5960 * @param value A floating point value to check for zero-ness
5961 * @return whether the passed-in value is far enough away from zero to be considered non-zero
5962 */
5963 private static boolean nonzero(float value) {
5964 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
5965 }
5966
5967 /**
Jeff Brown86671742010-09-30 20:00:15 -07005968 * Returns true if the transform matrix is the identity matrix.
5969 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08005970 *
Romain Guy33e72ae2010-07-17 12:40:29 -07005971 * @return True if the transform matrix is the identity matrix, false otherwise.
5972 */
Jeff Brown86671742010-09-30 20:00:15 -07005973 final boolean hasIdentityMatrix() {
5974 updateMatrix();
5975 return mMatrixIsIdentity;
5976 }
5977
5978 /**
5979 * Recomputes the transform matrix if necessary.
5980 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07005981 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07005982 if (mMatrixDirty) {
5983 // transform-related properties have changed since the last time someone
5984 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07005985
5986 // Figure out if we need to update the pivot point
5987 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08005988 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07005989 mPrevWidth = mRight - mLeft;
5990 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08005991 mPivotX = mPrevWidth / 2f;
5992 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07005993 }
5994 }
Chet Haasec3aa3612010-06-17 08:50:37 -07005995 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07005996 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
5997 mMatrix.setTranslate(mTranslationX, mTranslationY);
5998 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
5999 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6000 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006001 if (mCamera == null) {
6002 mCamera = new Camera();
6003 matrix3D = new Matrix();
6004 }
6005 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006006 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006007 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006008 mCamera.getMatrix(matrix3D);
6009 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006010 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006011 mMatrix.postConcat(matrix3D);
6012 mCamera.restore();
6013 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006014 mMatrixDirty = false;
6015 mMatrixIsIdentity = mMatrix.isIdentity();
6016 mInverseMatrixDirty = true;
6017 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006018 }
6019
6020 /**
6021 * Utility method to retrieve the inverse of the current mMatrix property.
6022 * We cache the matrix to avoid recalculating it when transform properties
6023 * have not changed.
6024 *
6025 * @return The inverse of the current matrix of this view.
6026 */
Jeff Brown86671742010-09-30 20:00:15 -07006027 final Matrix getInverseMatrix() {
6028 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006029 if (mInverseMatrixDirty) {
6030 if (mInverseMatrix == null) {
6031 mInverseMatrix = new Matrix();
6032 }
6033 mMatrix.invert(mInverseMatrix);
6034 mInverseMatrixDirty = false;
6035 }
6036 return mInverseMatrix;
6037 }
6038
6039 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006040 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6041 * views are drawn) from the camera to this view. The camera's distance
6042 * affects 3D transformations, for instance rotations around the X and Y
6043 * axis. If the rotationX or rotationY properties are changed and this view is
6044 * large (more than half the size of the screen), it is recommended to always
6045 * use a camera distance that's greater than the height (X axis rotation) or
6046 * the width (Y axis rotation) of this view.</p>
6047 *
6048 * <p>The distance of the camera from the view plane can have an affect on the
6049 * perspective distortion of the view when it is rotated around the x or y axis.
6050 * For example, a large distance will result in a large viewing angle, and there
6051 * will not be much perspective distortion of the view as it rotates. A short
6052 * distance may cause much more perspective distortion upon rotation, and can
6053 * also result in some drawing artifacts if the rotated view ends up partially
6054 * behind the camera (which is why the recommendation is to use a distance at
6055 * least as far as the size of the view, if the view is to be rotated.)</p>
6056 *
6057 * <p>The distance is expressed in "depth pixels." The default distance depends
6058 * on the screen density. For instance, on a medium density display, the
6059 * default distance is 1280. On a high density display, the default distance
6060 * is 1920.</p>
6061 *
6062 * <p>If you want to specify a distance that leads to visually consistent
6063 * results across various densities, use the following formula:</p>
6064 * <pre>
6065 * float scale = context.getResources().getDisplayMetrics().density;
6066 * view.setCameraDistance(distance * scale);
6067 * </pre>
6068 *
6069 * <p>The density scale factor of a high density display is 1.5,
6070 * and 1920 = 1280 * 1.5.</p>
6071 *
6072 * @param distance The distance in "depth pixels", if negative the opposite
6073 * value is used
6074 *
6075 * @see #setRotationX(float)
6076 * @see #setRotationY(float)
6077 */
6078 public void setCameraDistance(float distance) {
6079 invalidateParentCaches();
6080 invalidate(false);
6081
6082 final float dpi = mResources.getDisplayMetrics().densityDpi;
6083 if (mCamera == null) {
6084 mCamera = new Camera();
6085 matrix3D = new Matrix();
6086 }
6087
6088 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6089 mMatrixDirty = true;
6090
6091 invalidate(false);
6092 }
6093
6094 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006095 * The degrees that the view is rotated around the pivot point.
6096 *
Romain Guya5364ee2011-02-24 14:46:04 -08006097 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006098 * @see #getPivotX()
6099 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006100 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006101 * @return The degrees of rotation.
6102 */
6103 public float getRotation() {
6104 return mRotation;
6105 }
6106
6107 /**
Chet Haase897247b2010-09-09 14:54:47 -07006108 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6109 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006110 *
6111 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006112 *
6113 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006114 * @see #getPivotX()
6115 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006116 * @see #setRotationX(float)
6117 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006118 *
6119 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006120 */
6121 public void setRotation(float rotation) {
6122 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006123 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006124 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006125 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006126 mRotation = rotation;
6127 mMatrixDirty = true;
6128 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006129 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006130 }
6131 }
6132
6133 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006134 * The degrees that the view is rotated around the vertical axis through the pivot point.
6135 *
6136 * @see #getPivotX()
6137 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006138 * @see #setRotationY(float)
6139 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006140 * @return The degrees of Y rotation.
6141 */
6142 public float getRotationY() {
6143 return mRotationY;
6144 }
6145
6146 /**
Chet Haase897247b2010-09-09 14:54:47 -07006147 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6148 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6149 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006150 *
6151 * When rotating large views, it is recommended to adjust the camera distance
6152 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006153 *
6154 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006155 *
6156 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006157 * @see #getPivotX()
6158 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006159 * @see #setRotation(float)
6160 * @see #setRotationX(float)
6161 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006162 *
6163 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006164 */
6165 public void setRotationY(float rotationY) {
6166 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006167 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006168 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006169 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006170 mRotationY = rotationY;
6171 mMatrixDirty = true;
6172 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006173 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006174 }
6175 }
6176
6177 /**
6178 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6179 *
6180 * @see #getPivotX()
6181 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006182 * @see #setRotationX(float)
6183 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006184 * @return The degrees of X rotation.
6185 */
6186 public float getRotationX() {
6187 return mRotationX;
6188 }
6189
6190 /**
Chet Haase897247b2010-09-09 14:54:47 -07006191 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6192 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6193 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006194 *
6195 * When rotating large views, it is recommended to adjust the camera distance
6196 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006197 *
6198 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006199 *
6200 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006201 * @see #getPivotX()
6202 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006203 * @see #setRotation(float)
6204 * @see #setRotationY(float)
6205 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006206 *
6207 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006208 */
6209 public void setRotationX(float rotationX) {
6210 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006211 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006212 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006213 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006214 mRotationX = rotationX;
6215 mMatrixDirty = true;
6216 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006217 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006218 }
6219 }
6220
6221 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006222 * The amount that the view is scaled in x around the pivot point, as a proportion of
6223 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6224 *
Joe Onorato93162322010-09-16 15:42:01 -04006225 * <p>By default, this is 1.0f.
6226 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006227 * @see #getPivotX()
6228 * @see #getPivotY()
6229 * @return The scaling factor.
6230 */
6231 public float getScaleX() {
6232 return mScaleX;
6233 }
6234
6235 /**
6236 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6237 * the view's unscaled width. A value of 1 means that no scaling is applied.
6238 *
6239 * @param scaleX The scaling factor.
6240 * @see #getPivotX()
6241 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006242 *
6243 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006244 */
6245 public void setScaleX(float scaleX) {
6246 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006247 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006248 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006249 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006250 mScaleX = scaleX;
6251 mMatrixDirty = true;
6252 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006253 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006254 }
6255 }
6256
6257 /**
6258 * The amount that the view is scaled in y around the pivot point, as a proportion of
6259 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6260 *
Joe Onorato93162322010-09-16 15:42:01 -04006261 * <p>By default, this is 1.0f.
6262 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006263 * @see #getPivotX()
6264 * @see #getPivotY()
6265 * @return The scaling factor.
6266 */
6267 public float getScaleY() {
6268 return mScaleY;
6269 }
6270
6271 /**
6272 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6273 * the view's unscaled width. A value of 1 means that no scaling is applied.
6274 *
6275 * @param scaleY The scaling factor.
6276 * @see #getPivotX()
6277 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006278 *
6279 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006280 */
6281 public void setScaleY(float scaleY) {
6282 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006283 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006284 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006285 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006286 mScaleY = scaleY;
6287 mMatrixDirty = true;
6288 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006289 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006290 }
6291 }
6292
6293 /**
6294 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6295 * and {@link #setScaleX(float) scaled}.
6296 *
6297 * @see #getRotation()
6298 * @see #getScaleX()
6299 * @see #getScaleY()
6300 * @see #getPivotY()
6301 * @return The x location of the pivot point.
6302 */
6303 public float getPivotX() {
6304 return mPivotX;
6305 }
6306
6307 /**
6308 * Sets the x location of the point around which the view is
6309 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006310 * By default, the pivot point is centered on the object.
6311 * Setting this property disables this behavior and causes the view to use only the
6312 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006313 *
6314 * @param pivotX The x location of the pivot point.
6315 * @see #getRotation()
6316 * @see #getScaleX()
6317 * @see #getScaleY()
6318 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006319 *
6320 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006321 */
6322 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006323 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006324 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006325 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006326 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006327 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006328 mPivotX = pivotX;
6329 mMatrixDirty = true;
6330 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006331 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006332 }
6333 }
6334
6335 /**
6336 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6337 * and {@link #setScaleY(float) scaled}.
6338 *
6339 * @see #getRotation()
6340 * @see #getScaleX()
6341 * @see #getScaleY()
6342 * @see #getPivotY()
6343 * @return The y location of the pivot point.
6344 */
6345 public float getPivotY() {
6346 return mPivotY;
6347 }
6348
6349 /**
6350 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006351 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6352 * Setting this property disables this behavior and causes the view to use only the
6353 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006354 *
6355 * @param pivotY The y location of the pivot point.
6356 * @see #getRotation()
6357 * @see #getScaleX()
6358 * @see #getScaleY()
6359 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006360 *
6361 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006362 */
6363 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006364 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006365 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006366 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006367 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006368 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006369 mPivotY = pivotY;
6370 mMatrixDirty = true;
6371 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006372 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006373 }
6374 }
6375
6376 /**
6377 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6378 * completely transparent and 1 means the view is completely opaque.
6379 *
Joe Onorato93162322010-09-16 15:42:01 -04006380 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006381 * @return The opacity of the view.
6382 */
6383 public float getAlpha() {
6384 return mAlpha;
6385 }
6386
6387 /**
Romain Guy171c5922011-01-06 10:04:23 -08006388 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6389 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006390 *
Romain Guy171c5922011-01-06 10:04:23 -08006391 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6392 * responsible for applying the opacity itself. Otherwise, calling this method is
6393 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006394 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006395 *
6396 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006397 *
Joe Malin32736f02011-01-19 16:14:20 -08006398 * @see #setLayerType(int, android.graphics.Paint)
6399 *
Chet Haase73066682010-11-29 15:55:32 -08006400 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006401 */
6402 public void setAlpha(float alpha) {
6403 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006404 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006405 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006406 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006407 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006408 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006409 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006410 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006411 invalidate(false);
6412 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006413 }
6414
6415 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006416 * Faster version of setAlpha() which performs the same steps except there are
6417 * no calls to invalidate(). The caller of this function should perform proper invalidation
6418 * on the parent and this object. The return value indicates whether the subclass handles
6419 * alpha (the return value for onSetAlpha()).
6420 *
6421 * @param alpha The new value for the alpha property
6422 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6423 */
6424 boolean setAlphaNoInvalidation(float alpha) {
6425 mAlpha = alpha;
6426 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6427 if (subclassHandlesAlpha) {
6428 mPrivateFlags |= ALPHA_SET;
6429 } else {
6430 mPrivateFlags &= ~ALPHA_SET;
6431 }
6432 return subclassHandlesAlpha;
6433 }
6434
6435 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436 * Top position of this view relative to its parent.
6437 *
6438 * @return The top of this view, in pixels.
6439 */
6440 @ViewDebug.CapturedViewProperty
6441 public final int getTop() {
6442 return mTop;
6443 }
6444
6445 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006446 * Sets the top position of this view relative to its parent. This method is meant to be called
6447 * by the layout system and should not generally be called otherwise, because the property
6448 * may be changed at any time by the layout.
6449 *
6450 * @param top The top of this view, in pixels.
6451 */
6452 public final void setTop(int top) {
6453 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006454 updateMatrix();
6455 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006456 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006457 int minTop;
6458 int yLoc;
6459 if (top < mTop) {
6460 minTop = top;
6461 yLoc = top - mTop;
6462 } else {
6463 minTop = mTop;
6464 yLoc = 0;
6465 }
Chet Haasee9140a72011-02-16 16:23:29 -08006466 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006467 }
6468 } else {
6469 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006470 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006471 }
6472
Chet Haaseed032702010-10-01 14:05:54 -07006473 int width = mRight - mLeft;
6474 int oldHeight = mBottom - mTop;
6475
Chet Haase21cd1382010-09-01 17:42:29 -07006476 mTop = top;
6477
Chet Haaseed032702010-10-01 14:05:54 -07006478 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6479
Chet Haase21cd1382010-09-01 17:42:29 -07006480 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006481 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6482 // A change in dimension means an auto-centered pivot point changes, too
6483 mMatrixDirty = true;
6484 }
Chet Haase21cd1382010-09-01 17:42:29 -07006485 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006486 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006487 }
Chet Haase55dbb652010-12-21 20:15:08 -08006488 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006489 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006490 }
6491 }
6492
6493 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006494 * Bottom position of this view relative to its parent.
6495 *
6496 * @return The bottom of this view, in pixels.
6497 */
6498 @ViewDebug.CapturedViewProperty
6499 public final int getBottom() {
6500 return mBottom;
6501 }
6502
6503 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006504 * True if this view has changed since the last time being drawn.
6505 *
6506 * @return The dirty state of this view.
6507 */
6508 public boolean isDirty() {
6509 return (mPrivateFlags & DIRTY_MASK) != 0;
6510 }
6511
6512 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006513 * Sets the bottom position of this view relative to its parent. This method is meant to be
6514 * called by the layout system and should not generally be called otherwise, because the
6515 * property may be changed at any time by the layout.
6516 *
6517 * @param bottom The bottom of this view, in pixels.
6518 */
6519 public final void setBottom(int bottom) {
6520 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006521 updateMatrix();
6522 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006523 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006524 int maxBottom;
6525 if (bottom < mBottom) {
6526 maxBottom = mBottom;
6527 } else {
6528 maxBottom = bottom;
6529 }
Chet Haasee9140a72011-02-16 16:23:29 -08006530 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006531 }
6532 } else {
6533 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006534 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006535 }
6536
Chet Haaseed032702010-10-01 14:05:54 -07006537 int width = mRight - mLeft;
6538 int oldHeight = mBottom - mTop;
6539
Chet Haase21cd1382010-09-01 17:42:29 -07006540 mBottom = bottom;
6541
Chet Haaseed032702010-10-01 14:05:54 -07006542 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6543
Chet Haase21cd1382010-09-01 17:42:29 -07006544 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006545 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6546 // A change in dimension means an auto-centered pivot point changes, too
6547 mMatrixDirty = true;
6548 }
Chet Haase21cd1382010-09-01 17:42:29 -07006549 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006550 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006551 }
Chet Haase55dbb652010-12-21 20:15:08 -08006552 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006553 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006554 }
6555 }
6556
6557 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006558 * Left position of this view relative to its parent.
6559 *
6560 * @return The left edge of this view, in pixels.
6561 */
6562 @ViewDebug.CapturedViewProperty
6563 public final int getLeft() {
6564 return mLeft;
6565 }
6566
6567 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006568 * Sets the left position of this view relative to its parent. This method is meant to be called
6569 * by the layout system and should not generally be called otherwise, because the property
6570 * may be changed at any time by the layout.
6571 *
6572 * @param left The bottom of this view, in pixels.
6573 */
6574 public final void setLeft(int left) {
6575 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006576 updateMatrix();
6577 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006578 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006579 int minLeft;
6580 int xLoc;
6581 if (left < mLeft) {
6582 minLeft = left;
6583 xLoc = left - mLeft;
6584 } else {
6585 minLeft = mLeft;
6586 xLoc = 0;
6587 }
Chet Haasee9140a72011-02-16 16:23:29 -08006588 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006589 }
6590 } else {
6591 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006592 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006593 }
6594
Chet Haaseed032702010-10-01 14:05:54 -07006595 int oldWidth = mRight - mLeft;
6596 int height = mBottom - mTop;
6597
Chet Haase21cd1382010-09-01 17:42:29 -07006598 mLeft = left;
6599
Chet Haaseed032702010-10-01 14:05:54 -07006600 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6601
Chet Haase21cd1382010-09-01 17:42:29 -07006602 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006603 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6604 // A change in dimension means an auto-centered pivot point changes, too
6605 mMatrixDirty = true;
6606 }
Chet Haase21cd1382010-09-01 17:42:29 -07006607 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006608 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006609 }
Chet Haase55dbb652010-12-21 20:15:08 -08006610 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006611 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006612 }
6613 }
6614
6615 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006616 * Right position of this view relative to its parent.
6617 *
6618 * @return The right edge of this view, in pixels.
6619 */
6620 @ViewDebug.CapturedViewProperty
6621 public final int getRight() {
6622 return mRight;
6623 }
6624
6625 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006626 * Sets the right position of this view relative to its parent. This method is meant to be called
6627 * by the layout system and should not generally be called otherwise, because the property
6628 * may be changed at any time by the layout.
6629 *
6630 * @param right The bottom of this view, in pixels.
6631 */
6632 public final void setRight(int right) {
6633 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006634 updateMatrix();
6635 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006636 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006637 int maxRight;
6638 if (right < mRight) {
6639 maxRight = mRight;
6640 } else {
6641 maxRight = right;
6642 }
Chet Haasee9140a72011-02-16 16:23:29 -08006643 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006644 }
6645 } else {
6646 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006647 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006648 }
6649
Chet Haaseed032702010-10-01 14:05:54 -07006650 int oldWidth = mRight - mLeft;
6651 int height = mBottom - mTop;
6652
Chet Haase21cd1382010-09-01 17:42:29 -07006653 mRight = right;
6654
Chet Haaseed032702010-10-01 14:05:54 -07006655 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6656
Chet Haase21cd1382010-09-01 17:42:29 -07006657 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006658 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6659 // A change in dimension means an auto-centered pivot point changes, too
6660 mMatrixDirty = true;
6661 }
Chet Haase21cd1382010-09-01 17:42:29 -07006662 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006663 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006664 }
Chet Haase55dbb652010-12-21 20:15:08 -08006665 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006666 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006667 }
6668 }
6669
6670 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006671 * The visual x position of this view, in pixels. This is equivalent to the
6672 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006673 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006674 *
Chet Haasedf030d22010-07-30 17:22:38 -07006675 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006676 */
Chet Haasedf030d22010-07-30 17:22:38 -07006677 public float getX() {
6678 return mLeft + mTranslationX;
6679 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006680
Chet Haasedf030d22010-07-30 17:22:38 -07006681 /**
6682 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6683 * {@link #setTranslationX(float) translationX} property to be the difference between
6684 * the x value passed in and the current {@link #getLeft() left} property.
6685 *
6686 * @param x The visual x position of this view, in pixels.
6687 */
6688 public void setX(float x) {
6689 setTranslationX(x - mLeft);
6690 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006691
Chet Haasedf030d22010-07-30 17:22:38 -07006692 /**
6693 * The visual y position of this view, in pixels. This is equivalent to the
6694 * {@link #setTranslationY(float) translationY} property plus the current
6695 * {@link #getTop() top} property.
6696 *
6697 * @return The visual y position of this view, in pixels.
6698 */
6699 public float getY() {
6700 return mTop + mTranslationY;
6701 }
6702
6703 /**
6704 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6705 * {@link #setTranslationY(float) translationY} property to be the difference between
6706 * the y value passed in and the current {@link #getTop() top} property.
6707 *
6708 * @param y The visual y position of this view, in pixels.
6709 */
6710 public void setY(float y) {
6711 setTranslationY(y - mTop);
6712 }
6713
6714
6715 /**
6716 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6717 * This position is post-layout, in addition to wherever the object's
6718 * layout placed it.
6719 *
6720 * @return The horizontal position of this view relative to its left position, in pixels.
6721 */
6722 public float getTranslationX() {
6723 return mTranslationX;
6724 }
6725
6726 /**
6727 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6728 * This effectively positions the object post-layout, in addition to wherever the object's
6729 * layout placed it.
6730 *
6731 * @param translationX The horizontal position of this view relative to its left position,
6732 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006733 *
6734 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006735 */
6736 public void setTranslationX(float translationX) {
6737 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006738 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006739 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006740 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006741 mTranslationX = translationX;
6742 mMatrixDirty = true;
6743 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006744 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006745 }
6746 }
6747
6748 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006749 * The horizontal location of this view relative to its {@link #getTop() top} position.
6750 * This position is post-layout, in addition to wherever the object's
6751 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006752 *
Chet Haasedf030d22010-07-30 17:22:38 -07006753 * @return The vertical position of this view relative to its top position,
6754 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006755 */
Chet Haasedf030d22010-07-30 17:22:38 -07006756 public float getTranslationY() {
6757 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006758 }
6759
6760 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006761 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6762 * This effectively positions the object post-layout, in addition to wherever the object's
6763 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006764 *
Chet Haasedf030d22010-07-30 17:22:38 -07006765 * @param translationY The vertical position of this view relative to its top position,
6766 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006767 *
6768 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006769 */
Chet Haasedf030d22010-07-30 17:22:38 -07006770 public void setTranslationY(float translationY) {
6771 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006772 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006773 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006774 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006775 mTranslationY = translationY;
6776 mMatrixDirty = true;
6777 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006778 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006779 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006780 }
6781
6782 /**
Romain Guyda489792011-02-03 01:05:15 -08006783 * @hide
6784 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006785 public void setFastTranslationX(float x) {
6786 mTranslationX = x;
6787 mMatrixDirty = true;
6788 }
6789
6790 /**
6791 * @hide
6792 */
6793 public void setFastTranslationY(float y) {
6794 mTranslationY = y;
6795 mMatrixDirty = true;
6796 }
6797
6798 /**
6799 * @hide
6800 */
Romain Guyda489792011-02-03 01:05:15 -08006801 public void setFastX(float x) {
6802 mTranslationX = x - mLeft;
6803 mMatrixDirty = true;
6804 }
6805
6806 /**
6807 * @hide
6808 */
6809 public void setFastY(float y) {
6810 mTranslationY = y - mTop;
6811 mMatrixDirty = true;
6812 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006813
Romain Guyda489792011-02-03 01:05:15 -08006814 /**
6815 * @hide
6816 */
6817 public void setFastScaleX(float x) {
6818 mScaleX = x;
6819 mMatrixDirty = true;
6820 }
6821
6822 /**
6823 * @hide
6824 */
6825 public void setFastScaleY(float y) {
6826 mScaleY = y;
6827 mMatrixDirty = true;
6828 }
6829
6830 /**
6831 * @hide
6832 */
6833 public void setFastAlpha(float alpha) {
6834 mAlpha = alpha;
6835 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006836
Romain Guyda489792011-02-03 01:05:15 -08006837 /**
6838 * @hide
6839 */
6840 public void setFastRotationY(float y) {
6841 mRotationY = y;
6842 mMatrixDirty = true;
6843 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006844
Romain Guyda489792011-02-03 01:05:15 -08006845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006846 * Hit rectangle in parent's coordinates
6847 *
6848 * @param outRect The hit rectangle of the view.
6849 */
6850 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006851 updateMatrix();
6852 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006853 outRect.set(mLeft, mTop, mRight, mBottom);
6854 } else {
6855 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006856 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006857 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006858 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6859 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006860 }
6861 }
6862
6863 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07006864 * Determines whether the given point, in local coordinates is inside the view.
6865 */
6866 /*package*/ final boolean pointInView(float localX, float localY) {
6867 return localX >= 0 && localX < (mRight - mLeft)
6868 && localY >= 0 && localY < (mBottom - mTop);
6869 }
6870
6871 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006872 * Utility method to determine whether the given point, in local coordinates,
6873 * is inside the view, where the area of the view is expanded by the slop factor.
6874 * This method is called while processing touch-move events to determine if the event
6875 * is still within the view.
6876 */
6877 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07006878 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07006879 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006880 }
6881
6882 /**
6883 * When a view has focus and the user navigates away from it, the next view is searched for
6884 * starting from the rectangle filled in by this method.
6885 *
6886 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
6887 * view maintains some idea of internal selection, such as a cursor, or a selected row
6888 * or column, you should override this method and fill in a more specific rectangle.
6889 *
6890 * @param r The rectangle to fill in, in this view's coordinates.
6891 */
6892 public void getFocusedRect(Rect r) {
6893 getDrawingRect(r);
6894 }
6895
6896 /**
6897 * If some part of this view is not clipped by any of its parents, then
6898 * return that area in r in global (root) coordinates. To convert r to local
6899 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
6900 * -globalOffset.y)) If the view is completely clipped or translated out,
6901 * return false.
6902 *
6903 * @param r If true is returned, r holds the global coordinates of the
6904 * visible portion of this view.
6905 * @param globalOffset If true is returned, globalOffset holds the dx,dy
6906 * between this view and its root. globalOffet may be null.
6907 * @return true if r is non-empty (i.e. part of the view is visible at the
6908 * root level.
6909 */
6910 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
6911 int width = mRight - mLeft;
6912 int height = mBottom - mTop;
6913 if (width > 0 && height > 0) {
6914 r.set(0, 0, width, height);
6915 if (globalOffset != null) {
6916 globalOffset.set(-mScrollX, -mScrollY);
6917 }
6918 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
6919 }
6920 return false;
6921 }
6922
6923 public final boolean getGlobalVisibleRect(Rect r) {
6924 return getGlobalVisibleRect(r, null);
6925 }
6926
6927 public final boolean getLocalVisibleRect(Rect r) {
6928 Point offset = new Point();
6929 if (getGlobalVisibleRect(r, offset)) {
6930 r.offset(-offset.x, -offset.y); // make r local
6931 return true;
6932 }
6933 return false;
6934 }
6935
6936 /**
6937 * Offset this view's vertical location by the specified number of pixels.
6938 *
6939 * @param offset the number of pixels to offset the view by
6940 */
6941 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006942 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006943 updateMatrix();
6944 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006945 final ViewParent p = mParent;
6946 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006947 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006948 int minTop;
6949 int maxBottom;
6950 int yLoc;
6951 if (offset < 0) {
6952 minTop = mTop + offset;
6953 maxBottom = mBottom;
6954 yLoc = offset;
6955 } else {
6956 minTop = mTop;
6957 maxBottom = mBottom + offset;
6958 yLoc = 0;
6959 }
6960 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
6961 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07006962 }
6963 } else {
Chet Haaseed032702010-10-01 14:05:54 -07006964 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006965 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006966
Chet Haasec3aa3612010-06-17 08:50:37 -07006967 mTop += offset;
6968 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07006969
Chet Haasec3aa3612010-06-17 08:50:37 -07006970 if (!mMatrixIsIdentity) {
6971 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006972 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006973 }
Chet Haase678e0ad2011-01-25 09:37:18 -08006974 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07006975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006976 }
6977
6978 /**
6979 * Offset this view's horizontal location by the specified amount of pixels.
6980 *
6981 * @param offset the numer of pixels to offset the view by
6982 */
6983 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006984 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07006985 updateMatrix();
6986 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006987 final ViewParent p = mParent;
6988 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006989 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006990 int minLeft;
6991 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006992 if (offset < 0) {
6993 minLeft = mLeft + offset;
6994 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006995 } else {
6996 minLeft = mLeft;
6997 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07006998 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006999 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007000 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007001 }
7002 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007003 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007004 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007005
Chet Haasec3aa3612010-06-17 08:50:37 -07007006 mLeft += offset;
7007 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007008
Chet Haasec3aa3612010-06-17 08:50:37 -07007009 if (!mMatrixIsIdentity) {
7010 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007011 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007012 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007013 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007015 }
7016
7017 /**
7018 * Get the LayoutParams associated with this view. All views should have
7019 * layout parameters. These supply parameters to the <i>parent</i> of this
7020 * view specifying how it should be arranged. There are many subclasses of
7021 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7022 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007023 *
7024 * This method may return null if this View is not attached to a parent
7025 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7026 * was not invoked successfully. When a View is attached to a parent
7027 * ViewGroup, this method must not return null.
7028 *
7029 * @return The LayoutParams associated with this view, or null if no
7030 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007031 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007032 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007033 public ViewGroup.LayoutParams getLayoutParams() {
7034 return mLayoutParams;
7035 }
7036
7037 /**
7038 * Set the layout parameters associated with this view. These supply
7039 * parameters to the <i>parent</i> of this view specifying how it should be
7040 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7041 * correspond to the different subclasses of ViewGroup that are responsible
7042 * for arranging their children.
7043 *
Romain Guy01c174b2011-02-22 11:51:06 -08007044 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007045 */
7046 public void setLayoutParams(ViewGroup.LayoutParams params) {
7047 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007048 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007049 }
7050 mLayoutParams = params;
7051 requestLayout();
7052 }
7053
7054 /**
7055 * Set the scrolled position of your view. This will cause a call to
7056 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7057 * invalidated.
7058 * @param x the x position to scroll to
7059 * @param y the y position to scroll to
7060 */
7061 public void scrollTo(int x, int y) {
7062 if (mScrollX != x || mScrollY != y) {
7063 int oldX = mScrollX;
7064 int oldY = mScrollY;
7065 mScrollX = x;
7066 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007067 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007068 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007069 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007070 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007071 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007072 }
7073 }
7074
7075 /**
7076 * Move the scrolled position of your view. This will cause a call to
7077 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7078 * invalidated.
7079 * @param x the amount of pixels to scroll by horizontally
7080 * @param y the amount of pixels to scroll by vertically
7081 */
7082 public void scrollBy(int x, int y) {
7083 scrollTo(mScrollX + x, mScrollY + y);
7084 }
7085
7086 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007087 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7088 * animation to fade the scrollbars out after a default delay. If a subclass
7089 * provides animated scrolling, the start delay should equal the duration
7090 * of the scrolling animation.</p>
7091 *
7092 * <p>The animation starts only if at least one of the scrollbars is
7093 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7094 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7095 * this method returns true, and false otherwise. If the animation is
7096 * started, this method calls {@link #invalidate()}; in that case the
7097 * caller should not call {@link #invalidate()}.</p>
7098 *
7099 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007100 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007101 *
7102 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7103 * and {@link #scrollTo(int, int)}.</p>
7104 *
7105 * @return true if the animation is played, false otherwise
7106 *
7107 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007108 * @see #scrollBy(int, int)
7109 * @see #scrollTo(int, int)
7110 * @see #isHorizontalScrollBarEnabled()
7111 * @see #isVerticalScrollBarEnabled()
7112 * @see #setHorizontalScrollBarEnabled(boolean)
7113 * @see #setVerticalScrollBarEnabled(boolean)
7114 */
7115 protected boolean awakenScrollBars() {
7116 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007117 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007118 }
7119
7120 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007121 * Trigger the scrollbars to draw.
7122 * This method differs from awakenScrollBars() only in its default duration.
7123 * initialAwakenScrollBars() will show the scroll bars for longer than
7124 * usual to give the user more of a chance to notice them.
7125 *
7126 * @return true if the animation is played, false otherwise.
7127 */
7128 private boolean initialAwakenScrollBars() {
7129 return mScrollCache != null &&
7130 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7131 }
7132
7133 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007134 * <p>
7135 * Trigger the scrollbars to draw. When invoked this method starts an
7136 * animation to fade the scrollbars out after a fixed delay. If a subclass
7137 * provides animated scrolling, the start delay should equal the duration of
7138 * the scrolling animation.
7139 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007140 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007141 * <p>
7142 * The animation starts only if at least one of the scrollbars is enabled,
7143 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7144 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7145 * this method returns true, and false otherwise. If the animation is
7146 * started, this method calls {@link #invalidate()}; in that case the caller
7147 * should not call {@link #invalidate()}.
7148 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007149 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007150 * <p>
7151 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007152 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007153 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007154 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007155 * @param startDelay the delay, in milliseconds, after which the animation
7156 * should start; when the delay is 0, the animation starts
7157 * immediately
7158 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007159 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007160 * @see #scrollBy(int, int)
7161 * @see #scrollTo(int, int)
7162 * @see #isHorizontalScrollBarEnabled()
7163 * @see #isVerticalScrollBarEnabled()
7164 * @see #setHorizontalScrollBarEnabled(boolean)
7165 * @see #setVerticalScrollBarEnabled(boolean)
7166 */
7167 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007168 return awakenScrollBars(startDelay, true);
7169 }
Joe Malin32736f02011-01-19 16:14:20 -08007170
Mike Cleron290947b2009-09-29 18:34:32 -07007171 /**
7172 * <p>
7173 * Trigger the scrollbars to draw. When invoked this method starts an
7174 * animation to fade the scrollbars out after a fixed delay. If a subclass
7175 * provides animated scrolling, the start delay should equal the duration of
7176 * the scrolling animation.
7177 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007178 *
Mike Cleron290947b2009-09-29 18:34:32 -07007179 * <p>
7180 * The animation starts only if at least one of the scrollbars is enabled,
7181 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7182 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7183 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007184 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007185 * is set to true; in that case the caller
7186 * should not call {@link #invalidate()}.
7187 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007188 *
Mike Cleron290947b2009-09-29 18:34:32 -07007189 * <p>
7190 * This method should be invoked everytime a subclass directly updates the
7191 * scroll parameters.
7192 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007193 *
Mike Cleron290947b2009-09-29 18:34:32 -07007194 * @param startDelay the delay, in milliseconds, after which the animation
7195 * should start; when the delay is 0, the animation starts
7196 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007197 *
Mike Cleron290947b2009-09-29 18:34:32 -07007198 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007199 *
Mike Cleron290947b2009-09-29 18:34:32 -07007200 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007201 *
Mike Cleron290947b2009-09-29 18:34:32 -07007202 * @see #scrollBy(int, int)
7203 * @see #scrollTo(int, int)
7204 * @see #isHorizontalScrollBarEnabled()
7205 * @see #isVerticalScrollBarEnabled()
7206 * @see #setHorizontalScrollBarEnabled(boolean)
7207 * @see #setVerticalScrollBarEnabled(boolean)
7208 */
7209 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007210 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007211
Mike Cleronf116bf82009-09-27 19:14:12 -07007212 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7213 return false;
7214 }
7215
7216 if (scrollCache.scrollBar == null) {
7217 scrollCache.scrollBar = new ScrollBarDrawable();
7218 }
7219
7220 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7221
Mike Cleron290947b2009-09-29 18:34:32 -07007222 if (invalidate) {
7223 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007224 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007225 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007226
7227 if (scrollCache.state == ScrollabilityCache.OFF) {
7228 // FIXME: this is copied from WindowManagerService.
7229 // We should get this value from the system when it
7230 // is possible to do so.
7231 final int KEY_REPEAT_FIRST_DELAY = 750;
7232 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7233 }
7234
7235 // Tell mScrollCache when we should start fading. This may
7236 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007237 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007238 scrollCache.fadeStartTime = fadeStartTime;
7239 scrollCache.state = ScrollabilityCache.ON;
7240
7241 // Schedule our fader to run, unscheduling any old ones first
7242 if (mAttachInfo != null) {
7243 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7244 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7245 }
7246
7247 return true;
7248 }
7249
7250 return false;
7251 }
7252
7253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007254 * Mark the the area defined by dirty as needing to be drawn. If the view is
7255 * visible, {@link #onDraw} will be called at some point in the future.
7256 * This must be called from a UI thread. To call from a non-UI thread, call
7257 * {@link #postInvalidate()}.
7258 *
7259 * WARNING: This method is destructive to dirty.
7260 * @param dirty the rectangle representing the bounds of the dirty region
7261 */
7262 public void invalidate(Rect dirty) {
7263 if (ViewDebug.TRACE_HIERARCHY) {
7264 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7265 }
7266
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007267 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007268 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7269 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007270 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007271 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007272 final ViewParent p = mParent;
7273 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007274 //noinspection PointlessBooleanExpression,ConstantConditions
7275 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7276 if (p != null && ai != null && ai.mHardwareAccelerated) {
7277 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7278 // with a null dirty rect, which tells the ViewRoot to redraw everything
7279 p.invalidateChild(this, null);
7280 return;
7281 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007283 if (p != null && ai != null) {
7284 final int scrollX = mScrollX;
7285 final int scrollY = mScrollY;
7286 final Rect r = ai.mTmpInvalRect;
7287 r.set(dirty.left - scrollX, dirty.top - scrollY,
7288 dirty.right - scrollX, dirty.bottom - scrollY);
7289 mParent.invalidateChild(this, r);
7290 }
7291 }
7292 }
7293
7294 /**
7295 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7296 * The coordinates of the dirty rect are relative to the view.
7297 * If the view is visible, {@link #onDraw} will be called at some point
7298 * in the future. This must be called from a UI thread. To call
7299 * from a non-UI thread, call {@link #postInvalidate()}.
7300 * @param l the left position of the dirty region
7301 * @param t the top position of the dirty region
7302 * @param r the right position of the dirty region
7303 * @param b the bottom position of the dirty region
7304 */
7305 public void invalidate(int l, int t, int r, int b) {
7306 if (ViewDebug.TRACE_HIERARCHY) {
7307 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7308 }
7309
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007310 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007311 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7312 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007313 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007314 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007315 final ViewParent p = mParent;
7316 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007317 //noinspection PointlessBooleanExpression,ConstantConditions
7318 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7319 if (p != null && ai != null && ai.mHardwareAccelerated) {
7320 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7321 // with a null dirty rect, which tells the ViewRoot to redraw everything
7322 p.invalidateChild(this, null);
7323 return;
7324 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007325 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 if (p != null && ai != null && l < r && t < b) {
7327 final int scrollX = mScrollX;
7328 final int scrollY = mScrollY;
7329 final Rect tmpr = ai.mTmpInvalRect;
7330 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7331 p.invalidateChild(this, tmpr);
7332 }
7333 }
7334 }
7335
7336 /**
7337 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7338 * be called at some point in the future. This must be called from a
7339 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7340 */
7341 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007342 invalidate(true);
7343 }
Joe Malin32736f02011-01-19 16:14:20 -08007344
Chet Haaseed032702010-10-01 14:05:54 -07007345 /**
7346 * This is where the invalidate() work actually happens. A full invalidate()
7347 * causes the drawing cache to be invalidated, but this function can be called with
7348 * invalidateCache set to false to skip that invalidation step for cases that do not
7349 * need it (for example, a component that remains at the same dimensions with the same
7350 * content).
7351 *
7352 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7353 * well. This is usually true for a full invalidate, but may be set to false if the
7354 * View's contents or dimensions have not changed.
7355 */
Romain Guy849d0a32011-02-01 17:20:48 -08007356 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007357 if (ViewDebug.TRACE_HIERARCHY) {
7358 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7359 }
7360
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007361 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007362 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007363 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7364 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007365 mPrivateFlags &= ~DRAWN;
7366 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007367 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007368 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7369 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007370 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007371 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007372 //noinspection PointlessBooleanExpression,ConstantConditions
7373 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7374 if (p != null && ai != null && ai.mHardwareAccelerated) {
7375 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
7376 // with a null dirty rect, which tells the ViewRoot to redraw everything
7377 p.invalidateChild(this, null);
7378 return;
7379 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007380 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007382 if (p != null && ai != null) {
7383 final Rect r = ai.mTmpInvalRect;
7384 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7385 // Don't call invalidate -- we don't want to internally scroll
7386 // our own bounds
7387 p.invalidateChild(this, r);
7388 }
7389 }
7390 }
7391
7392 /**
Romain Guyda489792011-02-03 01:05:15 -08007393 * @hide
7394 */
7395 public void fastInvalidate() {
7396 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7397 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7398 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7399 if (mParent instanceof View) {
7400 ((View) mParent).mPrivateFlags |= INVALIDATED;
7401 }
7402 mPrivateFlags &= ~DRAWN;
7403 mPrivateFlags |= INVALIDATED;
7404 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007405 if (mParent != null && mAttachInfo != null) {
7406 if (mAttachInfo.mHardwareAccelerated) {
7407 mParent.invalidateChild(this, null);
7408 } else {
7409 final Rect r = mAttachInfo.mTmpInvalRect;
7410 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7411 // Don't call invalidate -- we don't want to internally scroll
7412 // our own bounds
7413 mParent.invalidateChild(this, r);
7414 }
Romain Guyda489792011-02-03 01:05:15 -08007415 }
7416 }
7417 }
7418
7419 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007420 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007421 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7422 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007423 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7424 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007425 *
7426 * @hide
7427 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007428 protected void invalidateParentCaches() {
7429 if (mParent instanceof View) {
7430 ((View) mParent).mPrivateFlags |= INVALIDATED;
7431 }
7432 }
Joe Malin32736f02011-01-19 16:14:20 -08007433
Romain Guy0fd89bf2011-01-26 15:41:30 -08007434 /**
7435 * Used to indicate that the parent of this view should be invalidated. This functionality
7436 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7437 * which is necessary when various parent-managed properties of the view change, such as
7438 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7439 * an invalidation event to the parent.
7440 *
7441 * @hide
7442 */
7443 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007444 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007445 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007446 }
7447 }
7448
7449 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007450 * Indicates whether this View is opaque. An opaque View guarantees that it will
7451 * draw all the pixels overlapping its bounds using a fully opaque color.
7452 *
7453 * Subclasses of View should override this method whenever possible to indicate
7454 * whether an instance is opaque. Opaque Views are treated in a special way by
7455 * the View hierarchy, possibly allowing it to perform optimizations during
7456 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007457 *
Romain Guy24443ea2009-05-11 11:56:30 -07007458 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007459 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007460 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007461 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007462 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7463 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007464 }
7465
Adam Powell20232d02010-12-08 21:08:53 -08007466 /**
7467 * @hide
7468 */
7469 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007470 // Opaque if:
7471 // - Has a background
7472 // - Background is opaque
7473 // - Doesn't have scrollbars or scrollbars are inside overlay
7474
7475 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7476 mPrivateFlags |= OPAQUE_BACKGROUND;
7477 } else {
7478 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7479 }
7480
7481 final int flags = mViewFlags;
7482 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7483 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7484 mPrivateFlags |= OPAQUE_SCROLLBARS;
7485 } else {
7486 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7487 }
7488 }
7489
7490 /**
7491 * @hide
7492 */
7493 protected boolean hasOpaqueScrollbars() {
7494 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007495 }
7496
7497 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007498 * @return A handler associated with the thread running the View. This
7499 * handler can be used to pump events in the UI events queue.
7500 */
7501 public Handler getHandler() {
7502 if (mAttachInfo != null) {
7503 return mAttachInfo.mHandler;
7504 }
7505 return null;
7506 }
7507
7508 /**
7509 * Causes the Runnable to be added to the message queue.
7510 * The runnable will be run on the user interface thread.
7511 *
7512 * @param action The Runnable that will be executed.
7513 *
7514 * @return Returns true if the Runnable was successfully placed in to the
7515 * message queue. Returns false on failure, usually because the
7516 * looper processing the message queue is exiting.
7517 */
7518 public boolean post(Runnable action) {
7519 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007520 AttachInfo attachInfo = mAttachInfo;
7521 if (attachInfo != null) {
7522 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007523 } else {
7524 // Assume that post will succeed later
7525 ViewRoot.getRunQueue().post(action);
7526 return true;
7527 }
7528
7529 return handler.post(action);
7530 }
7531
7532 /**
7533 * Causes the Runnable to be added to the message queue, to be run
7534 * after the specified amount of time elapses.
7535 * The runnable will be run on the user interface thread.
7536 *
7537 * @param action The Runnable that will be executed.
7538 * @param delayMillis The delay (in milliseconds) until the Runnable
7539 * will be executed.
7540 *
7541 * @return true if the Runnable was successfully placed in to the
7542 * message queue. Returns false on failure, usually because the
7543 * looper processing the message queue is exiting. Note that a
7544 * result of true does not mean the Runnable will be processed --
7545 * if the looper is quit before the delivery time of the message
7546 * occurs then the message will be dropped.
7547 */
7548 public boolean postDelayed(Runnable action, long delayMillis) {
7549 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007550 AttachInfo attachInfo = mAttachInfo;
7551 if (attachInfo != null) {
7552 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007553 } else {
7554 // Assume that post will succeed later
7555 ViewRoot.getRunQueue().postDelayed(action, delayMillis);
7556 return true;
7557 }
7558
7559 return handler.postDelayed(action, delayMillis);
7560 }
7561
7562 /**
7563 * Removes the specified Runnable from the message queue.
7564 *
7565 * @param action The Runnable to remove from the message handling queue
7566 *
7567 * @return true if this view could ask the Handler to remove the Runnable,
7568 * false otherwise. When the returned value is true, the Runnable
7569 * may or may not have been actually removed from the message queue
7570 * (for instance, if the Runnable was not in the queue already.)
7571 */
7572 public boolean removeCallbacks(Runnable action) {
7573 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007574 AttachInfo attachInfo = mAttachInfo;
7575 if (attachInfo != null) {
7576 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007577 } else {
7578 // Assume that post will succeed later
7579 ViewRoot.getRunQueue().removeCallbacks(action);
7580 return true;
7581 }
7582
7583 handler.removeCallbacks(action);
7584 return true;
7585 }
7586
7587 /**
7588 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7589 * Use this to invalidate the View from a non-UI thread.
7590 *
7591 * @see #invalidate()
7592 */
7593 public void postInvalidate() {
7594 postInvalidateDelayed(0);
7595 }
7596
7597 /**
7598 * Cause an invalidate of the specified area to happen on a subsequent cycle
7599 * through the event loop. Use this to invalidate the View from a non-UI thread.
7600 *
7601 * @param left The left coordinate of the rectangle to invalidate.
7602 * @param top The top coordinate of the rectangle to invalidate.
7603 * @param right The right coordinate of the rectangle to invalidate.
7604 * @param bottom The bottom coordinate of the rectangle to invalidate.
7605 *
7606 * @see #invalidate(int, int, int, int)
7607 * @see #invalidate(Rect)
7608 */
7609 public void postInvalidate(int left, int top, int right, int bottom) {
7610 postInvalidateDelayed(0, left, top, right, bottom);
7611 }
7612
7613 /**
7614 * Cause an invalidate to happen on a subsequent cycle through the event
7615 * loop. Waits for the specified amount of time.
7616 *
7617 * @param delayMilliseconds the duration in milliseconds to delay the
7618 * invalidation by
7619 */
7620 public void postInvalidateDelayed(long delayMilliseconds) {
7621 // We try only with the AttachInfo because there's no point in invalidating
7622 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007623 AttachInfo attachInfo = mAttachInfo;
7624 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007625 Message msg = Message.obtain();
7626 msg.what = AttachInfo.INVALIDATE_MSG;
7627 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007628 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007629 }
7630 }
7631
7632 /**
7633 * Cause an invalidate of the specified area to happen on a subsequent cycle
7634 * through the event loop. Waits for the specified amount of time.
7635 *
7636 * @param delayMilliseconds the duration in milliseconds to delay the
7637 * invalidation by
7638 * @param left The left coordinate of the rectangle to invalidate.
7639 * @param top The top coordinate of the rectangle to invalidate.
7640 * @param right The right coordinate of the rectangle to invalidate.
7641 * @param bottom The bottom coordinate of the rectangle to invalidate.
7642 */
7643 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7644 int right, int bottom) {
7645
7646 // We try only with the AttachInfo because there's no point in invalidating
7647 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007648 AttachInfo attachInfo = mAttachInfo;
7649 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007650 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7651 info.target = this;
7652 info.left = left;
7653 info.top = top;
7654 info.right = right;
7655 info.bottom = bottom;
7656
7657 final Message msg = Message.obtain();
7658 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7659 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007660 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 }
7662 }
7663
7664 /**
7665 * Called by a parent to request that a child update its values for mScrollX
7666 * and mScrollY if necessary. This will typically be done if the child is
7667 * animating a scroll using a {@link android.widget.Scroller Scroller}
7668 * object.
7669 */
7670 public void computeScroll() {
7671 }
7672
7673 /**
7674 * <p>Indicate whether the horizontal edges are faded when the view is
7675 * scrolled horizontally.</p>
7676 *
7677 * @return true if the horizontal edges should are faded on scroll, false
7678 * otherwise
7679 *
7680 * @see #setHorizontalFadingEdgeEnabled(boolean)
7681 * @attr ref android.R.styleable#View_fadingEdge
7682 */
7683 public boolean isHorizontalFadingEdgeEnabled() {
7684 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7685 }
7686
7687 /**
7688 * <p>Define whether the horizontal edges should be faded when this view
7689 * is scrolled horizontally.</p>
7690 *
7691 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7692 * be faded when the view is scrolled
7693 * horizontally
7694 *
7695 * @see #isHorizontalFadingEdgeEnabled()
7696 * @attr ref android.R.styleable#View_fadingEdge
7697 */
7698 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7699 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7700 if (horizontalFadingEdgeEnabled) {
7701 initScrollCache();
7702 }
7703
7704 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7705 }
7706 }
7707
7708 /**
7709 * <p>Indicate whether the vertical edges are faded when the view is
7710 * scrolled horizontally.</p>
7711 *
7712 * @return true if the vertical edges should are faded on scroll, false
7713 * otherwise
7714 *
7715 * @see #setVerticalFadingEdgeEnabled(boolean)
7716 * @attr ref android.R.styleable#View_fadingEdge
7717 */
7718 public boolean isVerticalFadingEdgeEnabled() {
7719 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7720 }
7721
7722 /**
7723 * <p>Define whether the vertical edges should be faded when this view
7724 * is scrolled vertically.</p>
7725 *
7726 * @param verticalFadingEdgeEnabled true if the vertical edges should
7727 * be faded when the view is scrolled
7728 * vertically
7729 *
7730 * @see #isVerticalFadingEdgeEnabled()
7731 * @attr ref android.R.styleable#View_fadingEdge
7732 */
7733 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7734 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7735 if (verticalFadingEdgeEnabled) {
7736 initScrollCache();
7737 }
7738
7739 mViewFlags ^= FADING_EDGE_VERTICAL;
7740 }
7741 }
7742
7743 /**
7744 * Returns the strength, or intensity, of the top faded edge. The strength is
7745 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7746 * returns 0.0 or 1.0 but no value in between.
7747 *
7748 * Subclasses should override this method to provide a smoother fade transition
7749 * when scrolling occurs.
7750 *
7751 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7752 */
7753 protected float getTopFadingEdgeStrength() {
7754 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7755 }
7756
7757 /**
7758 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7759 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7760 * returns 0.0 or 1.0 but no value in between.
7761 *
7762 * Subclasses should override this method to provide a smoother fade transition
7763 * when scrolling occurs.
7764 *
7765 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7766 */
7767 protected float getBottomFadingEdgeStrength() {
7768 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7769 computeVerticalScrollRange() ? 1.0f : 0.0f;
7770 }
7771
7772 /**
7773 * Returns the strength, or intensity, of the left faded edge. The strength is
7774 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7775 * returns 0.0 or 1.0 but no value in between.
7776 *
7777 * Subclasses should override this method to provide a smoother fade transition
7778 * when scrolling occurs.
7779 *
7780 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7781 */
7782 protected float getLeftFadingEdgeStrength() {
7783 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7784 }
7785
7786 /**
7787 * Returns the strength, or intensity, of the right faded edge. The strength is
7788 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7789 * returns 0.0 or 1.0 but no value in between.
7790 *
7791 * Subclasses should override this method to provide a smoother fade transition
7792 * when scrolling occurs.
7793 *
7794 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7795 */
7796 protected float getRightFadingEdgeStrength() {
7797 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7798 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7799 }
7800
7801 /**
7802 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7803 * scrollbar is not drawn by default.</p>
7804 *
7805 * @return true if the horizontal scrollbar should be painted, false
7806 * otherwise
7807 *
7808 * @see #setHorizontalScrollBarEnabled(boolean)
7809 */
7810 public boolean isHorizontalScrollBarEnabled() {
7811 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7812 }
7813
7814 /**
7815 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7816 * scrollbar is not drawn by default.</p>
7817 *
7818 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7819 * be painted
7820 *
7821 * @see #isHorizontalScrollBarEnabled()
7822 */
7823 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7824 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7825 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007826 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007827 recomputePadding();
7828 }
7829 }
7830
7831 /**
7832 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7833 * scrollbar is not drawn by default.</p>
7834 *
7835 * @return true if the vertical scrollbar should be painted, false
7836 * otherwise
7837 *
7838 * @see #setVerticalScrollBarEnabled(boolean)
7839 */
7840 public boolean isVerticalScrollBarEnabled() {
7841 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7842 }
7843
7844 /**
7845 * <p>Define whether the vertical scrollbar should be drawn or not. The
7846 * scrollbar is not drawn by default.</p>
7847 *
7848 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7849 * be painted
7850 *
7851 * @see #isVerticalScrollBarEnabled()
7852 */
7853 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7854 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7855 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007856 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007857 recomputePadding();
7858 }
7859 }
7860
Adam Powell20232d02010-12-08 21:08:53 -08007861 /**
7862 * @hide
7863 */
7864 protected void recomputePadding() {
7865 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 }
Joe Malin32736f02011-01-19 16:14:20 -08007867
Mike Cleronfe81d382009-09-28 14:22:16 -07007868 /**
7869 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08007870 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007871 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08007872 *
Mike Cleronfe81d382009-09-28 14:22:16 -07007873 */
7874 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
7875 initScrollCache();
7876 final ScrollabilityCache scrollabilityCache = mScrollCache;
7877 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007878 if (fadeScrollbars) {
7879 scrollabilityCache.state = ScrollabilityCache.OFF;
7880 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07007881 scrollabilityCache.state = ScrollabilityCache.ON;
7882 }
7883 }
Joe Malin32736f02011-01-19 16:14:20 -08007884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007885 /**
Joe Malin32736f02011-01-19 16:14:20 -08007886 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007887 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08007888 *
Mike Cleron52f0a642009-09-28 18:21:37 -07007889 * @return true if scrollbar fading is enabled
7890 */
7891 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08007892 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07007893 }
Joe Malin32736f02011-01-19 16:14:20 -08007894
Mike Cleron52f0a642009-09-28 18:21:37 -07007895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007896 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
7897 * inset. When inset, they add to the padding of the view. And the scrollbars
7898 * can be drawn inside the padding area or on the edge of the view. For example,
7899 * if a view has a background drawable and you want to draw the scrollbars
7900 * inside the padding specified by the drawable, you can use
7901 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
7902 * appear at the edge of the view, ignoring the padding, then you can use
7903 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
7904 * @param style the style of the scrollbars. Should be one of
7905 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
7906 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
7907 * @see #SCROLLBARS_INSIDE_OVERLAY
7908 * @see #SCROLLBARS_INSIDE_INSET
7909 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7910 * @see #SCROLLBARS_OUTSIDE_INSET
7911 */
7912 public void setScrollBarStyle(int style) {
7913 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
7914 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07007915 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007916 recomputePadding();
7917 }
7918 }
7919
7920 /**
7921 * <p>Returns the current scrollbar style.</p>
7922 * @return the current scrollbar style
7923 * @see #SCROLLBARS_INSIDE_OVERLAY
7924 * @see #SCROLLBARS_INSIDE_INSET
7925 * @see #SCROLLBARS_OUTSIDE_OVERLAY
7926 * @see #SCROLLBARS_OUTSIDE_INSET
7927 */
7928 public int getScrollBarStyle() {
7929 return mViewFlags & SCROLLBARS_STYLE_MASK;
7930 }
7931
7932 /**
7933 * <p>Compute the horizontal range that the horizontal scrollbar
7934 * represents.</p>
7935 *
7936 * <p>The range is expressed in arbitrary units that must be the same as the
7937 * units used by {@link #computeHorizontalScrollExtent()} and
7938 * {@link #computeHorizontalScrollOffset()}.</p>
7939 *
7940 * <p>The default range is the drawing width of this view.</p>
7941 *
7942 * @return the total horizontal range represented by the horizontal
7943 * scrollbar
7944 *
7945 * @see #computeHorizontalScrollExtent()
7946 * @see #computeHorizontalScrollOffset()
7947 * @see android.widget.ScrollBarDrawable
7948 */
7949 protected int computeHorizontalScrollRange() {
7950 return getWidth();
7951 }
7952
7953 /**
7954 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
7955 * within the horizontal range. This value is used to compute the position
7956 * of the thumb within the scrollbar's track.</p>
7957 *
7958 * <p>The range is expressed in arbitrary units that must be the same as the
7959 * units used by {@link #computeHorizontalScrollRange()} and
7960 * {@link #computeHorizontalScrollExtent()}.</p>
7961 *
7962 * <p>The default offset is the scroll offset of this view.</p>
7963 *
7964 * @return the horizontal offset of the scrollbar's thumb
7965 *
7966 * @see #computeHorizontalScrollRange()
7967 * @see #computeHorizontalScrollExtent()
7968 * @see android.widget.ScrollBarDrawable
7969 */
7970 protected int computeHorizontalScrollOffset() {
7971 return mScrollX;
7972 }
7973
7974 /**
7975 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
7976 * within the horizontal range. This value is used to compute the length
7977 * of the thumb within the scrollbar's track.</p>
7978 *
7979 * <p>The range is expressed in arbitrary units that must be the same as the
7980 * units used by {@link #computeHorizontalScrollRange()} and
7981 * {@link #computeHorizontalScrollOffset()}.</p>
7982 *
7983 * <p>The default extent is the drawing width of this view.</p>
7984 *
7985 * @return the horizontal extent of the scrollbar's thumb
7986 *
7987 * @see #computeHorizontalScrollRange()
7988 * @see #computeHorizontalScrollOffset()
7989 * @see android.widget.ScrollBarDrawable
7990 */
7991 protected int computeHorizontalScrollExtent() {
7992 return getWidth();
7993 }
7994
7995 /**
7996 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
7997 *
7998 * <p>The range is expressed in arbitrary units that must be the same as the
7999 * units used by {@link #computeVerticalScrollExtent()} and
8000 * {@link #computeVerticalScrollOffset()}.</p>
8001 *
8002 * @return the total vertical range represented by the vertical scrollbar
8003 *
8004 * <p>The default range is the drawing height of this view.</p>
8005 *
8006 * @see #computeVerticalScrollExtent()
8007 * @see #computeVerticalScrollOffset()
8008 * @see android.widget.ScrollBarDrawable
8009 */
8010 protected int computeVerticalScrollRange() {
8011 return getHeight();
8012 }
8013
8014 /**
8015 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8016 * within the horizontal range. This value is used to compute the position
8017 * of the thumb within the scrollbar's track.</p>
8018 *
8019 * <p>The range is expressed in arbitrary units that must be the same as the
8020 * units used by {@link #computeVerticalScrollRange()} and
8021 * {@link #computeVerticalScrollExtent()}.</p>
8022 *
8023 * <p>The default offset is the scroll offset of this view.</p>
8024 *
8025 * @return the vertical offset of the scrollbar's thumb
8026 *
8027 * @see #computeVerticalScrollRange()
8028 * @see #computeVerticalScrollExtent()
8029 * @see android.widget.ScrollBarDrawable
8030 */
8031 protected int computeVerticalScrollOffset() {
8032 return mScrollY;
8033 }
8034
8035 /**
8036 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8037 * within the vertical range. This value is used to compute the length
8038 * of the thumb within the scrollbar's track.</p>
8039 *
8040 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008041 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008042 * {@link #computeVerticalScrollOffset()}.</p>
8043 *
8044 * <p>The default extent is the drawing height of this view.</p>
8045 *
8046 * @return the vertical extent of the scrollbar's thumb
8047 *
8048 * @see #computeVerticalScrollRange()
8049 * @see #computeVerticalScrollOffset()
8050 * @see android.widget.ScrollBarDrawable
8051 */
8052 protected int computeVerticalScrollExtent() {
8053 return getHeight();
8054 }
8055
8056 /**
8057 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8058 * scrollbars are painted only if they have been awakened first.</p>
8059 *
8060 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008061 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008062 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008063 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008064 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008065 // scrollbars are drawn only when the animation is running
8066 final ScrollabilityCache cache = mScrollCache;
8067 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008068
Mike Cleronf116bf82009-09-27 19:14:12 -07008069 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008070
Mike Cleronf116bf82009-09-27 19:14:12 -07008071 if (state == ScrollabilityCache.OFF) {
8072 return;
8073 }
Joe Malin32736f02011-01-19 16:14:20 -08008074
Mike Cleronf116bf82009-09-27 19:14:12 -07008075 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008076
Mike Cleronf116bf82009-09-27 19:14:12 -07008077 if (state == ScrollabilityCache.FADING) {
8078 // We're fading -- get our fade interpolation
8079 if (cache.interpolatorValues == null) {
8080 cache.interpolatorValues = new float[1];
8081 }
Joe Malin32736f02011-01-19 16:14:20 -08008082
Mike Cleronf116bf82009-09-27 19:14:12 -07008083 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008084
Mike Cleronf116bf82009-09-27 19:14:12 -07008085 // Stops the animation if we're done
8086 if (cache.scrollBarInterpolator.timeToValues(values) ==
8087 Interpolator.Result.FREEZE_END) {
8088 cache.state = ScrollabilityCache.OFF;
8089 } else {
8090 cache.scrollBar.setAlpha(Math.round(values[0]));
8091 }
Joe Malin32736f02011-01-19 16:14:20 -08008092
8093 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008094 // drawing. We only want this when we're fading so that
8095 // we prevent excessive redraws
8096 invalidate = true;
8097 } else {
8098 // We're just on -- but we may have been fading before so
8099 // reset alpha
8100 cache.scrollBar.setAlpha(255);
8101 }
8102
Joe Malin32736f02011-01-19 16:14:20 -08008103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008104 final int viewFlags = mViewFlags;
8105
8106 final boolean drawHorizontalScrollBar =
8107 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8108 final boolean drawVerticalScrollBar =
8109 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8110 && !isVerticalScrollBarHidden();
8111
8112 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8113 final int width = mRight - mLeft;
8114 final int height = mBottom - mTop;
8115
8116 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117
Mike Reede8853fc2009-09-04 14:01:48 -04008118 final int scrollX = mScrollX;
8119 final int scrollY = mScrollY;
8120 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8121
Mike Cleronf116bf82009-09-27 19:14:12 -07008122 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008124 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008125 int size = scrollBar.getSize(false);
8126 if (size <= 0) {
8127 size = cache.scrollBarSize;
8128 }
8129
Mike Cleronf116bf82009-09-27 19:14:12 -07008130 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008131 computeHorizontalScrollOffset(),
8132 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008133 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008134 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008135 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008136 left = scrollX + (mPaddingLeft & inside);
8137 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8138 bottom = top + size;
8139 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8140 if (invalidate) {
8141 invalidate(left, top, right, bottom);
8142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008143 }
8144
8145 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008146 int size = scrollBar.getSize(true);
8147 if (size <= 0) {
8148 size = cache.scrollBarSize;
8149 }
8150
Mike Reede8853fc2009-09-04 14:01:48 -04008151 scrollBar.setParameters(computeVerticalScrollRange(),
8152 computeVerticalScrollOffset(),
8153 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008154 switch (mVerticalScrollbarPosition) {
8155 default:
8156 case SCROLLBAR_POSITION_DEFAULT:
8157 case SCROLLBAR_POSITION_RIGHT:
8158 left = scrollX + width - size - (mUserPaddingRight & inside);
8159 break;
8160 case SCROLLBAR_POSITION_LEFT:
8161 left = scrollX + (mUserPaddingLeft & inside);
8162 break;
8163 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008164 top = scrollY + (mPaddingTop & inside);
8165 right = left + size;
8166 bottom = scrollY + height - (mUserPaddingBottom & inside);
8167 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8168 if (invalidate) {
8169 invalidate(left, top, right, bottom);
8170 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008171 }
8172 }
8173 }
8174 }
Romain Guy8506ab42009-06-11 17:35:47 -07008175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008176 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008177 * 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 -08008178 * FastScroller is visible.
8179 * @return whether to temporarily hide the vertical scrollbar
8180 * @hide
8181 */
8182 protected boolean isVerticalScrollBarHidden() {
8183 return false;
8184 }
8185
8186 /**
8187 * <p>Draw the horizontal scrollbar if
8188 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8189 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008190 * @param canvas the canvas on which to draw the scrollbar
8191 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008192 *
8193 * @see #isHorizontalScrollBarEnabled()
8194 * @see #computeHorizontalScrollRange()
8195 * @see #computeHorizontalScrollExtent()
8196 * @see #computeHorizontalScrollOffset()
8197 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008198 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008199 */
Romain Guy8fb95422010-08-17 18:38:51 -07008200 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8201 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008202 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008203 scrollBar.draw(canvas);
8204 }
Mike Reede8853fc2009-09-04 14:01:48 -04008205
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008207 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8208 * returns true.</p>
8209 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008210 * @param canvas the canvas on which to draw the scrollbar
8211 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008212 *
8213 * @see #isVerticalScrollBarEnabled()
8214 * @see #computeVerticalScrollRange()
8215 * @see #computeVerticalScrollExtent()
8216 * @see #computeVerticalScrollOffset()
8217 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008218 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008219 */
Romain Guy8fb95422010-08-17 18:38:51 -07008220 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8221 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008222 scrollBar.setBounds(l, t, r, b);
8223 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008224 }
8225
8226 /**
8227 * Implement this to do your drawing.
8228 *
8229 * @param canvas the canvas on which the background will be drawn
8230 */
8231 protected void onDraw(Canvas canvas) {
8232 }
8233
8234 /*
8235 * Caller is responsible for calling requestLayout if necessary.
8236 * (This allows addViewInLayout to not request a new layout.)
8237 */
8238 void assignParent(ViewParent parent) {
8239 if (mParent == null) {
8240 mParent = parent;
8241 } else if (parent == null) {
8242 mParent = null;
8243 } else {
8244 throw new RuntimeException("view " + this + " being added, but"
8245 + " it already has a parent");
8246 }
8247 }
8248
8249 /**
8250 * This is called when the view is attached to a window. At this point it
8251 * has a Surface and will start drawing. Note that this function is
8252 * guaranteed to be called before {@link #onDraw}, however it may be called
8253 * any time before the first onDraw -- including before or after
8254 * {@link #onMeasure}.
8255 *
8256 * @see #onDetachedFromWindow()
8257 */
8258 protected void onAttachedToWindow() {
8259 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8260 mParent.requestTransparentRegion(this);
8261 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008262 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8263 initialAwakenScrollBars();
8264 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8265 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008266 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008267 }
8268
8269 /**
8270 * This is called when the view is detached from a window. At this point it
8271 * no longer has a surface for drawing.
8272 *
8273 * @see #onAttachedToWindow()
8274 */
8275 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008276 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008277
Romain Guya440b002010-02-24 15:57:54 -08008278 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008279 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008280 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008283
8284 if (mHardwareLayer != null) {
8285 mHardwareLayer.destroy();
8286 mHardwareLayer = null;
8287 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008288
Chet Haasedaf98e92011-01-10 14:10:36 -08008289 if (mDisplayList != null) {
8290 mDisplayList.invalidate();
8291 }
8292
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008293 if (mAttachInfo != null) {
8294 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8295 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8296 }
8297
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008298 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008299 }
8300
8301 /**
8302 * @return The number of times this view has been attached to a window
8303 */
8304 protected int getWindowAttachCount() {
8305 return mWindowAttachCount;
8306 }
8307
8308 /**
8309 * Retrieve a unique token identifying the window this view is attached to.
8310 * @return Return the window's token for use in
8311 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8312 */
8313 public IBinder getWindowToken() {
8314 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8315 }
8316
8317 /**
8318 * Retrieve a unique token identifying the top-level "real" window of
8319 * the window that this view is attached to. That is, this is like
8320 * {@link #getWindowToken}, except if the window this view in is a panel
8321 * window (attached to another containing window), then the token of
8322 * the containing window is returned instead.
8323 *
8324 * @return Returns the associated window token, either
8325 * {@link #getWindowToken()} or the containing window's token.
8326 */
8327 public IBinder getApplicationWindowToken() {
8328 AttachInfo ai = mAttachInfo;
8329 if (ai != null) {
8330 IBinder appWindowToken = ai.mPanelParentWindowToken;
8331 if (appWindowToken == null) {
8332 appWindowToken = ai.mWindowToken;
8333 }
8334 return appWindowToken;
8335 }
8336 return null;
8337 }
8338
8339 /**
8340 * Retrieve private session object this view hierarchy is using to
8341 * communicate with the window manager.
8342 * @return the session object to communicate with the window manager
8343 */
8344 /*package*/ IWindowSession getWindowSession() {
8345 return mAttachInfo != null ? mAttachInfo.mSession : null;
8346 }
8347
8348 /**
8349 * @param info the {@link android.view.View.AttachInfo} to associated with
8350 * this view
8351 */
8352 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8353 //System.out.println("Attached! " + this);
8354 mAttachInfo = info;
8355 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008356 // We will need to evaluate the drawable state at least once.
8357 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 if (mFloatingTreeObserver != null) {
8359 info.mTreeObserver.merge(mFloatingTreeObserver);
8360 mFloatingTreeObserver = null;
8361 }
8362 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8363 mAttachInfo.mScrollContainers.add(this);
8364 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8365 }
8366 performCollectViewAttributes(visibility);
8367 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008368
8369 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8370 mOnAttachStateChangeListeners;
8371 if (listeners != null && listeners.size() > 0) {
8372 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8373 // perform the dispatching. The iterator is a safe guard against listeners that
8374 // could mutate the list by calling the various add/remove methods. This prevents
8375 // the array from being modified while we iterate it.
8376 for (OnAttachStateChangeListener listener : listeners) {
8377 listener.onViewAttachedToWindow(this);
8378 }
8379 }
8380
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 int vis = info.mWindowVisibility;
8382 if (vis != GONE) {
8383 onWindowVisibilityChanged(vis);
8384 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008385 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8386 // If nobody has evaluated the drawable state yet, then do it now.
8387 refreshDrawableState();
8388 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008389 }
8390
8391 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 AttachInfo info = mAttachInfo;
8393 if (info != null) {
8394 int vis = info.mWindowVisibility;
8395 if (vis != GONE) {
8396 onWindowVisibilityChanged(GONE);
8397 }
8398 }
8399
8400 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008401
Adam Powell4afd62b2011-02-18 15:02:18 -08008402 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8403 mOnAttachStateChangeListeners;
8404 if (listeners != null && listeners.size() > 0) {
8405 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8406 // perform the dispatching. The iterator is a safe guard against listeners that
8407 // could mutate the list by calling the various add/remove methods. This prevents
8408 // the array from being modified while we iterate it.
8409 for (OnAttachStateChangeListener listener : listeners) {
8410 listener.onViewDetachedFromWindow(this);
8411 }
8412 }
8413
Romain Guy01d5edc2011-01-28 11:28:53 -08008414 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008415 mAttachInfo.mScrollContainers.remove(this);
8416 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8417 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008418
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008419 mAttachInfo = null;
8420 }
8421
8422 /**
8423 * Store this view hierarchy's frozen state into the given container.
8424 *
8425 * @param container The SparseArray in which to save the view's state.
8426 *
8427 * @see #restoreHierarchyState
8428 * @see #dispatchSaveInstanceState
8429 * @see #onSaveInstanceState
8430 */
8431 public void saveHierarchyState(SparseArray<Parcelable> container) {
8432 dispatchSaveInstanceState(container);
8433 }
8434
8435 /**
8436 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8437 * May be overridden to modify how freezing happens to a view's children; for example, some
8438 * views may want to not store state for their children.
8439 *
8440 * @param container The SparseArray in which to save the view's state.
8441 *
8442 * @see #dispatchRestoreInstanceState
8443 * @see #saveHierarchyState
8444 * @see #onSaveInstanceState
8445 */
8446 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8447 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8448 mPrivateFlags &= ~SAVE_STATE_CALLED;
8449 Parcelable state = onSaveInstanceState();
8450 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8451 throw new IllegalStateException(
8452 "Derived class did not call super.onSaveInstanceState()");
8453 }
8454 if (state != null) {
8455 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8456 // + ": " + state);
8457 container.put(mID, state);
8458 }
8459 }
8460 }
8461
8462 /**
8463 * Hook allowing a view to generate a representation of its internal state
8464 * that can later be used to create a new instance with that same state.
8465 * This state should only contain information that is not persistent or can
8466 * not be reconstructed later. For example, you will never store your
8467 * current position on screen because that will be computed again when a
8468 * new instance of the view is placed in its view hierarchy.
8469 * <p>
8470 * Some examples of things you may store here: the current cursor position
8471 * in a text view (but usually not the text itself since that is stored in a
8472 * content provider or other persistent storage), the currently selected
8473 * item in a list view.
8474 *
8475 * @return Returns a Parcelable object containing the view's current dynamic
8476 * state, or null if there is nothing interesting to save. The
8477 * default implementation returns null.
8478 * @see #onRestoreInstanceState
8479 * @see #saveHierarchyState
8480 * @see #dispatchSaveInstanceState
8481 * @see #setSaveEnabled(boolean)
8482 */
8483 protected Parcelable onSaveInstanceState() {
8484 mPrivateFlags |= SAVE_STATE_CALLED;
8485 return BaseSavedState.EMPTY_STATE;
8486 }
8487
8488 /**
8489 * Restore this view hierarchy's frozen state from the given container.
8490 *
8491 * @param container The SparseArray which holds previously frozen states.
8492 *
8493 * @see #saveHierarchyState
8494 * @see #dispatchRestoreInstanceState
8495 * @see #onRestoreInstanceState
8496 */
8497 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8498 dispatchRestoreInstanceState(container);
8499 }
8500
8501 /**
8502 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8503 * children. May be overridden to modify how restoreing happens to a view's children; for
8504 * example, some views may want to not store state for their children.
8505 *
8506 * @param container The SparseArray which holds previously saved state.
8507 *
8508 * @see #dispatchSaveInstanceState
8509 * @see #restoreHierarchyState
8510 * @see #onRestoreInstanceState
8511 */
8512 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8513 if (mID != NO_ID) {
8514 Parcelable state = container.get(mID);
8515 if (state != null) {
8516 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8517 // + ": " + state);
8518 mPrivateFlags &= ~SAVE_STATE_CALLED;
8519 onRestoreInstanceState(state);
8520 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8521 throw new IllegalStateException(
8522 "Derived class did not call super.onRestoreInstanceState()");
8523 }
8524 }
8525 }
8526 }
8527
8528 /**
8529 * Hook allowing a view to re-apply a representation of its internal state that had previously
8530 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8531 * null state.
8532 *
8533 * @param state The frozen state that had previously been returned by
8534 * {@link #onSaveInstanceState}.
8535 *
8536 * @see #onSaveInstanceState
8537 * @see #restoreHierarchyState
8538 * @see #dispatchRestoreInstanceState
8539 */
8540 protected void onRestoreInstanceState(Parcelable state) {
8541 mPrivateFlags |= SAVE_STATE_CALLED;
8542 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008543 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8544 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008545 + "when two views of different type have the same id in the same hierarchy. "
8546 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008547 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008548 }
8549 }
8550
8551 /**
8552 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8553 *
8554 * @return the drawing start time in milliseconds
8555 */
8556 public long getDrawingTime() {
8557 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8558 }
8559
8560 /**
8561 * <p>Enables or disables the duplication of the parent's state into this view. When
8562 * duplication is enabled, this view gets its drawable state from its parent rather
8563 * than from its own internal properties.</p>
8564 *
8565 * <p>Note: in the current implementation, setting this property to true after the
8566 * view was added to a ViewGroup might have no effect at all. This property should
8567 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8568 *
8569 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8570 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008571 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008572 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8573 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008574 *
8575 * @param enabled True to enable duplication of the parent's drawable state, false
8576 * to disable it.
8577 *
8578 * @see #getDrawableState()
8579 * @see #isDuplicateParentStateEnabled()
8580 */
8581 public void setDuplicateParentStateEnabled(boolean enabled) {
8582 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8583 }
8584
8585 /**
8586 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8587 *
8588 * @return True if this view's drawable state is duplicated from the parent,
8589 * false otherwise
8590 *
8591 * @see #getDrawableState()
8592 * @see #setDuplicateParentStateEnabled(boolean)
8593 */
8594 public boolean isDuplicateParentStateEnabled() {
8595 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8596 }
8597
8598 /**
Romain Guy171c5922011-01-06 10:04:23 -08008599 * <p>Specifies the type of layer backing this view. The layer can be
8600 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8601 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008602 *
Romain Guy171c5922011-01-06 10:04:23 -08008603 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8604 * instance that controls how the layer is composed on screen. The following
8605 * properties of the paint are taken into account when composing the layer:</p>
8606 * <ul>
8607 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8608 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8609 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8610 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008611 *
Romain Guy171c5922011-01-06 10:04:23 -08008612 * <p>If this view has an alpha value set to < 1.0 by calling
8613 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8614 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8615 * equivalent to setting a hardware layer on this view and providing a paint with
8616 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008617 *
Romain Guy171c5922011-01-06 10:04:23 -08008618 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8619 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8620 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008621 *
Romain Guy171c5922011-01-06 10:04:23 -08008622 * @param layerType The ype of layer to use with this view, must be one of
8623 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8624 * {@link #LAYER_TYPE_HARDWARE}
8625 * @param paint The paint used to compose the layer. This argument is optional
8626 * and can be null. It is ignored when the layer type is
8627 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008628 *
8629 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008630 * @see #LAYER_TYPE_NONE
8631 * @see #LAYER_TYPE_SOFTWARE
8632 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008633 * @see #setAlpha(float)
8634 *
Romain Guy171c5922011-01-06 10:04:23 -08008635 * @attr ref android.R.styleable#View_layerType
8636 */
8637 public void setLayerType(int layerType, Paint paint) {
8638 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008639 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008640 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8641 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008642
Romain Guyd6cd5722011-01-17 14:42:41 -08008643 if (layerType == mLayerType) {
8644 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8645 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008646 invalidateParentCaches();
8647 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008648 }
8649 return;
8650 }
Romain Guy171c5922011-01-06 10:04:23 -08008651
8652 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008653 switch (mLayerType) {
8654 case LAYER_TYPE_SOFTWARE:
8655 if (mDrawingCache != null) {
8656 mDrawingCache.recycle();
8657 mDrawingCache = null;
8658 }
Joe Malin32736f02011-01-19 16:14:20 -08008659
Romain Guy6c319ca2011-01-11 14:29:25 -08008660 if (mUnscaledDrawingCache != null) {
8661 mUnscaledDrawingCache.recycle();
8662 mUnscaledDrawingCache = null;
8663 }
8664 break;
8665 case LAYER_TYPE_HARDWARE:
8666 if (mHardwareLayer != null) {
8667 mHardwareLayer.destroy();
8668 mHardwareLayer = null;
8669 }
8670 break;
8671 default:
8672 break;
Romain Guy171c5922011-01-06 10:04:23 -08008673 }
8674
8675 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008676 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8677 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8678 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008679
Romain Guy0fd89bf2011-01-26 15:41:30 -08008680 invalidateParentCaches();
8681 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008682 }
8683
8684 /**
8685 * Indicates what type of layer is currently associated with this view. By default
8686 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8687 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8688 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008689 *
Romain Guy171c5922011-01-06 10:04:23 -08008690 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8691 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008692 *
8693 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008694 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008695 * @see #LAYER_TYPE_NONE
8696 * @see #LAYER_TYPE_SOFTWARE
8697 * @see #LAYER_TYPE_HARDWARE
8698 */
8699 public int getLayerType() {
8700 return mLayerType;
8701 }
Joe Malin32736f02011-01-19 16:14:20 -08008702
Romain Guy6c319ca2011-01-11 14:29:25 -08008703 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008704 * Forces this view's layer to be created and this view to be rendered
8705 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8706 * invoking this method will have no effect.
8707 *
8708 * This method can for instance be used to render a view into its layer before
8709 * starting an animation. If this view is complex, rendering into the layer
8710 * before starting the animation will avoid skipping frames.
8711 *
8712 * @throws IllegalStateException If this view is not attached to a window
8713 *
8714 * @see #setLayerType(int, android.graphics.Paint)
8715 */
8716 public void buildLayer() {
8717 if (mLayerType == LAYER_TYPE_NONE) return;
8718
8719 if (mAttachInfo == null) {
8720 throw new IllegalStateException("This view must be attached to a window first");
8721 }
8722
8723 switch (mLayerType) {
8724 case LAYER_TYPE_HARDWARE:
8725 getHardwareLayer();
8726 break;
8727 case LAYER_TYPE_SOFTWARE:
8728 buildDrawingCache(true);
8729 break;
8730 }
8731 }
8732
8733 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008734 * <p>Returns a hardware layer that can be used to draw this view again
8735 * without executing its draw method.</p>
8736 *
8737 * @return A HardwareLayer ready to render, or null if an error occurred.
8738 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008739 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008740 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8741 return null;
8742 }
8743
8744 final int width = mRight - mLeft;
8745 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008746
Romain Guy6c319ca2011-01-11 14:29:25 -08008747 if (width == 0 || height == 0) {
8748 return null;
8749 }
8750
8751 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8752 if (mHardwareLayer == null) {
8753 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8754 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008755 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008756 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8757 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008758 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008759 }
8760
Romain Guy5e7f7662011-01-24 22:35:56 -08008761 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8762 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8763 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008764 try {
8765 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008766 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008767 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008768
Chet Haase88172fe2011-03-07 17:36:33 -08008769 final int restoreCount = canvas.save();
8770
Romain Guy6c319ca2011-01-11 14:29:25 -08008771 computeScroll();
8772 canvas.translate(-mScrollX, -mScrollY);
8773
Romain Guy6c319ca2011-01-11 14:29:25 -08008774 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008775
Romain Guy6c319ca2011-01-11 14:29:25 -08008776 // Fast path for layouts with no backgrounds
8777 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8778 mPrivateFlags &= ~DIRTY_MASK;
8779 dispatchDraw(canvas);
8780 } else {
8781 draw(canvas);
8782 }
Joe Malin32736f02011-01-19 16:14:20 -08008783
Chet Haase88172fe2011-03-07 17:36:33 -08008784 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008785 } finally {
8786 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008787 mHardwareLayer.end(currentCanvas);
8788 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008789 }
8790 }
8791
8792 return mHardwareLayer;
8793 }
Romain Guy171c5922011-01-06 10:04:23 -08008794
8795 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008796 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8797 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8798 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8799 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8800 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8801 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008802 *
Romain Guy171c5922011-01-06 10:04:23 -08008803 * <p>Enabling the drawing cache is similar to
8804 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008805 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8806 * drawing cache has no effect on rendering because the system uses a different mechanism
8807 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8808 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8809 * for information on how to enable software and hardware layers.</p>
8810 *
8811 * <p>This API can be used to manually generate
8812 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8813 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008814 *
8815 * @param enabled true to enable the drawing cache, false otherwise
8816 *
8817 * @see #isDrawingCacheEnabled()
8818 * @see #getDrawingCache()
8819 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008820 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008821 */
8822 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008823 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008824 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8825 }
8826
8827 /**
8828 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8829 *
8830 * @return true if the drawing cache is enabled
8831 *
8832 * @see #setDrawingCacheEnabled(boolean)
8833 * @see #getDrawingCache()
8834 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008835 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 public boolean isDrawingCacheEnabled() {
8837 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8838 }
8839
8840 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008841 * Debugging utility which recursively outputs the dirty state of a view and its
8842 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008843 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008844 * @hide
8845 */
Romain Guy676b1732011-02-14 14:45:33 -08008846 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008847 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8848 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8849 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8850 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8851 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8852 if (clear) {
8853 mPrivateFlags &= clearMask;
8854 }
8855 if (this instanceof ViewGroup) {
8856 ViewGroup parent = (ViewGroup) this;
8857 final int count = parent.getChildCount();
8858 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008859 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008860 child.outputDirtyFlags(indent + " ", clear, clearMask);
8861 }
8862 }
8863 }
8864
8865 /**
8866 * This method is used by ViewGroup to cause its children to restore or recreate their
8867 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
8868 * to recreate its own display list, which would happen if it went through the normal
8869 * draw/dispatchDraw mechanisms.
8870 *
8871 * @hide
8872 */
8873 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08008874
8875 /**
8876 * A view that is not attached or hardware accelerated cannot create a display list.
8877 * This method checks these conditions and returns the appropriate result.
8878 *
8879 * @return true if view has the ability to create a display list, false otherwise.
8880 *
8881 * @hide
8882 */
8883 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08008884 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08008885 }
Joe Malin32736f02011-01-19 16:14:20 -08008886
Chet Haasedaf98e92011-01-10 14:10:36 -08008887 /**
Romain Guyb051e892010-09-28 19:09:36 -07008888 * <p>Returns a display list that can be used to draw this view again
8889 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008890 *
Romain Guyb051e892010-09-28 19:09:36 -07008891 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08008892 *
8893 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07008894 */
Chet Haasedaf98e92011-01-10 14:10:36 -08008895 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08008896 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07008897 return null;
8898 }
8899
Chet Haasedaf98e92011-01-10 14:10:36 -08008900 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
8901 mDisplayList == null || !mDisplayList.isValid() ||
8902 mRecreateDisplayList)) {
8903 // Don't need to recreate the display list, just need to tell our
8904 // children to restore/recreate theirs
8905 if (mDisplayList != null && mDisplayList.isValid() &&
8906 !mRecreateDisplayList) {
8907 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8908 mPrivateFlags &= ~DIRTY_MASK;
8909 dispatchGetDisplayList();
8910
8911 return mDisplayList;
8912 }
8913
8914 // If we got here, we're recreating it. Mark it as such to ensure that
8915 // we copy in child display lists into ours in drawChild()
8916 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08008917 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008918 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
8919 // If we're creating a new display list, make sure our parent gets invalidated
8920 // since they will need to recreate their display list to account for this
8921 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08008922 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08008923 }
Romain Guyb051e892010-09-28 19:09:36 -07008924
8925 final HardwareCanvas canvas = mDisplayList.start();
8926 try {
8927 int width = mRight - mLeft;
8928 int height = mBottom - mTop;
8929
8930 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08008931 // The dirty rect should always be null for a display list
8932 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07008933
Chet Haase88172fe2011-03-07 17:36:33 -08008934 final int restoreCount = canvas.save();
8935
Chet Haasedaf98e92011-01-10 14:10:36 -08008936 computeScroll();
8937 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008938 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008939
Romain Guyb051e892010-09-28 19:09:36 -07008940 // Fast path for layouts with no backgrounds
8941 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8942 mPrivateFlags &= ~DIRTY_MASK;
8943 dispatchDraw(canvas);
8944 } else {
8945 draw(canvas);
8946 }
Joe Malin32736f02011-01-19 16:14:20 -08008947
Chet Haase88172fe2011-03-07 17:36:33 -08008948 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07008949 } finally {
8950 canvas.onPostDraw();
8951
8952 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07008953 }
Chet Haase77785f92011-01-25 23:22:09 -08008954 } else {
8955 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
8956 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07008957 }
8958
8959 return mDisplayList;
8960 }
8961
8962 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07008963 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008964 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008965 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008966 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008967 * @see #getDrawingCache(boolean)
8968 */
8969 public Bitmap getDrawingCache() {
8970 return getDrawingCache(false);
8971 }
8972
8973 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
8975 * is null when caching is disabled. If caching is enabled and the cache is not ready,
8976 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
8977 * draw from the cache when the cache is enabled. To benefit from the cache, you must
8978 * request the drawing cache by calling this method and draw it on screen if the
8979 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008980 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008981 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
8982 * this method will create a bitmap of the same size as this view. Because this bitmap
8983 * will be drawn scaled by the parent ViewGroup, the result on screen might show
8984 * scaling artifacts. To avoid such artifacts, you should call this method by setting
8985 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
8986 * size than the view. This implies that your application must be able to handle this
8987 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008988 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008989 * @param autoScale Indicates whether the generated bitmap should be scaled based on
8990 * the current density of the screen when the application is in compatibility
8991 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 *
Romain Guyfbd8f692009-06-26 14:51:58 -07008993 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08008994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 * @see #setDrawingCacheEnabled(boolean)
8996 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07008997 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 * @see #destroyDrawingCache()
8999 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009000 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009001 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9002 return null;
9003 }
9004 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009005 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009006 }
Romain Guy02890fd2010-08-06 17:58:44 -07009007 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009008 }
9009
9010 /**
9011 * <p>Frees the resources used by the drawing cache. If you call
9012 * {@link #buildDrawingCache()} manually without calling
9013 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9014 * should cleanup the cache with this method afterwards.</p>
9015 *
9016 * @see #setDrawingCacheEnabled(boolean)
9017 * @see #buildDrawingCache()
9018 * @see #getDrawingCache()
9019 */
9020 public void destroyDrawingCache() {
9021 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009022 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009023 mDrawingCache = null;
9024 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009025 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009026 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009027 mUnscaledDrawingCache = null;
9028 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009029 }
9030
9031 /**
9032 * Setting a solid background color for the drawing cache's bitmaps will improve
9033 * perfromance and memory usage. Note, though that this should only be used if this
9034 * view will always be drawn on top of a solid color.
9035 *
9036 * @param color The background color to use for the drawing cache's bitmap
9037 *
9038 * @see #setDrawingCacheEnabled(boolean)
9039 * @see #buildDrawingCache()
9040 * @see #getDrawingCache()
9041 */
9042 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009043 if (color != mDrawingCacheBackgroundColor) {
9044 mDrawingCacheBackgroundColor = color;
9045 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9046 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009047 }
9048
9049 /**
9050 * @see #setDrawingCacheBackgroundColor(int)
9051 *
9052 * @return The background color to used for the drawing cache's bitmap
9053 */
9054 public int getDrawingCacheBackgroundColor() {
9055 return mDrawingCacheBackgroundColor;
9056 }
9057
9058 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009059 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009060 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009061 * @see #buildDrawingCache(boolean)
9062 */
9063 public void buildDrawingCache() {
9064 buildDrawingCache(false);
9065 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009066
Romain Guyfbd8f692009-06-26 14:51:58 -07009067 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009068 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9069 *
9070 * <p>If you call {@link #buildDrawingCache()} manually without calling
9071 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9072 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009073 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009074 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9075 * this method will create a bitmap of the same size as this view. Because this bitmap
9076 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9077 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9078 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9079 * size than the view. This implies that your application must be able to handle this
9080 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009081 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009082 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9083 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009084 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009085 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009086 *
9087 * @see #getDrawingCache()
9088 * @see #destroyDrawingCache()
9089 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009090 public void buildDrawingCache(boolean autoScale) {
9091 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009092 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009093 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009094
9095 if (ViewDebug.TRACE_HIERARCHY) {
9096 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9097 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009098
Romain Guy8506ab42009-06-11 17:35:47 -07009099 int width = mRight - mLeft;
9100 int height = mBottom - mTop;
9101
9102 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009103 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009104
Romain Guye1123222009-06-29 14:24:56 -07009105 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009106 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9107 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009108 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009109
9110 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009111 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009112 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009113
9114 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009115 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009116 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009117 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9118 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009119 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009120 return;
9121 }
9122
9123 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009124 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009125
9126 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009127 Bitmap.Config quality;
9128 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009129 // Never pick ARGB_4444 because it looks awful
9130 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009131 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9132 case DRAWING_CACHE_QUALITY_AUTO:
9133 quality = Bitmap.Config.ARGB_8888;
9134 break;
9135 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009136 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009137 break;
9138 case DRAWING_CACHE_QUALITY_HIGH:
9139 quality = Bitmap.Config.ARGB_8888;
9140 break;
9141 default:
9142 quality = Bitmap.Config.ARGB_8888;
9143 break;
9144 }
9145 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009146 // Optimization for translucent windows
9147 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009148 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009149 }
9150
9151 // Try to cleanup memory
9152 if (bitmap != null) bitmap.recycle();
9153
9154 try {
9155 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009156 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009157 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009158 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009159 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009160 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009161 }
Adam Powell26153a32010-11-08 15:22:27 -08009162 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009163 } catch (OutOfMemoryError e) {
9164 // If there is not enough memory to create the bitmap cache, just
9165 // ignore the issue as bitmap caches are not required to draw the
9166 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009167 if (autoScale) {
9168 mDrawingCache = null;
9169 } else {
9170 mUnscaledDrawingCache = null;
9171 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009172 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009173 return;
9174 }
9175
9176 clear = drawingCacheBackgroundColor != 0;
9177 }
9178
9179 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 if (attachInfo != null) {
9181 canvas = attachInfo.mCanvas;
9182 if (canvas == null) {
9183 canvas = new Canvas();
9184 }
9185 canvas.setBitmap(bitmap);
9186 // Temporarily clobber the cached Canvas in case one of our children
9187 // is also using a drawing cache. Without this, the children would
9188 // steal the canvas by attaching their own bitmap to it and bad, bad
9189 // thing would happen (invisible views, corrupted drawings, etc.)
9190 attachInfo.mCanvas = null;
9191 } else {
9192 // This case should hopefully never or seldom happen
9193 canvas = new Canvas(bitmap);
9194 }
9195
9196 if (clear) {
9197 bitmap.eraseColor(drawingCacheBackgroundColor);
9198 }
9199
9200 computeScroll();
9201 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009202
Romain Guye1123222009-06-29 14:24:56 -07009203 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009204 final float scale = attachInfo.mApplicationScale;
9205 canvas.scale(scale, scale);
9206 }
Joe Malin32736f02011-01-19 16:14:20 -08009207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009208 canvas.translate(-mScrollX, -mScrollY);
9209
Romain Guy5bcdff42009-05-14 21:27:18 -07009210 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009211 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9212 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009213 mPrivateFlags |= DRAWING_CACHE_VALID;
9214 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009215
9216 // Fast path for layouts with no backgrounds
9217 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9218 if (ViewDebug.TRACE_HIERARCHY) {
9219 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9220 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009221 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009222 dispatchDraw(canvas);
9223 } else {
9224 draw(canvas);
9225 }
9226
9227 canvas.restoreToCount(restoreCount);
9228
9229 if (attachInfo != null) {
9230 // Restore the cached Canvas for our siblings
9231 attachInfo.mCanvas = canvas;
9232 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009233 }
9234 }
9235
9236 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009237 * Create a snapshot of the view into a bitmap. We should probably make
9238 * some form of this public, but should think about the API.
9239 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009240 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009241 int width = mRight - mLeft;
9242 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009243
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009244 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009245 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009246 width = (int) ((width * scale) + 0.5f);
9247 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009248
Romain Guy8c11e312009-09-14 15:15:30 -07009249 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009250 if (bitmap == null) {
9251 throw new OutOfMemoryError();
9252 }
9253
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009254 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009255
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009256 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009257 if (attachInfo != null) {
9258 canvas = attachInfo.mCanvas;
9259 if (canvas == null) {
9260 canvas = new Canvas();
9261 }
9262 canvas.setBitmap(bitmap);
9263 // Temporarily clobber the cached Canvas in case one of our children
9264 // is also using a drawing cache. Without this, the children would
9265 // steal the canvas by attaching their own bitmap to it and bad, bad
9266 // things would happen (invisible views, corrupted drawings, etc.)
9267 attachInfo.mCanvas = null;
9268 } else {
9269 // This case should hopefully never or seldom happen
9270 canvas = new Canvas(bitmap);
9271 }
9272
Romain Guy5bcdff42009-05-14 21:27:18 -07009273 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009274 bitmap.eraseColor(backgroundColor);
9275 }
9276
9277 computeScroll();
9278 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009279 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009280 canvas.translate(-mScrollX, -mScrollY);
9281
Romain Guy5bcdff42009-05-14 21:27:18 -07009282 // Temporarily remove the dirty mask
9283 int flags = mPrivateFlags;
9284 mPrivateFlags &= ~DIRTY_MASK;
9285
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009286 // Fast path for layouts with no backgrounds
9287 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9288 dispatchDraw(canvas);
9289 } else {
9290 draw(canvas);
9291 }
9292
Romain Guy5bcdff42009-05-14 21:27:18 -07009293 mPrivateFlags = flags;
9294
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009295 canvas.restoreToCount(restoreCount);
9296
9297 if (attachInfo != null) {
9298 // Restore the cached Canvas for our siblings
9299 attachInfo.mCanvas = canvas;
9300 }
Romain Guy8506ab42009-06-11 17:35:47 -07009301
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009302 return bitmap;
9303 }
9304
9305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009306 * Indicates whether this View is currently in edit mode. A View is usually
9307 * in edit mode when displayed within a developer tool. For instance, if
9308 * this View is being drawn by a visual user interface builder, this method
9309 * should return true.
9310 *
9311 * Subclasses should check the return value of this method to provide
9312 * different behaviors if their normal behavior might interfere with the
9313 * host environment. For instance: the class spawns a thread in its
9314 * constructor, the drawing code relies on device-specific features, etc.
9315 *
9316 * This method is usually checked in the drawing code of custom widgets.
9317 *
9318 * @return True if this View is in edit mode, false otherwise.
9319 */
9320 public boolean isInEditMode() {
9321 return false;
9322 }
9323
9324 /**
9325 * If the View draws content inside its padding and enables fading edges,
9326 * it needs to support padding offsets. Padding offsets are added to the
9327 * fading edges to extend the length of the fade so that it covers pixels
9328 * drawn inside the padding.
9329 *
9330 * Subclasses of this class should override this method if they need
9331 * to draw content inside the padding.
9332 *
9333 * @return True if padding offset must be applied, false otherwise.
9334 *
9335 * @see #getLeftPaddingOffset()
9336 * @see #getRightPaddingOffset()
9337 * @see #getTopPaddingOffset()
9338 * @see #getBottomPaddingOffset()
9339 *
9340 * @since CURRENT
9341 */
9342 protected boolean isPaddingOffsetRequired() {
9343 return false;
9344 }
9345
9346 /**
9347 * Amount by which to extend the left fading region. Called only when
9348 * {@link #isPaddingOffsetRequired()} returns true.
9349 *
9350 * @return The left padding offset in pixels.
9351 *
9352 * @see #isPaddingOffsetRequired()
9353 *
9354 * @since CURRENT
9355 */
9356 protected int getLeftPaddingOffset() {
9357 return 0;
9358 }
9359
9360 /**
9361 * Amount by which to extend the right fading region. Called only when
9362 * {@link #isPaddingOffsetRequired()} returns true.
9363 *
9364 * @return The right padding offset in pixels.
9365 *
9366 * @see #isPaddingOffsetRequired()
9367 *
9368 * @since CURRENT
9369 */
9370 protected int getRightPaddingOffset() {
9371 return 0;
9372 }
9373
9374 /**
9375 * Amount by which to extend the top fading region. Called only when
9376 * {@link #isPaddingOffsetRequired()} returns true.
9377 *
9378 * @return The top padding offset in pixels.
9379 *
9380 * @see #isPaddingOffsetRequired()
9381 *
9382 * @since CURRENT
9383 */
9384 protected int getTopPaddingOffset() {
9385 return 0;
9386 }
9387
9388 /**
9389 * Amount by which to extend the bottom fading region. Called only when
9390 * {@link #isPaddingOffsetRequired()} returns true.
9391 *
9392 * @return The bottom padding offset in pixels.
9393 *
9394 * @see #isPaddingOffsetRequired()
9395 *
9396 * @since CURRENT
9397 */
9398 protected int getBottomPaddingOffset() {
9399 return 0;
9400 }
9401
9402 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009403 * <p>Indicates whether this view is attached to an hardware accelerated
9404 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009405 *
Romain Guy2bffd262010-09-12 17:40:02 -07009406 * <p>Even if this method returns true, it does not mean that every call
9407 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9408 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9409 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9410 * window is hardware accelerated,
9411 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9412 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009413 *
Romain Guy2bffd262010-09-12 17:40:02 -07009414 * @return True if the view is attached to a window and the window is
9415 * hardware accelerated; false in any other case.
9416 */
9417 public boolean isHardwareAccelerated() {
9418 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9419 }
Joe Malin32736f02011-01-19 16:14:20 -08009420
Romain Guy2bffd262010-09-12 17:40:02 -07009421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009422 * Manually render this view (and all of its children) to the given Canvas.
9423 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009424 * called. When implementing a view, implement {@link #onDraw} instead of
9425 * overriding this method. If you do need to override this method, call
9426 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009427 *
9428 * @param canvas The Canvas to which the View is rendered.
9429 */
9430 public void draw(Canvas canvas) {
9431 if (ViewDebug.TRACE_HIERARCHY) {
9432 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9433 }
9434
Romain Guy5bcdff42009-05-14 21:27:18 -07009435 final int privateFlags = mPrivateFlags;
9436 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9437 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9438 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009440 /*
9441 * Draw traversal performs several drawing steps which must be executed
9442 * in the appropriate order:
9443 *
9444 * 1. Draw the background
9445 * 2. If necessary, save the canvas' layers to prepare for fading
9446 * 3. Draw view's content
9447 * 4. Draw children
9448 * 5. If necessary, draw the fading edges and restore layers
9449 * 6. Draw decorations (scrollbars for instance)
9450 */
9451
9452 // Step 1, draw the background, if needed
9453 int saveCount;
9454
Romain Guy24443ea2009-05-11 11:56:30 -07009455 if (!dirtyOpaque) {
9456 final Drawable background = mBGDrawable;
9457 if (background != null) {
9458 final int scrollX = mScrollX;
9459 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009460
Romain Guy24443ea2009-05-11 11:56:30 -07009461 if (mBackgroundSizeChanged) {
9462 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9463 mBackgroundSizeChanged = false;
9464 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009465
Romain Guy24443ea2009-05-11 11:56:30 -07009466 if ((scrollX | scrollY) == 0) {
9467 background.draw(canvas);
9468 } else {
9469 canvas.translate(scrollX, scrollY);
9470 background.draw(canvas);
9471 canvas.translate(-scrollX, -scrollY);
9472 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009473 }
9474 }
9475
9476 // skip step 2 & 5 if possible (common case)
9477 final int viewFlags = mViewFlags;
9478 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9479 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9480 if (!verticalEdges && !horizontalEdges) {
9481 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009482 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009483
9484 // Step 4, draw the children
9485 dispatchDraw(canvas);
9486
9487 // Step 6, draw decorations (scrollbars)
9488 onDrawScrollBars(canvas);
9489
9490 // we're done...
9491 return;
9492 }
9493
9494 /*
9495 * Here we do the full fledged routine...
9496 * (this is an uncommon case where speed matters less,
9497 * this is why we repeat some of the tests that have been
9498 * done above)
9499 */
9500
9501 boolean drawTop = false;
9502 boolean drawBottom = false;
9503 boolean drawLeft = false;
9504 boolean drawRight = false;
9505
9506 float topFadeStrength = 0.0f;
9507 float bottomFadeStrength = 0.0f;
9508 float leftFadeStrength = 0.0f;
9509 float rightFadeStrength = 0.0f;
9510
9511 // Step 2, save the canvas' layers
9512 int paddingLeft = mPaddingLeft;
9513 int paddingTop = mPaddingTop;
9514
9515 final boolean offsetRequired = isPaddingOffsetRequired();
9516 if (offsetRequired) {
9517 paddingLeft += getLeftPaddingOffset();
9518 paddingTop += getTopPaddingOffset();
9519 }
9520
9521 int left = mScrollX + paddingLeft;
9522 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9523 int top = mScrollY + paddingTop;
9524 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9525
9526 if (offsetRequired) {
9527 right += getRightPaddingOffset();
9528 bottom += getBottomPaddingOffset();
9529 }
9530
9531 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009532 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9533 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009534
9535 // clip the fade length if top and bottom fades overlap
9536 // overlapping fades produce odd-looking artifacts
9537 if (verticalEdges && (top + length > bottom - length)) {
9538 length = (bottom - top) / 2;
9539 }
9540
9541 // also clip horizontal fades if necessary
9542 if (horizontalEdges && (left + length > right - length)) {
9543 length = (right - left) / 2;
9544 }
9545
9546 if (verticalEdges) {
9547 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009548 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009549 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009550 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009551 }
9552
9553 if (horizontalEdges) {
9554 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009555 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009556 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009557 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009558 }
9559
9560 saveCount = canvas.getSaveCount();
9561
9562 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009563 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009564 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9565
9566 if (drawTop) {
9567 canvas.saveLayer(left, top, right, top + length, null, flags);
9568 }
9569
9570 if (drawBottom) {
9571 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9572 }
9573
9574 if (drawLeft) {
9575 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9576 }
9577
9578 if (drawRight) {
9579 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9580 }
9581 } else {
9582 scrollabilityCache.setFadeColor(solidColor);
9583 }
9584
9585 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009586 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009587
9588 // Step 4, draw the children
9589 dispatchDraw(canvas);
9590
9591 // Step 5, draw the fade effect and restore layers
9592 final Paint p = scrollabilityCache.paint;
9593 final Matrix matrix = scrollabilityCache.matrix;
9594 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009595
9596 if (drawTop) {
9597 matrix.setScale(1, fadeHeight * topFadeStrength);
9598 matrix.postTranslate(left, top);
9599 fade.setLocalMatrix(matrix);
9600 canvas.drawRect(left, top, right, top + length, p);
9601 }
9602
9603 if (drawBottom) {
9604 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9605 matrix.postRotate(180);
9606 matrix.postTranslate(left, bottom);
9607 fade.setLocalMatrix(matrix);
9608 canvas.drawRect(left, bottom - length, right, bottom, p);
9609 }
9610
9611 if (drawLeft) {
9612 matrix.setScale(1, fadeHeight * leftFadeStrength);
9613 matrix.postRotate(-90);
9614 matrix.postTranslate(left, top);
9615 fade.setLocalMatrix(matrix);
9616 canvas.drawRect(left, top, left + length, bottom, p);
9617 }
9618
9619 if (drawRight) {
9620 matrix.setScale(1, fadeHeight * rightFadeStrength);
9621 matrix.postRotate(90);
9622 matrix.postTranslate(right, top);
9623 fade.setLocalMatrix(matrix);
9624 canvas.drawRect(right - length, top, right, bottom, p);
9625 }
9626
9627 canvas.restoreToCount(saveCount);
9628
9629 // Step 6, draw decorations (scrollbars)
9630 onDrawScrollBars(canvas);
9631 }
9632
9633 /**
9634 * Override this if your view is known to always be drawn on top of a solid color background,
9635 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9636 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9637 * should be set to 0xFF.
9638 *
9639 * @see #setVerticalFadingEdgeEnabled
9640 * @see #setHorizontalFadingEdgeEnabled
9641 *
9642 * @return The known solid color background for this view, or 0 if the color may vary
9643 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009644 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009645 public int getSolidColor() {
9646 return 0;
9647 }
9648
9649 /**
9650 * Build a human readable string representation of the specified view flags.
9651 *
9652 * @param flags the view flags to convert to a string
9653 * @return a String representing the supplied flags
9654 */
9655 private static String printFlags(int flags) {
9656 String output = "";
9657 int numFlags = 0;
9658 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9659 output += "TAKES_FOCUS";
9660 numFlags++;
9661 }
9662
9663 switch (flags & VISIBILITY_MASK) {
9664 case INVISIBLE:
9665 if (numFlags > 0) {
9666 output += " ";
9667 }
9668 output += "INVISIBLE";
9669 // USELESS HERE numFlags++;
9670 break;
9671 case GONE:
9672 if (numFlags > 0) {
9673 output += " ";
9674 }
9675 output += "GONE";
9676 // USELESS HERE numFlags++;
9677 break;
9678 default:
9679 break;
9680 }
9681 return output;
9682 }
9683
9684 /**
9685 * Build a human readable string representation of the specified private
9686 * view flags.
9687 *
9688 * @param privateFlags the private view flags to convert to a string
9689 * @return a String representing the supplied flags
9690 */
9691 private static String printPrivateFlags(int privateFlags) {
9692 String output = "";
9693 int numFlags = 0;
9694
9695 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9696 output += "WANTS_FOCUS";
9697 numFlags++;
9698 }
9699
9700 if ((privateFlags & FOCUSED) == FOCUSED) {
9701 if (numFlags > 0) {
9702 output += " ";
9703 }
9704 output += "FOCUSED";
9705 numFlags++;
9706 }
9707
9708 if ((privateFlags & SELECTED) == SELECTED) {
9709 if (numFlags > 0) {
9710 output += " ";
9711 }
9712 output += "SELECTED";
9713 numFlags++;
9714 }
9715
9716 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9717 if (numFlags > 0) {
9718 output += " ";
9719 }
9720 output += "IS_ROOT_NAMESPACE";
9721 numFlags++;
9722 }
9723
9724 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9725 if (numFlags > 0) {
9726 output += " ";
9727 }
9728 output += "HAS_BOUNDS";
9729 numFlags++;
9730 }
9731
9732 if ((privateFlags & DRAWN) == DRAWN) {
9733 if (numFlags > 0) {
9734 output += " ";
9735 }
9736 output += "DRAWN";
9737 // USELESS HERE numFlags++;
9738 }
9739 return output;
9740 }
9741
9742 /**
9743 * <p>Indicates whether or not this view's layout will be requested during
9744 * the next hierarchy layout pass.</p>
9745 *
9746 * @return true if the layout will be forced during next layout pass
9747 */
9748 public boolean isLayoutRequested() {
9749 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9750 }
9751
9752 /**
9753 * Assign a size and position to a view and all of its
9754 * descendants
9755 *
9756 * <p>This is the second phase of the layout mechanism.
9757 * (The first is measuring). In this phase, each parent calls
9758 * layout on all of its children to position them.
9759 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009760 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009761 *
Chet Haase9c087442011-01-12 16:20:16 -08009762 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009763 * Derived classes with children should override
9764 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009765 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009766 *
9767 * @param l Left position, relative to parent
9768 * @param t Top position, relative to parent
9769 * @param r Right position, relative to parent
9770 * @param b Bottom position, relative to parent
9771 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009772 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009773 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009774 int oldL = mLeft;
9775 int oldT = mTop;
9776 int oldB = mBottom;
9777 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009778 boolean changed = setFrame(l, t, r, b);
9779 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9780 if (ViewDebug.TRACE_HIERARCHY) {
9781 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9782 }
9783
9784 onLayout(changed, l, t, r, b);
9785 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009786
9787 if (mOnLayoutChangeListeners != null) {
9788 ArrayList<OnLayoutChangeListener> listenersCopy =
9789 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9790 int numListeners = listenersCopy.size();
9791 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009792 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009793 }
9794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009795 }
9796 mPrivateFlags &= ~FORCE_LAYOUT;
9797 }
9798
9799 /**
9800 * Called from layout when this view should
9801 * assign a size and position to each of its children.
9802 *
9803 * Derived classes with children should override
9804 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009805 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009806 * @param changed This is a new size or position for this view
9807 * @param left Left position, relative to parent
9808 * @param top Top position, relative to parent
9809 * @param right Right position, relative to parent
9810 * @param bottom Bottom position, relative to parent
9811 */
9812 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9813 }
9814
9815 /**
9816 * Assign a size and position to this view.
9817 *
9818 * This is called from layout.
9819 *
9820 * @param left Left position, relative to parent
9821 * @param top Top position, relative to parent
9822 * @param right Right position, relative to parent
9823 * @param bottom Bottom position, relative to parent
9824 * @return true if the new size and position are different than the
9825 * previous ones
9826 * {@hide}
9827 */
9828 protected boolean setFrame(int left, int top, int right, int bottom) {
9829 boolean changed = false;
9830
9831 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009832 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009833 + right + "," + bottom + ")");
9834 }
9835
9836 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9837 changed = true;
9838
9839 // Remember our drawn bit
9840 int drawn = mPrivateFlags & DRAWN;
9841
9842 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009843 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009844
9845
9846 int oldWidth = mRight - mLeft;
9847 int oldHeight = mBottom - mTop;
9848
9849 mLeft = left;
9850 mTop = top;
9851 mRight = right;
9852 mBottom = bottom;
9853
9854 mPrivateFlags |= HAS_BOUNDS;
9855
9856 int newWidth = right - left;
9857 int newHeight = bottom - top;
9858
9859 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009860 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9861 // A change in dimension means an auto-centered pivot point changes, too
9862 mMatrixDirty = true;
9863 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009864 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
9865 }
9866
9867 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
9868 // If we are visible, force the DRAWN bit to on so that
9869 // this invalidate will go through (at least to our parent).
9870 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009871 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009872 // the DRAWN bit.
9873 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009874 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -08009875 // parent display list may need to be recreated based on a change in the bounds
9876 // of any child
9877 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009878 }
9879
9880 // Reset drawn bit to original value (invalidate turns it off)
9881 mPrivateFlags |= drawn;
9882
9883 mBackgroundSizeChanged = true;
9884 }
9885 return changed;
9886 }
9887
9888 /**
9889 * Finalize inflating a view from XML. This is called as the last phase
9890 * of inflation, after all child views have been added.
9891 *
9892 * <p>Even if the subclass overrides onFinishInflate, they should always be
9893 * sure to call the super method, so that we get called.
9894 */
9895 protected void onFinishInflate() {
9896 }
9897
9898 /**
9899 * Returns the resources associated with this view.
9900 *
9901 * @return Resources object.
9902 */
9903 public Resources getResources() {
9904 return mResources;
9905 }
9906
9907 /**
9908 * Invalidates the specified Drawable.
9909 *
9910 * @param drawable the drawable to invalidate
9911 */
9912 public void invalidateDrawable(Drawable drawable) {
9913 if (verifyDrawable(drawable)) {
9914 final Rect dirty = drawable.getBounds();
9915 final int scrollX = mScrollX;
9916 final int scrollY = mScrollY;
9917
9918 invalidate(dirty.left + scrollX, dirty.top + scrollY,
9919 dirty.right + scrollX, dirty.bottom + scrollY);
9920 }
9921 }
9922
9923 /**
9924 * Schedules an action on a drawable to occur at a specified time.
9925 *
9926 * @param who the recipient of the action
9927 * @param what the action to run on the drawable
9928 * @param when the time at which the action must occur. Uses the
9929 * {@link SystemClock#uptimeMillis} timebase.
9930 */
9931 public void scheduleDrawable(Drawable who, Runnable what, long when) {
9932 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9933 mAttachInfo.mHandler.postAtTime(what, who, when);
9934 }
9935 }
9936
9937 /**
9938 * Cancels a scheduled action on a drawable.
9939 *
9940 * @param who the recipient of the action
9941 * @param what the action to cancel
9942 */
9943 public void unscheduleDrawable(Drawable who, Runnable what) {
9944 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
9945 mAttachInfo.mHandler.removeCallbacks(what, who);
9946 }
9947 }
9948
9949 /**
9950 * Unschedule any events associated with the given Drawable. This can be
9951 * used when selecting a new Drawable into a view, so that the previous
9952 * one is completely unscheduled.
9953 *
9954 * @param who The Drawable to unschedule.
9955 *
9956 * @see #drawableStateChanged
9957 */
9958 public void unscheduleDrawable(Drawable who) {
9959 if (mAttachInfo != null) {
9960 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
9961 }
9962 }
9963
9964 /**
9965 * If your view subclass is displaying its own Drawable objects, it should
9966 * override this function and return true for any Drawable it is
9967 * displaying. This allows animations for those drawables to be
9968 * scheduled.
9969 *
9970 * <p>Be sure to call through to the super class when overriding this
9971 * function.
9972 *
9973 * @param who The Drawable to verify. Return true if it is one you are
9974 * displaying, else return the result of calling through to the
9975 * super class.
9976 *
9977 * @return boolean If true than the Drawable is being displayed in the
9978 * view; else false and it is not allowed to animate.
9979 *
9980 * @see #unscheduleDrawable
9981 * @see #drawableStateChanged
9982 */
9983 protected boolean verifyDrawable(Drawable who) {
9984 return who == mBGDrawable;
9985 }
9986
9987 /**
9988 * This function is called whenever the state of the view changes in such
9989 * a way that it impacts the state of drawables being shown.
9990 *
9991 * <p>Be sure to call through to the superclass when overriding this
9992 * function.
9993 *
9994 * @see Drawable#setState
9995 */
9996 protected void drawableStateChanged() {
9997 Drawable d = mBGDrawable;
9998 if (d != null && d.isStateful()) {
9999 d.setState(getDrawableState());
10000 }
10001 }
10002
10003 /**
10004 * Call this to force a view to update its drawable state. This will cause
10005 * drawableStateChanged to be called on this view. Views that are interested
10006 * in the new state should call getDrawableState.
10007 *
10008 * @see #drawableStateChanged
10009 * @see #getDrawableState
10010 */
10011 public void refreshDrawableState() {
10012 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10013 drawableStateChanged();
10014
10015 ViewParent parent = mParent;
10016 if (parent != null) {
10017 parent.childDrawableStateChanged(this);
10018 }
10019 }
10020
10021 /**
10022 * Return an array of resource IDs of the drawable states representing the
10023 * current state of the view.
10024 *
10025 * @return The current drawable state
10026 *
10027 * @see Drawable#setState
10028 * @see #drawableStateChanged
10029 * @see #onCreateDrawableState
10030 */
10031 public final int[] getDrawableState() {
10032 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10033 return mDrawableState;
10034 } else {
10035 mDrawableState = onCreateDrawableState(0);
10036 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10037 return mDrawableState;
10038 }
10039 }
10040
10041 /**
10042 * Generate the new {@link android.graphics.drawable.Drawable} state for
10043 * this view. This is called by the view
10044 * system when the cached Drawable state is determined to be invalid. To
10045 * retrieve the current state, you should use {@link #getDrawableState}.
10046 *
10047 * @param extraSpace if non-zero, this is the number of extra entries you
10048 * would like in the returned array in which you can place your own
10049 * states.
10050 *
10051 * @return Returns an array holding the current {@link Drawable} state of
10052 * the view.
10053 *
10054 * @see #mergeDrawableStates
10055 */
10056 protected int[] onCreateDrawableState(int extraSpace) {
10057 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10058 mParent instanceof View) {
10059 return ((View) mParent).onCreateDrawableState(extraSpace);
10060 }
10061
10062 int[] drawableState;
10063
10064 int privateFlags = mPrivateFlags;
10065
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010066 int viewStateIndex = 0;
10067 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10068 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10069 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010070 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010071 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10072 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010073 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested) {
10074 // This is set if HW acceleration is requested, even if the current
10075 // process doesn't allow it. This is just to allow app preview
10076 // windows to better match their app.
10077 viewStateIndex |= VIEW_STATE_ACCELERATED;
10078 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010079 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010080
Christopher Tate3d4bf172011-03-28 16:16:46 -070010081 final int privateFlags2 = mPrivateFlags2;
10082 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10083 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010085 drawableState = VIEW_STATE_SETS[viewStateIndex];
10086
10087 //noinspection ConstantIfStatement
10088 if (false) {
10089 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10090 Log.i("View", toString()
10091 + " pressed=" + ((privateFlags & PRESSED) != 0)
10092 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10093 + " fo=" + hasFocus()
10094 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010095 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010096 + ": " + Arrays.toString(drawableState));
10097 }
10098
10099 if (extraSpace == 0) {
10100 return drawableState;
10101 }
10102
10103 final int[] fullState;
10104 if (drawableState != null) {
10105 fullState = new int[drawableState.length + extraSpace];
10106 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10107 } else {
10108 fullState = new int[extraSpace];
10109 }
10110
10111 return fullState;
10112 }
10113
10114 /**
10115 * Merge your own state values in <var>additionalState</var> into the base
10116 * state values <var>baseState</var> that were returned by
10117 * {@link #onCreateDrawableState}.
10118 *
10119 * @param baseState The base state values returned by
10120 * {@link #onCreateDrawableState}, which will be modified to also hold your
10121 * own additional state values.
10122 *
10123 * @param additionalState The additional state values you would like
10124 * added to <var>baseState</var>; this array is not modified.
10125 *
10126 * @return As a convenience, the <var>baseState</var> array you originally
10127 * passed into the function is returned.
10128 *
10129 * @see #onCreateDrawableState
10130 */
10131 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10132 final int N = baseState.length;
10133 int i = N - 1;
10134 while (i >= 0 && baseState[i] == 0) {
10135 i--;
10136 }
10137 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10138 return baseState;
10139 }
10140
10141 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010142 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10143 * on all Drawable objects associated with this view.
10144 */
10145 public void jumpDrawablesToCurrentState() {
10146 if (mBGDrawable != null) {
10147 mBGDrawable.jumpToCurrentState();
10148 }
10149 }
10150
10151 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010152 * Sets the background color for this view.
10153 * @param color the color of the background
10154 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010155 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010156 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010157 if (mBGDrawable instanceof ColorDrawable) {
10158 ((ColorDrawable) mBGDrawable).setColor(color);
10159 } else {
10160 setBackgroundDrawable(new ColorDrawable(color));
10161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162 }
10163
10164 /**
10165 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010166 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010167 * @param resid The identifier of the resource.
10168 * @attr ref android.R.styleable#View_background
10169 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010170 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010171 public void setBackgroundResource(int resid) {
10172 if (resid != 0 && resid == mBackgroundResource) {
10173 return;
10174 }
10175
10176 Drawable d= null;
10177 if (resid != 0) {
10178 d = mResources.getDrawable(resid);
10179 }
10180 setBackgroundDrawable(d);
10181
10182 mBackgroundResource = resid;
10183 }
10184
10185 /**
10186 * Set the background to a given Drawable, or remove the background. If the
10187 * background has padding, this View's padding is set to the background's
10188 * padding. However, when a background is removed, this View's padding isn't
10189 * touched. If setting the padding is desired, please use
10190 * {@link #setPadding(int, int, int, int)}.
10191 *
10192 * @param d The Drawable to use as the background, or null to remove the
10193 * background
10194 */
10195 public void setBackgroundDrawable(Drawable d) {
10196 boolean requestLayout = false;
10197
10198 mBackgroundResource = 0;
10199
10200 /*
10201 * Regardless of whether we're setting a new background or not, we want
10202 * to clear the previous drawable.
10203 */
10204 if (mBGDrawable != null) {
10205 mBGDrawable.setCallback(null);
10206 unscheduleDrawable(mBGDrawable);
10207 }
10208
10209 if (d != null) {
10210 Rect padding = sThreadLocal.get();
10211 if (padding == null) {
10212 padding = new Rect();
10213 sThreadLocal.set(padding);
10214 }
10215 if (d.getPadding(padding)) {
10216 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10217 }
10218
10219 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10220 // if it has a different minimum size, we should layout again
10221 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10222 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10223 requestLayout = true;
10224 }
10225
10226 d.setCallback(this);
10227 if (d.isStateful()) {
10228 d.setState(getDrawableState());
10229 }
10230 d.setVisible(getVisibility() == VISIBLE, false);
10231 mBGDrawable = d;
10232
10233 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10234 mPrivateFlags &= ~SKIP_DRAW;
10235 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10236 requestLayout = true;
10237 }
10238 } else {
10239 /* Remove the background */
10240 mBGDrawable = null;
10241
10242 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10243 /*
10244 * This view ONLY drew the background before and we're removing
10245 * the background, so now it won't draw anything
10246 * (hence we SKIP_DRAW)
10247 */
10248 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10249 mPrivateFlags |= SKIP_DRAW;
10250 }
10251
10252 /*
10253 * When the background is set, we try to apply its padding to this
10254 * View. When the background is removed, we don't touch this View's
10255 * padding. This is noted in the Javadocs. Hence, we don't need to
10256 * requestLayout(), the invalidate() below is sufficient.
10257 */
10258
10259 // The old background's minimum size could have affected this
10260 // View's layout, so let's requestLayout
10261 requestLayout = true;
10262 }
10263
Romain Guy8f1344f52009-05-15 16:03:59 -070010264 computeOpaqueFlags();
10265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010266 if (requestLayout) {
10267 requestLayout();
10268 }
10269
10270 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010271 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010272 }
10273
10274 /**
10275 * Gets the background drawable
10276 * @return The drawable used as the background for this view, if any.
10277 */
10278 public Drawable getBackground() {
10279 return mBGDrawable;
10280 }
10281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010282 /**
10283 * Sets the padding. The view may add on the space required to display
10284 * the scrollbars, depending on the style and visibility of the scrollbars.
10285 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10286 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10287 * from the values set in this call.
10288 *
10289 * @attr ref android.R.styleable#View_padding
10290 * @attr ref android.R.styleable#View_paddingBottom
10291 * @attr ref android.R.styleable#View_paddingLeft
10292 * @attr ref android.R.styleable#View_paddingRight
10293 * @attr ref android.R.styleable#View_paddingTop
10294 * @param left the left padding in pixels
10295 * @param top the top padding in pixels
10296 * @param right the right padding in pixels
10297 * @param bottom the bottom padding in pixels
10298 */
10299 public void setPadding(int left, int top, int right, int bottom) {
10300 boolean changed = false;
10301
Adam Powell20232d02010-12-08 21:08:53 -080010302 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010303 mUserPaddingRight = right;
10304 mUserPaddingBottom = bottom;
10305
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010306 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010307
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010308 // Common case is there are no scroll bars.
10309 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010310 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010311 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10312 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010313 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010314 switch (mVerticalScrollbarPosition) {
10315 case SCROLLBAR_POSITION_DEFAULT:
10316 case SCROLLBAR_POSITION_RIGHT:
10317 right += offset;
10318 break;
10319 case SCROLLBAR_POSITION_LEFT:
10320 left += offset;
10321 break;
10322 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010323 }
Adam Powell20232d02010-12-08 21:08:53 -080010324 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010325 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10326 ? 0 : getHorizontalScrollbarHeight();
10327 }
10328 }
Romain Guy8506ab42009-06-11 17:35:47 -070010329
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010330 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010331 changed = true;
10332 mPaddingLeft = left;
10333 }
10334 if (mPaddingTop != top) {
10335 changed = true;
10336 mPaddingTop = top;
10337 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010338 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010339 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010340 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010341 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010342 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010343 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010344 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010345 }
10346
10347 if (changed) {
10348 requestLayout();
10349 }
10350 }
10351
10352 /**
10353 * Returns the top padding of this view.
10354 *
10355 * @return the top padding in pixels
10356 */
10357 public int getPaddingTop() {
10358 return mPaddingTop;
10359 }
10360
10361 /**
10362 * Returns the bottom padding of this view. If there are inset and enabled
10363 * scrollbars, this value may include the space required to display the
10364 * scrollbars as well.
10365 *
10366 * @return the bottom padding in pixels
10367 */
10368 public int getPaddingBottom() {
10369 return mPaddingBottom;
10370 }
10371
10372 /**
10373 * Returns the left padding of this view. If there are inset and enabled
10374 * scrollbars, this value may include the space required to display the
10375 * scrollbars as well.
10376 *
10377 * @return the left padding in pixels
10378 */
10379 public int getPaddingLeft() {
10380 return mPaddingLeft;
10381 }
10382
10383 /**
10384 * Returns the right padding of this view. If there are inset and enabled
10385 * scrollbars, this value may include the space required to display the
10386 * scrollbars as well.
10387 *
10388 * @return the right padding in pixels
10389 */
10390 public int getPaddingRight() {
10391 return mPaddingRight;
10392 }
10393
10394 /**
10395 * Changes the selection state of this view. A view can be selected or not.
10396 * Note that selection is not the same as focus. Views are typically
10397 * selected in the context of an AdapterView like ListView or GridView;
10398 * the selected view is the view that is highlighted.
10399 *
10400 * @param selected true if the view must be selected, false otherwise
10401 */
10402 public void setSelected(boolean selected) {
10403 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10404 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010405 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010406 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010407 refreshDrawableState();
10408 dispatchSetSelected(selected);
10409 }
10410 }
10411
10412 /**
10413 * Dispatch setSelected to all of this View's children.
10414 *
10415 * @see #setSelected(boolean)
10416 *
10417 * @param selected The new selected state
10418 */
10419 protected void dispatchSetSelected(boolean selected) {
10420 }
10421
10422 /**
10423 * Indicates the selection state of this view.
10424 *
10425 * @return true if the view is selected, false otherwise
10426 */
10427 @ViewDebug.ExportedProperty
10428 public boolean isSelected() {
10429 return (mPrivateFlags & SELECTED) != 0;
10430 }
10431
10432 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010433 * Changes the activated state of this view. A view can be activated or not.
10434 * Note that activation is not the same as selection. Selection is
10435 * a transient property, representing the view (hierarchy) the user is
10436 * currently interacting with. Activation is a longer-term state that the
10437 * user can move views in and out of. For example, in a list view with
10438 * single or multiple selection enabled, the views in the current selection
10439 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10440 * here.) The activated state is propagated down to children of the view it
10441 * is set on.
10442 *
10443 * @param activated true if the view must be activated, false otherwise
10444 */
10445 public void setActivated(boolean activated) {
10446 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10447 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010448 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010449 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010450 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010451 }
10452 }
10453
10454 /**
10455 * Dispatch setActivated to all of this View's children.
10456 *
10457 * @see #setActivated(boolean)
10458 *
10459 * @param activated The new activated state
10460 */
10461 protected void dispatchSetActivated(boolean activated) {
10462 }
10463
10464 /**
10465 * Indicates the activation state of this view.
10466 *
10467 * @return true if the view is activated, false otherwise
10468 */
10469 @ViewDebug.ExportedProperty
10470 public boolean isActivated() {
10471 return (mPrivateFlags & ACTIVATED) != 0;
10472 }
10473
10474 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010475 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10476 * observer can be used to get notifications when global events, like
10477 * layout, happen.
10478 *
10479 * The returned ViewTreeObserver observer is not guaranteed to remain
10480 * valid for the lifetime of this View. If the caller of this method keeps
10481 * a long-lived reference to ViewTreeObserver, it should always check for
10482 * the return value of {@link ViewTreeObserver#isAlive()}.
10483 *
10484 * @return The ViewTreeObserver for this view's hierarchy.
10485 */
10486 public ViewTreeObserver getViewTreeObserver() {
10487 if (mAttachInfo != null) {
10488 return mAttachInfo.mTreeObserver;
10489 }
10490 if (mFloatingTreeObserver == null) {
10491 mFloatingTreeObserver = new ViewTreeObserver();
10492 }
10493 return mFloatingTreeObserver;
10494 }
10495
10496 /**
10497 * <p>Finds the topmost view in the current view hierarchy.</p>
10498 *
10499 * @return the topmost view containing this view
10500 */
10501 public View getRootView() {
10502 if (mAttachInfo != null) {
10503 final View v = mAttachInfo.mRootView;
10504 if (v != null) {
10505 return v;
10506 }
10507 }
Romain Guy8506ab42009-06-11 17:35:47 -070010508
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010509 View parent = this;
10510
10511 while (parent.mParent != null && parent.mParent instanceof View) {
10512 parent = (View) parent.mParent;
10513 }
10514
10515 return parent;
10516 }
10517
10518 /**
10519 * <p>Computes the coordinates of this view on the screen. The argument
10520 * must be an array of two integers. After the method returns, the array
10521 * contains the x and y location in that order.</p>
10522 *
10523 * @param location an array of two integers in which to hold the coordinates
10524 */
10525 public void getLocationOnScreen(int[] location) {
10526 getLocationInWindow(location);
10527
10528 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010529 if (info != null) {
10530 location[0] += info.mWindowLeft;
10531 location[1] += info.mWindowTop;
10532 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 }
10534
10535 /**
10536 * <p>Computes the coordinates of this view in its window. The argument
10537 * must be an array of two integers. After the method returns, the array
10538 * contains the x and y location in that order.</p>
10539 *
10540 * @param location an array of two integers in which to hold the coordinates
10541 */
10542 public void getLocationInWindow(int[] location) {
10543 if (location == null || location.length < 2) {
10544 throw new IllegalArgumentException("location must be an array of "
10545 + "two integers");
10546 }
10547
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010548 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10549 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550
10551 ViewParent viewParent = mParent;
10552 while (viewParent instanceof View) {
10553 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010554 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10555 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010556 viewParent = view.mParent;
10557 }
Romain Guy8506ab42009-06-11 17:35:47 -070010558
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010559 if (viewParent instanceof ViewRoot) {
10560 // *cough*
10561 final ViewRoot vr = (ViewRoot)viewParent;
10562 location[1] -= vr.mCurScrollY;
10563 }
10564 }
10565
10566 /**
10567 * {@hide}
10568 * @param id the id of the view to be found
10569 * @return the view of the specified id, null if cannot be found
10570 */
10571 protected View findViewTraversal(int id) {
10572 if (id == mID) {
10573 return this;
10574 }
10575 return null;
10576 }
10577
10578 /**
10579 * {@hide}
10580 * @param tag the tag of the view to be found
10581 * @return the view of specified tag, null if cannot be found
10582 */
10583 protected View findViewWithTagTraversal(Object tag) {
10584 if (tag != null && tag.equals(mTag)) {
10585 return this;
10586 }
10587 return null;
10588 }
10589
10590 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010591 * {@hide}
10592 * @param predicate The predicate to evaluate.
10593 * @return The first view that matches the predicate or null.
10594 */
10595 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10596 if (predicate.apply(this)) {
10597 return this;
10598 }
10599 return null;
10600 }
10601
10602 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603 * Look for a child view with the given id. If this view has the given
10604 * id, return this view.
10605 *
10606 * @param id The id to search for.
10607 * @return The view that has the given id in the hierarchy or null
10608 */
10609 public final View findViewById(int id) {
10610 if (id < 0) {
10611 return null;
10612 }
10613 return findViewTraversal(id);
10614 }
10615
10616 /**
10617 * Look for a child view with the given tag. If this view has the given
10618 * tag, return this view.
10619 *
10620 * @param tag The tag to search for, using "tag.equals(getTag())".
10621 * @return The View that has the given tag in the hierarchy or null
10622 */
10623 public final View findViewWithTag(Object tag) {
10624 if (tag == null) {
10625 return null;
10626 }
10627 return findViewWithTagTraversal(tag);
10628 }
10629
10630 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010631 * {@hide}
10632 * Look for a child view that matches the specified predicate.
10633 * If this view matches the predicate, return this view.
10634 *
10635 * @param predicate The predicate to evaluate.
10636 * @return The first view that matches the predicate or null.
10637 */
10638 public final View findViewByPredicate(Predicate<View> predicate) {
10639 return findViewByPredicateTraversal(predicate);
10640 }
10641
10642 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010643 * Sets the identifier for this view. The identifier does not have to be
10644 * unique in this view's hierarchy. The identifier should be a positive
10645 * number.
10646 *
10647 * @see #NO_ID
10648 * @see #getId
10649 * @see #findViewById
10650 *
10651 * @param id a number used to identify the view
10652 *
10653 * @attr ref android.R.styleable#View_id
10654 */
10655 public void setId(int id) {
10656 mID = id;
10657 }
10658
10659 /**
10660 * {@hide}
10661 *
10662 * @param isRoot true if the view belongs to the root namespace, false
10663 * otherwise
10664 */
10665 public void setIsRootNamespace(boolean isRoot) {
10666 if (isRoot) {
10667 mPrivateFlags |= IS_ROOT_NAMESPACE;
10668 } else {
10669 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10670 }
10671 }
10672
10673 /**
10674 * {@hide}
10675 *
10676 * @return true if the view belongs to the root namespace, false otherwise
10677 */
10678 public boolean isRootNamespace() {
10679 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10680 }
10681
10682 /**
10683 * Returns this view's identifier.
10684 *
10685 * @return a positive integer used to identify the view or {@link #NO_ID}
10686 * if the view has no ID
10687 *
10688 * @see #setId
10689 * @see #findViewById
10690 * @attr ref android.R.styleable#View_id
10691 */
10692 @ViewDebug.CapturedViewProperty
10693 public int getId() {
10694 return mID;
10695 }
10696
10697 /**
10698 * Returns this view's tag.
10699 *
10700 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010701 *
10702 * @see #setTag(Object)
10703 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010704 */
10705 @ViewDebug.ExportedProperty
10706 public Object getTag() {
10707 return mTag;
10708 }
10709
10710 /**
10711 * Sets the tag associated with this view. A tag can be used to mark
10712 * a view in its hierarchy and does not have to be unique within the
10713 * hierarchy. Tags can also be used to store data within a view without
10714 * resorting to another data structure.
10715 *
10716 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010717 *
10718 * @see #getTag()
10719 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010720 */
10721 public void setTag(final Object tag) {
10722 mTag = tag;
10723 }
10724
10725 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010726 * Returns the tag associated with this view and the specified key.
10727 *
10728 * @param key The key identifying the tag
10729 *
10730 * @return the Object stored in this view as a tag
10731 *
10732 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010733 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010734 */
10735 public Object getTag(int key) {
10736 SparseArray<Object> tags = null;
10737 synchronized (sTagsLock) {
10738 if (sTags != null) {
10739 tags = sTags.get(this);
10740 }
10741 }
10742
10743 if (tags != null) return tags.get(key);
10744 return null;
10745 }
10746
10747 /**
10748 * Sets a tag associated with this view and a key. A tag can be used
10749 * to mark a view in its hierarchy and does not have to be unique within
10750 * the hierarchy. Tags can also be used to store data within a view
10751 * without resorting to another data structure.
10752 *
10753 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010754 * application to ensure it is unique (see the <a
10755 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10756 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010757 * the Android framework or not associated with any package will cause
10758 * an {@link IllegalArgumentException} to be thrown.
10759 *
10760 * @param key The key identifying the tag
10761 * @param tag An Object to tag the view with
10762 *
10763 * @throws IllegalArgumentException If they specified key is not valid
10764 *
10765 * @see #setTag(Object)
10766 * @see #getTag(int)
10767 */
10768 public void setTag(int key, final Object tag) {
10769 // If the package id is 0x00 or 0x01, it's either an undefined package
10770 // or a framework id
10771 if ((key >>> 24) < 2) {
10772 throw new IllegalArgumentException("The key must be an application-specific "
10773 + "resource id.");
10774 }
10775
10776 setTagInternal(this, key, tag);
10777 }
10778
10779 /**
10780 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10781 * framework id.
10782 *
10783 * @hide
10784 */
10785 public void setTagInternal(int key, Object tag) {
10786 if ((key >>> 24) != 0x1) {
10787 throw new IllegalArgumentException("The key must be a framework-specific "
10788 + "resource id.");
10789 }
10790
Romain Guy8506ab42009-06-11 17:35:47 -070010791 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010792 }
10793
10794 private static void setTagInternal(View view, int key, Object tag) {
10795 SparseArray<Object> tags = null;
10796 synchronized (sTagsLock) {
10797 if (sTags == null) {
10798 sTags = new WeakHashMap<View, SparseArray<Object>>();
10799 } else {
10800 tags = sTags.get(view);
10801 }
10802 }
10803
10804 if (tags == null) {
10805 tags = new SparseArray<Object>(2);
10806 synchronized (sTagsLock) {
10807 sTags.put(view, tags);
10808 }
10809 }
10810
10811 tags.put(key, tag);
10812 }
10813
10814 /**
Romain Guy13922e02009-05-12 17:56:14 -070010815 * @param consistency The type of consistency. See ViewDebug for more information.
10816 *
10817 * @hide
10818 */
10819 protected boolean dispatchConsistencyCheck(int consistency) {
10820 return onConsistencyCheck(consistency);
10821 }
10822
10823 /**
10824 * Method that subclasses should implement to check their consistency. The type of
10825 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010826 *
Romain Guy13922e02009-05-12 17:56:14 -070010827 * @param consistency The type of consistency. See ViewDebug for more information.
10828 *
10829 * @throws IllegalStateException if the view is in an inconsistent state.
10830 *
10831 * @hide
10832 */
10833 protected boolean onConsistencyCheck(int consistency) {
10834 boolean result = true;
10835
10836 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10837 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10838
10839 if (checkLayout) {
10840 if (getParent() == null) {
10841 result = false;
10842 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10843 "View " + this + " does not have a parent.");
10844 }
10845
10846 if (mAttachInfo == null) {
10847 result = false;
10848 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10849 "View " + this + " is not attached to a window.");
10850 }
10851 }
10852
10853 if (checkDrawing) {
10854 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10855 // from their draw() method
10856
10857 if ((mPrivateFlags & DRAWN) != DRAWN &&
10858 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10859 result = false;
10860 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10861 "View " + this + " was invalidated but its drawing cache is valid.");
10862 }
10863 }
10864
10865 return result;
10866 }
10867
10868 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 * Prints information about this view in the log output, with the tag
10870 * {@link #VIEW_LOG_TAG}.
10871 *
10872 * @hide
10873 */
10874 public void debug() {
10875 debug(0);
10876 }
10877
10878 /**
10879 * Prints information about this view in the log output, with the tag
10880 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
10881 * indentation defined by the <code>depth</code>.
10882 *
10883 * @param depth the indentation level
10884 *
10885 * @hide
10886 */
10887 protected void debug(int depth) {
10888 String output = debugIndent(depth - 1);
10889
10890 output += "+ " + this;
10891 int id = getId();
10892 if (id != -1) {
10893 output += " (id=" + id + ")";
10894 }
10895 Object tag = getTag();
10896 if (tag != null) {
10897 output += " (tag=" + tag + ")";
10898 }
10899 Log.d(VIEW_LOG_TAG, output);
10900
10901 if ((mPrivateFlags & FOCUSED) != 0) {
10902 output = debugIndent(depth) + " FOCUSED";
10903 Log.d(VIEW_LOG_TAG, output);
10904 }
10905
10906 output = debugIndent(depth);
10907 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
10908 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
10909 + "} ";
10910 Log.d(VIEW_LOG_TAG, output);
10911
10912 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
10913 || mPaddingBottom != 0) {
10914 output = debugIndent(depth);
10915 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
10916 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
10917 Log.d(VIEW_LOG_TAG, output);
10918 }
10919
10920 output = debugIndent(depth);
10921 output += "mMeasureWidth=" + mMeasuredWidth +
10922 " mMeasureHeight=" + mMeasuredHeight;
10923 Log.d(VIEW_LOG_TAG, output);
10924
10925 output = debugIndent(depth);
10926 if (mLayoutParams == null) {
10927 output += "BAD! no layout params";
10928 } else {
10929 output = mLayoutParams.debug(output);
10930 }
10931 Log.d(VIEW_LOG_TAG, output);
10932
10933 output = debugIndent(depth);
10934 output += "flags={";
10935 output += View.printFlags(mViewFlags);
10936 output += "}";
10937 Log.d(VIEW_LOG_TAG, output);
10938
10939 output = debugIndent(depth);
10940 output += "privateFlags={";
10941 output += View.printPrivateFlags(mPrivateFlags);
10942 output += "}";
10943 Log.d(VIEW_LOG_TAG, output);
10944 }
10945
10946 /**
10947 * Creates an string of whitespaces used for indentation.
10948 *
10949 * @param depth the indentation level
10950 * @return a String containing (depth * 2 + 3) * 2 white spaces
10951 *
10952 * @hide
10953 */
10954 protected static String debugIndent(int depth) {
10955 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
10956 for (int i = 0; i < (depth * 2) + 3; i++) {
10957 spaces.append(' ').append(' ');
10958 }
10959 return spaces.toString();
10960 }
10961
10962 /**
10963 * <p>Return the offset of the widget's text baseline from the widget's top
10964 * boundary. If this widget does not support baseline alignment, this
10965 * method returns -1. </p>
10966 *
10967 * @return the offset of the baseline within the widget's bounds or -1
10968 * if baseline alignment is not supported
10969 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070010970 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010971 public int getBaseline() {
10972 return -1;
10973 }
10974
10975 /**
10976 * Call this when something has changed which has invalidated the
10977 * layout of this view. This will schedule a layout pass of the view
10978 * tree.
10979 */
10980 public void requestLayout() {
10981 if (ViewDebug.TRACE_HIERARCHY) {
10982 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
10983 }
10984
10985 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080010986 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010987
10988 if (mParent != null && !mParent.isLayoutRequested()) {
10989 mParent.requestLayout();
10990 }
10991 }
10992
10993 /**
10994 * Forces this view to be laid out during the next layout pass.
10995 * This method does not call requestLayout() or forceLayout()
10996 * on the parent.
10997 */
10998 public void forceLayout() {
10999 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011000 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011001 }
11002
11003 /**
11004 * <p>
11005 * This is called to find out how big a view should be. The parent
11006 * supplies constraint information in the width and height parameters.
11007 * </p>
11008 *
11009 * <p>
11010 * The actual mesurement work of a view is performed in
11011 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11012 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11013 * </p>
11014 *
11015 *
11016 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11017 * parent
11018 * @param heightMeasureSpec Vertical space requirements as imposed by the
11019 * parent
11020 *
11021 * @see #onMeasure(int, int)
11022 */
11023 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11024 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11025 widthMeasureSpec != mOldWidthMeasureSpec ||
11026 heightMeasureSpec != mOldHeightMeasureSpec) {
11027
11028 // first clears the measured dimension flag
11029 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11030
11031 if (ViewDebug.TRACE_HIERARCHY) {
11032 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11033 }
11034
11035 // measure ourselves, this should set the measured dimension flag back
11036 onMeasure(widthMeasureSpec, heightMeasureSpec);
11037
11038 // flag not set, setMeasuredDimension() was not invoked, we raise
11039 // an exception to warn the developer
11040 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11041 throw new IllegalStateException("onMeasure() did not set the"
11042 + " measured dimension by calling"
11043 + " setMeasuredDimension()");
11044 }
11045
11046 mPrivateFlags |= LAYOUT_REQUIRED;
11047 }
11048
11049 mOldWidthMeasureSpec = widthMeasureSpec;
11050 mOldHeightMeasureSpec = heightMeasureSpec;
11051 }
11052
11053 /**
11054 * <p>
11055 * Measure the view and its content to determine the measured width and the
11056 * measured height. This method is invoked by {@link #measure(int, int)} and
11057 * should be overriden by subclasses to provide accurate and efficient
11058 * measurement of their contents.
11059 * </p>
11060 *
11061 * <p>
11062 * <strong>CONTRACT:</strong> When overriding this method, you
11063 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11064 * measured width and height of this view. Failure to do so will trigger an
11065 * <code>IllegalStateException</code>, thrown by
11066 * {@link #measure(int, int)}. Calling the superclass'
11067 * {@link #onMeasure(int, int)} is a valid use.
11068 * </p>
11069 *
11070 * <p>
11071 * The base class implementation of measure defaults to the background size,
11072 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11073 * override {@link #onMeasure(int, int)} to provide better measurements of
11074 * their content.
11075 * </p>
11076 *
11077 * <p>
11078 * If this method is overridden, it is the subclass's responsibility to make
11079 * sure the measured height and width are at least the view's minimum height
11080 * and width ({@link #getSuggestedMinimumHeight()} and
11081 * {@link #getSuggestedMinimumWidth()}).
11082 * </p>
11083 *
11084 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11085 * The requirements are encoded with
11086 * {@link android.view.View.MeasureSpec}.
11087 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11088 * The requirements are encoded with
11089 * {@link android.view.View.MeasureSpec}.
11090 *
11091 * @see #getMeasuredWidth()
11092 * @see #getMeasuredHeight()
11093 * @see #setMeasuredDimension(int, int)
11094 * @see #getSuggestedMinimumHeight()
11095 * @see #getSuggestedMinimumWidth()
11096 * @see android.view.View.MeasureSpec#getMode(int)
11097 * @see android.view.View.MeasureSpec#getSize(int)
11098 */
11099 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11100 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11101 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11102 }
11103
11104 /**
11105 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11106 * measured width and measured height. Failing to do so will trigger an
11107 * exception at measurement time.</p>
11108 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011109 * @param measuredWidth The measured width of this view. May be a complex
11110 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11111 * {@link #MEASURED_STATE_TOO_SMALL}.
11112 * @param measuredHeight The measured height of this view. May be a complex
11113 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11114 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011115 */
11116 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11117 mMeasuredWidth = measuredWidth;
11118 mMeasuredHeight = measuredHeight;
11119
11120 mPrivateFlags |= MEASURED_DIMENSION_SET;
11121 }
11122
11123 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011124 * Merge two states as returned by {@link #getMeasuredState()}.
11125 * @param curState The current state as returned from a view or the result
11126 * of combining multiple views.
11127 * @param newState The new view state to combine.
11128 * @return Returns a new integer reflecting the combination of the two
11129 * states.
11130 */
11131 public static int combineMeasuredStates(int curState, int newState) {
11132 return curState | newState;
11133 }
11134
11135 /**
11136 * Version of {@link #resolveSizeAndState(int, int, int)}
11137 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11138 */
11139 public static int resolveSize(int size, int measureSpec) {
11140 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11141 }
11142
11143 /**
11144 * Utility to reconcile a desired size and state, with constraints imposed
11145 * by a MeasureSpec. Will take the desired size, unless a different size
11146 * is imposed by the constraints. The returned value is a compound integer,
11147 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11148 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11149 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011150 *
11151 * @param size How big the view wants to be
11152 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011153 * @return Size information bit mask as defined by
11154 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011155 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011156 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 int result = size;
11158 int specMode = MeasureSpec.getMode(measureSpec);
11159 int specSize = MeasureSpec.getSize(measureSpec);
11160 switch (specMode) {
11161 case MeasureSpec.UNSPECIFIED:
11162 result = size;
11163 break;
11164 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011165 if (specSize < size) {
11166 result = specSize | MEASURED_STATE_TOO_SMALL;
11167 } else {
11168 result = size;
11169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011170 break;
11171 case MeasureSpec.EXACTLY:
11172 result = specSize;
11173 break;
11174 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011175 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011176 }
11177
11178 /**
11179 * Utility to return a default size. Uses the supplied size if the
11180 * MeasureSpec imposed no contraints. Will get larger if allowed
11181 * by the MeasureSpec.
11182 *
11183 * @param size Default size for this view
11184 * @param measureSpec Constraints imposed by the parent
11185 * @return The size this view should be.
11186 */
11187 public static int getDefaultSize(int size, int measureSpec) {
11188 int result = size;
11189 int specMode = MeasureSpec.getMode(measureSpec);
11190 int specSize = MeasureSpec.getSize(measureSpec);
11191
11192 switch (specMode) {
11193 case MeasureSpec.UNSPECIFIED:
11194 result = size;
11195 break;
11196 case MeasureSpec.AT_MOST:
11197 case MeasureSpec.EXACTLY:
11198 result = specSize;
11199 break;
11200 }
11201 return result;
11202 }
11203
11204 /**
11205 * Returns the suggested minimum height that the view should use. This
11206 * returns the maximum of the view's minimum height
11207 * and the background's minimum height
11208 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11209 * <p>
11210 * When being used in {@link #onMeasure(int, int)}, the caller should still
11211 * ensure the returned height is within the requirements of the parent.
11212 *
11213 * @return The suggested minimum height of the view.
11214 */
11215 protected int getSuggestedMinimumHeight() {
11216 int suggestedMinHeight = mMinHeight;
11217
11218 if (mBGDrawable != null) {
11219 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11220 if (suggestedMinHeight < bgMinHeight) {
11221 suggestedMinHeight = bgMinHeight;
11222 }
11223 }
11224
11225 return suggestedMinHeight;
11226 }
11227
11228 /**
11229 * Returns the suggested minimum width that the view should use. This
11230 * returns the maximum of the view's minimum width)
11231 * and the background's minimum width
11232 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11233 * <p>
11234 * When being used in {@link #onMeasure(int, int)}, the caller should still
11235 * ensure the returned width is within the requirements of the parent.
11236 *
11237 * @return The suggested minimum width of the view.
11238 */
11239 protected int getSuggestedMinimumWidth() {
11240 int suggestedMinWidth = mMinWidth;
11241
11242 if (mBGDrawable != null) {
11243 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11244 if (suggestedMinWidth < bgMinWidth) {
11245 suggestedMinWidth = bgMinWidth;
11246 }
11247 }
11248
11249 return suggestedMinWidth;
11250 }
11251
11252 /**
11253 * Sets the minimum height of the view. It is not guaranteed the view will
11254 * be able to achieve this minimum height (for example, if its parent layout
11255 * constrains it with less available height).
11256 *
11257 * @param minHeight The minimum height the view will try to be.
11258 */
11259 public void setMinimumHeight(int minHeight) {
11260 mMinHeight = minHeight;
11261 }
11262
11263 /**
11264 * Sets the minimum width of the view. It is not guaranteed the view will
11265 * be able to achieve this minimum width (for example, if its parent layout
11266 * constrains it with less available width).
11267 *
11268 * @param minWidth The minimum width the view will try to be.
11269 */
11270 public void setMinimumWidth(int minWidth) {
11271 mMinWidth = minWidth;
11272 }
11273
11274 /**
11275 * Get the animation currently associated with this view.
11276 *
11277 * @return The animation that is currently playing or
11278 * scheduled to play for this view.
11279 */
11280 public Animation getAnimation() {
11281 return mCurrentAnimation;
11282 }
11283
11284 /**
11285 * Start the specified animation now.
11286 *
11287 * @param animation the animation to start now
11288 */
11289 public void startAnimation(Animation animation) {
11290 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11291 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011292 invalidateParentCaches();
11293 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011294 }
11295
11296 /**
11297 * Cancels any animations for this view.
11298 */
11299 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011300 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011301 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011302 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011304 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011305 }
11306
11307 /**
11308 * Sets the next animation to play for this view.
11309 * If you want the animation to play immediately, use
11310 * startAnimation. This method provides allows fine-grained
11311 * control over the start time and invalidation, but you
11312 * must make sure that 1) the animation has a start time set, and
11313 * 2) the view will be invalidated when the animation is supposed to
11314 * start.
11315 *
11316 * @param animation The next animation, or null.
11317 */
11318 public void setAnimation(Animation animation) {
11319 mCurrentAnimation = animation;
11320 if (animation != null) {
11321 animation.reset();
11322 }
11323 }
11324
11325 /**
11326 * Invoked by a parent ViewGroup to notify the start of the animation
11327 * currently associated with this view. If you override this method,
11328 * always call super.onAnimationStart();
11329 *
11330 * @see #setAnimation(android.view.animation.Animation)
11331 * @see #getAnimation()
11332 */
11333 protected void onAnimationStart() {
11334 mPrivateFlags |= ANIMATION_STARTED;
11335 }
11336
11337 /**
11338 * Invoked by a parent ViewGroup to notify the end of the animation
11339 * currently associated with this view. If you override this method,
11340 * always call super.onAnimationEnd();
11341 *
11342 * @see #setAnimation(android.view.animation.Animation)
11343 * @see #getAnimation()
11344 */
11345 protected void onAnimationEnd() {
11346 mPrivateFlags &= ~ANIMATION_STARTED;
11347 }
11348
11349 /**
11350 * Invoked if there is a Transform that involves alpha. Subclass that can
11351 * draw themselves with the specified alpha should return true, and then
11352 * respect that alpha when their onDraw() is called. If this returns false
11353 * then the view may be redirected to draw into an offscreen buffer to
11354 * fulfill the request, which will look fine, but may be slower than if the
11355 * subclass handles it internally. The default implementation returns false.
11356 *
11357 * @param alpha The alpha (0..255) to apply to the view's drawing
11358 * @return true if the view can draw with the specified alpha.
11359 */
11360 protected boolean onSetAlpha(int alpha) {
11361 return false;
11362 }
11363
11364 /**
11365 * This is used by the RootView to perform an optimization when
11366 * the view hierarchy contains one or several SurfaceView.
11367 * SurfaceView is always considered transparent, but its children are not,
11368 * therefore all View objects remove themselves from the global transparent
11369 * region (passed as a parameter to this function).
11370 *
11371 * @param region The transparent region for this ViewRoot (window).
11372 *
11373 * @return Returns true if the effective visibility of the view at this
11374 * point is opaque, regardless of the transparent region; returns false
11375 * if it is possible for underlying windows to be seen behind the view.
11376 *
11377 * {@hide}
11378 */
11379 public boolean gatherTransparentRegion(Region region) {
11380 final AttachInfo attachInfo = mAttachInfo;
11381 if (region != null && attachInfo != null) {
11382 final int pflags = mPrivateFlags;
11383 if ((pflags & SKIP_DRAW) == 0) {
11384 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11385 // remove it from the transparent region.
11386 final int[] location = attachInfo.mTransparentLocation;
11387 getLocationInWindow(location);
11388 region.op(location[0], location[1], location[0] + mRight - mLeft,
11389 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11390 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11391 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11392 // exists, so we remove the background drawable's non-transparent
11393 // parts from this transparent region.
11394 applyDrawableToTransparentRegion(mBGDrawable, region);
11395 }
11396 }
11397 return true;
11398 }
11399
11400 /**
11401 * Play a sound effect for this view.
11402 *
11403 * <p>The framework will play sound effects for some built in actions, such as
11404 * clicking, but you may wish to play these effects in your widget,
11405 * for instance, for internal navigation.
11406 *
11407 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11408 * {@link #isSoundEffectsEnabled()} is true.
11409 *
11410 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11411 */
11412 public void playSoundEffect(int soundConstant) {
11413 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11414 return;
11415 }
11416 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11417 }
11418
11419 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011420 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011421 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011422 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011423 *
11424 * <p>The framework will provide haptic feedback for some built in actions,
11425 * such as long presses, but you may wish to provide feedback for your
11426 * own widget.
11427 *
11428 * <p>The feedback will only be performed if
11429 * {@link #isHapticFeedbackEnabled()} is true.
11430 *
11431 * @param feedbackConstant One of the constants defined in
11432 * {@link HapticFeedbackConstants}
11433 */
11434 public boolean performHapticFeedback(int feedbackConstant) {
11435 return performHapticFeedback(feedbackConstant, 0);
11436 }
11437
11438 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011439 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011440 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011441 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 *
11443 * @param feedbackConstant One of the constants defined in
11444 * {@link HapticFeedbackConstants}
11445 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11446 */
11447 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11448 if (mAttachInfo == null) {
11449 return false;
11450 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011451 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011452 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011453 && !isHapticFeedbackEnabled()) {
11454 return false;
11455 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011456 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11457 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011458 }
11459
11460 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011461 * Request that the visibility of the status bar be changed.
11462 */
11463 public void setSystemUiVisibility(int visibility) {
11464 if (visibility != mSystemUiVisibility) {
11465 mSystemUiVisibility = visibility;
11466 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11467 mParent.recomputeViewAttributes(this);
11468 }
11469 }
11470 }
11471
11472 /**
11473 * Returns the status bar visibility that this view has requested.
11474 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011475 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011476 return mSystemUiVisibility;
11477 }
11478
11479 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11480 mOnSystemUiVisibilityChangeListener = l;
11481 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11482 mParent.recomputeViewAttributes(this);
11483 }
11484 }
11485
11486 /**
11487 */
11488 public void dispatchSystemUiVisibilityChanged(int visibility) {
11489 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011490 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011491 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011492 }
11493 }
11494
11495 /**
Joe Malin32736f02011-01-19 16:14:20 -080011496 * Creates an image that the system displays during the drag and drop
11497 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11498 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11499 * appearance as the given View. The default also positions the center of the drag shadow
11500 * directly under the touch point. If no View is provided (the constructor with no parameters
11501 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11502 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11503 * default is an invisible drag shadow.
11504 * <p>
11505 * You are not required to use the View you provide to the constructor as the basis of the
11506 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11507 * anything you want as the drag shadow.
11508 * </p>
11509 * <p>
11510 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11511 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11512 * size and position of the drag shadow. It uses this data to construct a
11513 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11514 * so that your application can draw the shadow image in the Canvas.
11515 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011516 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011517 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011518 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011519
11520 /**
Joe Malin32736f02011-01-19 16:14:20 -080011521 * Constructs a shadow image builder based on a View. By default, the resulting drag
11522 * shadow will have the same appearance and dimensions as the View, with the touch point
11523 * over the center of the View.
11524 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011525 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011526 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011527 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011528 }
11529
Christopher Tate17ed60c2011-01-18 12:50:26 -080011530 /**
11531 * Construct a shadow builder object with no associated View. This
11532 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11533 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11534 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011535 * reference to any View object. If they are not overridden, then the result is an
11536 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011537 */
11538 public DragShadowBuilder() {
11539 mView = new WeakReference<View>(null);
11540 }
11541
11542 /**
11543 * Returns the View object that had been passed to the
11544 * {@link #View.DragShadowBuilder(View)}
11545 * constructor. If that View parameter was {@code null} or if the
11546 * {@link #View.DragShadowBuilder()}
11547 * constructor was used to instantiate the builder object, this method will return
11548 * null.
11549 *
11550 * @return The View object associate with this builder object.
11551 */
Chris Tate6b391282010-10-14 15:48:59 -070011552 final public View getView() {
11553 return mView.get();
11554 }
11555
Christopher Tate2c095f32010-10-04 14:13:40 -070011556 /**
Joe Malin32736f02011-01-19 16:14:20 -080011557 * Provides the metrics for the shadow image. These include the dimensions of
11558 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011559 * be centered under the touch location while dragging.
11560 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011561 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011562 * same as the dimensions of the View itself and centers the shadow under
11563 * the touch point.
11564 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011565 *
Joe Malin32736f02011-01-19 16:14:20 -080011566 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11567 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11568 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11569 * image.
11570 *
11571 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11572 * shadow image that should be underneath the touch point during the drag and drop
11573 * operation. Your application must set {@link android.graphics.Point#x} to the
11574 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011575 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011576 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011577 final View view = mView.get();
11578 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011579 shadowSize.set(view.getWidth(), view.getHeight());
11580 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011581 } else {
11582 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11583 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011584 }
11585
11586 /**
Joe Malin32736f02011-01-19 16:14:20 -080011587 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11588 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011589 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011590 *
Joe Malin32736f02011-01-19 16:14:20 -080011591 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011592 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011593 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011594 final View view = mView.get();
11595 if (view != null) {
11596 view.draw(canvas);
11597 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011598 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011599 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011600 }
11601 }
11602
11603 /**
Joe Malin32736f02011-01-19 16:14:20 -080011604 * Starts a drag and drop operation. When your application calls this method, it passes a
11605 * {@link android.view.View.DragShadowBuilder} object to the system. The
11606 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11607 * to get metrics for the drag shadow, and then calls the object's
11608 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11609 * <p>
11610 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11611 * drag events to all the View objects in your application that are currently visible. It does
11612 * this either by calling the View object's drag listener (an implementation of
11613 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11614 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11615 * Both are passed a {@link android.view.DragEvent} object that has a
11616 * {@link android.view.DragEvent#getAction()} value of
11617 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11618 * </p>
11619 * <p>
11620 * Your application can invoke startDrag() on any attached View object. The View object does not
11621 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11622 * be related to the View the user selected for dragging.
11623 * </p>
11624 * @param data A {@link android.content.ClipData} object pointing to the data to be
11625 * transferred by the drag and drop operation.
11626 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11627 * drag shadow.
11628 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11629 * drop operation. This Object is put into every DragEvent object sent by the system during the
11630 * current drag.
11631 * <p>
11632 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11633 * to the target Views. For example, it can contain flags that differentiate between a
11634 * a copy operation and a move operation.
11635 * </p>
11636 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11637 * so the parameter should be set to 0.
11638 * @return {@code true} if the method completes successfully, or
11639 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11640 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011641 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011642 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011643 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011644 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011645 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011646 }
11647 boolean okay = false;
11648
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011649 Point shadowSize = new Point();
11650 Point shadowTouchPoint = new Point();
11651 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011652
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011653 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11654 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11655 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011656 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011657
Chris Tatea32dcf72010-10-14 12:13:50 -070011658 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011659 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11660 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011661 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011662 Surface surface = new Surface();
11663 try {
11664 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011665 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011666 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011667 + " surface=" + surface);
11668 if (token != null) {
11669 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011670 try {
Chris Tate6b391282010-10-14 15:48:59 -070011671 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011672 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011673 } finally {
11674 surface.unlockCanvasAndPost(canvas);
11675 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011676
Christopher Tate407b4e92010-11-30 17:14:08 -080011677 final ViewRoot root = getViewRoot();
11678
11679 // Cache the local state object for delivery with DragEvents
11680 root.setLocalDragState(myLocalState);
11681
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011682 // repurpose 'shadowSize' for the last touch point
11683 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011684
Christopher Tatea53146c2010-09-07 11:57:52 -070011685 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011686 shadowSize.x, shadowSize.y,
11687 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011688 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011689 }
11690 } catch (Exception e) {
11691 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11692 surface.destroy();
11693 }
11694
11695 return okay;
11696 }
11697
Christopher Tatea53146c2010-09-07 11:57:52 -070011698 /**
Joe Malin32736f02011-01-19 16:14:20 -080011699 * Handles drag events sent by the system following a call to
11700 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11701 *<p>
11702 * When the system calls this method, it passes a
11703 * {@link android.view.DragEvent} object. A call to
11704 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11705 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11706 * operation.
11707 * @param event The {@link android.view.DragEvent} sent by the system.
11708 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11709 * in DragEvent, indicating the type of drag event represented by this object.
11710 * @return {@code true} if the method was successful, otherwise {@code false}.
11711 * <p>
11712 * The method should return {@code true} in response to an action type of
11713 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11714 * operation.
11715 * </p>
11716 * <p>
11717 * The method should also return {@code true} in response to an action type of
11718 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11719 * {@code false} if it didn't.
11720 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011721 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011722 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011723 return false;
11724 }
11725
11726 /**
Joe Malin32736f02011-01-19 16:14:20 -080011727 * Detects if this View is enabled and has a drag event listener.
11728 * If both are true, then it calls the drag event listener with the
11729 * {@link android.view.DragEvent} it received. If the drag event listener returns
11730 * {@code true}, then dispatchDragEvent() returns {@code true}.
11731 * <p>
11732 * For all other cases, the method calls the
11733 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11734 * method and returns its result.
11735 * </p>
11736 * <p>
11737 * This ensures that a drag event is always consumed, even if the View does not have a drag
11738 * event listener. However, if the View has a listener and the listener returns true, then
11739 * onDragEvent() is not called.
11740 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011741 */
11742 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011743 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011744 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11745 && mOnDragListener.onDrag(this, event)) {
11746 return true;
11747 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011748 return onDragEvent(event);
11749 }
11750
Christopher Tate3d4bf172011-03-28 16:16:46 -070011751 boolean canAcceptDrag() {
11752 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11753 }
11754
Christopher Tatea53146c2010-09-07 11:57:52 -070011755 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011756 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11757 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011758 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011759 */
11760 public void onCloseSystemDialogs(String reason) {
11761 }
Joe Malin32736f02011-01-19 16:14:20 -080011762
Dianne Hackbornffa42482009-09-23 22:20:11 -070011763 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011764 * Given a Drawable whose bounds have been set to draw into this view,
11765 * update a Region being computed for {@link #gatherTransparentRegion} so
11766 * that any non-transparent parts of the Drawable are removed from the
11767 * given transparent region.
11768 *
11769 * @param dr The Drawable whose transparency is to be applied to the region.
11770 * @param region A Region holding the current transparency information,
11771 * where any parts of the region that are set are considered to be
11772 * transparent. On return, this region will be modified to have the
11773 * transparency information reduced by the corresponding parts of the
11774 * Drawable that are not transparent.
11775 * {@hide}
11776 */
11777 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11778 if (DBG) {
11779 Log.i("View", "Getting transparent region for: " + this);
11780 }
11781 final Region r = dr.getTransparentRegion();
11782 final Rect db = dr.getBounds();
11783 final AttachInfo attachInfo = mAttachInfo;
11784 if (r != null && attachInfo != null) {
11785 final int w = getRight()-getLeft();
11786 final int h = getBottom()-getTop();
11787 if (db.left > 0) {
11788 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11789 r.op(0, 0, db.left, h, Region.Op.UNION);
11790 }
11791 if (db.right < w) {
11792 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11793 r.op(db.right, 0, w, h, Region.Op.UNION);
11794 }
11795 if (db.top > 0) {
11796 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11797 r.op(0, 0, w, db.top, Region.Op.UNION);
11798 }
11799 if (db.bottom < h) {
11800 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11801 r.op(0, db.bottom, w, h, Region.Op.UNION);
11802 }
11803 final int[] location = attachInfo.mTransparentLocation;
11804 getLocationInWindow(location);
11805 r.translate(location[0], location[1]);
11806 region.op(r, Region.Op.INTERSECT);
11807 } else {
11808 region.op(db, Region.Op.DIFFERENCE);
11809 }
11810 }
11811
Adam Powelle14579b2009-12-16 18:39:52 -080011812 private void postCheckForLongClick(int delayOffset) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011813 mHasPerformedLongPress = false;
11814
11815 if (mPendingCheckForLongPress == null) {
11816 mPendingCheckForLongPress = new CheckForLongPress();
11817 }
11818 mPendingCheckForLongPress.rememberWindowAttachCount();
Adam Powelle14579b2009-12-16 18:39:52 -080011819 postDelayed(mPendingCheckForLongPress,
11820 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011821 }
11822
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011823 /**
11824 * Inflate a view from an XML resource. This convenience method wraps the {@link
11825 * LayoutInflater} class, which provides a full range of options for view inflation.
11826 *
11827 * @param context The Context object for your activity or application.
11828 * @param resource The resource ID to inflate
11829 * @param root A view group that will be the parent. Used to properly inflate the
11830 * layout_* parameters.
11831 * @see LayoutInflater
11832 */
11833 public static View inflate(Context context, int resource, ViewGroup root) {
11834 LayoutInflater factory = LayoutInflater.from(context);
11835 return factory.inflate(resource, root);
11836 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011838 /**
Adam Powell637d3372010-08-25 14:37:03 -070011839 * Scroll the view with standard behavior for scrolling beyond the normal
11840 * content boundaries. Views that call this method should override
11841 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11842 * results of an over-scroll operation.
11843 *
11844 * Views can use this method to handle any touch or fling-based scrolling.
11845 *
11846 * @param deltaX Change in X in pixels
11847 * @param deltaY Change in Y in pixels
11848 * @param scrollX Current X scroll value in pixels before applying deltaX
11849 * @param scrollY Current Y scroll value in pixels before applying deltaY
11850 * @param scrollRangeX Maximum content scroll range along the X axis
11851 * @param scrollRangeY Maximum content scroll range along the Y axis
11852 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11853 * along the X axis.
11854 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11855 * along the Y axis.
11856 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11857 * @return true if scrolling was clamped to an over-scroll boundary along either
11858 * axis, false otherwise.
11859 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070011860 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070011861 protected boolean overScrollBy(int deltaX, int deltaY,
11862 int scrollX, int scrollY,
11863 int scrollRangeX, int scrollRangeY,
11864 int maxOverScrollX, int maxOverScrollY,
11865 boolean isTouchEvent) {
11866 final int overScrollMode = mOverScrollMode;
11867 final boolean canScrollHorizontal =
11868 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
11869 final boolean canScrollVertical =
11870 computeVerticalScrollRange() > computeVerticalScrollExtent();
11871 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
11872 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
11873 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
11874 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
11875
11876 int newScrollX = scrollX + deltaX;
11877 if (!overScrollHorizontal) {
11878 maxOverScrollX = 0;
11879 }
11880
11881 int newScrollY = scrollY + deltaY;
11882 if (!overScrollVertical) {
11883 maxOverScrollY = 0;
11884 }
11885
11886 // Clamp values if at the limits and record
11887 final int left = -maxOverScrollX;
11888 final int right = maxOverScrollX + scrollRangeX;
11889 final int top = -maxOverScrollY;
11890 final int bottom = maxOverScrollY + scrollRangeY;
11891
11892 boolean clampedX = false;
11893 if (newScrollX > right) {
11894 newScrollX = right;
11895 clampedX = true;
11896 } else if (newScrollX < left) {
11897 newScrollX = left;
11898 clampedX = true;
11899 }
11900
11901 boolean clampedY = false;
11902 if (newScrollY > bottom) {
11903 newScrollY = bottom;
11904 clampedY = true;
11905 } else if (newScrollY < top) {
11906 newScrollY = top;
11907 clampedY = true;
11908 }
11909
11910 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
11911
11912 return clampedX || clampedY;
11913 }
11914
11915 /**
11916 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
11917 * respond to the results of an over-scroll operation.
11918 *
11919 * @param scrollX New X scroll value in pixels
11920 * @param scrollY New Y scroll value in pixels
11921 * @param clampedX True if scrollX was clamped to an over-scroll boundary
11922 * @param clampedY True if scrollY was clamped to an over-scroll boundary
11923 */
11924 protected void onOverScrolled(int scrollX, int scrollY,
11925 boolean clampedX, boolean clampedY) {
11926 // Intentionally empty.
11927 }
11928
11929 /**
11930 * Returns the over-scroll mode for this view. The result will be
11931 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11932 * (allow over-scrolling only if the view content is larger than the container),
11933 * or {@link #OVER_SCROLL_NEVER}.
11934 *
11935 * @return This view's over-scroll mode.
11936 */
11937 public int getOverScrollMode() {
11938 return mOverScrollMode;
11939 }
11940
11941 /**
11942 * Set the over-scroll mode for this view. Valid over-scroll modes are
11943 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
11944 * (allow over-scrolling only if the view content is larger than the container),
11945 * or {@link #OVER_SCROLL_NEVER}.
11946 *
11947 * Setting the over-scroll mode of a view will have an effect only if the
11948 * view is capable of scrolling.
11949 *
11950 * @param overScrollMode The new over-scroll mode for this view.
11951 */
11952 public void setOverScrollMode(int overScrollMode) {
11953 if (overScrollMode != OVER_SCROLL_ALWAYS &&
11954 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
11955 overScrollMode != OVER_SCROLL_NEVER) {
11956 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
11957 }
11958 mOverScrollMode = overScrollMode;
11959 }
11960
11961 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080011962 * Gets a scale factor that determines the distance the view should scroll
11963 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
11964 * @return The vertical scroll scale factor.
11965 * @hide
11966 */
11967 protected float getVerticalScrollFactor() {
11968 if (mVerticalScrollFactor == 0) {
11969 TypedValue outValue = new TypedValue();
11970 if (!mContext.getTheme().resolveAttribute(
11971 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
11972 throw new IllegalStateException(
11973 "Expected theme to define listPreferredItemHeight.");
11974 }
11975 mVerticalScrollFactor = outValue.getDimension(
11976 mContext.getResources().getDisplayMetrics());
11977 }
11978 return mVerticalScrollFactor;
11979 }
11980
11981 /**
11982 * Gets a scale factor that determines the distance the view should scroll
11983 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
11984 * @return The horizontal scroll scale factor.
11985 * @hide
11986 */
11987 protected float getHorizontalScrollFactor() {
11988 // TODO: Should use something else.
11989 return getVerticalScrollFactor();
11990 }
11991
11992 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011993 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
11994 * Each MeasureSpec represents a requirement for either the width or the height.
11995 * A MeasureSpec is comprised of a size and a mode. There are three possible
11996 * modes:
11997 * <dl>
11998 * <dt>UNSPECIFIED</dt>
11999 * <dd>
12000 * The parent has not imposed any constraint on the child. It can be whatever size
12001 * it wants.
12002 * </dd>
12003 *
12004 * <dt>EXACTLY</dt>
12005 * <dd>
12006 * The parent has determined an exact size for the child. The child is going to be
12007 * given those bounds regardless of how big it wants to be.
12008 * </dd>
12009 *
12010 * <dt>AT_MOST</dt>
12011 * <dd>
12012 * The child can be as large as it wants up to the specified size.
12013 * </dd>
12014 * </dl>
12015 *
12016 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12017 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12018 */
12019 public static class MeasureSpec {
12020 private static final int MODE_SHIFT = 30;
12021 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12022
12023 /**
12024 * Measure specification mode: The parent has not imposed any constraint
12025 * on the child. It can be whatever size it wants.
12026 */
12027 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12028
12029 /**
12030 * Measure specification mode: The parent has determined an exact size
12031 * for the child. The child is going to be given those bounds regardless
12032 * of how big it wants to be.
12033 */
12034 public static final int EXACTLY = 1 << MODE_SHIFT;
12035
12036 /**
12037 * Measure specification mode: The child can be as large as it wants up
12038 * to the specified size.
12039 */
12040 public static final int AT_MOST = 2 << MODE_SHIFT;
12041
12042 /**
12043 * Creates a measure specification based on the supplied size and mode.
12044 *
12045 * The mode must always be one of the following:
12046 * <ul>
12047 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12048 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12049 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12050 * </ul>
12051 *
12052 * @param size the size of the measure specification
12053 * @param mode the mode of the measure specification
12054 * @return the measure specification based on size and mode
12055 */
12056 public static int makeMeasureSpec(int size, int mode) {
12057 return size + mode;
12058 }
12059
12060 /**
12061 * Extracts the mode from the supplied measure specification.
12062 *
12063 * @param measureSpec the measure specification to extract the mode from
12064 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12065 * {@link android.view.View.MeasureSpec#AT_MOST} or
12066 * {@link android.view.View.MeasureSpec#EXACTLY}
12067 */
12068 public static int getMode(int measureSpec) {
12069 return (measureSpec & MODE_MASK);
12070 }
12071
12072 /**
12073 * Extracts the size from the supplied measure specification.
12074 *
12075 * @param measureSpec the measure specification to extract the size from
12076 * @return the size in pixels defined in the supplied measure specification
12077 */
12078 public static int getSize(int measureSpec) {
12079 return (measureSpec & ~MODE_MASK);
12080 }
12081
12082 /**
12083 * Returns a String representation of the specified measure
12084 * specification.
12085 *
12086 * @param measureSpec the measure specification to convert to a String
12087 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12088 */
12089 public static String toString(int measureSpec) {
12090 int mode = getMode(measureSpec);
12091 int size = getSize(measureSpec);
12092
12093 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12094
12095 if (mode == UNSPECIFIED)
12096 sb.append("UNSPECIFIED ");
12097 else if (mode == EXACTLY)
12098 sb.append("EXACTLY ");
12099 else if (mode == AT_MOST)
12100 sb.append("AT_MOST ");
12101 else
12102 sb.append(mode).append(" ");
12103
12104 sb.append(size);
12105 return sb.toString();
12106 }
12107 }
12108
12109 class CheckForLongPress implements Runnable {
12110
12111 private int mOriginalWindowAttachCount;
12112
12113 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012114 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012115 && mOriginalWindowAttachCount == mWindowAttachCount) {
12116 if (performLongClick()) {
12117 mHasPerformedLongPress = true;
12118 }
12119 }
12120 }
12121
12122 public void rememberWindowAttachCount() {
12123 mOriginalWindowAttachCount = mWindowAttachCount;
12124 }
12125 }
Joe Malin32736f02011-01-19 16:14:20 -080012126
Adam Powelle14579b2009-12-16 18:39:52 -080012127 private final class CheckForTap implements Runnable {
12128 public void run() {
12129 mPrivateFlags &= ~PREPRESSED;
12130 mPrivateFlags |= PRESSED;
12131 refreshDrawableState();
12132 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12133 postCheckForLongClick(ViewConfiguration.getTapTimeout());
12134 }
12135 }
12136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012137
Adam Powella35d7682010-03-12 14:48:13 -080012138 private final class PerformClick implements Runnable {
12139 public void run() {
12140 performClick();
12141 }
12142 }
12143
Dianne Hackborn63042d62011-01-26 18:56:29 -080012144 /** @hide */
12145 public void hackTurnOffWindowResizeAnim(boolean off) {
12146 mAttachInfo.mTurnOffWindowResizeAnim = off;
12147 }
Joe Malin32736f02011-01-19 16:14:20 -080012148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012149 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012150 * This method returns a ViewPropertyAnimator object, which can be used to animate
12151 * specific properties on this View.
12152 *
12153 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12154 */
12155 public ViewPropertyAnimator animate() {
12156 if (mAnimator == null) {
12157 mAnimator = new ViewPropertyAnimator(this);
12158 }
12159 return mAnimator;
12160 }
12161
12162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012163 * Interface definition for a callback to be invoked when a key event is
12164 * dispatched to this view. The callback will be invoked before the key
12165 * event is given to the view.
12166 */
12167 public interface OnKeyListener {
12168 /**
12169 * Called when a key is dispatched to a view. This allows listeners to
12170 * get a chance to respond before the target view.
12171 *
12172 * @param v The view the key has been dispatched to.
12173 * @param keyCode The code for the physical key that was pressed
12174 * @param event The KeyEvent object containing full information about
12175 * the event.
12176 * @return True if the listener has consumed the event, false otherwise.
12177 */
12178 boolean onKey(View v, int keyCode, KeyEvent event);
12179 }
12180
12181 /**
12182 * Interface definition for a callback to be invoked when a touch event is
12183 * dispatched to this view. The callback will be invoked before the touch
12184 * event is given to the view.
12185 */
12186 public interface OnTouchListener {
12187 /**
12188 * Called when a touch event is dispatched to a view. This allows listeners to
12189 * get a chance to respond before the target view.
12190 *
12191 * @param v The view the touch event has been dispatched to.
12192 * @param event The MotionEvent object containing full information about
12193 * the event.
12194 * @return True if the listener has consumed the event, false otherwise.
12195 */
12196 boolean onTouch(View v, MotionEvent event);
12197 }
12198
12199 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012200 * Interface definition for a callback to be invoked when a generic motion event is
12201 * dispatched to this view. The callback will be invoked before the generic motion
12202 * event is given to the view.
12203 */
12204 public interface OnGenericMotionListener {
12205 /**
12206 * Called when a generic motion event is dispatched to a view. This allows listeners to
12207 * get a chance to respond before the target view.
12208 *
12209 * @param v The view the generic motion event has been dispatched to.
12210 * @param event The MotionEvent object containing full information about
12211 * the event.
12212 * @return True if the listener has consumed the event, false otherwise.
12213 */
12214 boolean onGenericMotion(View v, MotionEvent event);
12215 }
12216
12217 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012218 * Interface definition for a callback to be invoked when a view has been clicked and held.
12219 */
12220 public interface OnLongClickListener {
12221 /**
12222 * Called when a view has been clicked and held.
12223 *
12224 * @param v The view that was clicked and held.
12225 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012226 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012227 */
12228 boolean onLongClick(View v);
12229 }
12230
12231 /**
Chris Tate32affef2010-10-18 15:29:21 -070012232 * Interface definition for a callback to be invoked when a drag is being dispatched
12233 * to this view. The callback will be invoked before the hosting view's own
12234 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12235 * onDrag(event) behavior, it should return 'false' from this callback.
12236 */
12237 public interface OnDragListener {
12238 /**
12239 * Called when a drag event is dispatched to a view. This allows listeners
12240 * to get a chance to override base View behavior.
12241 *
Joe Malin32736f02011-01-19 16:14:20 -080012242 * @param v The View that received the drag event.
12243 * @param event The {@link android.view.DragEvent} object for the drag event.
12244 * @return {@code true} if the drag event was handled successfully, or {@code false}
12245 * if the drag event was not handled. Note that {@code false} will trigger the View
12246 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012247 */
12248 boolean onDrag(View v, DragEvent event);
12249 }
12250
12251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012252 * Interface definition for a callback to be invoked when the focus state of
12253 * a view changed.
12254 */
12255 public interface OnFocusChangeListener {
12256 /**
12257 * Called when the focus state of a view has changed.
12258 *
12259 * @param v The view whose state has changed.
12260 * @param hasFocus The new focus state of v.
12261 */
12262 void onFocusChange(View v, boolean hasFocus);
12263 }
12264
12265 /**
12266 * Interface definition for a callback to be invoked when a view is clicked.
12267 */
12268 public interface OnClickListener {
12269 /**
12270 * Called when a view has been clicked.
12271 *
12272 * @param v The view that was clicked.
12273 */
12274 void onClick(View v);
12275 }
12276
12277 /**
12278 * Interface definition for a callback to be invoked when the context menu
12279 * for this view is being built.
12280 */
12281 public interface OnCreateContextMenuListener {
12282 /**
12283 * Called when the context menu for this view is being built. It is not
12284 * safe to hold onto the menu after this method returns.
12285 *
12286 * @param menu The context menu that is being built
12287 * @param v The view for which the context menu is being built
12288 * @param menuInfo Extra information about the item for which the
12289 * context menu should be shown. This information will vary
12290 * depending on the class of v.
12291 */
12292 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12293 }
12294
Joe Onorato664644d2011-01-23 17:53:23 -080012295 /**
12296 * Interface definition for a callback to be invoked when the status bar changes
12297 * visibility.
12298 *
12299 * @see #setOnSystemUiVisibilityChangeListener
12300 */
12301 public interface OnSystemUiVisibilityChangeListener {
12302 /**
12303 * Called when the status bar changes visibility because of a call to
12304 * {@link #setSystemUiVisibility}.
12305 *
12306 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12307 */
12308 public void onSystemUiVisibilityChange(int visibility);
12309 }
12310
Adam Powell4afd62b2011-02-18 15:02:18 -080012311 /**
12312 * Interface definition for a callback to be invoked when this view is attached
12313 * or detached from its window.
12314 */
12315 public interface OnAttachStateChangeListener {
12316 /**
12317 * Called when the view is attached to a window.
12318 * @param v The view that was attached
12319 */
12320 public void onViewAttachedToWindow(View v);
12321 /**
12322 * Called when the view is detached from a window.
12323 * @param v The view that was detached
12324 */
12325 public void onViewDetachedFromWindow(View v);
12326 }
12327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012328 private final class UnsetPressedState implements Runnable {
12329 public void run() {
12330 setPressed(false);
12331 }
12332 }
12333
12334 /**
12335 * Base class for derived classes that want to save and restore their own
12336 * state in {@link android.view.View#onSaveInstanceState()}.
12337 */
12338 public static class BaseSavedState extends AbsSavedState {
12339 /**
12340 * Constructor used when reading from a parcel. Reads the state of the superclass.
12341 *
12342 * @param source
12343 */
12344 public BaseSavedState(Parcel source) {
12345 super(source);
12346 }
12347
12348 /**
12349 * Constructor called by derived classes when creating their SavedState objects
12350 *
12351 * @param superState The state of the superclass of this view
12352 */
12353 public BaseSavedState(Parcelable superState) {
12354 super(superState);
12355 }
12356
12357 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12358 new Parcelable.Creator<BaseSavedState>() {
12359 public BaseSavedState createFromParcel(Parcel in) {
12360 return new BaseSavedState(in);
12361 }
12362
12363 public BaseSavedState[] newArray(int size) {
12364 return new BaseSavedState[size];
12365 }
12366 };
12367 }
12368
12369 /**
12370 * A set of information given to a view when it is attached to its parent
12371 * window.
12372 */
12373 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012374 interface Callbacks {
12375 void playSoundEffect(int effectId);
12376 boolean performHapticFeedback(int effectId, boolean always);
12377 }
12378
12379 /**
12380 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12381 * to a Handler. This class contains the target (View) to invalidate and
12382 * the coordinates of the dirty rectangle.
12383 *
12384 * For performance purposes, this class also implements a pool of up to
12385 * POOL_LIMIT objects that get reused. This reduces memory allocations
12386 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012387 */
Romain Guyd928d682009-03-31 17:52:16 -070012388 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012389 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012390 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12391 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012392 public InvalidateInfo newInstance() {
12393 return new InvalidateInfo();
12394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395
Romain Guyd928d682009-03-31 17:52:16 -070012396 public void onAcquired(InvalidateInfo element) {
12397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012398
Romain Guyd928d682009-03-31 17:52:16 -070012399 public void onReleased(InvalidateInfo element) {
12400 }
12401 }, POOL_LIMIT)
12402 );
12403
12404 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405
12406 View target;
12407
12408 int left;
12409 int top;
12410 int right;
12411 int bottom;
12412
Romain Guyd928d682009-03-31 17:52:16 -070012413 public void setNextPoolable(InvalidateInfo element) {
12414 mNext = element;
12415 }
12416
12417 public InvalidateInfo getNextPoolable() {
12418 return mNext;
12419 }
12420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012421 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012422 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012423 }
12424
12425 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012426 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012427 }
12428 }
12429
12430 final IWindowSession mSession;
12431
12432 final IWindow mWindow;
12433
12434 final IBinder mWindowToken;
12435
12436 final Callbacks mRootCallbacks;
12437
Chet Haasedaf98e92011-01-10 14:10:36 -080012438 Canvas mHardwareCanvas;
12439
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012440 /**
12441 * The top view of the hierarchy.
12442 */
12443 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012444
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 IBinder mPanelParentWindowToken;
12446 Surface mSurface;
12447
Romain Guyb051e892010-09-28 19:09:36 -070012448 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012449 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012450 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012451
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012452 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012453 * Scale factor used by the compatibility mode
12454 */
12455 float mApplicationScale;
12456
12457 /**
12458 * Indicates whether the application is in compatibility mode
12459 */
12460 boolean mScalingRequired;
12461
12462 /**
Dianne Hackborn63042d62011-01-26 18:56:29 -080012463 * If set, ViewRoot doesn't use its lame animation for when the window resizes.
12464 */
12465 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012466
Dianne Hackborn63042d62011-01-26 18:56:29 -080012467 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012468 * Left position of this view's window
12469 */
12470 int mWindowLeft;
12471
12472 /**
12473 * Top position of this view's window
12474 */
12475 int mWindowTop;
12476
12477 /**
Adam Powell26153a32010-11-08 15:22:27 -080012478 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012479 */
Adam Powell26153a32010-11-08 15:22:27 -080012480 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012481
12482 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012483 * For windows that are full-screen but using insets to layout inside
12484 * of the screen decorations, these are the current insets for the
12485 * content of the window.
12486 */
12487 final Rect mContentInsets = new Rect();
12488
12489 /**
12490 * For windows that are full-screen but using insets to layout inside
12491 * of the screen decorations, these are the current insets for the
12492 * actual visible parts of the window.
12493 */
12494 final Rect mVisibleInsets = new Rect();
12495
12496 /**
12497 * The internal insets given by this window. This value is
12498 * supplied by the client (through
12499 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12500 * be given to the window manager when changed to be used in laying
12501 * out windows behind it.
12502 */
12503 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12504 = new ViewTreeObserver.InternalInsetsInfo();
12505
12506 /**
12507 * All views in the window's hierarchy that serve as scroll containers,
12508 * used to determine if the window can be resized or must be panned
12509 * to adjust for a soft input area.
12510 */
12511 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12512
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012513 final KeyEvent.DispatcherState mKeyDispatchState
12514 = new KeyEvent.DispatcherState();
12515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012516 /**
12517 * Indicates whether the view's window currently has the focus.
12518 */
12519 boolean mHasWindowFocus;
12520
12521 /**
12522 * The current visibility of the window.
12523 */
12524 int mWindowVisibility;
12525
12526 /**
12527 * Indicates the time at which drawing started to occur.
12528 */
12529 long mDrawingTime;
12530
12531 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012532 * Indicates whether or not ignoring the DIRTY_MASK flags.
12533 */
12534 boolean mIgnoreDirtyState;
12535
12536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012537 * Indicates whether the view's window is currently in touch mode.
12538 */
12539 boolean mInTouchMode;
12540
12541 /**
12542 * Indicates that ViewRoot should trigger a global layout change
12543 * the next time it performs a traversal
12544 */
12545 boolean mRecomputeGlobalAttributes;
12546
12547 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012548 * Set during a traveral if any views want to keep the screen on.
12549 */
12550 boolean mKeepScreenOn;
12551
12552 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012553 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12554 */
12555 int mSystemUiVisibility;
12556
12557 /**
12558 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12559 * attached.
12560 */
12561 boolean mHasSystemUiListeners;
12562
12563 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012564 * Set if the visibility of any views has changed.
12565 */
12566 boolean mViewVisibilityChanged;
12567
12568 /**
12569 * Set to true if a view has been scrolled.
12570 */
12571 boolean mViewScrollChanged;
12572
12573 /**
12574 * Global to the view hierarchy used as a temporary for dealing with
12575 * x/y points in the transparent region computations.
12576 */
12577 final int[] mTransparentLocation = new int[2];
12578
12579 /**
12580 * Global to the view hierarchy used as a temporary for dealing with
12581 * x/y points in the ViewGroup.invalidateChild implementation.
12582 */
12583 final int[] mInvalidateChildLocation = new int[2];
12584
Chet Haasec3aa3612010-06-17 08:50:37 -070012585
12586 /**
12587 * Global to the view hierarchy used as a temporary for dealing with
12588 * x/y location when view is transformed.
12589 */
12590 final float[] mTmpTransformLocation = new float[2];
12591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012592 /**
12593 * The view tree observer used to dispatch global events like
12594 * layout, pre-draw, touch mode change, etc.
12595 */
12596 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12597
12598 /**
12599 * A Canvas used by the view hierarchy to perform bitmap caching.
12600 */
12601 Canvas mCanvas;
12602
12603 /**
12604 * A Handler supplied by a view's {@link android.view.ViewRoot}. This
12605 * handler can be used to pump events in the UI events queue.
12606 */
12607 final Handler mHandler;
12608
12609 /**
12610 * Identifier for messages requesting the view to be invalidated.
12611 * Such messages should be sent to {@link #mHandler}.
12612 */
12613 static final int INVALIDATE_MSG = 0x1;
12614
12615 /**
12616 * Identifier for messages requesting the view to invalidate a region.
12617 * Such messages should be sent to {@link #mHandler}.
12618 */
12619 static final int INVALIDATE_RECT_MSG = 0x2;
12620
12621 /**
12622 * Temporary for use in computing invalidate rectangles while
12623 * calling up the hierarchy.
12624 */
12625 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012626
12627 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012628 * Temporary for use in computing hit areas with transformed views
12629 */
12630 final RectF mTmpTransformRect = new RectF();
12631
12632 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012633 * Temporary list for use in collecting focusable descendents of a view.
12634 */
12635 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12636
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012637 /**
12638 * Creates a new set of attachment information with the specified
12639 * events handler and thread.
12640 *
12641 * @param handler the events handler the view must use
12642 */
12643 AttachInfo(IWindowSession session, IWindow window,
12644 Handler handler, Callbacks effectPlayer) {
12645 mSession = session;
12646 mWindow = window;
12647 mWindowToken = window.asBinder();
12648 mHandler = handler;
12649 mRootCallbacks = effectPlayer;
12650 }
12651 }
12652
12653 /**
12654 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12655 * is supported. This avoids keeping too many unused fields in most
12656 * instances of View.</p>
12657 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012658 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012659
Mike Cleronf116bf82009-09-27 19:14:12 -070012660 /**
12661 * Scrollbars are not visible
12662 */
12663 public static final int OFF = 0;
12664
12665 /**
12666 * Scrollbars are visible
12667 */
12668 public static final int ON = 1;
12669
12670 /**
12671 * Scrollbars are fading away
12672 */
12673 public static final int FADING = 2;
12674
12675 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012676
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012677 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012678 public int scrollBarDefaultDelayBeforeFade;
12679 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680
12681 public int scrollBarSize;
12682 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012683 public float[] interpolatorValues;
12684 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012685
12686 public final Paint paint;
12687 public final Matrix matrix;
12688 public Shader shader;
12689
Mike Cleronf116bf82009-09-27 19:14:12 -070012690 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12691
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012692 private static final float[] OPAQUE = { 255 };
12693 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012694
Mike Cleronf116bf82009-09-27 19:14:12 -070012695 /**
12696 * When fading should start. This time moves into the future every time
12697 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12698 */
12699 public long fadeStartTime;
12700
12701
12702 /**
12703 * The current state of the scrollbars: ON, OFF, or FADING
12704 */
12705 public int state = OFF;
12706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012707 private int mLastColor;
12708
Mike Cleronf116bf82009-09-27 19:14:12 -070012709 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012710 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12711 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012712 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12713 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012714
12715 paint = new Paint();
12716 matrix = new Matrix();
12717 // use use a height of 1, and then wack the matrix each time we
12718 // actually use it.
12719 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012721 paint.setShader(shader);
12722 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012723 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012724 }
Romain Guy8506ab42009-06-11 17:35:47 -070012725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012726 public void setFadeColor(int color) {
12727 if (color != 0 && color != mLastColor) {
12728 mLastColor = color;
12729 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012730
Romain Guye55e1a72009-08-27 10:42:26 -070012731 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12732 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012734 paint.setShader(shader);
12735 // Restore the default transfer mode (src_over)
12736 paint.setXfermode(null);
12737 }
12738 }
Joe Malin32736f02011-01-19 16:14:20 -080012739
Mike Cleronf116bf82009-09-27 19:14:12 -070012740 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012741 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012742 if (now >= fadeStartTime) {
12743
12744 // the animation fades the scrollbars out by changing
12745 // the opacity (alpha) from fully opaque to fully
12746 // transparent
12747 int nextFrame = (int) now;
12748 int framesCount = 0;
12749
12750 Interpolator interpolator = scrollBarInterpolator;
12751
12752 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012753 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012754
12755 // End transparent
12756 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012757 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012758
12759 state = FADING;
12760
12761 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012762 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012763 }
12764 }
12765
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012766 }
12767}