blob: f8d48daf0b073f640ae2ee22fdaa07405faf017d [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 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800954 * Horizontal direction of this view is from Left to Right.
955 * Use with {@link #setHorizontalDirection}.
956 * {@hide}
957 */
958 public static final int HORIZONTAL_DIRECTION_LTR = 0x00000000;
959
960 /**
961 * Horizontal direction of this view is from Right to Left.
962 * Use with {@link #setHorizontalDirection}.
963 * {@hide}
964 */
965 public static final int HORIZONTAL_DIRECTION_RTL = 0x40000000;
966
967 /**
968 * Horizontal direction of this view is inherited from its parent.
969 * Use with {@link #setHorizontalDirection}.
970 * {@hide}
971 */
972 public static final int HORIZONTAL_DIRECTION_INHERIT = 0x80000000;
973
974 /**
975 * Horizontal direction of this view is from deduced from the default language
976 * script for the locale. Use with {@link #setHorizontalDirection}.
977 * {@hide}
978 */
979 public static final int HORIZONTAL_DIRECTION_LOCALE = 0xC0000000;
980
981 /**
982 * Mask for use with setFlags indicating bits used for horizontalDirection.
983 * {@hide}
984 */
985 static final int HORIZONTAL_DIRECTION_MASK = 0xC0000000;
986
987 private static final int[] HORIZONTAL_DIRECTION_FLAGS = { HORIZONTAL_DIRECTION_LTR,
988 HORIZONTAL_DIRECTION_RTL, HORIZONTAL_DIRECTION_INHERIT, HORIZONTAL_DIRECTION_LOCALE};
989
990 /**
svetoslavganov75986cf2009-05-14 22:28:01 -0700991 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
992 * should add all focusable Views regardless if they are focusable in touch mode.
993 */
994 public static final int FOCUSABLES_ALL = 0x00000000;
995
996 /**
997 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
998 * should add only Views focusable in touch mode.
999 */
1000 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1001
1002 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 * Use with {@link #focusSearch}. Move focus to the previous selectable
1004 * item.
1005 */
1006 public static final int FOCUS_BACKWARD = 0x00000001;
1007
1008 /**
1009 * Use with {@link #focusSearch}. Move focus to the next selectable
1010 * item.
1011 */
1012 public static final int FOCUS_FORWARD = 0x00000002;
1013
1014 /**
1015 * Use with {@link #focusSearch}. Move focus to the left.
1016 */
1017 public static final int FOCUS_LEFT = 0x00000011;
1018
1019 /**
1020 * Use with {@link #focusSearch}. Move focus up.
1021 */
1022 public static final int FOCUS_UP = 0x00000021;
1023
1024 /**
1025 * Use with {@link #focusSearch}. Move focus to the right.
1026 */
1027 public static final int FOCUS_RIGHT = 0x00000042;
1028
1029 /**
1030 * Use with {@link #focusSearch}. Move focus down.
1031 */
1032 public static final int FOCUS_DOWN = 0x00000082;
1033
1034 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001035 * Bits of {@link #getMeasuredWidthAndState()} and
1036 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1037 */
1038 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1039
1040 /**
1041 * Bits of {@link #getMeasuredWidthAndState()} and
1042 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1043 */
1044 public static final int MEASURED_STATE_MASK = 0xff000000;
1045
1046 /**
1047 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1048 * for functions that combine both width and height into a single int,
1049 * such as {@link #getMeasuredState()} and the childState argument of
1050 * {@link #resolveSizeAndState(int, int, int)}.
1051 */
1052 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1053
1054 /**
1055 * Bit of {@link #getMeasuredWidthAndState()} and
1056 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1057 * is smaller that the space the view would like to have.
1058 */
1059 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1060
1061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062 * Base View state sets
1063 */
1064 // Singles
1065 /**
1066 * Indicates the view has no states set. States are used with
1067 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1068 * view depending on its state.
1069 *
1070 * @see android.graphics.drawable.Drawable
1071 * @see #getDrawableState()
1072 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001073 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 /**
1075 * Indicates the view is enabled. 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[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 /**
1084 * Indicates the view is focused. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is selected. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is pressed. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 * @hide
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view's window has focus. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 // Doubles
1121 /**
1122 * Indicates the view is enabled and has the focus.
1123 *
1124 * @see #ENABLED_STATE_SET
1125 * @see #FOCUSED_STATE_SET
1126 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001127 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 /**
1129 * Indicates the view is enabled and selected.
1130 *
1131 * @see #ENABLED_STATE_SET
1132 * @see #SELECTED_STATE_SET
1133 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001134 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 /**
1136 * Indicates the view is enabled and that its window has focus.
1137 *
1138 * @see #ENABLED_STATE_SET
1139 * @see #WINDOW_FOCUSED_STATE_SET
1140 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001141 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 /**
1143 * Indicates the view is focused and selected.
1144 *
1145 * @see #FOCUSED_STATE_SET
1146 * @see #SELECTED_STATE_SET
1147 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001148 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001149 /**
1150 * Indicates the view has the focus and that its window has the focus.
1151 *
1152 * @see #FOCUSED_STATE_SET
1153 * @see #WINDOW_FOCUSED_STATE_SET
1154 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001155 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001156 /**
1157 * Indicates the view is selected and that its window has the focus.
1158 *
1159 * @see #SELECTED_STATE_SET
1160 * @see #WINDOW_FOCUSED_STATE_SET
1161 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001162 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001163 // Triples
1164 /**
1165 * Indicates the view is enabled, focused and selected.
1166 *
1167 * @see #ENABLED_STATE_SET
1168 * @see #FOCUSED_STATE_SET
1169 * @see #SELECTED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Indicates the view is enabled, focused and its window has the focus.
1174 *
1175 * @see #ENABLED_STATE_SET
1176 * @see #FOCUSED_STATE_SET
1177 * @see #WINDOW_FOCUSED_STATE_SET
1178 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001179 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
1181 * Indicates the view is enabled, selected and its window has the focus.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #SELECTED_STATE_SET
1185 * @see #WINDOW_FOCUSED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is focused, selected and its window has the focus.
1190 *
1191 * @see #FOCUSED_STATE_SET
1192 * @see #SELECTED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is enabled, focused, selected and its window
1198 * has the focus.
1199 *
1200 * @see #ENABLED_STATE_SET
1201 * @see #FOCUSED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is pressed and its window has the focus.
1208 *
1209 * @see #PRESSED_STATE_SET
1210 * @see #WINDOW_FOCUSED_STATE_SET
1211 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001212 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 /**
1214 * Indicates the view is pressed and selected.
1215 *
1216 * @see #PRESSED_STATE_SET
1217 * @see #SELECTED_STATE_SET
1218 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001219 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001220 /**
1221 * Indicates the view is pressed, selected and its window has the focus.
1222 *
1223 * @see #PRESSED_STATE_SET
1224 * @see #SELECTED_STATE_SET
1225 * @see #WINDOW_FOCUSED_STATE_SET
1226 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001227 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001228 /**
1229 * Indicates the view is pressed and focused.
1230 *
1231 * @see #PRESSED_STATE_SET
1232 * @see #FOCUSED_STATE_SET
1233 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001234 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 /**
1236 * Indicates the view is pressed, focused and its window has the focus.
1237 *
1238 * @see #PRESSED_STATE_SET
1239 * @see #FOCUSED_STATE_SET
1240 * @see #WINDOW_FOCUSED_STATE_SET
1241 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001242 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 /**
1244 * Indicates the view is pressed, focused and selected.
1245 *
1246 * @see #PRESSED_STATE_SET
1247 * @see #SELECTED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused, selected and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 * @see #SELECTED_STATE_SET
1257 * @see #WINDOW_FOCUSED_STATE_SET
1258 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001259 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 /**
1261 * Indicates the view is pressed and enabled.
1262 *
1263 * @see #PRESSED_STATE_SET
1264 * @see #ENABLED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, enabled and its window has the focus.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #ENABLED_STATE_SET
1272 * @see #WINDOW_FOCUSED_STATE_SET
1273 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001274 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 /**
1276 * Indicates the view is pressed, enabled and selected.
1277 *
1278 * @see #PRESSED_STATE_SET
1279 * @see #ENABLED_STATE_SET
1280 * @see #SELECTED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled, selected and its window has the
1285 * focus.
1286 *
1287 * @see #PRESSED_STATE_SET
1288 * @see #ENABLED_STATE_SET
1289 * @see #SELECTED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled and focused.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #FOCUSED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled, focused and its window has the
1303 * focus.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #FOCUSED_STATE_SET
1308 * @see #WINDOW_FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled, focused and selected.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #SELECTED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 /**
1321 * Indicates the view is pressed, enabled, focused, selected and its window
1322 * has the focus.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #SELECTED_STATE_SET
1327 * @see #FOCUSED_STATE_SET
1328 * @see #WINDOW_FOCUSED_STATE_SET
1329 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001330 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001331
1332 /**
1333 * The order here is very important to {@link #getDrawableState()}
1334 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 private static final int[][] VIEW_STATE_SETS;
1336
Romain Guyb051e892010-09-28 19:09:36 -07001337 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1338 static final int VIEW_STATE_SELECTED = 1 << 1;
1339 static final int VIEW_STATE_FOCUSED = 1 << 2;
1340 static final int VIEW_STATE_ENABLED = 1 << 3;
1341 static final int VIEW_STATE_PRESSED = 1 << 4;
1342 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001343 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001344 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001345 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1346 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001347
1348 static final int[] VIEW_STATE_IDS = new int[] {
1349 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1350 R.attr.state_selected, VIEW_STATE_SELECTED,
1351 R.attr.state_focused, VIEW_STATE_FOCUSED,
1352 R.attr.state_enabled, VIEW_STATE_ENABLED,
1353 R.attr.state_pressed, VIEW_STATE_PRESSED,
1354 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001355 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001356 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001357 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1358 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001359 };
1360
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001361 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001362 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1363 throw new IllegalStateException(
1364 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1365 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001367 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001368 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001369 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001370 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001371 orderedIds[i * 2] = viewState;
1372 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001373 }
1374 }
1375 }
Romain Guyb051e892010-09-28 19:09:36 -07001376 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1377 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1378 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 int numBits = Integer.bitCount(i);
1380 int[] set = new int[numBits];
1381 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001382 for (int j = 0; j < orderedIds.length; j += 2) {
1383 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 set[pos++] = orderedIds[j];
1385 }
1386 }
1387 VIEW_STATE_SETS[i] = set;
1388 }
1389
1390 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1391 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1392 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1393 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1394 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1395 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1396 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1397 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1398 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1399 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1400 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1401 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1402 | VIEW_STATE_FOCUSED];
1403 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1404 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1405 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1406 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1407 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1408 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1409 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1410 | VIEW_STATE_ENABLED];
1411 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1413 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1414 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1415 | VIEW_STATE_ENABLED];
1416 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1418 | VIEW_STATE_ENABLED];
1419 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1422
1423 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1424 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1426 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1428 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1429 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1430 | VIEW_STATE_PRESSED];
1431 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1433 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1434 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1435 | VIEW_STATE_PRESSED];
1436 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1437 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1438 | VIEW_STATE_PRESSED];
1439 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1440 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1441 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1444 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1452 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1453 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1454 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1455 | VIEW_STATE_PRESSED];
1456 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1457 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1458 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1459 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1460 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1461 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1464 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 }
1467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001469 * Temporary Rect currently for use in setBackground(). This will probably
1470 * be extended in the future to hold our own class with more than just
1471 * a Rect. :)
1472 */
1473 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001474
1475 /**
1476 * Map used to store views' tags.
1477 */
1478 private static WeakHashMap<View, SparseArray<Object>> sTags;
1479
1480 /**
1481 * Lock used to access sTags.
1482 */
1483 private static final Object sTagsLock = new Object();
1484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 /**
1486 * The animation currently associated with this view.
1487 * @hide
1488 */
1489 protected Animation mCurrentAnimation = null;
1490
1491 /**
1492 * Width as measured during measure pass.
1493 * {@hide}
1494 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001495 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001496 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497
1498 /**
1499 * Height as measured during measure pass.
1500 * {@hide}
1501 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001502 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001503 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504
1505 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001506 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1507 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1508 * its display list. This flag, used only when hw accelerated, allows us to clear the
1509 * flag while retaining this information until it's needed (at getDisplayList() time and
1510 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1511 *
1512 * {@hide}
1513 */
1514 boolean mRecreateDisplayList = false;
1515
1516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001517 * The view's identifier.
1518 * {@hide}
1519 *
1520 * @see #setId(int)
1521 * @see #getId()
1522 */
1523 @ViewDebug.ExportedProperty(resolveId = true)
1524 int mID = NO_ID;
1525
1526 /**
1527 * The view's tag.
1528 * {@hide}
1529 *
1530 * @see #setTag(Object)
1531 * @see #getTag()
1532 */
1533 protected Object mTag;
1534
1535 // for mPrivateFlags:
1536 /** {@hide} */
1537 static final int WANTS_FOCUS = 0x00000001;
1538 /** {@hide} */
1539 static final int FOCUSED = 0x00000002;
1540 /** {@hide} */
1541 static final int SELECTED = 0x00000004;
1542 /** {@hide} */
1543 static final int IS_ROOT_NAMESPACE = 0x00000008;
1544 /** {@hide} */
1545 static final int HAS_BOUNDS = 0x00000010;
1546 /** {@hide} */
1547 static final int DRAWN = 0x00000020;
1548 /**
1549 * When this flag is set, this view is running an animation on behalf of its
1550 * children and should therefore not cancel invalidate requests, even if they
1551 * lie outside of this view's bounds.
1552 *
1553 * {@hide}
1554 */
1555 static final int DRAW_ANIMATION = 0x00000040;
1556 /** {@hide} */
1557 static final int SKIP_DRAW = 0x00000080;
1558 /** {@hide} */
1559 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1560 /** {@hide} */
1561 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1562 /** {@hide} */
1563 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1564 /** {@hide} */
1565 static final int MEASURED_DIMENSION_SET = 0x00000800;
1566 /** {@hide} */
1567 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001568 /** {@hide} */
1569 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001570
1571 private static final int PRESSED = 0x00004000;
1572
1573 /** {@hide} */
1574 static final int DRAWING_CACHE_VALID = 0x00008000;
1575 /**
1576 * Flag used to indicate that this view should be drawn once more (and only once
1577 * more) after its animation has completed.
1578 * {@hide}
1579 */
1580 static final int ANIMATION_STARTED = 0x00010000;
1581
1582 private static final int SAVE_STATE_CALLED = 0x00020000;
1583
1584 /**
1585 * Indicates that the View returned true when onSetAlpha() was called and that
1586 * the alpha must be restored.
1587 * {@hide}
1588 */
1589 static final int ALPHA_SET = 0x00040000;
1590
1591 /**
1592 * Set by {@link #setScrollContainer(boolean)}.
1593 */
1594 static final int SCROLL_CONTAINER = 0x00080000;
1595
1596 /**
1597 * Set by {@link #setScrollContainer(boolean)}.
1598 */
1599 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1600
1601 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001602 * View flag indicating whether this view was invalidated (fully or partially.)
1603 *
1604 * @hide
1605 */
1606 static final int DIRTY = 0x00200000;
1607
1608 /**
1609 * View flag indicating whether this view was invalidated by an opaque
1610 * invalidate request.
1611 *
1612 * @hide
1613 */
1614 static final int DIRTY_OPAQUE = 0x00400000;
1615
1616 /**
1617 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1618 *
1619 * @hide
1620 */
1621 static final int DIRTY_MASK = 0x00600000;
1622
1623 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001624 * Indicates whether the background is opaque.
1625 *
1626 * @hide
1627 */
1628 static final int OPAQUE_BACKGROUND = 0x00800000;
1629
1630 /**
1631 * Indicates whether the scrollbars are opaque.
1632 *
1633 * @hide
1634 */
1635 static final int OPAQUE_SCROLLBARS = 0x01000000;
1636
1637 /**
1638 * Indicates whether the view is opaque.
1639 *
1640 * @hide
1641 */
1642 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001643
Adam Powelle14579b2009-12-16 18:39:52 -08001644 /**
1645 * Indicates a prepressed state;
1646 * the short time between ACTION_DOWN and recognizing
1647 * a 'real' press. Prepressed is used to recognize quick taps
1648 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001649 *
Adam Powelle14579b2009-12-16 18:39:52 -08001650 * @hide
1651 */
1652 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001653
Adam Powellc9fbaab2010-02-16 17:16:19 -08001654 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001655 * Indicates whether the view is temporarily detached.
1656 *
1657 * @hide
1658 */
1659 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001660
Adam Powell8568c3a2010-04-19 14:26:11 -07001661 /**
1662 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001663 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001664 * @hide
1665 */
1666 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001667
1668 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001669 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1670 * @hide
1671 */
1672 private static final int HOVERED = 0x10000000;
1673
1674 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001675 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1676 * for transform operations
1677 *
1678 * @hide
1679 */
Adam Powellf37df072010-09-17 16:22:49 -07001680 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001681
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001682 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001683 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001684
Chet Haasefd2b0022010-08-06 13:08:56 -07001685 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001686 * Indicates that this view was specifically invalidated, not just dirtied because some
1687 * child view was invalidated. The flag is used to determine when we need to recreate
1688 * a view's display list (as opposed to just returning a reference to its existing
1689 * display list).
1690 *
1691 * @hide
1692 */
1693 static final int INVALIDATED = 0x80000000;
1694
Christopher Tate3d4bf172011-03-28 16:16:46 -07001695 /* Masks for mPrivateFlags2 */
1696
1697 /**
1698 * Indicates that this view has reported that it can accept the current drag's content.
1699 * Cleared when the drag operation concludes.
1700 * @hide
1701 */
1702 static final int DRAG_CAN_ACCEPT = 0x00000001;
1703
1704 /**
1705 * Indicates that this view is currently directly under the drag location in a
1706 * drag-and-drop operation involving content that it can accept. Cleared when
1707 * the drag exits the view, or when the drag operation concludes.
1708 * @hide
1709 */
1710 static final int DRAG_HOVERED = 0x00000002;
1711
1712 /* End of masks for mPrivateFlags2 */
1713
1714 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1715
Chet Haasedaf98e92011-01-10 14:10:36 -08001716 /**
Adam Powell637d3372010-08-25 14:37:03 -07001717 * Always allow a user to over-scroll this view, provided it is a
1718 * view that can scroll.
1719 *
1720 * @see #getOverScrollMode()
1721 * @see #setOverScrollMode(int)
1722 */
1723 public static final int OVER_SCROLL_ALWAYS = 0;
1724
1725 /**
1726 * Allow a user to over-scroll this view only if the content is large
1727 * enough to meaningfully scroll, provided it is a view that can scroll.
1728 *
1729 * @see #getOverScrollMode()
1730 * @see #setOverScrollMode(int)
1731 */
1732 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1733
1734 /**
1735 * Never allow a user to over-scroll this view.
1736 *
1737 * @see #getOverScrollMode()
1738 * @see #setOverScrollMode(int)
1739 */
1740 public static final int OVER_SCROLL_NEVER = 2;
1741
1742 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001743 * View has requested the status bar to be visible (the default).
1744 *
Joe Malin32736f02011-01-19 16:14:20 -08001745 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001746 */
1747 public static final int STATUS_BAR_VISIBLE = 0;
1748
1749 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001750 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001751 *
Joe Malin32736f02011-01-19 16:14:20 -08001752 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001753 */
1754 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1755
1756 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001757 * @hide
1758 *
1759 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1760 * out of the public fields to keep the undefined bits out of the developer's way.
1761 *
1762 * Flag to make the status bar not expandable. Unless you also
1763 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1764 */
1765 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1766
1767 /**
1768 * @hide
1769 *
1770 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1771 * out of the public fields to keep the undefined bits out of the developer's way.
1772 *
1773 * Flag to hide notification icons and scrolling ticker text.
1774 */
1775 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1776
1777 /**
1778 * @hide
1779 *
1780 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1781 * out of the public fields to keep the undefined bits out of the developer's way.
1782 *
1783 * Flag to disable incoming notification alerts. This will not block
1784 * icons, but it will block sound, vibrating and other visual or aural notifications.
1785 */
1786 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1787
1788 /**
1789 * @hide
1790 *
1791 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1792 * out of the public fields to keep the undefined bits out of the developer's way.
1793 *
1794 * Flag to hide only the scrolling ticker. Note that
1795 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1796 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1797 */
1798 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1799
1800 /**
1801 * @hide
1802 *
1803 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1804 * out of the public fields to keep the undefined bits out of the developer's way.
1805 *
1806 * Flag to hide the center system info area.
1807 */
1808 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1809
1810 /**
1811 * @hide
1812 *
1813 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1814 * out of the public fields to keep the undefined bits out of the developer's way.
1815 *
1816 * Flag to hide only the navigation buttons. Don't use this
1817 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001818 *
1819 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001820 */
1821 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1822
1823 /**
1824 * @hide
1825 *
1826 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1827 * out of the public fields to keep the undefined bits out of the developer's way.
1828 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001829 * Flag to hide only the back button. Don't use this
1830 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1831 */
1832 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1833
1834 /**
1835 * @hide
1836 *
1837 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1838 * out of the public fields to keep the undefined bits out of the developer's way.
1839 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001840 * Flag to hide only the clock. You might use this if your activity has
1841 * its own clock making the status bar's clock redundant.
1842 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001843 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1844
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001845 /**
1846 * @hide
1847 */
1848 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001849
1850 /**
Adam Powell637d3372010-08-25 14:37:03 -07001851 * Controls the over-scroll mode for this view.
1852 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1853 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1854 * and {@link #OVER_SCROLL_NEVER}.
1855 */
1856 private int mOverScrollMode;
1857
1858 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 * The parent this view is attached to.
1860 * {@hide}
1861 *
1862 * @see #getParent()
1863 */
1864 protected ViewParent mParent;
1865
1866 /**
1867 * {@hide}
1868 */
1869 AttachInfo mAttachInfo;
1870
1871 /**
1872 * {@hide}
1873 */
Romain Guy809a7f62009-05-14 15:44:42 -07001874 @ViewDebug.ExportedProperty(flagMapping = {
1875 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1876 name = "FORCE_LAYOUT"),
1877 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1878 name = "LAYOUT_REQUIRED"),
1879 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001880 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001881 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1882 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1883 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1884 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1885 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001887 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888
1889 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001890 * This view's request for the visibility of the status bar.
1891 * @hide
1892 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001893 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001894 int mSystemUiVisibility;
1895
1896 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001897 * Count of how many windows this view has been attached to.
1898 */
1899 int mWindowAttachCount;
1900
1901 /**
1902 * The layout parameters associated with this view and used by the parent
1903 * {@link android.view.ViewGroup} to determine how this view should be
1904 * laid out.
1905 * {@hide}
1906 */
1907 protected ViewGroup.LayoutParams mLayoutParams;
1908
1909 /**
1910 * The view flags hold various views states.
1911 * {@hide}
1912 */
1913 @ViewDebug.ExportedProperty
1914 int mViewFlags;
1915
1916 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001917 * The transform matrix for the View. This transform is calculated internally
1918 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1919 * is used by default. Do *not* use this variable directly; instead call
1920 * getMatrix(), which will automatically recalculate the matrix if necessary
1921 * to get the correct matrix based on the latest rotation and scale properties.
1922 */
1923 private final Matrix mMatrix = new Matrix();
1924
1925 /**
1926 * The transform matrix for the View. This transform is calculated internally
1927 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1928 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001929 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001930 * to get the correct matrix based on the latest rotation and scale properties.
1931 */
1932 private Matrix mInverseMatrix;
1933
1934 /**
1935 * An internal variable that tracks whether we need to recalculate the
1936 * transform matrix, based on whether the rotation or scaleX/Y properties
1937 * have changed since the matrix was last calculated.
1938 */
Chet Haasea00f3862011-02-22 06:34:40 -08001939 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001940
1941 /**
1942 * An internal variable that tracks whether we need to recalculate the
1943 * transform matrix, based on whether the rotation or scaleX/Y properties
1944 * have changed since the matrix was last calculated.
1945 */
1946 private boolean mInverseMatrixDirty = true;
1947
1948 /**
1949 * A variable that tracks whether we need to recalculate the
1950 * transform matrix, based on whether the rotation or scaleX/Y properties
1951 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001952 * is only valid after a call to updateMatrix() or to a function that
1953 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001954 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001955 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001956
1957 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001958 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
1959 */
1960 private Camera mCamera = null;
1961
1962 /**
1963 * This matrix is used when computing the matrix for 3D rotations.
1964 */
1965 private Matrix matrix3D = null;
1966
1967 /**
1968 * These prev values are used to recalculate a centered pivot point when necessary. The
1969 * pivot point is only used in matrix operations (when rotation, scale, or translation are
1970 * set), so thes values are only used then as well.
1971 */
1972 private int mPrevWidth = -1;
1973 private int mPrevHeight = -1;
1974
Joe Malin32736f02011-01-19 16:14:20 -08001975 private boolean mLastIsOpaque;
1976
Chet Haasefd2b0022010-08-06 13:08:56 -07001977 /**
1978 * Convenience value to check for float values that are close enough to zero to be considered
1979 * zero.
1980 */
Romain Guy2542d192010-08-18 11:47:12 -07001981 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001982
1983 /**
1984 * The degrees rotation around the vertical axis through the pivot point.
1985 */
1986 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001987 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001988
1989 /**
1990 * The degrees rotation around the horizontal axis through the pivot point.
1991 */
1992 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001993 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07001994
1995 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001996 * The degrees rotation around the pivot point.
1997 */
1998 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08001999 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002000
2001 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002002 * The amount of translation of the object away from its left property (post-layout).
2003 */
2004 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002005 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002006
2007 /**
2008 * The amount of translation of the object away from its top property (post-layout).
2009 */
2010 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002011 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002012
2013 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002014 * The amount of scale in the x direction around the pivot point. A
2015 * value of 1 means no scaling is applied.
2016 */
2017 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002018 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002019
2020 /**
2021 * The amount of scale in the y direction around the pivot point. A
2022 * value of 1 means no scaling is applied.
2023 */
2024 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002025 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002026
2027 /**
2028 * The amount of scale in the x direction around the pivot point. A
2029 * value of 1 means no scaling is applied.
2030 */
2031 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002032 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002033
2034 /**
2035 * The amount of scale in the y direction around the pivot point. A
2036 * value of 1 means no scaling is applied.
2037 */
2038 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002039 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002040
2041 /**
2042 * The opacity of the View. This is a value from 0 to 1, where 0 means
2043 * completely transparent and 1 means completely opaque.
2044 */
2045 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002046 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002047
2048 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002049 * The distance in pixels from the left edge of this view's parent
2050 * to the left edge of this view.
2051 * {@hide}
2052 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002053 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 protected int mLeft;
2055 /**
2056 * The distance in pixels from the left edge of this view's parent
2057 * to the right edge of this view.
2058 * {@hide}
2059 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002060 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 protected int mRight;
2062 /**
2063 * The distance in pixels from the top edge of this view's parent
2064 * to the top edge of this view.
2065 * {@hide}
2066 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002067 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002068 protected int mTop;
2069 /**
2070 * The distance in pixels from the top edge of this view's parent
2071 * to the bottom edge of this view.
2072 * {@hide}
2073 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002074 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 protected int mBottom;
2076
2077 /**
2078 * The offset, in pixels, by which the content of this view is scrolled
2079 * horizontally.
2080 * {@hide}
2081 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002082 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002083 protected int mScrollX;
2084 /**
2085 * The offset, in pixels, by which the content of this view is scrolled
2086 * vertically.
2087 * {@hide}
2088 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002089 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002090 protected int mScrollY;
2091
2092 /**
2093 * The left padding in pixels, that is the distance in pixels between the
2094 * left edge of this view and the left edge of its content.
2095 * {@hide}
2096 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002097 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002098 protected int mPaddingLeft;
2099 /**
2100 * The right padding in pixels, that is the distance in pixels between the
2101 * right edge of this view and the right edge of its content.
2102 * {@hide}
2103 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002104 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002105 protected int mPaddingRight;
2106 /**
2107 * The top padding in pixels, that is the distance in pixels between the
2108 * top edge of this view and the top edge of its content.
2109 * {@hide}
2110 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002111 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002112 protected int mPaddingTop;
2113 /**
2114 * The bottom padding in pixels, that is the distance in pixels between the
2115 * bottom edge of this view and the bottom edge of its content.
2116 * {@hide}
2117 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002118 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002119 protected int mPaddingBottom;
2120
2121 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002122 * Briefly describes the view and is primarily used for accessibility support.
2123 */
2124 private CharSequence mContentDescription;
2125
2126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 * Cache the paddingRight set by the user to append to the scrollbar's size.
2128 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002129 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002130 int mUserPaddingRight;
2131
2132 /**
2133 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2134 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002135 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 int mUserPaddingBottom;
2137
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002138 /**
Adam Powell20232d02010-12-08 21:08:53 -08002139 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2140 */
2141 @ViewDebug.ExportedProperty(category = "padding")
2142 int mUserPaddingLeft;
2143
2144 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002145 * @hide
2146 */
2147 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2148 /**
2149 * @hide
2150 */
2151 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152
2153 private Resources mResources = null;
2154
2155 private Drawable mBGDrawable;
2156
2157 private int mBackgroundResource;
2158 private boolean mBackgroundSizeChanged;
2159
2160 /**
2161 * Listener used to dispatch focus change events.
2162 * This field should be made private, so it is hidden from the SDK.
2163 * {@hide}
2164 */
2165 protected OnFocusChangeListener mOnFocusChangeListener;
2166
2167 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002168 * Listeners for layout change events.
2169 */
2170 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2171
2172 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002173 * Listeners for attach events.
2174 */
2175 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2176
2177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 * Listener used to dispatch click events.
2179 * This field should be made private, so it is hidden from the SDK.
2180 * {@hide}
2181 */
2182 protected OnClickListener mOnClickListener;
2183
2184 /**
2185 * Listener used to dispatch long click events.
2186 * This field should be made private, so it is hidden from the SDK.
2187 * {@hide}
2188 */
2189 protected OnLongClickListener mOnLongClickListener;
2190
2191 /**
2192 * Listener used to build the context menu.
2193 * This field should be made private, so it is hidden from the SDK.
2194 * {@hide}
2195 */
2196 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2197
2198 private OnKeyListener mOnKeyListener;
2199
2200 private OnTouchListener mOnTouchListener;
2201
Jeff Brown33bbfd22011-02-24 20:55:35 -08002202 private OnGenericMotionListener mOnGenericMotionListener;
2203
Chris Tate32affef2010-10-18 15:29:21 -07002204 private OnDragListener mOnDragListener;
2205
Joe Onorato664644d2011-01-23 17:53:23 -08002206 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002208 /**
2209 * The application environment this view lives in.
2210 * This field should be made private, so it is hidden from the SDK.
2211 * {@hide}
2212 */
2213 protected Context mContext;
2214
2215 private ScrollabilityCache mScrollCache;
2216
2217 private int[] mDrawableState = null;
2218
Romain Guy0211a0a2011-02-14 16:34:59 -08002219 /**
2220 * Set to true when drawing cache is enabled and cannot be created.
2221 *
2222 * @hide
2223 */
2224 public boolean mCachingFailed;
2225
Romain Guy02890fd2010-08-06 17:58:44 -07002226 private Bitmap mDrawingCache;
2227 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002228 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002229 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002230
2231 /**
2232 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2233 * the user may specify which view to go to next.
2234 */
2235 private int mNextFocusLeftId = View.NO_ID;
2236
2237 /**
2238 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2239 * the user may specify which view to go to next.
2240 */
2241 private int mNextFocusRightId = View.NO_ID;
2242
2243 /**
2244 * When this view has focus and the next focus is {@link #FOCUS_UP},
2245 * the user may specify which view to go to next.
2246 */
2247 private int mNextFocusUpId = View.NO_ID;
2248
2249 /**
2250 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2251 * the user may specify which view to go to next.
2252 */
2253 private int mNextFocusDownId = View.NO_ID;
2254
Jeff Brown4e6319b2010-12-13 10:36:51 -08002255 /**
2256 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2257 * the user may specify which view to go to next.
2258 */
2259 int mNextFocusForwardId = View.NO_ID;
2260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002262 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002263 private PerformClick mPerformClick;
Joe Malin32736f02011-01-19 16:14:20 -08002264
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002265 private UnsetPressedState mUnsetPressedState;
2266
2267 /**
2268 * Whether the long press's action has been invoked. The tap's action is invoked on the
2269 * up event while a long press is invoked as soon as the long press duration is reached, so
2270 * a long press could be performed before the tap is checked, in which case the tap's action
2271 * should not be invoked.
2272 */
2273 private boolean mHasPerformedLongPress;
2274
2275 /**
2276 * The minimum height of the view. We'll try our best to have the height
2277 * of this view to at least this amount.
2278 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002279 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002280 private int mMinHeight;
2281
2282 /**
2283 * The minimum width of the view. We'll try our best to have the width
2284 * of this view to at least this amount.
2285 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002286 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002287 private int mMinWidth;
2288
2289 /**
2290 * The delegate to handle touch events that are physically in this view
2291 * but should be handled by another view.
2292 */
2293 private TouchDelegate mTouchDelegate = null;
2294
2295 /**
2296 * Solid color to use as a background when creating the drawing cache. Enables
2297 * the cache to use 16 bit bitmaps instead of 32 bit.
2298 */
2299 private int mDrawingCacheBackgroundColor = 0;
2300
2301 /**
2302 * Special tree observer used when mAttachInfo is null.
2303 */
2304 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002305
Adam Powelle14579b2009-12-16 18:39:52 -08002306 /**
2307 * Cache the touch slop from the context that created the view.
2308 */
2309 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002311 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002312 * Object that handles automatic animation of view properties.
2313 */
2314 private ViewPropertyAnimator mAnimator = null;
2315
2316 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002317 * Flag indicating that a drag can cross window boundaries. When
2318 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2319 * with this flag set, all visible applications will be able to participate
2320 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002321 *
2322 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002323 */
2324 public static final int DRAG_FLAG_GLOBAL = 1;
2325
2326 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002327 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2328 */
2329 private float mVerticalScrollFactor;
2330
2331 /**
Adam Powell20232d02010-12-08 21:08:53 -08002332 * Position of the vertical scroll bar.
2333 */
2334 private int mVerticalScrollbarPosition;
2335
2336 /**
2337 * Position the scroll bar at the default position as determined by the system.
2338 */
2339 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2340
2341 /**
2342 * Position the scroll bar along the left edge.
2343 */
2344 public static final int SCROLLBAR_POSITION_LEFT = 1;
2345
2346 /**
2347 * Position the scroll bar along the right edge.
2348 */
2349 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2350
2351 /**
Romain Guy171c5922011-01-06 10:04:23 -08002352 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002353 *
2354 * @see #getLayerType()
2355 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002356 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002357 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002358 */
2359 public static final int LAYER_TYPE_NONE = 0;
2360
2361 /**
2362 * <p>Indicates that the view has a software layer. A software layer is backed
2363 * by a bitmap and causes the view to be rendered using Android's software
2364 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002365 *
Romain Guy171c5922011-01-06 10:04:23 -08002366 * <p>Software layers have various usages:</p>
2367 * <p>When the application is not using hardware acceleration, a software layer
2368 * is useful to apply a specific color filter and/or blending mode and/or
2369 * translucency to a view and all its children.</p>
2370 * <p>When the application is using hardware acceleration, a software layer
2371 * is useful to render drawing primitives not supported by the hardware
2372 * accelerated pipeline. It can also be used to cache a complex view tree
2373 * into a texture and reduce the complexity of drawing operations. For instance,
2374 * when animating a complex view tree with a translation, a software layer can
2375 * be used to render the view tree only once.</p>
2376 * <p>Software layers should be avoided when the affected view tree updates
2377 * often. Every update will require to re-render the software layer, which can
2378 * potentially be slow (particularly when hardware acceleration is turned on
2379 * since the layer will have to be uploaded into a hardware texture after every
2380 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002381 *
2382 * @see #getLayerType()
2383 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002384 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002385 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002386 */
2387 public static final int LAYER_TYPE_SOFTWARE = 1;
2388
2389 /**
2390 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2391 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2392 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2393 * rendering pipeline, but only if hardware acceleration is turned on for the
2394 * view hierarchy. When hardware acceleration is turned off, hardware layers
2395 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002396 *
Romain Guy171c5922011-01-06 10:04:23 -08002397 * <p>A hardware layer is useful to apply a specific color filter and/or
2398 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002399 * <p>A hardware layer can be used to cache a complex view tree into a
2400 * texture and reduce the complexity of drawing operations. For instance,
2401 * when animating a complex view tree with a translation, a hardware layer can
2402 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002403 * <p>A hardware layer can also be used to increase the rendering quality when
2404 * rotation transformations are applied on a view. It can also be used to
2405 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002406 *
2407 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002408 * @see #setLayerType(int, android.graphics.Paint)
2409 * @see #LAYER_TYPE_NONE
2410 * @see #LAYER_TYPE_SOFTWARE
2411 */
2412 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002413
Romain Guy3aaff3a2011-01-12 14:18:47 -08002414 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2415 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2416 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2417 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2418 })
Romain Guy171c5922011-01-06 10:04:23 -08002419 int mLayerType = LAYER_TYPE_NONE;
2420 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002421 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002422
2423 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002424 * Consistency verifier for debugging purposes.
2425 * @hide
2426 */
2427 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2428 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2429 new InputEventConsistencyVerifier(this, 0) : null;
2430
2431 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002432 * Simple constructor to use when creating a view from code.
2433 *
2434 * @param context The Context the view is running in, through which it can
2435 * access the current theme, resources, etc.
2436 */
2437 public View(Context context) {
2438 mContext = context;
2439 mResources = context != null ? context.getResources() : null;
Romain Guy8f1344f52009-05-15 16:03:59 -07002440 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED;
Adam Powelle14579b2009-12-16 18:39:52 -08002441 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002442 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002443 }
2444
2445 /**
2446 * Constructor that is called when inflating a view from XML. This is called
2447 * when a view is being constructed from an XML file, supplying attributes
2448 * that were specified in the XML file. This version uses a default style of
2449 * 0, so the only attribute values applied are those in the Context's Theme
2450 * and the given AttributeSet.
2451 *
2452 * <p>
2453 * The method onFinishInflate() will be called after all children have been
2454 * added.
2455 *
2456 * @param context The Context the view is running in, through which it can
2457 * access the current theme, resources, etc.
2458 * @param attrs The attributes of the XML tag that is inflating the view.
2459 * @see #View(Context, AttributeSet, int)
2460 */
2461 public View(Context context, AttributeSet attrs) {
2462 this(context, attrs, 0);
2463 }
2464
2465 /**
2466 * Perform inflation from XML and apply a class-specific base style. This
2467 * constructor of View allows subclasses to use their own base style when
2468 * they are inflating. For example, a Button class's constructor would call
2469 * this version of the super class constructor and supply
2470 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2471 * the theme's button style to modify all of the base view attributes (in
2472 * particular its background) as well as the Button class's attributes.
2473 *
2474 * @param context The Context the view is running in, through which it can
2475 * access the current theme, resources, etc.
2476 * @param attrs The attributes of the XML tag that is inflating the view.
2477 * @param defStyle The default style to apply to this view. If 0, no style
2478 * will be applied (beyond what is included in the theme). This may
2479 * either be an attribute resource, whose value will be retrieved
2480 * from the current theme, or an explicit style resource.
2481 * @see #View(Context, AttributeSet)
2482 */
2483 public View(Context context, AttributeSet attrs, int defStyle) {
2484 this(context);
2485
2486 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2487 defStyle, 0);
2488
2489 Drawable background = null;
2490
2491 int leftPadding = -1;
2492 int topPadding = -1;
2493 int rightPadding = -1;
2494 int bottomPadding = -1;
2495
2496 int padding = -1;
2497
2498 int viewFlagValues = 0;
2499 int viewFlagMasks = 0;
2500
2501 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002503 int x = 0;
2504 int y = 0;
2505
Chet Haase73066682010-11-29 15:55:32 -08002506 float tx = 0;
2507 float ty = 0;
2508 float rotation = 0;
2509 float rotationX = 0;
2510 float rotationY = 0;
2511 float sx = 1f;
2512 float sy = 1f;
2513 boolean transformSet = false;
2514
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002515 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2516
Adam Powell637d3372010-08-25 14:37:03 -07002517 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002518 final int N = a.getIndexCount();
2519 for (int i = 0; i < N; i++) {
2520 int attr = a.getIndex(i);
2521 switch (attr) {
2522 case com.android.internal.R.styleable.View_background:
2523 background = a.getDrawable(attr);
2524 break;
2525 case com.android.internal.R.styleable.View_padding:
2526 padding = a.getDimensionPixelSize(attr, -1);
2527 break;
2528 case com.android.internal.R.styleable.View_paddingLeft:
2529 leftPadding = a.getDimensionPixelSize(attr, -1);
2530 break;
2531 case com.android.internal.R.styleable.View_paddingTop:
2532 topPadding = a.getDimensionPixelSize(attr, -1);
2533 break;
2534 case com.android.internal.R.styleable.View_paddingRight:
2535 rightPadding = a.getDimensionPixelSize(attr, -1);
2536 break;
2537 case com.android.internal.R.styleable.View_paddingBottom:
2538 bottomPadding = a.getDimensionPixelSize(attr, -1);
2539 break;
2540 case com.android.internal.R.styleable.View_scrollX:
2541 x = a.getDimensionPixelOffset(attr, 0);
2542 break;
2543 case com.android.internal.R.styleable.View_scrollY:
2544 y = a.getDimensionPixelOffset(attr, 0);
2545 break;
Chet Haase73066682010-11-29 15:55:32 -08002546 case com.android.internal.R.styleable.View_alpha:
2547 setAlpha(a.getFloat(attr, 1f));
2548 break;
2549 case com.android.internal.R.styleable.View_transformPivotX:
2550 setPivotX(a.getDimensionPixelOffset(attr, 0));
2551 break;
2552 case com.android.internal.R.styleable.View_transformPivotY:
2553 setPivotY(a.getDimensionPixelOffset(attr, 0));
2554 break;
2555 case com.android.internal.R.styleable.View_translationX:
2556 tx = a.getDimensionPixelOffset(attr, 0);
2557 transformSet = true;
2558 break;
2559 case com.android.internal.R.styleable.View_translationY:
2560 ty = a.getDimensionPixelOffset(attr, 0);
2561 transformSet = true;
2562 break;
2563 case com.android.internal.R.styleable.View_rotation:
2564 rotation = a.getFloat(attr, 0);
2565 transformSet = true;
2566 break;
2567 case com.android.internal.R.styleable.View_rotationX:
2568 rotationX = a.getFloat(attr, 0);
2569 transformSet = true;
2570 break;
2571 case com.android.internal.R.styleable.View_rotationY:
2572 rotationY = a.getFloat(attr, 0);
2573 transformSet = true;
2574 break;
2575 case com.android.internal.R.styleable.View_scaleX:
2576 sx = a.getFloat(attr, 1f);
2577 transformSet = true;
2578 break;
2579 case com.android.internal.R.styleable.View_scaleY:
2580 sy = a.getFloat(attr, 1f);
2581 transformSet = true;
2582 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002583 case com.android.internal.R.styleable.View_id:
2584 mID = a.getResourceId(attr, NO_ID);
2585 break;
2586 case com.android.internal.R.styleable.View_tag:
2587 mTag = a.getText(attr);
2588 break;
2589 case com.android.internal.R.styleable.View_fitsSystemWindows:
2590 if (a.getBoolean(attr, false)) {
2591 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2592 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2593 }
2594 break;
2595 case com.android.internal.R.styleable.View_focusable:
2596 if (a.getBoolean(attr, false)) {
2597 viewFlagValues |= FOCUSABLE;
2598 viewFlagMasks |= FOCUSABLE_MASK;
2599 }
2600 break;
2601 case com.android.internal.R.styleable.View_focusableInTouchMode:
2602 if (a.getBoolean(attr, false)) {
2603 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2604 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2605 }
2606 break;
2607 case com.android.internal.R.styleable.View_clickable:
2608 if (a.getBoolean(attr, false)) {
2609 viewFlagValues |= CLICKABLE;
2610 viewFlagMasks |= CLICKABLE;
2611 }
2612 break;
2613 case com.android.internal.R.styleable.View_longClickable:
2614 if (a.getBoolean(attr, false)) {
2615 viewFlagValues |= LONG_CLICKABLE;
2616 viewFlagMasks |= LONG_CLICKABLE;
2617 }
2618 break;
2619 case com.android.internal.R.styleable.View_saveEnabled:
2620 if (!a.getBoolean(attr, true)) {
2621 viewFlagValues |= SAVE_DISABLED;
2622 viewFlagMasks |= SAVE_DISABLED_MASK;
2623 }
2624 break;
2625 case com.android.internal.R.styleable.View_duplicateParentState:
2626 if (a.getBoolean(attr, false)) {
2627 viewFlagValues |= DUPLICATE_PARENT_STATE;
2628 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2629 }
2630 break;
2631 case com.android.internal.R.styleable.View_visibility:
2632 final int visibility = a.getInt(attr, 0);
2633 if (visibility != 0) {
2634 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2635 viewFlagMasks |= VISIBILITY_MASK;
2636 }
2637 break;
Cibu Johny7632cb92010-02-22 13:01:02 -08002638 case com.android.internal.R.styleable.View_horizontalDirection:
2639 final int layoutDirection = a.getInt(attr, 0);
2640 if (layoutDirection != 0) {
2641 viewFlagValues |= HORIZONTAL_DIRECTION_FLAGS[layoutDirection];
2642 viewFlagMasks |= HORIZONTAL_DIRECTION_MASK;
2643 }
2644 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002645 case com.android.internal.R.styleable.View_drawingCacheQuality:
2646 final int cacheQuality = a.getInt(attr, 0);
2647 if (cacheQuality != 0) {
2648 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2649 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2650 }
2651 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002652 case com.android.internal.R.styleable.View_contentDescription:
2653 mContentDescription = a.getString(attr);
2654 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002655 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2656 if (!a.getBoolean(attr, true)) {
2657 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2658 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2659 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002660 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002661 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2662 if (!a.getBoolean(attr, true)) {
2663 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2664 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2665 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002666 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002667 case R.styleable.View_scrollbars:
2668 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2669 if (scrollbars != SCROLLBARS_NONE) {
2670 viewFlagValues |= scrollbars;
2671 viewFlagMasks |= SCROLLBARS_MASK;
2672 initializeScrollbars(a);
2673 }
2674 break;
2675 case R.styleable.View_fadingEdge:
2676 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2677 if (fadingEdge != FADING_EDGE_NONE) {
2678 viewFlagValues |= fadingEdge;
2679 viewFlagMasks |= FADING_EDGE_MASK;
2680 initializeFadingEdge(a);
2681 }
2682 break;
2683 case R.styleable.View_scrollbarStyle:
2684 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2685 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2686 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2687 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2688 }
2689 break;
2690 case R.styleable.View_isScrollContainer:
2691 setScrollContainer = true;
2692 if (a.getBoolean(attr, false)) {
2693 setScrollContainer(true);
2694 }
2695 break;
2696 case com.android.internal.R.styleable.View_keepScreenOn:
2697 if (a.getBoolean(attr, false)) {
2698 viewFlagValues |= KEEP_SCREEN_ON;
2699 viewFlagMasks |= KEEP_SCREEN_ON;
2700 }
2701 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002702 case R.styleable.View_filterTouchesWhenObscured:
2703 if (a.getBoolean(attr, false)) {
2704 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2705 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2706 }
2707 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 case R.styleable.View_nextFocusLeft:
2709 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2710 break;
2711 case R.styleable.View_nextFocusRight:
2712 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2713 break;
2714 case R.styleable.View_nextFocusUp:
2715 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2716 break;
2717 case R.styleable.View_nextFocusDown:
2718 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2719 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002720 case R.styleable.View_nextFocusForward:
2721 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2722 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 case R.styleable.View_minWidth:
2724 mMinWidth = a.getDimensionPixelSize(attr, 0);
2725 break;
2726 case R.styleable.View_minHeight:
2727 mMinHeight = a.getDimensionPixelSize(attr, 0);
2728 break;
Romain Guy9a817362009-05-01 10:57:14 -07002729 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002730 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002731 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002732 + "be used within a restricted context");
2733 }
2734
Romain Guy9a817362009-05-01 10:57:14 -07002735 final String handlerName = a.getString(attr);
2736 if (handlerName != null) {
2737 setOnClickListener(new OnClickListener() {
2738 private Method mHandler;
2739
2740 public void onClick(View v) {
2741 if (mHandler == null) {
2742 try {
2743 mHandler = getContext().getClass().getMethod(handlerName,
2744 View.class);
2745 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002746 int id = getId();
2747 String idText = id == NO_ID ? "" : " with id '"
2748 + getContext().getResources().getResourceEntryName(
2749 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002750 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002751 handlerName + "(View) in the activity "
2752 + getContext().getClass() + " for onClick handler"
2753 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002754 }
2755 }
2756
2757 try {
2758 mHandler.invoke(getContext(), View.this);
2759 } catch (IllegalAccessException e) {
2760 throw new IllegalStateException("Could not execute non "
2761 + "public method of the activity", e);
2762 } catch (InvocationTargetException e) {
2763 throw new IllegalStateException("Could not execute "
2764 + "method of the activity", e);
2765 }
2766 }
2767 });
2768 }
2769 break;
Adam Powell637d3372010-08-25 14:37:03 -07002770 case R.styleable.View_overScrollMode:
2771 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2772 break;
Adam Powell20232d02010-12-08 21:08:53 -08002773 case R.styleable.View_verticalScrollbarPosition:
2774 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2775 break;
Romain Guy171c5922011-01-06 10:04:23 -08002776 case R.styleable.View_layerType:
2777 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2778 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002779 }
2780 }
2781
Adam Powell637d3372010-08-25 14:37:03 -07002782 setOverScrollMode(overScrollMode);
2783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002784 if (background != null) {
2785 setBackgroundDrawable(background);
2786 }
2787
2788 if (padding >= 0) {
2789 leftPadding = padding;
2790 topPadding = padding;
2791 rightPadding = padding;
2792 bottomPadding = padding;
2793 }
2794
2795 // If the user specified the padding (either with android:padding or
2796 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2797 // use the default padding or the padding from the background drawable
2798 // (stored at this point in mPadding*)
2799 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2800 topPadding >= 0 ? topPadding : mPaddingTop,
2801 rightPadding >= 0 ? rightPadding : mPaddingRight,
2802 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2803
2804 if (viewFlagMasks != 0) {
2805 setFlags(viewFlagValues, viewFlagMasks);
2806 }
2807
2808 // Needs to be called after mViewFlags is set
2809 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2810 recomputePadding();
2811 }
2812
2813 if (x != 0 || y != 0) {
2814 scrollTo(x, y);
2815 }
2816
Chet Haase73066682010-11-29 15:55:32 -08002817 if (transformSet) {
2818 setTranslationX(tx);
2819 setTranslationY(ty);
2820 setRotation(rotation);
2821 setRotationX(rotationX);
2822 setRotationY(rotationY);
2823 setScaleX(sx);
2824 setScaleY(sy);
2825 }
2826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002827 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
2828 setScrollContainer(true);
2829 }
Romain Guy8f1344f52009-05-15 16:03:59 -07002830
2831 computeOpaqueFlags();
2832
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002833 a.recycle();
2834 }
2835
2836 /**
2837 * Non-public constructor for use in testing
2838 */
2839 View() {
2840 }
2841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 /**
2843 * <p>
2844 * Initializes the fading edges from a given set of styled attributes. This
2845 * method should be called by subclasses that need fading edges and when an
2846 * instance of these subclasses is created programmatically rather than
2847 * being inflated from XML. This method is automatically called when the XML
2848 * is inflated.
2849 * </p>
2850 *
2851 * @param a the styled attributes set to initialize the fading edges from
2852 */
2853 protected void initializeFadingEdge(TypedArray a) {
2854 initScrollCache();
2855
2856 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
2857 R.styleable.View_fadingEdgeLength,
2858 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
2859 }
2860
2861 /**
2862 * Returns the size of the vertical faded edges used to indicate that more
2863 * content in this view is visible.
2864 *
2865 * @return The size in pixels of the vertical faded edge or 0 if vertical
2866 * faded edges are not enabled for this view.
2867 * @attr ref android.R.styleable#View_fadingEdgeLength
2868 */
2869 public int getVerticalFadingEdgeLength() {
2870 if (isVerticalFadingEdgeEnabled()) {
2871 ScrollabilityCache cache = mScrollCache;
2872 if (cache != null) {
2873 return cache.fadingEdgeLength;
2874 }
2875 }
2876 return 0;
2877 }
2878
2879 /**
2880 * Set the size of the faded edge used to indicate that more content in this
2881 * view is available. Will not change whether the fading edge is enabled; use
2882 * {@link #setVerticalFadingEdgeEnabled} or {@link #setHorizontalFadingEdgeEnabled}
2883 * to enable the fading edge for the vertical or horizontal fading edges.
2884 *
2885 * @param length The size in pixels of the faded edge used to indicate that more
2886 * content in this view is visible.
2887 */
2888 public void setFadingEdgeLength(int length) {
2889 initScrollCache();
2890 mScrollCache.fadingEdgeLength = length;
2891 }
2892
2893 /**
2894 * Returns the size of the horizontal faded edges used to indicate that more
2895 * content in this view is visible.
2896 *
2897 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
2898 * faded edges are not enabled for this view.
2899 * @attr ref android.R.styleable#View_fadingEdgeLength
2900 */
2901 public int getHorizontalFadingEdgeLength() {
2902 if (isHorizontalFadingEdgeEnabled()) {
2903 ScrollabilityCache cache = mScrollCache;
2904 if (cache != null) {
2905 return cache.fadingEdgeLength;
2906 }
2907 }
2908 return 0;
2909 }
2910
2911 /**
2912 * Returns the width of the vertical scrollbar.
2913 *
2914 * @return The width in pixels of the vertical scrollbar or 0 if there
2915 * is no vertical scrollbar.
2916 */
2917 public int getVerticalScrollbarWidth() {
2918 ScrollabilityCache cache = mScrollCache;
2919 if (cache != null) {
2920 ScrollBarDrawable scrollBar = cache.scrollBar;
2921 if (scrollBar != null) {
2922 int size = scrollBar.getSize(true);
2923 if (size <= 0) {
2924 size = cache.scrollBarSize;
2925 }
2926 return size;
2927 }
2928 return 0;
2929 }
2930 return 0;
2931 }
2932
2933 /**
2934 * Returns the height of the horizontal scrollbar.
2935 *
2936 * @return The height in pixels of the horizontal scrollbar or 0 if
2937 * there is no horizontal scrollbar.
2938 */
2939 protected int getHorizontalScrollbarHeight() {
2940 ScrollabilityCache cache = mScrollCache;
2941 if (cache != null) {
2942 ScrollBarDrawable scrollBar = cache.scrollBar;
2943 if (scrollBar != null) {
2944 int size = scrollBar.getSize(false);
2945 if (size <= 0) {
2946 size = cache.scrollBarSize;
2947 }
2948 return size;
2949 }
2950 return 0;
2951 }
2952 return 0;
2953 }
2954
2955 /**
2956 * <p>
2957 * Initializes the scrollbars from a given set of styled attributes. This
2958 * method should be called by subclasses that need scrollbars and when an
2959 * instance of these subclasses is created programmatically rather than
2960 * being inflated from XML. This method is automatically called when the XML
2961 * is inflated.
2962 * </p>
2963 *
2964 * @param a the styled attributes set to initialize the scrollbars from
2965 */
2966 protected void initializeScrollbars(TypedArray a) {
2967 initScrollCache();
2968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08002970
Mike Cleronf116bf82009-09-27 19:14:12 -07002971 if (scrollabilityCache.scrollBar == null) {
2972 scrollabilityCache.scrollBar = new ScrollBarDrawable();
2973 }
Joe Malin32736f02011-01-19 16:14:20 -08002974
Romain Guy8bda2482010-03-02 11:42:11 -08002975 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002976
Mike Cleronf116bf82009-09-27 19:14:12 -07002977 if (!fadeScrollbars) {
2978 scrollabilityCache.state = ScrollabilityCache.ON;
2979 }
2980 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08002981
2982
Mike Cleronf116bf82009-09-27 19:14:12 -07002983 scrollabilityCache.scrollBarFadeDuration = a.getInt(
2984 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
2985 .getScrollBarFadeDuration());
2986 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
2987 R.styleable.View_scrollbarDefaultDelayBeforeFade,
2988 ViewConfiguration.getScrollDefaultDelay());
2989
Joe Malin32736f02011-01-19 16:14:20 -08002990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002991 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
2992 com.android.internal.R.styleable.View_scrollbarSize,
2993 ViewConfiguration.get(mContext).getScaledScrollBarSize());
2994
2995 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
2996 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
2997
2998 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
2999 if (thumb != null) {
3000 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3001 }
3002
3003 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3004 false);
3005 if (alwaysDraw) {
3006 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3007 }
3008
3009 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3010 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3011
3012 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3013 if (thumb != null) {
3014 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3015 }
3016
3017 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3018 false);
3019 if (alwaysDraw) {
3020 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3021 }
3022
3023 // Re-apply user/background padding so that scrollbar(s) get added
3024 recomputePadding();
3025 }
3026
3027 /**
3028 * <p>
3029 * Initalizes the scrollability cache if necessary.
3030 * </p>
3031 */
3032 private void initScrollCache() {
3033 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003034 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003035 }
3036 }
3037
3038 /**
Adam Powell20232d02010-12-08 21:08:53 -08003039 * Set the position of the vertical scroll bar. Should be one of
3040 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3041 * {@link #SCROLLBAR_POSITION_RIGHT}.
3042 *
3043 * @param position Where the vertical scroll bar should be positioned.
3044 */
3045 public void setVerticalScrollbarPosition(int position) {
3046 if (mVerticalScrollbarPosition != position) {
3047 mVerticalScrollbarPosition = position;
3048 computeOpaqueFlags();
3049 recomputePadding();
3050 }
3051 }
3052
3053 /**
3054 * @return The position where the vertical scroll bar will show, if applicable.
3055 * @see #setVerticalScrollbarPosition(int)
3056 */
3057 public int getVerticalScrollbarPosition() {
3058 return mVerticalScrollbarPosition;
3059 }
3060
3061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003062 * Register a callback to be invoked when focus of this view changed.
3063 *
3064 * @param l The callback that will run.
3065 */
3066 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3067 mOnFocusChangeListener = l;
3068 }
3069
3070 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003071 * Add a listener that will be called when the bounds of the view change due to
3072 * layout processing.
3073 *
3074 * @param listener The listener that will be called when layout bounds change.
3075 */
3076 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3077 if (mOnLayoutChangeListeners == null) {
3078 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3079 }
3080 mOnLayoutChangeListeners.add(listener);
3081 }
3082
3083 /**
3084 * Remove a listener for layout changes.
3085 *
3086 * @param listener The listener for layout bounds change.
3087 */
3088 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3089 if (mOnLayoutChangeListeners == null) {
3090 return;
3091 }
3092 mOnLayoutChangeListeners.remove(listener);
3093 }
3094
3095 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003096 * Add a listener for attach state changes.
3097 *
3098 * This listener will be called whenever this view is attached or detached
3099 * from a window. Remove the listener using
3100 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3101 *
3102 * @param listener Listener to attach
3103 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3104 */
3105 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3106 if (mOnAttachStateChangeListeners == null) {
3107 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3108 }
3109 mOnAttachStateChangeListeners.add(listener);
3110 }
3111
3112 /**
3113 * Remove a listener for attach state changes. The listener will receive no further
3114 * notification of window attach/detach events.
3115 *
3116 * @param listener Listener to remove
3117 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3118 */
3119 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3120 if (mOnAttachStateChangeListeners == null) {
3121 return;
3122 }
3123 mOnAttachStateChangeListeners.remove(listener);
3124 }
3125
3126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003127 * Returns the focus-change callback registered for this view.
3128 *
3129 * @return The callback, or null if one is not registered.
3130 */
3131 public OnFocusChangeListener getOnFocusChangeListener() {
3132 return mOnFocusChangeListener;
3133 }
3134
3135 /**
3136 * Register a callback to be invoked when this view is clicked. If this view is not
3137 * clickable, it becomes clickable.
3138 *
3139 * @param l The callback that will run
3140 *
3141 * @see #setClickable(boolean)
3142 */
3143 public void setOnClickListener(OnClickListener l) {
3144 if (!isClickable()) {
3145 setClickable(true);
3146 }
3147 mOnClickListener = l;
3148 }
3149
3150 /**
3151 * Register a callback to be invoked when this view is clicked and held. If this view is not
3152 * long clickable, it becomes long clickable.
3153 *
3154 * @param l The callback that will run
3155 *
3156 * @see #setLongClickable(boolean)
3157 */
3158 public void setOnLongClickListener(OnLongClickListener l) {
3159 if (!isLongClickable()) {
3160 setLongClickable(true);
3161 }
3162 mOnLongClickListener = l;
3163 }
3164
3165 /**
3166 * Register a callback to be invoked when the context menu for this view is
3167 * being built. If this view is not long clickable, it becomes long clickable.
3168 *
3169 * @param l The callback that will run
3170 *
3171 */
3172 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3173 if (!isLongClickable()) {
3174 setLongClickable(true);
3175 }
3176 mOnCreateContextMenuListener = l;
3177 }
3178
3179 /**
3180 * Call this view's OnClickListener, if it is defined.
3181 *
3182 * @return True there was an assigned OnClickListener that was called, false
3183 * otherwise is returned.
3184 */
3185 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003186 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003188 if (mOnClickListener != null) {
3189 playSoundEffect(SoundEffectConstants.CLICK);
3190 mOnClickListener.onClick(this);
3191 return true;
3192 }
3193
3194 return false;
3195 }
3196
3197 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003198 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3199 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003200 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003201 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003202 */
3203 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003204 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003206 boolean handled = false;
3207 if (mOnLongClickListener != null) {
3208 handled = mOnLongClickListener.onLongClick(View.this);
3209 }
3210 if (!handled) {
3211 handled = showContextMenu();
3212 }
3213 if (handled) {
3214 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3215 }
3216 return handled;
3217 }
3218
3219 /**
3220 * Bring up the context menu for this view.
3221 *
3222 * @return Whether a context menu was displayed.
3223 */
3224 public boolean showContextMenu() {
3225 return getParent().showContextMenuForChild(this);
3226 }
3227
3228 /**
Adam Powell6e346362010-07-23 10:18:23 -07003229 * Start an action mode.
3230 *
3231 * @param callback Callback that will control the lifecycle of the action mode
3232 * @return The new action mode if it is started, null otherwise
3233 *
3234 * @see ActionMode
3235 */
3236 public ActionMode startActionMode(ActionMode.Callback callback) {
3237 return getParent().startActionModeForChild(this, callback);
3238 }
3239
3240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003241 * Register a callback to be invoked when a key is pressed in this view.
3242 * @param l the key listener to attach to this view
3243 */
3244 public void setOnKeyListener(OnKeyListener l) {
3245 mOnKeyListener = l;
3246 }
3247
3248 /**
3249 * Register a callback to be invoked when a touch event is sent to this view.
3250 * @param l the touch listener to attach to this view
3251 */
3252 public void setOnTouchListener(OnTouchListener l) {
3253 mOnTouchListener = l;
3254 }
3255
3256 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003257 * Register a callback to be invoked when a generic motion event is sent to this view.
3258 * @param l the generic motion listener to attach to this view
3259 */
3260 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3261 mOnGenericMotionListener = l;
3262 }
3263
3264 /**
Joe Malin32736f02011-01-19 16:14:20 -08003265 * Register a drag event listener callback object for this View. The parameter is
3266 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3267 * View, the system calls the
3268 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3269 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003270 */
3271 public void setOnDragListener(OnDragListener l) {
3272 mOnDragListener = l;
3273 }
3274
3275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003276 * Give this view focus. This will cause {@link #onFocusChanged} to be called.
3277 *
3278 * Note: this does not check whether this {@link View} should get focus, it just
3279 * gives it focus no matter what. It should only be called internally by framework
3280 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3281 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003282 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3283 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003284 * focus moved when requestFocus() is called. It may not always
3285 * apply, in which case use the default View.FOCUS_DOWN.
3286 * @param previouslyFocusedRect The rectangle of the view that had focus
3287 * prior in this View's coordinate system.
3288 */
3289 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3290 if (DBG) {
3291 System.out.println(this + " requestFocus()");
3292 }
3293
3294 if ((mPrivateFlags & FOCUSED) == 0) {
3295 mPrivateFlags |= FOCUSED;
3296
3297 if (mParent != null) {
3298 mParent.requestChildFocus(this, this);
3299 }
3300
3301 onFocusChanged(true, direction, previouslyFocusedRect);
3302 refreshDrawableState();
3303 }
3304 }
3305
3306 /**
3307 * Request that a rectangle of this view be visible on the screen,
3308 * scrolling if necessary just enough.
3309 *
3310 * <p>A View should call this if it maintains some notion of which part
3311 * of its content is interesting. For example, a text editing view
3312 * should call this when its cursor moves.
3313 *
3314 * @param rectangle The rectangle.
3315 * @return Whether any parent scrolled.
3316 */
3317 public boolean requestRectangleOnScreen(Rect rectangle) {
3318 return requestRectangleOnScreen(rectangle, false);
3319 }
3320
3321 /**
3322 * Request that a rectangle of this view be visible on the screen,
3323 * scrolling if necessary just enough.
3324 *
3325 * <p>A View should call this if it maintains some notion of which part
3326 * of its content is interesting. For example, a text editing view
3327 * should call this when its cursor moves.
3328 *
3329 * <p>When <code>immediate</code> is set to true, scrolling will not be
3330 * animated.
3331 *
3332 * @param rectangle The rectangle.
3333 * @param immediate True to forbid animated scrolling, false otherwise
3334 * @return Whether any parent scrolled.
3335 */
3336 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3337 View child = this;
3338 ViewParent parent = mParent;
3339 boolean scrolled = false;
3340 while (parent != null) {
3341 scrolled |= parent.requestChildRectangleOnScreen(child,
3342 rectangle, immediate);
3343
3344 // offset rect so next call has the rectangle in the
3345 // coordinate system of its direct child.
3346 rectangle.offset(child.getLeft(), child.getTop());
3347 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3348
3349 if (!(parent instanceof View)) {
3350 break;
3351 }
Romain Guy8506ab42009-06-11 17:35:47 -07003352
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003353 child = (View) parent;
3354 parent = child.getParent();
3355 }
3356 return scrolled;
3357 }
3358
3359 /**
3360 * Called when this view wants to give up focus. This will cause
3361 * {@link #onFocusChanged} to be called.
3362 */
3363 public void clearFocus() {
3364 if (DBG) {
3365 System.out.println(this + " clearFocus()");
3366 }
3367
3368 if ((mPrivateFlags & FOCUSED) != 0) {
3369 mPrivateFlags &= ~FOCUSED;
3370
3371 if (mParent != null) {
3372 mParent.clearChildFocus(this);
3373 }
3374
3375 onFocusChanged(false, 0, null);
3376 refreshDrawableState();
3377 }
3378 }
3379
3380 /**
3381 * Called to clear the focus of a view that is about to be removed.
3382 * Doesn't call clearChildFocus, which prevents this view from taking
3383 * focus again before it has been removed from the parent
3384 */
3385 void clearFocusForRemoval() {
3386 if ((mPrivateFlags & FOCUSED) != 0) {
3387 mPrivateFlags &= ~FOCUSED;
3388
3389 onFocusChanged(false, 0, null);
3390 refreshDrawableState();
3391 }
3392 }
3393
3394 /**
3395 * Called internally by the view system when a new view is getting focus.
3396 * This is what clears the old focus.
3397 */
3398 void unFocus() {
3399 if (DBG) {
3400 System.out.println(this + " unFocus()");
3401 }
3402
3403 if ((mPrivateFlags & FOCUSED) != 0) {
3404 mPrivateFlags &= ~FOCUSED;
3405
3406 onFocusChanged(false, 0, null);
3407 refreshDrawableState();
3408 }
3409 }
3410
3411 /**
3412 * Returns true if this view has focus iteself, or is the ancestor of the
3413 * view that has focus.
3414 *
3415 * @return True if this view has or contains focus, false otherwise.
3416 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003417 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003418 public boolean hasFocus() {
3419 return (mPrivateFlags & FOCUSED) != 0;
3420 }
3421
3422 /**
3423 * Returns true if this view is focusable or if it contains a reachable View
3424 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3425 * is a View whose parents do not block descendants focus.
3426 *
3427 * Only {@link #VISIBLE} views are considered focusable.
3428 *
3429 * @return True if the view is focusable or if the view contains a focusable
3430 * View, false otherwise.
3431 *
3432 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3433 */
3434 public boolean hasFocusable() {
3435 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3436 }
3437
3438 /**
3439 * Called by the view system when the focus state of this view changes.
3440 * When the focus change event is caused by directional navigation, direction
3441 * and previouslyFocusedRect provide insight into where the focus is coming from.
3442 * When overriding, be sure to call up through to the super class so that
3443 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003444 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003445 * @param gainFocus True if the View has focus; false otherwise.
3446 * @param direction The direction focus has moved when requestFocus()
3447 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003448 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3449 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3450 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003451 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3452 * system, of the previously focused view. If applicable, this will be
3453 * passed in as finer grained information about where the focus is coming
3454 * from (in addition to direction). Will be <code>null</code> otherwise.
3455 */
3456 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003457 if (gainFocus) {
3458 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3459 }
3460
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003461 InputMethodManager imm = InputMethodManager.peekInstance();
3462 if (!gainFocus) {
3463 if (isPressed()) {
3464 setPressed(false);
3465 }
3466 if (imm != null && mAttachInfo != null
3467 && mAttachInfo.mHasWindowFocus) {
3468 imm.focusOut(this);
3469 }
Romain Guya2431d02009-04-30 16:30:00 -07003470 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003471 } else if (imm != null && mAttachInfo != null
3472 && mAttachInfo.mHasWindowFocus) {
3473 imm.focusIn(this);
3474 }
Romain Guy8506ab42009-06-11 17:35:47 -07003475
Romain Guy0fd89bf2011-01-26 15:41:30 -08003476 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 if (mOnFocusChangeListener != null) {
3478 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3479 }
Joe Malin32736f02011-01-19 16:14:20 -08003480
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003481 if (mAttachInfo != null) {
3482 mAttachInfo.mKeyDispatchState.reset(this);
3483 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003484 }
3485
3486 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003487 * {@inheritDoc}
3488 */
3489 public void sendAccessibilityEvent(int eventType) {
3490 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3491 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3492 }
3493 }
3494
3495 /**
3496 * {@inheritDoc}
3497 */
3498 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003499 if (!isShown()) {
3500 return;
3501 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003502
3503 // Populate these here since they are related to the View that
3504 // sends the event and should not be modified while dispatching
3505 // to descendants.
svetoslavganov75986cf2009-05-14 22:28:01 -07003506 event.setClassName(getClass().getName());
3507 event.setPackageName(getContext().getPackageName());
3508 event.setEnabled(isEnabled());
3509 event.setContentDescription(mContentDescription);
3510
3511 if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED && mAttachInfo != null) {
3512 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3513 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD, FOCUSABLES_ALL);
3514 event.setItemCount(focusablesTempList.size());
3515 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3516 focusablesTempList.clear();
3517 }
3518
3519 dispatchPopulateAccessibilityEvent(event);
3520
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003521 // In the beginning we called #isShown(), so we know that getParent() is not null.
3522 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003523 }
3524
3525 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003526 * Dispatches an {@link AccessibilityEvent} to the {@link View} children to be populated.
3527 * This method first calls {@link #onPopulateAccessibilityEvent(AccessibilityEvent)}
3528 * on this view allowing it to populate information about itself and also decide
3529 * whether to intercept the population i.e. to prevent its children from populating
3530 * the event.
svetoslavganov75986cf2009-05-14 22:28:01 -07003531 *
3532 * @param event The event.
3533 *
3534 * @return True if the event population was completed.
3535 */
3536 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003537 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003538 return false;
3539 }
3540
3541 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003542 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3543 * giving a chance to this View to populate the accessibility evnet with
3544 * information about itself.
3545 *
3546 * @param event The accessibility event which to populate.
3547 */
3548 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3549
3550 }
3551
3552 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003553 * Gets the {@link View} description. It briefly describes the view and is
3554 * primarily used for accessibility support. Set this property to enable
3555 * better accessibility support for your application. This is especially
3556 * true for views that do not have textual representation (For example,
3557 * ImageButton).
3558 *
3559 * @return The content descriptiopn.
3560 *
3561 * @attr ref android.R.styleable#View_contentDescription
3562 */
3563 public CharSequence getContentDescription() {
3564 return mContentDescription;
3565 }
3566
3567 /**
3568 * Sets the {@link View} description. It briefly describes the view and is
3569 * primarily used for accessibility support. Set this property to enable
3570 * better accessibility support for your application. This is especially
3571 * true for views that do not have textual representation (For example,
3572 * ImageButton).
3573 *
3574 * @param contentDescription The content description.
3575 *
3576 * @attr ref android.R.styleable#View_contentDescription
3577 */
3578 public void setContentDescription(CharSequence contentDescription) {
3579 mContentDescription = contentDescription;
3580 }
3581
3582 /**
Romain Guya2431d02009-04-30 16:30:00 -07003583 * Invoked whenever this view loses focus, either by losing window focus or by losing
3584 * focus within its window. This method can be used to clear any state tied to the
3585 * focus. For instance, if a button is held pressed with the trackball and the window
3586 * loses focus, this method can be used to cancel the press.
3587 *
3588 * Subclasses of View overriding this method should always call super.onFocusLost().
3589 *
3590 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07003591 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07003592 *
3593 * @hide pending API council approval
3594 */
3595 protected void onFocusLost() {
3596 resetPressedState();
3597 }
3598
3599 private void resetPressedState() {
3600 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
3601 return;
3602 }
3603
3604 if (isPressed()) {
3605 setPressed(false);
3606
3607 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05003608 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07003609 }
3610 }
3611 }
3612
3613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003614 * Returns true if this view has focus
3615 *
3616 * @return True if this view has focus, false otherwise.
3617 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003618 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003619 public boolean isFocused() {
3620 return (mPrivateFlags & FOCUSED) != 0;
3621 }
3622
3623 /**
3624 * Find the view in the hierarchy rooted at this view that currently has
3625 * focus.
3626 *
3627 * @return The view that currently has focus, or null if no focused view can
3628 * be found.
3629 */
3630 public View findFocus() {
3631 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
3632 }
3633
3634 /**
3635 * Change whether this view is one of the set of scrollable containers in
3636 * its window. This will be used to determine whether the window can
3637 * resize or must pan when a soft input area is open -- scrollable
3638 * containers allow the window to use resize mode since the container
3639 * will appropriately shrink.
3640 */
3641 public void setScrollContainer(boolean isScrollContainer) {
3642 if (isScrollContainer) {
3643 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
3644 mAttachInfo.mScrollContainers.add(this);
3645 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
3646 }
3647 mPrivateFlags |= SCROLL_CONTAINER;
3648 } else {
3649 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
3650 mAttachInfo.mScrollContainers.remove(this);
3651 }
3652 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
3653 }
3654 }
3655
3656 /**
3657 * Returns the quality of the drawing cache.
3658 *
3659 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3660 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3661 *
3662 * @see #setDrawingCacheQuality(int)
3663 * @see #setDrawingCacheEnabled(boolean)
3664 * @see #isDrawingCacheEnabled()
3665 *
3666 * @attr ref android.R.styleable#View_drawingCacheQuality
3667 */
3668 public int getDrawingCacheQuality() {
3669 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
3670 }
3671
3672 /**
3673 * Set the drawing cache quality of this view. This value is used only when the
3674 * drawing cache is enabled
3675 *
3676 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
3677 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
3678 *
3679 * @see #getDrawingCacheQuality()
3680 * @see #setDrawingCacheEnabled(boolean)
3681 * @see #isDrawingCacheEnabled()
3682 *
3683 * @attr ref android.R.styleable#View_drawingCacheQuality
3684 */
3685 public void setDrawingCacheQuality(int quality) {
3686 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
3687 }
3688
3689 /**
3690 * Returns whether the screen should remain on, corresponding to the current
3691 * value of {@link #KEEP_SCREEN_ON}.
3692 *
3693 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
3694 *
3695 * @see #setKeepScreenOn(boolean)
3696 *
3697 * @attr ref android.R.styleable#View_keepScreenOn
3698 */
3699 public boolean getKeepScreenOn() {
3700 return (mViewFlags & KEEP_SCREEN_ON) != 0;
3701 }
3702
3703 /**
3704 * Controls whether the screen should remain on, modifying the
3705 * value of {@link #KEEP_SCREEN_ON}.
3706 *
3707 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
3708 *
3709 * @see #getKeepScreenOn()
3710 *
3711 * @attr ref android.R.styleable#View_keepScreenOn
3712 */
3713 public void setKeepScreenOn(boolean keepScreenOn) {
3714 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
3715 }
3716
3717 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003718 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3719 * @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 -08003720 *
3721 * @attr ref android.R.styleable#View_nextFocusLeft
3722 */
3723 public int getNextFocusLeftId() {
3724 return mNextFocusLeftId;
3725 }
3726
3727 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003728 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
3729 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
3730 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 *
3732 * @attr ref android.R.styleable#View_nextFocusLeft
3733 */
3734 public void setNextFocusLeftId(int nextFocusLeftId) {
3735 mNextFocusLeftId = nextFocusLeftId;
3736 }
3737
3738 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003739 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3740 * @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 -08003741 *
3742 * @attr ref android.R.styleable#View_nextFocusRight
3743 */
3744 public int getNextFocusRightId() {
3745 return mNextFocusRightId;
3746 }
3747
3748 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003749 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
3750 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
3751 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 *
3753 * @attr ref android.R.styleable#View_nextFocusRight
3754 */
3755 public void setNextFocusRightId(int nextFocusRightId) {
3756 mNextFocusRightId = nextFocusRightId;
3757 }
3758
3759 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003760 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3761 * @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 -08003762 *
3763 * @attr ref android.R.styleable#View_nextFocusUp
3764 */
3765 public int getNextFocusUpId() {
3766 return mNextFocusUpId;
3767 }
3768
3769 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003770 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
3771 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
3772 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003773 *
3774 * @attr ref android.R.styleable#View_nextFocusUp
3775 */
3776 public void setNextFocusUpId(int nextFocusUpId) {
3777 mNextFocusUpId = nextFocusUpId;
3778 }
3779
3780 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003781 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3782 * @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 -08003783 *
3784 * @attr ref android.R.styleable#View_nextFocusDown
3785 */
3786 public int getNextFocusDownId() {
3787 return mNextFocusDownId;
3788 }
3789
3790 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003791 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
3792 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
3793 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003794 *
3795 * @attr ref android.R.styleable#View_nextFocusDown
3796 */
3797 public void setNextFocusDownId(int nextFocusDownId) {
3798 mNextFocusDownId = nextFocusDownId;
3799 }
3800
3801 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08003802 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3803 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
3804 *
3805 * @attr ref android.R.styleable#View_nextFocusForward
3806 */
3807 public int getNextFocusForwardId() {
3808 return mNextFocusForwardId;
3809 }
3810
3811 /**
3812 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
3813 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
3814 * decide automatically.
3815 *
3816 * @attr ref android.R.styleable#View_nextFocusForward
3817 */
3818 public void setNextFocusForwardId(int nextFocusForwardId) {
3819 mNextFocusForwardId = nextFocusForwardId;
3820 }
3821
3822 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003823 * Returns the visibility of this view and all of its ancestors
3824 *
3825 * @return True if this view and all of its ancestors are {@link #VISIBLE}
3826 */
3827 public boolean isShown() {
3828 View current = this;
3829 //noinspection ConstantConditions
3830 do {
3831 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
3832 return false;
3833 }
3834 ViewParent parent = current.mParent;
3835 if (parent == null) {
3836 return false; // We are not attached to the view root
3837 }
3838 if (!(parent instanceof View)) {
3839 return true;
3840 }
3841 current = (View) parent;
3842 } while (current != null);
3843
3844 return false;
3845 }
3846
3847 /**
3848 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
3849 * is set
3850 *
3851 * @param insets Insets for system windows
3852 *
3853 * @return True if this view applied the insets, false otherwise
3854 */
3855 protected boolean fitSystemWindows(Rect insets) {
3856 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
3857 mPaddingLeft = insets.left;
3858 mPaddingTop = insets.top;
3859 mPaddingRight = insets.right;
3860 mPaddingBottom = insets.bottom;
3861 requestLayout();
3862 return true;
3863 }
3864 return false;
3865 }
3866
3867 /**
3868 * Returns the visibility status for this view.
3869 *
3870 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3871 * @attr ref android.R.styleable#View_visibility
3872 */
3873 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07003874 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
3875 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
3876 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003877 })
3878 public int getVisibility() {
3879 return mViewFlags & VISIBILITY_MASK;
3880 }
3881
3882 /**
3883 * Set the enabled state of this view.
3884 *
3885 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
3886 * @attr ref android.R.styleable#View_visibility
3887 */
3888 @RemotableViewMethod
3889 public void setVisibility(int visibility) {
3890 setFlags(visibility, VISIBILITY_MASK);
3891 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
3892 }
3893
3894 /**
3895 * Returns the enabled status for this view. The interpretation of the
3896 * enabled state varies by subclass.
3897 *
3898 * @return True if this view is enabled, false otherwise.
3899 */
3900 @ViewDebug.ExportedProperty
3901 public boolean isEnabled() {
3902 return (mViewFlags & ENABLED_MASK) == ENABLED;
3903 }
3904
3905 /**
3906 * Set the enabled state of this view. The interpretation of the enabled
3907 * state varies by subclass.
3908 *
3909 * @param enabled True if this view is enabled, false otherwise.
3910 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08003911 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003912 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07003913 if (enabled == isEnabled()) return;
3914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003915 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
3916
3917 /*
3918 * The View most likely has to change its appearance, so refresh
3919 * the drawable state.
3920 */
3921 refreshDrawableState();
3922
3923 // Invalidate too, since the default behavior for views is to be
3924 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08003925 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003926 }
3927
3928 /**
3929 * Set whether this view can receive the focus.
3930 *
3931 * Setting this to false will also ensure that this view is not focusable
3932 * in touch mode.
3933 *
3934 * @param focusable If true, this view can receive the focus.
3935 *
3936 * @see #setFocusableInTouchMode(boolean)
3937 * @attr ref android.R.styleable#View_focusable
3938 */
3939 public void setFocusable(boolean focusable) {
3940 if (!focusable) {
3941 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
3942 }
3943 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
3944 }
3945
3946 /**
3947 * Set whether this view can receive focus while in touch mode.
3948 *
3949 * Setting this to true will also ensure that this view is focusable.
3950 *
3951 * @param focusableInTouchMode If true, this view can receive the focus while
3952 * in touch mode.
3953 *
3954 * @see #setFocusable(boolean)
3955 * @attr ref android.R.styleable#View_focusableInTouchMode
3956 */
3957 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
3958 // Focusable in touch mode should always be set before the focusable flag
3959 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
3960 // which, in touch mode, will not successfully request focus on this view
3961 // because the focusable in touch mode flag is not set
3962 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
3963 if (focusableInTouchMode) {
3964 setFlags(FOCUSABLE, FOCUSABLE_MASK);
3965 }
3966 }
3967
3968 /**
3969 * Set whether this view should have sound effects enabled for events such as
3970 * clicking and touching.
3971 *
3972 * <p>You may wish to disable sound effects for a view if you already play sounds,
3973 * for instance, a dial key that plays dtmf tones.
3974 *
3975 * @param soundEffectsEnabled whether sound effects are enabled for this view.
3976 * @see #isSoundEffectsEnabled()
3977 * @see #playSoundEffect(int)
3978 * @attr ref android.R.styleable#View_soundEffectsEnabled
3979 */
3980 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
3981 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
3982 }
3983
3984 /**
3985 * @return whether this view should have sound effects enabled for events such as
3986 * clicking and touching.
3987 *
3988 * @see #setSoundEffectsEnabled(boolean)
3989 * @see #playSoundEffect(int)
3990 * @attr ref android.R.styleable#View_soundEffectsEnabled
3991 */
3992 @ViewDebug.ExportedProperty
3993 public boolean isSoundEffectsEnabled() {
3994 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
3995 }
3996
3997 /**
3998 * Set whether this view should have haptic feedback for events such as
3999 * long presses.
4000 *
4001 * <p>You may wish to disable haptic feedback if your view already controls
4002 * its own haptic feedback.
4003 *
4004 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4005 * @see #isHapticFeedbackEnabled()
4006 * @see #performHapticFeedback(int)
4007 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4008 */
4009 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4010 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4011 }
4012
4013 /**
4014 * @return whether this view should have haptic feedback enabled for events
4015 * long presses.
4016 *
4017 * @see #setHapticFeedbackEnabled(boolean)
4018 * @see #performHapticFeedback(int)
4019 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4020 */
4021 @ViewDebug.ExportedProperty
4022 public boolean isHapticFeedbackEnabled() {
4023 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4024 }
4025
4026 /**
Cibu Johny7632cb92010-02-22 13:01:02 -08004027 * Returns the horizontal direction for this view.
4028 *
4029 * @return One of {@link #HORIZONTAL_DIRECTION_LTR},
4030 * {@link #HORIZONTAL_DIRECTION_RTL},
4031 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4032 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4033 * @attr ref android.R.styleable#View_horizontalDirection
4034 * @hide
4035 */
4036 @ViewDebug.ExportedProperty(mapping = {
4037 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LTR, to = "LTR"),
4038 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_RTL, to = "RTL"),
4039 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_INHERIT, to = "INHERIT"),
4040 @ViewDebug.IntToString(from = HORIZONTAL_DIRECTION_LOCALE, to = "LOCALE")
4041 })
4042 public int getHorizontalDirection() {
4043 return mViewFlags & HORIZONTAL_DIRECTION_MASK;
4044 }
4045
4046 /**
4047 * Set the horizontal direction for this view.
4048 *
4049 * @param horizontalDirection One of {@link #HORIZONTAL_DIRECTION_LTR},
4050 * {@link #HORIZONTAL_DIRECTION_RTL},
4051 * {@link #HORIZONTAL_DIRECTION_INHERIT} or
4052 * {@link #HORIZONTAL_DIRECTION_LOCALE}.
4053 * @attr ref android.R.styleable#View_horizontalDirection
4054 * @hide
4055 */
4056 @RemotableViewMethod
4057 public void setHorizontalDirection(int horizontalDirection) {
4058 setFlags(horizontalDirection, HORIZONTAL_DIRECTION_MASK);
4059 }
4060
4061 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004062 * If this view doesn't do any drawing on its own, set this flag to
4063 * allow further optimizations. By default, this flag is not set on
4064 * View, but could be set on some View subclasses such as ViewGroup.
4065 *
4066 * Typically, if you override {@link #onDraw} you should clear this flag.
4067 *
4068 * @param willNotDraw whether or not this View draw on its own
4069 */
4070 public void setWillNotDraw(boolean willNotDraw) {
4071 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4072 }
4073
4074 /**
4075 * Returns whether or not this View draws on its own.
4076 *
4077 * @return true if this view has nothing to draw, false otherwise
4078 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004079 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004080 public boolean willNotDraw() {
4081 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4082 }
4083
4084 /**
4085 * When a View's drawing cache is enabled, drawing is redirected to an
4086 * offscreen bitmap. Some views, like an ImageView, must be able to
4087 * bypass this mechanism if they already draw a single bitmap, to avoid
4088 * unnecessary usage of the memory.
4089 *
4090 * @param willNotCacheDrawing true if this view does not cache its
4091 * drawing, false otherwise
4092 */
4093 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4094 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4095 }
4096
4097 /**
4098 * Returns whether or not this View can cache its drawing or not.
4099 *
4100 * @return true if this view does not cache its drawing, false otherwise
4101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004102 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004103 public boolean willNotCacheDrawing() {
4104 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4105 }
4106
4107 /**
4108 * Indicates whether this view reacts to click events or not.
4109 *
4110 * @return true if the view is clickable, false otherwise
4111 *
4112 * @see #setClickable(boolean)
4113 * @attr ref android.R.styleable#View_clickable
4114 */
4115 @ViewDebug.ExportedProperty
4116 public boolean isClickable() {
4117 return (mViewFlags & CLICKABLE) == CLICKABLE;
4118 }
4119
4120 /**
4121 * Enables or disables click events for this view. When a view
4122 * is clickable it will change its state to "pressed" on every click.
4123 * Subclasses should set the view clickable to visually react to
4124 * user's clicks.
4125 *
4126 * @param clickable true to make the view clickable, false otherwise
4127 *
4128 * @see #isClickable()
4129 * @attr ref android.R.styleable#View_clickable
4130 */
4131 public void setClickable(boolean clickable) {
4132 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4133 }
4134
4135 /**
4136 * Indicates whether this view reacts to long click events or not.
4137 *
4138 * @return true if the view is long clickable, false otherwise
4139 *
4140 * @see #setLongClickable(boolean)
4141 * @attr ref android.R.styleable#View_longClickable
4142 */
4143 public boolean isLongClickable() {
4144 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4145 }
4146
4147 /**
4148 * Enables or disables long click events for this view. When a view is long
4149 * clickable it reacts to the user holding down the button for a longer
4150 * duration than a tap. This event can either launch the listener or a
4151 * context menu.
4152 *
4153 * @param longClickable true to make the view long clickable, false otherwise
4154 * @see #isLongClickable()
4155 * @attr ref android.R.styleable#View_longClickable
4156 */
4157 public void setLongClickable(boolean longClickable) {
4158 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4159 }
4160
4161 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004162 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004163 *
4164 * @see #isClickable()
4165 * @see #setClickable(boolean)
4166 *
4167 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4168 * the View's internal state from a previously set "pressed" state.
4169 */
4170 public void setPressed(boolean pressed) {
4171 if (pressed) {
4172 mPrivateFlags |= PRESSED;
4173 } else {
4174 mPrivateFlags &= ~PRESSED;
4175 }
4176 refreshDrawableState();
4177 dispatchSetPressed(pressed);
4178 }
4179
4180 /**
4181 * Dispatch setPressed to all of this View's children.
4182 *
4183 * @see #setPressed(boolean)
4184 *
4185 * @param pressed The new pressed state
4186 */
4187 protected void dispatchSetPressed(boolean pressed) {
4188 }
4189
4190 /**
4191 * Indicates whether the view is currently in pressed state. Unless
4192 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4193 * the pressed state.
4194 *
4195 * @see #setPressed
4196 * @see #isClickable()
4197 * @see #setClickable(boolean)
4198 *
4199 * @return true if the view is currently pressed, false otherwise
4200 */
4201 public boolean isPressed() {
4202 return (mPrivateFlags & PRESSED) == PRESSED;
4203 }
4204
4205 /**
4206 * Indicates whether this view will save its state (that is,
4207 * whether its {@link #onSaveInstanceState} method will be called).
4208 *
4209 * @return Returns true if the view state saving is enabled, else false.
4210 *
4211 * @see #setSaveEnabled(boolean)
4212 * @attr ref android.R.styleable#View_saveEnabled
4213 */
4214 public boolean isSaveEnabled() {
4215 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4216 }
4217
4218 /**
4219 * Controls whether the saving of this view's state is
4220 * enabled (that is, whether its {@link #onSaveInstanceState} method
4221 * will be called). Note that even if freezing is enabled, the
4222 * view still must have an id assigned to it (via {@link #setId setId()})
4223 * for its state to be saved. This flag can only disable the
4224 * saving of this view; any child views may still have their state saved.
4225 *
4226 * @param enabled Set to false to <em>disable</em> state saving, or true
4227 * (the default) to allow it.
4228 *
4229 * @see #isSaveEnabled()
4230 * @see #setId(int)
4231 * @see #onSaveInstanceState()
4232 * @attr ref android.R.styleable#View_saveEnabled
4233 */
4234 public void setSaveEnabled(boolean enabled) {
4235 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4236 }
4237
Jeff Brown85a31762010-09-01 17:01:00 -07004238 /**
4239 * Gets whether the framework should discard touches when the view's
4240 * window is obscured by another visible window.
4241 * Refer to the {@link View} security documentation for more details.
4242 *
4243 * @return True if touch filtering is enabled.
4244 *
4245 * @see #setFilterTouchesWhenObscured(boolean)
4246 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4247 */
4248 @ViewDebug.ExportedProperty
4249 public boolean getFilterTouchesWhenObscured() {
4250 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4251 }
4252
4253 /**
4254 * Sets whether the framework should discard touches when the view's
4255 * window is obscured by another visible window.
4256 * Refer to the {@link View} security documentation for more details.
4257 *
4258 * @param enabled True if touch filtering should be enabled.
4259 *
4260 * @see #getFilterTouchesWhenObscured
4261 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4262 */
4263 public void setFilterTouchesWhenObscured(boolean enabled) {
4264 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4265 FILTER_TOUCHES_WHEN_OBSCURED);
4266 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004267
4268 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004269 * Indicates whether the entire hierarchy under this view will save its
4270 * state when a state saving traversal occurs from its parent. The default
4271 * is true; if false, these views will not be saved unless
4272 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4273 *
4274 * @return Returns true if the view state saving from parent is enabled, else false.
4275 *
4276 * @see #setSaveFromParentEnabled(boolean)
4277 */
4278 public boolean isSaveFromParentEnabled() {
4279 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4280 }
4281
4282 /**
4283 * Controls whether the entire hierarchy under this view will save its
4284 * state when a state saving traversal occurs from its parent. The default
4285 * is true; if false, these views will not be saved unless
4286 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4287 *
4288 * @param enabled Set to false to <em>disable</em> state saving, or true
4289 * (the default) to allow it.
4290 *
4291 * @see #isSaveFromParentEnabled()
4292 * @see #setId(int)
4293 * @see #onSaveInstanceState()
4294 */
4295 public void setSaveFromParentEnabled(boolean enabled) {
4296 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4297 }
4298
4299
4300 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004301 * Returns whether this View is able to take focus.
4302 *
4303 * @return True if this view can take focus, or false otherwise.
4304 * @attr ref android.R.styleable#View_focusable
4305 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004306 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004307 public final boolean isFocusable() {
4308 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4309 }
4310
4311 /**
4312 * When a view is focusable, it may not want to take focus when in touch mode.
4313 * For example, a button would like focus when the user is navigating via a D-pad
4314 * so that the user can click on it, but once the user starts touching the screen,
4315 * the button shouldn't take focus
4316 * @return Whether the view is focusable in touch mode.
4317 * @attr ref android.R.styleable#View_focusableInTouchMode
4318 */
4319 @ViewDebug.ExportedProperty
4320 public final boolean isFocusableInTouchMode() {
4321 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4322 }
4323
4324 /**
4325 * Find the nearest view in the specified direction that can take focus.
4326 * This does not actually give focus to that view.
4327 *
4328 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4329 *
4330 * @return The nearest focusable in the specified direction, or null if none
4331 * can be found.
4332 */
4333 public View focusSearch(int direction) {
4334 if (mParent != null) {
4335 return mParent.focusSearch(this, direction);
4336 } else {
4337 return null;
4338 }
4339 }
4340
4341 /**
4342 * This method is the last chance for the focused view and its ancestors to
4343 * respond to an arrow key. This is called when the focused view did not
4344 * consume the key internally, nor could the view system find a new view in
4345 * the requested direction to give focus to.
4346 *
4347 * @param focused The currently focused view.
4348 * @param direction The direction focus wants to move. One of FOCUS_UP,
4349 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4350 * @return True if the this view consumed this unhandled move.
4351 */
4352 public boolean dispatchUnhandledMove(View focused, int direction) {
4353 return false;
4354 }
4355
4356 /**
4357 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004358 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004359 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004360 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4361 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004362 * @return The user specified next view, or null if there is none.
4363 */
4364 View findUserSetNextFocus(View root, int direction) {
4365 switch (direction) {
4366 case FOCUS_LEFT:
4367 if (mNextFocusLeftId == View.NO_ID) return null;
4368 return findViewShouldExist(root, mNextFocusLeftId);
4369 case FOCUS_RIGHT:
4370 if (mNextFocusRightId == View.NO_ID) return null;
4371 return findViewShouldExist(root, mNextFocusRightId);
4372 case FOCUS_UP:
4373 if (mNextFocusUpId == View.NO_ID) return null;
4374 return findViewShouldExist(root, mNextFocusUpId);
4375 case FOCUS_DOWN:
4376 if (mNextFocusDownId == View.NO_ID) return null;
4377 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004378 case FOCUS_FORWARD:
4379 if (mNextFocusForwardId == View.NO_ID) return null;
4380 return findViewShouldExist(root, mNextFocusForwardId);
4381 case FOCUS_BACKWARD: {
4382 final int id = mID;
4383 return root.findViewByPredicate(new Predicate<View>() {
4384 @Override
4385 public boolean apply(View t) {
4386 return t.mNextFocusForwardId == id;
4387 }
4388 });
4389 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004390 }
4391 return null;
4392 }
4393
4394 private static View findViewShouldExist(View root, int childViewId) {
4395 View result = root.findViewById(childViewId);
4396 if (result == null) {
4397 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4398 + "by user for id " + childViewId);
4399 }
4400 return result;
4401 }
4402
4403 /**
4404 * Find and return all focusable views that are descendants of this view,
4405 * possibly including this view if it is focusable itself.
4406 *
4407 * @param direction The direction of the focus
4408 * @return A list of focusable views
4409 */
4410 public ArrayList<View> getFocusables(int direction) {
4411 ArrayList<View> result = new ArrayList<View>(24);
4412 addFocusables(result, direction);
4413 return result;
4414 }
4415
4416 /**
4417 * Add any focusable views that are descendants of this view (possibly
4418 * including this view if it is focusable itself) to views. If we are in touch mode,
4419 * only add views that are also focusable in touch mode.
4420 *
4421 * @param views Focusable views found so far
4422 * @param direction The direction of the focus
4423 */
4424 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004425 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4426 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004427
svetoslavganov75986cf2009-05-14 22:28:01 -07004428 /**
4429 * Adds any focusable views that are descendants of this view (possibly
4430 * including this view if it is focusable itself) to views. This method
4431 * adds all focusable views regardless if we are in touch mode or
4432 * only views focusable in touch mode if we are in touch mode depending on
4433 * the focusable mode paramater.
4434 *
4435 * @param views Focusable views found so far or null if all we are interested is
4436 * the number of focusables.
4437 * @param direction The direction of the focus.
4438 * @param focusableMode The type of focusables to be added.
4439 *
4440 * @see #FOCUSABLES_ALL
4441 * @see #FOCUSABLES_TOUCH_MODE
4442 */
4443 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4444 if (!isFocusable()) {
4445 return;
4446 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004447
svetoslavganov75986cf2009-05-14 22:28:01 -07004448 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4449 isInTouchMode() && !isFocusableInTouchMode()) {
4450 return;
4451 }
4452
4453 if (views != null) {
4454 views.add(this);
4455 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004456 }
4457
4458 /**
4459 * Find and return all touchable views that are descendants of this view,
4460 * possibly including this view if it is touchable itself.
4461 *
4462 * @return A list of touchable views
4463 */
4464 public ArrayList<View> getTouchables() {
4465 ArrayList<View> result = new ArrayList<View>();
4466 addTouchables(result);
4467 return result;
4468 }
4469
4470 /**
4471 * Add any touchable views that are descendants of this view (possibly
4472 * including this view if it is touchable itself) to views.
4473 *
4474 * @param views Touchable views found so far
4475 */
4476 public void addTouchables(ArrayList<View> views) {
4477 final int viewFlags = mViewFlags;
4478
4479 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4480 && (viewFlags & ENABLED_MASK) == ENABLED) {
4481 views.add(this);
4482 }
4483 }
4484
4485 /**
4486 * Call this to try to give focus to a specific view or to one of its
4487 * descendants.
4488 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004489 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4490 * false), or if it is focusable and it is not focusable in touch mode
4491 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004492 *
4493 * See also {@link #focusSearch}, which is what you call to say that you
4494 * have focus, and you want your parent to look for the next one.
4495 *
4496 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4497 * {@link #FOCUS_DOWN} and <code>null</code>.
4498 *
4499 * @return Whether this view or one of its descendants actually took focus.
4500 */
4501 public final boolean requestFocus() {
4502 return requestFocus(View.FOCUS_DOWN);
4503 }
4504
4505
4506 /**
4507 * Call this to try to give focus to a specific view or to one of its
4508 * descendants and give it a hint about what direction focus is heading.
4509 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004510 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4511 * false), or if it is focusable and it is not focusable in touch mode
4512 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004513 *
4514 * See also {@link #focusSearch}, which is what you call to say that you
4515 * have focus, and you want your parent to look for the next one.
4516 *
4517 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4518 * <code>null</code> set for the previously focused rectangle.
4519 *
4520 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4521 * @return Whether this view or one of its descendants actually took focus.
4522 */
4523 public final boolean requestFocus(int direction) {
4524 return requestFocus(direction, null);
4525 }
4526
4527 /**
4528 * Call this to try to give focus to a specific view or to one of its descendants
4529 * and give it hints about the direction and a specific rectangle that the focus
4530 * is coming from. The rectangle can help give larger views a finer grained hint
4531 * about where focus is coming from, and therefore, where to show selection, or
4532 * forward focus change internally.
4533 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004534 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4535 * false), or if it is focusable and it is not focusable in touch mode
4536 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004537 *
4538 * A View will not take focus if it is not visible.
4539 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004540 * A View will not take focus if one of its parents has
4541 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
4542 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004543 *
4544 * See also {@link #focusSearch}, which is what you call to say that you
4545 * have focus, and you want your parent to look for the next one.
4546 *
4547 * You may wish to override this method if your custom {@link View} has an internal
4548 * {@link View} that it wishes to forward the request to.
4549 *
4550 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4551 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
4552 * to give a finer grained hint about where focus is coming from. May be null
4553 * if there is no hint.
4554 * @return Whether this view or one of its descendants actually took focus.
4555 */
4556 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
4557 // need to be focusable
4558 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
4559 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4560 return false;
4561 }
4562
4563 // need to be focusable in touch mode if in touch mode
4564 if (isInTouchMode() &&
4565 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
4566 return false;
4567 }
4568
4569 // need to not have any parents blocking us
4570 if (hasAncestorThatBlocksDescendantFocus()) {
4571 return false;
4572 }
4573
4574 handleFocusGainInternal(direction, previouslyFocusedRect);
4575 return true;
4576 }
4577
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004578 /** Gets the ViewAncestor, or null if not attached. */
4579 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07004580 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004581 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07004582 }
4583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004584 /**
4585 * Call this to try to give focus to a specific view or to one of its descendants. This is a
4586 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
4587 * touch mode to request focus when they are touched.
4588 *
4589 * @return Whether this view or one of its descendants actually took focus.
4590 *
4591 * @see #isInTouchMode()
4592 *
4593 */
4594 public final boolean requestFocusFromTouch() {
4595 // Leave touch mode if we need to
4596 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07004597 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07004598 if (viewRoot != null) {
4599 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 }
4601 }
4602 return requestFocus(View.FOCUS_DOWN);
4603 }
4604
4605 /**
4606 * @return Whether any ancestor of this view blocks descendant focus.
4607 */
4608 private boolean hasAncestorThatBlocksDescendantFocus() {
4609 ViewParent ancestor = mParent;
4610 while (ancestor instanceof ViewGroup) {
4611 final ViewGroup vgAncestor = (ViewGroup) ancestor;
4612 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
4613 return true;
4614 } else {
4615 ancestor = vgAncestor.getParent();
4616 }
4617 }
4618 return false;
4619 }
4620
4621 /**
Romain Guya440b002010-02-24 15:57:54 -08004622 * @hide
4623 */
4624 public void dispatchStartTemporaryDetach() {
4625 onStartTemporaryDetach();
4626 }
4627
4628 /**
4629 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004630 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
4631 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08004632 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004633 */
4634 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08004635 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08004636 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08004637 }
4638
4639 /**
4640 * @hide
4641 */
4642 public void dispatchFinishTemporaryDetach() {
4643 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004644 }
Romain Guy8506ab42009-06-11 17:35:47 -07004645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004646 /**
4647 * Called after {@link #onStartTemporaryDetach} when the container is done
4648 * changing the view.
4649 */
4650 public void onFinishTemporaryDetach() {
4651 }
Romain Guy8506ab42009-06-11 17:35:47 -07004652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004653 /**
4654 * capture information of this view for later analysis: developement only
4655 * check dynamic switch to make sure we only dump view
4656 * when ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW) is set
4657 */
4658 private static void captureViewInfo(String subTag, View v) {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004659 if (v == null || SystemProperties.getInt(ViewDebug.SYSTEM_PROPERTY_CAPTURE_VIEW, 0) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004660 return;
4661 }
4662 ViewDebug.dumpCapturedView(subTag, v);
4663 }
4664
4665 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004666 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
4667 * for this view's window. Returns null if the view is not currently attached
4668 * to the window. Normally you will not need to use this directly, but
4669 * just use the standard high-level event callbacks like {@link #onKeyDown}.
4670 */
4671 public KeyEvent.DispatcherState getKeyDispatcherState() {
4672 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
4673 }
Joe Malin32736f02011-01-19 16:14:20 -08004674
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07004675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004676 * Dispatch a key event before it is processed by any input method
4677 * associated with the view hierarchy. This can be used to intercept
4678 * key events in special situations before the IME consumes them; a
4679 * typical example would be handling the BACK key to update the application's
4680 * UI instead of allowing the IME to see it and close itself.
4681 *
4682 * @param event The key event to be dispatched.
4683 * @return True if the event was handled, false otherwise.
4684 */
4685 public boolean dispatchKeyEventPreIme(KeyEvent event) {
4686 return onKeyPreIme(event.getKeyCode(), event);
4687 }
4688
4689 /**
4690 * Dispatch a key event to the next view on the focus path. This path runs
4691 * from the top of the view tree down to the currently focused view. If this
4692 * view has focus, it will dispatch to itself. Otherwise it will dispatch
4693 * the next node down the focus path. This method also fires any key
4694 * listeners.
4695 *
4696 * @param event The key event to be dispatched.
4697 * @return True if the event was handled, false otherwise.
4698 */
4699 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004700 if (mInputEventConsistencyVerifier != null) {
4701 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
4702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004703
Romain Guyf607bdc2010-09-10 19:20:06 -07004704 //noinspection SimplifiableIfStatement,deprecation
Joe Onorato43a17652011-04-06 19:22:23 -07004705 if (false) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004706 captureViewInfo("captureViewKeyEvent", this);
4707 }
4708
Jeff Brown21bc5c92011-02-28 18:27:14 -08004709 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07004710 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004711 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4712 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
4713 return true;
4714 }
4715
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004716 if (event.dispatch(this, mAttachInfo != null
4717 ? mAttachInfo.mKeyDispatchState : null, this)) {
4718 return true;
4719 }
4720
4721 if (mInputEventConsistencyVerifier != null) {
4722 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4723 }
4724 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004725 }
4726
4727 /**
4728 * Dispatches a key shortcut event.
4729 *
4730 * @param event The key event to be dispatched.
4731 * @return True if the event was handled by the view, false otherwise.
4732 */
4733 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
4734 return onKeyShortcut(event.getKeyCode(), event);
4735 }
4736
4737 /**
4738 * Pass the touch screen motion event down to the target view, or this
4739 * view if it is the target.
4740 *
4741 * @param event The motion event to be dispatched.
4742 * @return True if the event was handled by the view, false otherwise.
4743 */
4744 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004745 if (mInputEventConsistencyVerifier != null) {
4746 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
4747 }
4748
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004749 if (onFilterTouchEventForSecurity(event)) {
4750 //noinspection SimplifiableIfStatement
4751 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
4752 mOnTouchListener.onTouch(this, event)) {
4753 return true;
4754 }
4755
4756 if (onTouchEvent(event)) {
4757 return true;
4758 }
Jeff Brown85a31762010-09-01 17:01:00 -07004759 }
4760
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004761 if (mInputEventConsistencyVerifier != null) {
4762 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004763 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004764 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004765 }
4766
4767 /**
Jeff Brown85a31762010-09-01 17:01:00 -07004768 * Filter the touch event to apply security policies.
4769 *
4770 * @param event The motion event to be filtered.
4771 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08004772 *
Jeff Brown85a31762010-09-01 17:01:00 -07004773 * @see #getFilterTouchesWhenObscured
4774 */
4775 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07004776 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07004777 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
4778 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
4779 // Window is obscured, drop this touch.
4780 return false;
4781 }
4782 return true;
4783 }
4784
4785 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004786 * Pass a trackball motion event down to the focused view.
4787 *
4788 * @param event The motion event to be dispatched.
4789 * @return True if the event was handled by the view, false otherwise.
4790 */
4791 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004792 if (mInputEventConsistencyVerifier != null) {
4793 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
4794 }
4795
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004796 //Log.i("view", "view=" + this + ", " + event.toString());
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004797 if (onTrackballEvent(event)) {
4798 return true;
4799 }
4800
4801 if (mInputEventConsistencyVerifier != null) {
4802 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4803 }
4804 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004805 }
4806
4807 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004808 * Dispatch a generic motion event.
4809 * <p>
4810 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
4811 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08004812 * delivered to the focused view. Hover events are handled specially and are delivered
4813 * to {@link #onHoverEvent}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08004814 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08004815 *
4816 * @param event The motion event to be dispatched.
4817 * @return True if the event was handled by the view, false otherwise.
4818 */
4819 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08004820 if (mInputEventConsistencyVerifier != null) {
4821 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
4822 }
4823
Jeff Browna032cc02011-03-07 16:56:21 -08004824 final int source = event.getSource();
4825 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
4826 final int action = event.getAction();
4827 if (action == MotionEvent.ACTION_HOVER_ENTER
4828 || action == MotionEvent.ACTION_HOVER_MOVE
4829 || action == MotionEvent.ACTION_HOVER_EXIT) {
4830 if (dispatchHoverEvent(event)) {
4831 return true;
4832 }
4833 } else if (dispatchGenericPointerEvent(event)) {
4834 return true;
4835 }
4836 } else if (dispatchGenericFocusedEvent(event)) {
4837 return true;
4838 }
4839
Romain Guy7b5b6ab2011-03-14 18:05:08 -07004840 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08004841 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
4842 && mOnGenericMotionListener.onGenericMotion(this, event)) {
4843 return true;
4844 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07004845
4846 if (onGenericMotionEvent(event)) {
4847 return true;
4848 }
4849
4850 if (mInputEventConsistencyVerifier != null) {
4851 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
4852 }
4853 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08004854 }
4855
4856 /**
Jeff Browna032cc02011-03-07 16:56:21 -08004857 * Dispatch a hover event.
4858 * <p>
4859 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4860 * </p>
4861 *
4862 * @param event The motion event to be dispatched.
4863 * @return True if the event was handled by the view, false otherwise.
4864 * @hide
4865 */
4866 protected boolean dispatchHoverEvent(MotionEvent event) {
4867 return onHoverEvent(event);
4868 }
4869
4870 /**
4871 * Dispatch a generic motion event to the view under the first pointer.
4872 * <p>
4873 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4874 * </p>
4875 *
4876 * @param event The motion event to be dispatched.
4877 * @return True if the event was handled by the view, false otherwise.
4878 * @hide
4879 */
4880 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
4881 return false;
4882 }
4883
4884 /**
4885 * Dispatch a generic motion event to the currently focused view.
4886 * <p>
4887 * Do not call this method directly. Call {@link #dispatchGenericMotionEvent} instead.
4888 * </p>
4889 *
4890 * @param event The motion event to be dispatched.
4891 * @return True if the event was handled by the view, false otherwise.
4892 * @hide
4893 */
4894 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
4895 return false;
4896 }
4897
4898 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08004899 * Dispatch a pointer event.
4900 * <p>
4901 * Dispatches touch related pointer events to {@link #onTouchEvent} and all
4902 * other events to {@link #onGenericMotionEvent}. This separation of concerns
4903 * reinforces the invariant that {@link #onTouchEvent} is really about touches
4904 * and should not be expected to handle other pointing device features.
4905 * </p>
4906 *
4907 * @param event The motion event to be dispatched.
4908 * @return True if the event was handled by the view, false otherwise.
4909 * @hide
4910 */
4911 public final boolean dispatchPointerEvent(MotionEvent event) {
4912 if (event.isTouchEvent()) {
4913 return dispatchTouchEvent(event);
4914 } else {
4915 return dispatchGenericMotionEvent(event);
4916 }
4917 }
4918
4919 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004920 * Called when the window containing this view gains or loses window focus.
4921 * ViewGroups should override to route to their children.
4922 *
4923 * @param hasFocus True if the window containing this view now has focus,
4924 * false otherwise.
4925 */
4926 public void dispatchWindowFocusChanged(boolean hasFocus) {
4927 onWindowFocusChanged(hasFocus);
4928 }
4929
4930 /**
4931 * Called when the window containing this view gains or loses focus. Note
4932 * that this is separate from view focus: to receive key events, both
4933 * your view and its window must have focus. If a window is displayed
4934 * on top of yours that takes input focus, then your own window will lose
4935 * focus but the view focus will remain unchanged.
4936 *
4937 * @param hasWindowFocus True if the window containing this view now has
4938 * focus, false otherwise.
4939 */
4940 public void onWindowFocusChanged(boolean hasWindowFocus) {
4941 InputMethodManager imm = InputMethodManager.peekInstance();
4942 if (!hasWindowFocus) {
4943 if (isPressed()) {
4944 setPressed(false);
4945 }
4946 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4947 imm.focusOut(this);
4948 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05004949 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08004950 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004951 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004952 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
4953 imm.focusIn(this);
4954 }
4955 refreshDrawableState();
4956 }
4957
4958 /**
4959 * Returns true if this view is in a window that currently has window focus.
4960 * Note that this is not the same as the view itself having focus.
4961 *
4962 * @return True if this view is in a window that currently has window focus.
4963 */
4964 public boolean hasWindowFocus() {
4965 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
4966 }
4967
4968 /**
Adam Powell326d8082009-12-09 15:10:07 -08004969 * Dispatch a view visibility change down the view hierarchy.
4970 * ViewGroups should override to route to their children.
4971 * @param changedView The view whose visibility changed. Could be 'this' or
4972 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004973 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4974 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004975 */
4976 protected void dispatchVisibilityChanged(View changedView, int visibility) {
4977 onVisibilityChanged(changedView, visibility);
4978 }
4979
4980 /**
4981 * Called when the visibility of the view or an ancestor of the view is changed.
4982 * @param changedView The view whose visibility changed. Could be 'this' or
4983 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08004984 * @param visibility The new visibility of changedView: {@link #VISIBLE},
4985 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08004986 */
4987 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07004988 if (visibility == VISIBLE) {
4989 if (mAttachInfo != null) {
4990 initialAwakenScrollBars();
4991 } else {
4992 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
4993 }
4994 }
Adam Powell326d8082009-12-09 15:10:07 -08004995 }
4996
4997 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08004998 * Dispatch a hint about whether this view is displayed. For instance, when
4999 * a View moves out of the screen, it might receives a display hint indicating
5000 * the view is not displayed. Applications should not <em>rely</em> on this hint
5001 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005002 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005003 * @param hint A hint about whether or not this view is displayed:
5004 * {@link #VISIBLE} or {@link #INVISIBLE}.
5005 */
5006 public void dispatchDisplayHint(int hint) {
5007 onDisplayHint(hint);
5008 }
5009
5010 /**
5011 * Gives this view a hint about whether is displayed or not. For instance, when
5012 * a View moves out of the screen, it might receives a display hint indicating
5013 * the view is not displayed. Applications should not <em>rely</em> on this hint
5014 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005015 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005016 * @param hint A hint about whether or not this view is displayed:
5017 * {@link #VISIBLE} or {@link #INVISIBLE}.
5018 */
5019 protected void onDisplayHint(int hint) {
5020 }
5021
5022 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005023 * Dispatch a window visibility change down the view hierarchy.
5024 * ViewGroups should override to route to their children.
5025 *
5026 * @param visibility The new visibility of the window.
5027 *
5028 * @see #onWindowVisibilityChanged
5029 */
5030 public void dispatchWindowVisibilityChanged(int visibility) {
5031 onWindowVisibilityChanged(visibility);
5032 }
5033
5034 /**
5035 * Called when the window containing has change its visibility
5036 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5037 * that this tells you whether or not your window is being made visible
5038 * to the window manager; this does <em>not</em> tell you whether or not
5039 * your window is obscured by other windows on the screen, even if it
5040 * is itself visible.
5041 *
5042 * @param visibility The new visibility of the window.
5043 */
5044 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005045 if (visibility == VISIBLE) {
5046 initialAwakenScrollBars();
5047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 }
5049
5050 /**
5051 * Returns the current visibility of the window this view is attached to
5052 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5053 *
5054 * @return Returns the current visibility of the view's window.
5055 */
5056 public int getWindowVisibility() {
5057 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5058 }
5059
5060 /**
5061 * Retrieve the overall visible display size in which the window this view is
5062 * attached to has been positioned in. This takes into account screen
5063 * decorations above the window, for both cases where the window itself
5064 * is being position inside of them or the window is being placed under
5065 * then and covered insets are used for the window to position its content
5066 * inside. In effect, this tells you the available area where content can
5067 * be placed and remain visible to users.
5068 *
5069 * <p>This function requires an IPC back to the window manager to retrieve
5070 * the requested information, so should not be used in performance critical
5071 * code like drawing.
5072 *
5073 * @param outRect Filled in with the visible display frame. If the view
5074 * is not attached to a window, this is simply the raw display size.
5075 */
5076 public void getWindowVisibleDisplayFrame(Rect outRect) {
5077 if (mAttachInfo != null) {
5078 try {
5079 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5080 } catch (RemoteException e) {
5081 return;
5082 }
5083 // XXX This is really broken, and probably all needs to be done
5084 // in the window manager, and we need to know more about whether
5085 // we want the area behind or in front of the IME.
5086 final Rect insets = mAttachInfo.mVisibleInsets;
5087 outRect.left += insets.left;
5088 outRect.top += insets.top;
5089 outRect.right -= insets.right;
5090 outRect.bottom -= insets.bottom;
5091 return;
5092 }
5093 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005094 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005095 }
5096
5097 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005098 * Dispatch a notification about a resource configuration change down
5099 * the view hierarchy.
5100 * ViewGroups should override to route to their children.
5101 *
5102 * @param newConfig The new resource configuration.
5103 *
5104 * @see #onConfigurationChanged
5105 */
5106 public void dispatchConfigurationChanged(Configuration newConfig) {
5107 onConfigurationChanged(newConfig);
5108 }
5109
5110 /**
5111 * Called when the current configuration of the resources being used
5112 * by the application have changed. You can use this to decide when
5113 * to reload resources that can changed based on orientation and other
5114 * configuration characterstics. You only need to use this if you are
5115 * not relying on the normal {@link android.app.Activity} mechanism of
5116 * recreating the activity instance upon a configuration change.
5117 *
5118 * @param newConfig The new resource configuration.
5119 */
5120 protected void onConfigurationChanged(Configuration newConfig) {
5121 }
5122
5123 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005124 * Private function to aggregate all per-view attributes in to the view
5125 * root.
5126 */
5127 void dispatchCollectViewAttributes(int visibility) {
5128 performCollectViewAttributes(visibility);
5129 }
5130
5131 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005132 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005133 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5134 mAttachInfo.mKeepScreenOn = true;
5135 }
5136 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5137 if (mOnSystemUiVisibilityChangeListener != null) {
5138 mAttachInfo.mHasSystemUiListeners = true;
5139 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005140 }
5141 }
5142
5143 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005144 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005145 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005146 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5147 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005148 ai.mRecomputeGlobalAttributes = true;
5149 }
5150 }
5151 }
5152
5153 /**
5154 * Returns whether the device is currently in touch mode. Touch mode is entered
5155 * once the user begins interacting with the device by touch, and affects various
5156 * things like whether focus is always visible to the user.
5157 *
5158 * @return Whether the device is in touch mode.
5159 */
5160 @ViewDebug.ExportedProperty
5161 public boolean isInTouchMode() {
5162 if (mAttachInfo != null) {
5163 return mAttachInfo.mInTouchMode;
5164 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005165 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005166 }
5167 }
5168
5169 /**
5170 * Returns the context the view is running in, through which it can
5171 * access the current theme, resources, etc.
5172 *
5173 * @return The view's Context.
5174 */
5175 @ViewDebug.CapturedViewProperty
5176 public final Context getContext() {
5177 return mContext;
5178 }
5179
5180 /**
5181 * Handle a key event before it is processed by any input method
5182 * associated with the view hierarchy. This can be used to intercept
5183 * key events in special situations before the IME consumes them; a
5184 * typical example would be handling the BACK key to update the application's
5185 * UI instead of allowing the IME to see it and close itself.
5186 *
5187 * @param keyCode The value in event.getKeyCode().
5188 * @param event Description of the key event.
5189 * @return If you handled the event, return true. If you want to allow the
5190 * event to be handled by the next receiver, return false.
5191 */
5192 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5193 return false;
5194 }
5195
5196 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005197 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5198 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005199 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5200 * is released, if the view is enabled and clickable.
5201 *
5202 * @param keyCode A key code that represents the button pressed, from
5203 * {@link android.view.KeyEvent}.
5204 * @param event The KeyEvent object that defines the button action.
5205 */
5206 public boolean onKeyDown(int keyCode, KeyEvent event) {
5207 boolean result = false;
5208
5209 switch (keyCode) {
5210 case KeyEvent.KEYCODE_DPAD_CENTER:
5211 case KeyEvent.KEYCODE_ENTER: {
5212 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5213 return true;
5214 }
5215 // Long clickable items don't necessarily have to be clickable
5216 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5217 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5218 (event.getRepeatCount() == 0)) {
5219 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005220 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005221 return true;
5222 }
5223 break;
5224 }
5225 }
5226 return result;
5227 }
5228
5229 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005230 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5231 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5232 * the event).
5233 */
5234 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5235 return false;
5236 }
5237
5238 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005239 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5240 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5242 * {@link KeyEvent#KEYCODE_ENTER} is released.
5243 *
5244 * @param keyCode A key code that represents the button pressed, from
5245 * {@link android.view.KeyEvent}.
5246 * @param event The KeyEvent object that defines the button action.
5247 */
5248 public boolean onKeyUp(int keyCode, KeyEvent event) {
5249 boolean result = false;
5250
5251 switch (keyCode) {
5252 case KeyEvent.KEYCODE_DPAD_CENTER:
5253 case KeyEvent.KEYCODE_ENTER: {
5254 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5255 return true;
5256 }
5257 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5258 setPressed(false);
5259
5260 if (!mHasPerformedLongPress) {
5261 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005262 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005263
5264 result = performClick();
5265 }
5266 }
5267 break;
5268 }
5269 }
5270 return result;
5271 }
5272
5273 /**
5274 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5275 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5276 * the event).
5277 *
5278 * @param keyCode A key code that represents the button pressed, from
5279 * {@link android.view.KeyEvent}.
5280 * @param repeatCount The number of times the action was made.
5281 * @param event The KeyEvent object that defines the button action.
5282 */
5283 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5284 return false;
5285 }
5286
5287 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005288 * Called on the focused view when a key shortcut event is not handled.
5289 * Override this method to implement local key shortcuts for the View.
5290 * Key shortcuts can also be implemented by setting the
5291 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005292 *
5293 * @param keyCode The value in event.getKeyCode().
5294 * @param event Description of the key event.
5295 * @return If you handled the event, return true. If you want to allow the
5296 * event to be handled by the next receiver, return false.
5297 */
5298 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5299 return false;
5300 }
5301
5302 /**
5303 * Check whether the called view is a text editor, in which case it
5304 * would make sense to automatically display a soft input window for
5305 * it. Subclasses should override this if they implement
5306 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005307 * a call on that method would return a non-null InputConnection, and
5308 * they are really a first-class editor that the user would normally
5309 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005310 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005311 * <p>The default implementation always returns false. This does
5312 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5313 * will not be called or the user can not otherwise perform edits on your
5314 * view; it is just a hint to the system that this is not the primary
5315 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005316 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005317 * @return Returns true if this view is a text editor, else false.
5318 */
5319 public boolean onCheckIsTextEditor() {
5320 return false;
5321 }
Romain Guy8506ab42009-06-11 17:35:47 -07005322
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005323 /**
5324 * Create a new InputConnection for an InputMethod to interact
5325 * with the view. The default implementation returns null, since it doesn't
5326 * support input methods. You can override this to implement such support.
5327 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005329 * <p>When implementing this, you probably also want to implement
5330 * {@link #onCheckIsTextEditor()} to indicate you will return a
5331 * non-null InputConnection.
5332 *
5333 * @param outAttrs Fill in with attribute information about the connection.
5334 */
5335 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5336 return null;
5337 }
5338
5339 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005340 * Called by the {@link android.view.inputmethod.InputMethodManager}
5341 * when a view who is not the current
5342 * input connection target is trying to make a call on the manager. The
5343 * default implementation returns false; you can override this to return
5344 * true for certain views if you are performing InputConnection proxying
5345 * to them.
5346 * @param view The View that is making the InputMethodManager call.
5347 * @return Return true to allow the call, false to reject.
5348 */
5349 public boolean checkInputConnectionProxy(View view) {
5350 return false;
5351 }
Romain Guy8506ab42009-06-11 17:35:47 -07005352
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005354 * Show the context menu for this view. It is not safe to hold on to the
5355 * menu after returning from this method.
5356 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005357 * You should normally not overload this method. Overload
5358 * {@link #onCreateContextMenu(ContextMenu)} or define an
5359 * {@link OnCreateContextMenuListener} to add items to the context menu.
5360 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005361 * @param menu The context menu to populate
5362 */
5363 public void createContextMenu(ContextMenu menu) {
5364 ContextMenuInfo menuInfo = getContextMenuInfo();
5365
5366 // Sets the current menu info so all items added to menu will have
5367 // my extra info set.
5368 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5369
5370 onCreateContextMenu(menu);
5371 if (mOnCreateContextMenuListener != null) {
5372 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5373 }
5374
5375 // Clear the extra information so subsequent items that aren't mine don't
5376 // have my extra info.
5377 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5378
5379 if (mParent != null) {
5380 mParent.createContextMenu(menu);
5381 }
5382 }
5383
5384 /**
5385 * Views should implement this if they have extra information to associate
5386 * with the context menu. The return result is supplied as a parameter to
5387 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5388 * callback.
5389 *
5390 * @return Extra information about the item for which the context menu
5391 * should be shown. This information will vary across different
5392 * subclasses of View.
5393 */
5394 protected ContextMenuInfo getContextMenuInfo() {
5395 return null;
5396 }
5397
5398 /**
5399 * Views should implement this if the view itself is going to add items to
5400 * the context menu.
5401 *
5402 * @param menu the context menu to populate
5403 */
5404 protected void onCreateContextMenu(ContextMenu menu) {
5405 }
5406
5407 /**
5408 * Implement this method to handle trackball motion events. The
5409 * <em>relative</em> movement of the trackball since the last event
5410 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5411 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5412 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5413 * they will often be fractional values, representing the more fine-grained
5414 * movement information available from a trackball).
5415 *
5416 * @param event The motion event.
5417 * @return True if the event was handled, false otherwise.
5418 */
5419 public boolean onTrackballEvent(MotionEvent event) {
5420 return false;
5421 }
5422
5423 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005424 * Implement this method to handle generic motion events.
5425 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005426 * Generic motion events describe joystick movements, mouse hovers, track pad
5427 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005428 * {@link MotionEvent#getSource() source} of the motion event specifies
5429 * the class of input that was received. Implementations of this method
5430 * must examine the bits in the source before processing the event.
5431 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005432 * </p><p>
5433 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5434 * are delivered to the view under the pointer. All other generic motion events are
5435 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005436 * </p>
5437 * <code>
5438 * public boolean onGenericMotionEvent(MotionEvent event) {
5439 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005440 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5441 * // process the joystick movement...
5442 * return true;
5443 * }
5444 * }
5445 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5446 * switch (event.getAction()) {
5447 * case MotionEvent.ACTION_HOVER_MOVE:
5448 * // process the mouse hover movement...
5449 * return true;
5450 * case MotionEvent.ACTION_SCROLL:
5451 * // process the scroll wheel movement...
5452 * return true;
5453 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005454 * }
5455 * return super.onGenericMotionEvent(event);
5456 * }
5457 * </code>
5458 *
5459 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005460 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005461 */
5462 public boolean onGenericMotionEvent(MotionEvent event) {
5463 return false;
5464 }
5465
5466 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005467 * Implement this method to handle hover events.
5468 * <p>
5469 * Hover events are pointer events with action {@link MotionEvent#ACTION_HOVER_ENTER},
5470 * {@link MotionEvent#ACTION_HOVER_MOVE}, or {@link MotionEvent#ACTION_HOVER_EXIT}.
5471 * </p><p>
5472 * The view receives hover enter as the pointer enters the bounds of the view and hover
5473 * exit as the pointer exits the bound of the view or just before the pointer goes down
5474 * (which implies that {@link #onTouchEvent} will be called soon).
5475 * </p><p>
5476 * If the view would like to handle the hover event itself and prevent its children
5477 * from receiving hover, it should return true from this method. If this method returns
5478 * true and a child has already received a hover enter event, the child will
5479 * automatically receive a hover exit event.
5480 * </p><p>
5481 * The default implementation sets the hovered state of the view if the view is
5482 * clickable.
5483 * </p>
5484 *
5485 * @param event The motion event that describes the hover.
5486 * @return True if this view handled the hover event and does not want its children
5487 * to receive the hover event.
5488 */
5489 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna032cc02011-03-07 16:56:21 -08005490 switch (event.getAction()) {
5491 case MotionEvent.ACTION_HOVER_ENTER:
5492 setHovered(true);
5493 break;
5494
5495 case MotionEvent.ACTION_HOVER_EXIT:
5496 setHovered(false);
5497 break;
5498 }
5499
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005500 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08005501 }
5502
5503 /**
5504 * Returns true if the view is currently hovered.
5505 *
5506 * @return True if the view is currently hovered.
5507 */
5508 public boolean isHovered() {
5509 return (mPrivateFlags & HOVERED) != 0;
5510 }
5511
5512 /**
5513 * Sets whether the view is currently hovered.
5514 *
5515 * @param hovered True if the view is hovered.
5516 */
5517 public void setHovered(boolean hovered) {
5518 if (hovered) {
5519 if ((mPrivateFlags & HOVERED) == 0) {
5520 mPrivateFlags |= HOVERED;
5521 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005522 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
Jeff Browna032cc02011-03-07 16:56:21 -08005523 }
5524 } else {
5525 if ((mPrivateFlags & HOVERED) != 0) {
5526 mPrivateFlags &= ~HOVERED;
5527 refreshDrawableState();
Svetoslav Ganov736c2752011-04-22 18:30:36 -07005528 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
Jeff Browna032cc02011-03-07 16:56:21 -08005529 }
5530 }
5531 }
5532
5533 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005534 * Implement this method to handle touch screen motion events.
5535 *
5536 * @param event The motion event.
5537 * @return True if the event was handled, false otherwise.
5538 */
5539 public boolean onTouchEvent(MotionEvent event) {
5540 final int viewFlags = mViewFlags;
5541
5542 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07005543 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
5544 mPrivateFlags &= ~PRESSED;
5545 refreshDrawableState();
5546 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005547 // A disabled view that is clickable still consumes the touch
5548 // events, it just doesn't respond to them.
5549 return (((viewFlags & CLICKABLE) == CLICKABLE ||
5550 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
5551 }
5552
5553 if (mTouchDelegate != null) {
5554 if (mTouchDelegate.onTouchEvent(event)) {
5555 return true;
5556 }
5557 }
5558
5559 if (((viewFlags & CLICKABLE) == CLICKABLE ||
5560 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
5561 switch (event.getAction()) {
5562 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08005563 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
5564 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005565 // take focus if we don't have it already and we should in
5566 // touch mode.
5567 boolean focusTaken = false;
5568 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
5569 focusTaken = requestFocus();
5570 }
5571
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08005572 if (prepressed) {
5573 // The button is being released before we actually
5574 // showed it as pressed. Make it show the pressed
5575 // state now (before scheduling the click) to ensure
5576 // the user sees it.
5577 mPrivateFlags |= PRESSED;
5578 refreshDrawableState();
5579 }
Joe Malin32736f02011-01-19 16:14:20 -08005580
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 if (!mHasPerformedLongPress) {
5582 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005583 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005584
5585 // Only perform take click actions if we were in the pressed state
5586 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08005587 // Use a Runnable and post this rather than calling
5588 // performClick directly. This lets other visual state
5589 // of the view update before click actions start.
5590 if (mPerformClick == null) {
5591 mPerformClick = new PerformClick();
5592 }
5593 if (!post(mPerformClick)) {
5594 performClick();
5595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005596 }
5597 }
5598
5599 if (mUnsetPressedState == null) {
5600 mUnsetPressedState = new UnsetPressedState();
5601 }
5602
Adam Powelle14579b2009-12-16 18:39:52 -08005603 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08005604 postDelayed(mUnsetPressedState,
5605 ViewConfiguration.getPressedStateDuration());
5606 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005607 // If the post failed, unpress right now
5608 mUnsetPressedState.run();
5609 }
Adam Powelle14579b2009-12-16 18:39:52 -08005610 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005611 }
5612 break;
5613
5614 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08005615 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005616
5617 // Walk up the hierarchy to determine if we're inside a scrolling container.
5618 boolean isInScrollingContainer = false;
5619 ViewParent p = getParent();
5620 while (p != null && p instanceof ViewGroup) {
5621 if (((ViewGroup) p).shouldDelayChildPressedState()) {
5622 isInScrollingContainer = true;
5623 break;
5624 }
5625 p = p.getParent();
5626 }
5627
5628 // For views inside a scrolling container, delay the pressed feedback for
5629 // a short period in case this is a scroll.
5630 if (isInScrollingContainer) {
5631 mPrivateFlags |= PREPRESSED;
5632 if (mPendingCheckForTap == null) {
5633 mPendingCheckForTap = new CheckForTap();
5634 }
5635 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
5636 } else {
5637 // Not inside a scrolling container, so show the feedback right away
5638 mPrivateFlags |= PRESSED;
5639 refreshDrawableState();
5640 checkForLongClick(0);
5641 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 break;
5643
5644 case MotionEvent.ACTION_CANCEL:
5645 mPrivateFlags &= ~PRESSED;
5646 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08005647 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005648 break;
5649
5650 case MotionEvent.ACTION_MOVE:
5651 final int x = (int) event.getX();
5652 final int y = (int) event.getY();
5653
5654 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07005655 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005656 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08005657 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005658 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08005659 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05005660 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005661
5662 // Need to switch from pressed to not pressed
5663 mPrivateFlags &= ~PRESSED;
5664 refreshDrawableState();
5665 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005666 }
5667 break;
5668 }
5669 return true;
5670 }
5671
5672 return false;
5673 }
5674
5675 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05005676 * Remove the longpress detection timer.
5677 */
5678 private void removeLongPressCallback() {
5679 if (mPendingCheckForLongPress != null) {
5680 removeCallbacks(mPendingCheckForLongPress);
5681 }
5682 }
Adam Powell3cb8b632011-01-21 15:34:14 -08005683
5684 /**
5685 * Remove the pending click action
5686 */
5687 private void removePerformClickCallback() {
5688 if (mPerformClick != null) {
5689 removeCallbacks(mPerformClick);
5690 }
5691 }
5692
Adam Powelle14579b2009-12-16 18:39:52 -08005693 /**
Romain Guya440b002010-02-24 15:57:54 -08005694 * Remove the prepress detection timer.
5695 */
5696 private void removeUnsetPressCallback() {
5697 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
5698 setPressed(false);
5699 removeCallbacks(mUnsetPressedState);
5700 }
5701 }
5702
5703 /**
Adam Powelle14579b2009-12-16 18:39:52 -08005704 * Remove the tap detection timer.
5705 */
5706 private void removeTapCallback() {
5707 if (mPendingCheckForTap != null) {
5708 mPrivateFlags &= ~PREPRESSED;
5709 removeCallbacks(mPendingCheckForTap);
5710 }
5711 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005712
5713 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005714 * Cancels a pending long press. Your subclass can use this if you
5715 * want the context menu to come up if the user presses and holds
5716 * at the same place, but you don't want it to come up if they press
5717 * and then move around enough to cause scrolling.
5718 */
5719 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05005720 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08005721
5722 /*
5723 * The prepressed state handled by the tap callback is a display
5724 * construct, but the tap callback will post a long press callback
5725 * less its own timeout. Remove it here.
5726 */
5727 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005728 }
5729
5730 /**
5731 * Sets the TouchDelegate for this View.
5732 */
5733 public void setTouchDelegate(TouchDelegate delegate) {
5734 mTouchDelegate = delegate;
5735 }
5736
5737 /**
5738 * Gets the TouchDelegate for this View.
5739 */
5740 public TouchDelegate getTouchDelegate() {
5741 return mTouchDelegate;
5742 }
5743
5744 /**
5745 * Set flags controlling behavior of this view.
5746 *
5747 * @param flags Constant indicating the value which should be set
5748 * @param mask Constant indicating the bit range that should be changed
5749 */
5750 void setFlags(int flags, int mask) {
5751 int old = mViewFlags;
5752 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
5753
5754 int changed = mViewFlags ^ old;
5755 if (changed == 0) {
5756 return;
5757 }
5758 int privateFlags = mPrivateFlags;
5759
5760 /* Check if the FOCUSABLE bit has changed */
5761 if (((changed & FOCUSABLE_MASK) != 0) &&
5762 ((privateFlags & HAS_BOUNDS) !=0)) {
5763 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
5764 && ((privateFlags & FOCUSED) != 0)) {
5765 /* Give up focus if we are no longer focusable */
5766 clearFocus();
5767 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
5768 && ((privateFlags & FOCUSED) == 0)) {
5769 /*
5770 * Tell the view system that we are now available to take focus
5771 * if no one else already has it.
5772 */
5773 if (mParent != null) mParent.focusableViewAvailable(this);
5774 }
5775 }
5776
5777 if ((flags & VISIBILITY_MASK) == VISIBLE) {
5778 if ((changed & VISIBILITY_MASK) != 0) {
5779 /*
5780 * If this view is becoming visible, set the DRAWN flag so that
5781 * the next invalidate() will not be skipped.
5782 */
5783 mPrivateFlags |= DRAWN;
5784
5785 needGlobalAttributesUpdate(true);
5786
5787 // a view becoming visible is worth notifying the parent
5788 // about in case nothing has focus. even if this specific view
5789 // isn't focusable, it may contain something that is, so let
5790 // the root view try to give this focus if nothing else does.
5791 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
5792 mParent.focusableViewAvailable(this);
5793 }
5794 }
5795 }
5796
5797 /* Check if the GONE bit has changed */
5798 if ((changed & GONE) != 0) {
5799 needGlobalAttributesUpdate(false);
5800 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005801 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005802
Romain Guyecd80ee2009-12-03 17:13:02 -08005803 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
5804 if (hasFocus()) clearFocus();
5805 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005806 }
5807 if (mAttachInfo != null) {
5808 mAttachInfo.mViewVisibilityChanged = true;
5809 }
5810 }
5811
5812 /* Check if the VISIBLE bit has changed */
5813 if ((changed & INVISIBLE) != 0) {
5814 needGlobalAttributesUpdate(false);
Romain Guy0fd89bf2011-01-26 15:41:30 -08005815 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005816
5817 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
5818 // root view becoming invisible shouldn't clear focus
5819 if (getRootView() != this) {
5820 clearFocus();
5821 }
5822 }
5823 if (mAttachInfo != null) {
5824 mAttachInfo.mViewVisibilityChanged = true;
5825 }
5826 }
5827
Adam Powell326d8082009-12-09 15:10:07 -08005828 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07005829 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08005830 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
5831 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07005832 }
Adam Powell326d8082009-12-09 15:10:07 -08005833 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
5834 }
5835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005836 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
5837 destroyDrawingCache();
5838 }
5839
5840 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
5841 destroyDrawingCache();
5842 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08005843 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 }
5845
5846 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
5847 destroyDrawingCache();
5848 mPrivateFlags &= ~DRAWING_CACHE_VALID;
5849 }
5850
5851 if ((changed & DRAW_MASK) != 0) {
5852 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
5853 if (mBGDrawable != null) {
5854 mPrivateFlags &= ~SKIP_DRAW;
5855 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
5856 } else {
5857 mPrivateFlags |= SKIP_DRAW;
5858 }
5859 } else {
5860 mPrivateFlags &= ~SKIP_DRAW;
5861 }
5862 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08005863 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005864 }
5865
5866 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08005867 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005868 mParent.recomputeViewAttributes(this);
5869 }
5870 }
Cibu Johny7632cb92010-02-22 13:01:02 -08005871
5872 if ((changed & HORIZONTAL_DIRECTION_MASK) != 0) {
5873 requestLayout();
5874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 }
5876
5877 /**
5878 * Change the view's z order in the tree, so it's on top of other sibling
5879 * views
5880 */
5881 public void bringToFront() {
5882 if (mParent != null) {
5883 mParent.bringChildToFront(this);
5884 }
5885 }
5886
5887 /**
5888 * This is called in response to an internal scroll in this view (i.e., the
5889 * view scrolled its own contents). This is typically as a result of
5890 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
5891 * called.
5892 *
5893 * @param l Current horizontal scroll origin.
5894 * @param t Current vertical scroll origin.
5895 * @param oldl Previous horizontal scroll origin.
5896 * @param oldt Previous vertical scroll origin.
5897 */
5898 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
5899 mBackgroundSizeChanged = true;
5900
5901 final AttachInfo ai = mAttachInfo;
5902 if (ai != null) {
5903 ai.mViewScrollChanged = true;
5904 }
5905 }
5906
5907 /**
Chet Haase21cd1382010-09-01 17:42:29 -07005908 * Interface definition for a callback to be invoked when the layout bounds of a view
5909 * changes due to layout processing.
5910 */
5911 public interface OnLayoutChangeListener {
5912 /**
5913 * Called when the focus state of a view has changed.
5914 *
5915 * @param v The view whose state has changed.
5916 * @param left The new value of the view's left property.
5917 * @param top The new value of the view's top property.
5918 * @param right The new value of the view's right property.
5919 * @param bottom The new value of the view's bottom property.
5920 * @param oldLeft The previous value of the view's left property.
5921 * @param oldTop The previous value of the view's top property.
5922 * @param oldRight The previous value of the view's right property.
5923 * @param oldBottom The previous value of the view's bottom property.
5924 */
5925 void onLayoutChange(View v, int left, int top, int right, int bottom,
5926 int oldLeft, int oldTop, int oldRight, int oldBottom);
5927 }
5928
5929 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005930 * This is called during layout when the size of this view has changed. If
5931 * you were just added to the view hierarchy, you're called with the old
5932 * values of 0.
5933 *
5934 * @param w Current width of this view.
5935 * @param h Current height of this view.
5936 * @param oldw Old width of this view.
5937 * @param oldh Old height of this view.
5938 */
5939 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
5940 }
5941
5942 /**
5943 * Called by draw to draw the child views. This may be overridden
5944 * by derived classes to gain control just before its children are drawn
5945 * (but after its own view has been drawn).
5946 * @param canvas the canvas on which to draw the view
5947 */
5948 protected void dispatchDraw(Canvas canvas) {
5949 }
5950
5951 /**
5952 * Gets the parent of this view. Note that the parent is a
5953 * ViewParent and not necessarily a View.
5954 *
5955 * @return Parent of this view.
5956 */
5957 public final ViewParent getParent() {
5958 return mParent;
5959 }
5960
5961 /**
5962 * Return the scrolled left position of this view. This is the left edge of
5963 * the displayed part of your view. You do not need to draw any pixels
5964 * farther left, since those are outside of the frame of your view on
5965 * screen.
5966 *
5967 * @return The left edge of the displayed part of your view, in pixels.
5968 */
5969 public final int getScrollX() {
5970 return mScrollX;
5971 }
5972
5973 /**
5974 * Return the scrolled top position of this view. This is the top edge of
5975 * the displayed part of your view. You do not need to draw any pixels above
5976 * it, since those are outside of the frame of your view on screen.
5977 *
5978 * @return The top edge of the displayed part of your view, in pixels.
5979 */
5980 public final int getScrollY() {
5981 return mScrollY;
5982 }
5983
5984 /**
5985 * Return the width of the your view.
5986 *
5987 * @return The width of your view, in pixels.
5988 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005989 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005990 public final int getWidth() {
5991 return mRight - mLeft;
5992 }
5993
5994 /**
5995 * Return the height of your view.
5996 *
5997 * @return The height of your view, in pixels.
5998 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07005999 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006000 public final int getHeight() {
6001 return mBottom - mTop;
6002 }
6003
6004 /**
6005 * Return the visible drawing bounds of your view. Fills in the output
6006 * rectangle with the values from getScrollX(), getScrollY(),
6007 * getWidth(), and getHeight().
6008 *
6009 * @param outRect The (scrolled) drawing bounds of the view.
6010 */
6011 public void getDrawingRect(Rect outRect) {
6012 outRect.left = mScrollX;
6013 outRect.top = mScrollY;
6014 outRect.right = mScrollX + (mRight - mLeft);
6015 outRect.bottom = mScrollY + (mBottom - mTop);
6016 }
6017
6018 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006019 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6020 * raw width component (that is the result is masked by
6021 * {@link #MEASURED_SIZE_MASK}).
6022 *
6023 * @return The raw measured width of this view.
6024 */
6025 public final int getMeasuredWidth() {
6026 return mMeasuredWidth & MEASURED_SIZE_MASK;
6027 }
6028
6029 /**
6030 * Return the full width measurement information for this view as computed
6031 * by the most recent call to {@link #measure}. This result is a bit mask
6032 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006033 * This should be used during measurement and layout calculations only. Use
6034 * {@link #getWidth()} to see how wide a view is after layout.
6035 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006036 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006037 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006038 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006039 return mMeasuredWidth;
6040 }
6041
6042 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006043 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6044 * raw width component (that is the result is masked by
6045 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006046 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006047 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006048 */
6049 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006050 return mMeasuredHeight & MEASURED_SIZE_MASK;
6051 }
6052
6053 /**
6054 * Return the full height measurement information for this view as computed
6055 * by the most recent call to {@link #measure}. This result is a bit mask
6056 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6057 * This should be used during measurement and layout calculations only. Use
6058 * {@link #getHeight()} to see how wide a view is after layout.
6059 *
6060 * @return The measured width of this view as a bit mask.
6061 */
6062 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006063 return mMeasuredHeight;
6064 }
6065
6066 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006067 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6068 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6069 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6070 * and the height component is at the shifted bits
6071 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6072 */
6073 public final int getMeasuredState() {
6074 return (mMeasuredWidth&MEASURED_STATE_MASK)
6075 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6076 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6077 }
6078
6079 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006080 * The transform matrix of this view, which is calculated based on the current
6081 * roation, scale, and pivot properties.
6082 *
6083 * @see #getRotation()
6084 * @see #getScaleX()
6085 * @see #getScaleY()
6086 * @see #getPivotX()
6087 * @see #getPivotY()
6088 * @return The current transform matrix for the view
6089 */
6090 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006091 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006092 return mMatrix;
6093 }
6094
6095 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006096 * Utility function to determine if the value is far enough away from zero to be
6097 * considered non-zero.
6098 * @param value A floating point value to check for zero-ness
6099 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6100 */
6101 private static boolean nonzero(float value) {
6102 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6103 }
6104
6105 /**
Jeff Brown86671742010-09-30 20:00:15 -07006106 * Returns true if the transform matrix is the identity matrix.
6107 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006108 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006109 * @return True if the transform matrix is the identity matrix, false otherwise.
6110 */
Jeff Brown86671742010-09-30 20:00:15 -07006111 final boolean hasIdentityMatrix() {
6112 updateMatrix();
6113 return mMatrixIsIdentity;
6114 }
6115
6116 /**
6117 * Recomputes the transform matrix if necessary.
6118 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006119 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006120 if (mMatrixDirty) {
6121 // transform-related properties have changed since the last time someone
6122 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006123
6124 // Figure out if we need to update the pivot point
6125 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006126 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006127 mPrevWidth = mRight - mLeft;
6128 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006129 mPivotX = mPrevWidth / 2f;
6130 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006131 }
6132 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006133 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006134 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6135 mMatrix.setTranslate(mTranslationX, mTranslationY);
6136 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6137 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6138 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006139 if (mCamera == null) {
6140 mCamera = new Camera();
6141 matrix3D = new Matrix();
6142 }
6143 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006144 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006145 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006146 mCamera.getMatrix(matrix3D);
6147 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006148 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006149 mMatrix.postConcat(matrix3D);
6150 mCamera.restore();
6151 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006152 mMatrixDirty = false;
6153 mMatrixIsIdentity = mMatrix.isIdentity();
6154 mInverseMatrixDirty = true;
6155 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006156 }
6157
6158 /**
6159 * Utility method to retrieve the inverse of the current mMatrix property.
6160 * We cache the matrix to avoid recalculating it when transform properties
6161 * have not changed.
6162 *
6163 * @return The inverse of the current matrix of this view.
6164 */
Jeff Brown86671742010-09-30 20:00:15 -07006165 final Matrix getInverseMatrix() {
6166 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006167 if (mInverseMatrixDirty) {
6168 if (mInverseMatrix == null) {
6169 mInverseMatrix = new Matrix();
6170 }
6171 mMatrix.invert(mInverseMatrix);
6172 mInverseMatrixDirty = false;
6173 }
6174 return mInverseMatrix;
6175 }
6176
6177 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006178 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6179 * views are drawn) from the camera to this view. The camera's distance
6180 * affects 3D transformations, for instance rotations around the X and Y
6181 * axis. If the rotationX or rotationY properties are changed and this view is
6182 * large (more than half the size of the screen), it is recommended to always
6183 * use a camera distance that's greater than the height (X axis rotation) or
6184 * the width (Y axis rotation) of this view.</p>
6185 *
6186 * <p>The distance of the camera from the view plane can have an affect on the
6187 * perspective distortion of the view when it is rotated around the x or y axis.
6188 * For example, a large distance will result in a large viewing angle, and there
6189 * will not be much perspective distortion of the view as it rotates. A short
6190 * distance may cause much more perspective distortion upon rotation, and can
6191 * also result in some drawing artifacts if the rotated view ends up partially
6192 * behind the camera (which is why the recommendation is to use a distance at
6193 * least as far as the size of the view, if the view is to be rotated.)</p>
6194 *
6195 * <p>The distance is expressed in "depth pixels." The default distance depends
6196 * on the screen density. For instance, on a medium density display, the
6197 * default distance is 1280. On a high density display, the default distance
6198 * is 1920.</p>
6199 *
6200 * <p>If you want to specify a distance that leads to visually consistent
6201 * results across various densities, use the following formula:</p>
6202 * <pre>
6203 * float scale = context.getResources().getDisplayMetrics().density;
6204 * view.setCameraDistance(distance * scale);
6205 * </pre>
6206 *
6207 * <p>The density scale factor of a high density display is 1.5,
6208 * and 1920 = 1280 * 1.5.</p>
6209 *
6210 * @param distance The distance in "depth pixels", if negative the opposite
6211 * value is used
6212 *
6213 * @see #setRotationX(float)
6214 * @see #setRotationY(float)
6215 */
6216 public void setCameraDistance(float distance) {
6217 invalidateParentCaches();
6218 invalidate(false);
6219
6220 final float dpi = mResources.getDisplayMetrics().densityDpi;
6221 if (mCamera == null) {
6222 mCamera = new Camera();
6223 matrix3D = new Matrix();
6224 }
6225
6226 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6227 mMatrixDirty = true;
6228
6229 invalidate(false);
6230 }
6231
6232 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006233 * The degrees that the view is rotated around the pivot point.
6234 *
Romain Guya5364ee2011-02-24 14:46:04 -08006235 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006236 * @see #getPivotX()
6237 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006238 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006239 * @return The degrees of rotation.
6240 */
6241 public float getRotation() {
6242 return mRotation;
6243 }
6244
6245 /**
Chet Haase897247b2010-09-09 14:54:47 -07006246 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6247 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006248 *
6249 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006250 *
6251 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006252 * @see #getPivotX()
6253 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006254 * @see #setRotationX(float)
6255 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006256 *
6257 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006258 */
6259 public void setRotation(float rotation) {
6260 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006261 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006262 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006263 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006264 mRotation = rotation;
6265 mMatrixDirty = true;
6266 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006267 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006268 }
6269 }
6270
6271 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006272 * The degrees that the view is rotated around the vertical axis through the pivot point.
6273 *
6274 * @see #getPivotX()
6275 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006276 * @see #setRotationY(float)
6277 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006278 * @return The degrees of Y rotation.
6279 */
6280 public float getRotationY() {
6281 return mRotationY;
6282 }
6283
6284 /**
Chet Haase897247b2010-09-09 14:54:47 -07006285 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6286 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6287 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006288 *
6289 * When rotating large views, it is recommended to adjust the camera distance
6290 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006291 *
6292 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006293 *
6294 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006295 * @see #getPivotX()
6296 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006297 * @see #setRotation(float)
6298 * @see #setRotationX(float)
6299 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006300 *
6301 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006302 */
6303 public void setRotationY(float rotationY) {
6304 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006305 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006306 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006307 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006308 mRotationY = rotationY;
6309 mMatrixDirty = true;
6310 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006311 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006312 }
6313 }
6314
6315 /**
6316 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6317 *
6318 * @see #getPivotX()
6319 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006320 * @see #setRotationX(float)
6321 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006322 * @return The degrees of X rotation.
6323 */
6324 public float getRotationX() {
6325 return mRotationX;
6326 }
6327
6328 /**
Chet Haase897247b2010-09-09 14:54:47 -07006329 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6330 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6331 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006332 *
6333 * When rotating large views, it is recommended to adjust the camera distance
6334 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006335 *
6336 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006337 *
6338 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006339 * @see #getPivotX()
6340 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006341 * @see #setRotation(float)
6342 * @see #setRotationY(float)
6343 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006344 *
6345 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006346 */
6347 public void setRotationX(float rotationX) {
6348 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006349 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006350 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006351 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006352 mRotationX = rotationX;
6353 mMatrixDirty = true;
6354 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006355 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006356 }
6357 }
6358
6359 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006360 * The amount that the view is scaled in x around the pivot point, as a proportion of
6361 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6362 *
Joe Onorato93162322010-09-16 15:42:01 -04006363 * <p>By default, this is 1.0f.
6364 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006365 * @see #getPivotX()
6366 * @see #getPivotY()
6367 * @return The scaling factor.
6368 */
6369 public float getScaleX() {
6370 return mScaleX;
6371 }
6372
6373 /**
6374 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6375 * the view's unscaled width. A value of 1 means that no scaling is applied.
6376 *
6377 * @param scaleX The scaling factor.
6378 * @see #getPivotX()
6379 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006380 *
6381 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006382 */
6383 public void setScaleX(float scaleX) {
6384 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006385 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006386 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006387 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006388 mScaleX = scaleX;
6389 mMatrixDirty = true;
6390 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006391 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006392 }
6393 }
6394
6395 /**
6396 * The amount that the view is scaled in y around the pivot point, as a proportion of
6397 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6398 *
Joe Onorato93162322010-09-16 15:42:01 -04006399 * <p>By default, this is 1.0f.
6400 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006401 * @see #getPivotX()
6402 * @see #getPivotY()
6403 * @return The scaling factor.
6404 */
6405 public float getScaleY() {
6406 return mScaleY;
6407 }
6408
6409 /**
6410 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
6411 * the view's unscaled width. A value of 1 means that no scaling is applied.
6412 *
6413 * @param scaleY The scaling factor.
6414 * @see #getPivotX()
6415 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006416 *
6417 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07006418 */
6419 public void setScaleY(float scaleY) {
6420 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006421 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006422 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006423 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006424 mScaleY = scaleY;
6425 mMatrixDirty = true;
6426 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006427 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006428 }
6429 }
6430
6431 /**
6432 * The x location of the point around which the view is {@link #setRotation(float) rotated}
6433 * and {@link #setScaleX(float) scaled}.
6434 *
6435 * @see #getRotation()
6436 * @see #getScaleX()
6437 * @see #getScaleY()
6438 * @see #getPivotY()
6439 * @return The x location of the pivot point.
6440 */
6441 public float getPivotX() {
6442 return mPivotX;
6443 }
6444
6445 /**
6446 * Sets the x location of the point around which the view is
6447 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07006448 * By default, the pivot point is centered on the object.
6449 * Setting this property disables this behavior and causes the view to use only the
6450 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006451 *
6452 * @param pivotX The x location of the pivot point.
6453 * @see #getRotation()
6454 * @see #getScaleX()
6455 * @see #getScaleY()
6456 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006457 *
6458 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07006459 */
6460 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006461 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006462 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006463 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006464 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006465 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006466 mPivotX = pivotX;
6467 mMatrixDirty = true;
6468 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006469 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006470 }
6471 }
6472
6473 /**
6474 * The y location of the point around which the view is {@link #setRotation(float) rotated}
6475 * and {@link #setScaleY(float) scaled}.
6476 *
6477 * @see #getRotation()
6478 * @see #getScaleX()
6479 * @see #getScaleY()
6480 * @see #getPivotY()
6481 * @return The y location of the pivot point.
6482 */
6483 public float getPivotY() {
6484 return mPivotY;
6485 }
6486
6487 /**
6488 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07006489 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
6490 * Setting this property disables this behavior and causes the view to use only the
6491 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07006492 *
6493 * @param pivotY The y location of the pivot point.
6494 * @see #getRotation()
6495 * @see #getScaleX()
6496 * @see #getScaleY()
6497 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006498 *
6499 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07006500 */
6501 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006502 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07006503 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006504 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006505 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006506 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006507 mPivotY = pivotY;
6508 mMatrixDirty = true;
6509 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006510 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006511 }
6512 }
6513
6514 /**
6515 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
6516 * completely transparent and 1 means the view is completely opaque.
6517 *
Joe Onorato93162322010-09-16 15:42:01 -04006518 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07006519 * @return The opacity of the view.
6520 */
6521 public float getAlpha() {
6522 return mAlpha;
6523 }
6524
6525 /**
Romain Guy171c5922011-01-06 10:04:23 -08006526 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
6527 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08006528 *
Romain Guy171c5922011-01-06 10:04:23 -08006529 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
6530 * responsible for applying the opacity itself. Otherwise, calling this method is
6531 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08006532 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07006533 *
6534 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08006535 *
Joe Malin32736f02011-01-19 16:14:20 -08006536 * @see #setLayerType(int, android.graphics.Paint)
6537 *
Chet Haase73066682010-11-29 15:55:32 -08006538 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07006539 */
6540 public void setAlpha(float alpha) {
6541 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006542 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07006543 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07006544 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006545 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006546 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07006547 } else {
Romain Guya3496a92010-10-12 11:53:24 -07006548 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07006549 invalidate(false);
6550 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006551 }
6552
6553 /**
Chet Haasea00f3862011-02-22 06:34:40 -08006554 * Faster version of setAlpha() which performs the same steps except there are
6555 * no calls to invalidate(). The caller of this function should perform proper invalidation
6556 * on the parent and this object. The return value indicates whether the subclass handles
6557 * alpha (the return value for onSetAlpha()).
6558 *
6559 * @param alpha The new value for the alpha property
6560 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
6561 */
6562 boolean setAlphaNoInvalidation(float alpha) {
6563 mAlpha = alpha;
6564 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
6565 if (subclassHandlesAlpha) {
6566 mPrivateFlags |= ALPHA_SET;
6567 } else {
6568 mPrivateFlags &= ~ALPHA_SET;
6569 }
6570 return subclassHandlesAlpha;
6571 }
6572
6573 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006574 * Top position of this view relative to its parent.
6575 *
6576 * @return The top of this view, in pixels.
6577 */
6578 @ViewDebug.CapturedViewProperty
6579 public final int getTop() {
6580 return mTop;
6581 }
6582
6583 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006584 * Sets the top position of this view relative to its parent. This method is meant to be called
6585 * by the layout system and should not generally be called otherwise, because the property
6586 * may be changed at any time by the layout.
6587 *
6588 * @param top The top of this view, in pixels.
6589 */
6590 public final void setTop(int top) {
6591 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07006592 updateMatrix();
6593 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006594 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006595 int minTop;
6596 int yLoc;
6597 if (top < mTop) {
6598 minTop = top;
6599 yLoc = top - mTop;
6600 } else {
6601 minTop = mTop;
6602 yLoc = 0;
6603 }
Chet Haasee9140a72011-02-16 16:23:29 -08006604 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006605 }
6606 } else {
6607 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006608 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006609 }
6610
Chet Haaseed032702010-10-01 14:05:54 -07006611 int width = mRight - mLeft;
6612 int oldHeight = mBottom - mTop;
6613
Chet Haase21cd1382010-09-01 17:42:29 -07006614 mTop = top;
6615
Chet Haaseed032702010-10-01 14:05:54 -07006616 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6617
Chet Haase21cd1382010-09-01 17:42:29 -07006618 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006619 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6620 // A change in dimension means an auto-centered pivot point changes, too
6621 mMatrixDirty = true;
6622 }
Chet Haase21cd1382010-09-01 17:42:29 -07006623 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006624 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006625 }
Chet Haase55dbb652010-12-21 20:15:08 -08006626 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006627 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006628 }
6629 }
6630
6631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 * Bottom position of this view relative to its parent.
6633 *
6634 * @return The bottom of this view, in pixels.
6635 */
6636 @ViewDebug.CapturedViewProperty
6637 public final int getBottom() {
6638 return mBottom;
6639 }
6640
6641 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08006642 * True if this view has changed since the last time being drawn.
6643 *
6644 * @return The dirty state of this view.
6645 */
6646 public boolean isDirty() {
6647 return (mPrivateFlags & DIRTY_MASK) != 0;
6648 }
6649
6650 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006651 * Sets the bottom position of this view relative to its parent. This method is meant to be
6652 * called by the layout system and should not generally be called otherwise, because the
6653 * property may be changed at any time by the layout.
6654 *
6655 * @param bottom The bottom of this view, in pixels.
6656 */
6657 public final void setBottom(int bottom) {
6658 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07006659 updateMatrix();
6660 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006661 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006662 int maxBottom;
6663 if (bottom < mBottom) {
6664 maxBottom = mBottom;
6665 } else {
6666 maxBottom = bottom;
6667 }
Chet Haasee9140a72011-02-16 16:23:29 -08006668 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006669 }
6670 } else {
6671 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006672 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006673 }
6674
Chet Haaseed032702010-10-01 14:05:54 -07006675 int width = mRight - mLeft;
6676 int oldHeight = mBottom - mTop;
6677
Chet Haase21cd1382010-09-01 17:42:29 -07006678 mBottom = bottom;
6679
Chet Haaseed032702010-10-01 14:05:54 -07006680 onSizeChanged(width, mBottom - mTop, width, oldHeight);
6681
Chet Haase21cd1382010-09-01 17:42:29 -07006682 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006683 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6684 // A change in dimension means an auto-centered pivot point changes, too
6685 mMatrixDirty = true;
6686 }
Chet Haase21cd1382010-09-01 17:42:29 -07006687 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006688 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006689 }
Chet Haase55dbb652010-12-21 20:15:08 -08006690 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006691 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006692 }
6693 }
6694
6695 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006696 * Left position of this view relative to its parent.
6697 *
6698 * @return The left edge of this view, in pixels.
6699 */
6700 @ViewDebug.CapturedViewProperty
6701 public final int getLeft() {
6702 return mLeft;
6703 }
6704
6705 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006706 * Sets the left position of this view relative to its parent. This method is meant to be called
6707 * by the layout system and should not generally be called otherwise, because the property
6708 * may be changed at any time by the layout.
6709 *
6710 * @param left The bottom of this view, in pixels.
6711 */
6712 public final void setLeft(int left) {
6713 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07006714 updateMatrix();
6715 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006716 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006717 int minLeft;
6718 int xLoc;
6719 if (left < mLeft) {
6720 minLeft = left;
6721 xLoc = left - mLeft;
6722 } else {
6723 minLeft = mLeft;
6724 xLoc = 0;
6725 }
Chet Haasee9140a72011-02-16 16:23:29 -08006726 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006727 }
6728 } else {
6729 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006730 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006731 }
6732
Chet Haaseed032702010-10-01 14:05:54 -07006733 int oldWidth = mRight - mLeft;
6734 int height = mBottom - mTop;
6735
Chet Haase21cd1382010-09-01 17:42:29 -07006736 mLeft = left;
6737
Chet Haaseed032702010-10-01 14:05:54 -07006738 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6739
Chet Haase21cd1382010-09-01 17:42:29 -07006740 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006741 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6742 // A change in dimension means an auto-centered pivot point changes, too
6743 mMatrixDirty = true;
6744 }
Chet Haase21cd1382010-09-01 17:42:29 -07006745 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006746 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006747 }
Chet Haase55dbb652010-12-21 20:15:08 -08006748 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006749 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006750 }
6751 }
6752
6753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006754 * Right position of this view relative to its parent.
6755 *
6756 * @return The right edge of this view, in pixels.
6757 */
6758 @ViewDebug.CapturedViewProperty
6759 public final int getRight() {
6760 return mRight;
6761 }
6762
6763 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006764 * Sets the right position of this view relative to its parent. This method is meant to be called
6765 * by the layout system and should not generally be called otherwise, because the property
6766 * may be changed at any time by the layout.
6767 *
6768 * @param right The bottom of this view, in pixels.
6769 */
6770 public final void setRight(int right) {
6771 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07006772 updateMatrix();
6773 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08006774 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07006775 int maxRight;
6776 if (right < mRight) {
6777 maxRight = mRight;
6778 } else {
6779 maxRight = right;
6780 }
Chet Haasee9140a72011-02-16 16:23:29 -08006781 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07006782 }
6783 } else {
6784 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08006785 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006786 }
6787
Chet Haaseed032702010-10-01 14:05:54 -07006788 int oldWidth = mRight - mLeft;
6789 int height = mBottom - mTop;
6790
Chet Haase21cd1382010-09-01 17:42:29 -07006791 mRight = right;
6792
Chet Haaseed032702010-10-01 14:05:54 -07006793 onSizeChanged(mRight - mLeft, height, oldWidth, height);
6794
Chet Haase21cd1382010-09-01 17:42:29 -07006795 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006796 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
6797 // A change in dimension means an auto-centered pivot point changes, too
6798 mMatrixDirty = true;
6799 }
Chet Haase21cd1382010-09-01 17:42:29 -07006800 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08006801 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07006802 }
Chet Haase55dbb652010-12-21 20:15:08 -08006803 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08006804 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07006805 }
6806 }
6807
6808 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006809 * The visual x position of this view, in pixels. This is equivalent to the
6810 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08006811 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07006812 *
Chet Haasedf030d22010-07-30 17:22:38 -07006813 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006814 */
Chet Haasedf030d22010-07-30 17:22:38 -07006815 public float getX() {
6816 return mLeft + mTranslationX;
6817 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006818
Chet Haasedf030d22010-07-30 17:22:38 -07006819 /**
6820 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
6821 * {@link #setTranslationX(float) translationX} property to be the difference between
6822 * the x value passed in and the current {@link #getLeft() left} property.
6823 *
6824 * @param x The visual x position of this view, in pixels.
6825 */
6826 public void setX(float x) {
6827 setTranslationX(x - mLeft);
6828 }
Romain Guy33e72ae2010-07-17 12:40:29 -07006829
Chet Haasedf030d22010-07-30 17:22:38 -07006830 /**
6831 * The visual y position of this view, in pixels. This is equivalent to the
6832 * {@link #setTranslationY(float) translationY} property plus the current
6833 * {@link #getTop() top} property.
6834 *
6835 * @return The visual y position of this view, in pixels.
6836 */
6837 public float getY() {
6838 return mTop + mTranslationY;
6839 }
6840
6841 /**
6842 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
6843 * {@link #setTranslationY(float) translationY} property to be the difference between
6844 * the y value passed in and the current {@link #getTop() top} property.
6845 *
6846 * @param y The visual y position of this view, in pixels.
6847 */
6848 public void setY(float y) {
6849 setTranslationY(y - mTop);
6850 }
6851
6852
6853 /**
6854 * The horizontal location of this view relative to its {@link #getLeft() left} position.
6855 * This position is post-layout, in addition to wherever the object's
6856 * layout placed it.
6857 *
6858 * @return The horizontal position of this view relative to its left position, in pixels.
6859 */
6860 public float getTranslationX() {
6861 return mTranslationX;
6862 }
6863
6864 /**
6865 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
6866 * This effectively positions the object post-layout, in addition to wherever the object's
6867 * layout placed it.
6868 *
6869 * @param translationX The horizontal position of this view relative to its left position,
6870 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006871 *
6872 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07006873 */
6874 public void setTranslationX(float translationX) {
6875 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006876 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006877 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006878 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006879 mTranslationX = translationX;
6880 mMatrixDirty = true;
6881 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006882 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006883 }
6884 }
6885
6886 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006887 * The horizontal location of this view relative to its {@link #getTop() top} position.
6888 * This position is post-layout, in addition to wherever the object's
6889 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006890 *
Chet Haasedf030d22010-07-30 17:22:38 -07006891 * @return The vertical position of this view relative to its top position,
6892 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07006893 */
Chet Haasedf030d22010-07-30 17:22:38 -07006894 public float getTranslationY() {
6895 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07006896 }
6897
6898 /**
Chet Haasedf030d22010-07-30 17:22:38 -07006899 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
6900 * This effectively positions the object post-layout, in addition to wherever the object's
6901 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07006902 *
Chet Haasedf030d22010-07-30 17:22:38 -07006903 * @param translationY The vertical position of this view relative to its top position,
6904 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08006905 *
6906 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07006907 */
Chet Haasedf030d22010-07-30 17:22:38 -07006908 public void setTranslationY(float translationY) {
6909 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006910 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07006911 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006912 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006913 mTranslationY = translationY;
6914 mMatrixDirty = true;
6915 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006916 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07006917 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006918 }
6919
6920 /**
Romain Guyda489792011-02-03 01:05:15 -08006921 * @hide
6922 */
Michael Jurkadece29f2011-02-03 01:41:49 -08006923 public void setFastTranslationX(float x) {
6924 mTranslationX = x;
6925 mMatrixDirty = true;
6926 }
6927
6928 /**
6929 * @hide
6930 */
6931 public void setFastTranslationY(float y) {
6932 mTranslationY = y;
6933 mMatrixDirty = true;
6934 }
6935
6936 /**
6937 * @hide
6938 */
Romain Guyda489792011-02-03 01:05:15 -08006939 public void setFastX(float x) {
6940 mTranslationX = x - mLeft;
6941 mMatrixDirty = true;
6942 }
6943
6944 /**
6945 * @hide
6946 */
6947 public void setFastY(float y) {
6948 mTranslationY = y - mTop;
6949 mMatrixDirty = true;
6950 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006951
Romain Guyda489792011-02-03 01:05:15 -08006952 /**
6953 * @hide
6954 */
6955 public void setFastScaleX(float x) {
6956 mScaleX = x;
6957 mMatrixDirty = true;
6958 }
6959
6960 /**
6961 * @hide
6962 */
6963 public void setFastScaleY(float y) {
6964 mScaleY = y;
6965 mMatrixDirty = true;
6966 }
6967
6968 /**
6969 * @hide
6970 */
6971 public void setFastAlpha(float alpha) {
6972 mAlpha = alpha;
6973 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006974
Romain Guyda489792011-02-03 01:05:15 -08006975 /**
6976 * @hide
6977 */
6978 public void setFastRotationY(float y) {
6979 mRotationY = y;
6980 mMatrixDirty = true;
6981 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08006982
Romain Guyda489792011-02-03 01:05:15 -08006983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006984 * Hit rectangle in parent's coordinates
6985 *
6986 * @param outRect The hit rectangle of the view.
6987 */
6988 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07006989 updateMatrix();
6990 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07006991 outRect.set(mLeft, mTop, mRight, mBottom);
6992 } else {
6993 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07006994 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07006995 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07006996 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
6997 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07006998 }
6999 }
7000
7001 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007002 * Determines whether the given point, in local coordinates is inside the view.
7003 */
7004 /*package*/ final boolean pointInView(float localX, float localY) {
7005 return localX >= 0 && localX < (mRight - mLeft)
7006 && localY >= 0 && localY < (mBottom - mTop);
7007 }
7008
7009 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007010 * Utility method to determine whether the given point, in local coordinates,
7011 * is inside the view, where the area of the view is expanded by the slop factor.
7012 * This method is called while processing touch-move events to determine if the event
7013 * is still within the view.
7014 */
7015 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007016 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007017 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007018 }
7019
7020 /**
7021 * When a view has focus and the user navigates away from it, the next view is searched for
7022 * starting from the rectangle filled in by this method.
7023 *
7024 * By default, the rectange is the {@link #getDrawingRect})of the view. However, if your
7025 * view maintains some idea of internal selection, such as a cursor, or a selected row
7026 * or column, you should override this method and fill in a more specific rectangle.
7027 *
7028 * @param r The rectangle to fill in, in this view's coordinates.
7029 */
7030 public void getFocusedRect(Rect r) {
7031 getDrawingRect(r);
7032 }
7033
7034 /**
7035 * If some part of this view is not clipped by any of its parents, then
7036 * return that area in r in global (root) coordinates. To convert r to local
7037 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7038 * -globalOffset.y)) If the view is completely clipped or translated out,
7039 * return false.
7040 *
7041 * @param r If true is returned, r holds the global coordinates of the
7042 * visible portion of this view.
7043 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7044 * between this view and its root. globalOffet may be null.
7045 * @return true if r is non-empty (i.e. part of the view is visible at the
7046 * root level.
7047 */
7048 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7049 int width = mRight - mLeft;
7050 int height = mBottom - mTop;
7051 if (width > 0 && height > 0) {
7052 r.set(0, 0, width, height);
7053 if (globalOffset != null) {
7054 globalOffset.set(-mScrollX, -mScrollY);
7055 }
7056 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7057 }
7058 return false;
7059 }
7060
7061 public final boolean getGlobalVisibleRect(Rect r) {
7062 return getGlobalVisibleRect(r, null);
7063 }
7064
7065 public final boolean getLocalVisibleRect(Rect r) {
7066 Point offset = new Point();
7067 if (getGlobalVisibleRect(r, offset)) {
7068 r.offset(-offset.x, -offset.y); // make r local
7069 return true;
7070 }
7071 return false;
7072 }
7073
7074 /**
7075 * Offset this view's vertical location by the specified number of pixels.
7076 *
7077 * @param offset the number of pixels to offset the view by
7078 */
7079 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007080 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007081 updateMatrix();
7082 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007083 final ViewParent p = mParent;
7084 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007085 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007086 int minTop;
7087 int maxBottom;
7088 int yLoc;
7089 if (offset < 0) {
7090 minTop = mTop + offset;
7091 maxBottom = mBottom;
7092 yLoc = offset;
7093 } else {
7094 minTop = mTop;
7095 maxBottom = mBottom + offset;
7096 yLoc = 0;
7097 }
7098 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7099 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007100 }
7101 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007102 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007103 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007104
Chet Haasec3aa3612010-06-17 08:50:37 -07007105 mTop += offset;
7106 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007107
Chet Haasec3aa3612010-06-17 08:50:37 -07007108 if (!mMatrixIsIdentity) {
7109 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007110 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007111 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007112 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007113 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007114 }
7115
7116 /**
7117 * Offset this view's horizontal location by the specified amount of pixels.
7118 *
7119 * @param offset the numer of pixels to offset the view by
7120 */
7121 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007122 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007123 updateMatrix();
7124 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007125 final ViewParent p = mParent;
7126 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007127 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007128 int minLeft;
7129 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007130 if (offset < 0) {
7131 minLeft = mLeft + offset;
7132 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007133 } else {
7134 minLeft = mLeft;
7135 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007136 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007137 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007138 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 }
7140 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007141 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007142 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007143
Chet Haasec3aa3612010-06-17 08:50:37 -07007144 mLeft += offset;
7145 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007146
Chet Haasec3aa3612010-06-17 08:50:37 -07007147 if (!mMatrixIsIdentity) {
7148 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007149 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007150 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007151 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007153 }
7154
7155 /**
7156 * Get the LayoutParams associated with this view. All views should have
7157 * layout parameters. These supply parameters to the <i>parent</i> of this
7158 * view specifying how it should be arranged. There are many subclasses of
7159 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7160 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007161 *
7162 * This method may return null if this View is not attached to a parent
7163 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7164 * was not invoked successfully. When a View is attached to a parent
7165 * ViewGroup, this method must not return null.
7166 *
7167 * @return The LayoutParams associated with this view, or null if no
7168 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007170 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007171 public ViewGroup.LayoutParams getLayoutParams() {
7172 return mLayoutParams;
7173 }
7174
7175 /**
7176 * Set the layout parameters associated with this view. These supply
7177 * parameters to the <i>parent</i> of this view specifying how it should be
7178 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7179 * correspond to the different subclasses of ViewGroup that are responsible
7180 * for arranging their children.
7181 *
Romain Guy01c174b2011-02-22 11:51:06 -08007182 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007183 */
7184 public void setLayoutParams(ViewGroup.LayoutParams params) {
7185 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007186 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007187 }
7188 mLayoutParams = params;
7189 requestLayout();
7190 }
7191
7192 /**
7193 * Set the scrolled position of your view. This will cause a call to
7194 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7195 * invalidated.
7196 * @param x the x position to scroll to
7197 * @param y the y position to scroll to
7198 */
7199 public void scrollTo(int x, int y) {
7200 if (mScrollX != x || mScrollY != y) {
7201 int oldX = mScrollX;
7202 int oldY = mScrollY;
7203 mScrollX = x;
7204 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007205 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007206 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007207 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007208 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007209 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007210 }
7211 }
7212
7213 /**
7214 * Move the scrolled position of your view. This will cause a call to
7215 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7216 * invalidated.
7217 * @param x the amount of pixels to scroll by horizontally
7218 * @param y the amount of pixels to scroll by vertically
7219 */
7220 public void scrollBy(int x, int y) {
7221 scrollTo(mScrollX + x, mScrollY + y);
7222 }
7223
7224 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007225 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7226 * animation to fade the scrollbars out after a default delay. If a subclass
7227 * provides animated scrolling, the start delay should equal the duration
7228 * of the scrolling animation.</p>
7229 *
7230 * <p>The animation starts only if at least one of the scrollbars is
7231 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7232 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7233 * this method returns true, and false otherwise. If the animation is
7234 * started, this method calls {@link #invalidate()}; in that case the
7235 * caller should not call {@link #invalidate()}.</p>
7236 *
7237 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007238 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007239 *
7240 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7241 * and {@link #scrollTo(int, int)}.</p>
7242 *
7243 * @return true if the animation is played, false otherwise
7244 *
7245 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007246 * @see #scrollBy(int, int)
7247 * @see #scrollTo(int, int)
7248 * @see #isHorizontalScrollBarEnabled()
7249 * @see #isVerticalScrollBarEnabled()
7250 * @see #setHorizontalScrollBarEnabled(boolean)
7251 * @see #setVerticalScrollBarEnabled(boolean)
7252 */
7253 protected boolean awakenScrollBars() {
7254 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007255 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007256 }
7257
7258 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007259 * Trigger the scrollbars to draw.
7260 * This method differs from awakenScrollBars() only in its default duration.
7261 * initialAwakenScrollBars() will show the scroll bars for longer than
7262 * usual to give the user more of a chance to notice them.
7263 *
7264 * @return true if the animation is played, false otherwise.
7265 */
7266 private boolean initialAwakenScrollBars() {
7267 return mScrollCache != null &&
7268 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7269 }
7270
7271 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007272 * <p>
7273 * Trigger the scrollbars to draw. When invoked this method starts an
7274 * animation to fade the scrollbars out after a fixed delay. If a subclass
7275 * provides animated scrolling, the start delay should equal the duration of
7276 * the scrolling animation.
7277 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007278 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007279 * <p>
7280 * The animation starts only if at least one of the scrollbars is enabled,
7281 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7282 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7283 * this method returns true, and false otherwise. If the animation is
7284 * started, this method calls {@link #invalidate()}; in that case the caller
7285 * should not call {@link #invalidate()}.
7286 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007287 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007288 * <p>
7289 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007290 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007291 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007292 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007293 * @param startDelay the delay, in milliseconds, after which the animation
7294 * should start; when the delay is 0, the animation starts
7295 * immediately
7296 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007297 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007298 * @see #scrollBy(int, int)
7299 * @see #scrollTo(int, int)
7300 * @see #isHorizontalScrollBarEnabled()
7301 * @see #isVerticalScrollBarEnabled()
7302 * @see #setHorizontalScrollBarEnabled(boolean)
7303 * @see #setVerticalScrollBarEnabled(boolean)
7304 */
7305 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007306 return awakenScrollBars(startDelay, true);
7307 }
Joe Malin32736f02011-01-19 16:14:20 -08007308
Mike Cleron290947b2009-09-29 18:34:32 -07007309 /**
7310 * <p>
7311 * Trigger the scrollbars to draw. When invoked this method starts an
7312 * animation to fade the scrollbars out after a fixed delay. If a subclass
7313 * provides animated scrolling, the start delay should equal the duration of
7314 * the scrolling animation.
7315 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007316 *
Mike Cleron290947b2009-09-29 18:34:32 -07007317 * <p>
7318 * The animation starts only if at least one of the scrollbars is enabled,
7319 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7320 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7321 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007322 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007323 * is set to true; in that case the caller
7324 * should not call {@link #invalidate()}.
7325 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007326 *
Mike Cleron290947b2009-09-29 18:34:32 -07007327 * <p>
7328 * This method should be invoked everytime a subclass directly updates the
7329 * scroll parameters.
7330 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007331 *
Mike Cleron290947b2009-09-29 18:34:32 -07007332 * @param startDelay the delay, in milliseconds, after which the animation
7333 * should start; when the delay is 0, the animation starts
7334 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007335 *
Mike Cleron290947b2009-09-29 18:34:32 -07007336 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007337 *
Mike Cleron290947b2009-09-29 18:34:32 -07007338 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007339 *
Mike Cleron290947b2009-09-29 18:34:32 -07007340 * @see #scrollBy(int, int)
7341 * @see #scrollTo(int, int)
7342 * @see #isHorizontalScrollBarEnabled()
7343 * @see #isVerticalScrollBarEnabled()
7344 * @see #setHorizontalScrollBarEnabled(boolean)
7345 * @see #setVerticalScrollBarEnabled(boolean)
7346 */
7347 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007348 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007349
Mike Cleronf116bf82009-09-27 19:14:12 -07007350 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7351 return false;
7352 }
7353
7354 if (scrollCache.scrollBar == null) {
7355 scrollCache.scrollBar = new ScrollBarDrawable();
7356 }
7357
7358 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7359
Mike Cleron290947b2009-09-29 18:34:32 -07007360 if (invalidate) {
7361 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007362 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007363 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007364
7365 if (scrollCache.state == ScrollabilityCache.OFF) {
7366 // FIXME: this is copied from WindowManagerService.
7367 // We should get this value from the system when it
7368 // is possible to do so.
7369 final int KEY_REPEAT_FIRST_DELAY = 750;
7370 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7371 }
7372
7373 // Tell mScrollCache when we should start fading. This may
7374 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007375 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007376 scrollCache.fadeStartTime = fadeStartTime;
7377 scrollCache.state = ScrollabilityCache.ON;
7378
7379 // Schedule our fader to run, unscheduling any old ones first
7380 if (mAttachInfo != null) {
7381 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7382 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7383 }
7384
7385 return true;
7386 }
7387
7388 return false;
7389 }
7390
7391 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007392 * Mark the the area defined by dirty as needing to be drawn. If the view is
7393 * visible, {@link #onDraw} will be called at some point in the future.
7394 * This must be called from a UI thread. To call from a non-UI thread, call
7395 * {@link #postInvalidate()}.
7396 *
7397 * WARNING: This method is destructive to dirty.
7398 * @param dirty the rectangle representing the bounds of the dirty region
7399 */
7400 public void invalidate(Rect dirty) {
7401 if (ViewDebug.TRACE_HIERARCHY) {
7402 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7403 }
7404
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007405 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007406 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7407 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007408 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007409 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007410 final ViewParent p = mParent;
7411 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007412 //noinspection PointlessBooleanExpression,ConstantConditions
7413 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7414 if (p != null && ai != null && ai.mHardwareAccelerated) {
7415 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007416 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007417 p.invalidateChild(this, null);
7418 return;
7419 }
Romain Guyaf636eb2010-12-09 17:47:21 -08007420 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007421 if (p != null && ai != null) {
7422 final int scrollX = mScrollX;
7423 final int scrollY = mScrollY;
7424 final Rect r = ai.mTmpInvalRect;
7425 r.set(dirty.left - scrollX, dirty.top - scrollY,
7426 dirty.right - scrollX, dirty.bottom - scrollY);
7427 mParent.invalidateChild(this, r);
7428 }
7429 }
7430 }
7431
7432 /**
7433 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
7434 * The coordinates of the dirty rect are relative to the view.
7435 * If the view is visible, {@link #onDraw} will be called at some point
7436 * in the future. This must be called from a UI thread. To call
7437 * from a non-UI thread, call {@link #postInvalidate()}.
7438 * @param l the left position of the dirty region
7439 * @param t the top position of the dirty region
7440 * @param r the right position of the dirty region
7441 * @param b the bottom position of the dirty region
7442 */
7443 public void invalidate(int l, int t, int r, int b) {
7444 if (ViewDebug.TRACE_HIERARCHY) {
7445 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7446 }
7447
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007448 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007449 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7450 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007451 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007452 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007453 final ViewParent p = mParent;
7454 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08007455 //noinspection PointlessBooleanExpression,ConstantConditions
7456 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7457 if (p != null && ai != null && ai.mHardwareAccelerated) {
7458 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007459 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007460 p.invalidateChild(this, null);
7461 return;
7462 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007463 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007464 if (p != null && ai != null && l < r && t < b) {
7465 final int scrollX = mScrollX;
7466 final int scrollY = mScrollY;
7467 final Rect tmpr = ai.mTmpInvalRect;
7468 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
7469 p.invalidateChild(this, tmpr);
7470 }
7471 }
7472 }
7473
7474 /**
7475 * Invalidate the whole view. If the view is visible, {@link #onDraw} will
7476 * be called at some point in the future. This must be called from a
7477 * UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
7478 */
7479 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07007480 invalidate(true);
7481 }
Joe Malin32736f02011-01-19 16:14:20 -08007482
Chet Haaseed032702010-10-01 14:05:54 -07007483 /**
7484 * This is where the invalidate() work actually happens. A full invalidate()
7485 * causes the drawing cache to be invalidated, but this function can be called with
7486 * invalidateCache set to false to skip that invalidation step for cases that do not
7487 * need it (for example, a component that remains at the same dimensions with the same
7488 * content).
7489 *
7490 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
7491 * well. This is usually true for a full invalidate, but may be set to false if the
7492 * View's contents or dimensions have not changed.
7493 */
Romain Guy849d0a32011-02-01 17:20:48 -08007494 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007495 if (ViewDebug.TRACE_HIERARCHY) {
7496 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7497 }
7498
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007499 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08007500 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08007501 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
7502 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07007503 mPrivateFlags &= ~DRAWN;
7504 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08007505 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07007506 mPrivateFlags &= ~DRAWING_CACHE_VALID;
7507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007508 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07007509 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08007510 //noinspection PointlessBooleanExpression,ConstantConditions
7511 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
7512 if (p != null && ai != null && ai.mHardwareAccelerated) {
7513 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007514 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08007515 p.invalidateChild(this, null);
7516 return;
7517 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08007518 }
Michael Jurkaebefea42010-11-15 16:04:01 -08007519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007520 if (p != null && ai != null) {
7521 final Rect r = ai.mTmpInvalRect;
7522 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7523 // Don't call invalidate -- we don't want to internally scroll
7524 // our own bounds
7525 p.invalidateChild(this, r);
7526 }
7527 }
7528 }
7529
7530 /**
Romain Guyda489792011-02-03 01:05:15 -08007531 * @hide
7532 */
7533 public void fastInvalidate() {
7534 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
7535 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7536 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
7537 if (mParent instanceof View) {
7538 ((View) mParent).mPrivateFlags |= INVALIDATED;
7539 }
7540 mPrivateFlags &= ~DRAWN;
7541 mPrivateFlags |= INVALIDATED;
7542 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07007543 if (mParent != null && mAttachInfo != null) {
7544 if (mAttachInfo.mHardwareAccelerated) {
7545 mParent.invalidateChild(this, null);
7546 } else {
7547 final Rect r = mAttachInfo.mTmpInvalRect;
7548 r.set(0, 0, mRight - mLeft, mBottom - mTop);
7549 // Don't call invalidate -- we don't want to internally scroll
7550 // our own bounds
7551 mParent.invalidateChild(this, r);
7552 }
Romain Guyda489792011-02-03 01:05:15 -08007553 }
7554 }
7555 }
7556
7557 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08007558 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08007559 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7560 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08007561 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
7562 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08007563 *
7564 * @hide
7565 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08007566 protected void invalidateParentCaches() {
7567 if (mParent instanceof View) {
7568 ((View) mParent).mPrivateFlags |= INVALIDATED;
7569 }
7570 }
Joe Malin32736f02011-01-19 16:14:20 -08007571
Romain Guy0fd89bf2011-01-26 15:41:30 -08007572 /**
7573 * Used to indicate that the parent of this view should be invalidated. This functionality
7574 * is used to force the parent to rebuild its display list (when hardware-accelerated),
7575 * which is necessary when various parent-managed properties of the view change, such as
7576 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
7577 * an invalidation event to the parent.
7578 *
7579 * @hide
7580 */
7581 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08007582 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007583 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08007584 }
7585 }
7586
7587 /**
Romain Guy24443ea2009-05-11 11:56:30 -07007588 * Indicates whether this View is opaque. An opaque View guarantees that it will
7589 * draw all the pixels overlapping its bounds using a fully opaque color.
7590 *
7591 * Subclasses of View should override this method whenever possible to indicate
7592 * whether an instance is opaque. Opaque Views are treated in a special way by
7593 * the View hierarchy, possibly allowing it to perform optimizations during
7594 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07007595 *
Romain Guy24443ea2009-05-11 11:56:30 -07007596 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07007597 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07007598 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07007599 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07007600 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
7601 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07007602 }
7603
Adam Powell20232d02010-12-08 21:08:53 -08007604 /**
7605 * @hide
7606 */
7607 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07007608 // Opaque if:
7609 // - Has a background
7610 // - Background is opaque
7611 // - Doesn't have scrollbars or scrollbars are inside overlay
7612
7613 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
7614 mPrivateFlags |= OPAQUE_BACKGROUND;
7615 } else {
7616 mPrivateFlags &= ~OPAQUE_BACKGROUND;
7617 }
7618
7619 final int flags = mViewFlags;
7620 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
7621 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
7622 mPrivateFlags |= OPAQUE_SCROLLBARS;
7623 } else {
7624 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
7625 }
7626 }
7627
7628 /**
7629 * @hide
7630 */
7631 protected boolean hasOpaqueScrollbars() {
7632 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07007633 }
7634
7635 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007636 * @return A handler associated with the thread running the View. This
7637 * handler can be used to pump events in the UI events queue.
7638 */
7639 public Handler getHandler() {
7640 if (mAttachInfo != null) {
7641 return mAttachInfo.mHandler;
7642 }
7643 return null;
7644 }
7645
7646 /**
7647 * Causes the Runnable to be added to the message queue.
7648 * The runnable will be run on the user interface thread.
7649 *
7650 * @param action The Runnable that will be executed.
7651 *
7652 * @return Returns true if the Runnable was successfully placed in to the
7653 * message queue. Returns false on failure, usually because the
7654 * looper processing the message queue is exiting.
7655 */
7656 public boolean post(Runnable action) {
7657 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007658 AttachInfo attachInfo = mAttachInfo;
7659 if (attachInfo != null) {
7660 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007661 } else {
7662 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007663 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007664 return true;
7665 }
7666
7667 return handler.post(action);
7668 }
7669
7670 /**
7671 * Causes the Runnable to be added to the message queue, to be run
7672 * after the specified amount of time elapses.
7673 * The runnable will be run on the user interface thread.
7674 *
7675 * @param action The Runnable that will be executed.
7676 * @param delayMillis The delay (in milliseconds) until the Runnable
7677 * will be executed.
7678 *
7679 * @return true if the Runnable was successfully placed in to the
7680 * message queue. Returns false on failure, usually because the
7681 * looper processing the message queue is exiting. Note that a
7682 * result of true does not mean the Runnable will be processed --
7683 * if the looper is quit before the delivery time of the message
7684 * occurs then the message will be dropped.
7685 */
7686 public boolean postDelayed(Runnable action, long delayMillis) {
7687 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007688 AttachInfo attachInfo = mAttachInfo;
7689 if (attachInfo != null) {
7690 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007691 } else {
7692 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007693 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007694 return true;
7695 }
7696
7697 return handler.postDelayed(action, delayMillis);
7698 }
7699
7700 /**
7701 * Removes the specified Runnable from the message queue.
7702 *
7703 * @param action The Runnable to remove from the message handling queue
7704 *
7705 * @return true if this view could ask the Handler to remove the Runnable,
7706 * false otherwise. When the returned value is true, the Runnable
7707 * may or may not have been actually removed from the message queue
7708 * (for instance, if the Runnable was not in the queue already.)
7709 */
7710 public boolean removeCallbacks(Runnable action) {
7711 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07007712 AttachInfo attachInfo = mAttachInfo;
7713 if (attachInfo != null) {
7714 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007715 } else {
7716 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07007717 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007718 return true;
7719 }
7720
7721 handler.removeCallbacks(action);
7722 return true;
7723 }
7724
7725 /**
7726 * Cause an invalidate to happen on a subsequent cycle through the event loop.
7727 * Use this to invalidate the View from a non-UI thread.
7728 *
7729 * @see #invalidate()
7730 */
7731 public void postInvalidate() {
7732 postInvalidateDelayed(0);
7733 }
7734
7735 /**
7736 * Cause an invalidate of the specified area to happen on a subsequent cycle
7737 * through the event loop. Use this to invalidate the View from a non-UI thread.
7738 *
7739 * @param left The left coordinate of the rectangle to invalidate.
7740 * @param top The top coordinate of the rectangle to invalidate.
7741 * @param right The right coordinate of the rectangle to invalidate.
7742 * @param bottom The bottom coordinate of the rectangle to invalidate.
7743 *
7744 * @see #invalidate(int, int, int, int)
7745 * @see #invalidate(Rect)
7746 */
7747 public void postInvalidate(int left, int top, int right, int bottom) {
7748 postInvalidateDelayed(0, left, top, right, bottom);
7749 }
7750
7751 /**
7752 * Cause an invalidate to happen on a subsequent cycle through the event
7753 * loop. Waits for the specified amount of time.
7754 *
7755 * @param delayMilliseconds the duration in milliseconds to delay the
7756 * invalidation by
7757 */
7758 public void postInvalidateDelayed(long delayMilliseconds) {
7759 // We try only with the AttachInfo because there's no point in invalidating
7760 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007761 AttachInfo attachInfo = mAttachInfo;
7762 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007763 Message msg = Message.obtain();
7764 msg.what = AttachInfo.INVALIDATE_MSG;
7765 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07007766 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 }
7768 }
7769
7770 /**
7771 * Cause an invalidate of the specified area to happen on a subsequent cycle
7772 * through the event loop. Waits for the specified amount of time.
7773 *
7774 * @param delayMilliseconds the duration in milliseconds to delay the
7775 * invalidation by
7776 * @param left The left coordinate of the rectangle to invalidate.
7777 * @param top The top coordinate of the rectangle to invalidate.
7778 * @param right The right coordinate of the rectangle to invalidate.
7779 * @param bottom The bottom coordinate of the rectangle to invalidate.
7780 */
7781 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
7782 int right, int bottom) {
7783
7784 // We try only with the AttachInfo because there's no point in invalidating
7785 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07007786 AttachInfo attachInfo = mAttachInfo;
7787 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007788 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
7789 info.target = this;
7790 info.left = left;
7791 info.top = top;
7792 info.right = right;
7793 info.bottom = bottom;
7794
7795 final Message msg = Message.obtain();
7796 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
7797 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07007798 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007799 }
7800 }
7801
7802 /**
7803 * Called by a parent to request that a child update its values for mScrollX
7804 * and mScrollY if necessary. This will typically be done if the child is
7805 * animating a scroll using a {@link android.widget.Scroller Scroller}
7806 * object.
7807 */
7808 public void computeScroll() {
7809 }
7810
7811 /**
7812 * <p>Indicate whether the horizontal edges are faded when the view is
7813 * scrolled horizontally.</p>
7814 *
7815 * @return true if the horizontal edges should are faded on scroll, false
7816 * otherwise
7817 *
7818 * @see #setHorizontalFadingEdgeEnabled(boolean)
7819 * @attr ref android.R.styleable#View_fadingEdge
7820 */
7821 public boolean isHorizontalFadingEdgeEnabled() {
7822 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
7823 }
7824
7825 /**
7826 * <p>Define whether the horizontal edges should be faded when this view
7827 * is scrolled horizontally.</p>
7828 *
7829 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
7830 * be faded when the view is scrolled
7831 * horizontally
7832 *
7833 * @see #isHorizontalFadingEdgeEnabled()
7834 * @attr ref android.R.styleable#View_fadingEdge
7835 */
7836 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
7837 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
7838 if (horizontalFadingEdgeEnabled) {
7839 initScrollCache();
7840 }
7841
7842 mViewFlags ^= FADING_EDGE_HORIZONTAL;
7843 }
7844 }
7845
7846 /**
7847 * <p>Indicate whether the vertical edges are faded when the view is
7848 * scrolled horizontally.</p>
7849 *
7850 * @return true if the vertical edges should are faded on scroll, false
7851 * otherwise
7852 *
7853 * @see #setVerticalFadingEdgeEnabled(boolean)
7854 * @attr ref android.R.styleable#View_fadingEdge
7855 */
7856 public boolean isVerticalFadingEdgeEnabled() {
7857 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
7858 }
7859
7860 /**
7861 * <p>Define whether the vertical edges should be faded when this view
7862 * is scrolled vertically.</p>
7863 *
7864 * @param verticalFadingEdgeEnabled true if the vertical edges should
7865 * be faded when the view is scrolled
7866 * vertically
7867 *
7868 * @see #isVerticalFadingEdgeEnabled()
7869 * @attr ref android.R.styleable#View_fadingEdge
7870 */
7871 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
7872 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
7873 if (verticalFadingEdgeEnabled) {
7874 initScrollCache();
7875 }
7876
7877 mViewFlags ^= FADING_EDGE_VERTICAL;
7878 }
7879 }
7880
7881 /**
7882 * Returns the strength, or intensity, of the top faded edge. The strength is
7883 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7884 * returns 0.0 or 1.0 but no value in between.
7885 *
7886 * Subclasses should override this method to provide a smoother fade transition
7887 * when scrolling occurs.
7888 *
7889 * @return the intensity of the top fade as a float between 0.0f and 1.0f
7890 */
7891 protected float getTopFadingEdgeStrength() {
7892 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
7893 }
7894
7895 /**
7896 * Returns the strength, or intensity, of the bottom faded edge. The strength is
7897 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7898 * returns 0.0 or 1.0 but no value in between.
7899 *
7900 * Subclasses should override this method to provide a smoother fade transition
7901 * when scrolling occurs.
7902 *
7903 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
7904 */
7905 protected float getBottomFadingEdgeStrength() {
7906 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
7907 computeVerticalScrollRange() ? 1.0f : 0.0f;
7908 }
7909
7910 /**
7911 * Returns the strength, or intensity, of the left faded edge. The strength is
7912 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7913 * returns 0.0 or 1.0 but no value in between.
7914 *
7915 * Subclasses should override this method to provide a smoother fade transition
7916 * when scrolling occurs.
7917 *
7918 * @return the intensity of the left fade as a float between 0.0f and 1.0f
7919 */
7920 protected float getLeftFadingEdgeStrength() {
7921 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
7922 }
7923
7924 /**
7925 * Returns the strength, or intensity, of the right faded edge. The strength is
7926 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
7927 * returns 0.0 or 1.0 but no value in between.
7928 *
7929 * Subclasses should override this method to provide a smoother fade transition
7930 * when scrolling occurs.
7931 *
7932 * @return the intensity of the right fade as a float between 0.0f and 1.0f
7933 */
7934 protected float getRightFadingEdgeStrength() {
7935 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
7936 computeHorizontalScrollRange() ? 1.0f : 0.0f;
7937 }
7938
7939 /**
7940 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
7941 * scrollbar is not drawn by default.</p>
7942 *
7943 * @return true if the horizontal scrollbar should be painted, false
7944 * otherwise
7945 *
7946 * @see #setHorizontalScrollBarEnabled(boolean)
7947 */
7948 public boolean isHorizontalScrollBarEnabled() {
7949 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
7950 }
7951
7952 /**
7953 * <p>Define whether the horizontal scrollbar should be drawn or not. The
7954 * scrollbar is not drawn by default.</p>
7955 *
7956 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
7957 * be painted
7958 *
7959 * @see #isHorizontalScrollBarEnabled()
7960 */
7961 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
7962 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
7963 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007964 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007965 recomputePadding();
7966 }
7967 }
7968
7969 /**
7970 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
7971 * scrollbar is not drawn by default.</p>
7972 *
7973 * @return true if the vertical scrollbar should be painted, false
7974 * otherwise
7975 *
7976 * @see #setVerticalScrollBarEnabled(boolean)
7977 */
7978 public boolean isVerticalScrollBarEnabled() {
7979 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
7980 }
7981
7982 /**
7983 * <p>Define whether the vertical scrollbar should be drawn or not. The
7984 * scrollbar is not drawn by default.</p>
7985 *
7986 * @param verticalScrollBarEnabled true if the vertical scrollbar should
7987 * be painted
7988 *
7989 * @see #isVerticalScrollBarEnabled()
7990 */
7991 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
7992 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
7993 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07007994 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007995 recomputePadding();
7996 }
7997 }
7998
Adam Powell20232d02010-12-08 21:08:53 -08007999 /**
8000 * @hide
8001 */
8002 protected void recomputePadding() {
8003 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 }
Joe Malin32736f02011-01-19 16:14:20 -08008005
Mike Cleronfe81d382009-09-28 14:22:16 -07008006 /**
8007 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008008 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008009 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008010 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008011 */
8012 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8013 initScrollCache();
8014 final ScrollabilityCache scrollabilityCache = mScrollCache;
8015 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008016 if (fadeScrollbars) {
8017 scrollabilityCache.state = ScrollabilityCache.OFF;
8018 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008019 scrollabilityCache.state = ScrollabilityCache.ON;
8020 }
8021 }
Joe Malin32736f02011-01-19 16:14:20 -08008022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008023 /**
Joe Malin32736f02011-01-19 16:14:20 -08008024 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008025 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008026 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008027 * @return true if scrollbar fading is enabled
8028 */
8029 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008030 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008031 }
Joe Malin32736f02011-01-19 16:14:20 -08008032
Mike Cleron52f0a642009-09-28 18:21:37 -07008033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8035 * inset. When inset, they add to the padding of the view. And the scrollbars
8036 * can be drawn inside the padding area or on the edge of the view. For example,
8037 * if a view has a background drawable and you want to draw the scrollbars
8038 * inside the padding specified by the drawable, you can use
8039 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8040 * appear at the edge of the view, ignoring the padding, then you can use
8041 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8042 * @param style the style of the scrollbars. Should be one of
8043 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8044 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8045 * @see #SCROLLBARS_INSIDE_OVERLAY
8046 * @see #SCROLLBARS_INSIDE_INSET
8047 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8048 * @see #SCROLLBARS_OUTSIDE_INSET
8049 */
8050 public void setScrollBarStyle(int style) {
8051 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8052 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008053 computeOpaqueFlags();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 recomputePadding();
8055 }
8056 }
8057
8058 /**
8059 * <p>Returns the current scrollbar style.</p>
8060 * @return the current scrollbar style
8061 * @see #SCROLLBARS_INSIDE_OVERLAY
8062 * @see #SCROLLBARS_INSIDE_INSET
8063 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8064 * @see #SCROLLBARS_OUTSIDE_INSET
8065 */
8066 public int getScrollBarStyle() {
8067 return mViewFlags & SCROLLBARS_STYLE_MASK;
8068 }
8069
8070 /**
8071 * <p>Compute the horizontal range that the horizontal scrollbar
8072 * represents.</p>
8073 *
8074 * <p>The range is expressed in arbitrary units that must be the same as the
8075 * units used by {@link #computeHorizontalScrollExtent()} and
8076 * {@link #computeHorizontalScrollOffset()}.</p>
8077 *
8078 * <p>The default range is the drawing width of this view.</p>
8079 *
8080 * @return the total horizontal range represented by the horizontal
8081 * scrollbar
8082 *
8083 * @see #computeHorizontalScrollExtent()
8084 * @see #computeHorizontalScrollOffset()
8085 * @see android.widget.ScrollBarDrawable
8086 */
8087 protected int computeHorizontalScrollRange() {
8088 return getWidth();
8089 }
8090
8091 /**
8092 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8093 * within the horizontal range. This value is used to compute the position
8094 * of the thumb within the scrollbar's track.</p>
8095 *
8096 * <p>The range is expressed in arbitrary units that must be the same as the
8097 * units used by {@link #computeHorizontalScrollRange()} and
8098 * {@link #computeHorizontalScrollExtent()}.</p>
8099 *
8100 * <p>The default offset is the scroll offset of this view.</p>
8101 *
8102 * @return the horizontal offset of the scrollbar's thumb
8103 *
8104 * @see #computeHorizontalScrollRange()
8105 * @see #computeHorizontalScrollExtent()
8106 * @see android.widget.ScrollBarDrawable
8107 */
8108 protected int computeHorizontalScrollOffset() {
8109 return mScrollX;
8110 }
8111
8112 /**
8113 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8114 * within the horizontal range. This value is used to compute the length
8115 * of the thumb within the scrollbar's track.</p>
8116 *
8117 * <p>The range is expressed in arbitrary units that must be the same as the
8118 * units used by {@link #computeHorizontalScrollRange()} and
8119 * {@link #computeHorizontalScrollOffset()}.</p>
8120 *
8121 * <p>The default extent is the drawing width of this view.</p>
8122 *
8123 * @return the horizontal extent of the scrollbar's thumb
8124 *
8125 * @see #computeHorizontalScrollRange()
8126 * @see #computeHorizontalScrollOffset()
8127 * @see android.widget.ScrollBarDrawable
8128 */
8129 protected int computeHorizontalScrollExtent() {
8130 return getWidth();
8131 }
8132
8133 /**
8134 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8135 *
8136 * <p>The range is expressed in arbitrary units that must be the same as the
8137 * units used by {@link #computeVerticalScrollExtent()} and
8138 * {@link #computeVerticalScrollOffset()}.</p>
8139 *
8140 * @return the total vertical range represented by the vertical scrollbar
8141 *
8142 * <p>The default range is the drawing height of this view.</p>
8143 *
8144 * @see #computeVerticalScrollExtent()
8145 * @see #computeVerticalScrollOffset()
8146 * @see android.widget.ScrollBarDrawable
8147 */
8148 protected int computeVerticalScrollRange() {
8149 return getHeight();
8150 }
8151
8152 /**
8153 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8154 * within the horizontal range. This value is used to compute the position
8155 * of the thumb within the scrollbar's track.</p>
8156 *
8157 * <p>The range is expressed in arbitrary units that must be the same as the
8158 * units used by {@link #computeVerticalScrollRange()} and
8159 * {@link #computeVerticalScrollExtent()}.</p>
8160 *
8161 * <p>The default offset is the scroll offset of this view.</p>
8162 *
8163 * @return the vertical offset of the scrollbar's thumb
8164 *
8165 * @see #computeVerticalScrollRange()
8166 * @see #computeVerticalScrollExtent()
8167 * @see android.widget.ScrollBarDrawable
8168 */
8169 protected int computeVerticalScrollOffset() {
8170 return mScrollY;
8171 }
8172
8173 /**
8174 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8175 * within the vertical range. This value is used to compute the length
8176 * of the thumb within the scrollbar's track.</p>
8177 *
8178 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008179 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008180 * {@link #computeVerticalScrollOffset()}.</p>
8181 *
8182 * <p>The default extent is the drawing height of this view.</p>
8183 *
8184 * @return the vertical extent of the scrollbar's thumb
8185 *
8186 * @see #computeVerticalScrollRange()
8187 * @see #computeVerticalScrollOffset()
8188 * @see android.widget.ScrollBarDrawable
8189 */
8190 protected int computeVerticalScrollExtent() {
8191 return getHeight();
8192 }
8193
8194 /**
8195 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8196 * scrollbars are painted only if they have been awakened first.</p>
8197 *
8198 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008199 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008200 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008201 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008202 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008203 // scrollbars are drawn only when the animation is running
8204 final ScrollabilityCache cache = mScrollCache;
8205 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008206
Mike Cleronf116bf82009-09-27 19:14:12 -07008207 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008208
Mike Cleronf116bf82009-09-27 19:14:12 -07008209 if (state == ScrollabilityCache.OFF) {
8210 return;
8211 }
Joe Malin32736f02011-01-19 16:14:20 -08008212
Mike Cleronf116bf82009-09-27 19:14:12 -07008213 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008214
Mike Cleronf116bf82009-09-27 19:14:12 -07008215 if (state == ScrollabilityCache.FADING) {
8216 // We're fading -- get our fade interpolation
8217 if (cache.interpolatorValues == null) {
8218 cache.interpolatorValues = new float[1];
8219 }
Joe Malin32736f02011-01-19 16:14:20 -08008220
Mike Cleronf116bf82009-09-27 19:14:12 -07008221 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008222
Mike Cleronf116bf82009-09-27 19:14:12 -07008223 // Stops the animation if we're done
8224 if (cache.scrollBarInterpolator.timeToValues(values) ==
8225 Interpolator.Result.FREEZE_END) {
8226 cache.state = ScrollabilityCache.OFF;
8227 } else {
8228 cache.scrollBar.setAlpha(Math.round(values[0]));
8229 }
Joe Malin32736f02011-01-19 16:14:20 -08008230
8231 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008232 // drawing. We only want this when we're fading so that
8233 // we prevent excessive redraws
8234 invalidate = true;
8235 } else {
8236 // We're just on -- but we may have been fading before so
8237 // reset alpha
8238 cache.scrollBar.setAlpha(255);
8239 }
8240
Joe Malin32736f02011-01-19 16:14:20 -08008241
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008242 final int viewFlags = mViewFlags;
8243
8244 final boolean drawHorizontalScrollBar =
8245 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8246 final boolean drawVerticalScrollBar =
8247 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8248 && !isVerticalScrollBarHidden();
8249
8250 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8251 final int width = mRight - mLeft;
8252 final int height = mBottom - mTop;
8253
8254 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255
Mike Reede8853fc2009-09-04 14:01:48 -04008256 final int scrollX = mScrollX;
8257 final int scrollY = mScrollY;
8258 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8259
Mike Cleronf116bf82009-09-27 19:14:12 -07008260 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008261
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008262 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008263 int size = scrollBar.getSize(false);
8264 if (size <= 0) {
8265 size = cache.scrollBarSize;
8266 }
8267
Mike Cleronf116bf82009-09-27 19:14:12 -07008268 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008269 computeHorizontalScrollOffset(),
8270 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008271 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008272 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008273 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008274 left = scrollX + (mPaddingLeft & inside);
8275 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8276 bottom = top + size;
8277 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8278 if (invalidate) {
8279 invalidate(left, top, right, bottom);
8280 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008281 }
8282
8283 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008284 int size = scrollBar.getSize(true);
8285 if (size <= 0) {
8286 size = cache.scrollBarSize;
8287 }
8288
Mike Reede8853fc2009-09-04 14:01:48 -04008289 scrollBar.setParameters(computeVerticalScrollRange(),
8290 computeVerticalScrollOffset(),
8291 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008292 switch (mVerticalScrollbarPosition) {
8293 default:
8294 case SCROLLBAR_POSITION_DEFAULT:
8295 case SCROLLBAR_POSITION_RIGHT:
8296 left = scrollX + width - size - (mUserPaddingRight & inside);
8297 break;
8298 case SCROLLBAR_POSITION_LEFT:
8299 left = scrollX + (mUserPaddingLeft & inside);
8300 break;
8301 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008302 top = scrollY + (mPaddingTop & inside);
8303 right = left + size;
8304 bottom = scrollY + height - (mUserPaddingBottom & inside);
8305 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8306 if (invalidate) {
8307 invalidate(left, top, right, bottom);
8308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 }
8310 }
8311 }
8312 }
Romain Guy8506ab42009-06-11 17:35:47 -07008313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008314 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008315 * 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 -08008316 * FastScroller is visible.
8317 * @return whether to temporarily hide the vertical scrollbar
8318 * @hide
8319 */
8320 protected boolean isVerticalScrollBarHidden() {
8321 return false;
8322 }
8323
8324 /**
8325 * <p>Draw the horizontal scrollbar if
8326 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008328 * @param canvas the canvas on which to draw the scrollbar
8329 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008330 *
8331 * @see #isHorizontalScrollBarEnabled()
8332 * @see #computeHorizontalScrollRange()
8333 * @see #computeHorizontalScrollExtent()
8334 * @see #computeHorizontalScrollOffset()
8335 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008336 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008337 */
Romain Guy8fb95422010-08-17 18:38:51 -07008338 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8339 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008340 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008341 scrollBar.draw(canvas);
8342 }
Mike Reede8853fc2009-09-04 14:01:48 -04008343
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008344 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008345 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8346 * returns true.</p>
8347 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008348 * @param canvas the canvas on which to draw the scrollbar
8349 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008350 *
8351 * @see #isVerticalScrollBarEnabled()
8352 * @see #computeVerticalScrollRange()
8353 * @see #computeVerticalScrollExtent()
8354 * @see #computeVerticalScrollOffset()
8355 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008356 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008357 */
Romain Guy8fb95422010-08-17 18:38:51 -07008358 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
8359 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04008360 scrollBar.setBounds(l, t, r, b);
8361 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008362 }
8363
8364 /**
8365 * Implement this to do your drawing.
8366 *
8367 * @param canvas the canvas on which the background will be drawn
8368 */
8369 protected void onDraw(Canvas canvas) {
8370 }
8371
8372 /*
8373 * Caller is responsible for calling requestLayout if necessary.
8374 * (This allows addViewInLayout to not request a new layout.)
8375 */
8376 void assignParent(ViewParent parent) {
8377 if (mParent == null) {
8378 mParent = parent;
8379 } else if (parent == null) {
8380 mParent = null;
8381 } else {
8382 throw new RuntimeException("view " + this + " being added, but"
8383 + " it already has a parent");
8384 }
8385 }
8386
8387 /**
8388 * This is called when the view is attached to a window. At this point it
8389 * has a Surface and will start drawing. Note that this function is
8390 * guaranteed to be called before {@link #onDraw}, however it may be called
8391 * any time before the first onDraw -- including before or after
8392 * {@link #onMeasure}.
8393 *
8394 * @see #onDetachedFromWindow()
8395 */
8396 protected void onAttachedToWindow() {
8397 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
8398 mParent.requestTransparentRegion(this);
8399 }
Adam Powell8568c3a2010-04-19 14:26:11 -07008400 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
8401 initialAwakenScrollBars();
8402 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
8403 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08008404 jumpDrawablesToCurrentState();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008405 }
8406
8407 /**
8408 * This is called when the view is detached from a window. At this point it
8409 * no longer has a surface for drawing.
8410 *
8411 * @see #onAttachedToWindow()
8412 */
8413 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08008414 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08008415
Romain Guya440b002010-02-24 15:57:54 -08008416 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05008417 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08008418 removePerformClickCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08008419
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008420 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08008421
8422 if (mHardwareLayer != null) {
8423 mHardwareLayer.destroy();
8424 mHardwareLayer = null;
8425 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008426
Chet Haasedaf98e92011-01-10 14:10:36 -08008427 if (mDisplayList != null) {
8428 mDisplayList.invalidate();
8429 }
8430
Romain Guy8dd5b1e2011-01-14 17:28:51 -08008431 if (mAttachInfo != null) {
8432 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
8433 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
8434 }
8435
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08008436 mCurrentAnimation = null;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008437 }
8438
8439 /**
8440 * @return The number of times this view has been attached to a window
8441 */
8442 protected int getWindowAttachCount() {
8443 return mWindowAttachCount;
8444 }
8445
8446 /**
8447 * Retrieve a unique token identifying the window this view is attached to.
8448 * @return Return the window's token for use in
8449 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
8450 */
8451 public IBinder getWindowToken() {
8452 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
8453 }
8454
8455 /**
8456 * Retrieve a unique token identifying the top-level "real" window of
8457 * the window that this view is attached to. That is, this is like
8458 * {@link #getWindowToken}, except if the window this view in is a panel
8459 * window (attached to another containing window), then the token of
8460 * the containing window is returned instead.
8461 *
8462 * @return Returns the associated window token, either
8463 * {@link #getWindowToken()} or the containing window's token.
8464 */
8465 public IBinder getApplicationWindowToken() {
8466 AttachInfo ai = mAttachInfo;
8467 if (ai != null) {
8468 IBinder appWindowToken = ai.mPanelParentWindowToken;
8469 if (appWindowToken == null) {
8470 appWindowToken = ai.mWindowToken;
8471 }
8472 return appWindowToken;
8473 }
8474 return null;
8475 }
8476
8477 /**
8478 * Retrieve private session object this view hierarchy is using to
8479 * communicate with the window manager.
8480 * @return the session object to communicate with the window manager
8481 */
8482 /*package*/ IWindowSession getWindowSession() {
8483 return mAttachInfo != null ? mAttachInfo.mSession : null;
8484 }
8485
8486 /**
8487 * @param info the {@link android.view.View.AttachInfo} to associated with
8488 * this view
8489 */
8490 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
8491 //System.out.println("Attached! " + this);
8492 mAttachInfo = info;
8493 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008494 // We will need to evaluate the drawable state at least once.
8495 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 if (mFloatingTreeObserver != null) {
8497 info.mTreeObserver.merge(mFloatingTreeObserver);
8498 mFloatingTreeObserver = null;
8499 }
8500 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
8501 mAttachInfo.mScrollContainers.add(this);
8502 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
8503 }
8504 performCollectViewAttributes(visibility);
8505 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08008506
8507 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8508 mOnAttachStateChangeListeners;
8509 if (listeners != null && listeners.size() > 0) {
8510 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8511 // perform the dispatching. The iterator is a safe guard against listeners that
8512 // could mutate the list by calling the various add/remove methods. This prevents
8513 // the array from being modified while we iterate it.
8514 for (OnAttachStateChangeListener listener : listeners) {
8515 listener.onViewAttachedToWindow(this);
8516 }
8517 }
8518
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008519 int vis = info.mWindowVisibility;
8520 if (vis != GONE) {
8521 onWindowVisibilityChanged(vis);
8522 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08008523 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
8524 // If nobody has evaluated the drawable state yet, then do it now.
8525 refreshDrawableState();
8526 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008527 }
8528
8529 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008530 AttachInfo info = mAttachInfo;
8531 if (info != null) {
8532 int vis = info.mWindowVisibility;
8533 if (vis != GONE) {
8534 onWindowVisibilityChanged(GONE);
8535 }
8536 }
8537
8538 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08008539
Adam Powell4afd62b2011-02-18 15:02:18 -08008540 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
8541 mOnAttachStateChangeListeners;
8542 if (listeners != null && listeners.size() > 0) {
8543 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
8544 // perform the dispatching. The iterator is a safe guard against listeners that
8545 // could mutate the list by calling the various add/remove methods. This prevents
8546 // the array from being modified while we iterate it.
8547 for (OnAttachStateChangeListener listener : listeners) {
8548 listener.onViewDetachedFromWindow(this);
8549 }
8550 }
8551
Romain Guy01d5edc2011-01-28 11:28:53 -08008552 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008553 mAttachInfo.mScrollContainers.remove(this);
8554 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
8555 }
Romain Guy01d5edc2011-01-28 11:28:53 -08008556
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 mAttachInfo = null;
8558 }
8559
8560 /**
8561 * Store this view hierarchy's frozen state into the given container.
8562 *
8563 * @param container The SparseArray in which to save the view's state.
8564 *
8565 * @see #restoreHierarchyState
8566 * @see #dispatchSaveInstanceState
8567 * @see #onSaveInstanceState
8568 */
8569 public void saveHierarchyState(SparseArray<Parcelable> container) {
8570 dispatchSaveInstanceState(container);
8571 }
8572
8573 /**
8574 * Called by {@link #saveHierarchyState} to store the state for this view and its children.
8575 * May be overridden to modify how freezing happens to a view's children; for example, some
8576 * views may want to not store state for their children.
8577 *
8578 * @param container The SparseArray in which to save the view's state.
8579 *
8580 * @see #dispatchRestoreInstanceState
8581 * @see #saveHierarchyState
8582 * @see #onSaveInstanceState
8583 */
8584 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
8585 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
8586 mPrivateFlags &= ~SAVE_STATE_CALLED;
8587 Parcelable state = onSaveInstanceState();
8588 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8589 throw new IllegalStateException(
8590 "Derived class did not call super.onSaveInstanceState()");
8591 }
8592 if (state != null) {
8593 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
8594 // + ": " + state);
8595 container.put(mID, state);
8596 }
8597 }
8598 }
8599
8600 /**
8601 * Hook allowing a view to generate a representation of its internal state
8602 * that can later be used to create a new instance with that same state.
8603 * This state should only contain information that is not persistent or can
8604 * not be reconstructed later. For example, you will never store your
8605 * current position on screen because that will be computed again when a
8606 * new instance of the view is placed in its view hierarchy.
8607 * <p>
8608 * Some examples of things you may store here: the current cursor position
8609 * in a text view (but usually not the text itself since that is stored in a
8610 * content provider or other persistent storage), the currently selected
8611 * item in a list view.
8612 *
8613 * @return Returns a Parcelable object containing the view's current dynamic
8614 * state, or null if there is nothing interesting to save. The
8615 * default implementation returns null.
8616 * @see #onRestoreInstanceState
8617 * @see #saveHierarchyState
8618 * @see #dispatchSaveInstanceState
8619 * @see #setSaveEnabled(boolean)
8620 */
8621 protected Parcelable onSaveInstanceState() {
8622 mPrivateFlags |= SAVE_STATE_CALLED;
8623 return BaseSavedState.EMPTY_STATE;
8624 }
8625
8626 /**
8627 * Restore this view hierarchy's frozen state from the given container.
8628 *
8629 * @param container The SparseArray which holds previously frozen states.
8630 *
8631 * @see #saveHierarchyState
8632 * @see #dispatchRestoreInstanceState
8633 * @see #onRestoreInstanceState
8634 */
8635 public void restoreHierarchyState(SparseArray<Parcelable> container) {
8636 dispatchRestoreInstanceState(container);
8637 }
8638
8639 /**
8640 * Called by {@link #restoreHierarchyState} to retrieve the state for this view and its
8641 * children. May be overridden to modify how restoreing happens to a view's children; for
8642 * example, some views may want to not store state for their children.
8643 *
8644 * @param container The SparseArray which holds previously saved state.
8645 *
8646 * @see #dispatchSaveInstanceState
8647 * @see #restoreHierarchyState
8648 * @see #onRestoreInstanceState
8649 */
8650 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
8651 if (mID != NO_ID) {
8652 Parcelable state = container.get(mID);
8653 if (state != null) {
8654 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
8655 // + ": " + state);
8656 mPrivateFlags &= ~SAVE_STATE_CALLED;
8657 onRestoreInstanceState(state);
8658 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
8659 throw new IllegalStateException(
8660 "Derived class did not call super.onRestoreInstanceState()");
8661 }
8662 }
8663 }
8664 }
8665
8666 /**
8667 * Hook allowing a view to re-apply a representation of its internal state that had previously
8668 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
8669 * null state.
8670 *
8671 * @param state The frozen state that had previously been returned by
8672 * {@link #onSaveInstanceState}.
8673 *
8674 * @see #onSaveInstanceState
8675 * @see #restoreHierarchyState
8676 * @see #dispatchRestoreInstanceState
8677 */
8678 protected void onRestoreInstanceState(Parcelable state) {
8679 mPrivateFlags |= SAVE_STATE_CALLED;
8680 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08008681 throw new IllegalArgumentException("Wrong state class, expecting View State but "
8682 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08008683 + "when two views of different type have the same id in the same hierarchy. "
8684 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08008685 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008686 }
8687 }
8688
8689 /**
8690 * <p>Return the time at which the drawing of the view hierarchy started.</p>
8691 *
8692 * @return the drawing start time in milliseconds
8693 */
8694 public long getDrawingTime() {
8695 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
8696 }
8697
8698 /**
8699 * <p>Enables or disables the duplication of the parent's state into this view. When
8700 * duplication is enabled, this view gets its drawable state from its parent rather
8701 * than from its own internal properties.</p>
8702 *
8703 * <p>Note: in the current implementation, setting this property to true after the
8704 * view was added to a ViewGroup might have no effect at all. This property should
8705 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
8706 *
8707 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
8708 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008709 *
Gilles Debunnefb817032011-01-13 13:52:49 -08008710 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
8711 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008712 *
8713 * @param enabled True to enable duplication of the parent's drawable state, false
8714 * to disable it.
8715 *
8716 * @see #getDrawableState()
8717 * @see #isDuplicateParentStateEnabled()
8718 */
8719 public void setDuplicateParentStateEnabled(boolean enabled) {
8720 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
8721 }
8722
8723 /**
8724 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
8725 *
8726 * @return True if this view's drawable state is duplicated from the parent,
8727 * false otherwise
8728 *
8729 * @see #getDrawableState()
8730 * @see #setDuplicateParentStateEnabled(boolean)
8731 */
8732 public boolean isDuplicateParentStateEnabled() {
8733 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
8734 }
8735
8736 /**
Romain Guy171c5922011-01-06 10:04:23 -08008737 * <p>Specifies the type of layer backing this view. The layer can be
8738 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
8739 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008740 *
Romain Guy171c5922011-01-06 10:04:23 -08008741 * <p>A layer is associated with an optional {@link android.graphics.Paint}
8742 * instance that controls how the layer is composed on screen. The following
8743 * properties of the paint are taken into account when composing the layer:</p>
8744 * <ul>
8745 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
8746 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
8747 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
8748 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08008749 *
Romain Guy171c5922011-01-06 10:04:23 -08008750 * <p>If this view has an alpha value set to < 1.0 by calling
8751 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
8752 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
8753 * equivalent to setting a hardware layer on this view and providing a paint with
8754 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08008755 *
Romain Guy171c5922011-01-06 10:04:23 -08008756 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
8757 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
8758 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008759 *
Romain Guy171c5922011-01-06 10:04:23 -08008760 * @param layerType The ype of layer to use with this view, must be one of
8761 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8762 * {@link #LAYER_TYPE_HARDWARE}
8763 * @param paint The paint used to compose the layer. This argument is optional
8764 * and can be null. It is ignored when the layer type is
8765 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08008766 *
8767 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08008768 * @see #LAYER_TYPE_NONE
8769 * @see #LAYER_TYPE_SOFTWARE
8770 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08008771 * @see #setAlpha(float)
8772 *
Romain Guy171c5922011-01-06 10:04:23 -08008773 * @attr ref android.R.styleable#View_layerType
8774 */
8775 public void setLayerType(int layerType, Paint paint) {
8776 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08008777 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08008778 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
8779 }
Chet Haasedaf98e92011-01-10 14:10:36 -08008780
Romain Guyd6cd5722011-01-17 14:42:41 -08008781 if (layerType == mLayerType) {
8782 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
8783 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08008784 invalidateParentCaches();
8785 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08008786 }
8787 return;
8788 }
Romain Guy171c5922011-01-06 10:04:23 -08008789
8790 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08008791 switch (mLayerType) {
8792 case LAYER_TYPE_SOFTWARE:
8793 if (mDrawingCache != null) {
8794 mDrawingCache.recycle();
8795 mDrawingCache = null;
8796 }
Joe Malin32736f02011-01-19 16:14:20 -08008797
Romain Guy6c319ca2011-01-11 14:29:25 -08008798 if (mUnscaledDrawingCache != null) {
8799 mUnscaledDrawingCache.recycle();
8800 mUnscaledDrawingCache = null;
8801 }
8802 break;
8803 case LAYER_TYPE_HARDWARE:
8804 if (mHardwareLayer != null) {
8805 mHardwareLayer.destroy();
8806 mHardwareLayer = null;
8807 }
8808 break;
8809 default:
8810 break;
Romain Guy171c5922011-01-06 10:04:23 -08008811 }
8812
8813 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08008814 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
8815 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
8816 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08008817
Romain Guy0fd89bf2011-01-26 15:41:30 -08008818 invalidateParentCaches();
8819 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08008820 }
8821
8822 /**
8823 * Indicates what type of layer is currently associated with this view. By default
8824 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
8825 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
8826 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08008827 *
Romain Guy171c5922011-01-06 10:04:23 -08008828 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
8829 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08008830 *
8831 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08008832 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08008833 * @see #LAYER_TYPE_NONE
8834 * @see #LAYER_TYPE_SOFTWARE
8835 * @see #LAYER_TYPE_HARDWARE
8836 */
8837 public int getLayerType() {
8838 return mLayerType;
8839 }
Joe Malin32736f02011-01-19 16:14:20 -08008840
Romain Guy6c319ca2011-01-11 14:29:25 -08008841 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08008842 * Forces this view's layer to be created and this view to be rendered
8843 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
8844 * invoking this method will have no effect.
8845 *
8846 * This method can for instance be used to render a view into its layer before
8847 * starting an animation. If this view is complex, rendering into the layer
8848 * before starting the animation will avoid skipping frames.
8849 *
8850 * @throws IllegalStateException If this view is not attached to a window
8851 *
8852 * @see #setLayerType(int, android.graphics.Paint)
8853 */
8854 public void buildLayer() {
8855 if (mLayerType == LAYER_TYPE_NONE) return;
8856
8857 if (mAttachInfo == null) {
8858 throw new IllegalStateException("This view must be attached to a window first");
8859 }
8860
8861 switch (mLayerType) {
8862 case LAYER_TYPE_HARDWARE:
8863 getHardwareLayer();
8864 break;
8865 case LAYER_TYPE_SOFTWARE:
8866 buildDrawingCache(true);
8867 break;
8868 }
8869 }
8870
8871 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08008872 * <p>Returns a hardware layer that can be used to draw this view again
8873 * without executing its draw method.</p>
8874 *
8875 * @return A HardwareLayer ready to render, or null if an error occurred.
8876 */
Romain Guy5e7f7662011-01-24 22:35:56 -08008877 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08008878 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
8879 return null;
8880 }
8881
8882 final int width = mRight - mLeft;
8883 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08008884
Romain Guy6c319ca2011-01-11 14:29:25 -08008885 if (width == 0 || height == 0) {
8886 return null;
8887 }
8888
8889 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
8890 if (mHardwareLayer == null) {
8891 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
8892 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08008893 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008894 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
8895 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08008896 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008897 }
8898
Romain Guy5e7f7662011-01-24 22:35:56 -08008899 Canvas currentCanvas = mAttachInfo.mHardwareCanvas;
8900 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
8901 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008902 try {
8903 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08008904 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08008905 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08008906
Chet Haase88172fe2011-03-07 17:36:33 -08008907 final int restoreCount = canvas.save();
8908
Romain Guy6c319ca2011-01-11 14:29:25 -08008909 computeScroll();
8910 canvas.translate(-mScrollX, -mScrollY);
8911
Romain Guy6c319ca2011-01-11 14:29:25 -08008912 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08008913
Romain Guy6c319ca2011-01-11 14:29:25 -08008914 // Fast path for layouts with no backgrounds
8915 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
8916 mPrivateFlags &= ~DIRTY_MASK;
8917 dispatchDraw(canvas);
8918 } else {
8919 draw(canvas);
8920 }
Joe Malin32736f02011-01-19 16:14:20 -08008921
Chet Haase88172fe2011-03-07 17:36:33 -08008922 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08008923 } finally {
8924 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08008925 mHardwareLayer.end(currentCanvas);
8926 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08008927 }
8928 }
8929
8930 return mHardwareLayer;
8931 }
Romain Guy171c5922011-01-06 10:04:23 -08008932
8933 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
8935 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
8936 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
8937 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
8938 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
8939 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08008940 *
Romain Guy171c5922011-01-06 10:04:23 -08008941 * <p>Enabling the drawing cache is similar to
8942 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08008943 * acceleration is turned off. When hardware acceleration is turned on, enabling the
8944 * drawing cache has no effect on rendering because the system uses a different mechanism
8945 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
8946 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
8947 * for information on how to enable software and hardware layers.</p>
8948 *
8949 * <p>This API can be used to manually generate
8950 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
8951 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008952 *
8953 * @param enabled true to enable the drawing cache, false otherwise
8954 *
8955 * @see #isDrawingCacheEnabled()
8956 * @see #getDrawingCache()
8957 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08008958 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008959 */
8960 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08008961 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008962 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
8963 }
8964
8965 /**
8966 * <p>Indicates whether the drawing cache is enabled for this view.</p>
8967 *
8968 * @return true if the drawing cache is enabled
8969 *
8970 * @see #setDrawingCacheEnabled(boolean)
8971 * @see #getDrawingCache()
8972 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008973 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008974 public boolean isDrawingCacheEnabled() {
8975 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
8976 }
8977
8978 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08008979 * Debugging utility which recursively outputs the dirty state of a view and its
8980 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08008981 *
Chet Haasedaf98e92011-01-10 14:10:36 -08008982 * @hide
8983 */
Romain Guy676b1732011-02-14 14:45:33 -08008984 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08008985 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
8986 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
8987 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
8988 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
8989 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
8990 if (clear) {
8991 mPrivateFlags &= clearMask;
8992 }
8993 if (this instanceof ViewGroup) {
8994 ViewGroup parent = (ViewGroup) this;
8995 final int count = parent.getChildCount();
8996 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08008997 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08008998 child.outputDirtyFlags(indent + " ", clear, clearMask);
8999 }
9000 }
9001 }
9002
9003 /**
9004 * This method is used by ViewGroup to cause its children to restore or recreate their
9005 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9006 * to recreate its own display list, which would happen if it went through the normal
9007 * draw/dispatchDraw mechanisms.
9008 *
9009 * @hide
9010 */
9011 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009012
9013 /**
9014 * A view that is not attached or hardware accelerated cannot create a display list.
9015 * This method checks these conditions and returns the appropriate result.
9016 *
9017 * @return true if view has the ability to create a display list, false otherwise.
9018 *
9019 * @hide
9020 */
9021 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009022 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009023 }
Joe Malin32736f02011-01-19 16:14:20 -08009024
Chet Haasedaf98e92011-01-10 14:10:36 -08009025 /**
Romain Guyb051e892010-09-28 19:09:36 -07009026 * <p>Returns a display list that can be used to draw this view again
9027 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009028 *
Romain Guyb051e892010-09-28 19:09:36 -07009029 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009030 *
9031 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009032 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009033 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009034 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009035 return null;
9036 }
9037
Chet Haasedaf98e92011-01-10 14:10:36 -08009038 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9039 mDisplayList == null || !mDisplayList.isValid() ||
9040 mRecreateDisplayList)) {
9041 // Don't need to recreate the display list, just need to tell our
9042 // children to restore/recreate theirs
9043 if (mDisplayList != null && mDisplayList.isValid() &&
9044 !mRecreateDisplayList) {
9045 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9046 mPrivateFlags &= ~DIRTY_MASK;
9047 dispatchGetDisplayList();
9048
9049 return mDisplayList;
9050 }
9051
9052 // If we got here, we're recreating it. Mark it as such to ensure that
9053 // we copy in child display lists into ours in drawChild()
9054 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009055 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009056 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9057 // If we're creating a new display list, make sure our parent gets invalidated
9058 // since they will need to recreate their display list to account for this
9059 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009060 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009061 }
Romain Guyb051e892010-09-28 19:09:36 -07009062
9063 final HardwareCanvas canvas = mDisplayList.start();
9064 try {
9065 int width = mRight - mLeft;
9066 int height = mBottom - mTop;
9067
9068 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009069 // The dirty rect should always be null for a display list
9070 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009071
Chet Haase88172fe2011-03-07 17:36:33 -08009072 final int restoreCount = canvas.save();
9073
Chet Haasedaf98e92011-01-10 14:10:36 -08009074 computeScroll();
9075 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009076 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009077
Romain Guyb051e892010-09-28 19:09:36 -07009078 // Fast path for layouts with no backgrounds
9079 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9080 mPrivateFlags &= ~DIRTY_MASK;
9081 dispatchDraw(canvas);
9082 } else {
9083 draw(canvas);
9084 }
Joe Malin32736f02011-01-19 16:14:20 -08009085
Chet Haase88172fe2011-03-07 17:36:33 -08009086 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009087 } finally {
9088 canvas.onPostDraw();
9089
9090 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009091 }
Chet Haase77785f92011-01-25 23:22:09 -08009092 } else {
9093 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9094 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009095 }
9096
9097 return mDisplayList;
9098 }
9099
9100 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009101 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009102 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009103 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009104 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009105 * @see #getDrawingCache(boolean)
9106 */
9107 public Bitmap getDrawingCache() {
9108 return getDrawingCache(false);
9109 }
9110
9111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009112 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9113 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9114 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9115 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9116 * request the drawing cache by calling this method and draw it on screen if the
9117 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009118 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009119 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9120 * this method will create a bitmap of the same size as this view. Because this bitmap
9121 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9122 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9123 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9124 * size than the view. This implies that your application must be able to handle this
9125 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009126 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009127 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9128 * the current density of the screen when the application is in compatibility
9129 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009130 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009131 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009132 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 * @see #setDrawingCacheEnabled(boolean)
9134 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009135 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 * @see #destroyDrawingCache()
9137 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009138 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9140 return null;
9141 }
9142 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009143 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009144 }
Romain Guy02890fd2010-08-06 17:58:44 -07009145 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009146 }
9147
9148 /**
9149 * <p>Frees the resources used by the drawing cache. If you call
9150 * {@link #buildDrawingCache()} manually without calling
9151 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9152 * should cleanup the cache with this method afterwards.</p>
9153 *
9154 * @see #setDrawingCacheEnabled(boolean)
9155 * @see #buildDrawingCache()
9156 * @see #getDrawingCache()
9157 */
9158 public void destroyDrawingCache() {
9159 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009160 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009161 mDrawingCache = null;
9162 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009163 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009164 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009165 mUnscaledDrawingCache = null;
9166 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009167 }
9168
9169 /**
9170 * Setting a solid background color for the drawing cache's bitmaps will improve
9171 * perfromance and memory usage. Note, though that this should only be used if this
9172 * view will always be drawn on top of a solid color.
9173 *
9174 * @param color The background color to use for the drawing cache's bitmap
9175 *
9176 * @see #setDrawingCacheEnabled(boolean)
9177 * @see #buildDrawingCache()
9178 * @see #getDrawingCache()
9179 */
9180 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009181 if (color != mDrawingCacheBackgroundColor) {
9182 mDrawingCacheBackgroundColor = color;
9183 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9184 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009185 }
9186
9187 /**
9188 * @see #setDrawingCacheBackgroundColor(int)
9189 *
9190 * @return The background color to used for the drawing cache's bitmap
9191 */
9192 public int getDrawingCacheBackgroundColor() {
9193 return mDrawingCacheBackgroundColor;
9194 }
9195
9196 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009197 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009198 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009199 * @see #buildDrawingCache(boolean)
9200 */
9201 public void buildDrawingCache() {
9202 buildDrawingCache(false);
9203 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009204
Romain Guyfbd8f692009-06-26 14:51:58 -07009205 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009206 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9207 *
9208 * <p>If you call {@link #buildDrawingCache()} manually without calling
9209 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9210 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009211 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009212 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9213 * this method will create a bitmap of the same size as this view. Because this bitmap
9214 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9215 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9216 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9217 * size than the view. This implies that your application must be able to handle this
9218 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009219 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009220 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9221 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009222 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009223 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009224 *
9225 * @see #getDrawingCache()
9226 * @see #destroyDrawingCache()
9227 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009228 public void buildDrawingCache(boolean autoScale) {
9229 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009230 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009231 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009232
9233 if (ViewDebug.TRACE_HIERARCHY) {
9234 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009236
Romain Guy8506ab42009-06-11 17:35:47 -07009237 int width = mRight - mLeft;
9238 int height = mBottom - mTop;
9239
9240 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009241 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -07009242
Romain Guye1123222009-06-29 14:24:56 -07009243 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009244 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
9245 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -07009246 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009247
9248 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -07009249 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -08009250 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009251
9252 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -07009253 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -08009254 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009255 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
9256 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -08009257 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009258 return;
9259 }
9260
9261 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -07009262 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009263
9264 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009265 Bitmap.Config quality;
9266 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -08009267 // Never pick ARGB_4444 because it looks awful
9268 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009269 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
9270 case DRAWING_CACHE_QUALITY_AUTO:
9271 quality = Bitmap.Config.ARGB_8888;
9272 break;
9273 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -08009274 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009275 break;
9276 case DRAWING_CACHE_QUALITY_HIGH:
9277 quality = Bitmap.Config.ARGB_8888;
9278 break;
9279 default:
9280 quality = Bitmap.Config.ARGB_8888;
9281 break;
9282 }
9283 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -07009284 // Optimization for translucent windows
9285 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -08009286 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 }
9288
9289 // Try to cleanup memory
9290 if (bitmap != null) bitmap.recycle();
9291
9292 try {
9293 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -07009294 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -07009295 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -07009296 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009297 } else {
Romain Guy02890fd2010-08-06 17:58:44 -07009298 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -07009299 }
Adam Powell26153a32010-11-08 15:22:27 -08009300 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009301 } catch (OutOfMemoryError e) {
9302 // If there is not enough memory to create the bitmap cache, just
9303 // ignore the issue as bitmap caches are not required to draw the
9304 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -07009305 if (autoScale) {
9306 mDrawingCache = null;
9307 } else {
9308 mUnscaledDrawingCache = null;
9309 }
Romain Guy0211a0a2011-02-14 16:34:59 -08009310 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009311 return;
9312 }
9313
9314 clear = drawingCacheBackgroundColor != 0;
9315 }
9316
9317 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009318 if (attachInfo != null) {
9319 canvas = attachInfo.mCanvas;
9320 if (canvas == null) {
9321 canvas = new Canvas();
9322 }
9323 canvas.setBitmap(bitmap);
9324 // Temporarily clobber the cached Canvas in case one of our children
9325 // is also using a drawing cache. Without this, the children would
9326 // steal the canvas by attaching their own bitmap to it and bad, bad
9327 // thing would happen (invisible views, corrupted drawings, etc.)
9328 attachInfo.mCanvas = null;
9329 } else {
9330 // This case should hopefully never or seldom happen
9331 canvas = new Canvas(bitmap);
9332 }
9333
9334 if (clear) {
9335 bitmap.eraseColor(drawingCacheBackgroundColor);
9336 }
9337
9338 computeScroll();
9339 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -08009340
Romain Guye1123222009-06-29 14:24:56 -07009341 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009342 final float scale = attachInfo.mApplicationScale;
9343 canvas.scale(scale, scale);
9344 }
Joe Malin32736f02011-01-19 16:14:20 -08009345
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 canvas.translate(-mScrollX, -mScrollY);
9347
Romain Guy5bcdff42009-05-14 21:27:18 -07009348 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -08009349 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
9350 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -07009351 mPrivateFlags |= DRAWING_CACHE_VALID;
9352 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009353
9354 // Fast path for layouts with no backgrounds
9355 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9356 if (ViewDebug.TRACE_HIERARCHY) {
9357 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9358 }
Romain Guy5bcdff42009-05-14 21:27:18 -07009359 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009360 dispatchDraw(canvas);
9361 } else {
9362 draw(canvas);
9363 }
9364
9365 canvas.restoreToCount(restoreCount);
9366
9367 if (attachInfo != null) {
9368 // Restore the cached Canvas for our siblings
9369 attachInfo.mCanvas = canvas;
9370 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 }
9372 }
9373
9374 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009375 * Create a snapshot of the view into a bitmap. We should probably make
9376 * some form of this public, but should think about the API.
9377 */
Romain Guy223ff5c2010-03-02 17:07:47 -08009378 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009379 int width = mRight - mLeft;
9380 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009381
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009382 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -07009383 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009384 width = (int) ((width * scale) + 0.5f);
9385 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -08009386
Romain Guy8c11e312009-09-14 15:15:30 -07009387 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009388 if (bitmap == null) {
9389 throw new OutOfMemoryError();
9390 }
9391
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009392 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -08009393
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009394 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009395 if (attachInfo != null) {
9396 canvas = attachInfo.mCanvas;
9397 if (canvas == null) {
9398 canvas = new Canvas();
9399 }
9400 canvas.setBitmap(bitmap);
9401 // Temporarily clobber the cached Canvas in case one of our children
9402 // is also using a drawing cache. Without this, the children would
9403 // steal the canvas by attaching their own bitmap to it and bad, bad
9404 // things would happen (invisible views, corrupted drawings, etc.)
9405 attachInfo.mCanvas = null;
9406 } else {
9407 // This case should hopefully never or seldom happen
9408 canvas = new Canvas(bitmap);
9409 }
9410
Romain Guy5bcdff42009-05-14 21:27:18 -07009411 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009412 bitmap.eraseColor(backgroundColor);
9413 }
9414
9415 computeScroll();
9416 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -07009417 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009418 canvas.translate(-mScrollX, -mScrollY);
9419
Romain Guy5bcdff42009-05-14 21:27:18 -07009420 // Temporarily remove the dirty mask
9421 int flags = mPrivateFlags;
9422 mPrivateFlags &= ~DIRTY_MASK;
9423
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009424 // Fast path for layouts with no backgrounds
9425 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9426 dispatchDraw(canvas);
9427 } else {
9428 draw(canvas);
9429 }
9430
Romain Guy5bcdff42009-05-14 21:27:18 -07009431 mPrivateFlags = flags;
9432
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009433 canvas.restoreToCount(restoreCount);
9434
9435 if (attachInfo != null) {
9436 // Restore the cached Canvas for our siblings
9437 attachInfo.mCanvas = canvas;
9438 }
Romain Guy8506ab42009-06-11 17:35:47 -07009439
Dianne Hackborn958b9ad2009-03-31 18:00:36 -07009440 return bitmap;
9441 }
9442
9443 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444 * Indicates whether this View is currently in edit mode. A View is usually
9445 * in edit mode when displayed within a developer tool. For instance, if
9446 * this View is being drawn by a visual user interface builder, this method
9447 * should return true.
9448 *
9449 * Subclasses should check the return value of this method to provide
9450 * different behaviors if their normal behavior might interfere with the
9451 * host environment. For instance: the class spawns a thread in its
9452 * constructor, the drawing code relies on device-specific features, etc.
9453 *
9454 * This method is usually checked in the drawing code of custom widgets.
9455 *
9456 * @return True if this View is in edit mode, false otherwise.
9457 */
9458 public boolean isInEditMode() {
9459 return false;
9460 }
9461
9462 /**
9463 * If the View draws content inside its padding and enables fading edges,
9464 * it needs to support padding offsets. Padding offsets are added to the
9465 * fading edges to extend the length of the fade so that it covers pixels
9466 * drawn inside the padding.
9467 *
9468 * Subclasses of this class should override this method if they need
9469 * to draw content inside the padding.
9470 *
9471 * @return True if padding offset must be applied, false otherwise.
9472 *
9473 * @see #getLeftPaddingOffset()
9474 * @see #getRightPaddingOffset()
9475 * @see #getTopPaddingOffset()
9476 * @see #getBottomPaddingOffset()
9477 *
9478 * @since CURRENT
9479 */
9480 protected boolean isPaddingOffsetRequired() {
9481 return false;
9482 }
9483
9484 /**
9485 * Amount by which to extend the left fading region. Called only when
9486 * {@link #isPaddingOffsetRequired()} returns true.
9487 *
9488 * @return The left padding offset in pixels.
9489 *
9490 * @see #isPaddingOffsetRequired()
9491 *
9492 * @since CURRENT
9493 */
9494 protected int getLeftPaddingOffset() {
9495 return 0;
9496 }
9497
9498 /**
9499 * Amount by which to extend the right fading region. Called only when
9500 * {@link #isPaddingOffsetRequired()} returns true.
9501 *
9502 * @return The right padding offset in pixels.
9503 *
9504 * @see #isPaddingOffsetRequired()
9505 *
9506 * @since CURRENT
9507 */
9508 protected int getRightPaddingOffset() {
9509 return 0;
9510 }
9511
9512 /**
9513 * Amount by which to extend the top fading region. Called only when
9514 * {@link #isPaddingOffsetRequired()} returns true.
9515 *
9516 * @return The top padding offset in pixels.
9517 *
9518 * @see #isPaddingOffsetRequired()
9519 *
9520 * @since CURRENT
9521 */
9522 protected int getTopPaddingOffset() {
9523 return 0;
9524 }
9525
9526 /**
9527 * Amount by which to extend the bottom fading region. Called only when
9528 * {@link #isPaddingOffsetRequired()} returns true.
9529 *
9530 * @return The bottom padding offset in pixels.
9531 *
9532 * @see #isPaddingOffsetRequired()
9533 *
9534 * @since CURRENT
9535 */
9536 protected int getBottomPaddingOffset() {
9537 return 0;
9538 }
9539
9540 /**
Romain Guy2bffd262010-09-12 17:40:02 -07009541 * <p>Indicates whether this view is attached to an hardware accelerated
9542 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009543 *
Romain Guy2bffd262010-09-12 17:40:02 -07009544 * <p>Even if this method returns true, it does not mean that every call
9545 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
9546 * accelerated {@link android.graphics.Canvas}. For instance, if this view
9547 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
9548 * window is hardware accelerated,
9549 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
9550 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009551 *
Romain Guy2bffd262010-09-12 17:40:02 -07009552 * @return True if the view is attached to a window and the window is
9553 * hardware accelerated; false in any other case.
9554 */
9555 public boolean isHardwareAccelerated() {
9556 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
9557 }
Joe Malin32736f02011-01-19 16:14:20 -08009558
Romain Guy2bffd262010-09-12 17:40:02 -07009559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009560 * Manually render this view (and all of its children) to the given Canvas.
9561 * The view must have already done a full layout before this function is
Chet Haasec75ec332010-12-17 07:44:30 -08009562 * called. When implementing a view, implement {@link #onDraw} instead of
9563 * overriding this method. If you do need to override this method, call
9564 * the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009565 *
9566 * @param canvas The Canvas to which the View is rendered.
9567 */
9568 public void draw(Canvas canvas) {
9569 if (ViewDebug.TRACE_HIERARCHY) {
9570 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
9571 }
9572
Romain Guy5bcdff42009-05-14 21:27:18 -07009573 final int privateFlags = mPrivateFlags;
9574 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
9575 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
9576 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -07009577
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 /*
9579 * Draw traversal performs several drawing steps which must be executed
9580 * in the appropriate order:
9581 *
9582 * 1. Draw the background
9583 * 2. If necessary, save the canvas' layers to prepare for fading
9584 * 3. Draw view's content
9585 * 4. Draw children
9586 * 5. If necessary, draw the fading edges and restore layers
9587 * 6. Draw decorations (scrollbars for instance)
9588 */
9589
9590 // Step 1, draw the background, if needed
9591 int saveCount;
9592
Romain Guy24443ea2009-05-11 11:56:30 -07009593 if (!dirtyOpaque) {
9594 final Drawable background = mBGDrawable;
9595 if (background != null) {
9596 final int scrollX = mScrollX;
9597 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009598
Romain Guy24443ea2009-05-11 11:56:30 -07009599 if (mBackgroundSizeChanged) {
9600 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
9601 mBackgroundSizeChanged = false;
9602 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009603
Romain Guy24443ea2009-05-11 11:56:30 -07009604 if ((scrollX | scrollY) == 0) {
9605 background.draw(canvas);
9606 } else {
9607 canvas.translate(scrollX, scrollY);
9608 background.draw(canvas);
9609 canvas.translate(-scrollX, -scrollY);
9610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009611 }
9612 }
9613
9614 // skip step 2 & 5 if possible (common case)
9615 final int viewFlags = mViewFlags;
9616 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
9617 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
9618 if (!verticalEdges && !horizontalEdges) {
9619 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009620 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621
9622 // Step 4, draw the children
9623 dispatchDraw(canvas);
9624
9625 // Step 6, draw decorations (scrollbars)
9626 onDrawScrollBars(canvas);
9627
9628 // we're done...
9629 return;
9630 }
9631
9632 /*
9633 * Here we do the full fledged routine...
9634 * (this is an uncommon case where speed matters less,
9635 * this is why we repeat some of the tests that have been
9636 * done above)
9637 */
9638
9639 boolean drawTop = false;
9640 boolean drawBottom = false;
9641 boolean drawLeft = false;
9642 boolean drawRight = false;
9643
9644 float topFadeStrength = 0.0f;
9645 float bottomFadeStrength = 0.0f;
9646 float leftFadeStrength = 0.0f;
9647 float rightFadeStrength = 0.0f;
9648
9649 // Step 2, save the canvas' layers
9650 int paddingLeft = mPaddingLeft;
9651 int paddingTop = mPaddingTop;
9652
9653 final boolean offsetRequired = isPaddingOffsetRequired();
9654 if (offsetRequired) {
9655 paddingLeft += getLeftPaddingOffset();
9656 paddingTop += getTopPaddingOffset();
9657 }
9658
9659 int left = mScrollX + paddingLeft;
9660 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
9661 int top = mScrollY + paddingTop;
9662 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
9663
9664 if (offsetRequired) {
9665 right += getRightPaddingOffset();
9666 bottom += getBottomPaddingOffset();
9667 }
9668
9669 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009670 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
9671 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009672
9673 // clip the fade length if top and bottom fades overlap
9674 // overlapping fades produce odd-looking artifacts
9675 if (verticalEdges && (top + length > bottom - length)) {
9676 length = (bottom - top) / 2;
9677 }
9678
9679 // also clip horizontal fades if necessary
9680 if (horizontalEdges && (left + length > right - length)) {
9681 length = (right - left) / 2;
9682 }
9683
9684 if (verticalEdges) {
9685 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009686 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009687 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009688 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009689 }
9690
9691 if (horizontalEdges) {
9692 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009693 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009694 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009695 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009696 }
9697
9698 saveCount = canvas.getSaveCount();
9699
9700 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -07009701 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009702 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
9703
9704 if (drawTop) {
9705 canvas.saveLayer(left, top, right, top + length, null, flags);
9706 }
9707
9708 if (drawBottom) {
9709 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
9710 }
9711
9712 if (drawLeft) {
9713 canvas.saveLayer(left, top, left + length, bottom, null, flags);
9714 }
9715
9716 if (drawRight) {
9717 canvas.saveLayer(right - length, top, right, bottom, null, flags);
9718 }
9719 } else {
9720 scrollabilityCache.setFadeColor(solidColor);
9721 }
9722
9723 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -07009724 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009725
9726 // Step 4, draw the children
9727 dispatchDraw(canvas);
9728
9729 // Step 5, draw the fade effect and restore layers
9730 final Paint p = scrollabilityCache.paint;
9731 final Matrix matrix = scrollabilityCache.matrix;
9732 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009733
9734 if (drawTop) {
9735 matrix.setScale(1, fadeHeight * topFadeStrength);
9736 matrix.postTranslate(left, top);
9737 fade.setLocalMatrix(matrix);
9738 canvas.drawRect(left, top, right, top + length, p);
9739 }
9740
9741 if (drawBottom) {
9742 matrix.setScale(1, fadeHeight * bottomFadeStrength);
9743 matrix.postRotate(180);
9744 matrix.postTranslate(left, bottom);
9745 fade.setLocalMatrix(matrix);
9746 canvas.drawRect(left, bottom - length, right, bottom, p);
9747 }
9748
9749 if (drawLeft) {
9750 matrix.setScale(1, fadeHeight * leftFadeStrength);
9751 matrix.postRotate(-90);
9752 matrix.postTranslate(left, top);
9753 fade.setLocalMatrix(matrix);
9754 canvas.drawRect(left, top, left + length, bottom, p);
9755 }
9756
9757 if (drawRight) {
9758 matrix.setScale(1, fadeHeight * rightFadeStrength);
9759 matrix.postRotate(90);
9760 matrix.postTranslate(right, top);
9761 fade.setLocalMatrix(matrix);
9762 canvas.drawRect(right - length, top, right, bottom, p);
9763 }
9764
9765 canvas.restoreToCount(saveCount);
9766
9767 // Step 6, draw decorations (scrollbars)
9768 onDrawScrollBars(canvas);
9769 }
9770
9771 /**
9772 * Override this if your view is known to always be drawn on top of a solid color background,
9773 * and needs to draw fading edges. Returning a non-zero color enables the view system to
9774 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
9775 * should be set to 0xFF.
9776 *
9777 * @see #setVerticalFadingEdgeEnabled
9778 * @see #setHorizontalFadingEdgeEnabled
9779 *
9780 * @return The known solid color background for this view, or 0 if the color may vary
9781 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -07009782 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009783 public int getSolidColor() {
9784 return 0;
9785 }
9786
9787 /**
9788 * Build a human readable string representation of the specified view flags.
9789 *
9790 * @param flags the view flags to convert to a string
9791 * @return a String representing the supplied flags
9792 */
9793 private static String printFlags(int flags) {
9794 String output = "";
9795 int numFlags = 0;
9796 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
9797 output += "TAKES_FOCUS";
9798 numFlags++;
9799 }
9800
9801 switch (flags & VISIBILITY_MASK) {
9802 case INVISIBLE:
9803 if (numFlags > 0) {
9804 output += " ";
9805 }
9806 output += "INVISIBLE";
9807 // USELESS HERE numFlags++;
9808 break;
9809 case GONE:
9810 if (numFlags > 0) {
9811 output += " ";
9812 }
9813 output += "GONE";
9814 // USELESS HERE numFlags++;
9815 break;
9816 default:
9817 break;
9818 }
9819 return output;
9820 }
9821
9822 /**
9823 * Build a human readable string representation of the specified private
9824 * view flags.
9825 *
9826 * @param privateFlags the private view flags to convert to a string
9827 * @return a String representing the supplied flags
9828 */
9829 private static String printPrivateFlags(int privateFlags) {
9830 String output = "";
9831 int numFlags = 0;
9832
9833 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
9834 output += "WANTS_FOCUS";
9835 numFlags++;
9836 }
9837
9838 if ((privateFlags & FOCUSED) == FOCUSED) {
9839 if (numFlags > 0) {
9840 output += " ";
9841 }
9842 output += "FOCUSED";
9843 numFlags++;
9844 }
9845
9846 if ((privateFlags & SELECTED) == SELECTED) {
9847 if (numFlags > 0) {
9848 output += " ";
9849 }
9850 output += "SELECTED";
9851 numFlags++;
9852 }
9853
9854 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
9855 if (numFlags > 0) {
9856 output += " ";
9857 }
9858 output += "IS_ROOT_NAMESPACE";
9859 numFlags++;
9860 }
9861
9862 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
9863 if (numFlags > 0) {
9864 output += " ";
9865 }
9866 output += "HAS_BOUNDS";
9867 numFlags++;
9868 }
9869
9870 if ((privateFlags & DRAWN) == DRAWN) {
9871 if (numFlags > 0) {
9872 output += " ";
9873 }
9874 output += "DRAWN";
9875 // USELESS HERE numFlags++;
9876 }
9877 return output;
9878 }
9879
9880 /**
9881 * <p>Indicates whether or not this view's layout will be requested during
9882 * the next hierarchy layout pass.</p>
9883 *
9884 * @return true if the layout will be forced during next layout pass
9885 */
9886 public boolean isLayoutRequested() {
9887 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
9888 }
9889
9890 /**
9891 * Assign a size and position to a view and all of its
9892 * descendants
9893 *
9894 * <p>This is the second phase of the layout mechanism.
9895 * (The first is measuring). In this phase, each parent calls
9896 * layout on all of its children to position them.
9897 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -08009898 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009899 *
Chet Haase9c087442011-01-12 16:20:16 -08009900 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009901 * Derived classes with children should override
9902 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -08009903 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 *
9905 * @param l Left position, relative to parent
9906 * @param t Top position, relative to parent
9907 * @param r Right position, relative to parent
9908 * @param b Bottom position, relative to parent
9909 */
Romain Guy5429e1d2010-09-07 12:38:00 -07009910 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -08009911 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -07009912 int oldL = mLeft;
9913 int oldT = mTop;
9914 int oldB = mBottom;
9915 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009916 boolean changed = setFrame(l, t, r, b);
9917 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
9918 if (ViewDebug.TRACE_HIERARCHY) {
9919 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
9920 }
9921
9922 onLayout(changed, l, t, r, b);
9923 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -07009924
9925 if (mOnLayoutChangeListeners != null) {
9926 ArrayList<OnLayoutChangeListener> listenersCopy =
9927 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
9928 int numListeners = listenersCopy.size();
9929 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -07009930 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -07009931 }
9932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009933 }
9934 mPrivateFlags &= ~FORCE_LAYOUT;
9935 }
9936
9937 /**
9938 * Called from layout when this view should
9939 * assign a size and position to each of its children.
9940 *
9941 * Derived classes with children should override
9942 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -07009943 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009944 * @param changed This is a new size or position for this view
9945 * @param left Left position, relative to parent
9946 * @param top Top position, relative to parent
9947 * @param right Right position, relative to parent
9948 * @param bottom Bottom position, relative to parent
9949 */
9950 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
9951 }
9952
9953 /**
9954 * Assign a size and position to this view.
9955 *
9956 * This is called from layout.
9957 *
9958 * @param left Left position, relative to parent
9959 * @param top Top position, relative to parent
9960 * @param right Right position, relative to parent
9961 * @param bottom Bottom position, relative to parent
9962 * @return true if the new size and position are different than the
9963 * previous ones
9964 * {@hide}
9965 */
9966 protected boolean setFrame(int left, int top, int right, int bottom) {
9967 boolean changed = false;
9968
9969 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -07009970 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009971 + right + "," + bottom + ")");
9972 }
9973
9974 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
9975 changed = true;
9976
9977 // Remember our drawn bit
9978 int drawn = mPrivateFlags & DRAWN;
9979
9980 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -08009981 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982
9983
9984 int oldWidth = mRight - mLeft;
9985 int oldHeight = mBottom - mTop;
9986
9987 mLeft = left;
9988 mTop = top;
9989 mRight = right;
9990 mBottom = bottom;
9991
9992 mPrivateFlags |= HAS_BOUNDS;
9993
9994 int newWidth = right - left;
9995 int newHeight = bottom - top;
9996
9997 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08009998 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
9999 // A change in dimension means an auto-centered pivot point changes, too
10000 mMatrixDirty = true;
10001 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010002 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10003 }
10004
10005 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10006 // If we are visible, force the DRAWN bit to on so that
10007 // this invalidate will go through (at least to our parent).
10008 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010009 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010010 // the DRAWN bit.
10011 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010012 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010013 // parent display list may need to be recreated based on a change in the bounds
10014 // of any child
10015 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 }
10017
10018 // Reset drawn bit to original value (invalidate turns it off)
10019 mPrivateFlags |= drawn;
10020
10021 mBackgroundSizeChanged = true;
10022 }
10023 return changed;
10024 }
10025
10026 /**
10027 * Finalize inflating a view from XML. This is called as the last phase
10028 * of inflation, after all child views have been added.
10029 *
10030 * <p>Even if the subclass overrides onFinishInflate, they should always be
10031 * sure to call the super method, so that we get called.
10032 */
10033 protected void onFinishInflate() {
10034 }
10035
10036 /**
10037 * Returns the resources associated with this view.
10038 *
10039 * @return Resources object.
10040 */
10041 public Resources getResources() {
10042 return mResources;
10043 }
10044
10045 /**
10046 * Invalidates the specified Drawable.
10047 *
10048 * @param drawable the drawable to invalidate
10049 */
10050 public void invalidateDrawable(Drawable drawable) {
10051 if (verifyDrawable(drawable)) {
10052 final Rect dirty = drawable.getBounds();
10053 final int scrollX = mScrollX;
10054 final int scrollY = mScrollY;
10055
10056 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10057 dirty.right + scrollX, dirty.bottom + scrollY);
10058 }
10059 }
10060
10061 /**
10062 * Schedules an action on a drawable to occur at a specified time.
10063 *
10064 * @param who the recipient of the action
10065 * @param what the action to run on the drawable
10066 * @param when the time at which the action must occur. Uses the
10067 * {@link SystemClock#uptimeMillis} timebase.
10068 */
10069 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10070 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10071 mAttachInfo.mHandler.postAtTime(what, who, when);
10072 }
10073 }
10074
10075 /**
10076 * Cancels a scheduled action on a drawable.
10077 *
10078 * @param who the recipient of the action
10079 * @param what the action to cancel
10080 */
10081 public void unscheduleDrawable(Drawable who, Runnable what) {
10082 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10083 mAttachInfo.mHandler.removeCallbacks(what, who);
10084 }
10085 }
10086
10087 /**
10088 * Unschedule any events associated with the given Drawable. This can be
10089 * used when selecting a new Drawable into a view, so that the previous
10090 * one is completely unscheduled.
10091 *
10092 * @param who The Drawable to unschedule.
10093 *
10094 * @see #drawableStateChanged
10095 */
10096 public void unscheduleDrawable(Drawable who) {
10097 if (mAttachInfo != null) {
10098 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10099 }
10100 }
10101
10102 /**
10103 * If your view subclass is displaying its own Drawable objects, it should
10104 * override this function and return true for any Drawable it is
10105 * displaying. This allows animations for those drawables to be
10106 * scheduled.
10107 *
10108 * <p>Be sure to call through to the super class when overriding this
10109 * function.
10110 *
10111 * @param who The Drawable to verify. Return true if it is one you are
10112 * displaying, else return the result of calling through to the
10113 * super class.
10114 *
10115 * @return boolean If true than the Drawable is being displayed in the
10116 * view; else false and it is not allowed to animate.
10117 *
10118 * @see #unscheduleDrawable
10119 * @see #drawableStateChanged
10120 */
10121 protected boolean verifyDrawable(Drawable who) {
10122 return who == mBGDrawable;
10123 }
10124
10125 /**
10126 * This function is called whenever the state of the view changes in such
10127 * a way that it impacts the state of drawables being shown.
10128 *
10129 * <p>Be sure to call through to the superclass when overriding this
10130 * function.
10131 *
10132 * @see Drawable#setState
10133 */
10134 protected void drawableStateChanged() {
10135 Drawable d = mBGDrawable;
10136 if (d != null && d.isStateful()) {
10137 d.setState(getDrawableState());
10138 }
10139 }
10140
10141 /**
10142 * Call this to force a view to update its drawable state. This will cause
10143 * drawableStateChanged to be called on this view. Views that are interested
10144 * in the new state should call getDrawableState.
10145 *
10146 * @see #drawableStateChanged
10147 * @see #getDrawableState
10148 */
10149 public void refreshDrawableState() {
10150 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10151 drawableStateChanged();
10152
10153 ViewParent parent = mParent;
10154 if (parent != null) {
10155 parent.childDrawableStateChanged(this);
10156 }
10157 }
10158
10159 /**
10160 * Return an array of resource IDs of the drawable states representing the
10161 * current state of the view.
10162 *
10163 * @return The current drawable state
10164 *
10165 * @see Drawable#setState
10166 * @see #drawableStateChanged
10167 * @see #onCreateDrawableState
10168 */
10169 public final int[] getDrawableState() {
10170 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10171 return mDrawableState;
10172 } else {
10173 mDrawableState = onCreateDrawableState(0);
10174 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10175 return mDrawableState;
10176 }
10177 }
10178
10179 /**
10180 * Generate the new {@link android.graphics.drawable.Drawable} state for
10181 * this view. This is called by the view
10182 * system when the cached Drawable state is determined to be invalid. To
10183 * retrieve the current state, you should use {@link #getDrawableState}.
10184 *
10185 * @param extraSpace if non-zero, this is the number of extra entries you
10186 * would like in the returned array in which you can place your own
10187 * states.
10188 *
10189 * @return Returns an array holding the current {@link Drawable} state of
10190 * the view.
10191 *
10192 * @see #mergeDrawableStates
10193 */
10194 protected int[] onCreateDrawableState(int extraSpace) {
10195 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10196 mParent instanceof View) {
10197 return ((View) mParent).onCreateDrawableState(extraSpace);
10198 }
10199
10200 int[] drawableState;
10201
10202 int privateFlags = mPrivateFlags;
10203
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010204 int viewStateIndex = 0;
10205 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10206 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10207 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010208 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010209 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10210 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010211 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10212 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010213 // This is set if HW acceleration is requested, even if the current
10214 // process doesn't allow it. This is just to allow app preview
10215 // windows to better match their app.
10216 viewStateIndex |= VIEW_STATE_ACCELERATED;
10217 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010218 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010219
Christopher Tate3d4bf172011-03-28 16:16:46 -070010220 final int privateFlags2 = mPrivateFlags2;
10221 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10222 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10223
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010224 drawableState = VIEW_STATE_SETS[viewStateIndex];
10225
10226 //noinspection ConstantIfStatement
10227 if (false) {
10228 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10229 Log.i("View", toString()
10230 + " pressed=" + ((privateFlags & PRESSED) != 0)
10231 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
10232 + " fo=" + hasFocus()
10233 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010234 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010235 + ": " + Arrays.toString(drawableState));
10236 }
10237
10238 if (extraSpace == 0) {
10239 return drawableState;
10240 }
10241
10242 final int[] fullState;
10243 if (drawableState != null) {
10244 fullState = new int[drawableState.length + extraSpace];
10245 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
10246 } else {
10247 fullState = new int[extraSpace];
10248 }
10249
10250 return fullState;
10251 }
10252
10253 /**
10254 * Merge your own state values in <var>additionalState</var> into the base
10255 * state values <var>baseState</var> that were returned by
10256 * {@link #onCreateDrawableState}.
10257 *
10258 * @param baseState The base state values returned by
10259 * {@link #onCreateDrawableState}, which will be modified to also hold your
10260 * own additional state values.
10261 *
10262 * @param additionalState The additional state values you would like
10263 * added to <var>baseState</var>; this array is not modified.
10264 *
10265 * @return As a convenience, the <var>baseState</var> array you originally
10266 * passed into the function is returned.
10267 *
10268 * @see #onCreateDrawableState
10269 */
10270 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
10271 final int N = baseState.length;
10272 int i = N - 1;
10273 while (i >= 0 && baseState[i] == 0) {
10274 i--;
10275 }
10276 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
10277 return baseState;
10278 }
10279
10280 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070010281 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
10282 * on all Drawable objects associated with this view.
10283 */
10284 public void jumpDrawablesToCurrentState() {
10285 if (mBGDrawable != null) {
10286 mBGDrawable.jumpToCurrentState();
10287 }
10288 }
10289
10290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010291 * Sets the background color for this view.
10292 * @param color the color of the background
10293 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010294 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010295 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070010296 if (mBGDrawable instanceof ColorDrawable) {
10297 ((ColorDrawable) mBGDrawable).setColor(color);
10298 } else {
10299 setBackgroundDrawable(new ColorDrawable(color));
10300 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010301 }
10302
10303 /**
10304 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070010305 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010306 * @param resid The identifier of the resource.
10307 * @attr ref android.R.styleable#View_background
10308 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000010309 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010310 public void setBackgroundResource(int resid) {
10311 if (resid != 0 && resid == mBackgroundResource) {
10312 return;
10313 }
10314
10315 Drawable d= null;
10316 if (resid != 0) {
10317 d = mResources.getDrawable(resid);
10318 }
10319 setBackgroundDrawable(d);
10320
10321 mBackgroundResource = resid;
10322 }
10323
10324 /**
10325 * Set the background to a given Drawable, or remove the background. If the
10326 * background has padding, this View's padding is set to the background's
10327 * padding. However, when a background is removed, this View's padding isn't
10328 * touched. If setting the padding is desired, please use
10329 * {@link #setPadding(int, int, int, int)}.
10330 *
10331 * @param d The Drawable to use as the background, or null to remove the
10332 * background
10333 */
10334 public void setBackgroundDrawable(Drawable d) {
10335 boolean requestLayout = false;
10336
10337 mBackgroundResource = 0;
10338
10339 /*
10340 * Regardless of whether we're setting a new background or not, we want
10341 * to clear the previous drawable.
10342 */
10343 if (mBGDrawable != null) {
10344 mBGDrawable.setCallback(null);
10345 unscheduleDrawable(mBGDrawable);
10346 }
10347
10348 if (d != null) {
10349 Rect padding = sThreadLocal.get();
10350 if (padding == null) {
10351 padding = new Rect();
10352 sThreadLocal.set(padding);
10353 }
10354 if (d.getPadding(padding)) {
10355 setPadding(padding.left, padding.top, padding.right, padding.bottom);
10356 }
10357
10358 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
10359 // if it has a different minimum size, we should layout again
10360 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
10361 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
10362 requestLayout = true;
10363 }
10364
10365 d.setCallback(this);
10366 if (d.isStateful()) {
10367 d.setState(getDrawableState());
10368 }
10369 d.setVisible(getVisibility() == VISIBLE, false);
10370 mBGDrawable = d;
10371
10372 if ((mPrivateFlags & SKIP_DRAW) != 0) {
10373 mPrivateFlags &= ~SKIP_DRAW;
10374 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
10375 requestLayout = true;
10376 }
10377 } else {
10378 /* Remove the background */
10379 mBGDrawable = null;
10380
10381 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
10382 /*
10383 * This view ONLY drew the background before and we're removing
10384 * the background, so now it won't draw anything
10385 * (hence we SKIP_DRAW)
10386 */
10387 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
10388 mPrivateFlags |= SKIP_DRAW;
10389 }
10390
10391 /*
10392 * When the background is set, we try to apply its padding to this
10393 * View. When the background is removed, we don't touch this View's
10394 * padding. This is noted in the Javadocs. Hence, we don't need to
10395 * requestLayout(), the invalidate() below is sufficient.
10396 */
10397
10398 // The old background's minimum size could have affected this
10399 // View's layout, so let's requestLayout
10400 requestLayout = true;
10401 }
10402
Romain Guy8f1344f52009-05-15 16:03:59 -070010403 computeOpaqueFlags();
10404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010405 if (requestLayout) {
10406 requestLayout();
10407 }
10408
10409 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010410 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010411 }
10412
10413 /**
10414 * Gets the background drawable
10415 * @return The drawable used as the background for this view, if any.
10416 */
10417 public Drawable getBackground() {
10418 return mBGDrawable;
10419 }
10420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010421 /**
10422 * Sets the padding. The view may add on the space required to display
10423 * the scrollbars, depending on the style and visibility of the scrollbars.
10424 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
10425 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
10426 * from the values set in this call.
10427 *
10428 * @attr ref android.R.styleable#View_padding
10429 * @attr ref android.R.styleable#View_paddingBottom
10430 * @attr ref android.R.styleable#View_paddingLeft
10431 * @attr ref android.R.styleable#View_paddingRight
10432 * @attr ref android.R.styleable#View_paddingTop
10433 * @param left the left padding in pixels
10434 * @param top the top padding in pixels
10435 * @param right the right padding in pixels
10436 * @param bottom the bottom padding in pixels
10437 */
10438 public void setPadding(int left, int top, int right, int bottom) {
10439 boolean changed = false;
10440
Adam Powell20232d02010-12-08 21:08:53 -080010441 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010442 mUserPaddingRight = right;
10443 mUserPaddingBottom = bottom;
10444
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010445 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070010446
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010447 // Common case is there are no scroll bars.
10448 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010449 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080010450 // TODO Determine what to do with SCROLLBAR_POSITION_DEFAULT based on RTL settings.
10451 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010452 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080010453 switch (mVerticalScrollbarPosition) {
10454 case SCROLLBAR_POSITION_DEFAULT:
10455 case SCROLLBAR_POSITION_RIGHT:
10456 right += offset;
10457 break;
10458 case SCROLLBAR_POSITION_LEFT:
10459 left += offset;
10460 break;
10461 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010462 }
Adam Powell20232d02010-12-08 21:08:53 -080010463 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010464 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
10465 ? 0 : getHorizontalScrollbarHeight();
10466 }
10467 }
Romain Guy8506ab42009-06-11 17:35:47 -070010468
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010469 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010470 changed = true;
10471 mPaddingLeft = left;
10472 }
10473 if (mPaddingTop != top) {
10474 changed = true;
10475 mPaddingTop = top;
10476 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010477 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010478 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010479 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010480 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010481 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010482 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070010483 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010484 }
10485
10486 if (changed) {
10487 requestLayout();
10488 }
10489 }
10490
10491 /**
10492 * Returns the top padding of this view.
10493 *
10494 * @return the top padding in pixels
10495 */
10496 public int getPaddingTop() {
10497 return mPaddingTop;
10498 }
10499
10500 /**
10501 * Returns the bottom padding of this view. If there are inset and enabled
10502 * scrollbars, this value may include the space required to display the
10503 * scrollbars as well.
10504 *
10505 * @return the bottom padding in pixels
10506 */
10507 public int getPaddingBottom() {
10508 return mPaddingBottom;
10509 }
10510
10511 /**
10512 * Returns the left padding of this view. If there are inset and enabled
10513 * scrollbars, this value may include the space required to display the
10514 * scrollbars as well.
10515 *
10516 * @return the left padding in pixels
10517 */
10518 public int getPaddingLeft() {
10519 return mPaddingLeft;
10520 }
10521
10522 /**
10523 * Returns the right padding of this view. If there are inset and enabled
10524 * scrollbars, this value may include the space required to display the
10525 * scrollbars as well.
10526 *
10527 * @return the right padding in pixels
10528 */
10529 public int getPaddingRight() {
10530 return mPaddingRight;
10531 }
10532
10533 /**
10534 * Changes the selection state of this view. A view can be selected or not.
10535 * Note that selection is not the same as focus. Views are typically
10536 * selected in the context of an AdapterView like ListView or GridView;
10537 * the selected view is the view that is highlighted.
10538 *
10539 * @param selected true if the view must be selected, false otherwise
10540 */
10541 public void setSelected(boolean selected) {
10542 if (((mPrivateFlags & SELECTED) != 0) != selected) {
10543 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070010544 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080010545 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010546 refreshDrawableState();
10547 dispatchSetSelected(selected);
10548 }
10549 }
10550
10551 /**
10552 * Dispatch setSelected to all of this View's children.
10553 *
10554 * @see #setSelected(boolean)
10555 *
10556 * @param selected The new selected state
10557 */
10558 protected void dispatchSetSelected(boolean selected) {
10559 }
10560
10561 /**
10562 * Indicates the selection state of this view.
10563 *
10564 * @return true if the view is selected, false otherwise
10565 */
10566 @ViewDebug.ExportedProperty
10567 public boolean isSelected() {
10568 return (mPrivateFlags & SELECTED) != 0;
10569 }
10570
10571 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010572 * Changes the activated state of this view. A view can be activated or not.
10573 * Note that activation is not the same as selection. Selection is
10574 * a transient property, representing the view (hierarchy) the user is
10575 * currently interacting with. Activation is a longer-term state that the
10576 * user can move views in and out of. For example, in a list view with
10577 * single or multiple selection enabled, the views in the current selection
10578 * set are activated. (Um, yeah, we are deeply sorry about the terminology
10579 * here.) The activated state is propagated down to children of the view it
10580 * is set on.
10581 *
10582 * @param activated true if the view must be activated, false otherwise
10583 */
10584 public void setActivated(boolean activated) {
10585 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
10586 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080010587 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010588 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070010589 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010590 }
10591 }
10592
10593 /**
10594 * Dispatch setActivated to all of this View's children.
10595 *
10596 * @see #setActivated(boolean)
10597 *
10598 * @param activated The new activated state
10599 */
10600 protected void dispatchSetActivated(boolean activated) {
10601 }
10602
10603 /**
10604 * Indicates the activation state of this view.
10605 *
10606 * @return true if the view is activated, false otherwise
10607 */
10608 @ViewDebug.ExportedProperty
10609 public boolean isActivated() {
10610 return (mPrivateFlags & ACTIVATED) != 0;
10611 }
10612
10613 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010614 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
10615 * observer can be used to get notifications when global events, like
10616 * layout, happen.
10617 *
10618 * The returned ViewTreeObserver observer is not guaranteed to remain
10619 * valid for the lifetime of this View. If the caller of this method keeps
10620 * a long-lived reference to ViewTreeObserver, it should always check for
10621 * the return value of {@link ViewTreeObserver#isAlive()}.
10622 *
10623 * @return The ViewTreeObserver for this view's hierarchy.
10624 */
10625 public ViewTreeObserver getViewTreeObserver() {
10626 if (mAttachInfo != null) {
10627 return mAttachInfo.mTreeObserver;
10628 }
10629 if (mFloatingTreeObserver == null) {
10630 mFloatingTreeObserver = new ViewTreeObserver();
10631 }
10632 return mFloatingTreeObserver;
10633 }
10634
10635 /**
10636 * <p>Finds the topmost view in the current view hierarchy.</p>
10637 *
10638 * @return the topmost view containing this view
10639 */
10640 public View getRootView() {
10641 if (mAttachInfo != null) {
10642 final View v = mAttachInfo.mRootView;
10643 if (v != null) {
10644 return v;
10645 }
10646 }
Romain Guy8506ab42009-06-11 17:35:47 -070010647
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010648 View parent = this;
10649
10650 while (parent.mParent != null && parent.mParent instanceof View) {
10651 parent = (View) parent.mParent;
10652 }
10653
10654 return parent;
10655 }
10656
10657 /**
10658 * <p>Computes the coordinates of this view on the screen. The argument
10659 * must be an array of two integers. After the method returns, the array
10660 * contains the x and y location in that order.</p>
10661 *
10662 * @param location an array of two integers in which to hold the coordinates
10663 */
10664 public void getLocationOnScreen(int[] location) {
10665 getLocationInWindow(location);
10666
10667 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070010668 if (info != null) {
10669 location[0] += info.mWindowLeft;
10670 location[1] += info.mWindowTop;
10671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010672 }
10673
10674 /**
10675 * <p>Computes the coordinates of this view in its window. The argument
10676 * must be an array of two integers. After the method returns, the array
10677 * contains the x and y location in that order.</p>
10678 *
10679 * @param location an array of two integers in which to hold the coordinates
10680 */
10681 public void getLocationInWindow(int[] location) {
10682 if (location == null || location.length < 2) {
10683 throw new IllegalArgumentException("location must be an array of "
10684 + "two integers");
10685 }
10686
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010687 location[0] = mLeft + (int) (mTranslationX + 0.5f);
10688 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010689
10690 ViewParent viewParent = mParent;
10691 while (viewParent instanceof View) {
10692 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080010693 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
10694 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010695 viewParent = view.mParent;
10696 }
Romain Guy8506ab42009-06-11 17:35:47 -070010697
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010698 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010699 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070010700 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010701 location[1] -= vr.mCurScrollY;
10702 }
10703 }
10704
10705 /**
10706 * {@hide}
10707 * @param id the id of the view to be found
10708 * @return the view of the specified id, null if cannot be found
10709 */
10710 protected View findViewTraversal(int id) {
10711 if (id == mID) {
10712 return this;
10713 }
10714 return null;
10715 }
10716
10717 /**
10718 * {@hide}
10719 * @param tag the tag of the view to be found
10720 * @return the view of specified tag, null if cannot be found
10721 */
10722 protected View findViewWithTagTraversal(Object tag) {
10723 if (tag != null && tag.equals(mTag)) {
10724 return this;
10725 }
10726 return null;
10727 }
10728
10729 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010730 * {@hide}
10731 * @param predicate The predicate to evaluate.
10732 * @return The first view that matches the predicate or null.
10733 */
10734 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
10735 if (predicate.apply(this)) {
10736 return this;
10737 }
10738 return null;
10739 }
10740
10741 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010742 * Look for a child view with the given id. If this view has the given
10743 * id, return this view.
10744 *
10745 * @param id The id to search for.
10746 * @return The view that has the given id in the hierarchy or null
10747 */
10748 public final View findViewById(int id) {
10749 if (id < 0) {
10750 return null;
10751 }
10752 return findViewTraversal(id);
10753 }
10754
10755 /**
10756 * Look for a child view with the given tag. If this view has the given
10757 * tag, return this view.
10758 *
10759 * @param tag The tag to search for, using "tag.equals(getTag())".
10760 * @return The View that has the given tag in the hierarchy or null
10761 */
10762 public final View findViewWithTag(Object tag) {
10763 if (tag == null) {
10764 return null;
10765 }
10766 return findViewWithTagTraversal(tag);
10767 }
10768
10769 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080010770 * {@hide}
10771 * Look for a child view that matches the specified predicate.
10772 * If this view matches the predicate, return this view.
10773 *
10774 * @param predicate The predicate to evaluate.
10775 * @return The first view that matches the predicate or null.
10776 */
10777 public final View findViewByPredicate(Predicate<View> predicate) {
10778 return findViewByPredicateTraversal(predicate);
10779 }
10780
10781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010782 * Sets the identifier for this view. The identifier does not have to be
10783 * unique in this view's hierarchy. The identifier should be a positive
10784 * number.
10785 *
10786 * @see #NO_ID
10787 * @see #getId
10788 * @see #findViewById
10789 *
10790 * @param id a number used to identify the view
10791 *
10792 * @attr ref android.R.styleable#View_id
10793 */
10794 public void setId(int id) {
10795 mID = id;
10796 }
10797
10798 /**
10799 * {@hide}
10800 *
10801 * @param isRoot true if the view belongs to the root namespace, false
10802 * otherwise
10803 */
10804 public void setIsRootNamespace(boolean isRoot) {
10805 if (isRoot) {
10806 mPrivateFlags |= IS_ROOT_NAMESPACE;
10807 } else {
10808 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
10809 }
10810 }
10811
10812 /**
10813 * {@hide}
10814 *
10815 * @return true if the view belongs to the root namespace, false otherwise
10816 */
10817 public boolean isRootNamespace() {
10818 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
10819 }
10820
10821 /**
10822 * Returns this view's identifier.
10823 *
10824 * @return a positive integer used to identify the view or {@link #NO_ID}
10825 * if the view has no ID
10826 *
10827 * @see #setId
10828 * @see #findViewById
10829 * @attr ref android.R.styleable#View_id
10830 */
10831 @ViewDebug.CapturedViewProperty
10832 public int getId() {
10833 return mID;
10834 }
10835
10836 /**
10837 * Returns this view's tag.
10838 *
10839 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070010840 *
10841 * @see #setTag(Object)
10842 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010843 */
10844 @ViewDebug.ExportedProperty
10845 public Object getTag() {
10846 return mTag;
10847 }
10848
10849 /**
10850 * Sets the tag associated with this view. A tag can be used to mark
10851 * a view in its hierarchy and does not have to be unique within the
10852 * hierarchy. Tags can also be used to store data within a view without
10853 * resorting to another data structure.
10854 *
10855 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070010856 *
10857 * @see #getTag()
10858 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010859 */
10860 public void setTag(final Object tag) {
10861 mTag = tag;
10862 }
10863
10864 /**
Romain Guyd90a3312009-05-06 14:54:28 -070010865 * Returns the tag associated with this view and the specified key.
10866 *
10867 * @param key The key identifying the tag
10868 *
10869 * @return the Object stored in this view as a tag
10870 *
10871 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070010872 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070010873 */
10874 public Object getTag(int key) {
10875 SparseArray<Object> tags = null;
10876 synchronized (sTagsLock) {
10877 if (sTags != null) {
10878 tags = sTags.get(this);
10879 }
10880 }
10881
10882 if (tags != null) return tags.get(key);
10883 return null;
10884 }
10885
10886 /**
10887 * Sets a tag associated with this view and a key. A tag can be used
10888 * to mark a view in its hierarchy and does not have to be unique within
10889 * the hierarchy. Tags can also be used to store data within a view
10890 * without resorting to another data structure.
10891 *
10892 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070010893 * application to ensure it is unique (see the <a
10894 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
10895 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070010896 * the Android framework or not associated with any package will cause
10897 * an {@link IllegalArgumentException} to be thrown.
10898 *
10899 * @param key The key identifying the tag
10900 * @param tag An Object to tag the view with
10901 *
10902 * @throws IllegalArgumentException If they specified key is not valid
10903 *
10904 * @see #setTag(Object)
10905 * @see #getTag(int)
10906 */
10907 public void setTag(int key, final Object tag) {
10908 // If the package id is 0x00 or 0x01, it's either an undefined package
10909 // or a framework id
10910 if ((key >>> 24) < 2) {
10911 throw new IllegalArgumentException("The key must be an application-specific "
10912 + "resource id.");
10913 }
10914
10915 setTagInternal(this, key, tag);
10916 }
10917
10918 /**
10919 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
10920 * framework id.
10921 *
10922 * @hide
10923 */
10924 public void setTagInternal(int key, Object tag) {
10925 if ((key >>> 24) != 0x1) {
10926 throw new IllegalArgumentException("The key must be a framework-specific "
10927 + "resource id.");
10928 }
10929
Romain Guy8506ab42009-06-11 17:35:47 -070010930 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070010931 }
10932
10933 private static void setTagInternal(View view, int key, Object tag) {
10934 SparseArray<Object> tags = null;
10935 synchronized (sTagsLock) {
10936 if (sTags == null) {
10937 sTags = new WeakHashMap<View, SparseArray<Object>>();
10938 } else {
10939 tags = sTags.get(view);
10940 }
10941 }
10942
10943 if (tags == null) {
10944 tags = new SparseArray<Object>(2);
10945 synchronized (sTagsLock) {
10946 sTags.put(view, tags);
10947 }
10948 }
10949
10950 tags.put(key, tag);
10951 }
10952
10953 /**
Romain Guy13922e02009-05-12 17:56:14 -070010954 * @param consistency The type of consistency. See ViewDebug for more information.
10955 *
10956 * @hide
10957 */
10958 protected boolean dispatchConsistencyCheck(int consistency) {
10959 return onConsistencyCheck(consistency);
10960 }
10961
10962 /**
10963 * Method that subclasses should implement to check their consistency. The type of
10964 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070010965 *
Romain Guy13922e02009-05-12 17:56:14 -070010966 * @param consistency The type of consistency. See ViewDebug for more information.
10967 *
10968 * @throws IllegalStateException if the view is in an inconsistent state.
10969 *
10970 * @hide
10971 */
10972 protected boolean onConsistencyCheck(int consistency) {
10973 boolean result = true;
10974
10975 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
10976 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
10977
10978 if (checkLayout) {
10979 if (getParent() == null) {
10980 result = false;
10981 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10982 "View " + this + " does not have a parent.");
10983 }
10984
10985 if (mAttachInfo == null) {
10986 result = false;
10987 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
10988 "View " + this + " is not attached to a window.");
10989 }
10990 }
10991
10992 if (checkDrawing) {
10993 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
10994 // from their draw() method
10995
10996 if ((mPrivateFlags & DRAWN) != DRAWN &&
10997 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
10998 result = false;
10999 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11000 "View " + this + " was invalidated but its drawing cache is valid.");
11001 }
11002 }
11003
11004 return result;
11005 }
11006
11007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011008 * Prints information about this view in the log output, with the tag
11009 * {@link #VIEW_LOG_TAG}.
11010 *
11011 * @hide
11012 */
11013 public void debug() {
11014 debug(0);
11015 }
11016
11017 /**
11018 * Prints information about this view in the log output, with the tag
11019 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11020 * indentation defined by the <code>depth</code>.
11021 *
11022 * @param depth the indentation level
11023 *
11024 * @hide
11025 */
11026 protected void debug(int depth) {
11027 String output = debugIndent(depth - 1);
11028
11029 output += "+ " + this;
11030 int id = getId();
11031 if (id != -1) {
11032 output += " (id=" + id + ")";
11033 }
11034 Object tag = getTag();
11035 if (tag != null) {
11036 output += " (tag=" + tag + ")";
11037 }
11038 Log.d(VIEW_LOG_TAG, output);
11039
11040 if ((mPrivateFlags & FOCUSED) != 0) {
11041 output = debugIndent(depth) + " FOCUSED";
11042 Log.d(VIEW_LOG_TAG, output);
11043 }
11044
11045 output = debugIndent(depth);
11046 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11047 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11048 + "} ";
11049 Log.d(VIEW_LOG_TAG, output);
11050
11051 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11052 || mPaddingBottom != 0) {
11053 output = debugIndent(depth);
11054 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11055 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11056 Log.d(VIEW_LOG_TAG, output);
11057 }
11058
11059 output = debugIndent(depth);
11060 output += "mMeasureWidth=" + mMeasuredWidth +
11061 " mMeasureHeight=" + mMeasuredHeight;
11062 Log.d(VIEW_LOG_TAG, output);
11063
11064 output = debugIndent(depth);
11065 if (mLayoutParams == null) {
11066 output += "BAD! no layout params";
11067 } else {
11068 output = mLayoutParams.debug(output);
11069 }
11070 Log.d(VIEW_LOG_TAG, output);
11071
11072 output = debugIndent(depth);
11073 output += "flags={";
11074 output += View.printFlags(mViewFlags);
11075 output += "}";
11076 Log.d(VIEW_LOG_TAG, output);
11077
11078 output = debugIndent(depth);
11079 output += "privateFlags={";
11080 output += View.printPrivateFlags(mPrivateFlags);
11081 output += "}";
11082 Log.d(VIEW_LOG_TAG, output);
11083 }
11084
11085 /**
11086 * Creates an string of whitespaces used for indentation.
11087 *
11088 * @param depth the indentation level
11089 * @return a String containing (depth * 2 + 3) * 2 white spaces
11090 *
11091 * @hide
11092 */
11093 protected static String debugIndent(int depth) {
11094 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11095 for (int i = 0; i < (depth * 2) + 3; i++) {
11096 spaces.append(' ').append(' ');
11097 }
11098 return spaces.toString();
11099 }
11100
11101 /**
11102 * <p>Return the offset of the widget's text baseline from the widget's top
11103 * boundary. If this widget does not support baseline alignment, this
11104 * method returns -1. </p>
11105 *
11106 * @return the offset of the baseline within the widget's bounds or -1
11107 * if baseline alignment is not supported
11108 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011109 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011110 public int getBaseline() {
11111 return -1;
11112 }
11113
11114 /**
11115 * Call this when something has changed which has invalidated the
11116 * layout of this view. This will schedule a layout pass of the view
11117 * tree.
11118 */
11119 public void requestLayout() {
11120 if (ViewDebug.TRACE_HIERARCHY) {
11121 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11122 }
11123
11124 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011125 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011126
11127 if (mParent != null && !mParent.isLayoutRequested()) {
11128 mParent.requestLayout();
11129 }
11130 }
11131
11132 /**
11133 * Forces this view to be laid out during the next layout pass.
11134 * This method does not call requestLayout() or forceLayout()
11135 * on the parent.
11136 */
11137 public void forceLayout() {
11138 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011139 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011140 }
11141
11142 /**
11143 * <p>
11144 * This is called to find out how big a view should be. The parent
11145 * supplies constraint information in the width and height parameters.
11146 * </p>
11147 *
11148 * <p>
11149 * The actual mesurement work of a view is performed in
11150 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
11151 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
11152 * </p>
11153 *
11154 *
11155 * @param widthMeasureSpec Horizontal space requirements as imposed by the
11156 * parent
11157 * @param heightMeasureSpec Vertical space requirements as imposed by the
11158 * parent
11159 *
11160 * @see #onMeasure(int, int)
11161 */
11162 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
11163 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
11164 widthMeasureSpec != mOldWidthMeasureSpec ||
11165 heightMeasureSpec != mOldHeightMeasureSpec) {
11166
11167 // first clears the measured dimension flag
11168 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
11169
11170 if (ViewDebug.TRACE_HIERARCHY) {
11171 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
11172 }
11173
11174 // measure ourselves, this should set the measured dimension flag back
11175 onMeasure(widthMeasureSpec, heightMeasureSpec);
11176
11177 // flag not set, setMeasuredDimension() was not invoked, we raise
11178 // an exception to warn the developer
11179 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
11180 throw new IllegalStateException("onMeasure() did not set the"
11181 + " measured dimension by calling"
11182 + " setMeasuredDimension()");
11183 }
11184
11185 mPrivateFlags |= LAYOUT_REQUIRED;
11186 }
11187
11188 mOldWidthMeasureSpec = widthMeasureSpec;
11189 mOldHeightMeasureSpec = heightMeasureSpec;
11190 }
11191
11192 /**
11193 * <p>
11194 * Measure the view and its content to determine the measured width and the
11195 * measured height. This method is invoked by {@link #measure(int, int)} and
11196 * should be overriden by subclasses to provide accurate and efficient
11197 * measurement of their contents.
11198 * </p>
11199 *
11200 * <p>
11201 * <strong>CONTRACT:</strong> When overriding this method, you
11202 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
11203 * measured width and height of this view. Failure to do so will trigger an
11204 * <code>IllegalStateException</code>, thrown by
11205 * {@link #measure(int, int)}. Calling the superclass'
11206 * {@link #onMeasure(int, int)} is a valid use.
11207 * </p>
11208 *
11209 * <p>
11210 * The base class implementation of measure defaults to the background size,
11211 * unless a larger size is allowed by the MeasureSpec. Subclasses should
11212 * override {@link #onMeasure(int, int)} to provide better measurements of
11213 * their content.
11214 * </p>
11215 *
11216 * <p>
11217 * If this method is overridden, it is the subclass's responsibility to make
11218 * sure the measured height and width are at least the view's minimum height
11219 * and width ({@link #getSuggestedMinimumHeight()} and
11220 * {@link #getSuggestedMinimumWidth()}).
11221 * </p>
11222 *
11223 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
11224 * The requirements are encoded with
11225 * {@link android.view.View.MeasureSpec}.
11226 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
11227 * The requirements are encoded with
11228 * {@link android.view.View.MeasureSpec}.
11229 *
11230 * @see #getMeasuredWidth()
11231 * @see #getMeasuredHeight()
11232 * @see #setMeasuredDimension(int, int)
11233 * @see #getSuggestedMinimumHeight()
11234 * @see #getSuggestedMinimumWidth()
11235 * @see android.view.View.MeasureSpec#getMode(int)
11236 * @see android.view.View.MeasureSpec#getSize(int)
11237 */
11238 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
11239 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
11240 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
11241 }
11242
11243 /**
11244 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
11245 * measured width and measured height. Failing to do so will trigger an
11246 * exception at measurement time.</p>
11247 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080011248 * @param measuredWidth The measured width of this view. May be a complex
11249 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11250 * {@link #MEASURED_STATE_TOO_SMALL}.
11251 * @param measuredHeight The measured height of this view. May be a complex
11252 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
11253 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011254 */
11255 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
11256 mMeasuredWidth = measuredWidth;
11257 mMeasuredHeight = measuredHeight;
11258
11259 mPrivateFlags |= MEASURED_DIMENSION_SET;
11260 }
11261
11262 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080011263 * Merge two states as returned by {@link #getMeasuredState()}.
11264 * @param curState The current state as returned from a view or the result
11265 * of combining multiple views.
11266 * @param newState The new view state to combine.
11267 * @return Returns a new integer reflecting the combination of the two
11268 * states.
11269 */
11270 public static int combineMeasuredStates(int curState, int newState) {
11271 return curState | newState;
11272 }
11273
11274 /**
11275 * Version of {@link #resolveSizeAndState(int, int, int)}
11276 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
11277 */
11278 public static int resolveSize(int size, int measureSpec) {
11279 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
11280 }
11281
11282 /**
11283 * Utility to reconcile a desired size and state, with constraints imposed
11284 * by a MeasureSpec. Will take the desired size, unless a different size
11285 * is imposed by the constraints. The returned value is a compound integer,
11286 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
11287 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
11288 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011289 *
11290 * @param size How big the view wants to be
11291 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080011292 * @return Size information bit mask as defined by
11293 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011294 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080011295 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011296 int result = size;
11297 int specMode = MeasureSpec.getMode(measureSpec);
11298 int specSize = MeasureSpec.getSize(measureSpec);
11299 switch (specMode) {
11300 case MeasureSpec.UNSPECIFIED:
11301 result = size;
11302 break;
11303 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080011304 if (specSize < size) {
11305 result = specSize | MEASURED_STATE_TOO_SMALL;
11306 } else {
11307 result = size;
11308 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011309 break;
11310 case MeasureSpec.EXACTLY:
11311 result = specSize;
11312 break;
11313 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080011314 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011315 }
11316
11317 /**
11318 * Utility to return a default size. Uses the supplied size if the
11319 * MeasureSpec imposed no contraints. Will get larger if allowed
11320 * by the MeasureSpec.
11321 *
11322 * @param size Default size for this view
11323 * @param measureSpec Constraints imposed by the parent
11324 * @return The size this view should be.
11325 */
11326 public static int getDefaultSize(int size, int measureSpec) {
11327 int result = size;
11328 int specMode = MeasureSpec.getMode(measureSpec);
11329 int specSize = MeasureSpec.getSize(measureSpec);
11330
11331 switch (specMode) {
11332 case MeasureSpec.UNSPECIFIED:
11333 result = size;
11334 break;
11335 case MeasureSpec.AT_MOST:
11336 case MeasureSpec.EXACTLY:
11337 result = specSize;
11338 break;
11339 }
11340 return result;
11341 }
11342
11343 /**
11344 * Returns the suggested minimum height that the view should use. This
11345 * returns the maximum of the view's minimum height
11346 * and the background's minimum height
11347 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
11348 * <p>
11349 * When being used in {@link #onMeasure(int, int)}, the caller should still
11350 * ensure the returned height is within the requirements of the parent.
11351 *
11352 * @return The suggested minimum height of the view.
11353 */
11354 protected int getSuggestedMinimumHeight() {
11355 int suggestedMinHeight = mMinHeight;
11356
11357 if (mBGDrawable != null) {
11358 final int bgMinHeight = mBGDrawable.getMinimumHeight();
11359 if (suggestedMinHeight < bgMinHeight) {
11360 suggestedMinHeight = bgMinHeight;
11361 }
11362 }
11363
11364 return suggestedMinHeight;
11365 }
11366
11367 /**
11368 * Returns the suggested minimum width that the view should use. This
11369 * returns the maximum of the view's minimum width)
11370 * and the background's minimum width
11371 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
11372 * <p>
11373 * When being used in {@link #onMeasure(int, int)}, the caller should still
11374 * ensure the returned width is within the requirements of the parent.
11375 *
11376 * @return The suggested minimum width of the view.
11377 */
11378 protected int getSuggestedMinimumWidth() {
11379 int suggestedMinWidth = mMinWidth;
11380
11381 if (mBGDrawable != null) {
11382 final int bgMinWidth = mBGDrawable.getMinimumWidth();
11383 if (suggestedMinWidth < bgMinWidth) {
11384 suggestedMinWidth = bgMinWidth;
11385 }
11386 }
11387
11388 return suggestedMinWidth;
11389 }
11390
11391 /**
11392 * Sets the minimum height of the view. It is not guaranteed the view will
11393 * be able to achieve this minimum height (for example, if its parent layout
11394 * constrains it with less available height).
11395 *
11396 * @param minHeight The minimum height the view will try to be.
11397 */
11398 public void setMinimumHeight(int minHeight) {
11399 mMinHeight = minHeight;
11400 }
11401
11402 /**
11403 * Sets the minimum width of the view. It is not guaranteed the view will
11404 * be able to achieve this minimum width (for example, if its parent layout
11405 * constrains it with less available width).
11406 *
11407 * @param minWidth The minimum width the view will try to be.
11408 */
11409 public void setMinimumWidth(int minWidth) {
11410 mMinWidth = minWidth;
11411 }
11412
11413 /**
11414 * Get the animation currently associated with this view.
11415 *
11416 * @return The animation that is currently playing or
11417 * scheduled to play for this view.
11418 */
11419 public Animation getAnimation() {
11420 return mCurrentAnimation;
11421 }
11422
11423 /**
11424 * Start the specified animation now.
11425 *
11426 * @param animation the animation to start now
11427 */
11428 public void startAnimation(Animation animation) {
11429 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
11430 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011431 invalidateParentCaches();
11432 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011433 }
11434
11435 /**
11436 * Cancels any animations for this view.
11437 */
11438 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080011439 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080011440 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080011441 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011442 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011443 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011444 }
11445
11446 /**
11447 * Sets the next animation to play for this view.
11448 * If you want the animation to play immediately, use
11449 * startAnimation. This method provides allows fine-grained
11450 * control over the start time and invalidation, but you
11451 * must make sure that 1) the animation has a start time set, and
11452 * 2) the view will be invalidated when the animation is supposed to
11453 * start.
11454 *
11455 * @param animation The next animation, or null.
11456 */
11457 public void setAnimation(Animation animation) {
11458 mCurrentAnimation = animation;
11459 if (animation != null) {
11460 animation.reset();
11461 }
11462 }
11463
11464 /**
11465 * Invoked by a parent ViewGroup to notify the start of the animation
11466 * currently associated with this view. If you override this method,
11467 * always call super.onAnimationStart();
11468 *
11469 * @see #setAnimation(android.view.animation.Animation)
11470 * @see #getAnimation()
11471 */
11472 protected void onAnimationStart() {
11473 mPrivateFlags |= ANIMATION_STARTED;
11474 }
11475
11476 /**
11477 * Invoked by a parent ViewGroup to notify the end of the animation
11478 * currently associated with this view. If you override this method,
11479 * always call super.onAnimationEnd();
11480 *
11481 * @see #setAnimation(android.view.animation.Animation)
11482 * @see #getAnimation()
11483 */
11484 protected void onAnimationEnd() {
11485 mPrivateFlags &= ~ANIMATION_STARTED;
11486 }
11487
11488 /**
11489 * Invoked if there is a Transform that involves alpha. Subclass that can
11490 * draw themselves with the specified alpha should return true, and then
11491 * respect that alpha when their onDraw() is called. If this returns false
11492 * then the view may be redirected to draw into an offscreen buffer to
11493 * fulfill the request, which will look fine, but may be slower than if the
11494 * subclass handles it internally. The default implementation returns false.
11495 *
11496 * @param alpha The alpha (0..255) to apply to the view's drawing
11497 * @return true if the view can draw with the specified alpha.
11498 */
11499 protected boolean onSetAlpha(int alpha) {
11500 return false;
11501 }
11502
11503 /**
11504 * This is used by the RootView to perform an optimization when
11505 * the view hierarchy contains one or several SurfaceView.
11506 * SurfaceView is always considered transparent, but its children are not,
11507 * therefore all View objects remove themselves from the global transparent
11508 * region (passed as a parameter to this function).
11509 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011510 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011511 *
11512 * @return Returns true if the effective visibility of the view at this
11513 * point is opaque, regardless of the transparent region; returns false
11514 * if it is possible for underlying windows to be seen behind the view.
11515 *
11516 * {@hide}
11517 */
11518 public boolean gatherTransparentRegion(Region region) {
11519 final AttachInfo attachInfo = mAttachInfo;
11520 if (region != null && attachInfo != null) {
11521 final int pflags = mPrivateFlags;
11522 if ((pflags & SKIP_DRAW) == 0) {
11523 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
11524 // remove it from the transparent region.
11525 final int[] location = attachInfo.mTransparentLocation;
11526 getLocationInWindow(location);
11527 region.op(location[0], location[1], location[0] + mRight - mLeft,
11528 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
11529 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
11530 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
11531 // exists, so we remove the background drawable's non-transparent
11532 // parts from this transparent region.
11533 applyDrawableToTransparentRegion(mBGDrawable, region);
11534 }
11535 }
11536 return true;
11537 }
11538
11539 /**
11540 * Play a sound effect for this view.
11541 *
11542 * <p>The framework will play sound effects for some built in actions, such as
11543 * clicking, but you may wish to play these effects in your widget,
11544 * for instance, for internal navigation.
11545 *
11546 * <p>The sound effect will only be played if sound effects are enabled by the user, and
11547 * {@link #isSoundEffectsEnabled()} is true.
11548 *
11549 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
11550 */
11551 public void playSoundEffect(int soundConstant) {
11552 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
11553 return;
11554 }
11555 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
11556 }
11557
11558 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011559 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011560 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011561 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011562 *
11563 * <p>The framework will provide haptic feedback for some built in actions,
11564 * such as long presses, but you may wish to provide feedback for your
11565 * own widget.
11566 *
11567 * <p>The feedback will only be performed if
11568 * {@link #isHapticFeedbackEnabled()} is true.
11569 *
11570 * @param feedbackConstant One of the constants defined in
11571 * {@link HapticFeedbackConstants}
11572 */
11573 public boolean performHapticFeedback(int feedbackConstant) {
11574 return performHapticFeedback(feedbackConstant, 0);
11575 }
11576
11577 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011578 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070011579 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070011580 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011581 *
11582 * @param feedbackConstant One of the constants defined in
11583 * {@link HapticFeedbackConstants}
11584 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
11585 */
11586 public boolean performHapticFeedback(int feedbackConstant, int flags) {
11587 if (mAttachInfo == null) {
11588 return false;
11589 }
Romain Guyf607bdc2010-09-10 19:20:06 -070011590 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070011591 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011592 && !isHapticFeedbackEnabled()) {
11593 return false;
11594 }
Romain Guy812ccbe2010-06-01 14:07:24 -070011595 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
11596 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011597 }
11598
11599 /**
Joe Onorato664644d2011-01-23 17:53:23 -080011600 * Request that the visibility of the status bar be changed.
11601 */
11602 public void setSystemUiVisibility(int visibility) {
11603 if (visibility != mSystemUiVisibility) {
11604 mSystemUiVisibility = visibility;
11605 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11606 mParent.recomputeViewAttributes(this);
11607 }
11608 }
11609 }
11610
11611 /**
11612 * Returns the status bar visibility that this view has requested.
11613 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080011614 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080011615 return mSystemUiVisibility;
11616 }
11617
11618 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
11619 mOnSystemUiVisibilityChangeListener = l;
11620 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
11621 mParent.recomputeViewAttributes(this);
11622 }
11623 }
11624
11625 /**
11626 */
11627 public void dispatchSystemUiVisibilityChanged(int visibility) {
11628 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080011629 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080011630 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080011631 }
11632 }
11633
11634 /**
Joe Malin32736f02011-01-19 16:14:20 -080011635 * Creates an image that the system displays during the drag and drop
11636 * operation. This is called a &quot;drag shadow&quot;. The default implementation
11637 * for a DragShadowBuilder based on a View returns an image that has exactly the same
11638 * appearance as the given View. The default also positions the center of the drag shadow
11639 * directly under the touch point. If no View is provided (the constructor with no parameters
11640 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
11641 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
11642 * default is an invisible drag shadow.
11643 * <p>
11644 * You are not required to use the View you provide to the constructor as the basis of the
11645 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
11646 * anything you want as the drag shadow.
11647 * </p>
11648 * <p>
11649 * You pass a DragShadowBuilder object to the system when you start the drag. The system
11650 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
11651 * size and position of the drag shadow. It uses this data to construct a
11652 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
11653 * so that your application can draw the shadow image in the Canvas.
11654 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011655 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011656 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070011657 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070011658
11659 /**
Joe Malin32736f02011-01-19 16:14:20 -080011660 * Constructs a shadow image builder based on a View. By default, the resulting drag
11661 * shadow will have the same appearance and dimensions as the View, with the touch point
11662 * over the center of the View.
11663 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070011664 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011665 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070011666 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070011667 }
11668
Christopher Tate17ed60c2011-01-18 12:50:26 -080011669 /**
11670 * Construct a shadow builder object with no associated View. This
11671 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
11672 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
11673 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080011674 * reference to any View object. If they are not overridden, then the result is an
11675 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080011676 */
11677 public DragShadowBuilder() {
11678 mView = new WeakReference<View>(null);
11679 }
11680
11681 /**
11682 * Returns the View object that had been passed to the
11683 * {@link #View.DragShadowBuilder(View)}
11684 * constructor. If that View parameter was {@code null} or if the
11685 * {@link #View.DragShadowBuilder()}
11686 * constructor was used to instantiate the builder object, this method will return
11687 * null.
11688 *
11689 * @return The View object associate with this builder object.
11690 */
Chris Tate6b391282010-10-14 15:48:59 -070011691 final public View getView() {
11692 return mView.get();
11693 }
11694
Christopher Tate2c095f32010-10-04 14:13:40 -070011695 /**
Joe Malin32736f02011-01-19 16:14:20 -080011696 * Provides the metrics for the shadow image. These include the dimensions of
11697 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070011698 * be centered under the touch location while dragging.
11699 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011700 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080011701 * same as the dimensions of the View itself and centers the shadow under
11702 * the touch point.
11703 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070011704 *
Joe Malin32736f02011-01-19 16:14:20 -080011705 * @param shadowSize A {@link android.graphics.Point} containing the width and height
11706 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
11707 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
11708 * image.
11709 *
11710 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
11711 * shadow image that should be underneath the touch point during the drag and drop
11712 * operation. Your application must set {@link android.graphics.Point#x} to the
11713 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070011714 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011715 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070011716 final View view = mView.get();
11717 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011718 shadowSize.set(view.getWidth(), view.getHeight());
11719 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070011720 } else {
11721 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
11722 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011723 }
11724
11725 /**
Joe Malin32736f02011-01-19 16:14:20 -080011726 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
11727 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011728 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070011729 *
Joe Malin32736f02011-01-19 16:14:20 -080011730 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070011731 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011732 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070011733 final View view = mView.get();
11734 if (view != null) {
11735 view.draw(canvas);
11736 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011737 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070011738 }
Christopher Tate2c095f32010-10-04 14:13:40 -070011739 }
11740 }
11741
11742 /**
Joe Malin32736f02011-01-19 16:14:20 -080011743 * Starts a drag and drop operation. When your application calls this method, it passes a
11744 * {@link android.view.View.DragShadowBuilder} object to the system. The
11745 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
11746 * to get metrics for the drag shadow, and then calls the object's
11747 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
11748 * <p>
11749 * Once the system has the drag shadow, it begins the drag and drop operation by sending
11750 * drag events to all the View objects in your application that are currently visible. It does
11751 * this either by calling the View object's drag listener (an implementation of
11752 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
11753 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
11754 * Both are passed a {@link android.view.DragEvent} object that has a
11755 * {@link android.view.DragEvent#getAction()} value of
11756 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
11757 * </p>
11758 * <p>
11759 * Your application can invoke startDrag() on any attached View object. The View object does not
11760 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
11761 * be related to the View the user selected for dragging.
11762 * </p>
11763 * @param data A {@link android.content.ClipData} object pointing to the data to be
11764 * transferred by the drag and drop operation.
11765 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
11766 * drag shadow.
11767 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
11768 * drop operation. This Object is put into every DragEvent object sent by the system during the
11769 * current drag.
11770 * <p>
11771 * myLocalState is a lightweight mechanism for the sending information from the dragged View
11772 * to the target Views. For example, it can contain flags that differentiate between a
11773 * a copy operation and a move operation.
11774 * </p>
11775 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
11776 * so the parameter should be set to 0.
11777 * @return {@code true} if the method completes successfully, or
11778 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
11779 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070011780 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011781 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011782 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070011783 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011784 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070011785 }
11786 boolean okay = false;
11787
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011788 Point shadowSize = new Point();
11789 Point shadowTouchPoint = new Point();
11790 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070011791
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011792 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
11793 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
11794 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070011795 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011796
Chris Tatea32dcf72010-10-14 12:13:50 -070011797 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011798 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
11799 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070011800 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011801 Surface surface = new Surface();
11802 try {
11803 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080011804 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070011805 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070011806 + " surface=" + surface);
11807 if (token != null) {
11808 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070011809 try {
Chris Tate6b391282010-10-14 15:48:59 -070011810 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011811 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070011812 } finally {
11813 surface.unlockCanvasAndPost(canvas);
11814 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011815
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011816 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080011817
11818 // Cache the local state object for delivery with DragEvents
11819 root.setLocalDragState(myLocalState);
11820
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011821 // repurpose 'shadowSize' for the last touch point
11822 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070011823
Christopher Tatea53146c2010-09-07 11:57:52 -070011824 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080011825 shadowSize.x, shadowSize.y,
11826 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070011827 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070011828 }
11829 } catch (Exception e) {
11830 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
11831 surface.destroy();
11832 }
11833
11834 return okay;
11835 }
11836
Christopher Tatea53146c2010-09-07 11:57:52 -070011837 /**
Joe Malin32736f02011-01-19 16:14:20 -080011838 * Handles drag events sent by the system following a call to
11839 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
11840 *<p>
11841 * When the system calls this method, it passes a
11842 * {@link android.view.DragEvent} object. A call to
11843 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
11844 * in DragEvent. The method uses these to determine what is happening in the drag and drop
11845 * operation.
11846 * @param event The {@link android.view.DragEvent} sent by the system.
11847 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
11848 * in DragEvent, indicating the type of drag event represented by this object.
11849 * @return {@code true} if the method was successful, otherwise {@code false}.
11850 * <p>
11851 * The method should return {@code true} in response to an action type of
11852 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
11853 * operation.
11854 * </p>
11855 * <p>
11856 * The method should also return {@code true} in response to an action type of
11857 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
11858 * {@code false} if it didn't.
11859 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011860 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070011861 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070011862 return false;
11863 }
11864
11865 /**
Joe Malin32736f02011-01-19 16:14:20 -080011866 * Detects if this View is enabled and has a drag event listener.
11867 * If both are true, then it calls the drag event listener with the
11868 * {@link android.view.DragEvent} it received. If the drag event listener returns
11869 * {@code true}, then dispatchDragEvent() returns {@code true}.
11870 * <p>
11871 * For all other cases, the method calls the
11872 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
11873 * method and returns its result.
11874 * </p>
11875 * <p>
11876 * This ensures that a drag event is always consumed, even if the View does not have a drag
11877 * event listener. However, if the View has a listener and the listener returns true, then
11878 * onDragEvent() is not called.
11879 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070011880 */
11881 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080011882 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070011883 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
11884 && mOnDragListener.onDrag(this, event)) {
11885 return true;
11886 }
Christopher Tatea53146c2010-09-07 11:57:52 -070011887 return onDragEvent(event);
11888 }
11889
Christopher Tate3d4bf172011-03-28 16:16:46 -070011890 boolean canAcceptDrag() {
11891 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
11892 }
11893
Christopher Tatea53146c2010-09-07 11:57:52 -070011894 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070011895 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
11896 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070011897 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070011898 */
11899 public void onCloseSystemDialogs(String reason) {
11900 }
Joe Malin32736f02011-01-19 16:14:20 -080011901
Dianne Hackbornffa42482009-09-23 22:20:11 -070011902 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011903 * Given a Drawable whose bounds have been set to draw into this view,
11904 * update a Region being computed for {@link #gatherTransparentRegion} so
11905 * that any non-transparent parts of the Drawable are removed from the
11906 * given transparent region.
11907 *
11908 * @param dr The Drawable whose transparency is to be applied to the region.
11909 * @param region A Region holding the current transparency information,
11910 * where any parts of the region that are set are considered to be
11911 * transparent. On return, this region will be modified to have the
11912 * transparency information reduced by the corresponding parts of the
11913 * Drawable that are not transparent.
11914 * {@hide}
11915 */
11916 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
11917 if (DBG) {
11918 Log.i("View", "Getting transparent region for: " + this);
11919 }
11920 final Region r = dr.getTransparentRegion();
11921 final Rect db = dr.getBounds();
11922 final AttachInfo attachInfo = mAttachInfo;
11923 if (r != null && attachInfo != null) {
11924 final int w = getRight()-getLeft();
11925 final int h = getBottom()-getTop();
11926 if (db.left > 0) {
11927 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
11928 r.op(0, 0, db.left, h, Region.Op.UNION);
11929 }
11930 if (db.right < w) {
11931 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
11932 r.op(db.right, 0, w, h, Region.Op.UNION);
11933 }
11934 if (db.top > 0) {
11935 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
11936 r.op(0, 0, w, db.top, Region.Op.UNION);
11937 }
11938 if (db.bottom < h) {
11939 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
11940 r.op(0, db.bottom, w, h, Region.Op.UNION);
11941 }
11942 final int[] location = attachInfo.mTransparentLocation;
11943 getLocationInWindow(location);
11944 r.translate(location[0], location[1]);
11945 region.op(r, Region.Op.INTERSECT);
11946 } else {
11947 region.op(db, Region.Op.DIFFERENCE);
11948 }
11949 }
11950
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011951 private void checkForLongClick(int delayOffset) {
11952 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
11953 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011954
Patrick Dubroye0a799a2011-05-04 16:19:22 -070011955 if (mPendingCheckForLongPress == null) {
11956 mPendingCheckForLongPress = new CheckForLongPress();
11957 }
11958 mPendingCheckForLongPress.rememberWindowAttachCount();
11959 postDelayed(mPendingCheckForLongPress,
11960 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011962 }
11963
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011964 /**
11965 * Inflate a view from an XML resource. This convenience method wraps the {@link
11966 * LayoutInflater} class, which provides a full range of options for view inflation.
11967 *
11968 * @param context The Context object for your activity or application.
11969 * @param resource The resource ID to inflate
11970 * @param root A view group that will be the parent. Used to properly inflate the
11971 * layout_* parameters.
11972 * @see LayoutInflater
11973 */
11974 public static View inflate(Context context, int resource, ViewGroup root) {
11975 LayoutInflater factory = LayoutInflater.from(context);
11976 return factory.inflate(resource, root);
11977 }
Romain Guy33e72ae2010-07-17 12:40:29 -070011978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011979 /**
Adam Powell637d3372010-08-25 14:37:03 -070011980 * Scroll the view with standard behavior for scrolling beyond the normal
11981 * content boundaries. Views that call this method should override
11982 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
11983 * results of an over-scroll operation.
11984 *
11985 * Views can use this method to handle any touch or fling-based scrolling.
11986 *
11987 * @param deltaX Change in X in pixels
11988 * @param deltaY Change in Y in pixels
11989 * @param scrollX Current X scroll value in pixels before applying deltaX
11990 * @param scrollY Current Y scroll value in pixels before applying deltaY
11991 * @param scrollRangeX Maximum content scroll range along the X axis
11992 * @param scrollRangeY Maximum content scroll range along the Y axis
11993 * @param maxOverScrollX Number of pixels to overscroll by in either direction
11994 * along the X axis.
11995 * @param maxOverScrollY Number of pixels to overscroll by in either direction
11996 * along the Y axis.
11997 * @param isTouchEvent true if this scroll operation is the result of a touch event.
11998 * @return true if scrolling was clamped to an over-scroll boundary along either
11999 * axis, false otherwise.
12000 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012001 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012002 protected boolean overScrollBy(int deltaX, int deltaY,
12003 int scrollX, int scrollY,
12004 int scrollRangeX, int scrollRangeY,
12005 int maxOverScrollX, int maxOverScrollY,
12006 boolean isTouchEvent) {
12007 final int overScrollMode = mOverScrollMode;
12008 final boolean canScrollHorizontal =
12009 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12010 final boolean canScrollVertical =
12011 computeVerticalScrollRange() > computeVerticalScrollExtent();
12012 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12013 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12014 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12015 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12016
12017 int newScrollX = scrollX + deltaX;
12018 if (!overScrollHorizontal) {
12019 maxOverScrollX = 0;
12020 }
12021
12022 int newScrollY = scrollY + deltaY;
12023 if (!overScrollVertical) {
12024 maxOverScrollY = 0;
12025 }
12026
12027 // Clamp values if at the limits and record
12028 final int left = -maxOverScrollX;
12029 final int right = maxOverScrollX + scrollRangeX;
12030 final int top = -maxOverScrollY;
12031 final int bottom = maxOverScrollY + scrollRangeY;
12032
12033 boolean clampedX = false;
12034 if (newScrollX > right) {
12035 newScrollX = right;
12036 clampedX = true;
12037 } else if (newScrollX < left) {
12038 newScrollX = left;
12039 clampedX = true;
12040 }
12041
12042 boolean clampedY = false;
12043 if (newScrollY > bottom) {
12044 newScrollY = bottom;
12045 clampedY = true;
12046 } else if (newScrollY < top) {
12047 newScrollY = top;
12048 clampedY = true;
12049 }
12050
12051 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12052
12053 return clampedX || clampedY;
12054 }
12055
12056 /**
12057 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12058 * respond to the results of an over-scroll operation.
12059 *
12060 * @param scrollX New X scroll value in pixels
12061 * @param scrollY New Y scroll value in pixels
12062 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12063 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12064 */
12065 protected void onOverScrolled(int scrollX, int scrollY,
12066 boolean clampedX, boolean clampedY) {
12067 // Intentionally empty.
12068 }
12069
12070 /**
12071 * Returns the over-scroll mode for this view. The result will be
12072 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12073 * (allow over-scrolling only if the view content is larger than the container),
12074 * or {@link #OVER_SCROLL_NEVER}.
12075 *
12076 * @return This view's over-scroll mode.
12077 */
12078 public int getOverScrollMode() {
12079 return mOverScrollMode;
12080 }
12081
12082 /**
12083 * Set the over-scroll mode for this view. Valid over-scroll modes are
12084 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12085 * (allow over-scrolling only if the view content is larger than the container),
12086 * or {@link #OVER_SCROLL_NEVER}.
12087 *
12088 * Setting the over-scroll mode of a view will have an effect only if the
12089 * view is capable of scrolling.
12090 *
12091 * @param overScrollMode The new over-scroll mode for this view.
12092 */
12093 public void setOverScrollMode(int overScrollMode) {
12094 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12095 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12096 overScrollMode != OVER_SCROLL_NEVER) {
12097 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12098 }
12099 mOverScrollMode = overScrollMode;
12100 }
12101
12102 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012103 * Gets a scale factor that determines the distance the view should scroll
12104 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12105 * @return The vertical scroll scale factor.
12106 * @hide
12107 */
12108 protected float getVerticalScrollFactor() {
12109 if (mVerticalScrollFactor == 0) {
12110 TypedValue outValue = new TypedValue();
12111 if (!mContext.getTheme().resolveAttribute(
12112 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12113 throw new IllegalStateException(
12114 "Expected theme to define listPreferredItemHeight.");
12115 }
12116 mVerticalScrollFactor = outValue.getDimension(
12117 mContext.getResources().getDisplayMetrics());
12118 }
12119 return mVerticalScrollFactor;
12120 }
12121
12122 /**
12123 * Gets a scale factor that determines the distance the view should scroll
12124 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12125 * @return The horizontal scroll scale factor.
12126 * @hide
12127 */
12128 protected float getHorizontalScrollFactor() {
12129 // TODO: Should use something else.
12130 return getVerticalScrollFactor();
12131 }
12132
12133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012134 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
12135 * Each MeasureSpec represents a requirement for either the width or the height.
12136 * A MeasureSpec is comprised of a size and a mode. There are three possible
12137 * modes:
12138 * <dl>
12139 * <dt>UNSPECIFIED</dt>
12140 * <dd>
12141 * The parent has not imposed any constraint on the child. It can be whatever size
12142 * it wants.
12143 * </dd>
12144 *
12145 * <dt>EXACTLY</dt>
12146 * <dd>
12147 * The parent has determined an exact size for the child. The child is going to be
12148 * given those bounds regardless of how big it wants to be.
12149 * </dd>
12150 *
12151 * <dt>AT_MOST</dt>
12152 * <dd>
12153 * The child can be as large as it wants up to the specified size.
12154 * </dd>
12155 * </dl>
12156 *
12157 * MeasureSpecs are implemented as ints to reduce object allocation. This class
12158 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
12159 */
12160 public static class MeasureSpec {
12161 private static final int MODE_SHIFT = 30;
12162 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
12163
12164 /**
12165 * Measure specification mode: The parent has not imposed any constraint
12166 * on the child. It can be whatever size it wants.
12167 */
12168 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
12169
12170 /**
12171 * Measure specification mode: The parent has determined an exact size
12172 * for the child. The child is going to be given those bounds regardless
12173 * of how big it wants to be.
12174 */
12175 public static final int EXACTLY = 1 << MODE_SHIFT;
12176
12177 /**
12178 * Measure specification mode: The child can be as large as it wants up
12179 * to the specified size.
12180 */
12181 public static final int AT_MOST = 2 << MODE_SHIFT;
12182
12183 /**
12184 * Creates a measure specification based on the supplied size and mode.
12185 *
12186 * The mode must always be one of the following:
12187 * <ul>
12188 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
12189 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
12190 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
12191 * </ul>
12192 *
12193 * @param size the size of the measure specification
12194 * @param mode the mode of the measure specification
12195 * @return the measure specification based on size and mode
12196 */
12197 public static int makeMeasureSpec(int size, int mode) {
12198 return size + mode;
12199 }
12200
12201 /**
12202 * Extracts the mode from the supplied measure specification.
12203 *
12204 * @param measureSpec the measure specification to extract the mode from
12205 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
12206 * {@link android.view.View.MeasureSpec#AT_MOST} or
12207 * {@link android.view.View.MeasureSpec#EXACTLY}
12208 */
12209 public static int getMode(int measureSpec) {
12210 return (measureSpec & MODE_MASK);
12211 }
12212
12213 /**
12214 * Extracts the size from the supplied measure specification.
12215 *
12216 * @param measureSpec the measure specification to extract the size from
12217 * @return the size in pixels defined in the supplied measure specification
12218 */
12219 public static int getSize(int measureSpec) {
12220 return (measureSpec & ~MODE_MASK);
12221 }
12222
12223 /**
12224 * Returns a String representation of the specified measure
12225 * specification.
12226 *
12227 * @param measureSpec the measure specification to convert to a String
12228 * @return a String with the following format: "MeasureSpec: MODE SIZE"
12229 */
12230 public static String toString(int measureSpec) {
12231 int mode = getMode(measureSpec);
12232 int size = getSize(measureSpec);
12233
12234 StringBuilder sb = new StringBuilder("MeasureSpec: ");
12235
12236 if (mode == UNSPECIFIED)
12237 sb.append("UNSPECIFIED ");
12238 else if (mode == EXACTLY)
12239 sb.append("EXACTLY ");
12240 else if (mode == AT_MOST)
12241 sb.append("AT_MOST ");
12242 else
12243 sb.append(mode).append(" ");
12244
12245 sb.append(size);
12246 return sb.toString();
12247 }
12248 }
12249
12250 class CheckForLongPress implements Runnable {
12251
12252 private int mOriginalWindowAttachCount;
12253
12254 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070012255 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012256 && mOriginalWindowAttachCount == mWindowAttachCount) {
12257 if (performLongClick()) {
12258 mHasPerformedLongPress = true;
12259 }
12260 }
12261 }
12262
12263 public void rememberWindowAttachCount() {
12264 mOriginalWindowAttachCount = mWindowAttachCount;
12265 }
12266 }
Joe Malin32736f02011-01-19 16:14:20 -080012267
Adam Powelle14579b2009-12-16 18:39:52 -080012268 private final class CheckForTap implements Runnable {
12269 public void run() {
12270 mPrivateFlags &= ~PREPRESSED;
12271 mPrivateFlags |= PRESSED;
12272 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012273 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080012274 }
12275 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012276
Adam Powella35d7682010-03-12 14:48:13 -080012277 private final class PerformClick implements Runnable {
12278 public void run() {
12279 performClick();
12280 }
12281 }
12282
Dianne Hackborn63042d62011-01-26 18:56:29 -080012283 /** @hide */
12284 public void hackTurnOffWindowResizeAnim(boolean off) {
12285 mAttachInfo.mTurnOffWindowResizeAnim = off;
12286 }
Joe Malin32736f02011-01-19 16:14:20 -080012287
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012288 /**
Chet Haasea00f3862011-02-22 06:34:40 -080012289 * This method returns a ViewPropertyAnimator object, which can be used to animate
12290 * specific properties on this View.
12291 *
12292 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
12293 */
12294 public ViewPropertyAnimator animate() {
12295 if (mAnimator == null) {
12296 mAnimator = new ViewPropertyAnimator(this);
12297 }
12298 return mAnimator;
12299 }
12300
12301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012302 * Interface definition for a callback to be invoked when a key event is
12303 * dispatched to this view. The callback will be invoked before the key
12304 * event is given to the view.
12305 */
12306 public interface OnKeyListener {
12307 /**
12308 * Called when a key is dispatched to a view. This allows listeners to
12309 * get a chance to respond before the target view.
12310 *
12311 * @param v The view the key has been dispatched to.
12312 * @param keyCode The code for the physical key that was pressed
12313 * @param event The KeyEvent object containing full information about
12314 * the event.
12315 * @return True if the listener has consumed the event, false otherwise.
12316 */
12317 boolean onKey(View v, int keyCode, KeyEvent event);
12318 }
12319
12320 /**
12321 * Interface definition for a callback to be invoked when a touch event is
12322 * dispatched to this view. The callback will be invoked before the touch
12323 * event is given to the view.
12324 */
12325 public interface OnTouchListener {
12326 /**
12327 * Called when a touch event is dispatched to a view. This allows listeners to
12328 * get a chance to respond before the target view.
12329 *
12330 * @param v The view the touch event has been dispatched to.
12331 * @param event The MotionEvent object containing full information about
12332 * the event.
12333 * @return True if the listener has consumed the event, false otherwise.
12334 */
12335 boolean onTouch(View v, MotionEvent event);
12336 }
12337
12338 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012339 * Interface definition for a callback to be invoked when a generic motion event is
12340 * dispatched to this view. The callback will be invoked before the generic motion
12341 * event is given to the view.
12342 */
12343 public interface OnGenericMotionListener {
12344 /**
12345 * Called when a generic motion event is dispatched to a view. This allows listeners to
12346 * get a chance to respond before the target view.
12347 *
12348 * @param v The view the generic motion event has been dispatched to.
12349 * @param event The MotionEvent object containing full information about
12350 * the event.
12351 * @return True if the listener has consumed the event, false otherwise.
12352 */
12353 boolean onGenericMotion(View v, MotionEvent event);
12354 }
12355
12356 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012357 * Interface definition for a callback to be invoked when a view has been clicked and held.
12358 */
12359 public interface OnLongClickListener {
12360 /**
12361 * Called when a view has been clicked and held.
12362 *
12363 * @param v The view that was clicked and held.
12364 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080012365 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012366 */
12367 boolean onLongClick(View v);
12368 }
12369
12370 /**
Chris Tate32affef2010-10-18 15:29:21 -070012371 * Interface definition for a callback to be invoked when a drag is being dispatched
12372 * to this view. The callback will be invoked before the hosting view's own
12373 * onDrag(event) method. If the listener wants to fall back to the hosting view's
12374 * onDrag(event) behavior, it should return 'false' from this callback.
12375 */
12376 public interface OnDragListener {
12377 /**
12378 * Called when a drag event is dispatched to a view. This allows listeners
12379 * to get a chance to override base View behavior.
12380 *
Joe Malin32736f02011-01-19 16:14:20 -080012381 * @param v The View that received the drag event.
12382 * @param event The {@link android.view.DragEvent} object for the drag event.
12383 * @return {@code true} if the drag event was handled successfully, or {@code false}
12384 * if the drag event was not handled. Note that {@code false} will trigger the View
12385 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070012386 */
12387 boolean onDrag(View v, DragEvent event);
12388 }
12389
12390 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012391 * Interface definition for a callback to be invoked when the focus state of
12392 * a view changed.
12393 */
12394 public interface OnFocusChangeListener {
12395 /**
12396 * Called when the focus state of a view has changed.
12397 *
12398 * @param v The view whose state has changed.
12399 * @param hasFocus The new focus state of v.
12400 */
12401 void onFocusChange(View v, boolean hasFocus);
12402 }
12403
12404 /**
12405 * Interface definition for a callback to be invoked when a view is clicked.
12406 */
12407 public interface OnClickListener {
12408 /**
12409 * Called when a view has been clicked.
12410 *
12411 * @param v The view that was clicked.
12412 */
12413 void onClick(View v);
12414 }
12415
12416 /**
12417 * Interface definition for a callback to be invoked when the context menu
12418 * for this view is being built.
12419 */
12420 public interface OnCreateContextMenuListener {
12421 /**
12422 * Called when the context menu for this view is being built. It is not
12423 * safe to hold onto the menu after this method returns.
12424 *
12425 * @param menu The context menu that is being built
12426 * @param v The view for which the context menu is being built
12427 * @param menuInfo Extra information about the item for which the
12428 * context menu should be shown. This information will vary
12429 * depending on the class of v.
12430 */
12431 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
12432 }
12433
Joe Onorato664644d2011-01-23 17:53:23 -080012434 /**
12435 * Interface definition for a callback to be invoked when the status bar changes
12436 * visibility.
12437 *
12438 * @see #setOnSystemUiVisibilityChangeListener
12439 */
12440 public interface OnSystemUiVisibilityChangeListener {
12441 /**
12442 * Called when the status bar changes visibility because of a call to
12443 * {@link #setSystemUiVisibility}.
12444 *
12445 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
12446 */
12447 public void onSystemUiVisibilityChange(int visibility);
12448 }
12449
Adam Powell4afd62b2011-02-18 15:02:18 -080012450 /**
12451 * Interface definition for a callback to be invoked when this view is attached
12452 * or detached from its window.
12453 */
12454 public interface OnAttachStateChangeListener {
12455 /**
12456 * Called when the view is attached to a window.
12457 * @param v The view that was attached
12458 */
12459 public void onViewAttachedToWindow(View v);
12460 /**
12461 * Called when the view is detached from a window.
12462 * @param v The view that was detached
12463 */
12464 public void onViewDetachedFromWindow(View v);
12465 }
12466
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467 private final class UnsetPressedState implements Runnable {
12468 public void run() {
12469 setPressed(false);
12470 }
12471 }
12472
12473 /**
12474 * Base class for derived classes that want to save and restore their own
12475 * state in {@link android.view.View#onSaveInstanceState()}.
12476 */
12477 public static class BaseSavedState extends AbsSavedState {
12478 /**
12479 * Constructor used when reading from a parcel. Reads the state of the superclass.
12480 *
12481 * @param source
12482 */
12483 public BaseSavedState(Parcel source) {
12484 super(source);
12485 }
12486
12487 /**
12488 * Constructor called by derived classes when creating their SavedState objects
12489 *
12490 * @param superState The state of the superclass of this view
12491 */
12492 public BaseSavedState(Parcelable superState) {
12493 super(superState);
12494 }
12495
12496 public static final Parcelable.Creator<BaseSavedState> CREATOR =
12497 new Parcelable.Creator<BaseSavedState>() {
12498 public BaseSavedState createFromParcel(Parcel in) {
12499 return new BaseSavedState(in);
12500 }
12501
12502 public BaseSavedState[] newArray(int size) {
12503 return new BaseSavedState[size];
12504 }
12505 };
12506 }
12507
12508 /**
12509 * A set of information given to a view when it is attached to its parent
12510 * window.
12511 */
12512 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012513 interface Callbacks {
12514 void playSoundEffect(int effectId);
12515 boolean performHapticFeedback(int effectId, boolean always);
12516 }
12517
12518 /**
12519 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
12520 * to a Handler. This class contains the target (View) to invalidate and
12521 * the coordinates of the dirty rectangle.
12522 *
12523 * For performance purposes, this class also implements a pool of up to
12524 * POOL_LIMIT objects that get reused. This reduces memory allocations
12525 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012526 */
Romain Guyd928d682009-03-31 17:52:16 -070012527 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012528 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070012529 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
12530 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070012531 public InvalidateInfo newInstance() {
12532 return new InvalidateInfo();
12533 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012534
Romain Guyd928d682009-03-31 17:52:16 -070012535 public void onAcquired(InvalidateInfo element) {
12536 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012537
Romain Guyd928d682009-03-31 17:52:16 -070012538 public void onReleased(InvalidateInfo element) {
12539 }
12540 }, POOL_LIMIT)
12541 );
12542
12543 private InvalidateInfo mNext;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012544
12545 View target;
12546
12547 int left;
12548 int top;
12549 int right;
12550 int bottom;
12551
Romain Guyd928d682009-03-31 17:52:16 -070012552 public void setNextPoolable(InvalidateInfo element) {
12553 mNext = element;
12554 }
12555
12556 public InvalidateInfo getNextPoolable() {
12557 return mNext;
12558 }
12559
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012560 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070012561 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012562 }
12563
12564 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070012565 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012566 }
12567 }
12568
12569 final IWindowSession mSession;
12570
12571 final IWindow mWindow;
12572
12573 final IBinder mWindowToken;
12574
12575 final Callbacks mRootCallbacks;
12576
Chet Haasedaf98e92011-01-10 14:10:36 -080012577 Canvas mHardwareCanvas;
12578
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012579 /**
12580 * The top view of the hierarchy.
12581 */
12582 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070012583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012584 IBinder mPanelParentWindowToken;
12585 Surface mSurface;
12586
Romain Guyb051e892010-09-28 19:09:36 -070012587 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080012588 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070012589 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080012590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012591 /**
Romain Guy8506ab42009-06-11 17:35:47 -070012592 * Scale factor used by the compatibility mode
12593 */
12594 float mApplicationScale;
12595
12596 /**
12597 * Indicates whether the application is in compatibility mode
12598 */
12599 boolean mScalingRequired;
12600
12601 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012602 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080012603 */
12604 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080012605
Dianne Hackborn63042d62011-01-26 18:56:29 -080012606 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012607 * Left position of this view's window
12608 */
12609 int mWindowLeft;
12610
12611 /**
12612 * Top position of this view's window
12613 */
12614 int mWindowTop;
12615
12616 /**
Adam Powell26153a32010-11-08 15:22:27 -080012617 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070012618 */
Adam Powell26153a32010-11-08 15:22:27 -080012619 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070012620
12621 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012622 * For windows that are full-screen but using insets to layout inside
12623 * of the screen decorations, these are the current insets for the
12624 * content of the window.
12625 */
12626 final Rect mContentInsets = new Rect();
12627
12628 /**
12629 * For windows that are full-screen but using insets to layout inside
12630 * of the screen decorations, these are the current insets for the
12631 * actual visible parts of the window.
12632 */
12633 final Rect mVisibleInsets = new Rect();
12634
12635 /**
12636 * The internal insets given by this window. This value is
12637 * supplied by the client (through
12638 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
12639 * be given to the window manager when changed to be used in laying
12640 * out windows behind it.
12641 */
12642 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
12643 = new ViewTreeObserver.InternalInsetsInfo();
12644
12645 /**
12646 * All views in the window's hierarchy that serve as scroll containers,
12647 * used to determine if the window can be resized or must be panned
12648 * to adjust for a soft input area.
12649 */
12650 final ArrayList<View> mScrollContainers = new ArrayList<View>();
12651
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070012652 final KeyEvent.DispatcherState mKeyDispatchState
12653 = new KeyEvent.DispatcherState();
12654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012655 /**
12656 * Indicates whether the view's window currently has the focus.
12657 */
12658 boolean mHasWindowFocus;
12659
12660 /**
12661 * The current visibility of the window.
12662 */
12663 int mWindowVisibility;
12664
12665 /**
12666 * Indicates the time at which drawing started to occur.
12667 */
12668 long mDrawingTime;
12669
12670 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070012671 * Indicates whether or not ignoring the DIRTY_MASK flags.
12672 */
12673 boolean mIgnoreDirtyState;
12674
12675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012676 * Indicates whether the view's window is currently in touch mode.
12677 */
12678 boolean mInTouchMode;
12679
12680 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012681 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012682 * the next time it performs a traversal
12683 */
12684 boolean mRecomputeGlobalAttributes;
12685
12686 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012687 * Set during a traveral if any views want to keep the screen on.
12688 */
12689 boolean mKeepScreenOn;
12690
12691 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012692 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
12693 */
12694 int mSystemUiVisibility;
12695
12696 /**
12697 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
12698 * attached.
12699 */
12700 boolean mHasSystemUiListeners;
12701
12702 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012703 * Set if the visibility of any views has changed.
12704 */
12705 boolean mViewVisibilityChanged;
12706
12707 /**
12708 * Set to true if a view has been scrolled.
12709 */
12710 boolean mViewScrollChanged;
12711
12712 /**
12713 * Global to the view hierarchy used as a temporary for dealing with
12714 * x/y points in the transparent region computations.
12715 */
12716 final int[] mTransparentLocation = new int[2];
12717
12718 /**
12719 * Global to the view hierarchy used as a temporary for dealing with
12720 * x/y points in the ViewGroup.invalidateChild implementation.
12721 */
12722 final int[] mInvalidateChildLocation = new int[2];
12723
Chet Haasec3aa3612010-06-17 08:50:37 -070012724
12725 /**
12726 * Global to the view hierarchy used as a temporary for dealing with
12727 * x/y location when view is transformed.
12728 */
12729 final float[] mTmpTransformLocation = new float[2];
12730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012731 /**
12732 * The view tree observer used to dispatch global events like
12733 * layout, pre-draw, touch mode change, etc.
12734 */
12735 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
12736
12737 /**
12738 * A Canvas used by the view hierarchy to perform bitmap caching.
12739 */
12740 Canvas mCanvas;
12741
12742 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012743 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012744 * handler can be used to pump events in the UI events queue.
12745 */
12746 final Handler mHandler;
12747
12748 /**
12749 * Identifier for messages requesting the view to be invalidated.
12750 * Such messages should be sent to {@link #mHandler}.
12751 */
12752 static final int INVALIDATE_MSG = 0x1;
12753
12754 /**
12755 * Identifier for messages requesting the view to invalidate a region.
12756 * Such messages should be sent to {@link #mHandler}.
12757 */
12758 static final int INVALIDATE_RECT_MSG = 0x2;
12759
12760 /**
12761 * Temporary for use in computing invalidate rectangles while
12762 * calling up the hierarchy.
12763 */
12764 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070012765
12766 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070012767 * Temporary for use in computing hit areas with transformed views
12768 */
12769 final RectF mTmpTransformRect = new RectF();
12770
12771 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070012772 * Temporary list for use in collecting focusable descendents of a view.
12773 */
12774 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
12775
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012776 /**
12777 * Creates a new set of attachment information with the specified
12778 * events handler and thread.
12779 *
12780 * @param handler the events handler the view must use
12781 */
12782 AttachInfo(IWindowSession session, IWindow window,
12783 Handler handler, Callbacks effectPlayer) {
12784 mSession = session;
12785 mWindow = window;
12786 mWindowToken = window.asBinder();
12787 mHandler = handler;
12788 mRootCallbacks = effectPlayer;
12789 }
12790 }
12791
12792 /**
12793 * <p>ScrollabilityCache holds various fields used by a View when scrolling
12794 * is supported. This avoids keeping too many unused fields in most
12795 * instances of View.</p>
12796 */
Mike Cleronf116bf82009-09-27 19:14:12 -070012797 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080012798
Mike Cleronf116bf82009-09-27 19:14:12 -070012799 /**
12800 * Scrollbars are not visible
12801 */
12802 public static final int OFF = 0;
12803
12804 /**
12805 * Scrollbars are visible
12806 */
12807 public static final int ON = 1;
12808
12809 /**
12810 * Scrollbars are fading away
12811 */
12812 public static final int FADING = 2;
12813
12814 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080012815
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012816 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070012817 public int scrollBarDefaultDelayBeforeFade;
12818 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012819
12820 public int scrollBarSize;
12821 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070012822 public float[] interpolatorValues;
12823 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012824
12825 public final Paint paint;
12826 public final Matrix matrix;
12827 public Shader shader;
12828
Mike Cleronf116bf82009-09-27 19:14:12 -070012829 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
12830
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012831 private static final float[] OPAQUE = { 255 };
12832 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080012833
Mike Cleronf116bf82009-09-27 19:14:12 -070012834 /**
12835 * When fading should start. This time moves into the future every time
12836 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
12837 */
12838 public long fadeStartTime;
12839
12840
12841 /**
12842 * The current state of the scrollbars: ON, OFF, or FADING
12843 */
12844 public int state = OFF;
12845
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012846 private int mLastColor;
12847
Mike Cleronf116bf82009-09-27 19:14:12 -070012848 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012849 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
12850 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070012851 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
12852 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012853
12854 paint = new Paint();
12855 matrix = new Matrix();
12856 // use use a height of 1, and then wack the matrix each time we
12857 // actually use it.
12858 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012859
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012860 paint.setShader(shader);
12861 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070012862 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012863 }
Romain Guy8506ab42009-06-11 17:35:47 -070012864
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012865 public void setFadeColor(int color) {
12866 if (color != 0 && color != mLastColor) {
12867 mLastColor = color;
12868 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070012869
Romain Guye55e1a72009-08-27 10:42:26 -070012870 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
12871 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070012872
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012873 paint.setShader(shader);
12874 // Restore the default transfer mode (src_over)
12875 paint.setXfermode(null);
12876 }
12877 }
Joe Malin32736f02011-01-19 16:14:20 -080012878
Mike Cleronf116bf82009-09-27 19:14:12 -070012879 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070012880 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070012881 if (now >= fadeStartTime) {
12882
12883 // the animation fades the scrollbars out by changing
12884 // the opacity (alpha) from fully opaque to fully
12885 // transparent
12886 int nextFrame = (int) now;
12887 int framesCount = 0;
12888
12889 Interpolator interpolator = scrollBarInterpolator;
12890
12891 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012892 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070012893
12894 // End transparent
12895 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080012896 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070012897
12898 state = FADING;
12899
12900 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080012901 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070012902 }
12903 }
12904
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012905 }
12906}