blob: 74dc10071e6bb60573256f9a823580e72d24b273 [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
Chet Haaseb39f0512011-05-24 14:36:40 -070019import android.util.FloatProperty;
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -070020import android.util.LocaleUtil;
Chet Haaseb39f0512011-05-24 14:36:40 -070021import android.util.Property;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080022import com.android.internal.R;
23import com.android.internal.util.Predicate;
24import com.android.internal.view.menu.MenuBuilder;
25
Christopher Tatea53146c2010-09-07 11:57:52 -070026import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080028import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.content.res.Resources;
30import android.content.res.TypedArray;
31import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070032import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070034import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.graphics.LinearGradient;
36import android.graphics.Matrix;
37import android.graphics.Paint;
38import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.graphics.PorterDuff;
41import android.graphics.PorterDuffXfermode;
42import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070043import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.graphics.Region;
45import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.graphics.drawable.ColorDrawable;
47import android.graphics.drawable.Drawable;
48import android.os.Handler;
49import android.os.IBinder;
50import android.os.Message;
51import android.os.Parcel;
52import android.os.Parcelable;
53import android.os.RemoteException;
54import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.util.AttributeSet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070057import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070059import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.util.Pools;
61import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080062import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityEventSource;
66import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070067import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070069import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070070import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071import android.view.inputmethod.InputConnection;
72import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073import android.widget.ScrollBarDrawable;
74
Christopher Tatea0374192010-10-05 13:06:41 -070075import java.lang.ref.WeakReference;
svetoslavganov75986cf2009-05-14 22:28:01 -070076import java.lang.reflect.InvocationTargetException;
77import java.lang.reflect.Method;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078import java.util.ArrayList;
79import java.util.Arrays;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -070080import java.util.Locale;
Romain Guyd90a3312009-05-06 14:54:28 -070081import java.util.WeakHashMap;
Adam Powell4afd62b2011-02-18 15:02:18 -080082import java.util.concurrent.CopyOnWriteArrayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84/**
85 * <p>
86 * This class represents the basic building block for user interface components. A View
87 * occupies a rectangular area on the screen and is responsible for drawing and
88 * event handling. View is the base class for <em>widgets</em>, which are
Romain Guy8506ab42009-06-11 17:35:47 -070089 * used to create interactive UI components (buttons, text fields, etc.). The
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * {@link android.view.ViewGroup} subclass is the base class for <em>layouts</em>, which
91 * are invisible containers that hold other Views (or other ViewGroups) and define
92 * their layout properties.
93 * </p>
94 *
95 * <div class="special">
Romain Guy8506ab42009-06-11 17:35:47 -070096 * <p>For an introduction to using this class to develop your
97 * application's user interface, read the Developer Guide documentation on
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * <strong><a href="{@docRoot}guide/topics/ui/index.html">User Interface</a></strong>. Special topics
Romain Guy8506ab42009-06-11 17:35:47 -070099 * include:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * <br/><a href="{@docRoot}guide/topics/ui/declaring-layout.html">Declaring Layout</a>
101 * <br/><a href="{@docRoot}guide/topics/ui/menus.html">Creating Menus</a>
102 * <br/><a href="{@docRoot}guide/topics/ui/layout-objects.html">Common Layout Objects</a>
103 * <br/><a href="{@docRoot}guide/topics/ui/binding.html">Binding to Data with AdapterView</a>
104 * <br/><a href="{@docRoot}guide/topics/ui/ui-events.html">Handling UI Events</a>
105 * <br/><a href="{@docRoot}guide/topics/ui/themes.html">Applying Styles and Themes</a>
106 * <br/><a href="{@docRoot}guide/topics/ui/custom-components.html">Building Custom Components</a>
107 * <br/><a href="{@docRoot}guide/topics/ui/how-android-draws.html">How Android Draws Views</a>.
108 * </p>
109 * </div>
Romain Guy8506ab42009-06-11 17:35:47 -0700110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * <a name="Using"></a>
112 * <h3>Using Views</h3>
113 * <p>
114 * All of the views in a window are arranged in a single tree. You can add views
115 * either from code or by specifying a tree of views in one or more XML layout
116 * files. There are many specialized subclasses of views that act as controls or
117 * are capable of displaying text, images, or other content.
118 * </p>
119 * <p>
120 * Once you have created a tree of views, there are typically a few types of
121 * common operations you may wish to perform:
122 * <ul>
123 * <li><strong>Set properties:</strong> for example setting the text of a
124 * {@link android.widget.TextView}. The available properties and the methods
125 * that set them will vary among the different subclasses of views. Note that
126 * properties that are known at build time can be set in the XML layout
127 * files.</li>
128 * <li><strong>Set focus:</strong> The framework will handled moving focus in
129 * response to user input. To force focus to a specific view, call
130 * {@link #requestFocus}.</li>
131 * <li><strong>Set up listeners:</strong> Views allow clients to set listeners
132 * that will be notified when something interesting happens to the view. For
133 * example, all views will let you set a listener to be notified when the view
134 * gains or loses focus. You can register such a listener using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700135 * {@link #setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
136 * Other view subclasses offer more specialized listeners. For example, a Button
137 * exposes a listener to notify clients when the button is clicked.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * <li><strong>Set visibility:</strong> You can hide or show views using
Romain Guy5c22a8c2011-05-13 11:48:45 -0700139 * {@link #setVisibility(int)}.</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 * </ul>
141 * </p>
142 * <p><em>
143 * Note: The Android framework is responsible for measuring, laying out and
144 * drawing views. You should not call methods that perform these actions on
145 * views yourself unless you are actually implementing a
146 * {@link android.view.ViewGroup}.
147 * </em></p>
148 *
149 * <a name="Lifecycle"></a>
150 * <h3>Implementing a Custom View</h3>
151 *
152 * <p>
153 * To implement a custom view, you will usually begin by providing overrides for
154 * some of the standard methods that the framework calls on all views. You do
155 * not need to override all of these methods. In fact, you can start by just
156 * overriding {@link #onDraw(android.graphics.Canvas)}.
157 * <table border="2" width="85%" align="center" cellpadding="5">
158 * <thead>
159 * <tr><th>Category</th> <th>Methods</th> <th>Description</th></tr>
160 * </thead>
161 *
162 * <tbody>
163 * <tr>
164 * <td rowspan="2">Creation</td>
165 * <td>Constructors</td>
166 * <td>There is a form of the constructor that are called when the view
167 * is created from code and a form that is called when the view is
168 * inflated from a layout file. The second form should parse and apply
169 * any attributes defined in the layout file.
170 * </td>
171 * </tr>
172 * <tr>
173 * <td><code>{@link #onFinishInflate()}</code></td>
174 * <td>Called after a view and all of its children has been inflated
175 * from XML.</td>
176 * </tr>
177 *
178 * <tr>
179 * <td rowspan="3">Layout</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700180 * <td><code>{@link #onMeasure(int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 * <td>Called to determine the size requirements for this view and all
182 * of its children.
183 * </td>
184 * </tr>
185 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700186 * <td><code>{@link #onLayout(boolean, int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * <td>Called when this view should assign a size and position to all
188 * of its children.
189 * </td>
190 * </tr>
191 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700192 * <td><code>{@link #onSizeChanged(int, int, int, int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * <td>Called when the size of this view has changed.
194 * </td>
195 * </tr>
196 *
197 * <tr>
198 * <td>Drawing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700199 * <td><code>{@link #onDraw(android.graphics.Canvas)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * <td>Called when the view should render its content.
201 * </td>
202 * </tr>
203 *
204 * <tr>
205 * <td rowspan="4">Event processing</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700206 * <td><code>{@link #onKeyDown(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * <td>Called when a new key event occurs.
208 * </td>
209 * </tr>
210 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700211 * <td><code>{@link #onKeyUp(int, KeyEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <td>Called when a key up event occurs.
213 * </td>
214 * </tr>
215 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700216 * <td><code>{@link #onTrackballEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 * <td>Called when a trackball motion event occurs.
218 * </td>
219 * </tr>
220 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700221 * <td><code>{@link #onTouchEvent(MotionEvent)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 * <td>Called when a touch screen motion event occurs.
223 * </td>
224 * </tr>
225 *
226 * <tr>
227 * <td rowspan="2">Focus</td>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700228 * <td><code>{@link #onFocusChanged(boolean, int, android.graphics.Rect)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 * <td>Called when the view gains or loses focus.
230 * </td>
231 * </tr>
232 *
233 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700234 * <td><code>{@link #onWindowFocusChanged(boolean)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <td>Called when the window containing the view gains or loses focus.
236 * </td>
237 * </tr>
238 *
239 * <tr>
240 * <td rowspan="3">Attaching</td>
241 * <td><code>{@link #onAttachedToWindow()}</code></td>
242 * <td>Called when the view is attached to a window.
243 * </td>
244 * </tr>
245 *
246 * <tr>
247 * <td><code>{@link #onDetachedFromWindow}</code></td>
248 * <td>Called when the view is detached from its window.
249 * </td>
250 * </tr>
251 *
252 * <tr>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700253 * <td><code>{@link #onWindowVisibilityChanged(int)}</code></td>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800254 * <td>Called when the visibility of the window containing the view
255 * has changed.
256 * </td>
257 * </tr>
258 * </tbody>
259 *
260 * </table>
261 * </p>
262 *
263 * <a name="IDs"></a>
264 * <h3>IDs</h3>
265 * Views may have an integer id associated with them. These ids are typically
266 * assigned in the layout XML files, and are used to find specific views within
267 * the view tree. A common pattern is to:
268 * <ul>
269 * <li>Define a Button in the layout file and assign it a unique ID.
270 * <pre>
Gilles Debunne0243caf2010-08-24 23:06:35 -0700271 * &lt;Button
272 * android:id="@+id/my_button"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800273 * android:layout_width="wrap_content"
274 * android:layout_height="wrap_content"
275 * android:text="@string/my_button_text"/&gt;
276 * </pre></li>
277 * <li>From the onCreate method of an Activity, find the Button
278 * <pre class="prettyprint">
279 * Button myButton = (Button) findViewById(R.id.my_button);
280 * </pre></li>
281 * </ul>
282 * <p>
283 * View IDs need not be unique throughout the tree, but it is good practice to
284 * ensure that they are at least unique within the part of the tree you are
285 * searching.
286 * </p>
287 *
288 * <a name="Position"></a>
289 * <h3>Position</h3>
290 * <p>
291 * The geometry of a view is that of a rectangle. A view has a location,
292 * expressed as a pair of <em>left</em> and <em>top</em> coordinates, and
293 * two dimensions, expressed as a width and a height. The unit for location
294 * and dimensions is the pixel.
295 * </p>
296 *
297 * <p>
298 * It is possible to retrieve the location of a view by invoking the methods
299 * {@link #getLeft()} and {@link #getTop()}. The former returns the left, or X,
300 * coordinate of the rectangle representing the view. The latter returns the
301 * top, or Y, coordinate of the rectangle representing the view. These methods
302 * both return the location of the view relative to its parent. For instance,
303 * when getLeft() returns 20, that means the view is located 20 pixels to the
304 * right of the left edge of its direct parent.
305 * </p>
306 *
307 * <p>
308 * In addition, several convenience methods are offered to avoid unnecessary
309 * computations, namely {@link #getRight()} and {@link #getBottom()}.
310 * These methods return the coordinates of the right and bottom edges of the
311 * rectangle representing the view. For instance, calling {@link #getRight()}
312 * is similar to the following computation: <code>getLeft() + getWidth()</code>
313 * (see <a href="#SizePaddingMargins">Size</a> for more information about the width.)
314 * </p>
315 *
316 * <a name="SizePaddingMargins"></a>
317 * <h3>Size, padding and margins</h3>
318 * <p>
319 * The size of a view is expressed with a width and a height. A view actually
320 * possess two pairs of width and height values.
321 * </p>
322 *
323 * <p>
324 * The first pair is known as <em>measured width</em> and
325 * <em>measured height</em>. These dimensions define how big a view wants to be
326 * within its parent (see <a href="#Layout">Layout</a> for more details.) The
327 * measured dimensions can be obtained by calling {@link #getMeasuredWidth()}
328 * and {@link #getMeasuredHeight()}.
329 * </p>
330 *
331 * <p>
332 * The second pair is simply known as <em>width</em> and <em>height</em>, or
333 * sometimes <em>drawing width</em> and <em>drawing height</em>. These
334 * dimensions define the actual size of the view on screen, at drawing time and
335 * after layout. These values may, but do not have to, be different from the
336 * measured width and height. The width and height can be obtained by calling
337 * {@link #getWidth()} and {@link #getHeight()}.
338 * </p>
339 *
340 * <p>
341 * To measure its dimensions, a view takes into account its padding. The padding
342 * is expressed in pixels for the left, top, right and bottom parts of the view.
343 * Padding can be used to offset the content of the view by a specific amount of
344 * pixels. For instance, a left padding of 2 will push the view's content by
345 * 2 pixels to the right of the left edge. Padding can be set using the
346 * {@link #setPadding(int, int, int, int)} method and queried by calling
347 * {@link #getPaddingLeft()}, {@link #getPaddingTop()},
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700348 * {@link #getPaddingRight()}, {@link #getPaddingBottom()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 * </p>
350 *
351 * <p>
352 * Even though a view can define a padding, it does not provide any support for
353 * margins. However, view groups provide such a support. Refer to
354 * {@link android.view.ViewGroup} and
355 * {@link android.view.ViewGroup.MarginLayoutParams} for further information.
356 * </p>
357 *
358 * <a name="Layout"></a>
359 * <h3>Layout</h3>
360 * <p>
361 * Layout is a two pass process: a measure pass and a layout pass. The measuring
362 * pass is implemented in {@link #measure(int, int)} and is a top-down traversal
363 * of the view tree. Each view pushes dimension specifications down the tree
364 * during the recursion. At the end of the measure pass, every view has stored
365 * its measurements. The second pass happens in
366 * {@link #layout(int,int,int,int)} and is also top-down. During
367 * this pass each parent is responsible for positioning all of its children
368 * using the sizes computed in the measure pass.
369 * </p>
370 *
371 * <p>
372 * When a view's measure() method returns, its {@link #getMeasuredWidth()} and
373 * {@link #getMeasuredHeight()} values must be set, along with those for all of
374 * that view's descendants. A view's measured width and measured height values
375 * must respect the constraints imposed by the view's parents. This guarantees
376 * that at the end of the measure pass, all parents accept all of their
377 * children's measurements. A parent view may call measure() more than once on
378 * its children. For example, the parent may measure each child once with
379 * unspecified dimensions to find out how big they want to be, then call
380 * measure() on them again with actual numbers if the sum of all the children's
381 * unconstrained sizes is too big or too small.
382 * </p>
383 *
384 * <p>
385 * The measure pass uses two classes to communicate dimensions. The
386 * {@link MeasureSpec} class is used by views to tell their parents how they
387 * want to be measured and positioned. The base LayoutParams class just
388 * describes how big the view wants to be for both width and height. For each
389 * dimension, it can specify one of:
390 * <ul>
391 * <li> an exact number
Romain Guy980a9382010-01-08 15:06:28 -0800392 * <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 -0800393 * (minus padding)
394 * <li> WRAP_CONTENT, which means that the view wants to be just big enough to
395 * enclose its content (plus padding).
396 * </ul>
397 * There are subclasses of LayoutParams for different subclasses of ViewGroup.
398 * For example, AbsoluteLayout has its own subclass of LayoutParams which adds
399 * an X and Y value.
400 * </p>
401 *
402 * <p>
403 * MeasureSpecs are used to push requirements down the tree from parent to
404 * child. A MeasureSpec can be in one of three modes:
405 * <ul>
406 * <li>UNSPECIFIED: This is used by a parent to determine the desired dimension
407 * of a child view. For example, a LinearLayout may call measure() on its child
408 * with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how
409 * tall the child view wants to be given a width of 240 pixels.
410 * <li>EXACTLY: This is used by the parent to impose an exact size on the
411 * child. The child must use this size, and guarantee that all of its
412 * descendants will fit within this size.
413 * <li>AT_MOST: This is used by the parent to impose a maximum size on the
414 * child. The child must gurantee that it and all of its descendants will fit
415 * within this size.
416 * </ul>
417 * </p>
418 *
419 * <p>
420 * To intiate a layout, call {@link #requestLayout}. This method is typically
421 * called by a view on itself when it believes that is can no longer fit within
422 * its current bounds.
423 * </p>
424 *
425 * <a name="Drawing"></a>
426 * <h3>Drawing</h3>
427 * <p>
428 * Drawing is handled by walking the tree and rendering each view that
429 * intersects the the invalid region. Because the tree is traversed in-order,
430 * this means that parents will draw before (i.e., behind) their children, with
431 * siblings drawn in the order they appear in the tree.
432 * If you set a background drawable for a View, then the View will draw it for you
433 * before calling back to its <code>onDraw()</code> method.
434 * </p>
435 *
436 * <p>
Romain Guy8506ab42009-06-11 17:35:47 -0700437 * 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 -0800438 * </p>
439 *
440 * <p>
441 * To force a view to draw, call {@link #invalidate()}.
442 * </p>
443 *
444 * <a name="EventHandlingThreading"></a>
445 * <h3>Event Handling and Threading</h3>
446 * <p>
447 * The basic cycle of a view is as follows:
448 * <ol>
449 * <li>An event comes in and is dispatched to the appropriate view. The view
450 * handles the event and notifies any listeners.</li>
451 * <li>If in the course of processing the event, the view's bounds may need
452 * to be changed, the view will call {@link #requestLayout()}.</li>
453 * <li>Similarly, if in the course of processing the event the view's appearance
454 * may need to be changed, the view will call {@link #invalidate()}.</li>
455 * <li>If either {@link #requestLayout()} or {@link #invalidate()} were called,
456 * the framework will take care of measuring, laying out, and drawing the tree
457 * as appropriate.</li>
458 * </ol>
459 * </p>
460 *
461 * <p><em>Note: The entire view tree is single threaded. You must always be on
462 * the UI thread when calling any method on any view.</em>
463 * If you are doing work on other threads and want to update the state of a view
464 * from that thread, you should use a {@link Handler}.
465 * </p>
466 *
467 * <a name="FocusHandling"></a>
468 * <h3>Focus Handling</h3>
469 * <p>
470 * The framework will handle routine focus movement in response to user input.
471 * This includes changing the focus as views are removed or hidden, or as new
472 * views become available. Views indicate their willingness to take focus
473 * through the {@link #isFocusable} method. To change whether a view can take
474 * focus, call {@link #setFocusable(boolean)}. When in touch mode (see notes below)
475 * views indicate whether they still would like focus via {@link #isFocusableInTouchMode}
476 * and can change this via {@link #setFocusableInTouchMode(boolean)}.
477 * </p>
478 * <p>
479 * Focus movement is based on an algorithm which finds the nearest neighbor in a
480 * given direction. In rare cases, the default algorithm may not match the
481 * intended behavior of the developer. In these situations, you can provide
482 * explicit overrides by using these XML attributes in the layout file:
483 * <pre>
484 * nextFocusDown
485 * nextFocusLeft
486 * nextFocusRight
487 * nextFocusUp
488 * </pre>
489 * </p>
490 *
491 *
492 * <p>
493 * To get a particular view to take focus, call {@link #requestFocus()}.
494 * </p>
495 *
496 * <a name="TouchMode"></a>
497 * <h3>Touch Mode</h3>
498 * <p>
499 * When a user is navigating a user interface via directional keys such as a D-pad, it is
500 * necessary to give focus to actionable items such as buttons so the user can see
501 * what will take input. If the device has touch capabilities, however, and the user
502 * begins interacting with the interface by touching it, it is no longer necessary to
503 * always highlight, or give focus to, a particular view. This motivates a mode
504 * for interaction named 'touch mode'.
505 * </p>
506 * <p>
507 * For a touch capable device, once the user touches the screen, the device
508 * will enter touch mode. From this point onward, only views for which
509 * {@link #isFocusableInTouchMode} is true will be focusable, such as text editing widgets.
510 * Other views that are touchable, like buttons, will not take focus when touched; they will
511 * only fire the on click listeners.
512 * </p>
513 * <p>
514 * Any time a user hits a directional key, such as a D-pad direction, the view device will
515 * exit touch mode, and find a view to take focus, so that the user may resume interacting
516 * with the user interface without touching the screen again.
517 * </p>
518 * <p>
519 * The touch mode state is maintained across {@link android.app.Activity}s. Call
520 * {@link #isInTouchMode} to see whether the device is currently in touch mode.
521 * </p>
522 *
523 * <a name="Scrolling"></a>
524 * <h3>Scrolling</h3>
525 * <p>
526 * The framework provides basic support for views that wish to internally
527 * scroll their content. This includes keeping track of the X and Y scroll
528 * offset as well as mechanisms for drawing scrollbars. See
Joe Malin32736f02011-01-19 16:14:20 -0800529 * {@link #scrollBy(int, int)}, {@link #scrollTo(int, int)}, and
Mike Cleronf116bf82009-09-27 19:14:12 -0700530 * {@link #awakenScrollBars()} for more details.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800531 * </p>
532 *
533 * <a name="Tags"></a>
534 * <h3>Tags</h3>
535 * <p>
536 * Unlike IDs, tags are not used to identify views. Tags are essentially an
537 * extra piece of information that can be associated with a view. They are most
538 * often used as a convenience to store data related to views in the views
539 * themselves rather than by putting them in a separate structure.
540 * </p>
541 *
542 * <a name="Animation"></a>
543 * <h3>Animation</h3>
544 * <p>
545 * You can attach an {@link Animation} object to a view using
546 * {@link #setAnimation(Animation)} or
547 * {@link #startAnimation(Animation)}. The animation can alter the scale,
548 * rotation, translation and alpha of a view over time. If the animation is
549 * attached to a view that has children, the animation will affect the entire
550 * subtree rooted by that node. When an animation is started, the framework will
551 * take care of redrawing the appropriate views until the animation completes.
552 * </p>
Romain Guy171c5922011-01-06 10:04:23 -0800553 * <p>
554 * Starting with Android 3.0, the preferred way of animating views is to use the
555 * {@link android.animation} package APIs.
556 * </p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 *
Jeff Brown85a31762010-09-01 17:01:00 -0700558 * <a name="Security"></a>
559 * <h3>Security</h3>
560 * <p>
561 * Sometimes it is essential that an application be able to verify that an action
562 * is being performed with the full knowledge and consent of the user, such as
563 * granting a permission request, making a purchase or clicking on an advertisement.
564 * Unfortunately, a malicious application could try to spoof the user into
565 * performing these actions, unaware, by concealing the intended purpose of the view.
566 * As a remedy, the framework offers a touch filtering mechanism that can be used to
567 * improve the security of views that provide access to sensitive functionality.
568 * </p><p>
Romain Guy5c22a8c2011-05-13 11:48:45 -0700569 * To enable touch filtering, call {@link #setFilterTouchesWhenObscured(boolean)} or set the
Jeff Brown49ed71d2010-12-06 17:13:33 -0800570 * android:filterTouchesWhenObscured layout attribute to true. When enabled, the framework
Jeff Brown85a31762010-09-01 17:01:00 -0700571 * will discard touches that are received whenever the view's window is obscured by
572 * another visible window. As a result, the view will not receive touches whenever a
573 * toast, dialog or other window appears above the view's window.
574 * </p><p>
575 * For more fine-grained control over security, consider overriding the
Romain Guy5c22a8c2011-05-13 11:48:45 -0700576 * {@link #onFilterTouchEventForSecurity(MotionEvent)} method to implement your own
577 * security policy. See also {@link MotionEvent#FLAG_WINDOW_IS_OBSCURED}.
Jeff Brown85a31762010-09-01 17:01:00 -0700578 * </p>
579 *
Romain Guy171c5922011-01-06 10:04:23 -0800580 * @attr ref android.R.styleable#View_alpha
Romain Guyd6a463a2009-05-21 23:10:10 -0700581 * @attr ref android.R.styleable#View_background
582 * @attr ref android.R.styleable#View_clickable
583 * @attr ref android.R.styleable#View_contentDescription
584 * @attr ref android.R.styleable#View_drawingCacheQuality
585 * @attr ref android.R.styleable#View_duplicateParentState
586 * @attr ref android.R.styleable#View_id
587 * @attr ref android.R.styleable#View_fadingEdge
588 * @attr ref android.R.styleable#View_fadingEdgeLength
Jeff Brown85a31762010-09-01 17:01:00 -0700589 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 * @attr ref android.R.styleable#View_fitsSystemWindows
Romain Guyd6a463a2009-05-21 23:10:10 -0700591 * @attr ref android.R.styleable#View_isScrollContainer
592 * @attr ref android.R.styleable#View_focusable
593 * @attr ref android.R.styleable#View_focusableInTouchMode
594 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
595 * @attr ref android.R.styleable#View_keepScreenOn
Romain Guy171c5922011-01-06 10:04:23 -0800596 * @attr ref android.R.styleable#View_layerType
Romain Guyd6a463a2009-05-21 23:10:10 -0700597 * @attr ref android.R.styleable#View_longClickable
598 * @attr ref android.R.styleable#View_minHeight
599 * @attr ref android.R.styleable#View_minWidth
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 * @attr ref android.R.styleable#View_nextFocusDown
601 * @attr ref android.R.styleable#View_nextFocusLeft
602 * @attr ref android.R.styleable#View_nextFocusRight
603 * @attr ref android.R.styleable#View_nextFocusUp
Romain Guyd6a463a2009-05-21 23:10:10 -0700604 * @attr ref android.R.styleable#View_onClick
605 * @attr ref android.R.styleable#View_padding
606 * @attr ref android.R.styleable#View_paddingBottom
607 * @attr ref android.R.styleable#View_paddingLeft
608 * @attr ref android.R.styleable#View_paddingRight
609 * @attr ref android.R.styleable#View_paddingTop
Fabrice Di Megliod8703a92011-06-16 18:54:08 -0700610 * @attr ref android.R.styleable#View_paddingStart
611 * @attr ref android.R.styleable#View_paddingEnd
Romain Guyd6a463a2009-05-21 23:10:10 -0700612 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800613 * @attr ref android.R.styleable#View_rotation
614 * @attr ref android.R.styleable#View_rotationX
615 * @attr ref android.R.styleable#View_rotationY
616 * @attr ref android.R.styleable#View_scaleX
617 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 * @attr ref android.R.styleable#View_scrollX
619 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700620 * @attr ref android.R.styleable#View_scrollbarSize
621 * @attr ref android.R.styleable#View_scrollbarStyle
622 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700623 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
624 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
626 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800627 * @attr ref android.R.styleable#View_scrollbarThumbVertical
628 * @attr ref android.R.styleable#View_scrollbarTrackVertical
629 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
630 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700631 * @attr ref android.R.styleable#View_soundEffectsEnabled
632 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800633 * @attr ref android.R.styleable#View_transformPivotX
634 * @attr ref android.R.styleable#View_transformPivotY
635 * @attr ref android.R.styleable#View_translationX
636 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700637 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800638 *
639 * @see android.view.ViewGroup
640 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700641public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 private static final boolean DBG = false;
643
644 /**
645 * The logging tag used by this class with android.util.Log.
646 */
647 protected static final String VIEW_LOG_TAG = "View";
648
649 /**
650 * Used to mark a View that has no ID.
651 */
652 public static final int NO_ID = -1;
653
654 /**
655 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
656 * calling setFlags.
657 */
658 private static final int NOT_FOCUSABLE = 0x00000000;
659
660 /**
661 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
662 * setFlags.
663 */
664 private static final int FOCUSABLE = 0x00000001;
665
666 /**
667 * Mask for use with setFlags indicating bits used for focus.
668 */
669 private static final int FOCUSABLE_MASK = 0x00000001;
670
671 /**
672 * This view will adjust its padding to fit sytem windows (e.g. status bar)
673 */
674 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
675
676 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700677 * This view is visible. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Romain Guy5c22a8c2011-05-13 11:48:45 -0700683 * Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 */
685 public static final int INVISIBLE = 0x00000004;
686
687 /**
688 * This view is invisible, and it doesn't take any space for layout
Romain Guy5c22a8c2011-05-13 11:48:45 -0700689 * purposes. Use with {@link #setVisibility(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 */
691 public static final int GONE = 0x00000008;
692
693 /**
694 * Mask for use with setFlags indicating bits used for visibility.
695 * {@hide}
696 */
697 static final int VISIBILITY_MASK = 0x0000000C;
698
699 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
700
701 /**
702 * This view is enabled. Intrepretation varies by subclass.
703 * Use with ENABLED_MASK when calling setFlags.
704 * {@hide}
705 */
706 static final int ENABLED = 0x00000000;
707
708 /**
709 * This view is disabled. Intrepretation varies by subclass.
710 * Use with ENABLED_MASK when calling setFlags.
711 * {@hide}
712 */
713 static final int DISABLED = 0x00000020;
714
715 /**
716 * Mask for use with setFlags indicating bits used for indicating whether
717 * this view is enabled
718 * {@hide}
719 */
720 static final int ENABLED_MASK = 0x00000020;
721
722 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700723 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
724 * called and further optimizations will be performed. It is okay to have
725 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 * {@hide}
727 */
728 static final int WILL_NOT_DRAW = 0x00000080;
729
730 /**
731 * Mask for use with setFlags indicating bits used for indicating whether
732 * this view is will draw
733 * {@hide}
734 */
735 static final int DRAW_MASK = 0x00000080;
736
737 /**
738 * <p>This view doesn't show scrollbars.</p>
739 * {@hide}
740 */
741 static final int SCROLLBARS_NONE = 0x00000000;
742
743 /**
744 * <p>This view shows horizontal scrollbars.</p>
745 * {@hide}
746 */
747 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
748
749 /**
750 * <p>This view shows vertical scrollbars.</p>
751 * {@hide}
752 */
753 static final int SCROLLBARS_VERTICAL = 0x00000200;
754
755 /**
756 * <p>Mask for use with setFlags indicating bits used for indicating which
757 * scrollbars are enabled.</p>
758 * {@hide}
759 */
760 static final int SCROLLBARS_MASK = 0x00000300;
761
Jeff Brown85a31762010-09-01 17:01:00 -0700762 /**
763 * Indicates that the view should filter touches when its window is obscured.
764 * Refer to the class comments for more information about this security feature.
765 * {@hide}
766 */
767 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
768
769 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
771 /**
772 * <p>This view doesn't show fading edges.</p>
773 * {@hide}
774 */
775 static final int FADING_EDGE_NONE = 0x00000000;
776
777 /**
778 * <p>This view shows horizontal fading edges.</p>
779 * {@hide}
780 */
781 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
782
783 /**
784 * <p>This view shows vertical fading edges.</p>
785 * {@hide}
786 */
787 static final int FADING_EDGE_VERTICAL = 0x00002000;
788
789 /**
790 * <p>Mask for use with setFlags indicating bits used for indicating which
791 * fading edges are enabled.</p>
792 * {@hide}
793 */
794 static final int FADING_EDGE_MASK = 0x00003000;
795
796 /**
797 * <p>Indicates this view can be clicked. When clickable, a View reacts
798 * to clicks by notifying the OnClickListener.<p>
799 * {@hide}
800 */
801 static final int CLICKABLE = 0x00004000;
802
803 /**
804 * <p>Indicates this view is caching its drawing into a bitmap.</p>
805 * {@hide}
806 */
807 static final int DRAWING_CACHE_ENABLED = 0x00008000;
808
809 /**
810 * <p>Indicates that no icicle should be saved for this view.<p>
811 * {@hide}
812 */
813 static final int SAVE_DISABLED = 0x000010000;
814
815 /**
816 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
817 * property.</p>
818 * {@hide}
819 */
820 static final int SAVE_DISABLED_MASK = 0x000010000;
821
822 /**
823 * <p>Indicates that no drawing cache should ever be created for this view.<p>
824 * {@hide}
825 */
826 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
827
828 /**
829 * <p>Indicates this view can take / keep focus when int touch mode.</p>
830 * {@hide}
831 */
832 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
833
834 /**
835 * <p>Enables low quality mode for the drawing cache.</p>
836 */
837 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
838
839 /**
840 * <p>Enables high quality mode for the drawing cache.</p>
841 */
842 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
843
844 /**
845 * <p>Enables automatic quality mode for the drawing cache.</p>
846 */
847 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
848
849 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
850 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
851 };
852
853 /**
854 * <p>Mask for use with setFlags indicating bits used for the cache
855 * quality property.</p>
856 * {@hide}
857 */
858 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
859
860 /**
861 * <p>
862 * Indicates this view can be long clicked. When long clickable, a View
863 * reacts to long clicks by notifying the OnLongClickListener or showing a
864 * context menu.
865 * </p>
866 * {@hide}
867 */
868 static final int LONG_CLICKABLE = 0x00200000;
869
870 /**
871 * <p>Indicates that this view gets its drawable states from its direct parent
872 * and ignores its original internal states.</p>
873 *
874 * @hide
875 */
876 static final int DUPLICATE_PARENT_STATE = 0x00400000;
877
878 /**
879 * The scrollbar style to display the scrollbars inside the content area,
880 * without increasing the padding. The scrollbars will be overlaid with
881 * translucency on the view's content.
882 */
883 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
884
885 /**
886 * The scrollbar style to display the scrollbars inside the padded area,
887 * increasing the padding of the view. The scrollbars will not overlap the
888 * content area of the view.
889 */
890 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
891
892 /**
893 * The scrollbar style to display the scrollbars at the edge of the view,
894 * without increasing the padding. The scrollbars will be overlaid with
895 * translucency.
896 */
897 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
898
899 /**
900 * The scrollbar style to display the scrollbars at the edge of the view,
901 * increasing the padding of the view. The scrollbars will only overlap the
902 * background, if any.
903 */
904 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
905
906 /**
907 * Mask to check if the scrollbar style is overlay or inset.
908 * {@hide}
909 */
910 static final int SCROLLBARS_INSET_MASK = 0x01000000;
911
912 /**
913 * Mask to check if the scrollbar style is inside or outside.
914 * {@hide}
915 */
916 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
917
918 /**
919 * Mask for scrollbar style.
920 * {@hide}
921 */
922 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
923
924 /**
925 * View flag indicating that the screen should remain on while the
926 * window containing this view is visible to the user. This effectively
927 * takes care of automatically setting the WindowManager's
928 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
929 */
930 public static final int KEEP_SCREEN_ON = 0x04000000;
931
932 /**
933 * View flag indicating whether this view should have sound effects enabled
934 * for events such as clicking and touching.
935 */
936 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
937
938 /**
939 * View flag indicating whether this view should have haptic feedback
940 * enabled for events such as long presses.
941 */
942 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
943
944 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700945 * <p>Indicates that the view hierarchy should stop saving state when
946 * it reaches this view. If state saving is initiated immediately at
947 * the view, it will be allowed.
948 * {@hide}
949 */
950 static final int PARENT_SAVE_DISABLED = 0x20000000;
951
952 /**
953 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
954 * {@hide}
955 */
956 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
957
958 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800959 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700960 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800961 * {@hide}
962 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800964
965 /**
966 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700967 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800968 * {@hide}
969 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800971
972 /**
973 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700974 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800975 * {@hide}
976 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800978
979 /**
980 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700981 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800982 * {@hide}
983 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800985
986 /**
987 * Mask for use with setFlags indicating bits used for horizontalDirection.
988 * {@hide}
989 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700990 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800991
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700992 /*
993 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
994 * flag value.
995 * {@hide}
996 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700997 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
998 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -0800999
1000 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001001 * Default horizontalDirection.
1002 * {@hide}
1003 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001004 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001005
1006 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001007 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1008 * should add all focusable Views regardless if they are focusable in touch mode.
1009 */
1010 public static final int FOCUSABLES_ALL = 0x00000000;
1011
1012 /**
1013 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1014 * should add only Views focusable in touch mode.
1015 */
1016 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1017
1018 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001019 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 * item.
1021 */
1022 public static final int FOCUS_BACKWARD = 0x00000001;
1023
1024 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001025 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 * item.
1027 */
1028 public static final int FOCUS_FORWARD = 0x00000002;
1029
1030 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001031 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 */
1033 public static final int FOCUS_LEFT = 0x00000011;
1034
1035 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001036 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 */
1038 public static final int FOCUS_UP = 0x00000021;
1039
1040 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001041 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 */
1043 public static final int FOCUS_RIGHT = 0x00000042;
1044
1045 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001046 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001047 */
1048 public static final int FOCUS_DOWN = 0x00000082;
1049
1050 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001051 * Bits of {@link #getMeasuredWidthAndState()} and
1052 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1053 */
1054 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1055
1056 /**
1057 * Bits of {@link #getMeasuredWidthAndState()} and
1058 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1059 */
1060 public static final int MEASURED_STATE_MASK = 0xff000000;
1061
1062 /**
1063 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1064 * for functions that combine both width and height into a single int,
1065 * such as {@link #getMeasuredState()} and the childState argument of
1066 * {@link #resolveSizeAndState(int, int, int)}.
1067 */
1068 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1069
1070 /**
1071 * Bit of {@link #getMeasuredWidthAndState()} and
1072 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1073 * is smaller that the space the view would like to have.
1074 */
1075 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1076
1077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 * Base View state sets
1079 */
1080 // Singles
1081 /**
1082 * Indicates the view has no states set. States are used with
1083 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1084 * view depending on its state.
1085 *
1086 * @see android.graphics.drawable.Drawable
1087 * @see #getDrawableState()
1088 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001089 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 /**
1091 * Indicates the view is enabled. States are used with
1092 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1093 * view depending on its state.
1094 *
1095 * @see android.graphics.drawable.Drawable
1096 * @see #getDrawableState()
1097 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001098 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001099 /**
1100 * Indicates the view is focused. States are used with
1101 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1102 * view depending on its state.
1103 *
1104 * @see android.graphics.drawable.Drawable
1105 * @see #getDrawableState()
1106 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001107 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001108 /**
1109 * Indicates the view is selected. States are used with
1110 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1111 * view depending on its state.
1112 *
1113 * @see android.graphics.drawable.Drawable
1114 * @see #getDrawableState()
1115 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001116 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 /**
1118 * Indicates the view is pressed. States are used with
1119 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1120 * view depending on its state.
1121 *
1122 * @see android.graphics.drawable.Drawable
1123 * @see #getDrawableState()
1124 * @hide
1125 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001126 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 /**
1128 * Indicates the view's window has focus. States are used with
1129 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1130 * view depending on its state.
1131 *
1132 * @see android.graphics.drawable.Drawable
1133 * @see #getDrawableState()
1134 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001135 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 // Doubles
1137 /**
1138 * Indicates the view is enabled and has the focus.
1139 *
1140 * @see #ENABLED_STATE_SET
1141 * @see #FOCUSED_STATE_SET
1142 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001143 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001144 /**
1145 * Indicates the view is enabled and selected.
1146 *
1147 * @see #ENABLED_STATE_SET
1148 * @see #SELECTED_STATE_SET
1149 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001150 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 /**
1152 * Indicates the view is enabled and that its window has focus.
1153 *
1154 * @see #ENABLED_STATE_SET
1155 * @see #WINDOW_FOCUSED_STATE_SET
1156 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001157 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 /**
1159 * Indicates the view is focused and selected.
1160 *
1161 * @see #FOCUSED_STATE_SET
1162 * @see #SELECTED_STATE_SET
1163 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001164 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 /**
1166 * Indicates the view has the focus and that its window has the focus.
1167 *
1168 * @see #FOCUSED_STATE_SET
1169 * @see #WINDOW_FOCUSED_STATE_SET
1170 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001171 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 /**
1173 * Indicates the view is selected and that its window has the focus.
1174 *
1175 * @see #SELECTED_STATE_SET
1176 * @see #WINDOW_FOCUSED_STATE_SET
1177 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001178 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 // Triples
1180 /**
1181 * Indicates the view is enabled, focused and selected.
1182 *
1183 * @see #ENABLED_STATE_SET
1184 * @see #FOCUSED_STATE_SET
1185 * @see #SELECTED_STATE_SET
1186 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001187 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 /**
1189 * Indicates the view is enabled, focused and its window has the focus.
1190 *
1191 * @see #ENABLED_STATE_SET
1192 * @see #FOCUSED_STATE_SET
1193 * @see #WINDOW_FOCUSED_STATE_SET
1194 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001195 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001196 /**
1197 * Indicates the view is enabled, selected and its window has the focus.
1198 *
1199 * @see #ENABLED_STATE_SET
1200 * @see #SELECTED_STATE_SET
1201 * @see #WINDOW_FOCUSED_STATE_SET
1202 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001203 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 /**
1205 * Indicates the view is focused, selected and its window has the focus.
1206 *
1207 * @see #FOCUSED_STATE_SET
1208 * @see #SELECTED_STATE_SET
1209 * @see #WINDOW_FOCUSED_STATE_SET
1210 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001211 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001212 /**
1213 * Indicates the view is enabled, focused, selected and its window
1214 * has the focus.
1215 *
1216 * @see #ENABLED_STATE_SET
1217 * @see #FOCUSED_STATE_SET
1218 * @see #SELECTED_STATE_SET
1219 * @see #WINDOW_FOCUSED_STATE_SET
1220 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001221 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 /**
1223 * Indicates the view is pressed and its window has the focus.
1224 *
1225 * @see #PRESSED_STATE_SET
1226 * @see #WINDOW_FOCUSED_STATE_SET
1227 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001228 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 /**
1230 * Indicates the view is pressed and selected.
1231 *
1232 * @see #PRESSED_STATE_SET
1233 * @see #SELECTED_STATE_SET
1234 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001235 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001236 /**
1237 * Indicates the view is pressed, selected and its window has the focus.
1238 *
1239 * @see #PRESSED_STATE_SET
1240 * @see #SELECTED_STATE_SET
1241 * @see #WINDOW_FOCUSED_STATE_SET
1242 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001243 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 /**
1245 * Indicates the view is pressed and focused.
1246 *
1247 * @see #PRESSED_STATE_SET
1248 * @see #FOCUSED_STATE_SET
1249 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001250 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 /**
1252 * Indicates the view is pressed, focused and its window has the focus.
1253 *
1254 * @see #PRESSED_STATE_SET
1255 * @see #FOCUSED_STATE_SET
1256 * @see #WINDOW_FOCUSED_STATE_SET
1257 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001258 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 /**
1260 * Indicates the view is pressed, focused and selected.
1261 *
1262 * @see #PRESSED_STATE_SET
1263 * @see #SELECTED_STATE_SET
1264 * @see #FOCUSED_STATE_SET
1265 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001266 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 /**
1268 * Indicates the view is pressed, focused, selected and its window has the focus.
1269 *
1270 * @see #PRESSED_STATE_SET
1271 * @see #FOCUSED_STATE_SET
1272 * @see #SELECTED_STATE_SET
1273 * @see #WINDOW_FOCUSED_STATE_SET
1274 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001275 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 /**
1277 * Indicates the view is pressed and enabled.
1278 *
1279 * @see #PRESSED_STATE_SET
1280 * @see #ENABLED_STATE_SET
1281 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001282 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 /**
1284 * Indicates the view is pressed, enabled and its window has the focus.
1285 *
1286 * @see #PRESSED_STATE_SET
1287 * @see #ENABLED_STATE_SET
1288 * @see #WINDOW_FOCUSED_STATE_SET
1289 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001290 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001291 /**
1292 * Indicates the view is pressed, enabled and selected.
1293 *
1294 * @see #PRESSED_STATE_SET
1295 * @see #ENABLED_STATE_SET
1296 * @see #SELECTED_STATE_SET
1297 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001298 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 /**
1300 * Indicates the view is pressed, enabled, selected and its window has the
1301 * focus.
1302 *
1303 * @see #PRESSED_STATE_SET
1304 * @see #ENABLED_STATE_SET
1305 * @see #SELECTED_STATE_SET
1306 * @see #WINDOW_FOCUSED_STATE_SET
1307 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001308 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 /**
1310 * Indicates the view is pressed, enabled and focused.
1311 *
1312 * @see #PRESSED_STATE_SET
1313 * @see #ENABLED_STATE_SET
1314 * @see #FOCUSED_STATE_SET
1315 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001316 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 /**
1318 * Indicates the view is pressed, enabled, focused and its window has the
1319 * focus.
1320 *
1321 * @see #PRESSED_STATE_SET
1322 * @see #ENABLED_STATE_SET
1323 * @see #FOCUSED_STATE_SET
1324 * @see #WINDOW_FOCUSED_STATE_SET
1325 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001326 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 /**
1328 * Indicates the view is pressed, enabled, focused and selected.
1329 *
1330 * @see #PRESSED_STATE_SET
1331 * @see #ENABLED_STATE_SET
1332 * @see #SELECTED_STATE_SET
1333 * @see #FOCUSED_STATE_SET
1334 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001335 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001336 /**
1337 * Indicates the view is pressed, enabled, focused, selected and its window
1338 * has the focus.
1339 *
1340 * @see #PRESSED_STATE_SET
1341 * @see #ENABLED_STATE_SET
1342 * @see #SELECTED_STATE_SET
1343 * @see #FOCUSED_STATE_SET
1344 * @see #WINDOW_FOCUSED_STATE_SET
1345 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001346 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347
1348 /**
1349 * The order here is very important to {@link #getDrawableState()}
1350 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001351 private static final int[][] VIEW_STATE_SETS;
1352
Romain Guyb051e892010-09-28 19:09:36 -07001353 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1354 static final int VIEW_STATE_SELECTED = 1 << 1;
1355 static final int VIEW_STATE_FOCUSED = 1 << 2;
1356 static final int VIEW_STATE_ENABLED = 1 << 3;
1357 static final int VIEW_STATE_PRESSED = 1 << 4;
1358 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001359 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001360 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001361 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1362 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001363
1364 static final int[] VIEW_STATE_IDS = new int[] {
1365 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1366 R.attr.state_selected, VIEW_STATE_SELECTED,
1367 R.attr.state_focused, VIEW_STATE_FOCUSED,
1368 R.attr.state_enabled, VIEW_STATE_ENABLED,
1369 R.attr.state_pressed, VIEW_STATE_PRESSED,
1370 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001371 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001372 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001373 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1374 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 };
1376
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001377 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001378 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1379 throw new IllegalStateException(
1380 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1381 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001382 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001383 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001387 orderedIds[i * 2] = viewState;
1388 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 }
1390 }
1391 }
Romain Guyb051e892010-09-28 19:09:36 -07001392 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1393 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1394 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001395 int numBits = Integer.bitCount(i);
1396 int[] set = new int[numBits];
1397 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001398 for (int j = 0; j < orderedIds.length; j += 2) {
1399 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001400 set[pos++] = orderedIds[j];
1401 }
1402 }
1403 VIEW_STATE_SETS[i] = set;
1404 }
1405
1406 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1407 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1408 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1409 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1410 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1411 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1412 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1414 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1418 | VIEW_STATE_FOCUSED];
1419 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1420 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1421 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1422 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1426 | VIEW_STATE_ENABLED];
1427 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1431 | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1434 | VIEW_STATE_ENABLED];
1435 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1437 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1438
1439 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1440 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1441 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1442 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1446 | VIEW_STATE_PRESSED];
1447 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1451 | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1457 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1458 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1462 | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1468 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1471 | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1474 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1477 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1480 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1481 | VIEW_STATE_PRESSED];
1482 }
1483
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 * Temporary Rect currently for use in setBackground(). This will probably
1486 * be extended in the future to hold our own class with more than just
1487 * a Rect. :)
1488 */
1489 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001490
1491 /**
1492 * Map used to store views' tags.
1493 */
1494 private static WeakHashMap<View, SparseArray<Object>> sTags;
1495
1496 /**
1497 * Lock used to access sTags.
1498 */
1499 private static final Object sTagsLock = new Object();
1500
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001501 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001502 * The next available accessiiblity id.
1503 */
1504 private static int sNextAccessibilityViewId;
1505
1506 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001507 * The animation currently associated with this view.
1508 * @hide
1509 */
1510 protected Animation mCurrentAnimation = null;
1511
1512 /**
1513 * Width as measured during measure pass.
1514 * {@hide}
1515 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001516 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001517 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518
1519 /**
1520 * Height as measured during measure pass.
1521 * {@hide}
1522 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001523 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001524 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525
1526 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001527 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1528 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1529 * its display list. This flag, used only when hw accelerated, allows us to clear the
1530 * flag while retaining this information until it's needed (at getDisplayList() time and
1531 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1532 *
1533 * {@hide}
1534 */
1535 boolean mRecreateDisplayList = false;
1536
1537 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 * The view's identifier.
1539 * {@hide}
1540 *
1541 * @see #setId(int)
1542 * @see #getId()
1543 */
1544 @ViewDebug.ExportedProperty(resolveId = true)
1545 int mID = NO_ID;
1546
1547 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001548 * The stable ID of this view for accessibility porposes.
1549 */
1550 int mAccessibilityViewId = NO_ID;
1551
1552 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 * The view's tag.
1554 * {@hide}
1555 *
1556 * @see #setTag(Object)
1557 * @see #getTag()
1558 */
1559 protected Object mTag;
1560
1561 // for mPrivateFlags:
1562 /** {@hide} */
1563 static final int WANTS_FOCUS = 0x00000001;
1564 /** {@hide} */
1565 static final int FOCUSED = 0x00000002;
1566 /** {@hide} */
1567 static final int SELECTED = 0x00000004;
1568 /** {@hide} */
1569 static final int IS_ROOT_NAMESPACE = 0x00000008;
1570 /** {@hide} */
1571 static final int HAS_BOUNDS = 0x00000010;
1572 /** {@hide} */
1573 static final int DRAWN = 0x00000020;
1574 /**
1575 * When this flag is set, this view is running an animation on behalf of its
1576 * children and should therefore not cancel invalidate requests, even if they
1577 * lie outside of this view's bounds.
1578 *
1579 * {@hide}
1580 */
1581 static final int DRAW_ANIMATION = 0x00000040;
1582 /** {@hide} */
1583 static final int SKIP_DRAW = 0x00000080;
1584 /** {@hide} */
1585 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1586 /** {@hide} */
1587 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1588 /** {@hide} */
1589 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1590 /** {@hide} */
1591 static final int MEASURED_DIMENSION_SET = 0x00000800;
1592 /** {@hide} */
1593 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001594 /** {@hide} */
1595 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596
1597 private static final int PRESSED = 0x00004000;
1598
1599 /** {@hide} */
1600 static final int DRAWING_CACHE_VALID = 0x00008000;
1601 /**
1602 * Flag used to indicate that this view should be drawn once more (and only once
1603 * more) after its animation has completed.
1604 * {@hide}
1605 */
1606 static final int ANIMATION_STARTED = 0x00010000;
1607
1608 private static final int SAVE_STATE_CALLED = 0x00020000;
1609
1610 /**
1611 * Indicates that the View returned true when onSetAlpha() was called and that
1612 * the alpha must be restored.
1613 * {@hide}
1614 */
1615 static final int ALPHA_SET = 0x00040000;
1616
1617 /**
1618 * Set by {@link #setScrollContainer(boolean)}.
1619 */
1620 static final int SCROLL_CONTAINER = 0x00080000;
1621
1622 /**
1623 * Set by {@link #setScrollContainer(boolean)}.
1624 */
1625 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1626
1627 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001628 * View flag indicating whether this view was invalidated (fully or partially.)
1629 *
1630 * @hide
1631 */
1632 static final int DIRTY = 0x00200000;
1633
1634 /**
1635 * View flag indicating whether this view was invalidated by an opaque
1636 * invalidate request.
1637 *
1638 * @hide
1639 */
1640 static final int DIRTY_OPAQUE = 0x00400000;
1641
1642 /**
1643 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1644 *
1645 * @hide
1646 */
1647 static final int DIRTY_MASK = 0x00600000;
1648
1649 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001650 * Indicates whether the background is opaque.
1651 *
1652 * @hide
1653 */
1654 static final int OPAQUE_BACKGROUND = 0x00800000;
1655
1656 /**
1657 * Indicates whether the scrollbars are opaque.
1658 *
1659 * @hide
1660 */
1661 static final int OPAQUE_SCROLLBARS = 0x01000000;
1662
1663 /**
1664 * Indicates whether the view is opaque.
1665 *
1666 * @hide
1667 */
1668 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001669
Adam Powelle14579b2009-12-16 18:39:52 -08001670 /**
1671 * Indicates a prepressed state;
1672 * the short time between ACTION_DOWN and recognizing
1673 * a 'real' press. Prepressed is used to recognize quick taps
1674 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001675 *
Adam Powelle14579b2009-12-16 18:39:52 -08001676 * @hide
1677 */
1678 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001679
Adam Powellc9fbaab2010-02-16 17:16:19 -08001680 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001681 * Indicates whether the view is temporarily detached.
1682 *
1683 * @hide
1684 */
1685 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001686
Adam Powell8568c3a2010-04-19 14:26:11 -07001687 /**
1688 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001689 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 * @hide
1691 */
1692 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001693
1694 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001695 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1696 * @hide
1697 */
1698 private static final int HOVERED = 0x10000000;
1699
1700 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001701 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1702 * for transform operations
1703 *
1704 * @hide
1705 */
Adam Powellf37df072010-09-17 16:22:49 -07001706 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001707
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001708 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001709 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710
Chet Haasefd2b0022010-08-06 13:08:56 -07001711 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001712 * Indicates that this view was specifically invalidated, not just dirtied because some
1713 * child view was invalidated. The flag is used to determine when we need to recreate
1714 * a view's display list (as opposed to just returning a reference to its existing
1715 * display list).
1716 *
1717 * @hide
1718 */
1719 static final int INVALIDATED = 0x80000000;
1720
Christopher Tate3d4bf172011-03-28 16:16:46 -07001721 /* Masks for mPrivateFlags2 */
1722
1723 /**
1724 * Indicates that this view has reported that it can accept the current drag's content.
1725 * Cleared when the drag operation concludes.
1726 * @hide
1727 */
1728 static final int DRAG_CAN_ACCEPT = 0x00000001;
1729
1730 /**
1731 * Indicates that this view is currently directly under the drag location in a
1732 * drag-and-drop operation involving content that it can accept. Cleared when
1733 * the drag exits the view, or when the drag operation concludes.
1734 * @hide
1735 */
1736 static final int DRAG_HOVERED = 0x00000002;
1737
Cibu Johny86666632010-02-22 13:01:02 -08001738 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001739 * Indicates whether the view layout direction has been resolved and drawn to the
1740 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001741 *
1742 * @hide
1743 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001744 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1745
1746 /**
1747 * Indicates whether the view layout direction has been resolved.
1748 *
1749 * @hide
1750 */
1751 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1752
Cibu Johny86666632010-02-22 13:01:02 -08001753
Christopher Tate3d4bf172011-03-28 16:16:46 -07001754 /* End of masks for mPrivateFlags2 */
1755
1756 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1757
Chet Haasedaf98e92011-01-10 14:10:36 -08001758 /**
Adam Powell637d3372010-08-25 14:37:03 -07001759 * Always allow a user to over-scroll this view, provided it is a
1760 * view that can scroll.
1761 *
1762 * @see #getOverScrollMode()
1763 * @see #setOverScrollMode(int)
1764 */
1765 public static final int OVER_SCROLL_ALWAYS = 0;
1766
1767 /**
1768 * Allow a user to over-scroll this view only if the content is large
1769 * enough to meaningfully scroll, provided it is a view that can scroll.
1770 *
1771 * @see #getOverScrollMode()
1772 * @see #setOverScrollMode(int)
1773 */
1774 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1775
1776 /**
1777 * Never allow a user to over-scroll this view.
1778 *
1779 * @see #getOverScrollMode()
1780 * @see #setOverScrollMode(int)
1781 */
1782 public static final int OVER_SCROLL_NEVER = 2;
1783
1784 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001785 * View has requested the status bar to be visible (the default).
1786 *
Joe Malin32736f02011-01-19 16:14:20 -08001787 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001788 */
1789 public static final int STATUS_BAR_VISIBLE = 0;
1790
1791 /**
Jeff Brown05dc66a2011-03-02 14:41:58 -08001792 * View has requested the status bar to be hidden.
Joe Onorato664644d2011-01-23 17:53:23 -08001793 *
Joe Malin32736f02011-01-19 16:14:20 -08001794 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001795 */
1796 public static final int STATUS_BAR_HIDDEN = 0x00000001;
1797
1798 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001799 * @hide
1800 *
1801 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1802 * out of the public fields to keep the undefined bits out of the developer's way.
1803 *
1804 * Flag to make the status bar not expandable. Unless you also
1805 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1806 */
1807 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1808
1809 /**
1810 * @hide
1811 *
1812 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1813 * out of the public fields to keep the undefined bits out of the developer's way.
1814 *
1815 * Flag to hide notification icons and scrolling ticker text.
1816 */
1817 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1818
1819 /**
1820 * @hide
1821 *
1822 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1823 * out of the public fields to keep the undefined bits out of the developer's way.
1824 *
1825 * Flag to disable incoming notification alerts. This will not block
1826 * icons, but it will block sound, vibrating and other visual or aural notifications.
1827 */
1828 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1829
1830 /**
1831 * @hide
1832 *
1833 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1834 * out of the public fields to keep the undefined bits out of the developer's way.
1835 *
1836 * Flag to hide only the scrolling ticker. Note that
1837 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1838 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1839 */
1840 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1841
1842 /**
1843 * @hide
1844 *
1845 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1846 * out of the public fields to keep the undefined bits out of the developer's way.
1847 *
1848 * Flag to hide the center system info area.
1849 */
1850 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1851
1852 /**
1853 * @hide
1854 *
1855 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1856 * out of the public fields to keep the undefined bits out of the developer's way.
1857 *
1858 * Flag to hide only the navigation buttons. Don't use this
1859 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001860 *
1861 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001862 */
1863 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001871 * Flag to hide only the back button. Don't use this
1872 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1873 */
1874 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1875
1876 /**
1877 * @hide
1878 *
1879 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1880 * out of the public fields to keep the undefined bits out of the developer's way.
1881 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001882 * Flag to hide only the clock. You might use this if your activity has
1883 * its own clock making the status bar's clock redundant.
1884 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001885 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1886
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001887 /**
1888 * @hide
1889 */
1890 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = STATUS_BAR_HIDDEN;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001891
1892 /**
Adam Powell637d3372010-08-25 14:37:03 -07001893 * Controls the over-scroll mode for this view.
1894 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1895 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1896 * and {@link #OVER_SCROLL_NEVER}.
1897 */
1898 private int mOverScrollMode;
1899
1900 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 * The parent this view is attached to.
1902 * {@hide}
1903 *
1904 * @see #getParent()
1905 */
1906 protected ViewParent mParent;
1907
1908 /**
1909 * {@hide}
1910 */
1911 AttachInfo mAttachInfo;
1912
1913 /**
1914 * {@hide}
1915 */
Romain Guy809a7f62009-05-14 15:44:42 -07001916 @ViewDebug.ExportedProperty(flagMapping = {
1917 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1918 name = "FORCE_LAYOUT"),
1919 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1920 name = "LAYOUT_REQUIRED"),
1921 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001922 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001923 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1924 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1925 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1926 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1927 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001929 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930
1931 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001932 * This view's request for the visibility of the status bar.
1933 * @hide
1934 */
Joe Onoratoac0ee892011-01-30 15:38:30 -08001935 @ViewDebug.ExportedProperty()
Joe Onorato664644d2011-01-23 17:53:23 -08001936 int mSystemUiVisibility;
1937
1938 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 * Count of how many windows this view has been attached to.
1940 */
1941 int mWindowAttachCount;
1942
1943 /**
1944 * The layout parameters associated with this view and used by the parent
1945 * {@link android.view.ViewGroup} to determine how this view should be
1946 * laid out.
1947 * {@hide}
1948 */
1949 protected ViewGroup.LayoutParams mLayoutParams;
1950
1951 /**
1952 * The view flags hold various views states.
1953 * {@hide}
1954 */
1955 @ViewDebug.ExportedProperty
1956 int mViewFlags;
1957
1958 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07001959 * The transform matrix for the View. This transform is calculated internally
1960 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1961 * is used by default. Do *not* use this variable directly; instead call
1962 * getMatrix(), which will automatically recalculate the matrix if necessary
1963 * to get the correct matrix based on the latest rotation and scale properties.
1964 */
1965 private final Matrix mMatrix = new Matrix();
1966
1967 /**
1968 * The transform matrix for the View. This transform is calculated internally
1969 * based on the rotation, scaleX, and scaleY properties. The identity matrix
1970 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07001971 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07001972 * to get the correct matrix based on the latest rotation and scale properties.
1973 */
1974 private Matrix mInverseMatrix;
1975
1976 /**
1977 * An internal variable that tracks whether we need to recalculate the
1978 * transform matrix, based on whether the rotation or scaleX/Y properties
1979 * have changed since the matrix was last calculated.
1980 */
Chet Haasea00f3862011-02-22 06:34:40 -08001981 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07001982
1983 /**
1984 * An internal variable that tracks whether we need to recalculate the
1985 * transform matrix, based on whether the rotation or scaleX/Y properties
1986 * have changed since the matrix was last calculated.
1987 */
1988 private boolean mInverseMatrixDirty = true;
1989
1990 /**
1991 * A variable that tracks whether we need to recalculate the
1992 * transform matrix, based on whether the rotation or scaleX/Y properties
1993 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07001994 * is only valid after a call to updateMatrix() or to a function that
1995 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07001996 */
Romain Guy33e72ae2010-07-17 12:40:29 -07001997 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07001998
1999 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07002000 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2001 */
2002 private Camera mCamera = null;
2003
2004 /**
2005 * This matrix is used when computing the matrix for 3D rotations.
2006 */
2007 private Matrix matrix3D = null;
2008
2009 /**
2010 * These prev values are used to recalculate a centered pivot point when necessary. The
2011 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2012 * set), so thes values are only used then as well.
2013 */
2014 private int mPrevWidth = -1;
2015 private int mPrevHeight = -1;
2016
Joe Malin32736f02011-01-19 16:14:20 -08002017 private boolean mLastIsOpaque;
2018
Chet Haasefd2b0022010-08-06 13:08:56 -07002019 /**
2020 * Convenience value to check for float values that are close enough to zero to be considered
2021 * zero.
2022 */
Romain Guy2542d192010-08-18 11:47:12 -07002023 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002024
2025 /**
2026 * The degrees rotation around the vertical axis through the pivot point.
2027 */
2028 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002029 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002030
2031 /**
2032 * The degrees rotation around the horizontal axis through the pivot point.
2033 */
2034 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002035 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002036
2037 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002038 * The degrees rotation around the pivot point.
2039 */
2040 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002041 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002042
2043 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002044 * The amount of translation of the object away from its left property (post-layout).
2045 */
2046 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002047 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002048
2049 /**
2050 * The amount of translation of the object away from its top property (post-layout).
2051 */
2052 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002053 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002054
2055 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002056 * The amount of scale in the x direction around the pivot point. A
2057 * value of 1 means no scaling is applied.
2058 */
2059 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002060 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002061
2062 /**
2063 * The amount of scale in the y direction around the pivot point. A
2064 * value of 1 means no scaling is applied.
2065 */
2066 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002067 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002068
2069 /**
2070 * The amount of scale in the x direction around the pivot point. A
2071 * value of 1 means no scaling is applied.
2072 */
2073 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002074 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002075
2076 /**
2077 * The amount of scale in the y direction around the pivot point. A
2078 * value of 1 means no scaling is applied.
2079 */
2080 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002081 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002082
2083 /**
2084 * The opacity of the View. This is a value from 0 to 1, where 0 means
2085 * completely transparent and 1 means completely opaque.
2086 */
2087 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002088 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002089
2090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 * The distance in pixels from the left edge of this view's parent
2092 * to the left edge of this view.
2093 * {@hide}
2094 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002095 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 protected int mLeft;
2097 /**
2098 * The distance in pixels from the left edge of this view's parent
2099 * to the right edge of this view.
2100 * {@hide}
2101 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002102 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 protected int mRight;
2104 /**
2105 * The distance in pixels from the top edge of this view's parent
2106 * to the top edge of this view.
2107 * {@hide}
2108 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002109 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002110 protected int mTop;
2111 /**
2112 * The distance in pixels from the top edge of this view's parent
2113 * to the bottom edge of this view.
2114 * {@hide}
2115 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002116 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002117 protected int mBottom;
2118
2119 /**
2120 * The offset, in pixels, by which the content of this view is scrolled
2121 * horizontally.
2122 * {@hide}
2123 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002124 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002125 protected int mScrollX;
2126 /**
2127 * The offset, in pixels, by which the content of this view is scrolled
2128 * vertically.
2129 * {@hide}
2130 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002131 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002132 protected int mScrollY;
2133
2134 /**
2135 * The left padding in pixels, that is the distance in pixels between the
2136 * left edge of this view and the left edge of its content.
2137 * {@hide}
2138 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002139 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002140 protected int mPaddingLeft;
2141 /**
2142 * The right padding in pixels, that is the distance in pixels between the
2143 * right edge of this view and the right edge of its content.
2144 * {@hide}
2145 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002146 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 protected int mPaddingRight;
2148 /**
2149 * The top padding in pixels, that is the distance in pixels between the
2150 * top edge of this view and the top edge of its content.
2151 * {@hide}
2152 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002153 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 protected int mPaddingTop;
2155 /**
2156 * The bottom padding in pixels, that is the distance in pixels between the
2157 * bottom edge of this view and the bottom edge of its content.
2158 * {@hide}
2159 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002160 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002161 protected int mPaddingBottom;
2162
2163 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002164 * Briefly describes the view and is primarily used for accessibility support.
2165 */
2166 private CharSequence mContentDescription;
2167
2168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002169 * Cache the paddingRight set by the user to append to the scrollbar's size.
2170 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002171 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 int mUserPaddingRight;
2173
2174 /**
2175 * Cache the paddingBottom set by the user to append to the scrollbar's size.
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 int mUserPaddingBottom;
2179
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002180 /**
Adam Powell20232d02010-12-08 21:08:53 -08002181 * Cache the paddingLeft set by the user to append to the scrollbar's size.
2182 */
2183 @ViewDebug.ExportedProperty(category = "padding")
2184 int mUserPaddingLeft;
2185
2186 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002187 * Cache if the user padding is relative.
2188 *
2189 */
2190 @ViewDebug.ExportedProperty(category = "padding")
2191 boolean mUserPaddingRelative;
2192
2193 /**
2194 * Cache the paddingStart set by the user to append to the scrollbar's size.
2195 *
2196 */
2197 @ViewDebug.ExportedProperty(category = "padding")
2198 int mUserPaddingStart;
2199
2200 /**
2201 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2202 *
2203 */
2204 @ViewDebug.ExportedProperty(category = "padding")
2205 int mUserPaddingEnd;
2206
2207 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002208 * @hide
2209 */
2210 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2211 /**
2212 * @hide
2213 */
2214 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215
2216 private Resources mResources = null;
2217
2218 private Drawable mBGDrawable;
2219
2220 private int mBackgroundResource;
2221 private boolean mBackgroundSizeChanged;
2222
2223 /**
2224 * Listener used to dispatch focus change events.
2225 * This field should be made private, so it is hidden from the SDK.
2226 * {@hide}
2227 */
2228 protected OnFocusChangeListener mOnFocusChangeListener;
2229
2230 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002231 * Listeners for layout change events.
2232 */
2233 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2234
2235 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002236 * Listeners for attach events.
2237 */
2238 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2239
2240 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002241 * Listener used to dispatch click events.
2242 * This field should be made private, so it is hidden from the SDK.
2243 * {@hide}
2244 */
2245 protected OnClickListener mOnClickListener;
2246
2247 /**
2248 * Listener used to dispatch long click events.
2249 * This field should be made private, so it is hidden from the SDK.
2250 * {@hide}
2251 */
2252 protected OnLongClickListener mOnLongClickListener;
2253
2254 /**
2255 * Listener used to build the context menu.
2256 * This field should be made private, so it is hidden from the SDK.
2257 * {@hide}
2258 */
2259 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2260
2261 private OnKeyListener mOnKeyListener;
2262
2263 private OnTouchListener mOnTouchListener;
2264
Jeff Brown10b62902011-06-20 16:40:37 -07002265 private OnHoverListener mOnHoverListener;
2266
Jeff Brown33bbfd22011-02-24 20:55:35 -08002267 private OnGenericMotionListener mOnGenericMotionListener;
2268
Chris Tate32affef2010-10-18 15:29:21 -07002269 private OnDragListener mOnDragListener;
2270
Joe Onorato664644d2011-01-23 17:53:23 -08002271 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2272
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002273 /**
2274 * The application environment this view lives in.
2275 * This field should be made private, so it is hidden from the SDK.
2276 * {@hide}
2277 */
2278 protected Context mContext;
2279
2280 private ScrollabilityCache mScrollCache;
2281
2282 private int[] mDrawableState = null;
2283
Romain Guy0211a0a2011-02-14 16:34:59 -08002284 /**
2285 * Set to true when drawing cache is enabled and cannot be created.
2286 *
2287 * @hide
2288 */
2289 public boolean mCachingFailed;
2290
Romain Guy02890fd2010-08-06 17:58:44 -07002291 private Bitmap mDrawingCache;
2292 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002293 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002294 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002295
2296 /**
2297 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2298 * the user may specify which view to go to next.
2299 */
2300 private int mNextFocusLeftId = View.NO_ID;
2301
2302 /**
2303 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2304 * the user may specify which view to go to next.
2305 */
2306 private int mNextFocusRightId = View.NO_ID;
2307
2308 /**
2309 * When this view has focus and the next focus is {@link #FOCUS_UP},
2310 * the user may specify which view to go to next.
2311 */
2312 private int mNextFocusUpId = View.NO_ID;
2313
2314 /**
2315 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2316 * the user may specify which view to go to next.
2317 */
2318 private int mNextFocusDownId = View.NO_ID;
2319
Jeff Brown4e6319b2010-12-13 10:36:51 -08002320 /**
2321 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2322 * the user may specify which view to go to next.
2323 */
2324 int mNextFocusForwardId = View.NO_ID;
2325
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002326 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002327 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002328 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002329 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002330
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002331 private UnsetPressedState mUnsetPressedState;
2332
2333 /**
2334 * Whether the long press's action has been invoked. The tap's action is invoked on the
2335 * up event while a long press is invoked as soon as the long press duration is reached, so
2336 * a long press could be performed before the tap is checked, in which case the tap's action
2337 * should not be invoked.
2338 */
2339 private boolean mHasPerformedLongPress;
2340
2341 /**
2342 * The minimum height of the view. We'll try our best to have the height
2343 * of this view to at least this amount.
2344 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002345 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346 private int mMinHeight;
2347
2348 /**
2349 * The minimum width of the view. We'll try our best to have the width
2350 * of this view to at least this amount.
2351 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002352 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002353 private int mMinWidth;
2354
2355 /**
2356 * The delegate to handle touch events that are physically in this view
2357 * but should be handled by another view.
2358 */
2359 private TouchDelegate mTouchDelegate = null;
2360
2361 /**
2362 * Solid color to use as a background when creating the drawing cache. Enables
2363 * the cache to use 16 bit bitmaps instead of 32 bit.
2364 */
2365 private int mDrawingCacheBackgroundColor = 0;
2366
2367 /**
2368 * Special tree observer used when mAttachInfo is null.
2369 */
2370 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002371
Adam Powelle14579b2009-12-16 18:39:52 -08002372 /**
2373 * Cache the touch slop from the context that created the view.
2374 */
2375 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002378 * Object that handles automatic animation of view properties.
2379 */
2380 private ViewPropertyAnimator mAnimator = null;
2381
2382 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002383 * Flag indicating that a drag can cross window boundaries. When
2384 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2385 * with this flag set, all visible applications will be able to participate
2386 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002387 *
2388 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002389 */
2390 public static final int DRAG_FLAG_GLOBAL = 1;
2391
2392 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002393 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2394 */
2395 private float mVerticalScrollFactor;
2396
2397 /**
Adam Powell20232d02010-12-08 21:08:53 -08002398 * Position of the vertical scroll bar.
2399 */
2400 private int mVerticalScrollbarPosition;
2401
2402 /**
2403 * Position the scroll bar at the default position as determined by the system.
2404 */
2405 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2406
2407 /**
2408 * Position the scroll bar along the left edge.
2409 */
2410 public static final int SCROLLBAR_POSITION_LEFT = 1;
2411
2412 /**
2413 * Position the scroll bar along the right edge.
2414 */
2415 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2416
2417 /**
Romain Guy171c5922011-01-06 10:04:23 -08002418 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002419 *
2420 * @see #getLayerType()
2421 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002422 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002423 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002424 */
2425 public static final int LAYER_TYPE_NONE = 0;
2426
2427 /**
2428 * <p>Indicates that the view has a software layer. A software layer is backed
2429 * by a bitmap and causes the view to be rendered using Android's software
2430 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002431 *
Romain Guy171c5922011-01-06 10:04:23 -08002432 * <p>Software layers have various usages:</p>
2433 * <p>When the application is not using hardware acceleration, a software layer
2434 * is useful to apply a specific color filter and/or blending mode and/or
2435 * translucency to a view and all its children.</p>
2436 * <p>When the application is using hardware acceleration, a software layer
2437 * is useful to render drawing primitives not supported by the hardware
2438 * accelerated pipeline. It can also be used to cache a complex view tree
2439 * into a texture and reduce the complexity of drawing operations. For instance,
2440 * when animating a complex view tree with a translation, a software layer can
2441 * be used to render the view tree only once.</p>
2442 * <p>Software layers should be avoided when the affected view tree updates
2443 * often. Every update will require to re-render the software layer, which can
2444 * potentially be slow (particularly when hardware acceleration is turned on
2445 * since the layer will have to be uploaded into a hardware texture after every
2446 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002447 *
2448 * @see #getLayerType()
2449 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002450 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002451 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002452 */
2453 public static final int LAYER_TYPE_SOFTWARE = 1;
2454
2455 /**
2456 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2457 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2458 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2459 * rendering pipeline, but only if hardware acceleration is turned on for the
2460 * view hierarchy. When hardware acceleration is turned off, hardware layers
2461 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002462 *
Romain Guy171c5922011-01-06 10:04:23 -08002463 * <p>A hardware layer is useful to apply a specific color filter and/or
2464 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002465 * <p>A hardware layer can be used to cache a complex view tree into a
2466 * texture and reduce the complexity of drawing operations. For instance,
2467 * when animating a complex view tree with a translation, a hardware layer can
2468 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002469 * <p>A hardware layer can also be used to increase the rendering quality when
2470 * rotation transformations are applied on a view. It can also be used to
2471 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002472 *
2473 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002474 * @see #setLayerType(int, android.graphics.Paint)
2475 * @see #LAYER_TYPE_NONE
2476 * @see #LAYER_TYPE_SOFTWARE
2477 */
2478 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002479
Romain Guy3aaff3a2011-01-12 14:18:47 -08002480 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2481 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2482 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2483 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2484 })
Romain Guy171c5922011-01-06 10:04:23 -08002485 int mLayerType = LAYER_TYPE_NONE;
2486 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002487 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002488
2489 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002490 * Set to true when the view is sending hover accessibility events because it
2491 * is the innermost hovered view.
2492 */
2493 private boolean mSendingHoverAccessibilityEvents;
2494
2495 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002496 * Undefined text direction (used by resolution algorithm).
2497 * @hide
2498 */
2499 public static final int TEXT_DIRECTION_UNDEFINED = -1;
2500
2501 /**
2502 * Text direction is inherited thru {@link ViewGroup}
2503 * @hide
2504 */
2505 public static final int TEXT_DIRECTION_INHERIT = 0;
2506
2507 /**
2508 * Text direction is using "first strong algorithm". The first strong directional character
2509 * determines the paragraph direction. If there is no strong directional character, the
2510 * paragraph direction is the view’s resolved ayout direction.
2511 *
2512 * @hide
2513 */
2514 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2515
2516 /**
2517 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2518 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
2519 * If there are neither, the paragraph direction is the view’s resolved layout direction.
2520 *
2521 * @hide
2522 */
2523 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2524
2525 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002526 * Text direction is the same as the one held by a 60% majority of the characters. If there is
2527 * no majority then the paragraph direction is the resolved layout direction of the View.
2528 *
2529 * @hide
2530 */
2531 public static final int TEXT_DIRECTION_CHAR_COUNT = 3;
2532
2533 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002534 * Text direction is forced to LTR.
2535 *
2536 * @hide
2537 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002538 public static final int TEXT_DIRECTION_LTR = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002539
2540 /**
2541 * Text direction is forced to RTL.
2542 *
2543 * @hide
2544 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002545 public static final int TEXT_DIRECTION_RTL = 5;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002546
2547 /**
2548 * Default text direction is inherited
2549 */
2550 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2551
2552 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002553 * Default threshold for "char count" heuristic.
2554 */
2555 protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f;
2556
2557 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002558 * The text direction that has been defined by {@link #setTextDirection(int)}.
2559 *
2560 * {@hide}
2561 */
2562 @ViewDebug.ExportedProperty(category = "text", mapping = {
2563 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2564 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2565 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2566 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002567 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002568 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2569 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2570 })
2571 protected int mTextDirection = DEFAULT_TEXT_DIRECTION;
2572
2573 /**
2574 * The resolved text direction. If resolution has not yet been done or has been reset, it will
2575 * be equal to {@link #TEXT_DIRECTION_UNDEFINED}. Otherwise it will be either {@link #TEXT_DIRECTION_LTR}
2576 * or {@link #TEXT_DIRECTION_RTL}.
2577 *
2578 * {@hide}
2579 */
2580 @ViewDebug.ExportedProperty(category = "text", mapping = {
2581 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2582 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2583 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2584 })
2585 protected int mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
2586
2587 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002588 * Consistency verifier for debugging purposes.
2589 * @hide
2590 */
2591 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2592 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2593 new InputEventConsistencyVerifier(this, 0) : null;
2594
2595 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002596 * Simple constructor to use when creating a view from code.
2597 *
2598 * @param context The Context the view is running in, through which it can
2599 * access the current theme, resources, etc.
2600 */
2601 public View(Context context) {
2602 mContext = context;
2603 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002604 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002605 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002606 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002607 mUserPaddingStart = -1;
2608 mUserPaddingEnd = -1;
2609 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002610 }
2611
2612 /**
2613 * Constructor that is called when inflating a view from XML. This is called
2614 * when a view is being constructed from an XML file, supplying attributes
2615 * that were specified in the XML file. This version uses a default style of
2616 * 0, so the only attribute values applied are those in the Context's Theme
2617 * and the given AttributeSet.
2618 *
2619 * <p>
2620 * The method onFinishInflate() will be called after all children have been
2621 * added.
2622 *
2623 * @param context The Context the view is running in, through which it can
2624 * access the current theme, resources, etc.
2625 * @param attrs The attributes of the XML tag that is inflating the view.
2626 * @see #View(Context, AttributeSet, int)
2627 */
2628 public View(Context context, AttributeSet attrs) {
2629 this(context, attrs, 0);
2630 }
2631
2632 /**
2633 * Perform inflation from XML and apply a class-specific base style. This
2634 * constructor of View allows subclasses to use their own base style when
2635 * they are inflating. For example, a Button class's constructor would call
2636 * this version of the super class constructor and supply
2637 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2638 * the theme's button style to modify all of the base view attributes (in
2639 * particular its background) as well as the Button class's attributes.
2640 *
2641 * @param context The Context the view is running in, through which it can
2642 * access the current theme, resources, etc.
2643 * @param attrs The attributes of the XML tag that is inflating the view.
2644 * @param defStyle The default style to apply to this view. If 0, no style
2645 * will be applied (beyond what is included in the theme). This may
2646 * either be an attribute resource, whose value will be retrieved
2647 * from the current theme, or an explicit style resource.
2648 * @see #View(Context, AttributeSet)
2649 */
2650 public View(Context context, AttributeSet attrs, int defStyle) {
2651 this(context);
2652
2653 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2654 defStyle, 0);
2655
2656 Drawable background = null;
2657
2658 int leftPadding = -1;
2659 int topPadding = -1;
2660 int rightPadding = -1;
2661 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002662 int startPadding = -1;
2663 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002664
2665 int padding = -1;
2666
2667 int viewFlagValues = 0;
2668 int viewFlagMasks = 0;
2669
2670 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002671
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002672 int x = 0;
2673 int y = 0;
2674
Chet Haase73066682010-11-29 15:55:32 -08002675 float tx = 0;
2676 float ty = 0;
2677 float rotation = 0;
2678 float rotationX = 0;
2679 float rotationY = 0;
2680 float sx = 1f;
2681 float sy = 1f;
2682 boolean transformSet = false;
2683
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002684 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2685
Adam Powell637d3372010-08-25 14:37:03 -07002686 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002687 final int N = a.getIndexCount();
2688 for (int i = 0; i < N; i++) {
2689 int attr = a.getIndex(i);
2690 switch (attr) {
2691 case com.android.internal.R.styleable.View_background:
2692 background = a.getDrawable(attr);
2693 break;
2694 case com.android.internal.R.styleable.View_padding:
2695 padding = a.getDimensionPixelSize(attr, -1);
2696 break;
2697 case com.android.internal.R.styleable.View_paddingLeft:
2698 leftPadding = a.getDimensionPixelSize(attr, -1);
2699 break;
2700 case com.android.internal.R.styleable.View_paddingTop:
2701 topPadding = a.getDimensionPixelSize(attr, -1);
2702 break;
2703 case com.android.internal.R.styleable.View_paddingRight:
2704 rightPadding = a.getDimensionPixelSize(attr, -1);
2705 break;
2706 case com.android.internal.R.styleable.View_paddingBottom:
2707 bottomPadding = a.getDimensionPixelSize(attr, -1);
2708 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002709 case com.android.internal.R.styleable.View_paddingStart:
2710 startPadding = a.getDimensionPixelSize(attr, -1);
2711 break;
2712 case com.android.internal.R.styleable.View_paddingEnd:
2713 endPadding = a.getDimensionPixelSize(attr, -1);
2714 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002715 case com.android.internal.R.styleable.View_scrollX:
2716 x = a.getDimensionPixelOffset(attr, 0);
2717 break;
2718 case com.android.internal.R.styleable.View_scrollY:
2719 y = a.getDimensionPixelOffset(attr, 0);
2720 break;
Chet Haase73066682010-11-29 15:55:32 -08002721 case com.android.internal.R.styleable.View_alpha:
2722 setAlpha(a.getFloat(attr, 1f));
2723 break;
2724 case com.android.internal.R.styleable.View_transformPivotX:
2725 setPivotX(a.getDimensionPixelOffset(attr, 0));
2726 break;
2727 case com.android.internal.R.styleable.View_transformPivotY:
2728 setPivotY(a.getDimensionPixelOffset(attr, 0));
2729 break;
2730 case com.android.internal.R.styleable.View_translationX:
2731 tx = a.getDimensionPixelOffset(attr, 0);
2732 transformSet = true;
2733 break;
2734 case com.android.internal.R.styleable.View_translationY:
2735 ty = a.getDimensionPixelOffset(attr, 0);
2736 transformSet = true;
2737 break;
2738 case com.android.internal.R.styleable.View_rotation:
2739 rotation = a.getFloat(attr, 0);
2740 transformSet = true;
2741 break;
2742 case com.android.internal.R.styleable.View_rotationX:
2743 rotationX = a.getFloat(attr, 0);
2744 transformSet = true;
2745 break;
2746 case com.android.internal.R.styleable.View_rotationY:
2747 rotationY = a.getFloat(attr, 0);
2748 transformSet = true;
2749 break;
2750 case com.android.internal.R.styleable.View_scaleX:
2751 sx = a.getFloat(attr, 1f);
2752 transformSet = true;
2753 break;
2754 case com.android.internal.R.styleable.View_scaleY:
2755 sy = a.getFloat(attr, 1f);
2756 transformSet = true;
2757 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002758 case com.android.internal.R.styleable.View_id:
2759 mID = a.getResourceId(attr, NO_ID);
2760 break;
2761 case com.android.internal.R.styleable.View_tag:
2762 mTag = a.getText(attr);
2763 break;
2764 case com.android.internal.R.styleable.View_fitsSystemWindows:
2765 if (a.getBoolean(attr, false)) {
2766 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2767 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2768 }
2769 break;
2770 case com.android.internal.R.styleable.View_focusable:
2771 if (a.getBoolean(attr, false)) {
2772 viewFlagValues |= FOCUSABLE;
2773 viewFlagMasks |= FOCUSABLE_MASK;
2774 }
2775 break;
2776 case com.android.internal.R.styleable.View_focusableInTouchMode:
2777 if (a.getBoolean(attr, false)) {
2778 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2779 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2780 }
2781 break;
2782 case com.android.internal.R.styleable.View_clickable:
2783 if (a.getBoolean(attr, false)) {
2784 viewFlagValues |= CLICKABLE;
2785 viewFlagMasks |= CLICKABLE;
2786 }
2787 break;
2788 case com.android.internal.R.styleable.View_longClickable:
2789 if (a.getBoolean(attr, false)) {
2790 viewFlagValues |= LONG_CLICKABLE;
2791 viewFlagMasks |= LONG_CLICKABLE;
2792 }
2793 break;
2794 case com.android.internal.R.styleable.View_saveEnabled:
2795 if (!a.getBoolean(attr, true)) {
2796 viewFlagValues |= SAVE_DISABLED;
2797 viewFlagMasks |= SAVE_DISABLED_MASK;
2798 }
2799 break;
2800 case com.android.internal.R.styleable.View_duplicateParentState:
2801 if (a.getBoolean(attr, false)) {
2802 viewFlagValues |= DUPLICATE_PARENT_STATE;
2803 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2804 }
2805 break;
2806 case com.android.internal.R.styleable.View_visibility:
2807 final int visibility = a.getInt(attr, 0);
2808 if (visibility != 0) {
2809 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2810 viewFlagMasks |= VISIBILITY_MASK;
2811 }
2812 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002813 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002814 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002815 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002816 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002817 final int layoutDirection = a.getInt(attr, -1);
2818 if (layoutDirection != -1) {
2819 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002820 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002821 // Set to default (LAYOUT_DIRECTION_INHERIT)
2822 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002823 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002824 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002825 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002826 case com.android.internal.R.styleable.View_drawingCacheQuality:
2827 final int cacheQuality = a.getInt(attr, 0);
2828 if (cacheQuality != 0) {
2829 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2830 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2831 }
2832 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002833 case com.android.internal.R.styleable.View_contentDescription:
2834 mContentDescription = a.getString(attr);
2835 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002836 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2837 if (!a.getBoolean(attr, true)) {
2838 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2839 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2840 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002841 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2843 if (!a.getBoolean(attr, true)) {
2844 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2845 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2846 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002847 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 case R.styleable.View_scrollbars:
2849 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2850 if (scrollbars != SCROLLBARS_NONE) {
2851 viewFlagValues |= scrollbars;
2852 viewFlagMasks |= SCROLLBARS_MASK;
2853 initializeScrollbars(a);
2854 }
2855 break;
2856 case R.styleable.View_fadingEdge:
2857 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2858 if (fadingEdge != FADING_EDGE_NONE) {
2859 viewFlagValues |= fadingEdge;
2860 viewFlagMasks |= FADING_EDGE_MASK;
2861 initializeFadingEdge(a);
2862 }
2863 break;
2864 case R.styleable.View_scrollbarStyle:
2865 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2866 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2867 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2868 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2869 }
2870 break;
2871 case R.styleable.View_isScrollContainer:
2872 setScrollContainer = true;
2873 if (a.getBoolean(attr, false)) {
2874 setScrollContainer(true);
2875 }
2876 break;
2877 case com.android.internal.R.styleable.View_keepScreenOn:
2878 if (a.getBoolean(attr, false)) {
2879 viewFlagValues |= KEEP_SCREEN_ON;
2880 viewFlagMasks |= KEEP_SCREEN_ON;
2881 }
2882 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002883 case R.styleable.View_filterTouchesWhenObscured:
2884 if (a.getBoolean(attr, false)) {
2885 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2886 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2887 }
2888 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002889 case R.styleable.View_nextFocusLeft:
2890 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2891 break;
2892 case R.styleable.View_nextFocusRight:
2893 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2894 break;
2895 case R.styleable.View_nextFocusUp:
2896 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2897 break;
2898 case R.styleable.View_nextFocusDown:
2899 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2900 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002901 case R.styleable.View_nextFocusForward:
2902 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2903 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002904 case R.styleable.View_minWidth:
2905 mMinWidth = a.getDimensionPixelSize(attr, 0);
2906 break;
2907 case R.styleable.View_minHeight:
2908 mMinHeight = a.getDimensionPixelSize(attr, 0);
2909 break;
Romain Guy9a817362009-05-01 10:57:14 -07002910 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002911 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002912 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002913 + "be used within a restricted context");
2914 }
2915
Romain Guy9a817362009-05-01 10:57:14 -07002916 final String handlerName = a.getString(attr);
2917 if (handlerName != null) {
2918 setOnClickListener(new OnClickListener() {
2919 private Method mHandler;
2920
2921 public void onClick(View v) {
2922 if (mHandler == null) {
2923 try {
2924 mHandler = getContext().getClass().getMethod(handlerName,
2925 View.class);
2926 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002927 int id = getId();
2928 String idText = id == NO_ID ? "" : " with id '"
2929 + getContext().getResources().getResourceEntryName(
2930 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002931 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002932 handlerName + "(View) in the activity "
2933 + getContext().getClass() + " for onClick handler"
2934 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002935 }
2936 }
2937
2938 try {
2939 mHandler.invoke(getContext(), View.this);
2940 } catch (IllegalAccessException e) {
2941 throw new IllegalStateException("Could not execute non "
2942 + "public method of the activity", e);
2943 } catch (InvocationTargetException e) {
2944 throw new IllegalStateException("Could not execute "
2945 + "method of the activity", e);
2946 }
2947 }
2948 });
2949 }
2950 break;
Adam Powell637d3372010-08-25 14:37:03 -07002951 case R.styleable.View_overScrollMode:
2952 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2953 break;
Adam Powell20232d02010-12-08 21:08:53 -08002954 case R.styleable.View_verticalScrollbarPosition:
2955 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2956 break;
Romain Guy171c5922011-01-06 10:04:23 -08002957 case R.styleable.View_layerType:
2958 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2959 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002960 case R.styleable.View_textDirection:
2961 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2962 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002963 }
2964 }
2965
Adam Powell637d3372010-08-25 14:37:03 -07002966 setOverScrollMode(overScrollMode);
2967
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002968 if (background != null) {
2969 setBackgroundDrawable(background);
2970 }
2971
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002972 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2973
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002974 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2975 // layout direction). Those cached values will be used later during padding resolution.
2976 mUserPaddingStart = startPadding;
2977 mUserPaddingEnd = endPadding;
2978
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002979 if (padding >= 0) {
2980 leftPadding = padding;
2981 topPadding = padding;
2982 rightPadding = padding;
2983 bottomPadding = padding;
2984 }
2985
2986 // If the user specified the padding (either with android:padding or
2987 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2988 // use the default padding or the padding from the background drawable
2989 // (stored at this point in mPadding*)
2990 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2991 topPadding >= 0 ? topPadding : mPaddingTop,
2992 rightPadding >= 0 ? rightPadding : mPaddingRight,
2993 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
2994
2995 if (viewFlagMasks != 0) {
2996 setFlags(viewFlagValues, viewFlagMasks);
2997 }
2998
2999 // Needs to be called after mViewFlags is set
3000 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3001 recomputePadding();
3002 }
3003
3004 if (x != 0 || y != 0) {
3005 scrollTo(x, y);
3006 }
3007
Chet Haase73066682010-11-29 15:55:32 -08003008 if (transformSet) {
3009 setTranslationX(tx);
3010 setTranslationY(ty);
3011 setRotation(rotation);
3012 setRotationX(rotationX);
3013 setRotationY(rotationY);
3014 setScaleX(sx);
3015 setScaleY(sy);
3016 }
3017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003018 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3019 setScrollContainer(true);
3020 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003021
3022 computeOpaqueFlags();
3023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 a.recycle();
3025 }
3026
3027 /**
3028 * Non-public constructor for use in testing
3029 */
3030 View() {
3031 }
3032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003033 /**
3034 * <p>
3035 * Initializes the fading edges from a given set of styled attributes. This
3036 * method should be called by subclasses that need fading edges and when an
3037 * instance of these subclasses is created programmatically rather than
3038 * being inflated from XML. This method is automatically called when the XML
3039 * is inflated.
3040 * </p>
3041 *
3042 * @param a the styled attributes set to initialize the fading edges from
3043 */
3044 protected void initializeFadingEdge(TypedArray a) {
3045 initScrollCache();
3046
3047 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3048 R.styleable.View_fadingEdgeLength,
3049 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3050 }
3051
3052 /**
3053 * Returns the size of the vertical faded edges used to indicate that more
3054 * content in this view is visible.
3055 *
3056 * @return The size in pixels of the vertical faded edge or 0 if vertical
3057 * faded edges are not enabled for this view.
3058 * @attr ref android.R.styleable#View_fadingEdgeLength
3059 */
3060 public int getVerticalFadingEdgeLength() {
3061 if (isVerticalFadingEdgeEnabled()) {
3062 ScrollabilityCache cache = mScrollCache;
3063 if (cache != null) {
3064 return cache.fadingEdgeLength;
3065 }
3066 }
3067 return 0;
3068 }
3069
3070 /**
3071 * Set the size of the faded edge used to indicate that more content in this
3072 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003073 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3074 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3075 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003076 *
3077 * @param length The size in pixels of the faded edge used to indicate that more
3078 * content in this view is visible.
3079 */
3080 public void setFadingEdgeLength(int length) {
3081 initScrollCache();
3082 mScrollCache.fadingEdgeLength = length;
3083 }
3084
3085 /**
3086 * Returns the size of the horizontal faded edges used to indicate that more
3087 * content in this view is visible.
3088 *
3089 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3090 * faded edges are not enabled for this view.
3091 * @attr ref android.R.styleable#View_fadingEdgeLength
3092 */
3093 public int getHorizontalFadingEdgeLength() {
3094 if (isHorizontalFadingEdgeEnabled()) {
3095 ScrollabilityCache cache = mScrollCache;
3096 if (cache != null) {
3097 return cache.fadingEdgeLength;
3098 }
3099 }
3100 return 0;
3101 }
3102
3103 /**
3104 * Returns the width of the vertical scrollbar.
3105 *
3106 * @return The width in pixels of the vertical scrollbar or 0 if there
3107 * is no vertical scrollbar.
3108 */
3109 public int getVerticalScrollbarWidth() {
3110 ScrollabilityCache cache = mScrollCache;
3111 if (cache != null) {
3112 ScrollBarDrawable scrollBar = cache.scrollBar;
3113 if (scrollBar != null) {
3114 int size = scrollBar.getSize(true);
3115 if (size <= 0) {
3116 size = cache.scrollBarSize;
3117 }
3118 return size;
3119 }
3120 return 0;
3121 }
3122 return 0;
3123 }
3124
3125 /**
3126 * Returns the height of the horizontal scrollbar.
3127 *
3128 * @return The height in pixels of the horizontal scrollbar or 0 if
3129 * there is no horizontal scrollbar.
3130 */
3131 protected int getHorizontalScrollbarHeight() {
3132 ScrollabilityCache cache = mScrollCache;
3133 if (cache != null) {
3134 ScrollBarDrawable scrollBar = cache.scrollBar;
3135 if (scrollBar != null) {
3136 int size = scrollBar.getSize(false);
3137 if (size <= 0) {
3138 size = cache.scrollBarSize;
3139 }
3140 return size;
3141 }
3142 return 0;
3143 }
3144 return 0;
3145 }
3146
3147 /**
3148 * <p>
3149 * Initializes the scrollbars from a given set of styled attributes. This
3150 * method should be called by subclasses that need scrollbars and when an
3151 * instance of these subclasses is created programmatically rather than
3152 * being inflated from XML. This method is automatically called when the XML
3153 * is inflated.
3154 * </p>
3155 *
3156 * @param a the styled attributes set to initialize the scrollbars from
3157 */
3158 protected void initializeScrollbars(TypedArray a) {
3159 initScrollCache();
3160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003161 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003162
Mike Cleronf116bf82009-09-27 19:14:12 -07003163 if (scrollabilityCache.scrollBar == null) {
3164 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3165 }
Joe Malin32736f02011-01-19 16:14:20 -08003166
Romain Guy8bda2482010-03-02 11:42:11 -08003167 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003168
Mike Cleronf116bf82009-09-27 19:14:12 -07003169 if (!fadeScrollbars) {
3170 scrollabilityCache.state = ScrollabilityCache.ON;
3171 }
3172 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003173
3174
Mike Cleronf116bf82009-09-27 19:14:12 -07003175 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3176 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3177 .getScrollBarFadeDuration());
3178 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3179 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3180 ViewConfiguration.getScrollDefaultDelay());
3181
Joe Malin32736f02011-01-19 16:14:20 -08003182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003183 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3184 com.android.internal.R.styleable.View_scrollbarSize,
3185 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3186
3187 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3188 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3189
3190 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3191 if (thumb != null) {
3192 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3193 }
3194
3195 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3196 false);
3197 if (alwaysDraw) {
3198 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3199 }
3200
3201 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3202 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3203
3204 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3205 if (thumb != null) {
3206 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3207 }
3208
3209 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3210 false);
3211 if (alwaysDraw) {
3212 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3213 }
3214
3215 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003216 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003217 }
3218
3219 /**
3220 * <p>
3221 * Initalizes the scrollability cache if necessary.
3222 * </p>
3223 */
3224 private void initScrollCache() {
3225 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003226 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003227 }
3228 }
3229
3230 /**
Adam Powell20232d02010-12-08 21:08:53 -08003231 * Set the position of the vertical scroll bar. Should be one of
3232 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3233 * {@link #SCROLLBAR_POSITION_RIGHT}.
3234 *
3235 * @param position Where the vertical scroll bar should be positioned.
3236 */
3237 public void setVerticalScrollbarPosition(int position) {
3238 if (mVerticalScrollbarPosition != position) {
3239 mVerticalScrollbarPosition = position;
3240 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003241 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003242 }
3243 }
3244
3245 /**
3246 * @return The position where the vertical scroll bar will show, if applicable.
3247 * @see #setVerticalScrollbarPosition(int)
3248 */
3249 public int getVerticalScrollbarPosition() {
3250 return mVerticalScrollbarPosition;
3251 }
3252
3253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003254 * Register a callback to be invoked when focus of this view changed.
3255 *
3256 * @param l The callback that will run.
3257 */
3258 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3259 mOnFocusChangeListener = l;
3260 }
3261
3262 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003263 * Add a listener that will be called when the bounds of the view change due to
3264 * layout processing.
3265 *
3266 * @param listener The listener that will be called when layout bounds change.
3267 */
3268 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3269 if (mOnLayoutChangeListeners == null) {
3270 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3271 }
3272 mOnLayoutChangeListeners.add(listener);
3273 }
3274
3275 /**
3276 * Remove a listener for layout changes.
3277 *
3278 * @param listener The listener for layout bounds change.
3279 */
3280 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3281 if (mOnLayoutChangeListeners == null) {
3282 return;
3283 }
3284 mOnLayoutChangeListeners.remove(listener);
3285 }
3286
3287 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003288 * Add a listener for attach state changes.
3289 *
3290 * This listener will be called whenever this view is attached or detached
3291 * from a window. Remove the listener using
3292 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3293 *
3294 * @param listener Listener to attach
3295 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3296 */
3297 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3298 if (mOnAttachStateChangeListeners == null) {
3299 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3300 }
3301 mOnAttachStateChangeListeners.add(listener);
3302 }
3303
3304 /**
3305 * Remove a listener for attach state changes. The listener will receive no further
3306 * notification of window attach/detach events.
3307 *
3308 * @param listener Listener to remove
3309 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3310 */
3311 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3312 if (mOnAttachStateChangeListeners == null) {
3313 return;
3314 }
3315 mOnAttachStateChangeListeners.remove(listener);
3316 }
3317
3318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003319 * Returns the focus-change callback registered for this view.
3320 *
3321 * @return The callback, or null if one is not registered.
3322 */
3323 public OnFocusChangeListener getOnFocusChangeListener() {
3324 return mOnFocusChangeListener;
3325 }
3326
3327 /**
3328 * Register a callback to be invoked when this view is clicked. If this view is not
3329 * clickable, it becomes clickable.
3330 *
3331 * @param l The callback that will run
3332 *
3333 * @see #setClickable(boolean)
3334 */
3335 public void setOnClickListener(OnClickListener l) {
3336 if (!isClickable()) {
3337 setClickable(true);
3338 }
3339 mOnClickListener = l;
3340 }
3341
3342 /**
3343 * Register a callback to be invoked when this view is clicked and held. If this view is not
3344 * long clickable, it becomes long clickable.
3345 *
3346 * @param l The callback that will run
3347 *
3348 * @see #setLongClickable(boolean)
3349 */
3350 public void setOnLongClickListener(OnLongClickListener l) {
3351 if (!isLongClickable()) {
3352 setLongClickable(true);
3353 }
3354 mOnLongClickListener = l;
3355 }
3356
3357 /**
3358 * Register a callback to be invoked when the context menu for this view is
3359 * being built. If this view is not long clickable, it becomes long clickable.
3360 *
3361 * @param l The callback that will run
3362 *
3363 */
3364 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3365 if (!isLongClickable()) {
3366 setLongClickable(true);
3367 }
3368 mOnCreateContextMenuListener = l;
3369 }
3370
3371 /**
3372 * Call this view's OnClickListener, if it is defined.
3373 *
3374 * @return True there was an assigned OnClickListener that was called, false
3375 * otherwise is returned.
3376 */
3377 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003378 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003380 if (mOnClickListener != null) {
3381 playSoundEffect(SoundEffectConstants.CLICK);
3382 mOnClickListener.onClick(this);
3383 return true;
3384 }
3385
3386 return false;
3387 }
3388
3389 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003390 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3391 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003392 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003393 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003394 */
3395 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003396 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 boolean handled = false;
3399 if (mOnLongClickListener != null) {
3400 handled = mOnLongClickListener.onLongClick(View.this);
3401 }
3402 if (!handled) {
3403 handled = showContextMenu();
3404 }
3405 if (handled) {
3406 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3407 }
3408 return handled;
3409 }
3410
3411 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003412 * Performs button-related actions during a touch down event.
3413 *
3414 * @param event The event.
3415 * @return True if the down was consumed.
3416 *
3417 * @hide
3418 */
3419 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3420 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3421 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3422 return true;
3423 }
3424 }
3425 return false;
3426 }
3427
3428 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003429 * Bring up the context menu for this view.
3430 *
3431 * @return Whether a context menu was displayed.
3432 */
3433 public boolean showContextMenu() {
3434 return getParent().showContextMenuForChild(this);
3435 }
3436
3437 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003438 * Bring up the context menu for this view, referring to the item under the specified point.
3439 *
3440 * @param x The referenced x coordinate.
3441 * @param y The referenced y coordinate.
3442 * @param metaState The keyboard modifiers that were pressed.
3443 * @return Whether a context menu was displayed.
3444 *
3445 * @hide
3446 */
3447 public boolean showContextMenu(float x, float y, int metaState) {
3448 return showContextMenu();
3449 }
3450
3451 /**
Adam Powell6e346362010-07-23 10:18:23 -07003452 * Start an action mode.
3453 *
3454 * @param callback Callback that will control the lifecycle of the action mode
3455 * @return The new action mode if it is started, null otherwise
3456 *
3457 * @see ActionMode
3458 */
3459 public ActionMode startActionMode(ActionMode.Callback callback) {
3460 return getParent().startActionModeForChild(this, callback);
3461 }
3462
3463 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003464 * Register a callback to be invoked when a key is pressed in this view.
3465 * @param l the key listener to attach to this view
3466 */
3467 public void setOnKeyListener(OnKeyListener l) {
3468 mOnKeyListener = l;
3469 }
3470
3471 /**
3472 * Register a callback to be invoked when a touch event is sent to this view.
3473 * @param l the touch listener to attach to this view
3474 */
3475 public void setOnTouchListener(OnTouchListener l) {
3476 mOnTouchListener = l;
3477 }
3478
3479 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003480 * Register a callback to be invoked when a generic motion event is sent to this view.
3481 * @param l the generic motion listener to attach to this view
3482 */
3483 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3484 mOnGenericMotionListener = l;
3485 }
3486
3487 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003488 * Register a callback to be invoked when a hover event is sent to this view.
3489 * @param l the hover listener to attach to this view
3490 */
3491 public void setOnHoverListener(OnHoverListener l) {
3492 mOnHoverListener = l;
3493 }
3494
3495 /**
Joe Malin32736f02011-01-19 16:14:20 -08003496 * Register a drag event listener callback object for this View. The parameter is
3497 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3498 * View, the system calls the
3499 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3500 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003501 */
3502 public void setOnDragListener(OnDragListener l) {
3503 mOnDragListener = l;
3504 }
3505
3506 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003507 * Give this view focus. This will cause
3508 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003509 *
3510 * Note: this does not check whether this {@link View} should get focus, it just
3511 * gives it focus no matter what. It should only be called internally by framework
3512 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3513 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003514 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3515 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003516 * focus moved when requestFocus() is called. It may not always
3517 * apply, in which case use the default View.FOCUS_DOWN.
3518 * @param previouslyFocusedRect The rectangle of the view that had focus
3519 * prior in this View's coordinate system.
3520 */
3521 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3522 if (DBG) {
3523 System.out.println(this + " requestFocus()");
3524 }
3525
3526 if ((mPrivateFlags & FOCUSED) == 0) {
3527 mPrivateFlags |= FOCUSED;
3528
3529 if (mParent != null) {
3530 mParent.requestChildFocus(this, this);
3531 }
3532
3533 onFocusChanged(true, direction, previouslyFocusedRect);
3534 refreshDrawableState();
3535 }
3536 }
3537
3538 /**
3539 * Request that a rectangle of this view be visible on the screen,
3540 * scrolling if necessary just enough.
3541 *
3542 * <p>A View should call this if it maintains some notion of which part
3543 * of its content is interesting. For example, a text editing view
3544 * should call this when its cursor moves.
3545 *
3546 * @param rectangle The rectangle.
3547 * @return Whether any parent scrolled.
3548 */
3549 public boolean requestRectangleOnScreen(Rect rectangle) {
3550 return requestRectangleOnScreen(rectangle, false);
3551 }
3552
3553 /**
3554 * Request that a rectangle of this view be visible on the screen,
3555 * scrolling if necessary just enough.
3556 *
3557 * <p>A View should call this if it maintains some notion of which part
3558 * of its content is interesting. For example, a text editing view
3559 * should call this when its cursor moves.
3560 *
3561 * <p>When <code>immediate</code> is set to true, scrolling will not be
3562 * animated.
3563 *
3564 * @param rectangle The rectangle.
3565 * @param immediate True to forbid animated scrolling, false otherwise
3566 * @return Whether any parent scrolled.
3567 */
3568 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3569 View child = this;
3570 ViewParent parent = mParent;
3571 boolean scrolled = false;
3572 while (parent != null) {
3573 scrolled |= parent.requestChildRectangleOnScreen(child,
3574 rectangle, immediate);
3575
3576 // offset rect so next call has the rectangle in the
3577 // coordinate system of its direct child.
3578 rectangle.offset(child.getLeft(), child.getTop());
3579 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3580
3581 if (!(parent instanceof View)) {
3582 break;
3583 }
Romain Guy8506ab42009-06-11 17:35:47 -07003584
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003585 child = (View) parent;
3586 parent = child.getParent();
3587 }
3588 return scrolled;
3589 }
3590
3591 /**
3592 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003593 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003594 */
3595 public void clearFocus() {
3596 if (DBG) {
3597 System.out.println(this + " clearFocus()");
3598 }
3599
3600 if ((mPrivateFlags & FOCUSED) != 0) {
3601 mPrivateFlags &= ~FOCUSED;
3602
3603 if (mParent != null) {
3604 mParent.clearChildFocus(this);
3605 }
3606
3607 onFocusChanged(false, 0, null);
3608 refreshDrawableState();
3609 }
3610 }
3611
3612 /**
3613 * Called to clear the focus of a view that is about to be removed.
3614 * Doesn't call clearChildFocus, which prevents this view from taking
3615 * focus again before it has been removed from the parent
3616 */
3617 void clearFocusForRemoval() {
3618 if ((mPrivateFlags & FOCUSED) != 0) {
3619 mPrivateFlags &= ~FOCUSED;
3620
3621 onFocusChanged(false, 0, null);
3622 refreshDrawableState();
3623 }
3624 }
3625
3626 /**
3627 * Called internally by the view system when a new view is getting focus.
3628 * This is what clears the old focus.
3629 */
3630 void unFocus() {
3631 if (DBG) {
3632 System.out.println(this + " unFocus()");
3633 }
3634
3635 if ((mPrivateFlags & FOCUSED) != 0) {
3636 mPrivateFlags &= ~FOCUSED;
3637
3638 onFocusChanged(false, 0, null);
3639 refreshDrawableState();
3640 }
3641 }
3642
3643 /**
3644 * Returns true if this view has focus iteself, or is the ancestor of the
3645 * view that has focus.
3646 *
3647 * @return True if this view has or contains focus, false otherwise.
3648 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003649 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003650 public boolean hasFocus() {
3651 return (mPrivateFlags & FOCUSED) != 0;
3652 }
3653
3654 /**
3655 * Returns true if this view is focusable or if it contains a reachable View
3656 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3657 * is a View whose parents do not block descendants focus.
3658 *
3659 * Only {@link #VISIBLE} views are considered focusable.
3660 *
3661 * @return True if the view is focusable or if the view contains a focusable
3662 * View, false otherwise.
3663 *
3664 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3665 */
3666 public boolean hasFocusable() {
3667 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3668 }
3669
3670 /**
3671 * Called by the view system when the focus state of this view changes.
3672 * When the focus change event is caused by directional navigation, direction
3673 * and previouslyFocusedRect provide insight into where the focus is coming from.
3674 * When overriding, be sure to call up through to the super class so that
3675 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003676 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003677 * @param gainFocus True if the View has focus; false otherwise.
3678 * @param direction The direction focus has moved when requestFocus()
3679 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003680 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3681 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3682 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3684 * system, of the previously focused view. If applicable, this will be
3685 * passed in as finer grained information about where the focus is coming
3686 * from (in addition to direction). Will be <code>null</code> otherwise.
3687 */
3688 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003689 if (gainFocus) {
3690 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3691 }
3692
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003693 InputMethodManager imm = InputMethodManager.peekInstance();
3694 if (!gainFocus) {
3695 if (isPressed()) {
3696 setPressed(false);
3697 }
3698 if (imm != null && mAttachInfo != null
3699 && mAttachInfo.mHasWindowFocus) {
3700 imm.focusOut(this);
3701 }
Romain Guya2431d02009-04-30 16:30:00 -07003702 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003703 } else if (imm != null && mAttachInfo != null
3704 && mAttachInfo.mHasWindowFocus) {
3705 imm.focusIn(this);
3706 }
Romain Guy8506ab42009-06-11 17:35:47 -07003707
Romain Guy0fd89bf2011-01-26 15:41:30 -08003708 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 if (mOnFocusChangeListener != null) {
3710 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3711 }
Joe Malin32736f02011-01-19 16:14:20 -08003712
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003713 if (mAttachInfo != null) {
3714 mAttachInfo.mKeyDispatchState.reset(this);
3715 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003716 }
3717
3718 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003719 * Sends an accessibility event of the given type. If accessiiblity is
3720 * not enabled this method has no effect. The default implementation calls
3721 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3722 * to populate information about the event source (this View), then calls
3723 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3724 * populate the text content of the event source including its descendants,
3725 * and last calls
3726 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3727 * on its parent to resuest sending of the event to interested parties.
3728 *
3729 * @param eventType The type of the event to send.
3730 *
3731 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3732 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3733 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003734 */
3735 public void sendAccessibilityEvent(int eventType) {
3736 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3737 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3738 }
3739 }
3740
3741 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003742 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3743 * takes as an argument an empty {@link AccessibilityEvent} and does not
3744 * perfrom a check whether accessibility is enabled.
3745 *
3746 * @param event The event to send.
3747 *
3748 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003749 */
3750 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003751 if (!isShown()) {
3752 return;
3753 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003754 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003755 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003756 // In the beginning we called #isShown(), so we know that getParent() is not null.
3757 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003758 }
3759
3760 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003761 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3762 * to its children for adding their text content to the event. Note that the
3763 * event text is populated in a separate dispatch path since we add to the
3764 * event not only the text of the source but also the text of all its descendants.
3765 * </p>
3766 * A typical implementation will call
3767 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3768 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3769 * on each child. Override this method if custom population of the event text
3770 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003771 *
3772 * @param event The event.
3773 *
3774 * @return True if the event population was completed.
3775 */
3776 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003777 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003778 return false;
3779 }
3780
3781 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003782 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003783 * giving a chance to this View to populate the accessibility event with its
3784 * text content. While the implementation is free to modify other event
3785 * attributes this should be performed in
3786 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3787 * <p>
3788 * Example: Adding formatted date string to an accessibility event in addition
3789 * to the text added by the super implementation.
3790 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003791 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3792 * super.onPopulateAccessibilityEvent(event);
3793 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3794 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3795 * mCurrentDate.getTimeInMillis(), flags);
3796 * event.getText().add(selectedDateUtterance);
3797 * }
3798 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003799 *
3800 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003801 *
3802 * @see #sendAccessibilityEvent(int)
3803 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003804 */
3805 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003806 }
3807
3808 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003809 * Initializes an {@link AccessibilityEvent} with information about the
3810 * the type of the event and this View which is the event source. In other
3811 * words, the source of an accessibility event is the view whose state
3812 * change triggered firing the event.
3813 * <p>
3814 * Example: Setting the password property of an event in addition
3815 * to properties set by the super implementation.
3816 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003817 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3818 * super.onInitializeAccessibilityEvent(event);
3819 * event.setPassword(true);
3820 * }
3821 * </code></pre></p>
3822 * @param event The event to initialeze.
3823 *
3824 * @see #sendAccessibilityEvent(int)
3825 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3826 */
3827 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003828 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003829 event.setClassName(getClass().getName());
3830 event.setPackageName(getContext().getPackageName());
3831 event.setEnabled(isEnabled());
3832 event.setContentDescription(mContentDescription);
3833
Svetoslav Ganova0156172011-06-26 17:55:44 -07003834 final int eventType = event.getEventType();
3835 switch (eventType) {
3836 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3837 if (mAttachInfo != null) {
3838 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3839 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3840 FOCUSABLES_ALL);
3841 event.setItemCount(focusablesTempList.size());
3842 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3843 focusablesTempList.clear();
3844 }
3845 } break;
3846 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3847 event.setScrollX(mScrollX);
3848 event.setScrollY(mScrollY);
3849 event.setItemCount(getHeight());
3850 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003851 }
3852 }
3853
3854 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003855 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3856 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3857 * This method is responsible for obtaining an accessibility node info from a
3858 * pool of reusable instances and calling
3859 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3860 * initialize the former.
3861 * <p>
3862 * Note: The client is responsible for recycling the obtained instance by calling
3863 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3864 * </p>
3865 * @return A populated {@link AccessibilityNodeInfo}.
3866 *
3867 * @see AccessibilityNodeInfo
3868 */
3869 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3870 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3871 onInitializeAccessibilityNodeInfo(info);
3872 return info;
3873 }
3874
3875 /**
3876 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3877 * The base implementation sets:
3878 * <ul>
3879 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003880 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3881 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003882 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3883 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3884 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3885 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3886 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3887 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3888 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3889 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3890 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3891 * </ul>
3892 * <p>
3893 * Subclasses should override this method, call the super implementation,
3894 * and set additional attributes.
3895 * </p>
3896 * @param info The instance to initialize.
3897 */
3898 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3899 Rect bounds = mAttachInfo.mTmpInvalRect;
3900 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003901 info.setBoundsInParent(bounds);
3902
3903 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3904 getLocationOnScreen(locationOnScreen);
3905 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3906 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003907
3908 ViewParent parent = getParent();
3909 if (parent instanceof View) {
3910 View parentView = (View) parent;
3911 info.setParent(parentView);
3912 }
3913
3914 info.setPackageName(mContext.getPackageName());
3915 info.setClassName(getClass().getName());
3916 info.setContentDescription(getContentDescription());
3917
3918 info.setEnabled(isEnabled());
3919 info.setClickable(isClickable());
3920 info.setFocusable(isFocusable());
3921 info.setFocused(isFocused());
3922 info.setSelected(isSelected());
3923 info.setLongClickable(isLongClickable());
3924
3925 // TODO: These make sense only if we are in an AdapterView but all
3926 // views can be selected. Maybe from accessiiblity perspective
3927 // we should report as selectable view in an AdapterView.
3928 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3929 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3930
3931 if (isFocusable()) {
3932 if (isFocused()) {
3933 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3934 } else {
3935 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3936 }
3937 }
3938 }
3939
3940 /**
3941 * Gets the unique identifier of this view on the screen for accessibility purposes.
3942 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3943 *
3944 * @return The view accessibility id.
3945 *
3946 * @hide
3947 */
3948 public int getAccessibilityViewId() {
3949 if (mAccessibilityViewId == NO_ID) {
3950 mAccessibilityViewId = sNextAccessibilityViewId++;
3951 }
3952 return mAccessibilityViewId;
3953 }
3954
3955 /**
3956 * Gets the unique identifier of the window in which this View reseides.
3957 *
3958 * @return The window accessibility id.
3959 *
3960 * @hide
3961 */
3962 public int getAccessibilityWindowId() {
3963 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3964 }
3965
3966 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003967 * Gets the {@link View} description. It briefly describes the view and is
3968 * primarily used for accessibility support. Set this property to enable
3969 * better accessibility support for your application. This is especially
3970 * true for views that do not have textual representation (For example,
3971 * ImageButton).
3972 *
3973 * @return The content descriptiopn.
3974 *
3975 * @attr ref android.R.styleable#View_contentDescription
3976 */
3977 public CharSequence getContentDescription() {
3978 return mContentDescription;
3979 }
3980
3981 /**
3982 * Sets the {@link View} description. It briefly describes the view and is
3983 * primarily used for accessibility support. Set this property to enable
3984 * better accessibility support for your application. This is especially
3985 * true for views that do not have textual representation (For example,
3986 * ImageButton).
3987 *
3988 * @param contentDescription The content description.
3989 *
3990 * @attr ref android.R.styleable#View_contentDescription
3991 */
3992 public void setContentDescription(CharSequence contentDescription) {
3993 mContentDescription = contentDescription;
3994 }
3995
3996 /**
Romain Guya2431d02009-04-30 16:30:00 -07003997 * Invoked whenever this view loses focus, either by losing window focus or by losing
3998 * focus within its window. This method can be used to clear any state tied to the
3999 * focus. For instance, if a button is held pressed with the trackball and the window
4000 * loses focus, this method can be used to cancel the press.
4001 *
4002 * Subclasses of View overriding this method should always call super.onFocusLost().
4003 *
4004 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004005 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004006 *
4007 * @hide pending API council approval
4008 */
4009 protected void onFocusLost() {
4010 resetPressedState();
4011 }
4012
4013 private void resetPressedState() {
4014 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4015 return;
4016 }
4017
4018 if (isPressed()) {
4019 setPressed(false);
4020
4021 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004022 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004023 }
4024 }
4025 }
4026
4027 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004028 * Returns true if this view has focus
4029 *
4030 * @return True if this view has focus, false otherwise.
4031 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004032 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004033 public boolean isFocused() {
4034 return (mPrivateFlags & FOCUSED) != 0;
4035 }
4036
4037 /**
4038 * Find the view in the hierarchy rooted at this view that currently has
4039 * focus.
4040 *
4041 * @return The view that currently has focus, or null if no focused view can
4042 * be found.
4043 */
4044 public View findFocus() {
4045 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4046 }
4047
4048 /**
4049 * Change whether this view is one of the set of scrollable containers in
4050 * its window. This will be used to determine whether the window can
4051 * resize or must pan when a soft input area is open -- scrollable
4052 * containers allow the window to use resize mode since the container
4053 * will appropriately shrink.
4054 */
4055 public void setScrollContainer(boolean isScrollContainer) {
4056 if (isScrollContainer) {
4057 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4058 mAttachInfo.mScrollContainers.add(this);
4059 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4060 }
4061 mPrivateFlags |= SCROLL_CONTAINER;
4062 } else {
4063 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4064 mAttachInfo.mScrollContainers.remove(this);
4065 }
4066 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4067 }
4068 }
4069
4070 /**
4071 * Returns the quality of the drawing cache.
4072 *
4073 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4074 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4075 *
4076 * @see #setDrawingCacheQuality(int)
4077 * @see #setDrawingCacheEnabled(boolean)
4078 * @see #isDrawingCacheEnabled()
4079 *
4080 * @attr ref android.R.styleable#View_drawingCacheQuality
4081 */
4082 public int getDrawingCacheQuality() {
4083 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4084 }
4085
4086 /**
4087 * Set the drawing cache quality of this view. This value is used only when the
4088 * drawing cache is enabled
4089 *
4090 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4091 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4092 *
4093 * @see #getDrawingCacheQuality()
4094 * @see #setDrawingCacheEnabled(boolean)
4095 * @see #isDrawingCacheEnabled()
4096 *
4097 * @attr ref android.R.styleable#View_drawingCacheQuality
4098 */
4099 public void setDrawingCacheQuality(int quality) {
4100 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4101 }
4102
4103 /**
4104 * Returns whether the screen should remain on, corresponding to the current
4105 * value of {@link #KEEP_SCREEN_ON}.
4106 *
4107 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4108 *
4109 * @see #setKeepScreenOn(boolean)
4110 *
4111 * @attr ref android.R.styleable#View_keepScreenOn
4112 */
4113 public boolean getKeepScreenOn() {
4114 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4115 }
4116
4117 /**
4118 * Controls whether the screen should remain on, modifying the
4119 * value of {@link #KEEP_SCREEN_ON}.
4120 *
4121 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4122 *
4123 * @see #getKeepScreenOn()
4124 *
4125 * @attr ref android.R.styleable#View_keepScreenOn
4126 */
4127 public void setKeepScreenOn(boolean keepScreenOn) {
4128 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4129 }
4130
4131 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004132 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4133 * @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 -08004134 *
4135 * @attr ref android.R.styleable#View_nextFocusLeft
4136 */
4137 public int getNextFocusLeftId() {
4138 return mNextFocusLeftId;
4139 }
4140
4141 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004142 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4143 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4144 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004145 *
4146 * @attr ref android.R.styleable#View_nextFocusLeft
4147 */
4148 public void setNextFocusLeftId(int nextFocusLeftId) {
4149 mNextFocusLeftId = nextFocusLeftId;
4150 }
4151
4152 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004153 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4154 * @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 -08004155 *
4156 * @attr ref android.R.styleable#View_nextFocusRight
4157 */
4158 public int getNextFocusRightId() {
4159 return mNextFocusRightId;
4160 }
4161
4162 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004163 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4164 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4165 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004166 *
4167 * @attr ref android.R.styleable#View_nextFocusRight
4168 */
4169 public void setNextFocusRightId(int nextFocusRightId) {
4170 mNextFocusRightId = nextFocusRightId;
4171 }
4172
4173 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004174 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4175 * @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 -08004176 *
4177 * @attr ref android.R.styleable#View_nextFocusUp
4178 */
4179 public int getNextFocusUpId() {
4180 return mNextFocusUpId;
4181 }
4182
4183 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004184 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4185 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4186 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004187 *
4188 * @attr ref android.R.styleable#View_nextFocusUp
4189 */
4190 public void setNextFocusUpId(int nextFocusUpId) {
4191 mNextFocusUpId = nextFocusUpId;
4192 }
4193
4194 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004195 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4196 * @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 -08004197 *
4198 * @attr ref android.R.styleable#View_nextFocusDown
4199 */
4200 public int getNextFocusDownId() {
4201 return mNextFocusDownId;
4202 }
4203
4204 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004205 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4206 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4207 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004208 *
4209 * @attr ref android.R.styleable#View_nextFocusDown
4210 */
4211 public void setNextFocusDownId(int nextFocusDownId) {
4212 mNextFocusDownId = nextFocusDownId;
4213 }
4214
4215 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004216 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4217 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4218 *
4219 * @attr ref android.R.styleable#View_nextFocusForward
4220 */
4221 public int getNextFocusForwardId() {
4222 return mNextFocusForwardId;
4223 }
4224
4225 /**
4226 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4227 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4228 * decide automatically.
4229 *
4230 * @attr ref android.R.styleable#View_nextFocusForward
4231 */
4232 public void setNextFocusForwardId(int nextFocusForwardId) {
4233 mNextFocusForwardId = nextFocusForwardId;
4234 }
4235
4236 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004237 * Returns the visibility of this view and all of its ancestors
4238 *
4239 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4240 */
4241 public boolean isShown() {
4242 View current = this;
4243 //noinspection ConstantConditions
4244 do {
4245 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4246 return false;
4247 }
4248 ViewParent parent = current.mParent;
4249 if (parent == null) {
4250 return false; // We are not attached to the view root
4251 }
4252 if (!(parent instanceof View)) {
4253 return true;
4254 }
4255 current = (View) parent;
4256 } while (current != null);
4257
4258 return false;
4259 }
4260
4261 /**
4262 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4263 * is set
4264 *
4265 * @param insets Insets for system windows
4266 *
4267 * @return True if this view applied the insets, false otherwise
4268 */
4269 protected boolean fitSystemWindows(Rect insets) {
4270 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4271 mPaddingLeft = insets.left;
4272 mPaddingTop = insets.top;
4273 mPaddingRight = insets.right;
4274 mPaddingBottom = insets.bottom;
4275 requestLayout();
4276 return true;
4277 }
4278 return false;
4279 }
4280
4281 /**
4282 * Returns the visibility status for this view.
4283 *
4284 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4285 * @attr ref android.R.styleable#View_visibility
4286 */
4287 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004288 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4289 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4290 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004291 })
4292 public int getVisibility() {
4293 return mViewFlags & VISIBILITY_MASK;
4294 }
4295
4296 /**
4297 * Set the enabled state of this view.
4298 *
4299 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4300 * @attr ref android.R.styleable#View_visibility
4301 */
4302 @RemotableViewMethod
4303 public void setVisibility(int visibility) {
4304 setFlags(visibility, VISIBILITY_MASK);
4305 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4306 }
4307
4308 /**
4309 * Returns the enabled status for this view. The interpretation of the
4310 * enabled state varies by subclass.
4311 *
4312 * @return True if this view is enabled, false otherwise.
4313 */
4314 @ViewDebug.ExportedProperty
4315 public boolean isEnabled() {
4316 return (mViewFlags & ENABLED_MASK) == ENABLED;
4317 }
4318
4319 /**
4320 * Set the enabled state of this view. The interpretation of the enabled
4321 * state varies by subclass.
4322 *
4323 * @param enabled True if this view is enabled, false otherwise.
4324 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004325 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004326 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004327 if (enabled == isEnabled()) return;
4328
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004329 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4330
4331 /*
4332 * The View most likely has to change its appearance, so refresh
4333 * the drawable state.
4334 */
4335 refreshDrawableState();
4336
4337 // Invalidate too, since the default behavior for views is to be
4338 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004339 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004340 }
4341
4342 /**
4343 * Set whether this view can receive the focus.
4344 *
4345 * Setting this to false will also ensure that this view is not focusable
4346 * in touch mode.
4347 *
4348 * @param focusable If true, this view can receive the focus.
4349 *
4350 * @see #setFocusableInTouchMode(boolean)
4351 * @attr ref android.R.styleable#View_focusable
4352 */
4353 public void setFocusable(boolean focusable) {
4354 if (!focusable) {
4355 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4356 }
4357 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4358 }
4359
4360 /**
4361 * Set whether this view can receive focus while in touch mode.
4362 *
4363 * Setting this to true will also ensure that this view is focusable.
4364 *
4365 * @param focusableInTouchMode If true, this view can receive the focus while
4366 * in touch mode.
4367 *
4368 * @see #setFocusable(boolean)
4369 * @attr ref android.R.styleable#View_focusableInTouchMode
4370 */
4371 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4372 // Focusable in touch mode should always be set before the focusable flag
4373 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4374 // which, in touch mode, will not successfully request focus on this view
4375 // because the focusable in touch mode flag is not set
4376 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4377 if (focusableInTouchMode) {
4378 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4379 }
4380 }
4381
4382 /**
4383 * Set whether this view should have sound effects enabled for events such as
4384 * clicking and touching.
4385 *
4386 * <p>You may wish to disable sound effects for a view if you already play sounds,
4387 * for instance, a dial key that plays dtmf tones.
4388 *
4389 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4390 * @see #isSoundEffectsEnabled()
4391 * @see #playSoundEffect(int)
4392 * @attr ref android.R.styleable#View_soundEffectsEnabled
4393 */
4394 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4395 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4396 }
4397
4398 /**
4399 * @return whether this view should have sound effects enabled for events such as
4400 * clicking and touching.
4401 *
4402 * @see #setSoundEffectsEnabled(boolean)
4403 * @see #playSoundEffect(int)
4404 * @attr ref android.R.styleable#View_soundEffectsEnabled
4405 */
4406 @ViewDebug.ExportedProperty
4407 public boolean isSoundEffectsEnabled() {
4408 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4409 }
4410
4411 /**
4412 * Set whether this view should have haptic feedback for events such as
4413 * long presses.
4414 *
4415 * <p>You may wish to disable haptic feedback if your view already controls
4416 * its own haptic feedback.
4417 *
4418 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4419 * @see #isHapticFeedbackEnabled()
4420 * @see #performHapticFeedback(int)
4421 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4422 */
4423 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4424 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4425 }
4426
4427 /**
4428 * @return whether this view should have haptic feedback enabled for events
4429 * long presses.
4430 *
4431 * @see #setHapticFeedbackEnabled(boolean)
4432 * @see #performHapticFeedback(int)
4433 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4434 */
4435 @ViewDebug.ExportedProperty
4436 public boolean isHapticFeedbackEnabled() {
4437 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4438 }
4439
4440 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004441 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004442 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004443 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4444 * {@link #LAYOUT_DIRECTION_RTL},
4445 * {@link #LAYOUT_DIRECTION_INHERIT} or
4446 * {@link #LAYOUT_DIRECTION_LOCALE}.
4447 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004448 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004449 * @hide
4450 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004451 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004452 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4453 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4454 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4455 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004456 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004457 public int getLayoutDirection() {
4458 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004459 }
4460
4461 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004462 * Set the layout direction for this view. This will propagate a reset of layout direction
4463 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004464 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004465 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4466 * {@link #LAYOUT_DIRECTION_RTL},
4467 * {@link #LAYOUT_DIRECTION_INHERIT} or
4468 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004469 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004470 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004471 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004472 * @hide
4473 */
4474 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004475 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004476 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004477 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004478 // Setting the flag will also request a layout.
4479 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4480 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004481 }
4482
4483 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004484 * Returns the resolved layout direction for this view.
4485 *
4486 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4487 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4488 *
4489 * @hide
4490 */
4491 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4492 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4493 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4494 })
4495 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004496 resolveLayoutDirectionIfNeeded();
4497 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004498 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4499 }
4500
4501 /**
4502 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4503 * layout attribute and/or the inherited value from the parent.</p>
4504 *
4505 * @return true if the layout is right-to-left.
4506 *
4507 * @hide
4508 */
4509 @ViewDebug.ExportedProperty(category = "layout")
4510 public boolean isLayoutRtl() {
4511 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4512 }
4513
4514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004515 * If this view doesn't do any drawing on its own, set this flag to
4516 * allow further optimizations. By default, this flag is not set on
4517 * View, but could be set on some View subclasses such as ViewGroup.
4518 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004519 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4520 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 *
4522 * @param willNotDraw whether or not this View draw on its own
4523 */
4524 public void setWillNotDraw(boolean willNotDraw) {
4525 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4526 }
4527
4528 /**
4529 * Returns whether or not this View draws on its own.
4530 *
4531 * @return true if this view has nothing to draw, false otherwise
4532 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004533 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004534 public boolean willNotDraw() {
4535 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4536 }
4537
4538 /**
4539 * When a View's drawing cache is enabled, drawing is redirected to an
4540 * offscreen bitmap. Some views, like an ImageView, must be able to
4541 * bypass this mechanism if they already draw a single bitmap, to avoid
4542 * unnecessary usage of the memory.
4543 *
4544 * @param willNotCacheDrawing true if this view does not cache its
4545 * drawing, false otherwise
4546 */
4547 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4548 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4549 }
4550
4551 /**
4552 * Returns whether or not this View can cache its drawing or not.
4553 *
4554 * @return true if this view does not cache its drawing, false otherwise
4555 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004556 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004557 public boolean willNotCacheDrawing() {
4558 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4559 }
4560
4561 /**
4562 * Indicates whether this view reacts to click events or not.
4563 *
4564 * @return true if the view is clickable, false otherwise
4565 *
4566 * @see #setClickable(boolean)
4567 * @attr ref android.R.styleable#View_clickable
4568 */
4569 @ViewDebug.ExportedProperty
4570 public boolean isClickable() {
4571 return (mViewFlags & CLICKABLE) == CLICKABLE;
4572 }
4573
4574 /**
4575 * Enables or disables click events for this view. When a view
4576 * is clickable it will change its state to "pressed" on every click.
4577 * Subclasses should set the view clickable to visually react to
4578 * user's clicks.
4579 *
4580 * @param clickable true to make the view clickable, false otherwise
4581 *
4582 * @see #isClickable()
4583 * @attr ref android.R.styleable#View_clickable
4584 */
4585 public void setClickable(boolean clickable) {
4586 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4587 }
4588
4589 /**
4590 * Indicates whether this view reacts to long click events or not.
4591 *
4592 * @return true if the view is long clickable, false otherwise
4593 *
4594 * @see #setLongClickable(boolean)
4595 * @attr ref android.R.styleable#View_longClickable
4596 */
4597 public boolean isLongClickable() {
4598 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4599 }
4600
4601 /**
4602 * Enables or disables long click events for this view. When a view is long
4603 * clickable it reacts to the user holding down the button for a longer
4604 * duration than a tap. This event can either launch the listener or a
4605 * context menu.
4606 *
4607 * @param longClickable true to make the view long clickable, false otherwise
4608 * @see #isLongClickable()
4609 * @attr ref android.R.styleable#View_longClickable
4610 */
4611 public void setLongClickable(boolean longClickable) {
4612 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4613 }
4614
4615 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004616 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004617 *
4618 * @see #isClickable()
4619 * @see #setClickable(boolean)
4620 *
4621 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4622 * the View's internal state from a previously set "pressed" state.
4623 */
4624 public void setPressed(boolean pressed) {
4625 if (pressed) {
4626 mPrivateFlags |= PRESSED;
4627 } else {
4628 mPrivateFlags &= ~PRESSED;
4629 }
4630 refreshDrawableState();
4631 dispatchSetPressed(pressed);
4632 }
4633
4634 /**
4635 * Dispatch setPressed to all of this View's children.
4636 *
4637 * @see #setPressed(boolean)
4638 *
4639 * @param pressed The new pressed state
4640 */
4641 protected void dispatchSetPressed(boolean pressed) {
4642 }
4643
4644 /**
4645 * Indicates whether the view is currently in pressed state. Unless
4646 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4647 * the pressed state.
4648 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004649 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004650 * @see #isClickable()
4651 * @see #setClickable(boolean)
4652 *
4653 * @return true if the view is currently pressed, false otherwise
4654 */
4655 public boolean isPressed() {
4656 return (mPrivateFlags & PRESSED) == PRESSED;
4657 }
4658
4659 /**
4660 * Indicates whether this view will save its state (that is,
4661 * whether its {@link #onSaveInstanceState} method will be called).
4662 *
4663 * @return Returns true if the view state saving is enabled, else false.
4664 *
4665 * @see #setSaveEnabled(boolean)
4666 * @attr ref android.R.styleable#View_saveEnabled
4667 */
4668 public boolean isSaveEnabled() {
4669 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4670 }
4671
4672 /**
4673 * Controls whether the saving of this view's state is
4674 * enabled (that is, whether its {@link #onSaveInstanceState} method
4675 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004676 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004677 * for its state to be saved. This flag can only disable the
4678 * saving of this view; any child views may still have their state saved.
4679 *
4680 * @param enabled Set to false to <em>disable</em> state saving, or true
4681 * (the default) to allow it.
4682 *
4683 * @see #isSaveEnabled()
4684 * @see #setId(int)
4685 * @see #onSaveInstanceState()
4686 * @attr ref android.R.styleable#View_saveEnabled
4687 */
4688 public void setSaveEnabled(boolean enabled) {
4689 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4690 }
4691
Jeff Brown85a31762010-09-01 17:01:00 -07004692 /**
4693 * Gets whether the framework should discard touches when the view's
4694 * window is obscured by another visible window.
4695 * Refer to the {@link View} security documentation for more details.
4696 *
4697 * @return True if touch filtering is enabled.
4698 *
4699 * @see #setFilterTouchesWhenObscured(boolean)
4700 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4701 */
4702 @ViewDebug.ExportedProperty
4703 public boolean getFilterTouchesWhenObscured() {
4704 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4705 }
4706
4707 /**
4708 * Sets whether the framework should discard touches when the view's
4709 * window is obscured by another visible window.
4710 * Refer to the {@link View} security documentation for more details.
4711 *
4712 * @param enabled True if touch filtering should be enabled.
4713 *
4714 * @see #getFilterTouchesWhenObscured
4715 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4716 */
4717 public void setFilterTouchesWhenObscured(boolean enabled) {
4718 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4719 FILTER_TOUCHES_WHEN_OBSCURED);
4720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004721
4722 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004723 * Indicates whether the entire hierarchy under this view will save its
4724 * state when a state saving traversal occurs from its parent. The default
4725 * is true; if false, these views will not be saved unless
4726 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4727 *
4728 * @return Returns true if the view state saving from parent is enabled, else false.
4729 *
4730 * @see #setSaveFromParentEnabled(boolean)
4731 */
4732 public boolean isSaveFromParentEnabled() {
4733 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4734 }
4735
4736 /**
4737 * Controls whether the entire hierarchy under this view will save its
4738 * state when a state saving traversal occurs from its parent. The default
4739 * is true; if false, these views will not be saved unless
4740 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4741 *
4742 * @param enabled Set to false to <em>disable</em> state saving, or true
4743 * (the default) to allow it.
4744 *
4745 * @see #isSaveFromParentEnabled()
4746 * @see #setId(int)
4747 * @see #onSaveInstanceState()
4748 */
4749 public void setSaveFromParentEnabled(boolean enabled) {
4750 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4751 }
4752
4753
4754 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004755 * Returns whether this View is able to take focus.
4756 *
4757 * @return True if this view can take focus, or false otherwise.
4758 * @attr ref android.R.styleable#View_focusable
4759 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004760 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 public final boolean isFocusable() {
4762 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4763 }
4764
4765 /**
4766 * When a view is focusable, it may not want to take focus when in touch mode.
4767 * For example, a button would like focus when the user is navigating via a D-pad
4768 * so that the user can click on it, but once the user starts touching the screen,
4769 * the button shouldn't take focus
4770 * @return Whether the view is focusable in touch mode.
4771 * @attr ref android.R.styleable#View_focusableInTouchMode
4772 */
4773 @ViewDebug.ExportedProperty
4774 public final boolean isFocusableInTouchMode() {
4775 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4776 }
4777
4778 /**
4779 * Find the nearest view in the specified direction that can take focus.
4780 * This does not actually give focus to that view.
4781 *
4782 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4783 *
4784 * @return The nearest focusable in the specified direction, or null if none
4785 * can be found.
4786 */
4787 public View focusSearch(int direction) {
4788 if (mParent != null) {
4789 return mParent.focusSearch(this, direction);
4790 } else {
4791 return null;
4792 }
4793 }
4794
4795 /**
4796 * This method is the last chance for the focused view and its ancestors to
4797 * respond to an arrow key. This is called when the focused view did not
4798 * consume the key internally, nor could the view system find a new view in
4799 * the requested direction to give focus to.
4800 *
4801 * @param focused The currently focused view.
4802 * @param direction The direction focus wants to move. One of FOCUS_UP,
4803 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4804 * @return True if the this view consumed this unhandled move.
4805 */
4806 public boolean dispatchUnhandledMove(View focused, int direction) {
4807 return false;
4808 }
4809
4810 /**
4811 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004812 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004813 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004814 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4815 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004816 * @return The user specified next view, or null if there is none.
4817 */
4818 View findUserSetNextFocus(View root, int direction) {
4819 switch (direction) {
4820 case FOCUS_LEFT:
4821 if (mNextFocusLeftId == View.NO_ID) return null;
4822 return findViewShouldExist(root, mNextFocusLeftId);
4823 case FOCUS_RIGHT:
4824 if (mNextFocusRightId == View.NO_ID) return null;
4825 return findViewShouldExist(root, mNextFocusRightId);
4826 case FOCUS_UP:
4827 if (mNextFocusUpId == View.NO_ID) return null;
4828 return findViewShouldExist(root, mNextFocusUpId);
4829 case FOCUS_DOWN:
4830 if (mNextFocusDownId == View.NO_ID) return null;
4831 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004832 case FOCUS_FORWARD:
4833 if (mNextFocusForwardId == View.NO_ID) return null;
4834 return findViewShouldExist(root, mNextFocusForwardId);
4835 case FOCUS_BACKWARD: {
4836 final int id = mID;
4837 return root.findViewByPredicate(new Predicate<View>() {
4838 @Override
4839 public boolean apply(View t) {
4840 return t.mNextFocusForwardId == id;
4841 }
4842 });
4843 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004844 }
4845 return null;
4846 }
4847
4848 private static View findViewShouldExist(View root, int childViewId) {
4849 View result = root.findViewById(childViewId);
4850 if (result == null) {
4851 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4852 + "by user for id " + childViewId);
4853 }
4854 return result;
4855 }
4856
4857 /**
4858 * Find and return all focusable views that are descendants of this view,
4859 * possibly including this view if it is focusable itself.
4860 *
4861 * @param direction The direction of the focus
4862 * @return A list of focusable views
4863 */
4864 public ArrayList<View> getFocusables(int direction) {
4865 ArrayList<View> result = new ArrayList<View>(24);
4866 addFocusables(result, direction);
4867 return result;
4868 }
4869
4870 /**
4871 * Add any focusable views that are descendants of this view (possibly
4872 * including this view if it is focusable itself) to views. If we are in touch mode,
4873 * only add views that are also focusable in touch mode.
4874 *
4875 * @param views Focusable views found so far
4876 * @param direction The direction of the focus
4877 */
4878 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004879 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004881
svetoslavganov75986cf2009-05-14 22:28:01 -07004882 /**
4883 * Adds any focusable views that are descendants of this view (possibly
4884 * including this view if it is focusable itself) to views. This method
4885 * adds all focusable views regardless if we are in touch mode or
4886 * only views focusable in touch mode if we are in touch mode depending on
4887 * the focusable mode paramater.
4888 *
4889 * @param views Focusable views found so far or null if all we are interested is
4890 * the number of focusables.
4891 * @param direction The direction of the focus.
4892 * @param focusableMode The type of focusables to be added.
4893 *
4894 * @see #FOCUSABLES_ALL
4895 * @see #FOCUSABLES_TOUCH_MODE
4896 */
4897 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4898 if (!isFocusable()) {
4899 return;
4900 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004901
svetoslavganov75986cf2009-05-14 22:28:01 -07004902 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4903 isInTouchMode() && !isFocusableInTouchMode()) {
4904 return;
4905 }
4906
4907 if (views != null) {
4908 views.add(this);
4909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004910 }
4911
4912 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004913 * Finds the Views that contain given text. The containment is case insensitive.
4914 * As View's text is considered any text content that View renders.
4915 *
4916 * @param outViews The output list of matching Views.
4917 * @param text The text to match against.
4918 */
4919 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4920 }
4921
4922 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 * Find and return all touchable views that are descendants of this view,
4924 * possibly including this view if it is touchable itself.
4925 *
4926 * @return A list of touchable views
4927 */
4928 public ArrayList<View> getTouchables() {
4929 ArrayList<View> result = new ArrayList<View>();
4930 addTouchables(result);
4931 return result;
4932 }
4933
4934 /**
4935 * Add any touchable views that are descendants of this view (possibly
4936 * including this view if it is touchable itself) to views.
4937 *
4938 * @param views Touchable views found so far
4939 */
4940 public void addTouchables(ArrayList<View> views) {
4941 final int viewFlags = mViewFlags;
4942
4943 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4944 && (viewFlags & ENABLED_MASK) == ENABLED) {
4945 views.add(this);
4946 }
4947 }
4948
4949 /**
4950 * Call this to try to give focus to a specific view or to one of its
4951 * descendants.
4952 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004953 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4954 * false), or if it is focusable and it is not focusable in touch mode
4955 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004956 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004957 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004958 * have focus, and you want your parent to look for the next one.
4959 *
4960 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4961 * {@link #FOCUS_DOWN} and <code>null</code>.
4962 *
4963 * @return Whether this view or one of its descendants actually took focus.
4964 */
4965 public final boolean requestFocus() {
4966 return requestFocus(View.FOCUS_DOWN);
4967 }
4968
4969
4970 /**
4971 * Call this to try to give focus to a specific view or to one of its
4972 * descendants and give it a hint about what direction focus is heading.
4973 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004974 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4975 * false), or if it is focusable and it is not focusable in touch mode
4976 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004977 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004978 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004979 * have focus, and you want your parent to look for the next one.
4980 *
4981 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4982 * <code>null</code> set for the previously focused rectangle.
4983 *
4984 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4985 * @return Whether this view or one of its descendants actually took focus.
4986 */
4987 public final boolean requestFocus(int direction) {
4988 return requestFocus(direction, null);
4989 }
4990
4991 /**
4992 * Call this to try to give focus to a specific view or to one of its descendants
4993 * and give it hints about the direction and a specific rectangle that the focus
4994 * is coming from. The rectangle can help give larger views a finer grained hint
4995 * about where focus is coming from, and therefore, where to show selection, or
4996 * forward focus change internally.
4997 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004998 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4999 * false), or if it is focusable and it is not focusable in touch mode
5000 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005001 *
5002 * A View will not take focus if it is not visible.
5003 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005004 * A View will not take focus if one of its parents has
5005 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5006 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005008 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005009 * have focus, and you want your parent to look for the next one.
5010 *
5011 * You may wish to override this method if your custom {@link View} has an internal
5012 * {@link View} that it wishes to forward the request to.
5013 *
5014 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5015 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5016 * to give a finer grained hint about where focus is coming from. May be null
5017 * if there is no hint.
5018 * @return Whether this view or one of its descendants actually took focus.
5019 */
5020 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5021 // need to be focusable
5022 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5023 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5024 return false;
5025 }
5026
5027 // need to be focusable in touch mode if in touch mode
5028 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005029 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5030 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 }
5032
5033 // need to not have any parents blocking us
5034 if (hasAncestorThatBlocksDescendantFocus()) {
5035 return false;
5036 }
5037
5038 handleFocusGainInternal(direction, previouslyFocusedRect);
5039 return true;
5040 }
5041
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005042 /** Gets the ViewAncestor, or null if not attached. */
5043 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005044 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005045 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005046 }
5047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005048 /**
5049 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5050 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5051 * touch mode to request focus when they are touched.
5052 *
5053 * @return Whether this view or one of its descendants actually took focus.
5054 *
5055 * @see #isInTouchMode()
5056 *
5057 */
5058 public final boolean requestFocusFromTouch() {
5059 // Leave touch mode if we need to
5060 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005061 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07005062 if (viewRoot != null) {
5063 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 }
5065 }
5066 return requestFocus(View.FOCUS_DOWN);
5067 }
5068
5069 /**
5070 * @return Whether any ancestor of this view blocks descendant focus.
5071 */
5072 private boolean hasAncestorThatBlocksDescendantFocus() {
5073 ViewParent ancestor = mParent;
5074 while (ancestor instanceof ViewGroup) {
5075 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5076 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5077 return true;
5078 } else {
5079 ancestor = vgAncestor.getParent();
5080 }
5081 }
5082 return false;
5083 }
5084
5085 /**
Romain Guya440b002010-02-24 15:57:54 -08005086 * @hide
5087 */
5088 public void dispatchStartTemporaryDetach() {
5089 onStartTemporaryDetach();
5090 }
5091
5092 /**
5093 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5095 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005096 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005097 */
5098 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005099 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005100 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005101 }
5102
5103 /**
5104 * @hide
5105 */
5106 public void dispatchFinishTemporaryDetach() {
5107 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005108 }
Romain Guy8506ab42009-06-11 17:35:47 -07005109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005110 /**
5111 * Called after {@link #onStartTemporaryDetach} when the container is done
5112 * changing the view.
5113 */
5114 public void onFinishTemporaryDetach() {
5115 }
Romain Guy8506ab42009-06-11 17:35:47 -07005116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005117 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005118 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5119 * for this view's window. Returns null if the view is not currently attached
5120 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005121 * just use the standard high-level event callbacks like
5122 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005123 */
5124 public KeyEvent.DispatcherState getKeyDispatcherState() {
5125 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5126 }
Joe Malin32736f02011-01-19 16:14:20 -08005127
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005128 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005129 * Dispatch a key event before it is processed by any input method
5130 * associated with the view hierarchy. This can be used to intercept
5131 * key events in special situations before the IME consumes them; a
5132 * typical example would be handling the BACK key to update the application's
5133 * UI instead of allowing the IME to see it and close itself.
5134 *
5135 * @param event The key event to be dispatched.
5136 * @return True if the event was handled, false otherwise.
5137 */
5138 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5139 return onKeyPreIme(event.getKeyCode(), event);
5140 }
5141
5142 /**
5143 * Dispatch a key event to the next view on the focus path. This path runs
5144 * from the top of the view tree down to the currently focused view. If this
5145 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5146 * the next node down the focus path. This method also fires any key
5147 * listeners.
5148 *
5149 * @param event The key event to be dispatched.
5150 * @return True if the event was handled, false otherwise.
5151 */
5152 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005153 if (mInputEventConsistencyVerifier != null) {
5154 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5155 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005156
Jeff Brown21bc5c92011-02-28 18:27:14 -08005157 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005158 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005159 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5160 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5161 return true;
5162 }
5163
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005164 if (event.dispatch(this, mAttachInfo != null
5165 ? mAttachInfo.mKeyDispatchState : null, this)) {
5166 return true;
5167 }
5168
5169 if (mInputEventConsistencyVerifier != null) {
5170 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5171 }
5172 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005173 }
5174
5175 /**
5176 * Dispatches a key shortcut event.
5177 *
5178 * @param event The key event to be dispatched.
5179 * @return True if the event was handled by the view, false otherwise.
5180 */
5181 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5182 return onKeyShortcut(event.getKeyCode(), event);
5183 }
5184
5185 /**
5186 * Pass the touch screen motion event down to the target view, or this
5187 * view if it is the target.
5188 *
5189 * @param event The motion event to be dispatched.
5190 * @return True if the event was handled by the view, false otherwise.
5191 */
5192 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005193 if (mInputEventConsistencyVerifier != null) {
5194 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5195 }
5196
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005197 if (onFilterTouchEventForSecurity(event)) {
5198 //noinspection SimplifiableIfStatement
5199 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5200 mOnTouchListener.onTouch(this, event)) {
5201 return true;
5202 }
5203
5204 if (onTouchEvent(event)) {
5205 return true;
5206 }
Jeff Brown85a31762010-09-01 17:01:00 -07005207 }
5208
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005209 if (mInputEventConsistencyVerifier != null) {
5210 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005211 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005212 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005213 }
5214
5215 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005216 * Filter the touch event to apply security policies.
5217 *
5218 * @param event The motion event to be filtered.
5219 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005220 *
Jeff Brown85a31762010-09-01 17:01:00 -07005221 * @see #getFilterTouchesWhenObscured
5222 */
5223 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005224 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005225 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5226 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5227 // Window is obscured, drop this touch.
5228 return false;
5229 }
5230 return true;
5231 }
5232
5233 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005234 * Pass a trackball motion event down to the focused view.
5235 *
5236 * @param event The motion event to be dispatched.
5237 * @return True if the event was handled by the view, false otherwise.
5238 */
5239 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005240 if (mInputEventConsistencyVerifier != null) {
5241 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5242 }
5243
Romain Guy02ccac62011-06-24 13:20:23 -07005244 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005245 }
5246
5247 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005248 * Dispatch a generic motion event.
5249 * <p>
5250 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5251 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005252 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005253 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005254 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005255 *
5256 * @param event The motion event to be dispatched.
5257 * @return True if the event was handled by the view, false otherwise.
5258 */
5259 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005260 if (mInputEventConsistencyVerifier != null) {
5261 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5262 }
5263
Jeff Browna032cc02011-03-07 16:56:21 -08005264 final int source = event.getSource();
5265 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5266 final int action = event.getAction();
5267 if (action == MotionEvent.ACTION_HOVER_ENTER
5268 || action == MotionEvent.ACTION_HOVER_MOVE
5269 || action == MotionEvent.ACTION_HOVER_EXIT) {
5270 if (dispatchHoverEvent(event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005271 // For compatibility with existing applications that handled HOVER_MOVE
5272 // events in onGenericMotionEvent, dispatch the event there. The
5273 // onHoverEvent method did not exist at the time.
5274 if (action == MotionEvent.ACTION_HOVER_MOVE) {
5275 dispatchGenericMotionEventInternal(event);
5276 }
Jeff Browna032cc02011-03-07 16:56:21 -08005277 return true;
5278 }
5279 } else if (dispatchGenericPointerEvent(event)) {
5280 return true;
5281 }
5282 } else if (dispatchGenericFocusedEvent(event)) {
5283 return true;
5284 }
5285
Jeff Brown10b62902011-06-20 16:40:37 -07005286 if (dispatchGenericMotionEventInternal(event)) {
5287 return true;
5288 }
5289
5290 if (mInputEventConsistencyVerifier != null) {
5291 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5292 }
5293 return false;
5294 }
5295
5296 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005297 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005298 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5299 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5300 return true;
5301 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005302
5303 if (onGenericMotionEvent(event)) {
5304 return true;
5305 }
5306
5307 if (mInputEventConsistencyVerifier != null) {
5308 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5309 }
5310 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005311 }
5312
5313 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005314 * Dispatch a hover event.
5315 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005316 * Do not call this method directly.
5317 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005318 * </p>
5319 *
5320 * @param event The motion event to be dispatched.
5321 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005322 */
5323 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005324 switch (event.getAction()) {
5325 case MotionEvent.ACTION_HOVER_ENTER:
5326 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
5327 mSendingHoverAccessibilityEvents = true;
5328 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
5329 }
5330 break;
5331 case MotionEvent.ACTION_HOVER_EXIT:
5332 if (mSendingHoverAccessibilityEvents) {
5333 mSendingHoverAccessibilityEvents = false;
5334 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
5335 }
5336 break;
5337 }
5338
Romain Guy02ccac62011-06-24 13:20:23 -07005339 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005340 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5341 && mOnHoverListener.onHover(this, event)) {
5342 return true;
5343 }
5344
Jeff Browna032cc02011-03-07 16:56:21 -08005345 return onHoverEvent(event);
5346 }
5347
5348 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005349 * Returns true if the view has a child to which it has recently sent
5350 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5351 * it does not have a hovered child, then it must be the innermost hovered view.
5352 * @hide
5353 */
5354 protected boolean hasHoveredChild() {
5355 return false;
5356 }
5357
5358 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005359 * Dispatch a generic motion event to the view under the first pointer.
5360 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005361 * Do not call this method directly.
5362 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005363 * </p>
5364 *
5365 * @param event The motion event to be dispatched.
5366 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005367 */
5368 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5369 return false;
5370 }
5371
5372 /**
5373 * Dispatch a generic motion event to the currently focused view.
5374 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005375 * Do not call this method directly.
5376 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005377 * </p>
5378 *
5379 * @param event The motion event to be dispatched.
5380 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005381 */
5382 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5383 return false;
5384 }
5385
5386 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005387 * Dispatch a pointer event.
5388 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005389 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5390 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5391 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005392 * and should not be expected to handle other pointing device features.
5393 * </p>
5394 *
5395 * @param event The motion event to be dispatched.
5396 * @return True if the event was handled by the view, false otherwise.
5397 * @hide
5398 */
5399 public final boolean dispatchPointerEvent(MotionEvent event) {
5400 if (event.isTouchEvent()) {
5401 return dispatchTouchEvent(event);
5402 } else {
5403 return dispatchGenericMotionEvent(event);
5404 }
5405 }
5406
5407 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005408 * Called when the window containing this view gains or loses window focus.
5409 * ViewGroups should override to route to their children.
5410 *
5411 * @param hasFocus True if the window containing this view now has focus,
5412 * false otherwise.
5413 */
5414 public void dispatchWindowFocusChanged(boolean hasFocus) {
5415 onWindowFocusChanged(hasFocus);
5416 }
5417
5418 /**
5419 * Called when the window containing this view gains or loses focus. Note
5420 * that this is separate from view focus: to receive key events, both
5421 * your view and its window must have focus. If a window is displayed
5422 * on top of yours that takes input focus, then your own window will lose
5423 * focus but the view focus will remain unchanged.
5424 *
5425 * @param hasWindowFocus True if the window containing this view now has
5426 * focus, false otherwise.
5427 */
5428 public void onWindowFocusChanged(boolean hasWindowFocus) {
5429 InputMethodManager imm = InputMethodManager.peekInstance();
5430 if (!hasWindowFocus) {
5431 if (isPressed()) {
5432 setPressed(false);
5433 }
5434 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5435 imm.focusOut(this);
5436 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005437 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005438 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005439 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005440 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5441 imm.focusIn(this);
5442 }
5443 refreshDrawableState();
5444 }
5445
5446 /**
5447 * Returns true if this view is in a window that currently has window focus.
5448 * Note that this is not the same as the view itself having focus.
5449 *
5450 * @return True if this view is in a window that currently has window focus.
5451 */
5452 public boolean hasWindowFocus() {
5453 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5454 }
5455
5456 /**
Adam Powell326d8082009-12-09 15:10:07 -08005457 * Dispatch a view visibility change down the view hierarchy.
5458 * ViewGroups should override to route to their children.
5459 * @param changedView The view whose visibility changed. Could be 'this' or
5460 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005461 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5462 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005463 */
5464 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5465 onVisibilityChanged(changedView, visibility);
5466 }
5467
5468 /**
5469 * Called when the visibility of the view or an ancestor of the view is changed.
5470 * @param changedView The view whose visibility changed. Could be 'this' or
5471 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005472 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5473 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005474 */
5475 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005476 if (visibility == VISIBLE) {
5477 if (mAttachInfo != null) {
5478 initialAwakenScrollBars();
5479 } else {
5480 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5481 }
5482 }
Adam Powell326d8082009-12-09 15:10:07 -08005483 }
5484
5485 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005486 * Dispatch a hint about whether this view is displayed. For instance, when
5487 * a View moves out of the screen, it might receives a display hint indicating
5488 * the view is not displayed. Applications should not <em>rely</em> on this hint
5489 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005490 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005491 * @param hint A hint about whether or not this view is displayed:
5492 * {@link #VISIBLE} or {@link #INVISIBLE}.
5493 */
5494 public void dispatchDisplayHint(int hint) {
5495 onDisplayHint(hint);
5496 }
5497
5498 /**
5499 * Gives this view a hint about whether is displayed or not. For instance, when
5500 * a View moves out of the screen, it might receives a display hint indicating
5501 * the view is not displayed. Applications should not <em>rely</em> on this hint
5502 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005503 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005504 * @param hint A hint about whether or not this view is displayed:
5505 * {@link #VISIBLE} or {@link #INVISIBLE}.
5506 */
5507 protected void onDisplayHint(int hint) {
5508 }
5509
5510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005511 * Dispatch a window visibility change down the view hierarchy.
5512 * ViewGroups should override to route to their children.
5513 *
5514 * @param visibility The new visibility of the window.
5515 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005516 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 */
5518 public void dispatchWindowVisibilityChanged(int visibility) {
5519 onWindowVisibilityChanged(visibility);
5520 }
5521
5522 /**
5523 * Called when the window containing has change its visibility
5524 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5525 * that this tells you whether or not your window is being made visible
5526 * to the window manager; this does <em>not</em> tell you whether or not
5527 * your window is obscured by other windows on the screen, even if it
5528 * is itself visible.
5529 *
5530 * @param visibility The new visibility of the window.
5531 */
5532 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005533 if (visibility == VISIBLE) {
5534 initialAwakenScrollBars();
5535 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005536 }
5537
5538 /**
5539 * Returns the current visibility of the window this view is attached to
5540 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5541 *
5542 * @return Returns the current visibility of the view's window.
5543 */
5544 public int getWindowVisibility() {
5545 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5546 }
5547
5548 /**
5549 * Retrieve the overall visible display size in which the window this view is
5550 * attached to has been positioned in. This takes into account screen
5551 * decorations above the window, for both cases where the window itself
5552 * is being position inside of them or the window is being placed under
5553 * then and covered insets are used for the window to position its content
5554 * inside. In effect, this tells you the available area where content can
5555 * be placed and remain visible to users.
5556 *
5557 * <p>This function requires an IPC back to the window manager to retrieve
5558 * the requested information, so should not be used in performance critical
5559 * code like drawing.
5560 *
5561 * @param outRect Filled in with the visible display frame. If the view
5562 * is not attached to a window, this is simply the raw display size.
5563 */
5564 public void getWindowVisibleDisplayFrame(Rect outRect) {
5565 if (mAttachInfo != null) {
5566 try {
5567 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5568 } catch (RemoteException e) {
5569 return;
5570 }
5571 // XXX This is really broken, and probably all needs to be done
5572 // in the window manager, and we need to know more about whether
5573 // we want the area behind or in front of the IME.
5574 final Rect insets = mAttachInfo.mVisibleInsets;
5575 outRect.left += insets.left;
5576 outRect.top += insets.top;
5577 outRect.right -= insets.right;
5578 outRect.bottom -= insets.bottom;
5579 return;
5580 }
5581 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005582 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005583 }
5584
5585 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005586 * Dispatch a notification about a resource configuration change down
5587 * the view hierarchy.
5588 * ViewGroups should override to route to their children.
5589 *
5590 * @param newConfig The new resource configuration.
5591 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005592 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005593 */
5594 public void dispatchConfigurationChanged(Configuration newConfig) {
5595 onConfigurationChanged(newConfig);
5596 }
5597
5598 /**
5599 * Called when the current configuration of the resources being used
5600 * by the application have changed. You can use this to decide when
5601 * to reload resources that can changed based on orientation and other
5602 * configuration characterstics. You only need to use this if you are
5603 * not relying on the normal {@link android.app.Activity} mechanism of
5604 * recreating the activity instance upon a configuration change.
5605 *
5606 * @param newConfig The new resource configuration.
5607 */
5608 protected void onConfigurationChanged(Configuration newConfig) {
5609 }
5610
5611 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005612 * Private function to aggregate all per-view attributes in to the view
5613 * root.
5614 */
5615 void dispatchCollectViewAttributes(int visibility) {
5616 performCollectViewAttributes(visibility);
5617 }
5618
5619 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005620 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005621 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5622 mAttachInfo.mKeepScreenOn = true;
5623 }
5624 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5625 if (mOnSystemUiVisibilityChangeListener != null) {
5626 mAttachInfo.mHasSystemUiListeners = true;
5627 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005628 }
5629 }
5630
5631 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005632 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005633 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005634 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5635 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005636 ai.mRecomputeGlobalAttributes = true;
5637 }
5638 }
5639 }
5640
5641 /**
5642 * Returns whether the device is currently in touch mode. Touch mode is entered
5643 * once the user begins interacting with the device by touch, and affects various
5644 * things like whether focus is always visible to the user.
5645 *
5646 * @return Whether the device is in touch mode.
5647 */
5648 @ViewDebug.ExportedProperty
5649 public boolean isInTouchMode() {
5650 if (mAttachInfo != null) {
5651 return mAttachInfo.mInTouchMode;
5652 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005653 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005654 }
5655 }
5656
5657 /**
5658 * Returns the context the view is running in, through which it can
5659 * access the current theme, resources, etc.
5660 *
5661 * @return The view's Context.
5662 */
5663 @ViewDebug.CapturedViewProperty
5664 public final Context getContext() {
5665 return mContext;
5666 }
5667
5668 /**
5669 * Handle a key event before it is processed by any input method
5670 * associated with the view hierarchy. This can be used to intercept
5671 * key events in special situations before the IME consumes them; a
5672 * typical example would be handling the BACK key to update the application's
5673 * UI instead of allowing the IME to see it and close itself.
5674 *
5675 * @param keyCode The value in event.getKeyCode().
5676 * @param event Description of the key event.
5677 * @return If you handled the event, return true. If you want to allow the
5678 * event to be handled by the next receiver, return false.
5679 */
5680 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5681 return false;
5682 }
5683
5684 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005685 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5686 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005687 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5688 * is released, if the view is enabled and clickable.
5689 *
5690 * @param keyCode A key code that represents the button pressed, from
5691 * {@link android.view.KeyEvent}.
5692 * @param event The KeyEvent object that defines the button action.
5693 */
5694 public boolean onKeyDown(int keyCode, KeyEvent event) {
5695 boolean result = false;
5696
5697 switch (keyCode) {
5698 case KeyEvent.KEYCODE_DPAD_CENTER:
5699 case KeyEvent.KEYCODE_ENTER: {
5700 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5701 return true;
5702 }
5703 // Long clickable items don't necessarily have to be clickable
5704 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5705 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5706 (event.getRepeatCount() == 0)) {
5707 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005708 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005709 return true;
5710 }
5711 break;
5712 }
5713 }
5714 return result;
5715 }
5716
5717 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005718 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5719 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5720 * the event).
5721 */
5722 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5723 return false;
5724 }
5725
5726 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005727 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5728 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005729 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5730 * {@link KeyEvent#KEYCODE_ENTER} is released.
5731 *
5732 * @param keyCode A key code that represents the button pressed, from
5733 * {@link android.view.KeyEvent}.
5734 * @param event The KeyEvent object that defines the button action.
5735 */
5736 public boolean onKeyUp(int keyCode, KeyEvent event) {
5737 boolean result = false;
5738
5739 switch (keyCode) {
5740 case KeyEvent.KEYCODE_DPAD_CENTER:
5741 case KeyEvent.KEYCODE_ENTER: {
5742 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5743 return true;
5744 }
5745 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5746 setPressed(false);
5747
5748 if (!mHasPerformedLongPress) {
5749 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005750 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751
5752 result = performClick();
5753 }
5754 }
5755 break;
5756 }
5757 }
5758 return result;
5759 }
5760
5761 /**
5762 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5763 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5764 * the event).
5765 *
5766 * @param keyCode A key code that represents the button pressed, from
5767 * {@link android.view.KeyEvent}.
5768 * @param repeatCount The number of times the action was made.
5769 * @param event The KeyEvent object that defines the button action.
5770 */
5771 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5772 return false;
5773 }
5774
5775 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005776 * Called on the focused view when a key shortcut event is not handled.
5777 * Override this method to implement local key shortcuts for the View.
5778 * Key shortcuts can also be implemented by setting the
5779 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005780 *
5781 * @param keyCode The value in event.getKeyCode().
5782 * @param event Description of the key event.
5783 * @return If you handled the event, return true. If you want to allow the
5784 * event to be handled by the next receiver, return false.
5785 */
5786 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5787 return false;
5788 }
5789
5790 /**
5791 * Check whether the called view is a text editor, in which case it
5792 * would make sense to automatically display a soft input window for
5793 * it. Subclasses should override this if they implement
5794 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005795 * a call on that method would return a non-null InputConnection, and
5796 * they are really a first-class editor that the user would normally
5797 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005798 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005799 * <p>The default implementation always returns false. This does
5800 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5801 * will not be called or the user can not otherwise perform edits on your
5802 * view; it is just a hint to the system that this is not the primary
5803 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005805 * @return Returns true if this view is a text editor, else false.
5806 */
5807 public boolean onCheckIsTextEditor() {
5808 return false;
5809 }
Romain Guy8506ab42009-06-11 17:35:47 -07005810
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 /**
5812 * Create a new InputConnection for an InputMethod to interact
5813 * with the view. The default implementation returns null, since it doesn't
5814 * support input methods. You can override this to implement such support.
5815 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 * <p>When implementing this, you probably also want to implement
5818 * {@link #onCheckIsTextEditor()} to indicate you will return a
5819 * non-null InputConnection.
5820 *
5821 * @param outAttrs Fill in with attribute information about the connection.
5822 */
5823 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5824 return null;
5825 }
5826
5827 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005828 * Called by the {@link android.view.inputmethod.InputMethodManager}
5829 * when a view who is not the current
5830 * input connection target is trying to make a call on the manager. The
5831 * default implementation returns false; you can override this to return
5832 * true for certain views if you are performing InputConnection proxying
5833 * to them.
5834 * @param view The View that is making the InputMethodManager call.
5835 * @return Return true to allow the call, false to reject.
5836 */
5837 public boolean checkInputConnectionProxy(View view) {
5838 return false;
5839 }
Romain Guy8506ab42009-06-11 17:35:47 -07005840
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005842 * Show the context menu for this view. It is not safe to hold on to the
5843 * menu after returning from this method.
5844 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005845 * You should normally not overload this method. Overload
5846 * {@link #onCreateContextMenu(ContextMenu)} or define an
5847 * {@link OnCreateContextMenuListener} to add items to the context menu.
5848 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005849 * @param menu The context menu to populate
5850 */
5851 public void createContextMenu(ContextMenu menu) {
5852 ContextMenuInfo menuInfo = getContextMenuInfo();
5853
5854 // Sets the current menu info so all items added to menu will have
5855 // my extra info set.
5856 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5857
5858 onCreateContextMenu(menu);
5859 if (mOnCreateContextMenuListener != null) {
5860 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5861 }
5862
5863 // Clear the extra information so subsequent items that aren't mine don't
5864 // have my extra info.
5865 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5866
5867 if (mParent != null) {
5868 mParent.createContextMenu(menu);
5869 }
5870 }
5871
5872 /**
5873 * Views should implement this if they have extra information to associate
5874 * with the context menu. The return result is supplied as a parameter to
5875 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5876 * callback.
5877 *
5878 * @return Extra information about the item for which the context menu
5879 * should be shown. This information will vary across different
5880 * subclasses of View.
5881 */
5882 protected ContextMenuInfo getContextMenuInfo() {
5883 return null;
5884 }
5885
5886 /**
5887 * Views should implement this if the view itself is going to add items to
5888 * the context menu.
5889 *
5890 * @param menu the context menu to populate
5891 */
5892 protected void onCreateContextMenu(ContextMenu menu) {
5893 }
5894
5895 /**
5896 * Implement this method to handle trackball motion events. The
5897 * <em>relative</em> movement of the trackball since the last event
5898 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5899 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5900 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5901 * they will often be fractional values, representing the more fine-grained
5902 * movement information available from a trackball).
5903 *
5904 * @param event The motion event.
5905 * @return True if the event was handled, false otherwise.
5906 */
5907 public boolean onTrackballEvent(MotionEvent event) {
5908 return false;
5909 }
5910
5911 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005912 * Implement this method to handle generic motion events.
5913 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005914 * Generic motion events describe joystick movements, mouse hovers, track pad
5915 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005916 * {@link MotionEvent#getSource() source} of the motion event specifies
5917 * the class of input that was received. Implementations of this method
5918 * must examine the bits in the source before processing the event.
5919 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005920 * </p><p>
5921 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5922 * are delivered to the view under the pointer. All other generic motion events are
5923 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005924 * </p>
5925 * <code>
5926 * public boolean onGenericMotionEvent(MotionEvent event) {
5927 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005928 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5929 * // process the joystick movement...
5930 * return true;
5931 * }
5932 * }
5933 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5934 * switch (event.getAction()) {
5935 * case MotionEvent.ACTION_HOVER_MOVE:
5936 * // process the mouse hover movement...
5937 * return true;
5938 * case MotionEvent.ACTION_SCROLL:
5939 * // process the scroll wheel movement...
5940 * return true;
5941 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005942 * }
5943 * return super.onGenericMotionEvent(event);
5944 * }
5945 * </code>
5946 *
5947 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005948 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005949 */
5950 public boolean onGenericMotionEvent(MotionEvent event) {
5951 return false;
5952 }
5953
5954 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005955 * Implement this method to handle hover events.
5956 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005957 * This method is called whenever a pointer is hovering into, over, or out of the
5958 * bounds of a view and the view is not currently being touched.
5959 * Hover events are represented as pointer events with action
5960 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5961 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5962 * </p>
5963 * <ul>
5964 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5965 * when the pointer enters the bounds of the view.</li>
5966 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5967 * when the pointer has already entered the bounds of the view and has moved.</li>
5968 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5969 * when the pointer has exited the bounds of the view or when the pointer is
5970 * about to go down due to a button click, tap, or similar user action that
5971 * causes the view to be touched.</li>
5972 * </ul>
5973 * <p>
5974 * The view should implement this method to return true to indicate that it is
5975 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005976 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005977 * The default implementation calls {@link #setHovered} to update the hovered state
5978 * of the view when a hover enter or hover exit event is received, if the view
5979 * is enabled and is clickable.
Jeff Browna032cc02011-03-07 16:56:21 -08005980 * </p>
5981 *
5982 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005983 * @return True if the view handled the hover event.
5984 *
5985 * @see #isHovered
5986 * @see #setHovered
5987 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005988 */
5989 public boolean onHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005990 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07005991 switch (event.getAction()) {
5992 case MotionEvent.ACTION_HOVER_ENTER:
5993 setHovered(true);
5994 break;
5995 case MotionEvent.ACTION_HOVER_EXIT:
5996 setHovered(false);
5997 break;
5998 }
5999 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006000 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006001 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006002 }
6003
6004 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006005 * Returns true if the view should handle {@link #onHoverEvent}
6006 * by calling {@link #setHovered} to change its hovered state.
6007 *
6008 * @return True if the view is hoverable.
6009 */
6010 private boolean isHoverable() {
6011 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006012 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006013 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6014 return false;
6015 }
6016
6017 return (viewFlags & CLICKABLE) == CLICKABLE
6018 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6019 }
6020
6021 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006022 * Returns true if the view is currently hovered.
6023 *
6024 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006025 *
6026 * @see #setHovered
6027 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006028 */
Jeff Brown10b62902011-06-20 16:40:37 -07006029 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006030 public boolean isHovered() {
6031 return (mPrivateFlags & HOVERED) != 0;
6032 }
6033
6034 /**
6035 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006036 * <p>
6037 * Calling this method also changes the drawable state of the view. This
6038 * enables the view to react to hover by using different drawable resources
6039 * to change its appearance.
6040 * </p><p>
6041 * The {@link #onHoverChanged} method is called when the hovered state changes.
6042 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006043 *
6044 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006045 *
6046 * @see #isHovered
6047 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006048 */
6049 public void setHovered(boolean hovered) {
6050 if (hovered) {
6051 if ((mPrivateFlags & HOVERED) == 0) {
6052 mPrivateFlags |= HOVERED;
6053 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006054 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006055 }
6056 } else {
6057 if ((mPrivateFlags & HOVERED) != 0) {
6058 mPrivateFlags &= ~HOVERED;
6059 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006060 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006061 }
6062 }
6063 }
6064
6065 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006066 * Implement this method to handle hover state changes.
6067 * <p>
6068 * This method is called whenever the hover state changes as a result of a
6069 * call to {@link #setHovered}.
6070 * </p>
6071 *
6072 * @param hovered The current hover state, as returned by {@link #isHovered}.
6073 *
6074 * @see #isHovered
6075 * @see #setHovered
6076 */
6077 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006078 }
6079
6080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006081 * Implement this method to handle touch screen motion events.
6082 *
6083 * @param event The motion event.
6084 * @return True if the event was handled, false otherwise.
6085 */
6086 public boolean onTouchEvent(MotionEvent event) {
6087 final int viewFlags = mViewFlags;
6088
6089 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006090 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6091 mPrivateFlags &= ~PRESSED;
6092 refreshDrawableState();
6093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006094 // A disabled view that is clickable still consumes the touch
6095 // events, it just doesn't respond to them.
6096 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6097 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6098 }
6099
6100 if (mTouchDelegate != null) {
6101 if (mTouchDelegate.onTouchEvent(event)) {
6102 return true;
6103 }
6104 }
6105
6106 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6107 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6108 switch (event.getAction()) {
6109 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006110 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6111 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006112 // take focus if we don't have it already and we should in
6113 // touch mode.
6114 boolean focusTaken = false;
6115 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6116 focusTaken = requestFocus();
6117 }
6118
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006119 if (prepressed) {
6120 // The button is being released before we actually
6121 // showed it as pressed. Make it show the pressed
6122 // state now (before scheduling the click) to ensure
6123 // the user sees it.
6124 mPrivateFlags |= PRESSED;
6125 refreshDrawableState();
6126 }
Joe Malin32736f02011-01-19 16:14:20 -08006127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006128 if (!mHasPerformedLongPress) {
6129 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006130 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006131
6132 // Only perform take click actions if we were in the pressed state
6133 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006134 // Use a Runnable and post this rather than calling
6135 // performClick directly. This lets other visual state
6136 // of the view update before click actions start.
6137 if (mPerformClick == null) {
6138 mPerformClick = new PerformClick();
6139 }
6140 if (!post(mPerformClick)) {
6141 performClick();
6142 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006143 }
6144 }
6145
6146 if (mUnsetPressedState == null) {
6147 mUnsetPressedState = new UnsetPressedState();
6148 }
6149
Adam Powelle14579b2009-12-16 18:39:52 -08006150 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006151 postDelayed(mUnsetPressedState,
6152 ViewConfiguration.getPressedStateDuration());
6153 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006154 // If the post failed, unpress right now
6155 mUnsetPressedState.run();
6156 }
Adam Powelle14579b2009-12-16 18:39:52 -08006157 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006158 }
6159 break;
6160
6161 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006162 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006163
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006164 if (performButtonActionOnTouchDown(event)) {
6165 break;
6166 }
6167
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006168 // Walk up the hierarchy to determine if we're inside a scrolling container.
6169 boolean isInScrollingContainer = false;
6170 ViewParent p = getParent();
6171 while (p != null && p instanceof ViewGroup) {
6172 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6173 isInScrollingContainer = true;
6174 break;
6175 }
6176 p = p.getParent();
6177 }
6178
6179 // For views inside a scrolling container, delay the pressed feedback for
6180 // a short period in case this is a scroll.
6181 if (isInScrollingContainer) {
6182 mPrivateFlags |= PREPRESSED;
6183 if (mPendingCheckForTap == null) {
6184 mPendingCheckForTap = new CheckForTap();
6185 }
6186 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6187 } else {
6188 // Not inside a scrolling container, so show the feedback right away
6189 mPrivateFlags |= PRESSED;
6190 refreshDrawableState();
6191 checkForLongClick(0);
6192 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006193 break;
6194
6195 case MotionEvent.ACTION_CANCEL:
6196 mPrivateFlags &= ~PRESSED;
6197 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006198 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 break;
6200
6201 case MotionEvent.ACTION_MOVE:
6202 final int x = (int) event.getX();
6203 final int y = (int) event.getY();
6204
6205 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006206 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006207 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006208 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006209 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006210 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006211 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006212
6213 // Need to switch from pressed to not pressed
6214 mPrivateFlags &= ~PRESSED;
6215 refreshDrawableState();
6216 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 }
6218 break;
6219 }
6220 return true;
6221 }
6222
6223 return false;
6224 }
6225
6226 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006227 * Remove the longpress detection timer.
6228 */
6229 private void removeLongPressCallback() {
6230 if (mPendingCheckForLongPress != null) {
6231 removeCallbacks(mPendingCheckForLongPress);
6232 }
6233 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006234
6235 /**
6236 * Remove the pending click action
6237 */
6238 private void removePerformClickCallback() {
6239 if (mPerformClick != null) {
6240 removeCallbacks(mPerformClick);
6241 }
6242 }
6243
Adam Powelle14579b2009-12-16 18:39:52 -08006244 /**
Romain Guya440b002010-02-24 15:57:54 -08006245 * Remove the prepress detection timer.
6246 */
6247 private void removeUnsetPressCallback() {
6248 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6249 setPressed(false);
6250 removeCallbacks(mUnsetPressedState);
6251 }
6252 }
6253
6254 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006255 * Remove the tap detection timer.
6256 */
6257 private void removeTapCallback() {
6258 if (mPendingCheckForTap != null) {
6259 mPrivateFlags &= ~PREPRESSED;
6260 removeCallbacks(mPendingCheckForTap);
6261 }
6262 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006263
6264 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006265 * Cancels a pending long press. Your subclass can use this if you
6266 * want the context menu to come up if the user presses and holds
6267 * at the same place, but you don't want it to come up if they press
6268 * and then move around enough to cause scrolling.
6269 */
6270 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006271 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006272
6273 /*
6274 * The prepressed state handled by the tap callback is a display
6275 * construct, but the tap callback will post a long press callback
6276 * less its own timeout. Remove it here.
6277 */
6278 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006279 }
6280
6281 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006282 * Remove the pending callback for sending a
6283 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6284 */
6285 private void removeSendViewScrolledAccessibilityEventCallback() {
6286 if (mSendViewScrolledAccessibilityEvent != null) {
6287 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6288 }
6289 }
6290
6291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 * Sets the TouchDelegate for this View.
6293 */
6294 public void setTouchDelegate(TouchDelegate delegate) {
6295 mTouchDelegate = delegate;
6296 }
6297
6298 /**
6299 * Gets the TouchDelegate for this View.
6300 */
6301 public TouchDelegate getTouchDelegate() {
6302 return mTouchDelegate;
6303 }
6304
6305 /**
6306 * Set flags controlling behavior of this view.
6307 *
6308 * @param flags Constant indicating the value which should be set
6309 * @param mask Constant indicating the bit range that should be changed
6310 */
6311 void setFlags(int flags, int mask) {
6312 int old = mViewFlags;
6313 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6314
6315 int changed = mViewFlags ^ old;
6316 if (changed == 0) {
6317 return;
6318 }
6319 int privateFlags = mPrivateFlags;
6320
6321 /* Check if the FOCUSABLE bit has changed */
6322 if (((changed & FOCUSABLE_MASK) != 0) &&
6323 ((privateFlags & HAS_BOUNDS) !=0)) {
6324 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6325 && ((privateFlags & FOCUSED) != 0)) {
6326 /* Give up focus if we are no longer focusable */
6327 clearFocus();
6328 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6329 && ((privateFlags & FOCUSED) == 0)) {
6330 /*
6331 * Tell the view system that we are now available to take focus
6332 * if no one else already has it.
6333 */
6334 if (mParent != null) mParent.focusableViewAvailable(this);
6335 }
6336 }
6337
6338 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6339 if ((changed & VISIBILITY_MASK) != 0) {
6340 /*
6341 * If this view is becoming visible, set the DRAWN flag so that
6342 * the next invalidate() will not be skipped.
6343 */
6344 mPrivateFlags |= DRAWN;
6345
6346 needGlobalAttributesUpdate(true);
6347
6348 // a view becoming visible is worth notifying the parent
6349 // about in case nothing has focus. even if this specific view
6350 // isn't focusable, it may contain something that is, so let
6351 // the root view try to give this focus if nothing else does.
6352 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6353 mParent.focusableViewAvailable(this);
6354 }
6355 }
6356 }
6357
6358 /* Check if the GONE bit has changed */
6359 if ((changed & GONE) != 0) {
6360 needGlobalAttributesUpdate(false);
6361 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006362 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006363
Romain Guyecd80ee2009-12-03 17:13:02 -08006364 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6365 if (hasFocus()) clearFocus();
6366 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006367 }
6368 if (mAttachInfo != null) {
6369 mAttachInfo.mViewVisibilityChanged = true;
6370 }
6371 }
6372
6373 /* Check if the VISIBLE bit has changed */
6374 if ((changed & INVISIBLE) != 0) {
6375 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006376 /*
6377 * If this view is becoming invisible, set the DRAWN flag so that
6378 * the next invalidate() will not be skipped.
6379 */
6380 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381
6382 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6383 // root view becoming invisible shouldn't clear focus
6384 if (getRootView() != this) {
6385 clearFocus();
6386 }
6387 }
6388 if (mAttachInfo != null) {
6389 mAttachInfo.mViewVisibilityChanged = true;
6390 }
6391 }
6392
Adam Powell326d8082009-12-09 15:10:07 -08006393 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006394 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006395 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6396 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006397 }
Adam Powell326d8082009-12-09 15:10:07 -08006398 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6399 }
6400
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006401 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6402 destroyDrawingCache();
6403 }
6404
6405 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6406 destroyDrawingCache();
6407 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006408 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006409 }
6410
6411 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6412 destroyDrawingCache();
6413 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6414 }
6415
6416 if ((changed & DRAW_MASK) != 0) {
6417 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6418 if (mBGDrawable != null) {
6419 mPrivateFlags &= ~SKIP_DRAW;
6420 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6421 } else {
6422 mPrivateFlags |= SKIP_DRAW;
6423 }
6424 } else {
6425 mPrivateFlags &= ~SKIP_DRAW;
6426 }
6427 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006428 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006429 }
6430
6431 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006432 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 mParent.recomputeViewAttributes(this);
6434 }
6435 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006436
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006437 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006438 requestLayout();
6439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006440 }
6441
6442 /**
6443 * Change the view's z order in the tree, so it's on top of other sibling
6444 * views
6445 */
6446 public void bringToFront() {
6447 if (mParent != null) {
6448 mParent.bringChildToFront(this);
6449 }
6450 }
6451
6452 /**
6453 * This is called in response to an internal scroll in this view (i.e., the
6454 * view scrolled its own contents). This is typically as a result of
6455 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6456 * called.
6457 *
6458 * @param l Current horizontal scroll origin.
6459 * @param t Current vertical scroll origin.
6460 * @param oldl Previous horizontal scroll origin.
6461 * @param oldt Previous vertical scroll origin.
6462 */
6463 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006464 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6465 postSendViewScrolledAccessibilityEventCallback();
6466 }
6467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006468 mBackgroundSizeChanged = true;
6469
6470 final AttachInfo ai = mAttachInfo;
6471 if (ai != null) {
6472 ai.mViewScrollChanged = true;
6473 }
6474 }
6475
6476 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006477 * Interface definition for a callback to be invoked when the layout bounds of a view
6478 * changes due to layout processing.
6479 */
6480 public interface OnLayoutChangeListener {
6481 /**
6482 * Called when the focus state of a view has changed.
6483 *
6484 * @param v The view whose state has changed.
6485 * @param left The new value of the view's left property.
6486 * @param top The new value of the view's top property.
6487 * @param right The new value of the view's right property.
6488 * @param bottom The new value of the view's bottom property.
6489 * @param oldLeft The previous value of the view's left property.
6490 * @param oldTop The previous value of the view's top property.
6491 * @param oldRight The previous value of the view's right property.
6492 * @param oldBottom The previous value of the view's bottom property.
6493 */
6494 void onLayoutChange(View v, int left, int top, int right, int bottom,
6495 int oldLeft, int oldTop, int oldRight, int oldBottom);
6496 }
6497
6498 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006499 * This is called during layout when the size of this view has changed. If
6500 * you were just added to the view hierarchy, you're called with the old
6501 * values of 0.
6502 *
6503 * @param w Current width of this view.
6504 * @param h Current height of this view.
6505 * @param oldw Old width of this view.
6506 * @param oldh Old height of this view.
6507 */
6508 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6509 }
6510
6511 /**
6512 * Called by draw to draw the child views. This may be overridden
6513 * by derived classes to gain control just before its children are drawn
6514 * (but after its own view has been drawn).
6515 * @param canvas the canvas on which to draw the view
6516 */
6517 protected void dispatchDraw(Canvas canvas) {
6518 }
6519
6520 /**
6521 * Gets the parent of this view. Note that the parent is a
6522 * ViewParent and not necessarily a View.
6523 *
6524 * @return Parent of this view.
6525 */
6526 public final ViewParent getParent() {
6527 return mParent;
6528 }
6529
6530 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006531 * Set the horizontal scrolled position of your view. This will cause a call to
6532 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6533 * invalidated.
6534 * @param value the x position to scroll to
6535 */
6536 public void setScrollX(int value) {
6537 scrollTo(value, mScrollY);
6538 }
6539
6540 /**
6541 * Set the vertical scrolled position of your view. This will cause a call to
6542 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6543 * invalidated.
6544 * @param value the y position to scroll to
6545 */
6546 public void setScrollY(int value) {
6547 scrollTo(mScrollX, value);
6548 }
6549
6550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006551 * Return the scrolled left position of this view. This is the left edge of
6552 * the displayed part of your view. You do not need to draw any pixels
6553 * farther left, since those are outside of the frame of your view on
6554 * screen.
6555 *
6556 * @return The left edge of the displayed part of your view, in pixels.
6557 */
6558 public final int getScrollX() {
6559 return mScrollX;
6560 }
6561
6562 /**
6563 * Return the scrolled top position of this view. This is the top edge of
6564 * the displayed part of your view. You do not need to draw any pixels above
6565 * it, since those are outside of the frame of your view on screen.
6566 *
6567 * @return The top edge of the displayed part of your view, in pixels.
6568 */
6569 public final int getScrollY() {
6570 return mScrollY;
6571 }
6572
6573 /**
6574 * Return the width of the your view.
6575 *
6576 * @return The width of your view, in pixels.
6577 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006578 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006579 public final int getWidth() {
6580 return mRight - mLeft;
6581 }
6582
6583 /**
6584 * Return the height of your view.
6585 *
6586 * @return The height of your view, in pixels.
6587 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006588 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006589 public final int getHeight() {
6590 return mBottom - mTop;
6591 }
6592
6593 /**
6594 * Return the visible drawing bounds of your view. Fills in the output
6595 * rectangle with the values from getScrollX(), getScrollY(),
6596 * getWidth(), and getHeight().
6597 *
6598 * @param outRect The (scrolled) drawing bounds of the view.
6599 */
6600 public void getDrawingRect(Rect outRect) {
6601 outRect.left = mScrollX;
6602 outRect.top = mScrollY;
6603 outRect.right = mScrollX + (mRight - mLeft);
6604 outRect.bottom = mScrollY + (mBottom - mTop);
6605 }
6606
6607 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006608 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6609 * raw width component (that is the result is masked by
6610 * {@link #MEASURED_SIZE_MASK}).
6611 *
6612 * @return The raw measured width of this view.
6613 */
6614 public final int getMeasuredWidth() {
6615 return mMeasuredWidth & MEASURED_SIZE_MASK;
6616 }
6617
6618 /**
6619 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006620 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006621 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006622 * This should be used during measurement and layout calculations only. Use
6623 * {@link #getWidth()} to see how wide a view is after layout.
6624 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006625 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006626 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006627 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 return mMeasuredWidth;
6629 }
6630
6631 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006632 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6633 * raw width component (that is the result is masked by
6634 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006635 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006636 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006637 */
6638 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006639 return mMeasuredHeight & MEASURED_SIZE_MASK;
6640 }
6641
6642 /**
6643 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006644 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006645 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6646 * This should be used during measurement and layout calculations only. Use
6647 * {@link #getHeight()} to see how wide a view is after layout.
6648 *
6649 * @return The measured width of this view as a bit mask.
6650 */
6651 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006652 return mMeasuredHeight;
6653 }
6654
6655 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006656 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6657 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6658 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6659 * and the height component is at the shifted bits
6660 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6661 */
6662 public final int getMeasuredState() {
6663 return (mMeasuredWidth&MEASURED_STATE_MASK)
6664 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6665 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6666 }
6667
6668 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006669 * The transform matrix of this view, which is calculated based on the current
6670 * roation, scale, and pivot properties.
6671 *
6672 * @see #getRotation()
6673 * @see #getScaleX()
6674 * @see #getScaleY()
6675 * @see #getPivotX()
6676 * @see #getPivotY()
6677 * @return The current transform matrix for the view
6678 */
6679 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006680 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006681 return mMatrix;
6682 }
6683
6684 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006685 * Utility function to determine if the value is far enough away from zero to be
6686 * considered non-zero.
6687 * @param value A floating point value to check for zero-ness
6688 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6689 */
6690 private static boolean nonzero(float value) {
6691 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6692 }
6693
6694 /**
Jeff Brown86671742010-09-30 20:00:15 -07006695 * Returns true if the transform matrix is the identity matrix.
6696 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006697 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006698 * @return True if the transform matrix is the identity matrix, false otherwise.
6699 */
Jeff Brown86671742010-09-30 20:00:15 -07006700 final boolean hasIdentityMatrix() {
6701 updateMatrix();
6702 return mMatrixIsIdentity;
6703 }
6704
6705 /**
6706 * Recomputes the transform matrix if necessary.
6707 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006708 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006709 if (mMatrixDirty) {
6710 // transform-related properties have changed since the last time someone
6711 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006712
6713 // Figure out if we need to update the pivot point
6714 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006715 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006716 mPrevWidth = mRight - mLeft;
6717 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006718 mPivotX = mPrevWidth / 2f;
6719 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006720 }
6721 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006722 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006723 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6724 mMatrix.setTranslate(mTranslationX, mTranslationY);
6725 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6726 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6727 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006728 if (mCamera == null) {
6729 mCamera = new Camera();
6730 matrix3D = new Matrix();
6731 }
6732 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006733 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006734 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006735 mCamera.getMatrix(matrix3D);
6736 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006737 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006738 mMatrix.postConcat(matrix3D);
6739 mCamera.restore();
6740 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006741 mMatrixDirty = false;
6742 mMatrixIsIdentity = mMatrix.isIdentity();
6743 mInverseMatrixDirty = true;
6744 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006745 }
6746
6747 /**
6748 * Utility method to retrieve the inverse of the current mMatrix property.
6749 * We cache the matrix to avoid recalculating it when transform properties
6750 * have not changed.
6751 *
6752 * @return The inverse of the current matrix of this view.
6753 */
Jeff Brown86671742010-09-30 20:00:15 -07006754 final Matrix getInverseMatrix() {
6755 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006756 if (mInverseMatrixDirty) {
6757 if (mInverseMatrix == null) {
6758 mInverseMatrix = new Matrix();
6759 }
6760 mMatrix.invert(mInverseMatrix);
6761 mInverseMatrixDirty = false;
6762 }
6763 return mInverseMatrix;
6764 }
6765
6766 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006767 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6768 * views are drawn) from the camera to this view. The camera's distance
6769 * affects 3D transformations, for instance rotations around the X and Y
6770 * axis. If the rotationX or rotationY properties are changed and this view is
6771 * large (more than half the size of the screen), it is recommended to always
6772 * use a camera distance that's greater than the height (X axis rotation) or
6773 * the width (Y axis rotation) of this view.</p>
6774 *
6775 * <p>The distance of the camera from the view plane can have an affect on the
6776 * perspective distortion of the view when it is rotated around the x or y axis.
6777 * For example, a large distance will result in a large viewing angle, and there
6778 * will not be much perspective distortion of the view as it rotates. A short
6779 * distance may cause much more perspective distortion upon rotation, and can
6780 * also result in some drawing artifacts if the rotated view ends up partially
6781 * behind the camera (which is why the recommendation is to use a distance at
6782 * least as far as the size of the view, if the view is to be rotated.)</p>
6783 *
6784 * <p>The distance is expressed in "depth pixels." The default distance depends
6785 * on the screen density. For instance, on a medium density display, the
6786 * default distance is 1280. On a high density display, the default distance
6787 * is 1920.</p>
6788 *
6789 * <p>If you want to specify a distance that leads to visually consistent
6790 * results across various densities, use the following formula:</p>
6791 * <pre>
6792 * float scale = context.getResources().getDisplayMetrics().density;
6793 * view.setCameraDistance(distance * scale);
6794 * </pre>
6795 *
6796 * <p>The density scale factor of a high density display is 1.5,
6797 * and 1920 = 1280 * 1.5.</p>
6798 *
6799 * @param distance The distance in "depth pixels", if negative the opposite
6800 * value is used
6801 *
6802 * @see #setRotationX(float)
6803 * @see #setRotationY(float)
6804 */
6805 public void setCameraDistance(float distance) {
6806 invalidateParentCaches();
6807 invalidate(false);
6808
6809 final float dpi = mResources.getDisplayMetrics().densityDpi;
6810 if (mCamera == null) {
6811 mCamera = new Camera();
6812 matrix3D = new Matrix();
6813 }
6814
6815 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6816 mMatrixDirty = true;
6817
6818 invalidate(false);
6819 }
6820
6821 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006822 * The degrees that the view is rotated around the pivot point.
6823 *
Romain Guya5364ee2011-02-24 14:46:04 -08006824 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006825 * @see #getPivotX()
6826 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006827 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006828 * @return The degrees of rotation.
6829 */
6830 public float getRotation() {
6831 return mRotation;
6832 }
6833
6834 /**
Chet Haase897247b2010-09-09 14:54:47 -07006835 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6836 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006837 *
6838 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006839 *
6840 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006841 * @see #getPivotX()
6842 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006843 * @see #setRotationX(float)
6844 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006845 *
6846 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006847 */
6848 public void setRotation(float rotation) {
6849 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006850 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006851 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006852 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006853 mRotation = rotation;
6854 mMatrixDirty = true;
6855 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006856 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006857 }
6858 }
6859
6860 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006861 * The degrees that the view is rotated around the vertical axis through the pivot point.
6862 *
6863 * @see #getPivotX()
6864 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006865 * @see #setRotationY(float)
6866 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006867 * @return The degrees of Y rotation.
6868 */
6869 public float getRotationY() {
6870 return mRotationY;
6871 }
6872
6873 /**
Chet Haase897247b2010-09-09 14:54:47 -07006874 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6875 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6876 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006877 *
6878 * When rotating large views, it is recommended to adjust the camera distance
6879 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006880 *
6881 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006882 *
6883 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006884 * @see #getPivotX()
6885 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006886 * @see #setRotation(float)
6887 * @see #setRotationX(float)
6888 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006889 *
6890 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006891 */
6892 public void setRotationY(float rotationY) {
6893 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006894 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006895 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006896 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006897 mRotationY = rotationY;
6898 mMatrixDirty = true;
6899 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006900 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006901 }
6902 }
6903
6904 /**
6905 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6906 *
6907 * @see #getPivotX()
6908 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006909 * @see #setRotationX(float)
6910 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006911 * @return The degrees of X rotation.
6912 */
6913 public float getRotationX() {
6914 return mRotationX;
6915 }
6916
6917 /**
Chet Haase897247b2010-09-09 14:54:47 -07006918 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6919 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6920 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006921 *
6922 * When rotating large views, it is recommended to adjust the camera distance
6923 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006924 *
6925 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006926 *
6927 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006928 * @see #getPivotX()
6929 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006930 * @see #setRotation(float)
6931 * @see #setRotationY(float)
6932 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006933 *
6934 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006935 */
6936 public void setRotationX(float rotationX) {
6937 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006938 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006939 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006940 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006941 mRotationX = rotationX;
6942 mMatrixDirty = true;
6943 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006944 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006945 }
6946 }
6947
6948 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006949 * The amount that the view is scaled in x around the pivot point, as a proportion of
6950 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6951 *
Joe Onorato93162322010-09-16 15:42:01 -04006952 * <p>By default, this is 1.0f.
6953 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006954 * @see #getPivotX()
6955 * @see #getPivotY()
6956 * @return The scaling factor.
6957 */
6958 public float getScaleX() {
6959 return mScaleX;
6960 }
6961
6962 /**
6963 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6964 * the view's unscaled width. A value of 1 means that no scaling is applied.
6965 *
6966 * @param scaleX The scaling factor.
6967 * @see #getPivotX()
6968 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006969 *
6970 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006971 */
6972 public void setScaleX(float scaleX) {
6973 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006974 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006975 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006976 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006977 mScaleX = scaleX;
6978 mMatrixDirty = true;
6979 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006980 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006981 }
6982 }
6983
6984 /**
6985 * The amount that the view is scaled in y around the pivot point, as a proportion of
6986 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6987 *
Joe Onorato93162322010-09-16 15:42:01 -04006988 * <p>By default, this is 1.0f.
6989 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006990 * @see #getPivotX()
6991 * @see #getPivotY()
6992 * @return The scaling factor.
6993 */
6994 public float getScaleY() {
6995 return mScaleY;
6996 }
6997
6998 /**
6999 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7000 * the view's unscaled width. A value of 1 means that no scaling is applied.
7001 *
7002 * @param scaleY The scaling factor.
7003 * @see #getPivotX()
7004 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007005 *
7006 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007007 */
7008 public void setScaleY(float scaleY) {
7009 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007010 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007011 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007012 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007013 mScaleY = scaleY;
7014 mMatrixDirty = true;
7015 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007016 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007017 }
7018 }
7019
7020 /**
7021 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7022 * and {@link #setScaleX(float) scaled}.
7023 *
7024 * @see #getRotation()
7025 * @see #getScaleX()
7026 * @see #getScaleY()
7027 * @see #getPivotY()
7028 * @return The x location of the pivot point.
7029 */
7030 public float getPivotX() {
7031 return mPivotX;
7032 }
7033
7034 /**
7035 * Sets the x location of the point around which the view is
7036 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007037 * By default, the pivot point is centered on the object.
7038 * Setting this property disables this behavior and causes the view to use only the
7039 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007040 *
7041 * @param pivotX The x location of the pivot point.
7042 * @see #getRotation()
7043 * @see #getScaleX()
7044 * @see #getScaleY()
7045 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007046 *
7047 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007048 */
7049 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007050 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007051 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007052 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007053 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007054 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007055 mPivotX = pivotX;
7056 mMatrixDirty = true;
7057 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007058 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007059 }
7060 }
7061
7062 /**
7063 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7064 * and {@link #setScaleY(float) scaled}.
7065 *
7066 * @see #getRotation()
7067 * @see #getScaleX()
7068 * @see #getScaleY()
7069 * @see #getPivotY()
7070 * @return The y location of the pivot point.
7071 */
7072 public float getPivotY() {
7073 return mPivotY;
7074 }
7075
7076 /**
7077 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007078 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7079 * Setting this property disables this behavior and causes the view to use only the
7080 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007081 *
7082 * @param pivotY The y location of the pivot point.
7083 * @see #getRotation()
7084 * @see #getScaleX()
7085 * @see #getScaleY()
7086 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007087 *
7088 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 */
7090 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007091 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007092 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007093 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007094 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007095 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007096 mPivotY = pivotY;
7097 mMatrixDirty = true;
7098 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007099 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007100 }
7101 }
7102
7103 /**
7104 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7105 * completely transparent and 1 means the view is completely opaque.
7106 *
Joe Onorato93162322010-09-16 15:42:01 -04007107 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007108 * @return The opacity of the view.
7109 */
7110 public float getAlpha() {
7111 return mAlpha;
7112 }
7113
7114 /**
Romain Guy171c5922011-01-06 10:04:23 -08007115 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7116 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007117 *
Romain Guy171c5922011-01-06 10:04:23 -08007118 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7119 * responsible for applying the opacity itself. Otherwise, calling this method is
7120 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007121 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007122 *
7123 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007124 *
Joe Malin32736f02011-01-19 16:14:20 -08007125 * @see #setLayerType(int, android.graphics.Paint)
7126 *
Chet Haase73066682010-11-29 15:55:32 -08007127 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007128 */
7129 public void setAlpha(float alpha) {
7130 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007131 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007132 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007133 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007134 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007135 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007136 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007137 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007138 invalidate(false);
7139 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007140 }
7141
7142 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007143 * Faster version of setAlpha() which performs the same steps except there are
7144 * no calls to invalidate(). The caller of this function should perform proper invalidation
7145 * on the parent and this object. The return value indicates whether the subclass handles
7146 * alpha (the return value for onSetAlpha()).
7147 *
7148 * @param alpha The new value for the alpha property
7149 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7150 */
7151 boolean setAlphaNoInvalidation(float alpha) {
7152 mAlpha = alpha;
7153 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7154 if (subclassHandlesAlpha) {
7155 mPrivateFlags |= ALPHA_SET;
7156 } else {
7157 mPrivateFlags &= ~ALPHA_SET;
7158 }
7159 return subclassHandlesAlpha;
7160 }
7161
7162 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007163 * Top position of this view relative to its parent.
7164 *
7165 * @return The top of this view, in pixels.
7166 */
7167 @ViewDebug.CapturedViewProperty
7168 public final int getTop() {
7169 return mTop;
7170 }
7171
7172 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007173 * Sets the top position of this view relative to its parent. This method is meant to be called
7174 * by the layout system and should not generally be called otherwise, because the property
7175 * may be changed at any time by the layout.
7176 *
7177 * @param top The top of this view, in pixels.
7178 */
7179 public final void setTop(int top) {
7180 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007181 updateMatrix();
7182 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007183 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007184 int minTop;
7185 int yLoc;
7186 if (top < mTop) {
7187 minTop = top;
7188 yLoc = top - mTop;
7189 } else {
7190 minTop = mTop;
7191 yLoc = 0;
7192 }
Chet Haasee9140a72011-02-16 16:23:29 -08007193 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007194 }
7195 } else {
7196 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007197 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007198 }
7199
Chet Haaseed032702010-10-01 14:05:54 -07007200 int width = mRight - mLeft;
7201 int oldHeight = mBottom - mTop;
7202
Chet Haase21cd1382010-09-01 17:42:29 -07007203 mTop = top;
7204
Chet Haaseed032702010-10-01 14:05:54 -07007205 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7206
Chet Haase21cd1382010-09-01 17:42:29 -07007207 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007208 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7209 // A change in dimension means an auto-centered pivot point changes, too
7210 mMatrixDirty = true;
7211 }
Chet Haase21cd1382010-09-01 17:42:29 -07007212 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007213 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007214 }
Chet Haase55dbb652010-12-21 20:15:08 -08007215 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007216 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007217 }
7218 }
7219
7220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007221 * Bottom position of this view relative to its parent.
7222 *
7223 * @return The bottom of this view, in pixels.
7224 */
7225 @ViewDebug.CapturedViewProperty
7226 public final int getBottom() {
7227 return mBottom;
7228 }
7229
7230 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007231 * True if this view has changed since the last time being drawn.
7232 *
7233 * @return The dirty state of this view.
7234 */
7235 public boolean isDirty() {
7236 return (mPrivateFlags & DIRTY_MASK) != 0;
7237 }
7238
7239 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007240 * Sets the bottom position of this view relative to its parent. This method is meant to be
7241 * called by the layout system and should not generally be called otherwise, because the
7242 * property may be changed at any time by the layout.
7243 *
7244 * @param bottom The bottom of this view, in pixels.
7245 */
7246 public final void setBottom(int bottom) {
7247 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007248 updateMatrix();
7249 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007250 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007251 int maxBottom;
7252 if (bottom < mBottom) {
7253 maxBottom = mBottom;
7254 } else {
7255 maxBottom = bottom;
7256 }
Chet Haasee9140a72011-02-16 16:23:29 -08007257 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007258 }
7259 } else {
7260 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007261 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007262 }
7263
Chet Haaseed032702010-10-01 14:05:54 -07007264 int width = mRight - mLeft;
7265 int oldHeight = mBottom - mTop;
7266
Chet Haase21cd1382010-09-01 17:42:29 -07007267 mBottom = bottom;
7268
Chet Haaseed032702010-10-01 14:05:54 -07007269 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7270
Chet Haase21cd1382010-09-01 17:42:29 -07007271 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007272 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7273 // A change in dimension means an auto-centered pivot point changes, too
7274 mMatrixDirty = true;
7275 }
Chet Haase21cd1382010-09-01 17:42:29 -07007276 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007277 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007278 }
Chet Haase55dbb652010-12-21 20:15:08 -08007279 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007280 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007281 }
7282 }
7283
7284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007285 * Left position of this view relative to its parent.
7286 *
7287 * @return The left edge of this view, in pixels.
7288 */
7289 @ViewDebug.CapturedViewProperty
7290 public final int getLeft() {
7291 return mLeft;
7292 }
7293
7294 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007295 * Sets the left position of this view relative to its parent. This method is meant to be called
7296 * by the layout system and should not generally be called otherwise, because the property
7297 * may be changed at any time by the layout.
7298 *
7299 * @param left The bottom of this view, in pixels.
7300 */
7301 public final void setLeft(int left) {
7302 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007303 updateMatrix();
7304 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007305 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007306 int minLeft;
7307 int xLoc;
7308 if (left < mLeft) {
7309 minLeft = left;
7310 xLoc = left - mLeft;
7311 } else {
7312 minLeft = mLeft;
7313 xLoc = 0;
7314 }
Chet Haasee9140a72011-02-16 16:23:29 -08007315 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007316 }
7317 } else {
7318 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007319 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007320 }
7321
Chet Haaseed032702010-10-01 14:05:54 -07007322 int oldWidth = mRight - mLeft;
7323 int height = mBottom - mTop;
7324
Chet Haase21cd1382010-09-01 17:42:29 -07007325 mLeft = left;
7326
Chet Haaseed032702010-10-01 14:05:54 -07007327 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7328
Chet Haase21cd1382010-09-01 17:42:29 -07007329 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007330 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7331 // A change in dimension means an auto-centered pivot point changes, too
7332 mMatrixDirty = true;
7333 }
Chet Haase21cd1382010-09-01 17:42:29 -07007334 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007335 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007336 }
Chet Haase55dbb652010-12-21 20:15:08 -08007337 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007338 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007339 }
7340 }
7341
7342 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007343 * Right position of this view relative to its parent.
7344 *
7345 * @return The right edge of this view, in pixels.
7346 */
7347 @ViewDebug.CapturedViewProperty
7348 public final int getRight() {
7349 return mRight;
7350 }
7351
7352 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007353 * Sets the right position of this view relative to its parent. This method is meant to be called
7354 * by the layout system and should not generally be called otherwise, because the property
7355 * may be changed at any time by the layout.
7356 *
7357 * @param right The bottom of this view, in pixels.
7358 */
7359 public final void setRight(int right) {
7360 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007361 updateMatrix();
7362 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007363 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007364 int maxRight;
7365 if (right < mRight) {
7366 maxRight = mRight;
7367 } else {
7368 maxRight = right;
7369 }
Chet Haasee9140a72011-02-16 16:23:29 -08007370 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007371 }
7372 } else {
7373 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007374 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007375 }
7376
Chet Haaseed032702010-10-01 14:05:54 -07007377 int oldWidth = mRight - mLeft;
7378 int height = mBottom - mTop;
7379
Chet Haase21cd1382010-09-01 17:42:29 -07007380 mRight = right;
7381
Chet Haaseed032702010-10-01 14:05:54 -07007382 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7383
Chet Haase21cd1382010-09-01 17:42:29 -07007384 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007385 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7386 // A change in dimension means an auto-centered pivot point changes, too
7387 mMatrixDirty = true;
7388 }
Chet Haase21cd1382010-09-01 17:42:29 -07007389 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007390 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007391 }
Chet Haase55dbb652010-12-21 20:15:08 -08007392 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007393 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007394 }
7395 }
7396
7397 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007398 * The visual x position of this view, in pixels. This is equivalent to the
7399 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007400 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007401 *
Chet Haasedf030d22010-07-30 17:22:38 -07007402 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007403 */
Chet Haasedf030d22010-07-30 17:22:38 -07007404 public float getX() {
7405 return mLeft + mTranslationX;
7406 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007407
Chet Haasedf030d22010-07-30 17:22:38 -07007408 /**
7409 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7410 * {@link #setTranslationX(float) translationX} property to be the difference between
7411 * the x value passed in and the current {@link #getLeft() left} property.
7412 *
7413 * @param x The visual x position of this view, in pixels.
7414 */
7415 public void setX(float x) {
7416 setTranslationX(x - mLeft);
7417 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007418
Chet Haasedf030d22010-07-30 17:22:38 -07007419 /**
7420 * The visual y position of this view, in pixels. This is equivalent to the
7421 * {@link #setTranslationY(float) translationY} property plus the current
7422 * {@link #getTop() top} property.
7423 *
7424 * @return The visual y position of this view, in pixels.
7425 */
7426 public float getY() {
7427 return mTop + mTranslationY;
7428 }
7429
7430 /**
7431 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7432 * {@link #setTranslationY(float) translationY} property to be the difference between
7433 * the y value passed in and the current {@link #getTop() top} property.
7434 *
7435 * @param y The visual y position of this view, in pixels.
7436 */
7437 public void setY(float y) {
7438 setTranslationY(y - mTop);
7439 }
7440
7441
7442 /**
7443 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7444 * This position is post-layout, in addition to wherever the object's
7445 * layout placed it.
7446 *
7447 * @return The horizontal position of this view relative to its left position, in pixels.
7448 */
7449 public float getTranslationX() {
7450 return mTranslationX;
7451 }
7452
7453 /**
7454 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7455 * This effectively positions the object post-layout, in addition to wherever the object's
7456 * layout placed it.
7457 *
7458 * @param translationX The horizontal position of this view relative to its left position,
7459 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007460 *
7461 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007462 */
7463 public void setTranslationX(float translationX) {
7464 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007465 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007466 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007467 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007468 mTranslationX = translationX;
7469 mMatrixDirty = true;
7470 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007471 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007472 }
7473 }
7474
7475 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007476 * The horizontal location of this view relative to its {@link #getTop() top} position.
7477 * This position is post-layout, in addition to wherever the object's
7478 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007479 *
Chet Haasedf030d22010-07-30 17:22:38 -07007480 * @return The vertical position of this view relative to its top position,
7481 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007482 */
Chet Haasedf030d22010-07-30 17:22:38 -07007483 public float getTranslationY() {
7484 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 }
7486
7487 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007488 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7489 * This effectively positions the object post-layout, in addition to wherever the object's
7490 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 *
Chet Haasedf030d22010-07-30 17:22:38 -07007492 * @param translationY The vertical position of this view relative to its top position,
7493 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007494 *
7495 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007496 */
Chet Haasedf030d22010-07-30 17:22:38 -07007497 public void setTranslationY(float translationY) {
7498 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007499 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007500 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007501 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007502 mTranslationY = translationY;
7503 mMatrixDirty = true;
7504 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007505 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007506 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007507 }
7508
7509 /**
Romain Guyda489792011-02-03 01:05:15 -08007510 * @hide
7511 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007512 public void setFastTranslationX(float x) {
7513 mTranslationX = x;
7514 mMatrixDirty = true;
7515 }
7516
7517 /**
7518 * @hide
7519 */
7520 public void setFastTranslationY(float y) {
7521 mTranslationY = y;
7522 mMatrixDirty = true;
7523 }
7524
7525 /**
7526 * @hide
7527 */
Romain Guyda489792011-02-03 01:05:15 -08007528 public void setFastX(float x) {
7529 mTranslationX = x - mLeft;
7530 mMatrixDirty = true;
7531 }
7532
7533 /**
7534 * @hide
7535 */
7536 public void setFastY(float y) {
7537 mTranslationY = y - mTop;
7538 mMatrixDirty = true;
7539 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007540
Romain Guyda489792011-02-03 01:05:15 -08007541 /**
7542 * @hide
7543 */
7544 public void setFastScaleX(float x) {
7545 mScaleX = x;
7546 mMatrixDirty = true;
7547 }
7548
7549 /**
7550 * @hide
7551 */
7552 public void setFastScaleY(float y) {
7553 mScaleY = y;
7554 mMatrixDirty = true;
7555 }
7556
7557 /**
7558 * @hide
7559 */
7560 public void setFastAlpha(float alpha) {
7561 mAlpha = alpha;
7562 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007563
Romain Guyda489792011-02-03 01:05:15 -08007564 /**
7565 * @hide
7566 */
7567 public void setFastRotationY(float y) {
7568 mRotationY = y;
7569 mMatrixDirty = true;
7570 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007571
Romain Guyda489792011-02-03 01:05:15 -08007572 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007573 * Hit rectangle in parent's coordinates
7574 *
7575 * @param outRect The hit rectangle of the view.
7576 */
7577 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007578 updateMatrix();
7579 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007580 outRect.set(mLeft, mTop, mRight, mBottom);
7581 } else {
7582 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007583 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007584 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007585 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7586 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007587 }
7588 }
7589
7590 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007591 * Determines whether the given point, in local coordinates is inside the view.
7592 */
7593 /*package*/ final boolean pointInView(float localX, float localY) {
7594 return localX >= 0 && localX < (mRight - mLeft)
7595 && localY >= 0 && localY < (mBottom - mTop);
7596 }
7597
7598 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007599 * Utility method to determine whether the given point, in local coordinates,
7600 * is inside the view, where the area of the view is expanded by the slop factor.
7601 * This method is called while processing touch-move events to determine if the event
7602 * is still within the view.
7603 */
7604 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007605 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007606 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007607 }
7608
7609 /**
7610 * When a view has focus and the user navigates away from it, the next view is searched for
7611 * starting from the rectangle filled in by this method.
7612 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007613 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7614 * of the view. However, if your view maintains some idea of internal selection,
7615 * such as a cursor, or a selected row or column, you should override this method and
7616 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007617 *
7618 * @param r The rectangle to fill in, in this view's coordinates.
7619 */
7620 public void getFocusedRect(Rect r) {
7621 getDrawingRect(r);
7622 }
7623
7624 /**
7625 * If some part of this view is not clipped by any of its parents, then
7626 * return that area in r in global (root) coordinates. To convert r to local
7627 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7628 * -globalOffset.y)) If the view is completely clipped or translated out,
7629 * return false.
7630 *
7631 * @param r If true is returned, r holds the global coordinates of the
7632 * visible portion of this view.
7633 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7634 * between this view and its root. globalOffet may be null.
7635 * @return true if r is non-empty (i.e. part of the view is visible at the
7636 * root level.
7637 */
7638 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7639 int width = mRight - mLeft;
7640 int height = mBottom - mTop;
7641 if (width > 0 && height > 0) {
7642 r.set(0, 0, width, height);
7643 if (globalOffset != null) {
7644 globalOffset.set(-mScrollX, -mScrollY);
7645 }
7646 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7647 }
7648 return false;
7649 }
7650
7651 public final boolean getGlobalVisibleRect(Rect r) {
7652 return getGlobalVisibleRect(r, null);
7653 }
7654
7655 public final boolean getLocalVisibleRect(Rect r) {
7656 Point offset = new Point();
7657 if (getGlobalVisibleRect(r, offset)) {
7658 r.offset(-offset.x, -offset.y); // make r local
7659 return true;
7660 }
7661 return false;
7662 }
7663
7664 /**
7665 * Offset this view's vertical location by the specified number of pixels.
7666 *
7667 * @param offset the number of pixels to offset the view by
7668 */
7669 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007670 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007671 updateMatrix();
7672 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007673 final ViewParent p = mParent;
7674 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007675 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007676 int minTop;
7677 int maxBottom;
7678 int yLoc;
7679 if (offset < 0) {
7680 minTop = mTop + offset;
7681 maxBottom = mBottom;
7682 yLoc = offset;
7683 } else {
7684 minTop = mTop;
7685 maxBottom = mBottom + offset;
7686 yLoc = 0;
7687 }
7688 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7689 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007690 }
7691 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007692 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007693 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007694
Chet Haasec3aa3612010-06-17 08:50:37 -07007695 mTop += offset;
7696 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007697
Chet Haasec3aa3612010-06-17 08:50:37 -07007698 if (!mMatrixIsIdentity) {
7699 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007700 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007701 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007702 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007703 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007704 }
7705
7706 /**
7707 * Offset this view's horizontal location by the specified amount of pixels.
7708 *
7709 * @param offset the numer of pixels to offset the view by
7710 */
7711 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007712 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007713 updateMatrix();
7714 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007715 final ViewParent p = mParent;
7716 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007717 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007718 int minLeft;
7719 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007720 if (offset < 0) {
7721 minLeft = mLeft + offset;
7722 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007723 } else {
7724 minLeft = mLeft;
7725 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007726 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007727 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007728 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007729 }
7730 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007731 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007732 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007733
Chet Haasec3aa3612010-06-17 08:50:37 -07007734 mLeft += offset;
7735 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007736
Chet Haasec3aa3612010-06-17 08:50:37 -07007737 if (!mMatrixIsIdentity) {
7738 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007739 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007740 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007741 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007742 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007743 }
7744
7745 /**
7746 * Get the LayoutParams associated with this view. All views should have
7747 * layout parameters. These supply parameters to the <i>parent</i> of this
7748 * view specifying how it should be arranged. There are many subclasses of
7749 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7750 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007751 *
7752 * This method may return null if this View is not attached to a parent
7753 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7754 * was not invoked successfully. When a View is attached to a parent
7755 * ViewGroup, this method must not return null.
7756 *
7757 * @return The LayoutParams associated with this view, or null if no
7758 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007759 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007760 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007761 public ViewGroup.LayoutParams getLayoutParams() {
7762 return mLayoutParams;
7763 }
7764
7765 /**
7766 * Set the layout parameters associated with this view. These supply
7767 * parameters to the <i>parent</i> of this view specifying how it should be
7768 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7769 * correspond to the different subclasses of ViewGroup that are responsible
7770 * for arranging their children.
7771 *
Romain Guy01c174b2011-02-22 11:51:06 -08007772 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007773 */
7774 public void setLayoutParams(ViewGroup.LayoutParams params) {
7775 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007776 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007777 }
7778 mLayoutParams = params;
7779 requestLayout();
7780 }
7781
7782 /**
7783 * Set the scrolled position of your view. This will cause a call to
7784 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7785 * invalidated.
7786 * @param x the x position to scroll to
7787 * @param y the y position to scroll to
7788 */
7789 public void scrollTo(int x, int y) {
7790 if (mScrollX != x || mScrollY != y) {
7791 int oldX = mScrollX;
7792 int oldY = mScrollY;
7793 mScrollX = x;
7794 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007795 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007796 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007797 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007798 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007800 }
7801 }
7802
7803 /**
7804 * Move the scrolled position of your view. This will cause a call to
7805 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7806 * invalidated.
7807 * @param x the amount of pixels to scroll by horizontally
7808 * @param y the amount of pixels to scroll by vertically
7809 */
7810 public void scrollBy(int x, int y) {
7811 scrollTo(mScrollX + x, mScrollY + y);
7812 }
7813
7814 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007815 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7816 * animation to fade the scrollbars out after a default delay. If a subclass
7817 * provides animated scrolling, the start delay should equal the duration
7818 * of the scrolling animation.</p>
7819 *
7820 * <p>The animation starts only if at least one of the scrollbars is
7821 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7822 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7823 * this method returns true, and false otherwise. If the animation is
7824 * started, this method calls {@link #invalidate()}; in that case the
7825 * caller should not call {@link #invalidate()}.</p>
7826 *
7827 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007828 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007829 *
7830 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7831 * and {@link #scrollTo(int, int)}.</p>
7832 *
7833 * @return true if the animation is played, false otherwise
7834 *
7835 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007836 * @see #scrollBy(int, int)
7837 * @see #scrollTo(int, int)
7838 * @see #isHorizontalScrollBarEnabled()
7839 * @see #isVerticalScrollBarEnabled()
7840 * @see #setHorizontalScrollBarEnabled(boolean)
7841 * @see #setVerticalScrollBarEnabled(boolean)
7842 */
7843 protected boolean awakenScrollBars() {
7844 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007845 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007846 }
7847
7848 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007849 * Trigger the scrollbars to draw.
7850 * This method differs from awakenScrollBars() only in its default duration.
7851 * initialAwakenScrollBars() will show the scroll bars for longer than
7852 * usual to give the user more of a chance to notice them.
7853 *
7854 * @return true if the animation is played, false otherwise.
7855 */
7856 private boolean initialAwakenScrollBars() {
7857 return mScrollCache != null &&
7858 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7859 }
7860
7861 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007862 * <p>
7863 * Trigger the scrollbars to draw. When invoked this method starts an
7864 * animation to fade the scrollbars out after a fixed delay. If a subclass
7865 * provides animated scrolling, the start delay should equal the duration of
7866 * the scrolling animation.
7867 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007868 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007869 * <p>
7870 * The animation starts only if at least one of the scrollbars is enabled,
7871 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7872 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7873 * this method returns true, and false otherwise. If the animation is
7874 * started, this method calls {@link #invalidate()}; in that case the caller
7875 * should not call {@link #invalidate()}.
7876 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007877 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007878 * <p>
7879 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007880 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007881 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007882 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007883 * @param startDelay the delay, in milliseconds, after which the animation
7884 * should start; when the delay is 0, the animation starts
7885 * immediately
7886 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007887 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007888 * @see #scrollBy(int, int)
7889 * @see #scrollTo(int, int)
7890 * @see #isHorizontalScrollBarEnabled()
7891 * @see #isVerticalScrollBarEnabled()
7892 * @see #setHorizontalScrollBarEnabled(boolean)
7893 * @see #setVerticalScrollBarEnabled(boolean)
7894 */
7895 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007896 return awakenScrollBars(startDelay, true);
7897 }
Joe Malin32736f02011-01-19 16:14:20 -08007898
Mike Cleron290947b2009-09-29 18:34:32 -07007899 /**
7900 * <p>
7901 * Trigger the scrollbars to draw. When invoked this method starts an
7902 * animation to fade the scrollbars out after a fixed delay. If a subclass
7903 * provides animated scrolling, the start delay should equal the duration of
7904 * the scrolling animation.
7905 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007906 *
Mike Cleron290947b2009-09-29 18:34:32 -07007907 * <p>
7908 * The animation starts only if at least one of the scrollbars is enabled,
7909 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7910 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7911 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007912 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007913 * is set to true; in that case the caller
7914 * should not call {@link #invalidate()}.
7915 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007916 *
Mike Cleron290947b2009-09-29 18:34:32 -07007917 * <p>
7918 * This method should be invoked everytime a subclass directly updates the
7919 * scroll parameters.
7920 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007921 *
Mike Cleron290947b2009-09-29 18:34:32 -07007922 * @param startDelay the delay, in milliseconds, after which the animation
7923 * should start; when the delay is 0, the animation starts
7924 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007925 *
Mike Cleron290947b2009-09-29 18:34:32 -07007926 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007927 *
Mike Cleron290947b2009-09-29 18:34:32 -07007928 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007929 *
Mike Cleron290947b2009-09-29 18:34:32 -07007930 * @see #scrollBy(int, int)
7931 * @see #scrollTo(int, int)
7932 * @see #isHorizontalScrollBarEnabled()
7933 * @see #isVerticalScrollBarEnabled()
7934 * @see #setHorizontalScrollBarEnabled(boolean)
7935 * @see #setVerticalScrollBarEnabled(boolean)
7936 */
7937 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007938 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007939
Mike Cleronf116bf82009-09-27 19:14:12 -07007940 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7941 return false;
7942 }
7943
7944 if (scrollCache.scrollBar == null) {
7945 scrollCache.scrollBar = new ScrollBarDrawable();
7946 }
7947
7948 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7949
Mike Cleron290947b2009-09-29 18:34:32 -07007950 if (invalidate) {
7951 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007952 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007953 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007954
7955 if (scrollCache.state == ScrollabilityCache.OFF) {
7956 // FIXME: this is copied from WindowManagerService.
7957 // We should get this value from the system when it
7958 // is possible to do so.
7959 final int KEY_REPEAT_FIRST_DELAY = 750;
7960 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7961 }
7962
7963 // Tell mScrollCache when we should start fading. This may
7964 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007965 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007966 scrollCache.fadeStartTime = fadeStartTime;
7967 scrollCache.state = ScrollabilityCache.ON;
7968
7969 // Schedule our fader to run, unscheduling any old ones first
7970 if (mAttachInfo != null) {
7971 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7972 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7973 }
7974
7975 return true;
7976 }
7977
7978 return false;
7979 }
7980
7981 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007982 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007983 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7984 * in the future. This must be called from a UI thread. To call from a non-UI
7985 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007986 *
7987 * WARNING: This method is destructive to dirty.
7988 * @param dirty the rectangle representing the bounds of the dirty region
7989 */
7990 public void invalidate(Rect dirty) {
7991 if (ViewDebug.TRACE_HIERARCHY) {
7992 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7993 }
7994
Romain Guy2fe9a8f2010-10-04 20:17:01 -07007995 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08007996 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
7997 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007998 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08007999 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008000 final ViewParent p = mParent;
8001 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008002 //noinspection PointlessBooleanExpression,ConstantConditions
8003 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8004 if (p != null && ai != null && ai.mHardwareAccelerated) {
8005 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008006 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008007 p.invalidateChild(this, null);
8008 return;
8009 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008011 if (p != null && ai != null) {
8012 final int scrollX = mScrollX;
8013 final int scrollY = mScrollY;
8014 final Rect r = ai.mTmpInvalRect;
8015 r.set(dirty.left - scrollX, dirty.top - scrollY,
8016 dirty.right - scrollX, dirty.bottom - scrollY);
8017 mParent.invalidateChild(this, r);
8018 }
8019 }
8020 }
8021
8022 /**
8023 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8024 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008025 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8026 * will be called at some point in the future. This must be called from
8027 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008028 * @param l the left position of the dirty region
8029 * @param t the top position of the dirty region
8030 * @param r the right position of the dirty region
8031 * @param b the bottom position of the dirty region
8032 */
8033 public void invalidate(int l, int t, int r, int b) {
8034 if (ViewDebug.TRACE_HIERARCHY) {
8035 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8036 }
8037
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008038 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008039 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8040 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008041 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008042 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008043 final ViewParent p = mParent;
8044 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008045 //noinspection PointlessBooleanExpression,ConstantConditions
8046 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8047 if (p != null && ai != null && ai.mHardwareAccelerated) {
8048 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008049 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008050 p.invalidateChild(this, null);
8051 return;
8052 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008054 if (p != null && ai != null && l < r && t < b) {
8055 final int scrollX = mScrollX;
8056 final int scrollY = mScrollY;
8057 final Rect tmpr = ai.mTmpInvalRect;
8058 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8059 p.invalidateChild(this, tmpr);
8060 }
8061 }
8062 }
8063
8064 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008065 * Invalidate the whole view. If the view is visible,
8066 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8067 * the future. This must be called from a UI thread. To call from a non-UI thread,
8068 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008069 */
8070 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008071 invalidate(true);
8072 }
Joe Malin32736f02011-01-19 16:14:20 -08008073
Chet Haaseed032702010-10-01 14:05:54 -07008074 /**
8075 * This is where the invalidate() work actually happens. A full invalidate()
8076 * causes the drawing cache to be invalidated, but this function can be called with
8077 * invalidateCache set to false to skip that invalidation step for cases that do not
8078 * need it (for example, a component that remains at the same dimensions with the same
8079 * content).
8080 *
8081 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8082 * well. This is usually true for a full invalidate, but may be set to false if the
8083 * View's contents or dimensions have not changed.
8084 */
Romain Guy849d0a32011-02-01 17:20:48 -08008085 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008086 if (ViewDebug.TRACE_HIERARCHY) {
8087 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8088 }
8089
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008090 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008091 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008092 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8093 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008094 mPrivateFlags &= ~DRAWN;
8095 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008096 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008097 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8098 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008099 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008100 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008101 //noinspection PointlessBooleanExpression,ConstantConditions
8102 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8103 if (p != null && ai != null && ai.mHardwareAccelerated) {
8104 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008105 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008106 p.invalidateChild(this, null);
8107 return;
8108 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008109 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 if (p != null && ai != null) {
8112 final Rect r = ai.mTmpInvalRect;
8113 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8114 // Don't call invalidate -- we don't want to internally scroll
8115 // our own bounds
8116 p.invalidateChild(this, r);
8117 }
8118 }
8119 }
8120
8121 /**
Romain Guyda489792011-02-03 01:05:15 -08008122 * @hide
8123 */
8124 public void fastInvalidate() {
8125 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8126 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8127 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8128 if (mParent instanceof View) {
8129 ((View) mParent).mPrivateFlags |= INVALIDATED;
8130 }
8131 mPrivateFlags &= ~DRAWN;
8132 mPrivateFlags |= INVALIDATED;
8133 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008134 if (mParent != null && mAttachInfo != null) {
8135 if (mAttachInfo.mHardwareAccelerated) {
8136 mParent.invalidateChild(this, null);
8137 } else {
8138 final Rect r = mAttachInfo.mTmpInvalRect;
8139 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8140 // Don't call invalidate -- we don't want to internally scroll
8141 // our own bounds
8142 mParent.invalidateChild(this, r);
8143 }
Romain Guyda489792011-02-03 01:05:15 -08008144 }
8145 }
8146 }
8147
8148 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008149 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008150 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8151 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008152 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8153 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008154 *
8155 * @hide
8156 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008157 protected void invalidateParentCaches() {
8158 if (mParent instanceof View) {
8159 ((View) mParent).mPrivateFlags |= INVALIDATED;
8160 }
8161 }
Joe Malin32736f02011-01-19 16:14:20 -08008162
Romain Guy0fd89bf2011-01-26 15:41:30 -08008163 /**
8164 * Used to indicate that the parent of this view should be invalidated. This functionality
8165 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8166 * which is necessary when various parent-managed properties of the view change, such as
8167 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8168 * an invalidation event to the parent.
8169 *
8170 * @hide
8171 */
8172 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008173 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008174 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008175 }
8176 }
8177
8178 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008179 * Indicates whether this View is opaque. An opaque View guarantees that it will
8180 * draw all the pixels overlapping its bounds using a fully opaque color.
8181 *
8182 * Subclasses of View should override this method whenever possible to indicate
8183 * whether an instance is opaque. Opaque Views are treated in a special way by
8184 * the View hierarchy, possibly allowing it to perform optimizations during
8185 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008186 *
Romain Guy24443ea2009-05-11 11:56:30 -07008187 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008188 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008189 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008190 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008191 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8192 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008193 }
8194
Adam Powell20232d02010-12-08 21:08:53 -08008195 /**
8196 * @hide
8197 */
8198 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008199 // Opaque if:
8200 // - Has a background
8201 // - Background is opaque
8202 // - Doesn't have scrollbars or scrollbars are inside overlay
8203
8204 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8205 mPrivateFlags |= OPAQUE_BACKGROUND;
8206 } else {
8207 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8208 }
8209
8210 final int flags = mViewFlags;
8211 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8212 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8213 mPrivateFlags |= OPAQUE_SCROLLBARS;
8214 } else {
8215 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8216 }
8217 }
8218
8219 /**
8220 * @hide
8221 */
8222 protected boolean hasOpaqueScrollbars() {
8223 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008224 }
8225
8226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008227 * @return A handler associated with the thread running the View. This
8228 * handler can be used to pump events in the UI events queue.
8229 */
8230 public Handler getHandler() {
8231 if (mAttachInfo != null) {
8232 return mAttachInfo.mHandler;
8233 }
8234 return null;
8235 }
8236
8237 /**
8238 * Causes the Runnable to be added to the message queue.
8239 * The runnable will be run on the user interface thread.
8240 *
8241 * @param action The Runnable that will be executed.
8242 *
8243 * @return Returns true if the Runnable was successfully placed in to the
8244 * message queue. Returns false on failure, usually because the
8245 * looper processing the message queue is exiting.
8246 */
8247 public boolean post(Runnable action) {
8248 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008249 AttachInfo attachInfo = mAttachInfo;
8250 if (attachInfo != null) {
8251 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008252 } else {
8253 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008254 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008255 return true;
8256 }
8257
8258 return handler.post(action);
8259 }
8260
8261 /**
8262 * Causes the Runnable to be added to the message queue, to be run
8263 * after the specified amount of time elapses.
8264 * The runnable will be run on the user interface thread.
8265 *
8266 * @param action The Runnable that will be executed.
8267 * @param delayMillis The delay (in milliseconds) until the Runnable
8268 * will be executed.
8269 *
8270 * @return true if the Runnable was successfully placed in to the
8271 * message queue. Returns false on failure, usually because the
8272 * looper processing the message queue is exiting. Note that a
8273 * result of true does not mean the Runnable will be processed --
8274 * if the looper is quit before the delivery time of the message
8275 * occurs then the message will be dropped.
8276 */
8277 public boolean postDelayed(Runnable action, long delayMillis) {
8278 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008279 AttachInfo attachInfo = mAttachInfo;
8280 if (attachInfo != null) {
8281 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008282 } else {
8283 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008284 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008285 return true;
8286 }
8287
8288 return handler.postDelayed(action, delayMillis);
8289 }
8290
8291 /**
8292 * Removes the specified Runnable from the message queue.
8293 *
8294 * @param action The Runnable to remove from the message handling queue
8295 *
8296 * @return true if this view could ask the Handler to remove the Runnable,
8297 * false otherwise. When the returned value is true, the Runnable
8298 * may or may not have been actually removed from the message queue
8299 * (for instance, if the Runnable was not in the queue already.)
8300 */
8301 public boolean removeCallbacks(Runnable action) {
8302 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008303 AttachInfo attachInfo = mAttachInfo;
8304 if (attachInfo != null) {
8305 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008306 } else {
8307 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008308 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008309 return true;
8310 }
8311
8312 handler.removeCallbacks(action);
8313 return true;
8314 }
8315
8316 /**
8317 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8318 * Use this to invalidate the View from a non-UI thread.
8319 *
8320 * @see #invalidate()
8321 */
8322 public void postInvalidate() {
8323 postInvalidateDelayed(0);
8324 }
8325
8326 /**
8327 * Cause an invalidate of the specified area to happen on a subsequent cycle
8328 * through the event loop. Use this to invalidate the View from a non-UI thread.
8329 *
8330 * @param left The left coordinate of the rectangle to invalidate.
8331 * @param top The top coordinate of the rectangle to invalidate.
8332 * @param right The right coordinate of the rectangle to invalidate.
8333 * @param bottom The bottom coordinate of the rectangle to invalidate.
8334 *
8335 * @see #invalidate(int, int, int, int)
8336 * @see #invalidate(Rect)
8337 */
8338 public void postInvalidate(int left, int top, int right, int bottom) {
8339 postInvalidateDelayed(0, left, top, right, bottom);
8340 }
8341
8342 /**
8343 * Cause an invalidate to happen on a subsequent cycle through the event
8344 * loop. Waits for the specified amount of time.
8345 *
8346 * @param delayMilliseconds the duration in milliseconds to delay the
8347 * invalidation by
8348 */
8349 public void postInvalidateDelayed(long delayMilliseconds) {
8350 // We try only with the AttachInfo because there's no point in invalidating
8351 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008352 AttachInfo attachInfo = mAttachInfo;
8353 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008354 Message msg = Message.obtain();
8355 msg.what = AttachInfo.INVALIDATE_MSG;
8356 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008357 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008358 }
8359 }
8360
8361 /**
8362 * Cause an invalidate of the specified area to happen on a subsequent cycle
8363 * through the event loop. Waits for the specified amount of time.
8364 *
8365 * @param delayMilliseconds the duration in milliseconds to delay the
8366 * invalidation by
8367 * @param left The left coordinate of the rectangle to invalidate.
8368 * @param top The top coordinate of the rectangle to invalidate.
8369 * @param right The right coordinate of the rectangle to invalidate.
8370 * @param bottom The bottom coordinate of the rectangle to invalidate.
8371 */
8372 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8373 int right, int bottom) {
8374
8375 // We try only with the AttachInfo because there's no point in invalidating
8376 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008377 AttachInfo attachInfo = mAttachInfo;
8378 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008379 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8380 info.target = this;
8381 info.left = left;
8382 info.top = top;
8383 info.right = right;
8384 info.bottom = bottom;
8385
8386 final Message msg = Message.obtain();
8387 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8388 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008389 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008390 }
8391 }
8392
8393 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008394 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8395 * This event is sent at most once every
8396 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8397 */
8398 private void postSendViewScrolledAccessibilityEventCallback() {
8399 if (mSendViewScrolledAccessibilityEvent == null) {
8400 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8401 }
8402 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8403 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8404 postDelayed(mSendViewScrolledAccessibilityEvent,
8405 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8406 }
8407 }
8408
8409 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008410 * Called by a parent to request that a child update its values for mScrollX
8411 * and mScrollY if necessary. This will typically be done if the child is
8412 * animating a scroll using a {@link android.widget.Scroller Scroller}
8413 * object.
8414 */
8415 public void computeScroll() {
8416 }
8417
8418 /**
8419 * <p>Indicate whether the horizontal edges are faded when the view is
8420 * scrolled horizontally.</p>
8421 *
8422 * @return true if the horizontal edges should are faded on scroll, false
8423 * otherwise
8424 *
8425 * @see #setHorizontalFadingEdgeEnabled(boolean)
8426 * @attr ref android.R.styleable#View_fadingEdge
8427 */
8428 public boolean isHorizontalFadingEdgeEnabled() {
8429 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8430 }
8431
8432 /**
8433 * <p>Define whether the horizontal edges should be faded when this view
8434 * is scrolled horizontally.</p>
8435 *
8436 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8437 * be faded when the view is scrolled
8438 * horizontally
8439 *
8440 * @see #isHorizontalFadingEdgeEnabled()
8441 * @attr ref android.R.styleable#View_fadingEdge
8442 */
8443 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8444 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8445 if (horizontalFadingEdgeEnabled) {
8446 initScrollCache();
8447 }
8448
8449 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8450 }
8451 }
8452
8453 /**
8454 * <p>Indicate whether the vertical edges are faded when the view is
8455 * scrolled horizontally.</p>
8456 *
8457 * @return true if the vertical edges should are faded on scroll, false
8458 * otherwise
8459 *
8460 * @see #setVerticalFadingEdgeEnabled(boolean)
8461 * @attr ref android.R.styleable#View_fadingEdge
8462 */
8463 public boolean isVerticalFadingEdgeEnabled() {
8464 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8465 }
8466
8467 /**
8468 * <p>Define whether the vertical edges should be faded when this view
8469 * is scrolled vertically.</p>
8470 *
8471 * @param verticalFadingEdgeEnabled true if the vertical edges should
8472 * be faded when the view is scrolled
8473 * vertically
8474 *
8475 * @see #isVerticalFadingEdgeEnabled()
8476 * @attr ref android.R.styleable#View_fadingEdge
8477 */
8478 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8479 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8480 if (verticalFadingEdgeEnabled) {
8481 initScrollCache();
8482 }
8483
8484 mViewFlags ^= FADING_EDGE_VERTICAL;
8485 }
8486 }
8487
8488 /**
8489 * Returns the strength, or intensity, of the top faded edge. The strength is
8490 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8491 * returns 0.0 or 1.0 but no value in between.
8492 *
8493 * Subclasses should override this method to provide a smoother fade transition
8494 * when scrolling occurs.
8495 *
8496 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8497 */
8498 protected float getTopFadingEdgeStrength() {
8499 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8500 }
8501
8502 /**
8503 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8504 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8505 * returns 0.0 or 1.0 but no value in between.
8506 *
8507 * Subclasses should override this method to provide a smoother fade transition
8508 * when scrolling occurs.
8509 *
8510 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8511 */
8512 protected float getBottomFadingEdgeStrength() {
8513 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8514 computeVerticalScrollRange() ? 1.0f : 0.0f;
8515 }
8516
8517 /**
8518 * Returns the strength, or intensity, of the left faded edge. The strength is
8519 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8520 * returns 0.0 or 1.0 but no value in between.
8521 *
8522 * Subclasses should override this method to provide a smoother fade transition
8523 * when scrolling occurs.
8524 *
8525 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8526 */
8527 protected float getLeftFadingEdgeStrength() {
8528 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8529 }
8530
8531 /**
8532 * Returns the strength, or intensity, of the right faded edge. The strength is
8533 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8534 * returns 0.0 or 1.0 but no value in between.
8535 *
8536 * Subclasses should override this method to provide a smoother fade transition
8537 * when scrolling occurs.
8538 *
8539 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8540 */
8541 protected float getRightFadingEdgeStrength() {
8542 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8543 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8544 }
8545
8546 /**
8547 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8548 * scrollbar is not drawn by default.</p>
8549 *
8550 * @return true if the horizontal scrollbar should be painted, false
8551 * otherwise
8552 *
8553 * @see #setHorizontalScrollBarEnabled(boolean)
8554 */
8555 public boolean isHorizontalScrollBarEnabled() {
8556 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8557 }
8558
8559 /**
8560 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8561 * scrollbar is not drawn by default.</p>
8562 *
8563 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8564 * be painted
8565 *
8566 * @see #isHorizontalScrollBarEnabled()
8567 */
8568 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8569 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8570 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008571 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008572 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008573 }
8574 }
8575
8576 /**
8577 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8578 * scrollbar is not drawn by default.</p>
8579 *
8580 * @return true if the vertical scrollbar should be painted, false
8581 * otherwise
8582 *
8583 * @see #setVerticalScrollBarEnabled(boolean)
8584 */
8585 public boolean isVerticalScrollBarEnabled() {
8586 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8587 }
8588
8589 /**
8590 * <p>Define whether the vertical scrollbar should be drawn or not. The
8591 * scrollbar is not drawn by default.</p>
8592 *
8593 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8594 * be painted
8595 *
8596 * @see #isVerticalScrollBarEnabled()
8597 */
8598 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8599 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8600 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008601 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008602 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008603 }
8604 }
8605
Adam Powell20232d02010-12-08 21:08:53 -08008606 /**
8607 * @hide
8608 */
8609 protected void recomputePadding() {
8610 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008611 }
Joe Malin32736f02011-01-19 16:14:20 -08008612
Mike Cleronfe81d382009-09-28 14:22:16 -07008613 /**
8614 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008615 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008616 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008617 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008618 */
8619 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8620 initScrollCache();
8621 final ScrollabilityCache scrollabilityCache = mScrollCache;
8622 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008623 if (fadeScrollbars) {
8624 scrollabilityCache.state = ScrollabilityCache.OFF;
8625 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008626 scrollabilityCache.state = ScrollabilityCache.ON;
8627 }
8628 }
Joe Malin32736f02011-01-19 16:14:20 -08008629
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008630 /**
Joe Malin32736f02011-01-19 16:14:20 -08008631 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008632 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008633 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008634 * @return true if scrollbar fading is enabled
8635 */
8636 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008637 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008638 }
Joe Malin32736f02011-01-19 16:14:20 -08008639
Mike Cleron52f0a642009-09-28 18:21:37 -07008640 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008641 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8642 * inset. When inset, they add to the padding of the view. And the scrollbars
8643 * can be drawn inside the padding area or on the edge of the view. For example,
8644 * if a view has a background drawable and you want to draw the scrollbars
8645 * inside the padding specified by the drawable, you can use
8646 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8647 * appear at the edge of the view, ignoring the padding, then you can use
8648 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8649 * @param style the style of the scrollbars. Should be one of
8650 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8651 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8652 * @see #SCROLLBARS_INSIDE_OVERLAY
8653 * @see #SCROLLBARS_INSIDE_INSET
8654 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8655 * @see #SCROLLBARS_OUTSIDE_INSET
8656 */
8657 public void setScrollBarStyle(int style) {
8658 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8659 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008660 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008661 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008662 }
8663 }
8664
8665 /**
8666 * <p>Returns the current scrollbar style.</p>
8667 * @return the current scrollbar style
8668 * @see #SCROLLBARS_INSIDE_OVERLAY
8669 * @see #SCROLLBARS_INSIDE_INSET
8670 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8671 * @see #SCROLLBARS_OUTSIDE_INSET
8672 */
8673 public int getScrollBarStyle() {
8674 return mViewFlags & SCROLLBARS_STYLE_MASK;
8675 }
8676
8677 /**
8678 * <p>Compute the horizontal range that the horizontal scrollbar
8679 * represents.</p>
8680 *
8681 * <p>The range is expressed in arbitrary units that must be the same as the
8682 * units used by {@link #computeHorizontalScrollExtent()} and
8683 * {@link #computeHorizontalScrollOffset()}.</p>
8684 *
8685 * <p>The default range is the drawing width of this view.</p>
8686 *
8687 * @return the total horizontal range represented by the horizontal
8688 * scrollbar
8689 *
8690 * @see #computeHorizontalScrollExtent()
8691 * @see #computeHorizontalScrollOffset()
8692 * @see android.widget.ScrollBarDrawable
8693 */
8694 protected int computeHorizontalScrollRange() {
8695 return getWidth();
8696 }
8697
8698 /**
8699 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8700 * within the horizontal range. This value is used to compute the position
8701 * of the thumb within the scrollbar's track.</p>
8702 *
8703 * <p>The range is expressed in arbitrary units that must be the same as the
8704 * units used by {@link #computeHorizontalScrollRange()} and
8705 * {@link #computeHorizontalScrollExtent()}.</p>
8706 *
8707 * <p>The default offset is the scroll offset of this view.</p>
8708 *
8709 * @return the horizontal offset of the scrollbar's thumb
8710 *
8711 * @see #computeHorizontalScrollRange()
8712 * @see #computeHorizontalScrollExtent()
8713 * @see android.widget.ScrollBarDrawable
8714 */
8715 protected int computeHorizontalScrollOffset() {
8716 return mScrollX;
8717 }
8718
8719 /**
8720 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8721 * within the horizontal range. This value is used to compute the length
8722 * of the thumb within the scrollbar's track.</p>
8723 *
8724 * <p>The range is expressed in arbitrary units that must be the same as the
8725 * units used by {@link #computeHorizontalScrollRange()} and
8726 * {@link #computeHorizontalScrollOffset()}.</p>
8727 *
8728 * <p>The default extent is the drawing width of this view.</p>
8729 *
8730 * @return the horizontal extent of the scrollbar's thumb
8731 *
8732 * @see #computeHorizontalScrollRange()
8733 * @see #computeHorizontalScrollOffset()
8734 * @see android.widget.ScrollBarDrawable
8735 */
8736 protected int computeHorizontalScrollExtent() {
8737 return getWidth();
8738 }
8739
8740 /**
8741 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8742 *
8743 * <p>The range is expressed in arbitrary units that must be the same as the
8744 * units used by {@link #computeVerticalScrollExtent()} and
8745 * {@link #computeVerticalScrollOffset()}.</p>
8746 *
8747 * @return the total vertical range represented by the vertical scrollbar
8748 *
8749 * <p>The default range is the drawing height of this view.</p>
8750 *
8751 * @see #computeVerticalScrollExtent()
8752 * @see #computeVerticalScrollOffset()
8753 * @see android.widget.ScrollBarDrawable
8754 */
8755 protected int computeVerticalScrollRange() {
8756 return getHeight();
8757 }
8758
8759 /**
8760 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8761 * within the horizontal range. This value is used to compute the position
8762 * of the thumb within the scrollbar's track.</p>
8763 *
8764 * <p>The range is expressed in arbitrary units that must be the same as the
8765 * units used by {@link #computeVerticalScrollRange()} and
8766 * {@link #computeVerticalScrollExtent()}.</p>
8767 *
8768 * <p>The default offset is the scroll offset of this view.</p>
8769 *
8770 * @return the vertical offset of the scrollbar's thumb
8771 *
8772 * @see #computeVerticalScrollRange()
8773 * @see #computeVerticalScrollExtent()
8774 * @see android.widget.ScrollBarDrawable
8775 */
8776 protected int computeVerticalScrollOffset() {
8777 return mScrollY;
8778 }
8779
8780 /**
8781 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8782 * within the vertical range. This value is used to compute the length
8783 * of the thumb within the scrollbar's track.</p>
8784 *
8785 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008786 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008787 * {@link #computeVerticalScrollOffset()}.</p>
8788 *
8789 * <p>The default extent is the drawing height of this view.</p>
8790 *
8791 * @return the vertical extent of the scrollbar's thumb
8792 *
8793 * @see #computeVerticalScrollRange()
8794 * @see #computeVerticalScrollOffset()
8795 * @see android.widget.ScrollBarDrawable
8796 */
8797 protected int computeVerticalScrollExtent() {
8798 return getHeight();
8799 }
8800
8801 /**
Adam Powell69159442011-06-13 17:53:06 -07008802 * Check if this view can be scrolled horizontally in a certain direction.
8803 *
8804 * @param direction Negative to check scrolling left, positive to check scrolling right.
8805 * @return true if this view can be scrolled in the specified direction, false otherwise.
8806 */
8807 public boolean canScrollHorizontally(int direction) {
8808 final int offset = computeHorizontalScrollOffset();
8809 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8810 if (range == 0) return false;
8811 if (direction < 0) {
8812 return offset > 0;
8813 } else {
8814 return offset < range - 1;
8815 }
8816 }
8817
8818 /**
8819 * Check if this view can be scrolled vertically in a certain direction.
8820 *
8821 * @param direction Negative to check scrolling up, positive to check scrolling down.
8822 * @return true if this view can be scrolled in the specified direction, false otherwise.
8823 */
8824 public boolean canScrollVertically(int direction) {
8825 final int offset = computeVerticalScrollOffset();
8826 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8827 if (range == 0) return false;
8828 if (direction < 0) {
8829 return offset > 0;
8830 } else {
8831 return offset < range - 1;
8832 }
8833 }
8834
8835 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008836 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8837 * scrollbars are painted only if they have been awakened first.</p>
8838 *
8839 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008840 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008841 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008843 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008844 // scrollbars are drawn only when the animation is running
8845 final ScrollabilityCache cache = mScrollCache;
8846 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008847
Mike Cleronf116bf82009-09-27 19:14:12 -07008848 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008849
Mike Cleronf116bf82009-09-27 19:14:12 -07008850 if (state == ScrollabilityCache.OFF) {
8851 return;
8852 }
Joe Malin32736f02011-01-19 16:14:20 -08008853
Mike Cleronf116bf82009-09-27 19:14:12 -07008854 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008855
Mike Cleronf116bf82009-09-27 19:14:12 -07008856 if (state == ScrollabilityCache.FADING) {
8857 // We're fading -- get our fade interpolation
8858 if (cache.interpolatorValues == null) {
8859 cache.interpolatorValues = new float[1];
8860 }
Joe Malin32736f02011-01-19 16:14:20 -08008861
Mike Cleronf116bf82009-09-27 19:14:12 -07008862 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008863
Mike Cleronf116bf82009-09-27 19:14:12 -07008864 // Stops the animation if we're done
8865 if (cache.scrollBarInterpolator.timeToValues(values) ==
8866 Interpolator.Result.FREEZE_END) {
8867 cache.state = ScrollabilityCache.OFF;
8868 } else {
8869 cache.scrollBar.setAlpha(Math.round(values[0]));
8870 }
Joe Malin32736f02011-01-19 16:14:20 -08008871
8872 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008873 // drawing. We only want this when we're fading so that
8874 // we prevent excessive redraws
8875 invalidate = true;
8876 } else {
8877 // We're just on -- but we may have been fading before so
8878 // reset alpha
8879 cache.scrollBar.setAlpha(255);
8880 }
8881
Joe Malin32736f02011-01-19 16:14:20 -08008882
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008883 final int viewFlags = mViewFlags;
8884
8885 final boolean drawHorizontalScrollBar =
8886 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8887 final boolean drawVerticalScrollBar =
8888 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8889 && !isVerticalScrollBarHidden();
8890
8891 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8892 final int width = mRight - mLeft;
8893 final int height = mBottom - mTop;
8894
8895 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008896
Mike Reede8853fc2009-09-04 14:01:48 -04008897 final int scrollX = mScrollX;
8898 final int scrollY = mScrollY;
8899 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8900
Mike Cleronf116bf82009-09-27 19:14:12 -07008901 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008902
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008903 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008904 int size = scrollBar.getSize(false);
8905 if (size <= 0) {
8906 size = cache.scrollBarSize;
8907 }
8908
Mike Cleronf116bf82009-09-27 19:14:12 -07008909 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008910 computeHorizontalScrollOffset(),
8911 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008912 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008913 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008914 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008915 left = scrollX + (mPaddingLeft & inside);
8916 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8917 bottom = top + size;
8918 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8919 if (invalidate) {
8920 invalidate(left, top, right, bottom);
8921 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008922 }
8923
8924 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008925 int size = scrollBar.getSize(true);
8926 if (size <= 0) {
8927 size = cache.scrollBarSize;
8928 }
8929
Mike Reede8853fc2009-09-04 14:01:48 -04008930 scrollBar.setParameters(computeVerticalScrollRange(),
8931 computeVerticalScrollOffset(),
8932 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008933 switch (mVerticalScrollbarPosition) {
8934 default:
8935 case SCROLLBAR_POSITION_DEFAULT:
8936 case SCROLLBAR_POSITION_RIGHT:
8937 left = scrollX + width - size - (mUserPaddingRight & inside);
8938 break;
8939 case SCROLLBAR_POSITION_LEFT:
8940 left = scrollX + (mUserPaddingLeft & inside);
8941 break;
8942 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008943 top = scrollY + (mPaddingTop & inside);
8944 right = left + size;
8945 bottom = scrollY + height - (mUserPaddingBottom & inside);
8946 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8947 if (invalidate) {
8948 invalidate(left, top, right, bottom);
8949 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008950 }
8951 }
8952 }
8953 }
Romain Guy8506ab42009-06-11 17:35:47 -07008954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008955 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008956 * 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 -08008957 * FastScroller is visible.
8958 * @return whether to temporarily hide the vertical scrollbar
8959 * @hide
8960 */
8961 protected boolean isVerticalScrollBarHidden() {
8962 return false;
8963 }
8964
8965 /**
8966 * <p>Draw the horizontal scrollbar if
8967 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8968 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008969 * @param canvas the canvas on which to draw the scrollbar
8970 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008971 *
8972 * @see #isHorizontalScrollBarEnabled()
8973 * @see #computeHorizontalScrollRange()
8974 * @see #computeHorizontalScrollExtent()
8975 * @see #computeHorizontalScrollOffset()
8976 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008977 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008978 */
Romain Guy8fb95422010-08-17 18:38:51 -07008979 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8980 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008981 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008982 scrollBar.draw(canvas);
8983 }
Mike Reede8853fc2009-09-04 14:01:48 -04008984
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008985 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008986 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8987 * returns true.</p>
8988 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 * @param canvas the canvas on which to draw the scrollbar
8990 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 *
8992 * @see #isVerticalScrollBarEnabled()
8993 * @see #computeVerticalScrollRange()
8994 * @see #computeVerticalScrollExtent()
8995 * @see #computeVerticalScrollOffset()
8996 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04008997 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008998 */
Romain Guy8fb95422010-08-17 18:38:51 -07008999 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9000 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009001 scrollBar.setBounds(l, t, r, b);
9002 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009003 }
9004
9005 /**
9006 * Implement this to do your drawing.
9007 *
9008 * @param canvas the canvas on which the background will be drawn
9009 */
9010 protected void onDraw(Canvas canvas) {
9011 }
9012
9013 /*
9014 * Caller is responsible for calling requestLayout if necessary.
9015 * (This allows addViewInLayout to not request a new layout.)
9016 */
9017 void assignParent(ViewParent parent) {
9018 if (mParent == null) {
9019 mParent = parent;
9020 } else if (parent == null) {
9021 mParent = null;
9022 } else {
9023 throw new RuntimeException("view " + this + " being added, but"
9024 + " it already has a parent");
9025 }
9026 }
9027
9028 /**
9029 * This is called when the view is attached to a window. At this point it
9030 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009031 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9032 * however it may be called any time before the first onDraw -- including
9033 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009034 *
9035 * @see #onDetachedFromWindow()
9036 */
9037 protected void onAttachedToWindow() {
9038 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9039 mParent.requestTransparentRegion(this);
9040 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009041 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9042 initialAwakenScrollBars();
9043 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9044 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009045 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009046 resolveLayoutDirectionIfNeeded();
9047 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009048 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009049 if (isFocused()) {
9050 InputMethodManager imm = InputMethodManager.peekInstance();
9051 imm.focusIn(this);
9052 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009053 }
Cibu Johny86666632010-02-22 13:01:02 -08009054
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009055 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009056 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9057 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009058 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009059 private void resolveLayoutDirectionIfNeeded() {
9060 // Do not resolve if it is not needed
9061 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9062
9063 // Clear any previous layout direction resolution
9064 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9065
9066 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009067 switch (getLayoutDirection()) {
9068 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08009069 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07009070 if (mParent != null &&
9071 mParent instanceof ViewGroup &&
9072 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009073 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009074 }
9075 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009076 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009077 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009078 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009079 case LAYOUT_DIRECTION_LOCALE:
9080 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009081 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009082 }
9083 break;
9084 default:
9085 // Nothing to do, LTR by default
9086 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009087
9088 // Set to resolved
9089 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9090 }
9091
9092 private void resolvePadding() {
9093 // If the user specified the absolute padding (either with android:padding or
9094 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9095 // use the default padding or the padding from the background drawable
9096 // (stored at this point in mPadding*)
9097 switch (getResolvedLayoutDirection()) {
9098 case LAYOUT_DIRECTION_RTL:
9099 // Start user padding override Right user padding. Otherwise, if Right user
9100 // padding is not defined, use the default Right padding. If Right user padding
9101 // is defined, just use it.
9102 if (mUserPaddingStart >= 0) {
9103 mUserPaddingRight = mUserPaddingStart;
9104 } else if (mUserPaddingRight < 0) {
9105 mUserPaddingRight = mPaddingRight;
9106 }
9107 if (mUserPaddingEnd >= 0) {
9108 mUserPaddingLeft = mUserPaddingEnd;
9109 } else if (mUserPaddingLeft < 0) {
9110 mUserPaddingLeft = mPaddingLeft;
9111 }
9112 break;
9113 case LAYOUT_DIRECTION_LTR:
9114 default:
9115 // Start user padding override Left user padding. Otherwise, if Left user
9116 // padding is not defined, use the default left padding. If Left user padding
9117 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009118 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009119 mUserPaddingLeft = mUserPaddingStart;
9120 } else if (mUserPaddingLeft < 0) {
9121 mUserPaddingLeft = mPaddingLeft;
9122 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009123 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009124 mUserPaddingRight = mUserPaddingEnd;
9125 } else if (mUserPaddingRight < 0) {
9126 mUserPaddingRight = mPaddingRight;
9127 }
9128 }
9129
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009130 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9131
9132 recomputePadding();
9133 }
9134
9135 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009136 * Reset the resolved layout direction.
9137 *
9138 * Subclasses need to override this method to clear cached information that depends on the
9139 * resolved layout direction, or to inform child views that inherit their layout direction.
9140 * Overrides must also call the superclass implementation at the start of their implementation.
9141 *
9142 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009143 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009144 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009145 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009146 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009147 }
9148
9149 /**
9150 * Check if a Locale is corresponding to a RTL script.
9151 *
9152 * @param locale Locale to check
9153 * @return true if a Locale is corresponding to a RTL script.
9154 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009155 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009156 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9157 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009158 }
9159
9160 /**
9161 * This is called when the view is detached from a window. At this point it
9162 * no longer has a surface for drawing.
9163 *
9164 * @see #onAttachedToWindow()
9165 */
9166 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009167 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009168
Romain Guya440b002010-02-24 15:57:54 -08009169 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009170 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009171 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009172 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009173
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009174 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009175
9176 if (mHardwareLayer != null) {
9177 mHardwareLayer.destroy();
9178 mHardwareLayer = null;
9179 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009180
Chet Haasedaf98e92011-01-10 14:10:36 -08009181 if (mDisplayList != null) {
9182 mDisplayList.invalidate();
9183 }
9184
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009185 if (mAttachInfo != null) {
9186 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9187 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9188 }
9189
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009190 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009191
9192 resetResolvedLayoutDirection();
9193 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009194 }
9195
9196 /**
9197 * @return The number of times this view has been attached to a window
9198 */
9199 protected int getWindowAttachCount() {
9200 return mWindowAttachCount;
9201 }
9202
9203 /**
9204 * Retrieve a unique token identifying the window this view is attached to.
9205 * @return Return the window's token for use in
9206 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9207 */
9208 public IBinder getWindowToken() {
9209 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9210 }
9211
9212 /**
9213 * Retrieve a unique token identifying the top-level "real" window of
9214 * the window that this view is attached to. That is, this is like
9215 * {@link #getWindowToken}, except if the window this view in is a panel
9216 * window (attached to another containing window), then the token of
9217 * the containing window is returned instead.
9218 *
9219 * @return Returns the associated window token, either
9220 * {@link #getWindowToken()} or the containing window's token.
9221 */
9222 public IBinder getApplicationWindowToken() {
9223 AttachInfo ai = mAttachInfo;
9224 if (ai != null) {
9225 IBinder appWindowToken = ai.mPanelParentWindowToken;
9226 if (appWindowToken == null) {
9227 appWindowToken = ai.mWindowToken;
9228 }
9229 return appWindowToken;
9230 }
9231 return null;
9232 }
9233
9234 /**
9235 * Retrieve private session object this view hierarchy is using to
9236 * communicate with the window manager.
9237 * @return the session object to communicate with the window manager
9238 */
9239 /*package*/ IWindowSession getWindowSession() {
9240 return mAttachInfo != null ? mAttachInfo.mSession : null;
9241 }
9242
9243 /**
9244 * @param info the {@link android.view.View.AttachInfo} to associated with
9245 * this view
9246 */
9247 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9248 //System.out.println("Attached! " + this);
9249 mAttachInfo = info;
9250 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009251 // We will need to evaluate the drawable state at least once.
9252 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009253 if (mFloatingTreeObserver != null) {
9254 info.mTreeObserver.merge(mFloatingTreeObserver);
9255 mFloatingTreeObserver = null;
9256 }
9257 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9258 mAttachInfo.mScrollContainers.add(this);
9259 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9260 }
9261 performCollectViewAttributes(visibility);
9262 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009263
9264 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9265 mOnAttachStateChangeListeners;
9266 if (listeners != null && listeners.size() > 0) {
9267 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9268 // perform the dispatching. The iterator is a safe guard against listeners that
9269 // could mutate the list by calling the various add/remove methods. This prevents
9270 // the array from being modified while we iterate it.
9271 for (OnAttachStateChangeListener listener : listeners) {
9272 listener.onViewAttachedToWindow(this);
9273 }
9274 }
9275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009276 int vis = info.mWindowVisibility;
9277 if (vis != GONE) {
9278 onWindowVisibilityChanged(vis);
9279 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009280 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9281 // If nobody has evaluated the drawable state yet, then do it now.
9282 refreshDrawableState();
9283 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009284 }
9285
9286 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009287 AttachInfo info = mAttachInfo;
9288 if (info != null) {
9289 int vis = info.mWindowVisibility;
9290 if (vis != GONE) {
9291 onWindowVisibilityChanged(GONE);
9292 }
9293 }
9294
9295 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009296
Adam Powell4afd62b2011-02-18 15:02:18 -08009297 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9298 mOnAttachStateChangeListeners;
9299 if (listeners != null && listeners.size() > 0) {
9300 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9301 // perform the dispatching. The iterator is a safe guard against listeners that
9302 // could mutate the list by calling the various add/remove methods. This prevents
9303 // the array from being modified while we iterate it.
9304 for (OnAttachStateChangeListener listener : listeners) {
9305 listener.onViewDetachedFromWindow(this);
9306 }
9307 }
9308
Romain Guy01d5edc2011-01-28 11:28:53 -08009309 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009310 mAttachInfo.mScrollContainers.remove(this);
9311 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9312 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009313
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009314 mAttachInfo = null;
9315 }
9316
9317 /**
9318 * Store this view hierarchy's frozen state into the given container.
9319 *
9320 * @param container The SparseArray in which to save the view's state.
9321 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009322 * @see #restoreHierarchyState(android.util.SparseArray)
9323 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9324 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009325 */
9326 public void saveHierarchyState(SparseArray<Parcelable> container) {
9327 dispatchSaveInstanceState(container);
9328 }
9329
9330 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009331 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9332 * this view and its children. May be overridden to modify how freezing happens to a
9333 * view's children; for example, some views may want to not store state for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009334 *
9335 * @param container The SparseArray in which to save the view's state.
9336 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009337 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9338 * @see #saveHierarchyState(android.util.SparseArray)
9339 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009340 */
9341 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9342 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9343 mPrivateFlags &= ~SAVE_STATE_CALLED;
9344 Parcelable state = onSaveInstanceState();
9345 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9346 throw new IllegalStateException(
9347 "Derived class did not call super.onSaveInstanceState()");
9348 }
9349 if (state != null) {
9350 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9351 // + ": " + state);
9352 container.put(mID, state);
9353 }
9354 }
9355 }
9356
9357 /**
9358 * Hook allowing a view to generate a representation of its internal state
9359 * that can later be used to create a new instance with that same state.
9360 * This state should only contain information that is not persistent or can
9361 * not be reconstructed later. For example, you will never store your
9362 * current position on screen because that will be computed again when a
9363 * new instance of the view is placed in its view hierarchy.
9364 * <p>
9365 * Some examples of things you may store here: the current cursor position
9366 * in a text view (but usually not the text itself since that is stored in a
9367 * content provider or other persistent storage), the currently selected
9368 * item in a list view.
9369 *
9370 * @return Returns a Parcelable object containing the view's current dynamic
9371 * state, or null if there is nothing interesting to save. The
9372 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009373 * @see #onRestoreInstanceState(android.os.Parcelable)
9374 * @see #saveHierarchyState(android.util.SparseArray)
9375 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009376 * @see #setSaveEnabled(boolean)
9377 */
9378 protected Parcelable onSaveInstanceState() {
9379 mPrivateFlags |= SAVE_STATE_CALLED;
9380 return BaseSavedState.EMPTY_STATE;
9381 }
9382
9383 /**
9384 * Restore this view hierarchy's frozen state from the given container.
9385 *
9386 * @param container The SparseArray which holds previously frozen states.
9387 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009388 * @see #saveHierarchyState(android.util.SparseArray)
9389 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9390 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009391 */
9392 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9393 dispatchRestoreInstanceState(container);
9394 }
9395
9396 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009397 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9398 * state for this view and its children. May be overridden to modify how restoring
9399 * happens to a view's children; for example, some views may want to not store state
9400 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009401 *
9402 * @param container The SparseArray which holds previously saved state.
9403 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009404 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9405 * @see #restoreHierarchyState(android.util.SparseArray)
9406 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 */
9408 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9409 if (mID != NO_ID) {
9410 Parcelable state = container.get(mID);
9411 if (state != null) {
9412 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9413 // + ": " + state);
9414 mPrivateFlags &= ~SAVE_STATE_CALLED;
9415 onRestoreInstanceState(state);
9416 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9417 throw new IllegalStateException(
9418 "Derived class did not call super.onRestoreInstanceState()");
9419 }
9420 }
9421 }
9422 }
9423
9424 /**
9425 * Hook allowing a view to re-apply a representation of its internal state that had previously
9426 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9427 * null state.
9428 *
9429 * @param state The frozen state that had previously been returned by
9430 * {@link #onSaveInstanceState}.
9431 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009432 * @see #onSaveInstanceState()
9433 * @see #restoreHierarchyState(android.util.SparseArray)
9434 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009435 */
9436 protected void onRestoreInstanceState(Parcelable state) {
9437 mPrivateFlags |= SAVE_STATE_CALLED;
9438 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009439 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9440 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009441 + "when two views of different type have the same id in the same hierarchy. "
9442 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009443 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009444 }
9445 }
9446
9447 /**
9448 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9449 *
9450 * @return the drawing start time in milliseconds
9451 */
9452 public long getDrawingTime() {
9453 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9454 }
9455
9456 /**
9457 * <p>Enables or disables the duplication of the parent's state into this view. When
9458 * duplication is enabled, this view gets its drawable state from its parent rather
9459 * than from its own internal properties.</p>
9460 *
9461 * <p>Note: in the current implementation, setting this property to true after the
9462 * view was added to a ViewGroup might have no effect at all. This property should
9463 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9464 *
9465 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9466 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009467 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009468 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9469 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009470 *
9471 * @param enabled True to enable duplication of the parent's drawable state, false
9472 * to disable it.
9473 *
9474 * @see #getDrawableState()
9475 * @see #isDuplicateParentStateEnabled()
9476 */
9477 public void setDuplicateParentStateEnabled(boolean enabled) {
9478 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9479 }
9480
9481 /**
9482 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9483 *
9484 * @return True if this view's drawable state is duplicated from the parent,
9485 * false otherwise
9486 *
9487 * @see #getDrawableState()
9488 * @see #setDuplicateParentStateEnabled(boolean)
9489 */
9490 public boolean isDuplicateParentStateEnabled() {
9491 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9492 }
9493
9494 /**
Romain Guy171c5922011-01-06 10:04:23 -08009495 * <p>Specifies the type of layer backing this view. The layer can be
9496 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9497 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009498 *
Romain Guy171c5922011-01-06 10:04:23 -08009499 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9500 * instance that controls how the layer is composed on screen. The following
9501 * properties of the paint are taken into account when composing the layer:</p>
9502 * <ul>
9503 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9504 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9505 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9506 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009507 *
Romain Guy171c5922011-01-06 10:04:23 -08009508 * <p>If this view has an alpha value set to < 1.0 by calling
9509 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9510 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9511 * equivalent to setting a hardware layer on this view and providing a paint with
9512 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009513 *
Romain Guy171c5922011-01-06 10:04:23 -08009514 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9515 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9516 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009517 *
Romain Guy171c5922011-01-06 10:04:23 -08009518 * @param layerType The ype of layer to use with this view, must be one of
9519 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9520 * {@link #LAYER_TYPE_HARDWARE}
9521 * @param paint The paint used to compose the layer. This argument is optional
9522 * and can be null. It is ignored when the layer type is
9523 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009524 *
9525 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009526 * @see #LAYER_TYPE_NONE
9527 * @see #LAYER_TYPE_SOFTWARE
9528 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009529 * @see #setAlpha(float)
9530 *
Romain Guy171c5922011-01-06 10:04:23 -08009531 * @attr ref android.R.styleable#View_layerType
9532 */
9533 public void setLayerType(int layerType, Paint paint) {
9534 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009535 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009536 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9537 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009538
Romain Guyd6cd5722011-01-17 14:42:41 -08009539 if (layerType == mLayerType) {
9540 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9541 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009542 invalidateParentCaches();
9543 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009544 }
9545 return;
9546 }
Romain Guy171c5922011-01-06 10:04:23 -08009547
9548 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009549 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009550 case LAYER_TYPE_HARDWARE:
9551 if (mHardwareLayer != null) {
9552 mHardwareLayer.destroy();
9553 mHardwareLayer = null;
9554 }
9555 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009556 case LAYER_TYPE_SOFTWARE:
9557 if (mDrawingCache != null) {
9558 mDrawingCache.recycle();
9559 mDrawingCache = null;
9560 }
Joe Malin32736f02011-01-19 16:14:20 -08009561
Romain Guy6c319ca2011-01-11 14:29:25 -08009562 if (mUnscaledDrawingCache != null) {
9563 mUnscaledDrawingCache.recycle();
9564 mUnscaledDrawingCache = null;
9565 }
9566 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009567 default:
9568 break;
Romain Guy171c5922011-01-06 10:04:23 -08009569 }
9570
9571 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009572 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9573 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9574 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009575
Romain Guy0fd89bf2011-01-26 15:41:30 -08009576 invalidateParentCaches();
9577 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009578 }
9579
9580 /**
9581 * Indicates what type of layer is currently associated with this view. By default
9582 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9583 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9584 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009585 *
Romain Guy171c5922011-01-06 10:04:23 -08009586 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9587 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009588 *
9589 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009590 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009591 * @see #LAYER_TYPE_NONE
9592 * @see #LAYER_TYPE_SOFTWARE
9593 * @see #LAYER_TYPE_HARDWARE
9594 */
9595 public int getLayerType() {
9596 return mLayerType;
9597 }
Joe Malin32736f02011-01-19 16:14:20 -08009598
Romain Guy6c319ca2011-01-11 14:29:25 -08009599 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009600 * Forces this view's layer to be created and this view to be rendered
9601 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9602 * invoking this method will have no effect.
9603 *
9604 * This method can for instance be used to render a view into its layer before
9605 * starting an animation. If this view is complex, rendering into the layer
9606 * before starting the animation will avoid skipping frames.
9607 *
9608 * @throws IllegalStateException If this view is not attached to a window
9609 *
9610 * @see #setLayerType(int, android.graphics.Paint)
9611 */
9612 public void buildLayer() {
9613 if (mLayerType == LAYER_TYPE_NONE) return;
9614
9615 if (mAttachInfo == null) {
9616 throw new IllegalStateException("This view must be attached to a window first");
9617 }
9618
9619 switch (mLayerType) {
9620 case LAYER_TYPE_HARDWARE:
9621 getHardwareLayer();
9622 break;
9623 case LAYER_TYPE_SOFTWARE:
9624 buildDrawingCache(true);
9625 break;
9626 }
9627 }
9628
9629 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009630 * <p>Returns a hardware layer that can be used to draw this view again
9631 * without executing its draw method.</p>
9632 *
9633 * @return A HardwareLayer ready to render, or null if an error occurred.
9634 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009635 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009636 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9637 return null;
9638 }
9639
9640 final int width = mRight - mLeft;
9641 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009642
Romain Guy6c319ca2011-01-11 14:29:25 -08009643 if (width == 0 || height == 0) {
9644 return null;
9645 }
9646
9647 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9648 if (mHardwareLayer == null) {
9649 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9650 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009651 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009652 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9653 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009654 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009655 }
9656
Romain Guy59a12ca2011-06-09 17:48:21 -07009657 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009658 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9659 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009660 try {
9661 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009662 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009663 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009664
Chet Haase88172fe2011-03-07 17:36:33 -08009665 final int restoreCount = canvas.save();
9666
Romain Guy6c319ca2011-01-11 14:29:25 -08009667 computeScroll();
9668 canvas.translate(-mScrollX, -mScrollY);
9669
Romain Guy6c319ca2011-01-11 14:29:25 -08009670 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009671
Romain Guy6c319ca2011-01-11 14:29:25 -08009672 // Fast path for layouts with no backgrounds
9673 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9674 mPrivateFlags &= ~DIRTY_MASK;
9675 dispatchDraw(canvas);
9676 } else {
9677 draw(canvas);
9678 }
Joe Malin32736f02011-01-19 16:14:20 -08009679
Chet Haase88172fe2011-03-07 17:36:33 -08009680 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009681 } finally {
9682 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009683 mHardwareLayer.end(currentCanvas);
9684 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009685 }
9686 }
9687
9688 return mHardwareLayer;
9689 }
Romain Guy171c5922011-01-06 10:04:23 -08009690
9691 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009692 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9693 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9694 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9695 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9696 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9697 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009698 *
Romain Guy171c5922011-01-06 10:04:23 -08009699 * <p>Enabling the drawing cache is similar to
9700 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009701 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9702 * drawing cache has no effect on rendering because the system uses a different mechanism
9703 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9704 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9705 * for information on how to enable software and hardware layers.</p>
9706 *
9707 * <p>This API can be used to manually generate
9708 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9709 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009710 *
9711 * @param enabled true to enable the drawing cache, false otherwise
9712 *
9713 * @see #isDrawingCacheEnabled()
9714 * @see #getDrawingCache()
9715 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009716 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009717 */
9718 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009719 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009720 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9721 }
9722
9723 /**
9724 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9725 *
9726 * @return true if the drawing cache is enabled
9727 *
9728 * @see #setDrawingCacheEnabled(boolean)
9729 * @see #getDrawingCache()
9730 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009731 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009732 public boolean isDrawingCacheEnabled() {
9733 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9734 }
9735
9736 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009737 * Debugging utility which recursively outputs the dirty state of a view and its
9738 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009739 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009740 * @hide
9741 */
Romain Guy676b1732011-02-14 14:45:33 -08009742 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009743 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9744 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9745 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9746 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9747 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9748 if (clear) {
9749 mPrivateFlags &= clearMask;
9750 }
9751 if (this instanceof ViewGroup) {
9752 ViewGroup parent = (ViewGroup) this;
9753 final int count = parent.getChildCount();
9754 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009755 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009756 child.outputDirtyFlags(indent + " ", clear, clearMask);
9757 }
9758 }
9759 }
9760
9761 /**
9762 * This method is used by ViewGroup to cause its children to restore or recreate their
9763 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9764 * to recreate its own display list, which would happen if it went through the normal
9765 * draw/dispatchDraw mechanisms.
9766 *
9767 * @hide
9768 */
9769 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009770
9771 /**
9772 * A view that is not attached or hardware accelerated cannot create a display list.
9773 * This method checks these conditions and returns the appropriate result.
9774 *
9775 * @return true if view has the ability to create a display list, false otherwise.
9776 *
9777 * @hide
9778 */
9779 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009780 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009781 }
Joe Malin32736f02011-01-19 16:14:20 -08009782
Chet Haasedaf98e92011-01-10 14:10:36 -08009783 /**
Romain Guyb051e892010-09-28 19:09:36 -07009784 * <p>Returns a display list that can be used to draw this view again
9785 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009786 *
Romain Guyb051e892010-09-28 19:09:36 -07009787 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009788 *
9789 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009790 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009791 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009792 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009793 return null;
9794 }
9795
Chet Haasedaf98e92011-01-10 14:10:36 -08009796 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9797 mDisplayList == null || !mDisplayList.isValid() ||
9798 mRecreateDisplayList)) {
9799 // Don't need to recreate the display list, just need to tell our
9800 // children to restore/recreate theirs
9801 if (mDisplayList != null && mDisplayList.isValid() &&
9802 !mRecreateDisplayList) {
9803 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9804 mPrivateFlags &= ~DIRTY_MASK;
9805 dispatchGetDisplayList();
9806
9807 return mDisplayList;
9808 }
9809
9810 // If we got here, we're recreating it. Mark it as such to ensure that
9811 // we copy in child display lists into ours in drawChild()
9812 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009813 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009814 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9815 // If we're creating a new display list, make sure our parent gets invalidated
9816 // since they will need to recreate their display list to account for this
9817 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009818 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009819 }
Romain Guyb051e892010-09-28 19:09:36 -07009820
9821 final HardwareCanvas canvas = mDisplayList.start();
9822 try {
9823 int width = mRight - mLeft;
9824 int height = mBottom - mTop;
9825
9826 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009827 // The dirty rect should always be null for a display list
9828 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009829
Chet Haase88172fe2011-03-07 17:36:33 -08009830 final int restoreCount = canvas.save();
9831
Chet Haasedaf98e92011-01-10 14:10:36 -08009832 computeScroll();
9833 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009834 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009835 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009836
Romain Guyb051e892010-09-28 19:09:36 -07009837 // Fast path for layouts with no backgrounds
9838 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009839 dispatchDraw(canvas);
9840 } else {
9841 draw(canvas);
9842 }
Joe Malin32736f02011-01-19 16:14:20 -08009843
Chet Haase88172fe2011-03-07 17:36:33 -08009844 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009845 } finally {
9846 canvas.onPostDraw();
9847
9848 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009849 }
Chet Haase77785f92011-01-25 23:22:09 -08009850 } else {
9851 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9852 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009853 }
9854
9855 return mDisplayList;
9856 }
9857
9858 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009859 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009860 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009861 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009862 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009863 * @see #getDrawingCache(boolean)
9864 */
9865 public Bitmap getDrawingCache() {
9866 return getDrawingCache(false);
9867 }
9868
9869 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009870 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9871 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9872 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9873 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9874 * request the drawing cache by calling this method and draw it on screen if the
9875 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009876 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009877 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9878 * this method will create a bitmap of the same size as this view. Because this bitmap
9879 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9880 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9881 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9882 * size than the view. This implies that your application must be able to handle this
9883 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009884 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009885 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9886 * the current density of the screen when the application is in compatibility
9887 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009888 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009889 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009890 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009891 * @see #setDrawingCacheEnabled(boolean)
9892 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009893 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 * @see #destroyDrawingCache()
9895 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009896 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9898 return null;
9899 }
9900 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009901 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009902 }
Romain Guy02890fd2010-08-06 17:58:44 -07009903 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009904 }
9905
9906 /**
9907 * <p>Frees the resources used by the drawing cache. If you call
9908 * {@link #buildDrawingCache()} manually without calling
9909 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9910 * should cleanup the cache with this method afterwards.</p>
9911 *
9912 * @see #setDrawingCacheEnabled(boolean)
9913 * @see #buildDrawingCache()
9914 * @see #getDrawingCache()
9915 */
9916 public void destroyDrawingCache() {
9917 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009918 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009919 mDrawingCache = null;
9920 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009921 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009922 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009923 mUnscaledDrawingCache = null;
9924 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 }
9926
9927 /**
9928 * Setting a solid background color for the drawing cache's bitmaps will improve
9929 * perfromance and memory usage. Note, though that this should only be used if this
9930 * view will always be drawn on top of a solid color.
9931 *
9932 * @param color The background color to use for the drawing cache's bitmap
9933 *
9934 * @see #setDrawingCacheEnabled(boolean)
9935 * @see #buildDrawingCache()
9936 * @see #getDrawingCache()
9937 */
9938 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009939 if (color != mDrawingCacheBackgroundColor) {
9940 mDrawingCacheBackgroundColor = color;
9941 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9942 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009943 }
9944
9945 /**
9946 * @see #setDrawingCacheBackgroundColor(int)
9947 *
9948 * @return The background color to used for the drawing cache's bitmap
9949 */
9950 public int getDrawingCacheBackgroundColor() {
9951 return mDrawingCacheBackgroundColor;
9952 }
9953
9954 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009955 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009956 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009957 * @see #buildDrawingCache(boolean)
9958 */
9959 public void buildDrawingCache() {
9960 buildDrawingCache(false);
9961 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009962
Romain Guyfbd8f692009-06-26 14:51:58 -07009963 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009964 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9965 *
9966 * <p>If you call {@link #buildDrawingCache()} manually without calling
9967 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9968 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009969 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009970 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9971 * this method will create a bitmap of the same size as this view. Because this bitmap
9972 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9973 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9974 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9975 * size than the view. This implies that your application must be able to handle this
9976 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009977 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009978 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9979 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009980 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009981 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009982 *
9983 * @see #getDrawingCache()
9984 * @see #destroyDrawingCache()
9985 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009986 public void buildDrawingCache(boolean autoScale) {
9987 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009988 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009989 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009990
9991 if (ViewDebug.TRACE_HIERARCHY) {
9992 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9993 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009994
Romain Guy8506ab42009-06-11 17:35:47 -07009995 int width = mRight - mLeft;
9996 int height = mBottom - mTop;
9997
9998 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -07009999 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010000
Romain Guye1123222009-06-29 14:24:56 -070010001 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010002 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10003 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010004 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010005
10006 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010007 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010008 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010009
10010 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010011 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010012 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010013 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10014 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010015 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010016 return;
10017 }
10018
10019 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010020 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010021
10022 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010023 Bitmap.Config quality;
10024 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010025 // Never pick ARGB_4444 because it looks awful
10026 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10028 case DRAWING_CACHE_QUALITY_AUTO:
10029 quality = Bitmap.Config.ARGB_8888;
10030 break;
10031 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010032 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 break;
10034 case DRAWING_CACHE_QUALITY_HIGH:
10035 quality = Bitmap.Config.ARGB_8888;
10036 break;
10037 default:
10038 quality = Bitmap.Config.ARGB_8888;
10039 break;
10040 }
10041 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010042 // Optimization for translucent windows
10043 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010044 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010045 }
10046
10047 // Try to cleanup memory
10048 if (bitmap != null) bitmap.recycle();
10049
10050 try {
10051 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010052 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010053 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010054 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010055 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010056 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010057 }
Adam Powell26153a32010-11-08 15:22:27 -080010058 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010059 } catch (OutOfMemoryError e) {
10060 // If there is not enough memory to create the bitmap cache, just
10061 // ignore the issue as bitmap caches are not required to draw the
10062 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010063 if (autoScale) {
10064 mDrawingCache = null;
10065 } else {
10066 mUnscaledDrawingCache = null;
10067 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010068 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 return;
10070 }
10071
10072 clear = drawingCacheBackgroundColor != 0;
10073 }
10074
10075 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 if (attachInfo != null) {
10077 canvas = attachInfo.mCanvas;
10078 if (canvas == null) {
10079 canvas = new Canvas();
10080 }
10081 canvas.setBitmap(bitmap);
10082 // Temporarily clobber the cached Canvas in case one of our children
10083 // is also using a drawing cache. Without this, the children would
10084 // steal the canvas by attaching their own bitmap to it and bad, bad
10085 // thing would happen (invisible views, corrupted drawings, etc.)
10086 attachInfo.mCanvas = null;
10087 } else {
10088 // This case should hopefully never or seldom happen
10089 canvas = new Canvas(bitmap);
10090 }
10091
10092 if (clear) {
10093 bitmap.eraseColor(drawingCacheBackgroundColor);
10094 }
10095
10096 computeScroll();
10097 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010098
Romain Guye1123222009-06-29 14:24:56 -070010099 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010100 final float scale = attachInfo.mApplicationScale;
10101 canvas.scale(scale, scale);
10102 }
Joe Malin32736f02011-01-19 16:14:20 -080010103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010104 canvas.translate(-mScrollX, -mScrollY);
10105
Romain Guy5bcdff42009-05-14 21:27:18 -070010106 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010107 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10108 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010109 mPrivateFlags |= DRAWING_CACHE_VALID;
10110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010111
10112 // Fast path for layouts with no backgrounds
10113 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10114 if (ViewDebug.TRACE_HIERARCHY) {
10115 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10116 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010117 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010118 dispatchDraw(canvas);
10119 } else {
10120 draw(canvas);
10121 }
10122
10123 canvas.restoreToCount(restoreCount);
10124
10125 if (attachInfo != null) {
10126 // Restore the cached Canvas for our siblings
10127 attachInfo.mCanvas = canvas;
10128 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010129 }
10130 }
10131
10132 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010133 * Create a snapshot of the view into a bitmap. We should probably make
10134 * some form of this public, but should think about the API.
10135 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010136 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010137 int width = mRight - mLeft;
10138 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010139
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010140 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010141 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010142 width = (int) ((width * scale) + 0.5f);
10143 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010144
Romain Guy8c11e312009-09-14 15:15:30 -070010145 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010146 if (bitmap == null) {
10147 throw new OutOfMemoryError();
10148 }
10149
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010150 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010151
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010152 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010153 if (attachInfo != null) {
10154 canvas = attachInfo.mCanvas;
10155 if (canvas == null) {
10156 canvas = new Canvas();
10157 }
10158 canvas.setBitmap(bitmap);
10159 // Temporarily clobber the cached Canvas in case one of our children
10160 // is also using a drawing cache. Without this, the children would
10161 // steal the canvas by attaching their own bitmap to it and bad, bad
10162 // things would happen (invisible views, corrupted drawings, etc.)
10163 attachInfo.mCanvas = null;
10164 } else {
10165 // This case should hopefully never or seldom happen
10166 canvas = new Canvas(bitmap);
10167 }
10168
Romain Guy5bcdff42009-05-14 21:27:18 -070010169 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010170 bitmap.eraseColor(backgroundColor);
10171 }
10172
10173 computeScroll();
10174 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010175 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010176 canvas.translate(-mScrollX, -mScrollY);
10177
Romain Guy5bcdff42009-05-14 21:27:18 -070010178 // Temporarily remove the dirty mask
10179 int flags = mPrivateFlags;
10180 mPrivateFlags &= ~DIRTY_MASK;
10181
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010182 // Fast path for layouts with no backgrounds
10183 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10184 dispatchDraw(canvas);
10185 } else {
10186 draw(canvas);
10187 }
10188
Romain Guy5bcdff42009-05-14 21:27:18 -070010189 mPrivateFlags = flags;
10190
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010191 canvas.restoreToCount(restoreCount);
10192
10193 if (attachInfo != null) {
10194 // Restore the cached Canvas for our siblings
10195 attachInfo.mCanvas = canvas;
10196 }
Romain Guy8506ab42009-06-11 17:35:47 -070010197
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010198 return bitmap;
10199 }
10200
10201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010202 * Indicates whether this View is currently in edit mode. A View is usually
10203 * in edit mode when displayed within a developer tool. For instance, if
10204 * this View is being drawn by a visual user interface builder, this method
10205 * should return true.
10206 *
10207 * Subclasses should check the return value of this method to provide
10208 * different behaviors if their normal behavior might interfere with the
10209 * host environment. For instance: the class spawns a thread in its
10210 * constructor, the drawing code relies on device-specific features, etc.
10211 *
10212 * This method is usually checked in the drawing code of custom widgets.
10213 *
10214 * @return True if this View is in edit mode, false otherwise.
10215 */
10216 public boolean isInEditMode() {
10217 return false;
10218 }
10219
10220 /**
10221 * If the View draws content inside its padding and enables fading edges,
10222 * it needs to support padding offsets. Padding offsets are added to the
10223 * fading edges to extend the length of the fade so that it covers pixels
10224 * drawn inside the padding.
10225 *
10226 * Subclasses of this class should override this method if they need
10227 * to draw content inside the padding.
10228 *
10229 * @return True if padding offset must be applied, false otherwise.
10230 *
10231 * @see #getLeftPaddingOffset()
10232 * @see #getRightPaddingOffset()
10233 * @see #getTopPaddingOffset()
10234 * @see #getBottomPaddingOffset()
10235 *
10236 * @since CURRENT
10237 */
10238 protected boolean isPaddingOffsetRequired() {
10239 return false;
10240 }
10241
10242 /**
10243 * Amount by which to extend the left fading region. Called only when
10244 * {@link #isPaddingOffsetRequired()} returns true.
10245 *
10246 * @return The left padding offset in pixels.
10247 *
10248 * @see #isPaddingOffsetRequired()
10249 *
10250 * @since CURRENT
10251 */
10252 protected int getLeftPaddingOffset() {
10253 return 0;
10254 }
10255
10256 /**
10257 * Amount by which to extend the right fading region. Called only when
10258 * {@link #isPaddingOffsetRequired()} returns true.
10259 *
10260 * @return The right padding offset in pixels.
10261 *
10262 * @see #isPaddingOffsetRequired()
10263 *
10264 * @since CURRENT
10265 */
10266 protected int getRightPaddingOffset() {
10267 return 0;
10268 }
10269
10270 /**
10271 * Amount by which to extend the top fading region. Called only when
10272 * {@link #isPaddingOffsetRequired()} returns true.
10273 *
10274 * @return The top padding offset in pixels.
10275 *
10276 * @see #isPaddingOffsetRequired()
10277 *
10278 * @since CURRENT
10279 */
10280 protected int getTopPaddingOffset() {
10281 return 0;
10282 }
10283
10284 /**
10285 * Amount by which to extend the bottom fading region. Called only when
10286 * {@link #isPaddingOffsetRequired()} returns true.
10287 *
10288 * @return The bottom padding offset in pixels.
10289 *
10290 * @see #isPaddingOffsetRequired()
10291 *
10292 * @since CURRENT
10293 */
10294 protected int getBottomPaddingOffset() {
10295 return 0;
10296 }
10297
10298 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010299 * <p>Indicates whether this view is attached to an hardware accelerated
10300 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010301 *
Romain Guy2bffd262010-09-12 17:40:02 -070010302 * <p>Even if this method returns true, it does not mean that every call
10303 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10304 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10305 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10306 * window is hardware accelerated,
10307 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10308 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010309 *
Romain Guy2bffd262010-09-12 17:40:02 -070010310 * @return True if the view is attached to a window and the window is
10311 * hardware accelerated; false in any other case.
10312 */
10313 public boolean isHardwareAccelerated() {
10314 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10315 }
Joe Malin32736f02011-01-19 16:14:20 -080010316
Romain Guy2bffd262010-09-12 17:40:02 -070010317 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010318 * Manually render this view (and all of its children) to the given Canvas.
10319 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010320 * called. When implementing a view, implement
10321 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10322 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010323 *
10324 * @param canvas The Canvas to which the View is rendered.
10325 */
10326 public void draw(Canvas canvas) {
10327 if (ViewDebug.TRACE_HIERARCHY) {
10328 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10329 }
10330
Romain Guy5bcdff42009-05-14 21:27:18 -070010331 final int privateFlags = mPrivateFlags;
10332 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10333 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10334 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010336 /*
10337 * Draw traversal performs several drawing steps which must be executed
10338 * in the appropriate order:
10339 *
10340 * 1. Draw the background
10341 * 2. If necessary, save the canvas' layers to prepare for fading
10342 * 3. Draw view's content
10343 * 4. Draw children
10344 * 5. If necessary, draw the fading edges and restore layers
10345 * 6. Draw decorations (scrollbars for instance)
10346 */
10347
10348 // Step 1, draw the background, if needed
10349 int saveCount;
10350
Romain Guy24443ea2009-05-11 11:56:30 -070010351 if (!dirtyOpaque) {
10352 final Drawable background = mBGDrawable;
10353 if (background != null) {
10354 final int scrollX = mScrollX;
10355 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010356
Romain Guy24443ea2009-05-11 11:56:30 -070010357 if (mBackgroundSizeChanged) {
10358 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10359 mBackgroundSizeChanged = false;
10360 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010361
Romain Guy24443ea2009-05-11 11:56:30 -070010362 if ((scrollX | scrollY) == 0) {
10363 background.draw(canvas);
10364 } else {
10365 canvas.translate(scrollX, scrollY);
10366 background.draw(canvas);
10367 canvas.translate(-scrollX, -scrollY);
10368 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010369 }
10370 }
10371
10372 // skip step 2 & 5 if possible (common case)
10373 final int viewFlags = mViewFlags;
10374 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10375 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10376 if (!verticalEdges && !horizontalEdges) {
10377 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010378 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379
10380 // Step 4, draw the children
10381 dispatchDraw(canvas);
10382
10383 // Step 6, draw decorations (scrollbars)
10384 onDrawScrollBars(canvas);
10385
10386 // we're done...
10387 return;
10388 }
10389
10390 /*
10391 * Here we do the full fledged routine...
10392 * (this is an uncommon case where speed matters less,
10393 * this is why we repeat some of the tests that have been
10394 * done above)
10395 */
10396
10397 boolean drawTop = false;
10398 boolean drawBottom = false;
10399 boolean drawLeft = false;
10400 boolean drawRight = false;
10401
10402 float topFadeStrength = 0.0f;
10403 float bottomFadeStrength = 0.0f;
10404 float leftFadeStrength = 0.0f;
10405 float rightFadeStrength = 0.0f;
10406
10407 // Step 2, save the canvas' layers
10408 int paddingLeft = mPaddingLeft;
10409 int paddingTop = mPaddingTop;
10410
10411 final boolean offsetRequired = isPaddingOffsetRequired();
10412 if (offsetRequired) {
10413 paddingLeft += getLeftPaddingOffset();
10414 paddingTop += getTopPaddingOffset();
10415 }
10416
10417 int left = mScrollX + paddingLeft;
10418 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10419 int top = mScrollY + paddingTop;
10420 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10421
10422 if (offsetRequired) {
10423 right += getRightPaddingOffset();
10424 bottom += getBottomPaddingOffset();
10425 }
10426
10427 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010428 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10429 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010430
10431 // clip the fade length if top and bottom fades overlap
10432 // overlapping fades produce odd-looking artifacts
10433 if (verticalEdges && (top + length > bottom - length)) {
10434 length = (bottom - top) / 2;
10435 }
10436
10437 // also clip horizontal fades if necessary
10438 if (horizontalEdges && (left + length > right - length)) {
10439 length = (right - left) / 2;
10440 }
10441
10442 if (verticalEdges) {
10443 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010444 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010445 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010446 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010447 }
10448
10449 if (horizontalEdges) {
10450 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010451 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010452 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010453 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010454 }
10455
10456 saveCount = canvas.getSaveCount();
10457
10458 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010459 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10461
10462 if (drawTop) {
10463 canvas.saveLayer(left, top, right, top + length, null, flags);
10464 }
10465
10466 if (drawBottom) {
10467 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10468 }
10469
10470 if (drawLeft) {
10471 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10472 }
10473
10474 if (drawRight) {
10475 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10476 }
10477 } else {
10478 scrollabilityCache.setFadeColor(solidColor);
10479 }
10480
10481 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010482 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010483
10484 // Step 4, draw the children
10485 dispatchDraw(canvas);
10486
10487 // Step 5, draw the fade effect and restore layers
10488 final Paint p = scrollabilityCache.paint;
10489 final Matrix matrix = scrollabilityCache.matrix;
10490 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010491
10492 if (drawTop) {
10493 matrix.setScale(1, fadeHeight * topFadeStrength);
10494 matrix.postTranslate(left, top);
10495 fade.setLocalMatrix(matrix);
10496 canvas.drawRect(left, top, right, top + length, p);
10497 }
10498
10499 if (drawBottom) {
10500 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10501 matrix.postRotate(180);
10502 matrix.postTranslate(left, bottom);
10503 fade.setLocalMatrix(matrix);
10504 canvas.drawRect(left, bottom - length, right, bottom, p);
10505 }
10506
10507 if (drawLeft) {
10508 matrix.setScale(1, fadeHeight * leftFadeStrength);
10509 matrix.postRotate(-90);
10510 matrix.postTranslate(left, top);
10511 fade.setLocalMatrix(matrix);
10512 canvas.drawRect(left, top, left + length, bottom, p);
10513 }
10514
10515 if (drawRight) {
10516 matrix.setScale(1, fadeHeight * rightFadeStrength);
10517 matrix.postRotate(90);
10518 matrix.postTranslate(right, top);
10519 fade.setLocalMatrix(matrix);
10520 canvas.drawRect(right - length, top, right, bottom, p);
10521 }
10522
10523 canvas.restoreToCount(saveCount);
10524
10525 // Step 6, draw decorations (scrollbars)
10526 onDrawScrollBars(canvas);
10527 }
10528
10529 /**
10530 * Override this if your view is known to always be drawn on top of a solid color background,
10531 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10532 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10533 * should be set to 0xFF.
10534 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010535 * @see #setVerticalFadingEdgeEnabled(boolean)
10536 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010537 *
10538 * @return The known solid color background for this view, or 0 if the color may vary
10539 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010540 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010541 public int getSolidColor() {
10542 return 0;
10543 }
10544
10545 /**
10546 * Build a human readable string representation of the specified view flags.
10547 *
10548 * @param flags the view flags to convert to a string
10549 * @return a String representing the supplied flags
10550 */
10551 private static String printFlags(int flags) {
10552 String output = "";
10553 int numFlags = 0;
10554 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10555 output += "TAKES_FOCUS";
10556 numFlags++;
10557 }
10558
10559 switch (flags & VISIBILITY_MASK) {
10560 case INVISIBLE:
10561 if (numFlags > 0) {
10562 output += " ";
10563 }
10564 output += "INVISIBLE";
10565 // USELESS HERE numFlags++;
10566 break;
10567 case GONE:
10568 if (numFlags > 0) {
10569 output += " ";
10570 }
10571 output += "GONE";
10572 // USELESS HERE numFlags++;
10573 break;
10574 default:
10575 break;
10576 }
10577 return output;
10578 }
10579
10580 /**
10581 * Build a human readable string representation of the specified private
10582 * view flags.
10583 *
10584 * @param privateFlags the private view flags to convert to a string
10585 * @return a String representing the supplied flags
10586 */
10587 private static String printPrivateFlags(int privateFlags) {
10588 String output = "";
10589 int numFlags = 0;
10590
10591 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10592 output += "WANTS_FOCUS";
10593 numFlags++;
10594 }
10595
10596 if ((privateFlags & FOCUSED) == FOCUSED) {
10597 if (numFlags > 0) {
10598 output += " ";
10599 }
10600 output += "FOCUSED";
10601 numFlags++;
10602 }
10603
10604 if ((privateFlags & SELECTED) == SELECTED) {
10605 if (numFlags > 0) {
10606 output += " ";
10607 }
10608 output += "SELECTED";
10609 numFlags++;
10610 }
10611
10612 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10613 if (numFlags > 0) {
10614 output += " ";
10615 }
10616 output += "IS_ROOT_NAMESPACE";
10617 numFlags++;
10618 }
10619
10620 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10621 if (numFlags > 0) {
10622 output += " ";
10623 }
10624 output += "HAS_BOUNDS";
10625 numFlags++;
10626 }
10627
10628 if ((privateFlags & DRAWN) == DRAWN) {
10629 if (numFlags > 0) {
10630 output += " ";
10631 }
10632 output += "DRAWN";
10633 // USELESS HERE numFlags++;
10634 }
10635 return output;
10636 }
10637
10638 /**
10639 * <p>Indicates whether or not this view's layout will be requested during
10640 * the next hierarchy layout pass.</p>
10641 *
10642 * @return true if the layout will be forced during next layout pass
10643 */
10644 public boolean isLayoutRequested() {
10645 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10646 }
10647
10648 /**
10649 * Assign a size and position to a view and all of its
10650 * descendants
10651 *
10652 * <p>This is the second phase of the layout mechanism.
10653 * (The first is measuring). In this phase, each parent calls
10654 * layout on all of its children to position them.
10655 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010656 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010657 *
Chet Haase9c087442011-01-12 16:20:16 -080010658 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010659 * Derived classes with children should override
10660 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010661 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010662 *
10663 * @param l Left position, relative to parent
10664 * @param t Top position, relative to parent
10665 * @param r Right position, relative to parent
10666 * @param b Bottom position, relative to parent
10667 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010668 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010669 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010670 int oldL = mLeft;
10671 int oldT = mTop;
10672 int oldB = mBottom;
10673 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010674 boolean changed = setFrame(l, t, r, b);
10675 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10676 if (ViewDebug.TRACE_HIERARCHY) {
10677 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10678 }
10679
10680 onLayout(changed, l, t, r, b);
10681 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010682
10683 if (mOnLayoutChangeListeners != null) {
10684 ArrayList<OnLayoutChangeListener> listenersCopy =
10685 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10686 int numListeners = listenersCopy.size();
10687 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010688 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010689 }
10690 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010691 }
10692 mPrivateFlags &= ~FORCE_LAYOUT;
10693 }
10694
10695 /**
10696 * Called from layout when this view should
10697 * assign a size and position to each of its children.
10698 *
10699 * Derived classes with children should override
10700 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010701 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010702 * @param changed This is a new size or position for this view
10703 * @param left Left position, relative to parent
10704 * @param top Top position, relative to parent
10705 * @param right Right position, relative to parent
10706 * @param bottom Bottom position, relative to parent
10707 */
10708 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10709 }
10710
10711 /**
10712 * Assign a size and position to this view.
10713 *
10714 * This is called from layout.
10715 *
10716 * @param left Left position, relative to parent
10717 * @param top Top position, relative to parent
10718 * @param right Right position, relative to parent
10719 * @param bottom Bottom position, relative to parent
10720 * @return true if the new size and position are different than the
10721 * previous ones
10722 * {@hide}
10723 */
10724 protected boolean setFrame(int left, int top, int right, int bottom) {
10725 boolean changed = false;
10726
10727 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010728 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 + right + "," + bottom + ")");
10730 }
10731
10732 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10733 changed = true;
10734
10735 // Remember our drawn bit
10736 int drawn = mPrivateFlags & DRAWN;
10737
10738 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010739 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010740
10741
10742 int oldWidth = mRight - mLeft;
10743 int oldHeight = mBottom - mTop;
10744
10745 mLeft = left;
10746 mTop = top;
10747 mRight = right;
10748 mBottom = bottom;
10749
10750 mPrivateFlags |= HAS_BOUNDS;
10751
10752 int newWidth = right - left;
10753 int newHeight = bottom - top;
10754
10755 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010756 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10757 // A change in dimension means an auto-centered pivot point changes, too
10758 mMatrixDirty = true;
10759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010760 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10761 }
10762
10763 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10764 // If we are visible, force the DRAWN bit to on so that
10765 // this invalidate will go through (at least to our parent).
10766 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010767 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010768 // the DRAWN bit.
10769 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010770 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010771 // parent display list may need to be recreated based on a change in the bounds
10772 // of any child
10773 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 }
10775
10776 // Reset drawn bit to original value (invalidate turns it off)
10777 mPrivateFlags |= drawn;
10778
10779 mBackgroundSizeChanged = true;
10780 }
10781 return changed;
10782 }
10783
10784 /**
10785 * Finalize inflating a view from XML. This is called as the last phase
10786 * of inflation, after all child views have been added.
10787 *
10788 * <p>Even if the subclass overrides onFinishInflate, they should always be
10789 * sure to call the super method, so that we get called.
10790 */
10791 protected void onFinishInflate() {
10792 }
10793
10794 /**
10795 * Returns the resources associated with this view.
10796 *
10797 * @return Resources object.
10798 */
10799 public Resources getResources() {
10800 return mResources;
10801 }
10802
10803 /**
10804 * Invalidates the specified Drawable.
10805 *
10806 * @param drawable the drawable to invalidate
10807 */
10808 public void invalidateDrawable(Drawable drawable) {
10809 if (verifyDrawable(drawable)) {
10810 final Rect dirty = drawable.getBounds();
10811 final int scrollX = mScrollX;
10812 final int scrollY = mScrollY;
10813
10814 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10815 dirty.right + scrollX, dirty.bottom + scrollY);
10816 }
10817 }
10818
10819 /**
10820 * Schedules an action on a drawable to occur at a specified time.
10821 *
10822 * @param who the recipient of the action
10823 * @param what the action to run on the drawable
10824 * @param when the time at which the action must occur. Uses the
10825 * {@link SystemClock#uptimeMillis} timebase.
10826 */
10827 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10828 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10829 mAttachInfo.mHandler.postAtTime(what, who, when);
10830 }
10831 }
10832
10833 /**
10834 * Cancels a scheduled action on a drawable.
10835 *
10836 * @param who the recipient of the action
10837 * @param what the action to cancel
10838 */
10839 public void unscheduleDrawable(Drawable who, Runnable what) {
10840 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10841 mAttachInfo.mHandler.removeCallbacks(what, who);
10842 }
10843 }
10844
10845 /**
10846 * Unschedule any events associated with the given Drawable. This can be
10847 * used when selecting a new Drawable into a view, so that the previous
10848 * one is completely unscheduled.
10849 *
10850 * @param who The Drawable to unschedule.
10851 *
10852 * @see #drawableStateChanged
10853 */
10854 public void unscheduleDrawable(Drawable who) {
10855 if (mAttachInfo != null) {
10856 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10857 }
10858 }
10859
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010860 /**
10861 * Return the layout direction of a given Drawable.
10862 *
10863 * @param who the Drawable to query
10864 *
10865 * @hide
10866 */
10867 public int getResolvedLayoutDirection(Drawable who) {
10868 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010869 }
10870
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010871 /**
10872 * If your view subclass is displaying its own Drawable objects, it should
10873 * override this function and return true for any Drawable it is
10874 * displaying. This allows animations for those drawables to be
10875 * scheduled.
10876 *
10877 * <p>Be sure to call through to the super class when overriding this
10878 * function.
10879 *
10880 * @param who The Drawable to verify. Return true if it is one you are
10881 * displaying, else return the result of calling through to the
10882 * super class.
10883 *
10884 * @return boolean If true than the Drawable is being displayed in the
10885 * view; else false and it is not allowed to animate.
10886 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010887 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10888 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010889 */
10890 protected boolean verifyDrawable(Drawable who) {
10891 return who == mBGDrawable;
10892 }
10893
10894 /**
10895 * This function is called whenever the state of the view changes in such
10896 * a way that it impacts the state of drawables being shown.
10897 *
10898 * <p>Be sure to call through to the superclass when overriding this
10899 * function.
10900 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010901 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010902 */
10903 protected void drawableStateChanged() {
10904 Drawable d = mBGDrawable;
10905 if (d != null && d.isStateful()) {
10906 d.setState(getDrawableState());
10907 }
10908 }
10909
10910 /**
10911 * Call this to force a view to update its drawable state. This will cause
10912 * drawableStateChanged to be called on this view. Views that are interested
10913 * in the new state should call getDrawableState.
10914 *
10915 * @see #drawableStateChanged
10916 * @see #getDrawableState
10917 */
10918 public void refreshDrawableState() {
10919 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10920 drawableStateChanged();
10921
10922 ViewParent parent = mParent;
10923 if (parent != null) {
10924 parent.childDrawableStateChanged(this);
10925 }
10926 }
10927
10928 /**
10929 * Return an array of resource IDs of the drawable states representing the
10930 * current state of the view.
10931 *
10932 * @return The current drawable state
10933 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010934 * @see Drawable#setState(int[])
10935 * @see #drawableStateChanged()
10936 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010937 */
10938 public final int[] getDrawableState() {
10939 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10940 return mDrawableState;
10941 } else {
10942 mDrawableState = onCreateDrawableState(0);
10943 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10944 return mDrawableState;
10945 }
10946 }
10947
10948 /**
10949 * Generate the new {@link android.graphics.drawable.Drawable} state for
10950 * this view. This is called by the view
10951 * system when the cached Drawable state is determined to be invalid. To
10952 * retrieve the current state, you should use {@link #getDrawableState}.
10953 *
10954 * @param extraSpace if non-zero, this is the number of extra entries you
10955 * would like in the returned array in which you can place your own
10956 * states.
10957 *
10958 * @return Returns an array holding the current {@link Drawable} state of
10959 * the view.
10960 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010961 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010962 */
10963 protected int[] onCreateDrawableState(int extraSpace) {
10964 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10965 mParent instanceof View) {
10966 return ((View) mParent).onCreateDrawableState(extraSpace);
10967 }
10968
10969 int[] drawableState;
10970
10971 int privateFlags = mPrivateFlags;
10972
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010973 int viewStateIndex = 0;
10974 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10975 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10976 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010977 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010978 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10979 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010980 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10981 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010982 // This is set if HW acceleration is requested, even if the current
10983 // process doesn't allow it. This is just to allow app preview
10984 // windows to better match their app.
10985 viewStateIndex |= VIEW_STATE_ACCELERATED;
10986 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010987 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010988
Christopher Tate3d4bf172011-03-28 16:16:46 -070010989 final int privateFlags2 = mPrivateFlags2;
10990 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10991 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010993 drawableState = VIEW_STATE_SETS[viewStateIndex];
10994
10995 //noinspection ConstantIfStatement
10996 if (false) {
10997 Log.i("View", "drawableStateIndex=" + viewStateIndex);
10998 Log.i("View", toString()
10999 + " pressed=" + ((privateFlags & PRESSED) != 0)
11000 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11001 + " fo=" + hasFocus()
11002 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011003 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011004 + ": " + Arrays.toString(drawableState));
11005 }
11006
11007 if (extraSpace == 0) {
11008 return drawableState;
11009 }
11010
11011 final int[] fullState;
11012 if (drawableState != null) {
11013 fullState = new int[drawableState.length + extraSpace];
11014 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11015 } else {
11016 fullState = new int[extraSpace];
11017 }
11018
11019 return fullState;
11020 }
11021
11022 /**
11023 * Merge your own state values in <var>additionalState</var> into the base
11024 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011025 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011026 *
11027 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011028 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011029 * own additional state values.
11030 *
11031 * @param additionalState The additional state values you would like
11032 * added to <var>baseState</var>; this array is not modified.
11033 *
11034 * @return As a convenience, the <var>baseState</var> array you originally
11035 * passed into the function is returned.
11036 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011037 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011038 */
11039 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11040 final int N = baseState.length;
11041 int i = N - 1;
11042 while (i >= 0 && baseState[i] == 0) {
11043 i--;
11044 }
11045 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11046 return baseState;
11047 }
11048
11049 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011050 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11051 * on all Drawable objects associated with this view.
11052 */
11053 public void jumpDrawablesToCurrentState() {
11054 if (mBGDrawable != null) {
11055 mBGDrawable.jumpToCurrentState();
11056 }
11057 }
11058
11059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011060 * Sets the background color for this view.
11061 * @param color the color of the background
11062 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011063 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011064 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011065 if (mBGDrawable instanceof ColorDrawable) {
11066 ((ColorDrawable) mBGDrawable).setColor(color);
11067 } else {
11068 setBackgroundDrawable(new ColorDrawable(color));
11069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 }
11071
11072 /**
11073 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011074 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011075 * @param resid The identifier of the resource.
11076 * @attr ref android.R.styleable#View_background
11077 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011078 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011079 public void setBackgroundResource(int resid) {
11080 if (resid != 0 && resid == mBackgroundResource) {
11081 return;
11082 }
11083
11084 Drawable d= null;
11085 if (resid != 0) {
11086 d = mResources.getDrawable(resid);
11087 }
11088 setBackgroundDrawable(d);
11089
11090 mBackgroundResource = resid;
11091 }
11092
11093 /**
11094 * Set the background to a given Drawable, or remove the background. If the
11095 * background has padding, this View's padding is set to the background's
11096 * padding. However, when a background is removed, this View's padding isn't
11097 * touched. If setting the padding is desired, please use
11098 * {@link #setPadding(int, int, int, int)}.
11099 *
11100 * @param d The Drawable to use as the background, or null to remove the
11101 * background
11102 */
11103 public void setBackgroundDrawable(Drawable d) {
11104 boolean requestLayout = false;
11105
11106 mBackgroundResource = 0;
11107
11108 /*
11109 * Regardless of whether we're setting a new background or not, we want
11110 * to clear the previous drawable.
11111 */
11112 if (mBGDrawable != null) {
11113 mBGDrawable.setCallback(null);
11114 unscheduleDrawable(mBGDrawable);
11115 }
11116
11117 if (d != null) {
11118 Rect padding = sThreadLocal.get();
11119 if (padding == null) {
11120 padding = new Rect();
11121 sThreadLocal.set(padding);
11122 }
11123 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011124 switch (d.getResolvedLayoutDirectionSelf()) {
11125 case LAYOUT_DIRECTION_RTL:
11126 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11127 break;
11128 case LAYOUT_DIRECTION_LTR:
11129 default:
11130 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11131 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 }
11133
11134 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11135 // if it has a different minimum size, we should layout again
11136 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11137 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11138 requestLayout = true;
11139 }
11140
11141 d.setCallback(this);
11142 if (d.isStateful()) {
11143 d.setState(getDrawableState());
11144 }
11145 d.setVisible(getVisibility() == VISIBLE, false);
11146 mBGDrawable = d;
11147
11148 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11149 mPrivateFlags &= ~SKIP_DRAW;
11150 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11151 requestLayout = true;
11152 }
11153 } else {
11154 /* Remove the background */
11155 mBGDrawable = null;
11156
11157 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11158 /*
11159 * This view ONLY drew the background before and we're removing
11160 * the background, so now it won't draw anything
11161 * (hence we SKIP_DRAW)
11162 */
11163 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11164 mPrivateFlags |= SKIP_DRAW;
11165 }
11166
11167 /*
11168 * When the background is set, we try to apply its padding to this
11169 * View. When the background is removed, we don't touch this View's
11170 * padding. This is noted in the Javadocs. Hence, we don't need to
11171 * requestLayout(), the invalidate() below is sufficient.
11172 */
11173
11174 // The old background's minimum size could have affected this
11175 // View's layout, so let's requestLayout
11176 requestLayout = true;
11177 }
11178
Romain Guy8f1344f52009-05-15 16:03:59 -070011179 computeOpaqueFlags();
11180
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011181 if (requestLayout) {
11182 requestLayout();
11183 }
11184
11185 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011186 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 }
11188
11189 /**
11190 * Gets the background drawable
11191 * @return The drawable used as the background for this view, if any.
11192 */
11193 public Drawable getBackground() {
11194 return mBGDrawable;
11195 }
11196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011197 /**
11198 * Sets the padding. The view may add on the space required to display
11199 * the scrollbars, depending on the style and visibility of the scrollbars.
11200 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11201 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11202 * from the values set in this call.
11203 *
11204 * @attr ref android.R.styleable#View_padding
11205 * @attr ref android.R.styleable#View_paddingBottom
11206 * @attr ref android.R.styleable#View_paddingLeft
11207 * @attr ref android.R.styleable#View_paddingRight
11208 * @attr ref android.R.styleable#View_paddingTop
11209 * @param left the left padding in pixels
11210 * @param top the top padding in pixels
11211 * @param right the right padding in pixels
11212 * @param bottom the bottom padding in pixels
11213 */
11214 public void setPadding(int left, int top, int right, int bottom) {
11215 boolean changed = false;
11216
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011217 mUserPaddingRelative = false;
11218
Adam Powell20232d02010-12-08 21:08:53 -080011219 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011220 mUserPaddingRight = right;
11221 mUserPaddingBottom = bottom;
11222
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011223 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011224
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011225 // Common case is there are no scroll bars.
11226 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011227 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011228 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011229 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011230 switch (mVerticalScrollbarPosition) {
11231 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011232 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11233 left += offset;
11234 } else {
11235 right += offset;
11236 }
11237 break;
Adam Powell20232d02010-12-08 21:08:53 -080011238 case SCROLLBAR_POSITION_RIGHT:
11239 right += offset;
11240 break;
11241 case SCROLLBAR_POSITION_LEFT:
11242 left += offset;
11243 break;
11244 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011245 }
Adam Powell20232d02010-12-08 21:08:53 -080011246 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011247 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11248 ? 0 : getHorizontalScrollbarHeight();
11249 }
11250 }
Romain Guy8506ab42009-06-11 17:35:47 -070011251
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011252 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011253 changed = true;
11254 mPaddingLeft = left;
11255 }
11256 if (mPaddingTop != top) {
11257 changed = true;
11258 mPaddingTop = top;
11259 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011260 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011261 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011262 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011263 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011264 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011266 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 }
11268
11269 if (changed) {
11270 requestLayout();
11271 }
11272 }
11273
11274 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011275 * Sets the relative padding. The view may add on the space required to display
11276 * the scrollbars, depending on the style and visibility of the scrollbars.
11277 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11278 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11279 * from the values set in this call.
11280 *
11281 * @attr ref android.R.styleable#View_padding
11282 * @attr ref android.R.styleable#View_paddingBottom
11283 * @attr ref android.R.styleable#View_paddingStart
11284 * @attr ref android.R.styleable#View_paddingEnd
11285 * @attr ref android.R.styleable#View_paddingTop
11286 * @param start the start padding in pixels
11287 * @param top the top padding in pixels
11288 * @param end the end padding in pixels
11289 * @param bottom the bottom padding in pixels
11290 *
11291 * @hide
11292 */
11293 public void setPaddingRelative(int start, int top, int end, int bottom) {
11294 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011295
11296 mUserPaddingStart = start;
11297 mUserPaddingEnd = end;
11298
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011299 switch(getResolvedLayoutDirection()) {
11300 case LAYOUT_DIRECTION_RTL:
11301 setPadding(end, top, start, bottom);
11302 break;
11303 case LAYOUT_DIRECTION_LTR:
11304 default:
11305 setPadding(start, top, end, bottom);
11306 }
11307 }
11308
11309 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011310 * Returns the top padding of this view.
11311 *
11312 * @return the top padding in pixels
11313 */
11314 public int getPaddingTop() {
11315 return mPaddingTop;
11316 }
11317
11318 /**
11319 * Returns the bottom padding of this view. If there are inset and enabled
11320 * scrollbars, this value may include the space required to display the
11321 * scrollbars as well.
11322 *
11323 * @return the bottom padding in pixels
11324 */
11325 public int getPaddingBottom() {
11326 return mPaddingBottom;
11327 }
11328
11329 /**
11330 * Returns the left padding of this view. If there are inset and enabled
11331 * scrollbars, this value may include the space required to display the
11332 * scrollbars as well.
11333 *
11334 * @return the left padding in pixels
11335 */
11336 public int getPaddingLeft() {
11337 return mPaddingLeft;
11338 }
11339
11340 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011341 * Returns the start padding of this view. If there are inset and enabled
11342 * scrollbars, this value may include the space required to display the
11343 * scrollbars as well.
11344 *
11345 * @return the start padding in pixels
11346 *
11347 * @hide
11348 */
11349 public int getPaddingStart() {
11350 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11351 mPaddingRight : mPaddingLeft;
11352 }
11353
11354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011355 * Returns the right padding of this view. If there are inset and enabled
11356 * scrollbars, this value may include the space required to display the
11357 * scrollbars as well.
11358 *
11359 * @return the right padding in pixels
11360 */
11361 public int getPaddingRight() {
11362 return mPaddingRight;
11363 }
11364
11365 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011366 * Returns the end padding of this view. If there are inset and enabled
11367 * scrollbars, this value may include the space required to display the
11368 * scrollbars as well.
11369 *
11370 * @return the end padding in pixels
11371 *
11372 * @hide
11373 */
11374 public int getPaddingEnd() {
11375 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11376 mPaddingLeft : mPaddingRight;
11377 }
11378
11379 /**
11380 * Return if the padding as been set thru relative values
11381 * {@link #setPaddingRelative(int, int, int, int)} or thru
11382 * @attr ref android.R.styleable#View_paddingStart or
11383 * @attr ref android.R.styleable#View_paddingEnd
11384 *
11385 * @return true if the padding is relative or false if it is not.
11386 *
11387 * @hide
11388 */
11389 public boolean isPaddingRelative() {
11390 return mUserPaddingRelative;
11391 }
11392
11393 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011394 * Changes the selection state of this view. A view can be selected or not.
11395 * Note that selection is not the same as focus. Views are typically
11396 * selected in the context of an AdapterView like ListView or GridView;
11397 * the selected view is the view that is highlighted.
11398 *
11399 * @param selected true if the view must be selected, false otherwise
11400 */
11401 public void setSelected(boolean selected) {
11402 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11403 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011404 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011405 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011406 refreshDrawableState();
11407 dispatchSetSelected(selected);
11408 }
11409 }
11410
11411 /**
11412 * Dispatch setSelected to all of this View's children.
11413 *
11414 * @see #setSelected(boolean)
11415 *
11416 * @param selected The new selected state
11417 */
11418 protected void dispatchSetSelected(boolean selected) {
11419 }
11420
11421 /**
11422 * Indicates the selection state of this view.
11423 *
11424 * @return true if the view is selected, false otherwise
11425 */
11426 @ViewDebug.ExportedProperty
11427 public boolean isSelected() {
11428 return (mPrivateFlags & SELECTED) != 0;
11429 }
11430
11431 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011432 * Changes the activated state of this view. A view can be activated or not.
11433 * Note that activation is not the same as selection. Selection is
11434 * a transient property, representing the view (hierarchy) the user is
11435 * currently interacting with. Activation is a longer-term state that the
11436 * user can move views in and out of. For example, in a list view with
11437 * single or multiple selection enabled, the views in the current selection
11438 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11439 * here.) The activated state is propagated down to children of the view it
11440 * is set on.
11441 *
11442 * @param activated true if the view must be activated, false otherwise
11443 */
11444 public void setActivated(boolean activated) {
11445 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11446 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011447 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011448 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011449 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011450 }
11451 }
11452
11453 /**
11454 * Dispatch setActivated to all of this View's children.
11455 *
11456 * @see #setActivated(boolean)
11457 *
11458 * @param activated The new activated state
11459 */
11460 protected void dispatchSetActivated(boolean activated) {
11461 }
11462
11463 /**
11464 * Indicates the activation state of this view.
11465 *
11466 * @return true if the view is activated, false otherwise
11467 */
11468 @ViewDebug.ExportedProperty
11469 public boolean isActivated() {
11470 return (mPrivateFlags & ACTIVATED) != 0;
11471 }
11472
11473 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011474 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11475 * observer can be used to get notifications when global events, like
11476 * layout, happen.
11477 *
11478 * The returned ViewTreeObserver observer is not guaranteed to remain
11479 * valid for the lifetime of this View. If the caller of this method keeps
11480 * a long-lived reference to ViewTreeObserver, it should always check for
11481 * the return value of {@link ViewTreeObserver#isAlive()}.
11482 *
11483 * @return The ViewTreeObserver for this view's hierarchy.
11484 */
11485 public ViewTreeObserver getViewTreeObserver() {
11486 if (mAttachInfo != null) {
11487 return mAttachInfo.mTreeObserver;
11488 }
11489 if (mFloatingTreeObserver == null) {
11490 mFloatingTreeObserver = new ViewTreeObserver();
11491 }
11492 return mFloatingTreeObserver;
11493 }
11494
11495 /**
11496 * <p>Finds the topmost view in the current view hierarchy.</p>
11497 *
11498 * @return the topmost view containing this view
11499 */
11500 public View getRootView() {
11501 if (mAttachInfo != null) {
11502 final View v = mAttachInfo.mRootView;
11503 if (v != null) {
11504 return v;
11505 }
11506 }
Romain Guy8506ab42009-06-11 17:35:47 -070011507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011508 View parent = this;
11509
11510 while (parent.mParent != null && parent.mParent instanceof View) {
11511 parent = (View) parent.mParent;
11512 }
11513
11514 return parent;
11515 }
11516
11517 /**
11518 * <p>Computes the coordinates of this view on the screen. The argument
11519 * must be an array of two integers. After the method returns, the array
11520 * contains the x and y location in that order.</p>
11521 *
11522 * @param location an array of two integers in which to hold the coordinates
11523 */
11524 public void getLocationOnScreen(int[] location) {
11525 getLocationInWindow(location);
11526
11527 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011528 if (info != null) {
11529 location[0] += info.mWindowLeft;
11530 location[1] += info.mWindowTop;
11531 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011532 }
11533
11534 /**
11535 * <p>Computes the coordinates of this view in its window. The argument
11536 * must be an array of two integers. After the method returns, the array
11537 * contains the x and y location in that order.</p>
11538 *
11539 * @param location an array of two integers in which to hold the coordinates
11540 */
11541 public void getLocationInWindow(int[] location) {
11542 if (location == null || location.length < 2) {
11543 throw new IllegalArgumentException("location must be an array of "
11544 + "two integers");
11545 }
11546
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011547 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11548 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011549
11550 ViewParent viewParent = mParent;
11551 while (viewParent instanceof View) {
11552 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011553 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11554 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555 viewParent = view.mParent;
11556 }
Romain Guy8506ab42009-06-11 17:35:47 -070011557
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011558 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011559 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011560 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 location[1] -= vr.mCurScrollY;
11562 }
11563 }
11564
11565 /**
11566 * {@hide}
11567 * @param id the id of the view to be found
11568 * @return the view of the specified id, null if cannot be found
11569 */
11570 protected View findViewTraversal(int id) {
11571 if (id == mID) {
11572 return this;
11573 }
11574 return null;
11575 }
11576
11577 /**
11578 * {@hide}
11579 * @param tag the tag of the view to be found
11580 * @return the view of specified tag, null if cannot be found
11581 */
11582 protected View findViewWithTagTraversal(Object tag) {
11583 if (tag != null && tag.equals(mTag)) {
11584 return this;
11585 }
11586 return null;
11587 }
11588
11589 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011590 * {@hide}
11591 * @param predicate The predicate to evaluate.
11592 * @return The first view that matches the predicate or null.
11593 */
11594 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11595 if (predicate.apply(this)) {
11596 return this;
11597 }
11598 return null;
11599 }
11600
11601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 * Look for a child view with the given id. If this view has the given
11603 * id, return this view.
11604 *
11605 * @param id The id to search for.
11606 * @return The view that has the given id in the hierarchy or null
11607 */
11608 public final View findViewById(int id) {
11609 if (id < 0) {
11610 return null;
11611 }
11612 return findViewTraversal(id);
11613 }
11614
11615 /**
11616 * Look for a child view with the given tag. If this view has the given
11617 * tag, return this view.
11618 *
11619 * @param tag The tag to search for, using "tag.equals(getTag())".
11620 * @return The View that has the given tag in the hierarchy or null
11621 */
11622 public final View findViewWithTag(Object tag) {
11623 if (tag == null) {
11624 return null;
11625 }
11626 return findViewWithTagTraversal(tag);
11627 }
11628
11629 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011630 * {@hide}
11631 * Look for a child view that matches the specified predicate.
11632 * If this view matches the predicate, return this view.
11633 *
11634 * @param predicate The predicate to evaluate.
11635 * @return The first view that matches the predicate or null.
11636 */
11637 public final View findViewByPredicate(Predicate<View> predicate) {
11638 return findViewByPredicateTraversal(predicate);
11639 }
11640
11641 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011642 * Sets the identifier for this view. The identifier does not have to be
11643 * unique in this view's hierarchy. The identifier should be a positive
11644 * number.
11645 *
11646 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011647 * @see #getId()
11648 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011649 *
11650 * @param id a number used to identify the view
11651 *
11652 * @attr ref android.R.styleable#View_id
11653 */
11654 public void setId(int id) {
11655 mID = id;
11656 }
11657
11658 /**
11659 * {@hide}
11660 *
11661 * @param isRoot true if the view belongs to the root namespace, false
11662 * otherwise
11663 */
11664 public void setIsRootNamespace(boolean isRoot) {
11665 if (isRoot) {
11666 mPrivateFlags |= IS_ROOT_NAMESPACE;
11667 } else {
11668 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11669 }
11670 }
11671
11672 /**
11673 * {@hide}
11674 *
11675 * @return true if the view belongs to the root namespace, false otherwise
11676 */
11677 public boolean isRootNamespace() {
11678 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11679 }
11680
11681 /**
11682 * Returns this view's identifier.
11683 *
11684 * @return a positive integer used to identify the view or {@link #NO_ID}
11685 * if the view has no ID
11686 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011687 * @see #setId(int)
11688 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011689 * @attr ref android.R.styleable#View_id
11690 */
11691 @ViewDebug.CapturedViewProperty
11692 public int getId() {
11693 return mID;
11694 }
11695
11696 /**
11697 * Returns this view's tag.
11698 *
11699 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011700 *
11701 * @see #setTag(Object)
11702 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011703 */
11704 @ViewDebug.ExportedProperty
11705 public Object getTag() {
11706 return mTag;
11707 }
11708
11709 /**
11710 * Sets the tag associated with this view. A tag can be used to mark
11711 * a view in its hierarchy and does not have to be unique within the
11712 * hierarchy. Tags can also be used to store data within a view without
11713 * resorting to another data structure.
11714 *
11715 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011716 *
11717 * @see #getTag()
11718 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011719 */
11720 public void setTag(final Object tag) {
11721 mTag = tag;
11722 }
11723
11724 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011725 * Returns the tag associated with this view and the specified key.
11726 *
11727 * @param key The key identifying the tag
11728 *
11729 * @return the Object stored in this view as a tag
11730 *
11731 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011732 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011733 */
11734 public Object getTag(int key) {
11735 SparseArray<Object> tags = null;
11736 synchronized (sTagsLock) {
11737 if (sTags != null) {
11738 tags = sTags.get(this);
11739 }
11740 }
11741
11742 if (tags != null) return tags.get(key);
11743 return null;
11744 }
11745
11746 /**
11747 * Sets a tag associated with this view and a key. A tag can be used
11748 * to mark a view in its hierarchy and does not have to be unique within
11749 * the hierarchy. Tags can also be used to store data within a view
11750 * without resorting to another data structure.
11751 *
11752 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011753 * application to ensure it is unique (see the <a
11754 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11755 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011756 * the Android framework or not associated with any package will cause
11757 * an {@link IllegalArgumentException} to be thrown.
11758 *
11759 * @param key The key identifying the tag
11760 * @param tag An Object to tag the view with
11761 *
11762 * @throws IllegalArgumentException If they specified key is not valid
11763 *
11764 * @see #setTag(Object)
11765 * @see #getTag(int)
11766 */
11767 public void setTag(int key, final Object tag) {
11768 // If the package id is 0x00 or 0x01, it's either an undefined package
11769 // or a framework id
11770 if ((key >>> 24) < 2) {
11771 throw new IllegalArgumentException("The key must be an application-specific "
11772 + "resource id.");
11773 }
11774
11775 setTagInternal(this, key, tag);
11776 }
11777
11778 /**
11779 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11780 * framework id.
11781 *
11782 * @hide
11783 */
11784 public void setTagInternal(int key, Object tag) {
11785 if ((key >>> 24) != 0x1) {
11786 throw new IllegalArgumentException("The key must be a framework-specific "
11787 + "resource id.");
11788 }
11789
Romain Guy8506ab42009-06-11 17:35:47 -070011790 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011791 }
11792
11793 private static void setTagInternal(View view, int key, Object tag) {
11794 SparseArray<Object> tags = null;
11795 synchronized (sTagsLock) {
11796 if (sTags == null) {
11797 sTags = new WeakHashMap<View, SparseArray<Object>>();
11798 } else {
11799 tags = sTags.get(view);
11800 }
11801 }
11802
11803 if (tags == null) {
11804 tags = new SparseArray<Object>(2);
11805 synchronized (sTagsLock) {
11806 sTags.put(view, tags);
11807 }
11808 }
11809
11810 tags.put(key, tag);
11811 }
11812
11813 /**
Romain Guy13922e02009-05-12 17:56:14 -070011814 * @param consistency The type of consistency. See ViewDebug for more information.
11815 *
11816 * @hide
11817 */
11818 protected boolean dispatchConsistencyCheck(int consistency) {
11819 return onConsistencyCheck(consistency);
11820 }
11821
11822 /**
11823 * Method that subclasses should implement to check their consistency. The type of
11824 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011825 *
Romain Guy13922e02009-05-12 17:56:14 -070011826 * @param consistency The type of consistency. See ViewDebug for more information.
11827 *
11828 * @throws IllegalStateException if the view is in an inconsistent state.
11829 *
11830 * @hide
11831 */
11832 protected boolean onConsistencyCheck(int consistency) {
11833 boolean result = true;
11834
11835 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11836 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11837
11838 if (checkLayout) {
11839 if (getParent() == null) {
11840 result = false;
11841 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11842 "View " + this + " does not have a parent.");
11843 }
11844
11845 if (mAttachInfo == null) {
11846 result = false;
11847 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11848 "View " + this + " is not attached to a window.");
11849 }
11850 }
11851
11852 if (checkDrawing) {
11853 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11854 // from their draw() method
11855
11856 if ((mPrivateFlags & DRAWN) != DRAWN &&
11857 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11858 result = false;
11859 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11860 "View " + this + " was invalidated but its drawing cache is valid.");
11861 }
11862 }
11863
11864 return result;
11865 }
11866
11867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011868 * Prints information about this view in the log output, with the tag
11869 * {@link #VIEW_LOG_TAG}.
11870 *
11871 * @hide
11872 */
11873 public void debug() {
11874 debug(0);
11875 }
11876
11877 /**
11878 * Prints information about this view in the log output, with the tag
11879 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11880 * indentation defined by the <code>depth</code>.
11881 *
11882 * @param depth the indentation level
11883 *
11884 * @hide
11885 */
11886 protected void debug(int depth) {
11887 String output = debugIndent(depth - 1);
11888
11889 output += "+ " + this;
11890 int id = getId();
11891 if (id != -1) {
11892 output += " (id=" + id + ")";
11893 }
11894 Object tag = getTag();
11895 if (tag != null) {
11896 output += " (tag=" + tag + ")";
11897 }
11898 Log.d(VIEW_LOG_TAG, output);
11899
11900 if ((mPrivateFlags & FOCUSED) != 0) {
11901 output = debugIndent(depth) + " FOCUSED";
11902 Log.d(VIEW_LOG_TAG, output);
11903 }
11904
11905 output = debugIndent(depth);
11906 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11907 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11908 + "} ";
11909 Log.d(VIEW_LOG_TAG, output);
11910
11911 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11912 || mPaddingBottom != 0) {
11913 output = debugIndent(depth);
11914 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11915 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11916 Log.d(VIEW_LOG_TAG, output);
11917 }
11918
11919 output = debugIndent(depth);
11920 output += "mMeasureWidth=" + mMeasuredWidth +
11921 " mMeasureHeight=" + mMeasuredHeight;
11922 Log.d(VIEW_LOG_TAG, output);
11923
11924 output = debugIndent(depth);
11925 if (mLayoutParams == null) {
11926 output += "BAD! no layout params";
11927 } else {
11928 output = mLayoutParams.debug(output);
11929 }
11930 Log.d(VIEW_LOG_TAG, output);
11931
11932 output = debugIndent(depth);
11933 output += "flags={";
11934 output += View.printFlags(mViewFlags);
11935 output += "}";
11936 Log.d(VIEW_LOG_TAG, output);
11937
11938 output = debugIndent(depth);
11939 output += "privateFlags={";
11940 output += View.printPrivateFlags(mPrivateFlags);
11941 output += "}";
11942 Log.d(VIEW_LOG_TAG, output);
11943 }
11944
11945 /**
11946 * Creates an string of whitespaces used for indentation.
11947 *
11948 * @param depth the indentation level
11949 * @return a String containing (depth * 2 + 3) * 2 white spaces
11950 *
11951 * @hide
11952 */
11953 protected static String debugIndent(int depth) {
11954 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11955 for (int i = 0; i < (depth * 2) + 3; i++) {
11956 spaces.append(' ').append(' ');
11957 }
11958 return spaces.toString();
11959 }
11960
11961 /**
11962 * <p>Return the offset of the widget's text baseline from the widget's top
11963 * boundary. If this widget does not support baseline alignment, this
11964 * method returns -1. </p>
11965 *
11966 * @return the offset of the baseline within the widget's bounds or -1
11967 * if baseline alignment is not supported
11968 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011969 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011970 public int getBaseline() {
11971 return -1;
11972 }
11973
11974 /**
11975 * Call this when something has changed which has invalidated the
11976 * layout of this view. This will schedule a layout pass of the view
11977 * tree.
11978 */
11979 public void requestLayout() {
11980 if (ViewDebug.TRACE_HIERARCHY) {
11981 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11982 }
11983
11984 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011985 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011986
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070011987 if (mLayoutParams != null && mParent != null) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -070011988 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
11989 }
11990
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011991 if (mParent != null && !mParent.isLayoutRequested()) {
11992 mParent.requestLayout();
11993 }
11994 }
11995
11996 /**
11997 * Forces this view to be laid out during the next layout pass.
11998 * This method does not call requestLayout() or forceLayout()
11999 * on the parent.
12000 */
12001 public void forceLayout() {
12002 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012003 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012004 }
12005
12006 /**
12007 * <p>
12008 * This is called to find out how big a view should be. The parent
12009 * supplies constraint information in the width and height parameters.
12010 * </p>
12011 *
12012 * <p>
12013 * The actual mesurement work of a view is performed in
12014 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12015 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12016 * </p>
12017 *
12018 *
12019 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12020 * parent
12021 * @param heightMeasureSpec Vertical space requirements as imposed by the
12022 * parent
12023 *
12024 * @see #onMeasure(int, int)
12025 */
12026 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12027 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12028 widthMeasureSpec != mOldWidthMeasureSpec ||
12029 heightMeasureSpec != mOldHeightMeasureSpec) {
12030
12031 // first clears the measured dimension flag
12032 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12033
12034 if (ViewDebug.TRACE_HIERARCHY) {
12035 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12036 }
12037
12038 // measure ourselves, this should set the measured dimension flag back
12039 onMeasure(widthMeasureSpec, heightMeasureSpec);
12040
12041 // flag not set, setMeasuredDimension() was not invoked, we raise
12042 // an exception to warn the developer
12043 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12044 throw new IllegalStateException("onMeasure() did not set the"
12045 + " measured dimension by calling"
12046 + " setMeasuredDimension()");
12047 }
12048
12049 mPrivateFlags |= LAYOUT_REQUIRED;
12050 }
12051
12052 mOldWidthMeasureSpec = widthMeasureSpec;
12053 mOldHeightMeasureSpec = heightMeasureSpec;
12054 }
12055
12056 /**
12057 * <p>
12058 * Measure the view and its content to determine the measured width and the
12059 * measured height. This method is invoked by {@link #measure(int, int)} and
12060 * should be overriden by subclasses to provide accurate and efficient
12061 * measurement of their contents.
12062 * </p>
12063 *
12064 * <p>
12065 * <strong>CONTRACT:</strong> When overriding this method, you
12066 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12067 * measured width and height of this view. Failure to do so will trigger an
12068 * <code>IllegalStateException</code>, thrown by
12069 * {@link #measure(int, int)}. Calling the superclass'
12070 * {@link #onMeasure(int, int)} is a valid use.
12071 * </p>
12072 *
12073 * <p>
12074 * The base class implementation of measure defaults to the background size,
12075 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12076 * override {@link #onMeasure(int, int)} to provide better measurements of
12077 * their content.
12078 * </p>
12079 *
12080 * <p>
12081 * If this method is overridden, it is the subclass's responsibility to make
12082 * sure the measured height and width are at least the view's minimum height
12083 * and width ({@link #getSuggestedMinimumHeight()} and
12084 * {@link #getSuggestedMinimumWidth()}).
12085 * </p>
12086 *
12087 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12088 * The requirements are encoded with
12089 * {@link android.view.View.MeasureSpec}.
12090 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12091 * The requirements are encoded with
12092 * {@link android.view.View.MeasureSpec}.
12093 *
12094 * @see #getMeasuredWidth()
12095 * @see #getMeasuredHeight()
12096 * @see #setMeasuredDimension(int, int)
12097 * @see #getSuggestedMinimumHeight()
12098 * @see #getSuggestedMinimumWidth()
12099 * @see android.view.View.MeasureSpec#getMode(int)
12100 * @see android.view.View.MeasureSpec#getSize(int)
12101 */
12102 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12103 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12104 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12105 }
12106
12107 /**
12108 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12109 * measured width and measured height. Failing to do so will trigger an
12110 * exception at measurement time.</p>
12111 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012112 * @param measuredWidth The measured width of this view. May be a complex
12113 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12114 * {@link #MEASURED_STATE_TOO_SMALL}.
12115 * @param measuredHeight The measured height of this view. May be a complex
12116 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12117 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012118 */
12119 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12120 mMeasuredWidth = measuredWidth;
12121 mMeasuredHeight = measuredHeight;
12122
12123 mPrivateFlags |= MEASURED_DIMENSION_SET;
12124 }
12125
12126 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012127 * Merge two states as returned by {@link #getMeasuredState()}.
12128 * @param curState The current state as returned from a view or the result
12129 * of combining multiple views.
12130 * @param newState The new view state to combine.
12131 * @return Returns a new integer reflecting the combination of the two
12132 * states.
12133 */
12134 public static int combineMeasuredStates(int curState, int newState) {
12135 return curState | newState;
12136 }
12137
12138 /**
12139 * Version of {@link #resolveSizeAndState(int, int, int)}
12140 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12141 */
12142 public static int resolveSize(int size, int measureSpec) {
12143 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12144 }
12145
12146 /**
12147 * Utility to reconcile a desired size and state, with constraints imposed
12148 * by a MeasureSpec. Will take the desired size, unless a different size
12149 * is imposed by the constraints. The returned value is a compound integer,
12150 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12151 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12152 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012153 *
12154 * @param size How big the view wants to be
12155 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012156 * @return Size information bit mask as defined by
12157 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012158 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012159 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012160 int result = size;
12161 int specMode = MeasureSpec.getMode(measureSpec);
12162 int specSize = MeasureSpec.getSize(measureSpec);
12163 switch (specMode) {
12164 case MeasureSpec.UNSPECIFIED:
12165 result = size;
12166 break;
12167 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012168 if (specSize < size) {
12169 result = specSize | MEASURED_STATE_TOO_SMALL;
12170 } else {
12171 result = size;
12172 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012173 break;
12174 case MeasureSpec.EXACTLY:
12175 result = specSize;
12176 break;
12177 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012178 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 }
12180
12181 /**
12182 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012183 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012184 * by the MeasureSpec.
12185 *
12186 * @param size Default size for this view
12187 * @param measureSpec Constraints imposed by the parent
12188 * @return The size this view should be.
12189 */
12190 public static int getDefaultSize(int size, int measureSpec) {
12191 int result = size;
12192 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012193 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012194
12195 switch (specMode) {
12196 case MeasureSpec.UNSPECIFIED:
12197 result = size;
12198 break;
12199 case MeasureSpec.AT_MOST:
12200 case MeasureSpec.EXACTLY:
12201 result = specSize;
12202 break;
12203 }
12204 return result;
12205 }
12206
12207 /**
12208 * Returns the suggested minimum height that the view should use. This
12209 * returns the maximum of the view's minimum height
12210 * and the background's minimum height
12211 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12212 * <p>
12213 * When being used in {@link #onMeasure(int, int)}, the caller should still
12214 * ensure the returned height is within the requirements of the parent.
12215 *
12216 * @return The suggested minimum height of the view.
12217 */
12218 protected int getSuggestedMinimumHeight() {
12219 int suggestedMinHeight = mMinHeight;
12220
12221 if (mBGDrawable != null) {
12222 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12223 if (suggestedMinHeight < bgMinHeight) {
12224 suggestedMinHeight = bgMinHeight;
12225 }
12226 }
12227
12228 return suggestedMinHeight;
12229 }
12230
12231 /**
12232 * Returns the suggested minimum width that the view should use. This
12233 * returns the maximum of the view's minimum width)
12234 * and the background's minimum width
12235 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12236 * <p>
12237 * When being used in {@link #onMeasure(int, int)}, the caller should still
12238 * ensure the returned width is within the requirements of the parent.
12239 *
12240 * @return The suggested minimum width of the view.
12241 */
12242 protected int getSuggestedMinimumWidth() {
12243 int suggestedMinWidth = mMinWidth;
12244
12245 if (mBGDrawable != null) {
12246 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12247 if (suggestedMinWidth < bgMinWidth) {
12248 suggestedMinWidth = bgMinWidth;
12249 }
12250 }
12251
12252 return suggestedMinWidth;
12253 }
12254
12255 /**
12256 * Sets the minimum height of the view. It is not guaranteed the view will
12257 * be able to achieve this minimum height (for example, if its parent layout
12258 * constrains it with less available height).
12259 *
12260 * @param minHeight The minimum height the view will try to be.
12261 */
12262 public void setMinimumHeight(int minHeight) {
12263 mMinHeight = minHeight;
12264 }
12265
12266 /**
12267 * Sets the minimum width of the view. It is not guaranteed the view will
12268 * be able to achieve this minimum width (for example, if its parent layout
12269 * constrains it with less available width).
12270 *
12271 * @param minWidth The minimum width the view will try to be.
12272 */
12273 public void setMinimumWidth(int minWidth) {
12274 mMinWidth = minWidth;
12275 }
12276
12277 /**
12278 * Get the animation currently associated with this view.
12279 *
12280 * @return The animation that is currently playing or
12281 * scheduled to play for this view.
12282 */
12283 public Animation getAnimation() {
12284 return mCurrentAnimation;
12285 }
12286
12287 /**
12288 * Start the specified animation now.
12289 *
12290 * @param animation the animation to start now
12291 */
12292 public void startAnimation(Animation animation) {
12293 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12294 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012295 invalidateParentCaches();
12296 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012297 }
12298
12299 /**
12300 * Cancels any animations for this view.
12301 */
12302 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012303 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012304 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012305 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012306 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012307 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012308 }
12309
12310 /**
12311 * Sets the next animation to play for this view.
12312 * If you want the animation to play immediately, use
12313 * startAnimation. This method provides allows fine-grained
12314 * control over the start time and invalidation, but you
12315 * must make sure that 1) the animation has a start time set, and
12316 * 2) the view will be invalidated when the animation is supposed to
12317 * start.
12318 *
12319 * @param animation The next animation, or null.
12320 */
12321 public void setAnimation(Animation animation) {
12322 mCurrentAnimation = animation;
12323 if (animation != null) {
12324 animation.reset();
12325 }
12326 }
12327
12328 /**
12329 * Invoked by a parent ViewGroup to notify the start of the animation
12330 * currently associated with this view. If you override this method,
12331 * always call super.onAnimationStart();
12332 *
12333 * @see #setAnimation(android.view.animation.Animation)
12334 * @see #getAnimation()
12335 */
12336 protected void onAnimationStart() {
12337 mPrivateFlags |= ANIMATION_STARTED;
12338 }
12339
12340 /**
12341 * Invoked by a parent ViewGroup to notify the end of the animation
12342 * currently associated with this view. If you override this method,
12343 * always call super.onAnimationEnd();
12344 *
12345 * @see #setAnimation(android.view.animation.Animation)
12346 * @see #getAnimation()
12347 */
12348 protected void onAnimationEnd() {
12349 mPrivateFlags &= ~ANIMATION_STARTED;
12350 }
12351
12352 /**
12353 * Invoked if there is a Transform that involves alpha. Subclass that can
12354 * draw themselves with the specified alpha should return true, and then
12355 * respect that alpha when their onDraw() is called. If this returns false
12356 * then the view may be redirected to draw into an offscreen buffer to
12357 * fulfill the request, which will look fine, but may be slower than if the
12358 * subclass handles it internally. The default implementation returns false.
12359 *
12360 * @param alpha The alpha (0..255) to apply to the view's drawing
12361 * @return true if the view can draw with the specified alpha.
12362 */
12363 protected boolean onSetAlpha(int alpha) {
12364 return false;
12365 }
12366
12367 /**
12368 * This is used by the RootView to perform an optimization when
12369 * the view hierarchy contains one or several SurfaceView.
12370 * SurfaceView is always considered transparent, but its children are not,
12371 * therefore all View objects remove themselves from the global transparent
12372 * region (passed as a parameter to this function).
12373 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012374 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012375 *
12376 * @return Returns true if the effective visibility of the view at this
12377 * point is opaque, regardless of the transparent region; returns false
12378 * if it is possible for underlying windows to be seen behind the view.
12379 *
12380 * {@hide}
12381 */
12382 public boolean gatherTransparentRegion(Region region) {
12383 final AttachInfo attachInfo = mAttachInfo;
12384 if (region != null && attachInfo != null) {
12385 final int pflags = mPrivateFlags;
12386 if ((pflags & SKIP_DRAW) == 0) {
12387 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12388 // remove it from the transparent region.
12389 final int[] location = attachInfo.mTransparentLocation;
12390 getLocationInWindow(location);
12391 region.op(location[0], location[1], location[0] + mRight - mLeft,
12392 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12393 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12394 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12395 // exists, so we remove the background drawable's non-transparent
12396 // parts from this transparent region.
12397 applyDrawableToTransparentRegion(mBGDrawable, region);
12398 }
12399 }
12400 return true;
12401 }
12402
12403 /**
12404 * Play a sound effect for this view.
12405 *
12406 * <p>The framework will play sound effects for some built in actions, such as
12407 * clicking, but you may wish to play these effects in your widget,
12408 * for instance, for internal navigation.
12409 *
12410 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12411 * {@link #isSoundEffectsEnabled()} is true.
12412 *
12413 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12414 */
12415 public void playSoundEffect(int soundConstant) {
12416 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12417 return;
12418 }
12419 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12420 }
12421
12422 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012423 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012424 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012425 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012426 *
12427 * <p>The framework will provide haptic feedback for some built in actions,
12428 * such as long presses, but you may wish to provide feedback for your
12429 * own widget.
12430 *
12431 * <p>The feedback will only be performed if
12432 * {@link #isHapticFeedbackEnabled()} is true.
12433 *
12434 * @param feedbackConstant One of the constants defined in
12435 * {@link HapticFeedbackConstants}
12436 */
12437 public boolean performHapticFeedback(int feedbackConstant) {
12438 return performHapticFeedback(feedbackConstant, 0);
12439 }
12440
12441 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012442 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012443 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012444 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012445 *
12446 * @param feedbackConstant One of the constants defined in
12447 * {@link HapticFeedbackConstants}
12448 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12449 */
12450 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12451 if (mAttachInfo == null) {
12452 return false;
12453 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012454 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012455 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012456 && !isHapticFeedbackEnabled()) {
12457 return false;
12458 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012459 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12460 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012461 }
12462
12463 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012464 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012465 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012466 */
12467 public void setSystemUiVisibility(int visibility) {
12468 if (visibility != mSystemUiVisibility) {
12469 mSystemUiVisibility = visibility;
12470 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12471 mParent.recomputeViewAttributes(this);
12472 }
12473 }
12474 }
12475
12476 /**
12477 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012478 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012479 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012480 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012481 return mSystemUiVisibility;
12482 }
12483
Scott Mainec6331b2011-05-24 16:55:56 -070012484 /**
12485 * Set a listener to receive callbacks when the visibility of the system bar changes.
12486 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12487 */
Joe Onorato664644d2011-01-23 17:53:23 -080012488 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12489 mOnSystemUiVisibilityChangeListener = l;
12490 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12491 mParent.recomputeViewAttributes(this);
12492 }
12493 }
12494
12495 /**
12496 */
12497 public void dispatchSystemUiVisibilityChanged(int visibility) {
12498 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012499 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012500 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012501 }
12502 }
12503
12504 /**
Joe Malin32736f02011-01-19 16:14:20 -080012505 * Creates an image that the system displays during the drag and drop
12506 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12507 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12508 * appearance as the given View. The default also positions the center of the drag shadow
12509 * directly under the touch point. If no View is provided (the constructor with no parameters
12510 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12511 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12512 * default is an invisible drag shadow.
12513 * <p>
12514 * You are not required to use the View you provide to the constructor as the basis of the
12515 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12516 * anything you want as the drag shadow.
12517 * </p>
12518 * <p>
12519 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12520 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12521 * size and position of the drag shadow. It uses this data to construct a
12522 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12523 * so that your application can draw the shadow image in the Canvas.
12524 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012525 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012526 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012527 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012528
12529 /**
Joe Malin32736f02011-01-19 16:14:20 -080012530 * Constructs a shadow image builder based on a View. By default, the resulting drag
12531 * shadow will have the same appearance and dimensions as the View, with the touch point
12532 * over the center of the View.
12533 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012534 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012535 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012536 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012537 }
12538
Christopher Tate17ed60c2011-01-18 12:50:26 -080012539 /**
12540 * Construct a shadow builder object with no associated View. This
12541 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12542 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12543 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012544 * reference to any View object. If they are not overridden, then the result is an
12545 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012546 */
12547 public DragShadowBuilder() {
12548 mView = new WeakReference<View>(null);
12549 }
12550
12551 /**
12552 * Returns the View object that had been passed to the
12553 * {@link #View.DragShadowBuilder(View)}
12554 * constructor. If that View parameter was {@code null} or if the
12555 * {@link #View.DragShadowBuilder()}
12556 * constructor was used to instantiate the builder object, this method will return
12557 * null.
12558 *
12559 * @return The View object associate with this builder object.
12560 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012561 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012562 final public View getView() {
12563 return mView.get();
12564 }
12565
Christopher Tate2c095f32010-10-04 14:13:40 -070012566 /**
Joe Malin32736f02011-01-19 16:14:20 -080012567 * Provides the metrics for the shadow image. These include the dimensions of
12568 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012569 * be centered under the touch location while dragging.
12570 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012571 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012572 * same as the dimensions of the View itself and centers the shadow under
12573 * the touch point.
12574 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012575 *
Joe Malin32736f02011-01-19 16:14:20 -080012576 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12577 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12578 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12579 * image.
12580 *
12581 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12582 * shadow image that should be underneath the touch point during the drag and drop
12583 * operation. Your application must set {@link android.graphics.Point#x} to the
12584 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012585 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012586 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012587 final View view = mView.get();
12588 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012589 shadowSize.set(view.getWidth(), view.getHeight());
12590 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012591 } else {
12592 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12593 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012594 }
12595
12596 /**
Joe Malin32736f02011-01-19 16:14:20 -080012597 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12598 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012599 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012600 *
Joe Malin32736f02011-01-19 16:14:20 -080012601 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012602 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012603 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012604 final View view = mView.get();
12605 if (view != null) {
12606 view.draw(canvas);
12607 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012608 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012609 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012610 }
12611 }
12612
12613 /**
Joe Malin32736f02011-01-19 16:14:20 -080012614 * Starts a drag and drop operation. When your application calls this method, it passes a
12615 * {@link android.view.View.DragShadowBuilder} object to the system. The
12616 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12617 * to get metrics for the drag shadow, and then calls the object's
12618 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12619 * <p>
12620 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12621 * drag events to all the View objects in your application that are currently visible. It does
12622 * this either by calling the View object's drag listener (an implementation of
12623 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12624 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12625 * Both are passed a {@link android.view.DragEvent} object that has a
12626 * {@link android.view.DragEvent#getAction()} value of
12627 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12628 * </p>
12629 * <p>
12630 * Your application can invoke startDrag() on any attached View object. The View object does not
12631 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12632 * be related to the View the user selected for dragging.
12633 * </p>
12634 * @param data A {@link android.content.ClipData} object pointing to the data to be
12635 * transferred by the drag and drop operation.
12636 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12637 * drag shadow.
12638 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12639 * drop operation. This Object is put into every DragEvent object sent by the system during the
12640 * current drag.
12641 * <p>
12642 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12643 * to the target Views. For example, it can contain flags that differentiate between a
12644 * a copy operation and a move operation.
12645 * </p>
12646 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12647 * so the parameter should be set to 0.
12648 * @return {@code true} if the method completes successfully, or
12649 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12650 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012651 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012652 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012653 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012654 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012655 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012656 }
12657 boolean okay = false;
12658
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012659 Point shadowSize = new Point();
12660 Point shadowTouchPoint = new Point();
12661 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012662
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012663 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12664 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12665 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012666 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012667
Chris Tatea32dcf72010-10-14 12:13:50 -070012668 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012669 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12670 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012671 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012672 Surface surface = new Surface();
12673 try {
12674 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012675 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012676 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012677 + " surface=" + surface);
12678 if (token != null) {
12679 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012680 try {
Chris Tate6b391282010-10-14 15:48:59 -070012681 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012682 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012683 } finally {
12684 surface.unlockCanvasAndPost(canvas);
12685 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012686
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012687 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012688
12689 // Cache the local state object for delivery with DragEvents
12690 root.setLocalDragState(myLocalState);
12691
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012692 // repurpose 'shadowSize' for the last touch point
12693 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012694
Christopher Tatea53146c2010-09-07 11:57:52 -070012695 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012696 shadowSize.x, shadowSize.y,
12697 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012698 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012699 }
12700 } catch (Exception e) {
12701 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12702 surface.destroy();
12703 }
12704
12705 return okay;
12706 }
12707
Christopher Tatea53146c2010-09-07 11:57:52 -070012708 /**
Joe Malin32736f02011-01-19 16:14:20 -080012709 * Handles drag events sent by the system following a call to
12710 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12711 *<p>
12712 * When the system calls this method, it passes a
12713 * {@link android.view.DragEvent} object. A call to
12714 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12715 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12716 * operation.
12717 * @param event The {@link android.view.DragEvent} sent by the system.
12718 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12719 * in DragEvent, indicating the type of drag event represented by this object.
12720 * @return {@code true} if the method was successful, otherwise {@code false}.
12721 * <p>
12722 * The method should return {@code true} in response to an action type of
12723 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12724 * operation.
12725 * </p>
12726 * <p>
12727 * The method should also return {@code true} in response to an action type of
12728 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12729 * {@code false} if it didn't.
12730 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012731 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012732 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012733 return false;
12734 }
12735
12736 /**
Joe Malin32736f02011-01-19 16:14:20 -080012737 * Detects if this View is enabled and has a drag event listener.
12738 * If both are true, then it calls the drag event listener with the
12739 * {@link android.view.DragEvent} it received. If the drag event listener returns
12740 * {@code true}, then dispatchDragEvent() returns {@code true}.
12741 * <p>
12742 * For all other cases, the method calls the
12743 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12744 * method and returns its result.
12745 * </p>
12746 * <p>
12747 * This ensures that a drag event is always consumed, even if the View does not have a drag
12748 * event listener. However, if the View has a listener and the listener returns true, then
12749 * onDragEvent() is not called.
12750 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012751 */
12752 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012753 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012754 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12755 && mOnDragListener.onDrag(this, event)) {
12756 return true;
12757 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012758 return onDragEvent(event);
12759 }
12760
Christopher Tate3d4bf172011-03-28 16:16:46 -070012761 boolean canAcceptDrag() {
12762 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12763 }
12764
Christopher Tatea53146c2010-09-07 11:57:52 -070012765 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012766 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12767 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012768 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012769 */
12770 public void onCloseSystemDialogs(String reason) {
12771 }
Joe Malin32736f02011-01-19 16:14:20 -080012772
Dianne Hackbornffa42482009-09-23 22:20:11 -070012773 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012774 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012775 * update a Region being computed for
12776 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012777 * that any non-transparent parts of the Drawable are removed from the
12778 * given transparent region.
12779 *
12780 * @param dr The Drawable whose transparency is to be applied to the region.
12781 * @param region A Region holding the current transparency information,
12782 * where any parts of the region that are set are considered to be
12783 * transparent. On return, this region will be modified to have the
12784 * transparency information reduced by the corresponding parts of the
12785 * Drawable that are not transparent.
12786 * {@hide}
12787 */
12788 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12789 if (DBG) {
12790 Log.i("View", "Getting transparent region for: " + this);
12791 }
12792 final Region r = dr.getTransparentRegion();
12793 final Rect db = dr.getBounds();
12794 final AttachInfo attachInfo = mAttachInfo;
12795 if (r != null && attachInfo != null) {
12796 final int w = getRight()-getLeft();
12797 final int h = getBottom()-getTop();
12798 if (db.left > 0) {
12799 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12800 r.op(0, 0, db.left, h, Region.Op.UNION);
12801 }
12802 if (db.right < w) {
12803 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12804 r.op(db.right, 0, w, h, Region.Op.UNION);
12805 }
12806 if (db.top > 0) {
12807 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12808 r.op(0, 0, w, db.top, Region.Op.UNION);
12809 }
12810 if (db.bottom < h) {
12811 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12812 r.op(0, db.bottom, w, h, Region.Op.UNION);
12813 }
12814 final int[] location = attachInfo.mTransparentLocation;
12815 getLocationInWindow(location);
12816 r.translate(location[0], location[1]);
12817 region.op(r, Region.Op.INTERSECT);
12818 } else {
12819 region.op(db, Region.Op.DIFFERENCE);
12820 }
12821 }
12822
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012823 private void checkForLongClick(int delayOffset) {
12824 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12825 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012826
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012827 if (mPendingCheckForLongPress == null) {
12828 mPendingCheckForLongPress = new CheckForLongPress();
12829 }
12830 mPendingCheckForLongPress.rememberWindowAttachCount();
12831 postDelayed(mPendingCheckForLongPress,
12832 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012833 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012834 }
12835
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012836 /**
12837 * Inflate a view from an XML resource. This convenience method wraps the {@link
12838 * LayoutInflater} class, which provides a full range of options for view inflation.
12839 *
12840 * @param context The Context object for your activity or application.
12841 * @param resource The resource ID to inflate
12842 * @param root A view group that will be the parent. Used to properly inflate the
12843 * layout_* parameters.
12844 * @see LayoutInflater
12845 */
12846 public static View inflate(Context context, int resource, ViewGroup root) {
12847 LayoutInflater factory = LayoutInflater.from(context);
12848 return factory.inflate(resource, root);
12849 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012850
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012851 /**
Adam Powell637d3372010-08-25 14:37:03 -070012852 * Scroll the view with standard behavior for scrolling beyond the normal
12853 * content boundaries. Views that call this method should override
12854 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12855 * results of an over-scroll operation.
12856 *
12857 * Views can use this method to handle any touch or fling-based scrolling.
12858 *
12859 * @param deltaX Change in X in pixels
12860 * @param deltaY Change in Y in pixels
12861 * @param scrollX Current X scroll value in pixels before applying deltaX
12862 * @param scrollY Current Y scroll value in pixels before applying deltaY
12863 * @param scrollRangeX Maximum content scroll range along the X axis
12864 * @param scrollRangeY Maximum content scroll range along the Y axis
12865 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12866 * along the X axis.
12867 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12868 * along the Y axis.
12869 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12870 * @return true if scrolling was clamped to an over-scroll boundary along either
12871 * axis, false otherwise.
12872 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012873 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012874 protected boolean overScrollBy(int deltaX, int deltaY,
12875 int scrollX, int scrollY,
12876 int scrollRangeX, int scrollRangeY,
12877 int maxOverScrollX, int maxOverScrollY,
12878 boolean isTouchEvent) {
12879 final int overScrollMode = mOverScrollMode;
12880 final boolean canScrollHorizontal =
12881 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12882 final boolean canScrollVertical =
12883 computeVerticalScrollRange() > computeVerticalScrollExtent();
12884 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12885 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12886 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12887 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12888
12889 int newScrollX = scrollX + deltaX;
12890 if (!overScrollHorizontal) {
12891 maxOverScrollX = 0;
12892 }
12893
12894 int newScrollY = scrollY + deltaY;
12895 if (!overScrollVertical) {
12896 maxOverScrollY = 0;
12897 }
12898
12899 // Clamp values if at the limits and record
12900 final int left = -maxOverScrollX;
12901 final int right = maxOverScrollX + scrollRangeX;
12902 final int top = -maxOverScrollY;
12903 final int bottom = maxOverScrollY + scrollRangeY;
12904
12905 boolean clampedX = false;
12906 if (newScrollX > right) {
12907 newScrollX = right;
12908 clampedX = true;
12909 } else if (newScrollX < left) {
12910 newScrollX = left;
12911 clampedX = true;
12912 }
12913
12914 boolean clampedY = false;
12915 if (newScrollY > bottom) {
12916 newScrollY = bottom;
12917 clampedY = true;
12918 } else if (newScrollY < top) {
12919 newScrollY = top;
12920 clampedY = true;
12921 }
12922
12923 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12924
12925 return clampedX || clampedY;
12926 }
12927
12928 /**
12929 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12930 * respond to the results of an over-scroll operation.
12931 *
12932 * @param scrollX New X scroll value in pixels
12933 * @param scrollY New Y scroll value in pixels
12934 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12935 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12936 */
12937 protected void onOverScrolled(int scrollX, int scrollY,
12938 boolean clampedX, boolean clampedY) {
12939 // Intentionally empty.
12940 }
12941
12942 /**
12943 * Returns the over-scroll mode for this view. The result will be
12944 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12945 * (allow over-scrolling only if the view content is larger than the container),
12946 * or {@link #OVER_SCROLL_NEVER}.
12947 *
12948 * @return This view's over-scroll mode.
12949 */
12950 public int getOverScrollMode() {
12951 return mOverScrollMode;
12952 }
12953
12954 /**
12955 * Set the over-scroll mode for this view. Valid over-scroll modes are
12956 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12957 * (allow over-scrolling only if the view content is larger than the container),
12958 * or {@link #OVER_SCROLL_NEVER}.
12959 *
12960 * Setting the over-scroll mode of a view will have an effect only if the
12961 * view is capable of scrolling.
12962 *
12963 * @param overScrollMode The new over-scroll mode for this view.
12964 */
12965 public void setOverScrollMode(int overScrollMode) {
12966 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12967 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12968 overScrollMode != OVER_SCROLL_NEVER) {
12969 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12970 }
12971 mOverScrollMode = overScrollMode;
12972 }
12973
12974 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012975 * Gets a scale factor that determines the distance the view should scroll
12976 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12977 * @return The vertical scroll scale factor.
12978 * @hide
12979 */
12980 protected float getVerticalScrollFactor() {
12981 if (mVerticalScrollFactor == 0) {
12982 TypedValue outValue = new TypedValue();
12983 if (!mContext.getTheme().resolveAttribute(
12984 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12985 throw new IllegalStateException(
12986 "Expected theme to define listPreferredItemHeight.");
12987 }
12988 mVerticalScrollFactor = outValue.getDimension(
12989 mContext.getResources().getDisplayMetrics());
12990 }
12991 return mVerticalScrollFactor;
12992 }
12993
12994 /**
12995 * Gets a scale factor that determines the distance the view should scroll
12996 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
12997 * @return The horizontal scroll scale factor.
12998 * @hide
12999 */
13000 protected float getHorizontalScrollFactor() {
13001 // TODO: Should use something else.
13002 return getVerticalScrollFactor();
13003 }
13004
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013005 /**
13006 * Return the value specifying the text direction or policy that was set with
13007 * {@link #setTextDirection(int)}.
13008 *
13009 * @return the defined text direction. It can be one of:
13010 *
13011 * {@link #TEXT_DIRECTION_INHERIT},
13012 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13013 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013014 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013015 * {@link #TEXT_DIRECTION_LTR},
13016 * {@link #TEXT_DIRECTION_RTL},
13017 *
13018 * @hide
13019 */
13020 public int getTextDirection() {
13021 return mTextDirection;
13022 }
13023
13024 /**
13025 * Set the text direction.
13026 *
13027 * @param textDirection the direction to set. Should be one of:
13028 *
13029 * {@link #TEXT_DIRECTION_INHERIT},
13030 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13031 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013032 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013033 * {@link #TEXT_DIRECTION_LTR},
13034 * {@link #TEXT_DIRECTION_RTL},
13035 *
13036 * @hide
13037 */
13038 public void setTextDirection(int textDirection) {
13039 if (textDirection != mTextDirection) {
13040 mTextDirection = textDirection;
13041 resetResolvedTextDirection();
13042 requestLayout();
13043 }
13044 }
13045
13046 /**
13047 * Return the resolved text direction.
13048 *
13049 * @return the resolved text direction. Return one of:
13050 *
13051 * {@link #TEXT_DIRECTION_LTR},
13052 * {@link #TEXT_DIRECTION_RTL},
13053 *
13054 * @hide
13055 */
13056 public int getResolvedTextDirection() {
13057 if (!isResolvedTextDirection()) {
13058 resolveTextDirection();
13059 }
13060 return mResolvedTextDirection;
13061 }
13062
13063 /**
13064 * Resolve the text direction. Classes that extend View and want to do a specific text direction
13065 * resolution will need to implement this method and set the mResolvedTextDirection to
13066 * either TEXT_DIRECTION_LTR if direction is LTR or TEXT_DIRECTION_RTL if
13067 * direction is RTL.
13068 */
13069 protected void resolveTextDirection() {
13070 }
13071
13072 /**
13073 * Return if the text direction has been resolved or not.
13074 *
13075 * @return true, if resolved and false if not resolved
13076 *
13077 * @hide
13078 */
13079 public boolean isResolvedTextDirection() {
13080 return (mResolvedTextDirection != TEXT_DIRECTION_UNDEFINED);
13081 }
13082
13083 /**
13084 * Reset resolved text direction. Will be resolved during a call to getResolvedLayoutDirection().
13085 */
13086 protected void resetResolvedTextDirection() {
13087 mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
13088 }
13089
Chet Haaseb39f0512011-05-24 14:36:40 -070013090 //
13091 // Properties
13092 //
13093 /**
13094 * A Property wrapper around the <code>alpha</code> functionality handled by the
13095 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13096 */
Romain Guy02ccac62011-06-24 13:20:23 -070013097 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013098 @Override
13099 public void setValue(View object, float value) {
13100 object.setAlpha(value);
13101 }
13102
13103 @Override
13104 public Float get(View object) {
13105 return object.getAlpha();
13106 }
13107 };
13108
13109 /**
13110 * A Property wrapper around the <code>translationX</code> functionality handled by the
13111 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13112 */
13113 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13114 @Override
13115 public void setValue(View object, float value) {
13116 object.setTranslationX(value);
13117 }
13118
13119 @Override
13120 public Float get(View object) {
13121 return object.getTranslationX();
13122 }
13123 };
13124
13125 /**
13126 * A Property wrapper around the <code>translationY</code> functionality handled by the
13127 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13128 */
13129 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13130 @Override
13131 public void setValue(View object, float value) {
13132 object.setTranslationY(value);
13133 }
13134
13135 @Override
13136 public Float get(View object) {
13137 return object.getTranslationY();
13138 }
13139 };
13140
13141 /**
13142 * A Property wrapper around the <code>x</code> functionality handled by the
13143 * {@link View#setX(float)} and {@link View#getX()} methods.
13144 */
13145 public static Property<View, Float> X = new FloatProperty<View>("x") {
13146 @Override
13147 public void setValue(View object, float value) {
13148 object.setX(value);
13149 }
13150
13151 @Override
13152 public Float get(View object) {
13153 return object.getX();
13154 }
13155 };
13156
13157 /**
13158 * A Property wrapper around the <code>y</code> functionality handled by the
13159 * {@link View#setY(float)} and {@link View#getY()} methods.
13160 */
13161 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13162 @Override
13163 public void setValue(View object, float value) {
13164 object.setY(value);
13165 }
13166
13167 @Override
13168 public Float get(View object) {
13169 return object.getY();
13170 }
13171 };
13172
13173 /**
13174 * A Property wrapper around the <code>rotation</code> functionality handled by the
13175 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13176 */
13177 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13178 @Override
13179 public void setValue(View object, float value) {
13180 object.setRotation(value);
13181 }
13182
13183 @Override
13184 public Float get(View object) {
13185 return object.getRotation();
13186 }
13187 };
13188
13189 /**
13190 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13191 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13192 */
13193 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13194 @Override
13195 public void setValue(View object, float value) {
13196 object.setRotationX(value);
13197 }
13198
13199 @Override
13200 public Float get(View object) {
13201 return object.getRotationX();
13202 }
13203 };
13204
13205 /**
13206 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13207 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13208 */
13209 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13210 @Override
13211 public void setValue(View object, float value) {
13212 object.setRotationY(value);
13213 }
13214
13215 @Override
13216 public Float get(View object) {
13217 return object.getRotationY();
13218 }
13219 };
13220
13221 /**
13222 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13223 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13224 */
13225 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13226 @Override
13227 public void setValue(View object, float value) {
13228 object.setScaleX(value);
13229 }
13230
13231 @Override
13232 public Float get(View object) {
13233 return object.getScaleX();
13234 }
13235 };
13236
13237 /**
13238 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13239 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13240 */
13241 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13242 @Override
13243 public void setValue(View object, float value) {
13244 object.setScaleY(value);
13245 }
13246
13247 @Override
13248 public Float get(View object) {
13249 return object.getScaleY();
13250 }
13251 };
13252
Jeff Brown33bbfd22011-02-24 20:55:35 -080013253 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013254 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13255 * Each MeasureSpec represents a requirement for either the width or the height.
13256 * A MeasureSpec is comprised of a size and a mode. There are three possible
13257 * modes:
13258 * <dl>
13259 * <dt>UNSPECIFIED</dt>
13260 * <dd>
13261 * The parent has not imposed any constraint on the child. It can be whatever size
13262 * it wants.
13263 * </dd>
13264 *
13265 * <dt>EXACTLY</dt>
13266 * <dd>
13267 * The parent has determined an exact size for the child. The child is going to be
13268 * given those bounds regardless of how big it wants to be.
13269 * </dd>
13270 *
13271 * <dt>AT_MOST</dt>
13272 * <dd>
13273 * The child can be as large as it wants up to the specified size.
13274 * </dd>
13275 * </dl>
13276 *
13277 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13278 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13279 */
13280 public static class MeasureSpec {
13281 private static final int MODE_SHIFT = 30;
13282 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13283
13284 /**
13285 * Measure specification mode: The parent has not imposed any constraint
13286 * on the child. It can be whatever size it wants.
13287 */
13288 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13289
13290 /**
13291 * Measure specification mode: The parent has determined an exact size
13292 * for the child. The child is going to be given those bounds regardless
13293 * of how big it wants to be.
13294 */
13295 public static final int EXACTLY = 1 << MODE_SHIFT;
13296
13297 /**
13298 * Measure specification mode: The child can be as large as it wants up
13299 * to the specified size.
13300 */
13301 public static final int AT_MOST = 2 << MODE_SHIFT;
13302
13303 /**
13304 * Creates a measure specification based on the supplied size and mode.
13305 *
13306 * The mode must always be one of the following:
13307 * <ul>
13308 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13309 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13310 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13311 * </ul>
13312 *
13313 * @param size the size of the measure specification
13314 * @param mode the mode of the measure specification
13315 * @return the measure specification based on size and mode
13316 */
13317 public static int makeMeasureSpec(int size, int mode) {
13318 return size + mode;
13319 }
13320
13321 /**
13322 * Extracts the mode from the supplied measure specification.
13323 *
13324 * @param measureSpec the measure specification to extract the mode from
13325 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13326 * {@link android.view.View.MeasureSpec#AT_MOST} or
13327 * {@link android.view.View.MeasureSpec#EXACTLY}
13328 */
13329 public static int getMode(int measureSpec) {
13330 return (measureSpec & MODE_MASK);
13331 }
13332
13333 /**
13334 * Extracts the size from the supplied measure specification.
13335 *
13336 * @param measureSpec the measure specification to extract the size from
13337 * @return the size in pixels defined in the supplied measure specification
13338 */
13339 public static int getSize(int measureSpec) {
13340 return (measureSpec & ~MODE_MASK);
13341 }
13342
13343 /**
13344 * Returns a String representation of the specified measure
13345 * specification.
13346 *
13347 * @param measureSpec the measure specification to convert to a String
13348 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13349 */
13350 public static String toString(int measureSpec) {
13351 int mode = getMode(measureSpec);
13352 int size = getSize(measureSpec);
13353
13354 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13355
13356 if (mode == UNSPECIFIED)
13357 sb.append("UNSPECIFIED ");
13358 else if (mode == EXACTLY)
13359 sb.append("EXACTLY ");
13360 else if (mode == AT_MOST)
13361 sb.append("AT_MOST ");
13362 else
13363 sb.append(mode).append(" ");
13364
13365 sb.append(size);
13366 return sb.toString();
13367 }
13368 }
13369
13370 class CheckForLongPress implements Runnable {
13371
13372 private int mOriginalWindowAttachCount;
13373
13374 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013375 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013376 && mOriginalWindowAttachCount == mWindowAttachCount) {
13377 if (performLongClick()) {
13378 mHasPerformedLongPress = true;
13379 }
13380 }
13381 }
13382
13383 public void rememberWindowAttachCount() {
13384 mOriginalWindowAttachCount = mWindowAttachCount;
13385 }
13386 }
Joe Malin32736f02011-01-19 16:14:20 -080013387
Adam Powelle14579b2009-12-16 18:39:52 -080013388 private final class CheckForTap implements Runnable {
13389 public void run() {
13390 mPrivateFlags &= ~PREPRESSED;
13391 mPrivateFlags |= PRESSED;
13392 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013393 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013394 }
13395 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013396
Adam Powella35d7682010-03-12 14:48:13 -080013397 private final class PerformClick implements Runnable {
13398 public void run() {
13399 performClick();
13400 }
13401 }
13402
Dianne Hackborn63042d62011-01-26 18:56:29 -080013403 /** @hide */
13404 public void hackTurnOffWindowResizeAnim(boolean off) {
13405 mAttachInfo.mTurnOffWindowResizeAnim = off;
13406 }
Joe Malin32736f02011-01-19 16:14:20 -080013407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013408 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013409 * This method returns a ViewPropertyAnimator object, which can be used to animate
13410 * specific properties on this View.
13411 *
13412 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13413 */
13414 public ViewPropertyAnimator animate() {
13415 if (mAnimator == null) {
13416 mAnimator = new ViewPropertyAnimator(this);
13417 }
13418 return mAnimator;
13419 }
13420
13421 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013422 * Interface definition for a callback to be invoked when a key event is
13423 * dispatched to this view. The callback will be invoked before the key
13424 * event is given to the view.
13425 */
13426 public interface OnKeyListener {
13427 /**
13428 * Called when a key is dispatched to a view. This allows listeners to
13429 * get a chance to respond before the target view.
13430 *
13431 * @param v The view the key has been dispatched to.
13432 * @param keyCode The code for the physical key that was pressed
13433 * @param event The KeyEvent object containing full information about
13434 * the event.
13435 * @return True if the listener has consumed the event, false otherwise.
13436 */
13437 boolean onKey(View v, int keyCode, KeyEvent event);
13438 }
13439
13440 /**
13441 * Interface definition for a callback to be invoked when a touch event is
13442 * dispatched to this view. The callback will be invoked before the touch
13443 * event is given to the view.
13444 */
13445 public interface OnTouchListener {
13446 /**
13447 * Called when a touch event is dispatched to a view. This allows listeners to
13448 * get a chance to respond before the target view.
13449 *
13450 * @param v The view the touch event has been dispatched to.
13451 * @param event The MotionEvent object containing full information about
13452 * the event.
13453 * @return True if the listener has consumed the event, false otherwise.
13454 */
13455 boolean onTouch(View v, MotionEvent event);
13456 }
13457
13458 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013459 * Interface definition for a callback to be invoked when a hover event is
13460 * dispatched to this view. The callback will be invoked before the hover
13461 * event is given to the view.
13462 */
13463 public interface OnHoverListener {
13464 /**
13465 * Called when a hover event is dispatched to a view. This allows listeners to
13466 * get a chance to respond before the target view.
13467 *
13468 * @param v The view the hover event has been dispatched to.
13469 * @param event The MotionEvent object containing full information about
13470 * the event.
13471 * @return True if the listener has consumed the event, false otherwise.
13472 */
13473 boolean onHover(View v, MotionEvent event);
13474 }
13475
13476 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013477 * Interface definition for a callback to be invoked when a generic motion event is
13478 * dispatched to this view. The callback will be invoked before the generic motion
13479 * event is given to the view.
13480 */
13481 public interface OnGenericMotionListener {
13482 /**
13483 * Called when a generic motion event is dispatched to a view. This allows listeners to
13484 * get a chance to respond before the target view.
13485 *
13486 * @param v The view the generic motion event has been dispatched to.
13487 * @param event The MotionEvent object containing full information about
13488 * the event.
13489 * @return True if the listener has consumed the event, false otherwise.
13490 */
13491 boolean onGenericMotion(View v, MotionEvent event);
13492 }
13493
13494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495 * Interface definition for a callback to be invoked when a view has been clicked and held.
13496 */
13497 public interface OnLongClickListener {
13498 /**
13499 * Called when a view has been clicked and held.
13500 *
13501 * @param v The view that was clicked and held.
13502 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013503 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013504 */
13505 boolean onLongClick(View v);
13506 }
13507
13508 /**
Chris Tate32affef2010-10-18 15:29:21 -070013509 * Interface definition for a callback to be invoked when a drag is being dispatched
13510 * to this view. The callback will be invoked before the hosting view's own
13511 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13512 * onDrag(event) behavior, it should return 'false' from this callback.
13513 */
13514 public interface OnDragListener {
13515 /**
13516 * Called when a drag event is dispatched to a view. This allows listeners
13517 * to get a chance to override base View behavior.
13518 *
Joe Malin32736f02011-01-19 16:14:20 -080013519 * @param v The View that received the drag event.
13520 * @param event The {@link android.view.DragEvent} object for the drag event.
13521 * @return {@code true} if the drag event was handled successfully, or {@code false}
13522 * if the drag event was not handled. Note that {@code false} will trigger the View
13523 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013524 */
13525 boolean onDrag(View v, DragEvent event);
13526 }
13527
13528 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013529 * Interface definition for a callback to be invoked when the focus state of
13530 * a view changed.
13531 */
13532 public interface OnFocusChangeListener {
13533 /**
13534 * Called when the focus state of a view has changed.
13535 *
13536 * @param v The view whose state has changed.
13537 * @param hasFocus The new focus state of v.
13538 */
13539 void onFocusChange(View v, boolean hasFocus);
13540 }
13541
13542 /**
13543 * Interface definition for a callback to be invoked when a view is clicked.
13544 */
13545 public interface OnClickListener {
13546 /**
13547 * Called when a view has been clicked.
13548 *
13549 * @param v The view that was clicked.
13550 */
13551 void onClick(View v);
13552 }
13553
13554 /**
13555 * Interface definition for a callback to be invoked when the context menu
13556 * for this view is being built.
13557 */
13558 public interface OnCreateContextMenuListener {
13559 /**
13560 * Called when the context menu for this view is being built. It is not
13561 * safe to hold onto the menu after this method returns.
13562 *
13563 * @param menu The context menu that is being built
13564 * @param v The view for which the context menu is being built
13565 * @param menuInfo Extra information about the item for which the
13566 * context menu should be shown. This information will vary
13567 * depending on the class of v.
13568 */
13569 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13570 }
13571
Joe Onorato664644d2011-01-23 17:53:23 -080013572 /**
13573 * Interface definition for a callback to be invoked when the status bar changes
13574 * visibility.
13575 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013576 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013577 */
13578 public interface OnSystemUiVisibilityChangeListener {
13579 /**
13580 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013581 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013582 *
13583 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13584 */
13585 public void onSystemUiVisibilityChange(int visibility);
13586 }
13587
Adam Powell4afd62b2011-02-18 15:02:18 -080013588 /**
13589 * Interface definition for a callback to be invoked when this view is attached
13590 * or detached from its window.
13591 */
13592 public interface OnAttachStateChangeListener {
13593 /**
13594 * Called when the view is attached to a window.
13595 * @param v The view that was attached
13596 */
13597 public void onViewAttachedToWindow(View v);
13598 /**
13599 * Called when the view is detached from a window.
13600 * @param v The view that was detached
13601 */
13602 public void onViewDetachedFromWindow(View v);
13603 }
13604
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013605 private final class UnsetPressedState implements Runnable {
13606 public void run() {
13607 setPressed(false);
13608 }
13609 }
13610
13611 /**
13612 * Base class for derived classes that want to save and restore their own
13613 * state in {@link android.view.View#onSaveInstanceState()}.
13614 */
13615 public static class BaseSavedState extends AbsSavedState {
13616 /**
13617 * Constructor used when reading from a parcel. Reads the state of the superclass.
13618 *
13619 * @param source
13620 */
13621 public BaseSavedState(Parcel source) {
13622 super(source);
13623 }
13624
13625 /**
13626 * Constructor called by derived classes when creating their SavedState objects
13627 *
13628 * @param superState The state of the superclass of this view
13629 */
13630 public BaseSavedState(Parcelable superState) {
13631 super(superState);
13632 }
13633
13634 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13635 new Parcelable.Creator<BaseSavedState>() {
13636 public BaseSavedState createFromParcel(Parcel in) {
13637 return new BaseSavedState(in);
13638 }
13639
13640 public BaseSavedState[] newArray(int size) {
13641 return new BaseSavedState[size];
13642 }
13643 };
13644 }
13645
13646 /**
13647 * A set of information given to a view when it is attached to its parent
13648 * window.
13649 */
13650 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013651 interface Callbacks {
13652 void playSoundEffect(int effectId);
13653 boolean performHapticFeedback(int effectId, boolean always);
13654 }
13655
13656 /**
13657 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13658 * to a Handler. This class contains the target (View) to invalidate and
13659 * the coordinates of the dirty rectangle.
13660 *
13661 * For performance purposes, this class also implements a pool of up to
13662 * POOL_LIMIT objects that get reused. This reduces memory allocations
13663 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013664 */
Romain Guyd928d682009-03-31 17:52:16 -070013665 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013666 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013667 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13668 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013669 public InvalidateInfo newInstance() {
13670 return new InvalidateInfo();
13671 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672
Romain Guyd928d682009-03-31 17:52:16 -070013673 public void onAcquired(InvalidateInfo element) {
13674 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013675
Romain Guyd928d682009-03-31 17:52:16 -070013676 public void onReleased(InvalidateInfo element) {
13677 }
13678 }, POOL_LIMIT)
13679 );
13680
13681 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013682 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013683
13684 View target;
13685
13686 int left;
13687 int top;
13688 int right;
13689 int bottom;
13690
Romain Guyd928d682009-03-31 17:52:16 -070013691 public void setNextPoolable(InvalidateInfo element) {
13692 mNext = element;
13693 }
13694
13695 public InvalidateInfo getNextPoolable() {
13696 return mNext;
13697 }
13698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013699 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013700 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013701 }
13702
13703 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013704 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013705 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013706
13707 public boolean isPooled() {
13708 return mIsPooled;
13709 }
13710
13711 public void setPooled(boolean isPooled) {
13712 mIsPooled = isPooled;
13713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013714 }
13715
13716 final IWindowSession mSession;
13717
13718 final IWindow mWindow;
13719
13720 final IBinder mWindowToken;
13721
13722 final Callbacks mRootCallbacks;
13723
Romain Guy59a12ca2011-06-09 17:48:21 -070013724 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013725
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013726 /**
13727 * The top view of the hierarchy.
13728 */
13729 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013730
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013731 IBinder mPanelParentWindowToken;
13732 Surface mSurface;
13733
Romain Guyb051e892010-09-28 19:09:36 -070013734 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013735 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013736 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013737
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013739 * Scale factor used by the compatibility mode
13740 */
13741 float mApplicationScale;
13742
13743 /**
13744 * Indicates whether the application is in compatibility mode
13745 */
13746 boolean mScalingRequired;
13747
13748 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013749 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013750 */
13751 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013752
Dianne Hackborn63042d62011-01-26 18:56:29 -080013753 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013754 * Left position of this view's window
13755 */
13756 int mWindowLeft;
13757
13758 /**
13759 * Top position of this view's window
13760 */
13761 int mWindowTop;
13762
13763 /**
Adam Powell26153a32010-11-08 15:22:27 -080013764 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013765 */
Adam Powell26153a32010-11-08 15:22:27 -080013766 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013767
13768 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013769 * For windows that are full-screen but using insets to layout inside
13770 * of the screen decorations, these are the current insets for the
13771 * content of the window.
13772 */
13773 final Rect mContentInsets = new Rect();
13774
13775 /**
13776 * For windows that are full-screen but using insets to layout inside
13777 * of the screen decorations, these are the current insets for the
13778 * actual visible parts of the window.
13779 */
13780 final Rect mVisibleInsets = new Rect();
13781
13782 /**
13783 * The internal insets given by this window. This value is
13784 * supplied by the client (through
13785 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13786 * be given to the window manager when changed to be used in laying
13787 * out windows behind it.
13788 */
13789 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13790 = new ViewTreeObserver.InternalInsetsInfo();
13791
13792 /**
13793 * All views in the window's hierarchy that serve as scroll containers,
13794 * used to determine if the window can be resized or must be panned
13795 * to adjust for a soft input area.
13796 */
13797 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13798
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013799 final KeyEvent.DispatcherState mKeyDispatchState
13800 = new KeyEvent.DispatcherState();
13801
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013802 /**
13803 * Indicates whether the view's window currently has the focus.
13804 */
13805 boolean mHasWindowFocus;
13806
13807 /**
13808 * The current visibility of the window.
13809 */
13810 int mWindowVisibility;
13811
13812 /**
13813 * Indicates the time at which drawing started to occur.
13814 */
13815 long mDrawingTime;
13816
13817 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013818 * Indicates whether or not ignoring the DIRTY_MASK flags.
13819 */
13820 boolean mIgnoreDirtyState;
13821
13822 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013823 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13824 * to avoid clearing that flag prematurely.
13825 */
13826 boolean mSetIgnoreDirtyState = false;
13827
13828 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013829 * Indicates whether the view's window is currently in touch mode.
13830 */
13831 boolean mInTouchMode;
13832
13833 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013834 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 * the next time it performs a traversal
13836 */
13837 boolean mRecomputeGlobalAttributes;
13838
13839 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013840 * Set during a traveral if any views want to keep the screen on.
13841 */
13842 boolean mKeepScreenOn;
13843
13844 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013845 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13846 */
13847 int mSystemUiVisibility;
13848
13849 /**
13850 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13851 * attached.
13852 */
13853 boolean mHasSystemUiListeners;
13854
13855 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013856 * Set if the visibility of any views has changed.
13857 */
13858 boolean mViewVisibilityChanged;
13859
13860 /**
13861 * Set to true if a view has been scrolled.
13862 */
13863 boolean mViewScrollChanged;
13864
13865 /**
13866 * Global to the view hierarchy used as a temporary for dealing with
13867 * x/y points in the transparent region computations.
13868 */
13869 final int[] mTransparentLocation = new int[2];
13870
13871 /**
13872 * Global to the view hierarchy used as a temporary for dealing with
13873 * x/y points in the ViewGroup.invalidateChild implementation.
13874 */
13875 final int[] mInvalidateChildLocation = new int[2];
13876
Chet Haasec3aa3612010-06-17 08:50:37 -070013877
13878 /**
13879 * Global to the view hierarchy used as a temporary for dealing with
13880 * x/y location when view is transformed.
13881 */
13882 final float[] mTmpTransformLocation = new float[2];
13883
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013884 /**
13885 * The view tree observer used to dispatch global events like
13886 * layout, pre-draw, touch mode change, etc.
13887 */
13888 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13889
13890 /**
13891 * A Canvas used by the view hierarchy to perform bitmap caching.
13892 */
13893 Canvas mCanvas;
13894
13895 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013896 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013897 * handler can be used to pump events in the UI events queue.
13898 */
13899 final Handler mHandler;
13900
13901 /**
13902 * Identifier for messages requesting the view to be invalidated.
13903 * Such messages should be sent to {@link #mHandler}.
13904 */
13905 static final int INVALIDATE_MSG = 0x1;
13906
13907 /**
13908 * Identifier for messages requesting the view to invalidate a region.
13909 * Such messages should be sent to {@link #mHandler}.
13910 */
13911 static final int INVALIDATE_RECT_MSG = 0x2;
13912
13913 /**
13914 * Temporary for use in computing invalidate rectangles while
13915 * calling up the hierarchy.
13916 */
13917 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013918
13919 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013920 * Temporary for use in computing hit areas with transformed views
13921 */
13922 final RectF mTmpTransformRect = new RectF();
13923
13924 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013925 * Temporary list for use in collecting focusable descendents of a view.
13926 */
13927 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13928
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013929 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013930 * The id of the window for accessibility purposes.
13931 */
13932 int mAccessibilityWindowId = View.NO_ID;
13933
13934 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 * Creates a new set of attachment information with the specified
13936 * events handler and thread.
13937 *
13938 * @param handler the events handler the view must use
13939 */
13940 AttachInfo(IWindowSession session, IWindow window,
13941 Handler handler, Callbacks effectPlayer) {
13942 mSession = session;
13943 mWindow = window;
13944 mWindowToken = window.asBinder();
13945 mHandler = handler;
13946 mRootCallbacks = effectPlayer;
13947 }
13948 }
13949
13950 /**
13951 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13952 * is supported. This avoids keeping too many unused fields in most
13953 * instances of View.</p>
13954 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013955 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013956
Mike Cleronf116bf82009-09-27 19:14:12 -070013957 /**
13958 * Scrollbars are not visible
13959 */
13960 public static final int OFF = 0;
13961
13962 /**
13963 * Scrollbars are visible
13964 */
13965 public static final int ON = 1;
13966
13967 /**
13968 * Scrollbars are fading away
13969 */
13970 public static final int FADING = 2;
13971
13972 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013975 public int scrollBarDefaultDelayBeforeFade;
13976 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013977
13978 public int scrollBarSize;
13979 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013980 public float[] interpolatorValues;
13981 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013982
13983 public final Paint paint;
13984 public final Matrix matrix;
13985 public Shader shader;
13986
Mike Cleronf116bf82009-09-27 19:14:12 -070013987 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13988
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013989 private static final float[] OPAQUE = { 255 };
13990 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013991
Mike Cleronf116bf82009-09-27 19:14:12 -070013992 /**
13993 * When fading should start. This time moves into the future every time
13994 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
13995 */
13996 public long fadeStartTime;
13997
13998
13999 /**
14000 * The current state of the scrollbars: ON, OFF, or FADING
14001 */
14002 public int state = OFF;
14003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 private int mLastColor;
14005
Mike Cleronf116bf82009-09-27 19:14:12 -070014006 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014007 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14008 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014009 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14010 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014011
14012 paint = new Paint();
14013 matrix = new Matrix();
14014 // use use a height of 1, and then wack the matrix each time we
14015 // actually use it.
14016 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014018 paint.setShader(shader);
14019 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014020 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014021 }
Romain Guy8506ab42009-06-11 17:35:47 -070014022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014023 public void setFadeColor(int color) {
14024 if (color != 0 && color != mLastColor) {
14025 mLastColor = color;
14026 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014027
Romain Guye55e1a72009-08-27 10:42:26 -070014028 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14029 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014031 paint.setShader(shader);
14032 // Restore the default transfer mode (src_over)
14033 paint.setXfermode(null);
14034 }
14035 }
Joe Malin32736f02011-01-19 16:14:20 -080014036
Mike Cleronf116bf82009-09-27 19:14:12 -070014037 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014038 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014039 if (now >= fadeStartTime) {
14040
14041 // the animation fades the scrollbars out by changing
14042 // the opacity (alpha) from fully opaque to fully
14043 // transparent
14044 int nextFrame = (int) now;
14045 int framesCount = 0;
14046
14047 Interpolator interpolator = scrollBarInterpolator;
14048
14049 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014050 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014051
14052 // End transparent
14053 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014054 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014055
14056 state = FADING;
14057
14058 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014059 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014060 }
14061 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014062 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014063
Svetoslav Ganova0156172011-06-26 17:55:44 -070014064 /**
14065 * Resuable callback for sending
14066 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14067 */
14068 private class SendViewScrolledAccessibilityEvent implements Runnable {
14069 public volatile boolean mIsPending;
14070
14071 public void run() {
14072 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14073 mIsPending = false;
14074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 }
14076}