blob: 8627ab2b1a1b1504985648a84fa356dd319b69f5 [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.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002170 *
2171 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002172 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002173 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002174 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175
2176 /**
2177 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002178 *
2179 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002181 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002182 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002183
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002184 /**
Adam Powell20232d02010-12-08 21:08:53 -08002185 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002186 *
2187 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002188 */
2189 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002190 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002191
2192 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002193 * Cache if the user padding is relative.
2194 *
2195 */
2196 @ViewDebug.ExportedProperty(category = "padding")
2197 boolean mUserPaddingRelative;
2198
2199 /**
2200 * Cache the paddingStart set by the user to append to the scrollbar's size.
2201 *
2202 */
2203 @ViewDebug.ExportedProperty(category = "padding")
2204 int mUserPaddingStart;
2205
2206 /**
2207 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2208 *
2209 */
2210 @ViewDebug.ExportedProperty(category = "padding")
2211 int mUserPaddingEnd;
2212
2213 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002214 * @hide
2215 */
2216 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2217 /**
2218 * @hide
2219 */
2220 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
2222 private Resources mResources = null;
2223
2224 private Drawable mBGDrawable;
2225
2226 private int mBackgroundResource;
2227 private boolean mBackgroundSizeChanged;
2228
2229 /**
2230 * Listener used to dispatch focus change events.
2231 * This field should be made private, so it is hidden from the SDK.
2232 * {@hide}
2233 */
2234 protected OnFocusChangeListener mOnFocusChangeListener;
2235
2236 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002237 * Listeners for layout change events.
2238 */
2239 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2240
2241 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002242 * Listeners for attach events.
2243 */
2244 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2245
2246 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 * Listener used to dispatch click events.
2248 * This field should be made private, so it is hidden from the SDK.
2249 * {@hide}
2250 */
2251 protected OnClickListener mOnClickListener;
2252
2253 /**
2254 * Listener used to dispatch long click events.
2255 * This field should be made private, so it is hidden from the SDK.
2256 * {@hide}
2257 */
2258 protected OnLongClickListener mOnLongClickListener;
2259
2260 /**
2261 * Listener used to build the context menu.
2262 * This field should be made private, so it is hidden from the SDK.
2263 * {@hide}
2264 */
2265 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2266
2267 private OnKeyListener mOnKeyListener;
2268
2269 private OnTouchListener mOnTouchListener;
2270
Jeff Brown10b62902011-06-20 16:40:37 -07002271 private OnHoverListener mOnHoverListener;
2272
Jeff Brown33bbfd22011-02-24 20:55:35 -08002273 private OnGenericMotionListener mOnGenericMotionListener;
2274
Chris Tate32affef2010-10-18 15:29:21 -07002275 private OnDragListener mOnDragListener;
2276
Joe Onorato664644d2011-01-23 17:53:23 -08002277 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002279 /**
2280 * The application environment this view lives in.
2281 * This field should be made private, so it is hidden from the SDK.
2282 * {@hide}
2283 */
2284 protected Context mContext;
2285
2286 private ScrollabilityCache mScrollCache;
2287
2288 private int[] mDrawableState = null;
2289
Romain Guy0211a0a2011-02-14 16:34:59 -08002290 /**
2291 * Set to true when drawing cache is enabled and cannot be created.
2292 *
2293 * @hide
2294 */
2295 public boolean mCachingFailed;
2296
Romain Guy02890fd2010-08-06 17:58:44 -07002297 private Bitmap mDrawingCache;
2298 private Bitmap mUnscaledDrawingCache;
Romain Guyb051e892010-09-28 19:09:36 -07002299 private DisplayList mDisplayList;
Romain Guy6c319ca2011-01-11 14:29:25 -08002300 private HardwareLayer mHardwareLayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002301
2302 /**
2303 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2304 * the user may specify which view to go to next.
2305 */
2306 private int mNextFocusLeftId = View.NO_ID;
2307
2308 /**
2309 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2310 * the user may specify which view to go to next.
2311 */
2312 private int mNextFocusRightId = View.NO_ID;
2313
2314 /**
2315 * When this view has focus and the next focus is {@link #FOCUS_UP},
2316 * the user may specify which view to go to next.
2317 */
2318 private int mNextFocusUpId = View.NO_ID;
2319
2320 /**
2321 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2322 * the user may specify which view to go to next.
2323 */
2324 private int mNextFocusDownId = View.NO_ID;
2325
Jeff Brown4e6319b2010-12-13 10:36:51 -08002326 /**
2327 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2328 * the user may specify which view to go to next.
2329 */
2330 int mNextFocusForwardId = View.NO_ID;
2331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002332 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002333 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002334 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002335 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002336
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 private UnsetPressedState mUnsetPressedState;
2338
2339 /**
2340 * Whether the long press's action has been invoked. The tap's action is invoked on the
2341 * up event while a long press is invoked as soon as the long press duration is reached, so
2342 * a long press could be performed before the tap is checked, in which case the tap's action
2343 * should not be invoked.
2344 */
2345 private boolean mHasPerformedLongPress;
2346
2347 /**
2348 * The minimum height of the view. We'll try our best to have the height
2349 * of this view to at least this amount.
2350 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002351 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 private int mMinHeight;
2353
2354 /**
2355 * The minimum width of the view. We'll try our best to have the width
2356 * of this view to at least this amount.
2357 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002358 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002359 private int mMinWidth;
2360
2361 /**
2362 * The delegate to handle touch events that are physically in this view
2363 * but should be handled by another view.
2364 */
2365 private TouchDelegate mTouchDelegate = null;
2366
2367 /**
2368 * Solid color to use as a background when creating the drawing cache. Enables
2369 * the cache to use 16 bit bitmaps instead of 32 bit.
2370 */
2371 private int mDrawingCacheBackgroundColor = 0;
2372
2373 /**
2374 * Special tree observer used when mAttachInfo is null.
2375 */
2376 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002377
Adam Powelle14579b2009-12-16 18:39:52 -08002378 /**
2379 * Cache the touch slop from the context that created the view.
2380 */
2381 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002384 * Object that handles automatic animation of view properties.
2385 */
2386 private ViewPropertyAnimator mAnimator = null;
2387
2388 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002389 * Flag indicating that a drag can cross window boundaries. When
2390 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2391 * with this flag set, all visible applications will be able to participate
2392 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002393 *
2394 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002395 */
2396 public static final int DRAG_FLAG_GLOBAL = 1;
2397
2398 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002399 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2400 */
2401 private float mVerticalScrollFactor;
2402
2403 /**
Adam Powell20232d02010-12-08 21:08:53 -08002404 * Position of the vertical scroll bar.
2405 */
2406 private int mVerticalScrollbarPosition;
2407
2408 /**
2409 * Position the scroll bar at the default position as determined by the system.
2410 */
2411 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2412
2413 /**
2414 * Position the scroll bar along the left edge.
2415 */
2416 public static final int SCROLLBAR_POSITION_LEFT = 1;
2417
2418 /**
2419 * Position the scroll bar along the right edge.
2420 */
2421 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2422
2423 /**
Romain Guy171c5922011-01-06 10:04:23 -08002424 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002425 *
2426 * @see #getLayerType()
2427 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002428 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002429 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002430 */
2431 public static final int LAYER_TYPE_NONE = 0;
2432
2433 /**
2434 * <p>Indicates that the view has a software layer. A software layer is backed
2435 * by a bitmap and causes the view to be rendered using Android's software
2436 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002437 *
Romain Guy171c5922011-01-06 10:04:23 -08002438 * <p>Software layers have various usages:</p>
2439 * <p>When the application is not using hardware acceleration, a software layer
2440 * is useful to apply a specific color filter and/or blending mode and/or
2441 * translucency to a view and all its children.</p>
2442 * <p>When the application is using hardware acceleration, a software layer
2443 * is useful to render drawing primitives not supported by the hardware
2444 * accelerated pipeline. It can also be used to cache a complex view tree
2445 * into a texture and reduce the complexity of drawing operations. For instance,
2446 * when animating a complex view tree with a translation, a software layer can
2447 * be used to render the view tree only once.</p>
2448 * <p>Software layers should be avoided when the affected view tree updates
2449 * often. Every update will require to re-render the software layer, which can
2450 * potentially be slow (particularly when hardware acceleration is turned on
2451 * since the layer will have to be uploaded into a hardware texture after every
2452 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002453 *
2454 * @see #getLayerType()
2455 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002456 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002457 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002458 */
2459 public static final int LAYER_TYPE_SOFTWARE = 1;
2460
2461 /**
2462 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2463 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2464 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2465 * rendering pipeline, but only if hardware acceleration is turned on for the
2466 * view hierarchy. When hardware acceleration is turned off, hardware layers
2467 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002468 *
Romain Guy171c5922011-01-06 10:04:23 -08002469 * <p>A hardware layer is useful to apply a specific color filter and/or
2470 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002471 * <p>A hardware layer can be used to cache a complex view tree into a
2472 * texture and reduce the complexity of drawing operations. For instance,
2473 * when animating a complex view tree with a translation, a hardware layer can
2474 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002475 * <p>A hardware layer can also be used to increase the rendering quality when
2476 * rotation transformations are applied on a view. It can also be used to
2477 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002478 *
2479 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002480 * @see #setLayerType(int, android.graphics.Paint)
2481 * @see #LAYER_TYPE_NONE
2482 * @see #LAYER_TYPE_SOFTWARE
2483 */
2484 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002485
Romain Guy3aaff3a2011-01-12 14:18:47 -08002486 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2487 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2488 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2489 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2490 })
Romain Guy171c5922011-01-06 10:04:23 -08002491 int mLayerType = LAYER_TYPE_NONE;
2492 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002493 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002494
2495 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002496 * Set to true when the view is sending hover accessibility events because it
2497 * is the innermost hovered view.
2498 */
2499 private boolean mSendingHoverAccessibilityEvents;
2500
2501 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002502 * Undefined text direction (used by resolution algorithm).
2503 * @hide
2504 */
2505 public static final int TEXT_DIRECTION_UNDEFINED = -1;
2506
2507 /**
2508 * Text direction is inherited thru {@link ViewGroup}
2509 * @hide
2510 */
2511 public static final int TEXT_DIRECTION_INHERIT = 0;
2512
2513 /**
2514 * Text direction is using "first strong algorithm". The first strong directional character
2515 * determines the paragraph direction. If there is no strong directional character, the
2516 * paragraph direction is the view’s resolved ayout direction.
2517 *
2518 * @hide
2519 */
2520 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2521
2522 /**
2523 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2524 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
2525 * If there are neither, the paragraph direction is the view’s resolved layout direction.
2526 *
2527 * @hide
2528 */
2529 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2530
2531 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002532 * Text direction is the same as the one held by a 60% majority of the characters. If there is
2533 * no majority then the paragraph direction is the resolved layout direction of the View.
2534 *
2535 * @hide
2536 */
2537 public static final int TEXT_DIRECTION_CHAR_COUNT = 3;
2538
2539 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002540 * Text direction is forced to LTR.
2541 *
2542 * @hide
2543 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002544 public static final int TEXT_DIRECTION_LTR = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002545
2546 /**
2547 * Text direction is forced to RTL.
2548 *
2549 * @hide
2550 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002551 public static final int TEXT_DIRECTION_RTL = 5;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002552
2553 /**
2554 * Default text direction is inherited
2555 */
2556 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2557
2558 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002559 * Default threshold for "char count" heuristic.
2560 */
2561 protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f;
2562
2563 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002564 * The text direction that has been defined by {@link #setTextDirection(int)}.
2565 *
2566 * {@hide}
2567 */
2568 @ViewDebug.ExportedProperty(category = "text", mapping = {
2569 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2570 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2571 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2572 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002573 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002574 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2575 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2576 })
2577 protected int mTextDirection = DEFAULT_TEXT_DIRECTION;
2578
2579 /**
2580 * The resolved text direction. If resolution has not yet been done or has been reset, it will
2581 * be equal to {@link #TEXT_DIRECTION_UNDEFINED}. Otherwise it will be either {@link #TEXT_DIRECTION_LTR}
2582 * or {@link #TEXT_DIRECTION_RTL}.
2583 *
2584 * {@hide}
2585 */
2586 @ViewDebug.ExportedProperty(category = "text", mapping = {
2587 @ViewDebug.IntToString(from = TEXT_DIRECTION_UNDEFINED, to = "UNDEFINED"),
2588 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2589 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2590 })
2591 protected int mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
2592
2593 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002594 * Consistency verifier for debugging purposes.
2595 * @hide
2596 */
2597 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2598 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2599 new InputEventConsistencyVerifier(this, 0) : null;
2600
2601 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002602 * Simple constructor to use when creating a view from code.
2603 *
2604 * @param context The Context the view is running in, through which it can
2605 * access the current theme, resources, etc.
2606 */
2607 public View(Context context) {
2608 mContext = context;
2609 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002610 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002611 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002612 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002613 mUserPaddingStart = -1;
2614 mUserPaddingEnd = -1;
2615 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002616 }
2617
2618 /**
2619 * Constructor that is called when inflating a view from XML. This is called
2620 * when a view is being constructed from an XML file, supplying attributes
2621 * that were specified in the XML file. This version uses a default style of
2622 * 0, so the only attribute values applied are those in the Context's Theme
2623 * and the given AttributeSet.
2624 *
2625 * <p>
2626 * The method onFinishInflate() will be called after all children have been
2627 * added.
2628 *
2629 * @param context The Context the view is running in, through which it can
2630 * access the current theme, resources, etc.
2631 * @param attrs The attributes of the XML tag that is inflating the view.
2632 * @see #View(Context, AttributeSet, int)
2633 */
2634 public View(Context context, AttributeSet attrs) {
2635 this(context, attrs, 0);
2636 }
2637
2638 /**
2639 * Perform inflation from XML and apply a class-specific base style. This
2640 * constructor of View allows subclasses to use their own base style when
2641 * they are inflating. For example, a Button class's constructor would call
2642 * this version of the super class constructor and supply
2643 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2644 * the theme's button style to modify all of the base view attributes (in
2645 * particular its background) as well as the Button class's attributes.
2646 *
2647 * @param context The Context the view is running in, through which it can
2648 * access the current theme, resources, etc.
2649 * @param attrs The attributes of the XML tag that is inflating the view.
2650 * @param defStyle The default style to apply to this view. If 0, no style
2651 * will be applied (beyond what is included in the theme). This may
2652 * either be an attribute resource, whose value will be retrieved
2653 * from the current theme, or an explicit style resource.
2654 * @see #View(Context, AttributeSet)
2655 */
2656 public View(Context context, AttributeSet attrs, int defStyle) {
2657 this(context);
2658
2659 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2660 defStyle, 0);
2661
2662 Drawable background = null;
2663
2664 int leftPadding = -1;
2665 int topPadding = -1;
2666 int rightPadding = -1;
2667 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002668 int startPadding = -1;
2669 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002670
2671 int padding = -1;
2672
2673 int viewFlagValues = 0;
2674 int viewFlagMasks = 0;
2675
2676 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002677
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002678 int x = 0;
2679 int y = 0;
2680
Chet Haase73066682010-11-29 15:55:32 -08002681 float tx = 0;
2682 float ty = 0;
2683 float rotation = 0;
2684 float rotationX = 0;
2685 float rotationY = 0;
2686 float sx = 1f;
2687 float sy = 1f;
2688 boolean transformSet = false;
2689
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002690 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2691
Adam Powell637d3372010-08-25 14:37:03 -07002692 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002693 final int N = a.getIndexCount();
2694 for (int i = 0; i < N; i++) {
2695 int attr = a.getIndex(i);
2696 switch (attr) {
2697 case com.android.internal.R.styleable.View_background:
2698 background = a.getDrawable(attr);
2699 break;
2700 case com.android.internal.R.styleable.View_padding:
2701 padding = a.getDimensionPixelSize(attr, -1);
2702 break;
2703 case com.android.internal.R.styleable.View_paddingLeft:
2704 leftPadding = a.getDimensionPixelSize(attr, -1);
2705 break;
2706 case com.android.internal.R.styleable.View_paddingTop:
2707 topPadding = a.getDimensionPixelSize(attr, -1);
2708 break;
2709 case com.android.internal.R.styleable.View_paddingRight:
2710 rightPadding = a.getDimensionPixelSize(attr, -1);
2711 break;
2712 case com.android.internal.R.styleable.View_paddingBottom:
2713 bottomPadding = a.getDimensionPixelSize(attr, -1);
2714 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002715 case com.android.internal.R.styleable.View_paddingStart:
2716 startPadding = a.getDimensionPixelSize(attr, -1);
2717 break;
2718 case com.android.internal.R.styleable.View_paddingEnd:
2719 endPadding = a.getDimensionPixelSize(attr, -1);
2720 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002721 case com.android.internal.R.styleable.View_scrollX:
2722 x = a.getDimensionPixelOffset(attr, 0);
2723 break;
2724 case com.android.internal.R.styleable.View_scrollY:
2725 y = a.getDimensionPixelOffset(attr, 0);
2726 break;
Chet Haase73066682010-11-29 15:55:32 -08002727 case com.android.internal.R.styleable.View_alpha:
2728 setAlpha(a.getFloat(attr, 1f));
2729 break;
2730 case com.android.internal.R.styleable.View_transformPivotX:
2731 setPivotX(a.getDimensionPixelOffset(attr, 0));
2732 break;
2733 case com.android.internal.R.styleable.View_transformPivotY:
2734 setPivotY(a.getDimensionPixelOffset(attr, 0));
2735 break;
2736 case com.android.internal.R.styleable.View_translationX:
2737 tx = a.getDimensionPixelOffset(attr, 0);
2738 transformSet = true;
2739 break;
2740 case com.android.internal.R.styleable.View_translationY:
2741 ty = a.getDimensionPixelOffset(attr, 0);
2742 transformSet = true;
2743 break;
2744 case com.android.internal.R.styleable.View_rotation:
2745 rotation = a.getFloat(attr, 0);
2746 transformSet = true;
2747 break;
2748 case com.android.internal.R.styleable.View_rotationX:
2749 rotationX = a.getFloat(attr, 0);
2750 transformSet = true;
2751 break;
2752 case com.android.internal.R.styleable.View_rotationY:
2753 rotationY = a.getFloat(attr, 0);
2754 transformSet = true;
2755 break;
2756 case com.android.internal.R.styleable.View_scaleX:
2757 sx = a.getFloat(attr, 1f);
2758 transformSet = true;
2759 break;
2760 case com.android.internal.R.styleable.View_scaleY:
2761 sy = a.getFloat(attr, 1f);
2762 transformSet = true;
2763 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002764 case com.android.internal.R.styleable.View_id:
2765 mID = a.getResourceId(attr, NO_ID);
2766 break;
2767 case com.android.internal.R.styleable.View_tag:
2768 mTag = a.getText(attr);
2769 break;
2770 case com.android.internal.R.styleable.View_fitsSystemWindows:
2771 if (a.getBoolean(attr, false)) {
2772 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2773 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2774 }
2775 break;
2776 case com.android.internal.R.styleable.View_focusable:
2777 if (a.getBoolean(attr, false)) {
2778 viewFlagValues |= FOCUSABLE;
2779 viewFlagMasks |= FOCUSABLE_MASK;
2780 }
2781 break;
2782 case com.android.internal.R.styleable.View_focusableInTouchMode:
2783 if (a.getBoolean(attr, false)) {
2784 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2785 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2786 }
2787 break;
2788 case com.android.internal.R.styleable.View_clickable:
2789 if (a.getBoolean(attr, false)) {
2790 viewFlagValues |= CLICKABLE;
2791 viewFlagMasks |= CLICKABLE;
2792 }
2793 break;
2794 case com.android.internal.R.styleable.View_longClickable:
2795 if (a.getBoolean(attr, false)) {
2796 viewFlagValues |= LONG_CLICKABLE;
2797 viewFlagMasks |= LONG_CLICKABLE;
2798 }
2799 break;
2800 case com.android.internal.R.styleable.View_saveEnabled:
2801 if (!a.getBoolean(attr, true)) {
2802 viewFlagValues |= SAVE_DISABLED;
2803 viewFlagMasks |= SAVE_DISABLED_MASK;
2804 }
2805 break;
2806 case com.android.internal.R.styleable.View_duplicateParentState:
2807 if (a.getBoolean(attr, false)) {
2808 viewFlagValues |= DUPLICATE_PARENT_STATE;
2809 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2810 }
2811 break;
2812 case com.android.internal.R.styleable.View_visibility:
2813 final int visibility = a.getInt(attr, 0);
2814 if (visibility != 0) {
2815 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2816 viewFlagMasks |= VISIBILITY_MASK;
2817 }
2818 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002819 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002820 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002821 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002822 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002823 final int layoutDirection = a.getInt(attr, -1);
2824 if (layoutDirection != -1) {
2825 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002826 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002827 // Set to default (LAYOUT_DIRECTION_INHERIT)
2828 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002829 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002830 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002831 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002832 case com.android.internal.R.styleable.View_drawingCacheQuality:
2833 final int cacheQuality = a.getInt(attr, 0);
2834 if (cacheQuality != 0) {
2835 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2836 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2837 }
2838 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002839 case com.android.internal.R.styleable.View_contentDescription:
2840 mContentDescription = a.getString(attr);
2841 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002842 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2843 if (!a.getBoolean(attr, true)) {
2844 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2845 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2846 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002847 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002848 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2849 if (!a.getBoolean(attr, true)) {
2850 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2851 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2852 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002853 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002854 case R.styleable.View_scrollbars:
2855 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2856 if (scrollbars != SCROLLBARS_NONE) {
2857 viewFlagValues |= scrollbars;
2858 viewFlagMasks |= SCROLLBARS_MASK;
2859 initializeScrollbars(a);
2860 }
2861 break;
2862 case R.styleable.View_fadingEdge:
2863 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2864 if (fadingEdge != FADING_EDGE_NONE) {
2865 viewFlagValues |= fadingEdge;
2866 viewFlagMasks |= FADING_EDGE_MASK;
2867 initializeFadingEdge(a);
2868 }
2869 break;
2870 case R.styleable.View_scrollbarStyle:
2871 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2872 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2873 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2874 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2875 }
2876 break;
2877 case R.styleable.View_isScrollContainer:
2878 setScrollContainer = true;
2879 if (a.getBoolean(attr, false)) {
2880 setScrollContainer(true);
2881 }
2882 break;
2883 case com.android.internal.R.styleable.View_keepScreenOn:
2884 if (a.getBoolean(attr, false)) {
2885 viewFlagValues |= KEEP_SCREEN_ON;
2886 viewFlagMasks |= KEEP_SCREEN_ON;
2887 }
2888 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002889 case R.styleable.View_filterTouchesWhenObscured:
2890 if (a.getBoolean(attr, false)) {
2891 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2892 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2893 }
2894 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002895 case R.styleable.View_nextFocusLeft:
2896 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2897 break;
2898 case R.styleable.View_nextFocusRight:
2899 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2900 break;
2901 case R.styleable.View_nextFocusUp:
2902 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2903 break;
2904 case R.styleable.View_nextFocusDown:
2905 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2906 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002907 case R.styleable.View_nextFocusForward:
2908 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2909 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002910 case R.styleable.View_minWidth:
2911 mMinWidth = a.getDimensionPixelSize(attr, 0);
2912 break;
2913 case R.styleable.View_minHeight:
2914 mMinHeight = a.getDimensionPixelSize(attr, 0);
2915 break;
Romain Guy9a817362009-05-01 10:57:14 -07002916 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002917 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002918 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002919 + "be used within a restricted context");
2920 }
2921
Romain Guy9a817362009-05-01 10:57:14 -07002922 final String handlerName = a.getString(attr);
2923 if (handlerName != null) {
2924 setOnClickListener(new OnClickListener() {
2925 private Method mHandler;
2926
2927 public void onClick(View v) {
2928 if (mHandler == null) {
2929 try {
2930 mHandler = getContext().getClass().getMethod(handlerName,
2931 View.class);
2932 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002933 int id = getId();
2934 String idText = id == NO_ID ? "" : " with id '"
2935 + getContext().getResources().getResourceEntryName(
2936 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002937 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002938 handlerName + "(View) in the activity "
2939 + getContext().getClass() + " for onClick handler"
2940 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002941 }
2942 }
2943
2944 try {
2945 mHandler.invoke(getContext(), View.this);
2946 } catch (IllegalAccessException e) {
2947 throw new IllegalStateException("Could not execute non "
2948 + "public method of the activity", e);
2949 } catch (InvocationTargetException e) {
2950 throw new IllegalStateException("Could not execute "
2951 + "method of the activity", e);
2952 }
2953 }
2954 });
2955 }
2956 break;
Adam Powell637d3372010-08-25 14:37:03 -07002957 case R.styleable.View_overScrollMode:
2958 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2959 break;
Adam Powell20232d02010-12-08 21:08:53 -08002960 case R.styleable.View_verticalScrollbarPosition:
2961 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2962 break;
Romain Guy171c5922011-01-06 10:04:23 -08002963 case R.styleable.View_layerType:
2964 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2965 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002966 case R.styleable.View_textDirection:
2967 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2968 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002969 }
2970 }
2971
Adam Powell637d3372010-08-25 14:37:03 -07002972 setOverScrollMode(overScrollMode);
2973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002974 if (background != null) {
2975 setBackgroundDrawable(background);
2976 }
2977
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002978 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
2979
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002980 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
2981 // layout direction). Those cached values will be used later during padding resolution.
2982 mUserPaddingStart = startPadding;
2983 mUserPaddingEnd = endPadding;
2984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002985 if (padding >= 0) {
2986 leftPadding = padding;
2987 topPadding = padding;
2988 rightPadding = padding;
2989 bottomPadding = padding;
2990 }
2991
2992 // If the user specified the padding (either with android:padding or
2993 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
2994 // use the default padding or the padding from the background drawable
2995 // (stored at this point in mPadding*)
2996 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
2997 topPadding >= 0 ? topPadding : mPaddingTop,
2998 rightPadding >= 0 ? rightPadding : mPaddingRight,
2999 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3000
3001 if (viewFlagMasks != 0) {
3002 setFlags(viewFlagValues, viewFlagMasks);
3003 }
3004
3005 // Needs to be called after mViewFlags is set
3006 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3007 recomputePadding();
3008 }
3009
3010 if (x != 0 || y != 0) {
3011 scrollTo(x, y);
3012 }
3013
Chet Haase73066682010-11-29 15:55:32 -08003014 if (transformSet) {
3015 setTranslationX(tx);
3016 setTranslationY(ty);
3017 setRotation(rotation);
3018 setRotationX(rotationX);
3019 setRotationY(rotationY);
3020 setScaleX(sx);
3021 setScaleY(sy);
3022 }
3023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003024 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3025 setScrollContainer(true);
3026 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003027
3028 computeOpaqueFlags();
3029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003030 a.recycle();
3031 }
3032
3033 /**
3034 * Non-public constructor for use in testing
3035 */
3036 View() {
3037 }
3038
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003039 /**
3040 * <p>
3041 * Initializes the fading edges from a given set of styled attributes. This
3042 * method should be called by subclasses that need fading edges and when an
3043 * instance of these subclasses is created programmatically rather than
3044 * being inflated from XML. This method is automatically called when the XML
3045 * is inflated.
3046 * </p>
3047 *
3048 * @param a the styled attributes set to initialize the fading edges from
3049 */
3050 protected void initializeFadingEdge(TypedArray a) {
3051 initScrollCache();
3052
3053 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3054 R.styleable.View_fadingEdgeLength,
3055 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3056 }
3057
3058 /**
3059 * Returns the size of the vertical faded edges used to indicate that more
3060 * content in this view is visible.
3061 *
3062 * @return The size in pixels of the vertical faded edge or 0 if vertical
3063 * faded edges are not enabled for this view.
3064 * @attr ref android.R.styleable#View_fadingEdgeLength
3065 */
3066 public int getVerticalFadingEdgeLength() {
3067 if (isVerticalFadingEdgeEnabled()) {
3068 ScrollabilityCache cache = mScrollCache;
3069 if (cache != null) {
3070 return cache.fadingEdgeLength;
3071 }
3072 }
3073 return 0;
3074 }
3075
3076 /**
3077 * Set the size of the faded edge used to indicate that more content in this
3078 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003079 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3080 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3081 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003082 *
3083 * @param length The size in pixels of the faded edge used to indicate that more
3084 * content in this view is visible.
3085 */
3086 public void setFadingEdgeLength(int length) {
3087 initScrollCache();
3088 mScrollCache.fadingEdgeLength = length;
3089 }
3090
3091 /**
3092 * Returns the size of the horizontal faded edges used to indicate that more
3093 * content in this view is visible.
3094 *
3095 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3096 * faded edges are not enabled for this view.
3097 * @attr ref android.R.styleable#View_fadingEdgeLength
3098 */
3099 public int getHorizontalFadingEdgeLength() {
3100 if (isHorizontalFadingEdgeEnabled()) {
3101 ScrollabilityCache cache = mScrollCache;
3102 if (cache != null) {
3103 return cache.fadingEdgeLength;
3104 }
3105 }
3106 return 0;
3107 }
3108
3109 /**
3110 * Returns the width of the vertical scrollbar.
3111 *
3112 * @return The width in pixels of the vertical scrollbar or 0 if there
3113 * is no vertical scrollbar.
3114 */
3115 public int getVerticalScrollbarWidth() {
3116 ScrollabilityCache cache = mScrollCache;
3117 if (cache != null) {
3118 ScrollBarDrawable scrollBar = cache.scrollBar;
3119 if (scrollBar != null) {
3120 int size = scrollBar.getSize(true);
3121 if (size <= 0) {
3122 size = cache.scrollBarSize;
3123 }
3124 return size;
3125 }
3126 return 0;
3127 }
3128 return 0;
3129 }
3130
3131 /**
3132 * Returns the height of the horizontal scrollbar.
3133 *
3134 * @return The height in pixels of the horizontal scrollbar or 0 if
3135 * there is no horizontal scrollbar.
3136 */
3137 protected int getHorizontalScrollbarHeight() {
3138 ScrollabilityCache cache = mScrollCache;
3139 if (cache != null) {
3140 ScrollBarDrawable scrollBar = cache.scrollBar;
3141 if (scrollBar != null) {
3142 int size = scrollBar.getSize(false);
3143 if (size <= 0) {
3144 size = cache.scrollBarSize;
3145 }
3146 return size;
3147 }
3148 return 0;
3149 }
3150 return 0;
3151 }
3152
3153 /**
3154 * <p>
3155 * Initializes the scrollbars from a given set of styled attributes. This
3156 * method should be called by subclasses that need scrollbars and when an
3157 * instance of these subclasses is created programmatically rather than
3158 * being inflated from XML. This method is automatically called when the XML
3159 * is inflated.
3160 * </p>
3161 *
3162 * @param a the styled attributes set to initialize the scrollbars from
3163 */
3164 protected void initializeScrollbars(TypedArray a) {
3165 initScrollCache();
3166
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003167 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003168
Mike Cleronf116bf82009-09-27 19:14:12 -07003169 if (scrollabilityCache.scrollBar == null) {
3170 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3171 }
Joe Malin32736f02011-01-19 16:14:20 -08003172
Romain Guy8bda2482010-03-02 11:42:11 -08003173 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003174
Mike Cleronf116bf82009-09-27 19:14:12 -07003175 if (!fadeScrollbars) {
3176 scrollabilityCache.state = ScrollabilityCache.ON;
3177 }
3178 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003179
3180
Mike Cleronf116bf82009-09-27 19:14:12 -07003181 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3182 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3183 .getScrollBarFadeDuration());
3184 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3185 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3186 ViewConfiguration.getScrollDefaultDelay());
3187
Joe Malin32736f02011-01-19 16:14:20 -08003188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003189 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3190 com.android.internal.R.styleable.View_scrollbarSize,
3191 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3192
3193 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3194 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3195
3196 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3197 if (thumb != null) {
3198 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3199 }
3200
3201 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3202 false);
3203 if (alwaysDraw) {
3204 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3205 }
3206
3207 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3208 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3209
3210 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3211 if (thumb != null) {
3212 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3213 }
3214
3215 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3216 false);
3217 if (alwaysDraw) {
3218 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3219 }
3220
3221 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003222 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003223 }
3224
3225 /**
3226 * <p>
3227 * Initalizes the scrollability cache if necessary.
3228 * </p>
3229 */
3230 private void initScrollCache() {
3231 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003232 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003233 }
3234 }
3235
3236 /**
Adam Powell20232d02010-12-08 21:08:53 -08003237 * Set the position of the vertical scroll bar. Should be one of
3238 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3239 * {@link #SCROLLBAR_POSITION_RIGHT}.
3240 *
3241 * @param position Where the vertical scroll bar should be positioned.
3242 */
3243 public void setVerticalScrollbarPosition(int position) {
3244 if (mVerticalScrollbarPosition != position) {
3245 mVerticalScrollbarPosition = position;
3246 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003247 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003248 }
3249 }
3250
3251 /**
3252 * @return The position where the vertical scroll bar will show, if applicable.
3253 * @see #setVerticalScrollbarPosition(int)
3254 */
3255 public int getVerticalScrollbarPosition() {
3256 return mVerticalScrollbarPosition;
3257 }
3258
3259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003260 * Register a callback to be invoked when focus of this view changed.
3261 *
3262 * @param l The callback that will run.
3263 */
3264 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3265 mOnFocusChangeListener = l;
3266 }
3267
3268 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003269 * Add a listener that will be called when the bounds of the view change due to
3270 * layout processing.
3271 *
3272 * @param listener The listener that will be called when layout bounds change.
3273 */
3274 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3275 if (mOnLayoutChangeListeners == null) {
3276 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3277 }
3278 mOnLayoutChangeListeners.add(listener);
3279 }
3280
3281 /**
3282 * Remove a listener for layout changes.
3283 *
3284 * @param listener The listener for layout bounds change.
3285 */
3286 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3287 if (mOnLayoutChangeListeners == null) {
3288 return;
3289 }
3290 mOnLayoutChangeListeners.remove(listener);
3291 }
3292
3293 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003294 * Add a listener for attach state changes.
3295 *
3296 * This listener will be called whenever this view is attached or detached
3297 * from a window. Remove the listener using
3298 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3299 *
3300 * @param listener Listener to attach
3301 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3302 */
3303 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3304 if (mOnAttachStateChangeListeners == null) {
3305 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3306 }
3307 mOnAttachStateChangeListeners.add(listener);
3308 }
3309
3310 /**
3311 * Remove a listener for attach state changes. The listener will receive no further
3312 * notification of window attach/detach events.
3313 *
3314 * @param listener Listener to remove
3315 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3316 */
3317 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3318 if (mOnAttachStateChangeListeners == null) {
3319 return;
3320 }
3321 mOnAttachStateChangeListeners.remove(listener);
3322 }
3323
3324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003325 * Returns the focus-change callback registered for this view.
3326 *
3327 * @return The callback, or null if one is not registered.
3328 */
3329 public OnFocusChangeListener getOnFocusChangeListener() {
3330 return mOnFocusChangeListener;
3331 }
3332
3333 /**
3334 * Register a callback to be invoked when this view is clicked. If this view is not
3335 * clickable, it becomes clickable.
3336 *
3337 * @param l The callback that will run
3338 *
3339 * @see #setClickable(boolean)
3340 */
3341 public void setOnClickListener(OnClickListener l) {
3342 if (!isClickable()) {
3343 setClickable(true);
3344 }
3345 mOnClickListener = l;
3346 }
3347
3348 /**
3349 * Register a callback to be invoked when this view is clicked and held. If this view is not
3350 * long clickable, it becomes long clickable.
3351 *
3352 * @param l The callback that will run
3353 *
3354 * @see #setLongClickable(boolean)
3355 */
3356 public void setOnLongClickListener(OnLongClickListener l) {
3357 if (!isLongClickable()) {
3358 setLongClickable(true);
3359 }
3360 mOnLongClickListener = l;
3361 }
3362
3363 /**
3364 * Register a callback to be invoked when the context menu for this view is
3365 * being built. If this view is not long clickable, it becomes long clickable.
3366 *
3367 * @param l The callback that will run
3368 *
3369 */
3370 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3371 if (!isLongClickable()) {
3372 setLongClickable(true);
3373 }
3374 mOnCreateContextMenuListener = l;
3375 }
3376
3377 /**
3378 * Call this view's OnClickListener, if it is defined.
3379 *
3380 * @return True there was an assigned OnClickListener that was called, false
3381 * otherwise is returned.
3382 */
3383 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003384 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003386 if (mOnClickListener != null) {
3387 playSoundEffect(SoundEffectConstants.CLICK);
3388 mOnClickListener.onClick(this);
3389 return true;
3390 }
3391
3392 return false;
3393 }
3394
3395 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003396 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3397 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003398 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003399 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003400 */
3401 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003402 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003404 boolean handled = false;
3405 if (mOnLongClickListener != null) {
3406 handled = mOnLongClickListener.onLongClick(View.this);
3407 }
3408 if (!handled) {
3409 handled = showContextMenu();
3410 }
3411 if (handled) {
3412 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3413 }
3414 return handled;
3415 }
3416
3417 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003418 * Performs button-related actions during a touch down event.
3419 *
3420 * @param event The event.
3421 * @return True if the down was consumed.
3422 *
3423 * @hide
3424 */
3425 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3426 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3427 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3428 return true;
3429 }
3430 }
3431 return false;
3432 }
3433
3434 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003435 * Bring up the context menu for this view.
3436 *
3437 * @return Whether a context menu was displayed.
3438 */
3439 public boolean showContextMenu() {
3440 return getParent().showContextMenuForChild(this);
3441 }
3442
3443 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003444 * Bring up the context menu for this view, referring to the item under the specified point.
3445 *
3446 * @param x The referenced x coordinate.
3447 * @param y The referenced y coordinate.
3448 * @param metaState The keyboard modifiers that were pressed.
3449 * @return Whether a context menu was displayed.
3450 *
3451 * @hide
3452 */
3453 public boolean showContextMenu(float x, float y, int metaState) {
3454 return showContextMenu();
3455 }
3456
3457 /**
Adam Powell6e346362010-07-23 10:18:23 -07003458 * Start an action mode.
3459 *
3460 * @param callback Callback that will control the lifecycle of the action mode
3461 * @return The new action mode if it is started, null otherwise
3462 *
3463 * @see ActionMode
3464 */
3465 public ActionMode startActionMode(ActionMode.Callback callback) {
3466 return getParent().startActionModeForChild(this, callback);
3467 }
3468
3469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003470 * Register a callback to be invoked when a key is pressed in this view.
3471 * @param l the key listener to attach to this view
3472 */
3473 public void setOnKeyListener(OnKeyListener l) {
3474 mOnKeyListener = l;
3475 }
3476
3477 /**
3478 * Register a callback to be invoked when a touch event is sent to this view.
3479 * @param l the touch listener to attach to this view
3480 */
3481 public void setOnTouchListener(OnTouchListener l) {
3482 mOnTouchListener = l;
3483 }
3484
3485 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003486 * Register a callback to be invoked when a generic motion event is sent to this view.
3487 * @param l the generic motion listener to attach to this view
3488 */
3489 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3490 mOnGenericMotionListener = l;
3491 }
3492
3493 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003494 * Register a callback to be invoked when a hover event is sent to this view.
3495 * @param l the hover listener to attach to this view
3496 */
3497 public void setOnHoverListener(OnHoverListener l) {
3498 mOnHoverListener = l;
3499 }
3500
3501 /**
Joe Malin32736f02011-01-19 16:14:20 -08003502 * Register a drag event listener callback object for this View. The parameter is
3503 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3504 * View, the system calls the
3505 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3506 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003507 */
3508 public void setOnDragListener(OnDragListener l) {
3509 mOnDragListener = l;
3510 }
3511
3512 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003513 * Give this view focus. This will cause
3514 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003515 *
3516 * Note: this does not check whether this {@link View} should get focus, it just
3517 * gives it focus no matter what. It should only be called internally by framework
3518 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3519 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003520 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3521 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003522 * focus moved when requestFocus() is called. It may not always
3523 * apply, in which case use the default View.FOCUS_DOWN.
3524 * @param previouslyFocusedRect The rectangle of the view that had focus
3525 * prior in this View's coordinate system.
3526 */
3527 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3528 if (DBG) {
3529 System.out.println(this + " requestFocus()");
3530 }
3531
3532 if ((mPrivateFlags & FOCUSED) == 0) {
3533 mPrivateFlags |= FOCUSED;
3534
3535 if (mParent != null) {
3536 mParent.requestChildFocus(this, this);
3537 }
3538
3539 onFocusChanged(true, direction, previouslyFocusedRect);
3540 refreshDrawableState();
3541 }
3542 }
3543
3544 /**
3545 * Request that a rectangle of this view be visible on the screen,
3546 * scrolling if necessary just enough.
3547 *
3548 * <p>A View should call this if it maintains some notion of which part
3549 * of its content is interesting. For example, a text editing view
3550 * should call this when its cursor moves.
3551 *
3552 * @param rectangle The rectangle.
3553 * @return Whether any parent scrolled.
3554 */
3555 public boolean requestRectangleOnScreen(Rect rectangle) {
3556 return requestRectangleOnScreen(rectangle, false);
3557 }
3558
3559 /**
3560 * Request that a rectangle of this view be visible on the screen,
3561 * scrolling if necessary just enough.
3562 *
3563 * <p>A View should call this if it maintains some notion of which part
3564 * of its content is interesting. For example, a text editing view
3565 * should call this when its cursor moves.
3566 *
3567 * <p>When <code>immediate</code> is set to true, scrolling will not be
3568 * animated.
3569 *
3570 * @param rectangle The rectangle.
3571 * @param immediate True to forbid animated scrolling, false otherwise
3572 * @return Whether any parent scrolled.
3573 */
3574 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3575 View child = this;
3576 ViewParent parent = mParent;
3577 boolean scrolled = false;
3578 while (parent != null) {
3579 scrolled |= parent.requestChildRectangleOnScreen(child,
3580 rectangle, immediate);
3581
3582 // offset rect so next call has the rectangle in the
3583 // coordinate system of its direct child.
3584 rectangle.offset(child.getLeft(), child.getTop());
3585 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3586
3587 if (!(parent instanceof View)) {
3588 break;
3589 }
Romain Guy8506ab42009-06-11 17:35:47 -07003590
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003591 child = (View) parent;
3592 parent = child.getParent();
3593 }
3594 return scrolled;
3595 }
3596
3597 /**
3598 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003599 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003600 */
3601 public void clearFocus() {
3602 if (DBG) {
3603 System.out.println(this + " clearFocus()");
3604 }
3605
3606 if ((mPrivateFlags & FOCUSED) != 0) {
3607 mPrivateFlags &= ~FOCUSED;
3608
3609 if (mParent != null) {
3610 mParent.clearChildFocus(this);
3611 }
3612
3613 onFocusChanged(false, 0, null);
3614 refreshDrawableState();
3615 }
3616 }
3617
3618 /**
3619 * Called to clear the focus of a view that is about to be removed.
3620 * Doesn't call clearChildFocus, which prevents this view from taking
3621 * focus again before it has been removed from the parent
3622 */
3623 void clearFocusForRemoval() {
3624 if ((mPrivateFlags & FOCUSED) != 0) {
3625 mPrivateFlags &= ~FOCUSED;
3626
3627 onFocusChanged(false, 0, null);
3628 refreshDrawableState();
3629 }
3630 }
3631
3632 /**
3633 * Called internally by the view system when a new view is getting focus.
3634 * This is what clears the old focus.
3635 */
3636 void unFocus() {
3637 if (DBG) {
3638 System.out.println(this + " unFocus()");
3639 }
3640
3641 if ((mPrivateFlags & FOCUSED) != 0) {
3642 mPrivateFlags &= ~FOCUSED;
3643
3644 onFocusChanged(false, 0, null);
3645 refreshDrawableState();
3646 }
3647 }
3648
3649 /**
3650 * Returns true if this view has focus iteself, or is the ancestor of the
3651 * view that has focus.
3652 *
3653 * @return True if this view has or contains focus, false otherwise.
3654 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003655 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003656 public boolean hasFocus() {
3657 return (mPrivateFlags & FOCUSED) != 0;
3658 }
3659
3660 /**
3661 * Returns true if this view is focusable or if it contains a reachable View
3662 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3663 * is a View whose parents do not block descendants focus.
3664 *
3665 * Only {@link #VISIBLE} views are considered focusable.
3666 *
3667 * @return True if the view is focusable or if the view contains a focusable
3668 * View, false otherwise.
3669 *
3670 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3671 */
3672 public boolean hasFocusable() {
3673 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3674 }
3675
3676 /**
3677 * Called by the view system when the focus state of this view changes.
3678 * When the focus change event is caused by directional navigation, direction
3679 * and previouslyFocusedRect provide insight into where the focus is coming from.
3680 * When overriding, be sure to call up through to the super class so that
3681 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003682 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003683 * @param gainFocus True if the View has focus; false otherwise.
3684 * @param direction The direction focus has moved when requestFocus()
3685 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003686 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3687 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3688 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003689 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3690 * system, of the previously focused view. If applicable, this will be
3691 * passed in as finer grained information about where the focus is coming
3692 * from (in addition to direction). Will be <code>null</code> otherwise.
3693 */
3694 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003695 if (gainFocus) {
3696 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3697 }
3698
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003699 InputMethodManager imm = InputMethodManager.peekInstance();
3700 if (!gainFocus) {
3701 if (isPressed()) {
3702 setPressed(false);
3703 }
3704 if (imm != null && mAttachInfo != null
3705 && mAttachInfo.mHasWindowFocus) {
3706 imm.focusOut(this);
3707 }
Romain Guya2431d02009-04-30 16:30:00 -07003708 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003709 } else if (imm != null && mAttachInfo != null
3710 && mAttachInfo.mHasWindowFocus) {
3711 imm.focusIn(this);
3712 }
Romain Guy8506ab42009-06-11 17:35:47 -07003713
Romain Guy0fd89bf2011-01-26 15:41:30 -08003714 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003715 if (mOnFocusChangeListener != null) {
3716 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3717 }
Joe Malin32736f02011-01-19 16:14:20 -08003718
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003719 if (mAttachInfo != null) {
3720 mAttachInfo.mKeyDispatchState.reset(this);
3721 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003722 }
3723
3724 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003725 * Sends an accessibility event of the given type. If accessiiblity is
3726 * not enabled this method has no effect. The default implementation calls
3727 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3728 * to populate information about the event source (this View), then calls
3729 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3730 * populate the text content of the event source including its descendants,
3731 * and last calls
3732 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3733 * on its parent to resuest sending of the event to interested parties.
3734 *
3735 * @param eventType The type of the event to send.
3736 *
3737 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3738 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3739 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003740 */
3741 public void sendAccessibilityEvent(int eventType) {
3742 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3743 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3744 }
3745 }
3746
3747 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003748 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3749 * takes as an argument an empty {@link AccessibilityEvent} and does not
3750 * perfrom a check whether accessibility is enabled.
3751 *
3752 * @param event The event to send.
3753 *
3754 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003755 */
3756 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003757 if (!isShown()) {
3758 return;
3759 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003760 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003761 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003762 // In the beginning we called #isShown(), so we know that getParent() is not null.
3763 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003764 }
3765
3766 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003767 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3768 * to its children for adding their text content to the event. Note that the
3769 * event text is populated in a separate dispatch path since we add to the
3770 * event not only the text of the source but also the text of all its descendants.
3771 * </p>
3772 * A typical implementation will call
3773 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3774 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3775 * on each child. Override this method if custom population of the event text
3776 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003777 *
3778 * @param event The event.
3779 *
3780 * @return True if the event population was completed.
3781 */
3782 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003783 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003784 return false;
3785 }
3786
3787 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003788 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003789 * giving a chance to this View to populate the accessibility event with its
3790 * text content. While the implementation is free to modify other event
3791 * attributes this should be performed in
3792 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3793 * <p>
3794 * Example: Adding formatted date string to an accessibility event in addition
3795 * to the text added by the super implementation.
3796 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003797 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3798 * super.onPopulateAccessibilityEvent(event);
3799 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3800 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3801 * mCurrentDate.getTimeInMillis(), flags);
3802 * event.getText().add(selectedDateUtterance);
3803 * }
3804 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003805 *
3806 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003807 *
3808 * @see #sendAccessibilityEvent(int)
3809 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003810 */
3811 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003812 }
3813
3814 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003815 * Initializes an {@link AccessibilityEvent} with information about the
3816 * the type of the event and this View which is the event source. In other
3817 * words, the source of an accessibility event is the view whose state
3818 * change triggered firing the event.
3819 * <p>
3820 * Example: Setting the password property of an event in addition
3821 * to properties set by the super implementation.
3822 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003823 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3824 * super.onInitializeAccessibilityEvent(event);
3825 * event.setPassword(true);
3826 * }
3827 * </code></pre></p>
3828 * @param event The event to initialeze.
3829 *
3830 * @see #sendAccessibilityEvent(int)
3831 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3832 */
3833 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003834 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003835 event.setClassName(getClass().getName());
3836 event.setPackageName(getContext().getPackageName());
3837 event.setEnabled(isEnabled());
3838 event.setContentDescription(mContentDescription);
3839
Svetoslav Ganova0156172011-06-26 17:55:44 -07003840 final int eventType = event.getEventType();
3841 switch (eventType) {
3842 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3843 if (mAttachInfo != null) {
3844 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3845 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3846 FOCUSABLES_ALL);
3847 event.setItemCount(focusablesTempList.size());
3848 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3849 focusablesTempList.clear();
3850 }
3851 } break;
3852 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3853 event.setScrollX(mScrollX);
3854 event.setScrollY(mScrollY);
3855 event.setItemCount(getHeight());
3856 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003857 }
3858 }
3859
3860 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003861 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3862 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3863 * This method is responsible for obtaining an accessibility node info from a
3864 * pool of reusable instances and calling
3865 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3866 * initialize the former.
3867 * <p>
3868 * Note: The client is responsible for recycling the obtained instance by calling
3869 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3870 * </p>
3871 * @return A populated {@link AccessibilityNodeInfo}.
3872 *
3873 * @see AccessibilityNodeInfo
3874 */
3875 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3876 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3877 onInitializeAccessibilityNodeInfo(info);
3878 return info;
3879 }
3880
3881 /**
3882 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3883 * The base implementation sets:
3884 * <ul>
3885 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003886 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3887 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003888 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3889 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3890 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3891 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3892 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3893 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3894 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3895 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3896 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3897 * </ul>
3898 * <p>
3899 * Subclasses should override this method, call the super implementation,
3900 * and set additional attributes.
3901 * </p>
3902 * @param info The instance to initialize.
3903 */
3904 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3905 Rect bounds = mAttachInfo.mTmpInvalRect;
3906 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003907 info.setBoundsInParent(bounds);
3908
3909 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3910 getLocationOnScreen(locationOnScreen);
3911 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3912 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003913
3914 ViewParent parent = getParent();
3915 if (parent instanceof View) {
3916 View parentView = (View) parent;
3917 info.setParent(parentView);
3918 }
3919
3920 info.setPackageName(mContext.getPackageName());
3921 info.setClassName(getClass().getName());
3922 info.setContentDescription(getContentDescription());
3923
3924 info.setEnabled(isEnabled());
3925 info.setClickable(isClickable());
3926 info.setFocusable(isFocusable());
3927 info.setFocused(isFocused());
3928 info.setSelected(isSelected());
3929 info.setLongClickable(isLongClickable());
3930
3931 // TODO: These make sense only if we are in an AdapterView but all
3932 // views can be selected. Maybe from accessiiblity perspective
3933 // we should report as selectable view in an AdapterView.
3934 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3935 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3936
3937 if (isFocusable()) {
3938 if (isFocused()) {
3939 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3940 } else {
3941 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3942 }
3943 }
3944 }
3945
3946 /**
3947 * Gets the unique identifier of this view on the screen for accessibility purposes.
3948 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3949 *
3950 * @return The view accessibility id.
3951 *
3952 * @hide
3953 */
3954 public int getAccessibilityViewId() {
3955 if (mAccessibilityViewId == NO_ID) {
3956 mAccessibilityViewId = sNextAccessibilityViewId++;
3957 }
3958 return mAccessibilityViewId;
3959 }
3960
3961 /**
3962 * Gets the unique identifier of the window in which this View reseides.
3963 *
3964 * @return The window accessibility id.
3965 *
3966 * @hide
3967 */
3968 public int getAccessibilityWindowId() {
3969 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
3970 }
3971
3972 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07003973 * Gets the {@link View} description. It briefly describes the view and is
3974 * primarily used for accessibility support. Set this property to enable
3975 * better accessibility support for your application. This is especially
3976 * true for views that do not have textual representation (For example,
3977 * ImageButton).
3978 *
3979 * @return The content descriptiopn.
3980 *
3981 * @attr ref android.R.styleable#View_contentDescription
3982 */
3983 public CharSequence getContentDescription() {
3984 return mContentDescription;
3985 }
3986
3987 /**
3988 * Sets the {@link View} description. It briefly describes the view and is
3989 * primarily used for accessibility support. Set this property to enable
3990 * better accessibility support for your application. This is especially
3991 * true for views that do not have textual representation (For example,
3992 * ImageButton).
3993 *
3994 * @param contentDescription The content description.
3995 *
3996 * @attr ref android.R.styleable#View_contentDescription
3997 */
3998 public void setContentDescription(CharSequence contentDescription) {
3999 mContentDescription = contentDescription;
4000 }
4001
4002 /**
Romain Guya2431d02009-04-30 16:30:00 -07004003 * Invoked whenever this view loses focus, either by losing window focus or by losing
4004 * focus within its window. This method can be used to clear any state tied to the
4005 * focus. For instance, if a button is held pressed with the trackball and the window
4006 * loses focus, this method can be used to cancel the press.
4007 *
4008 * Subclasses of View overriding this method should always call super.onFocusLost().
4009 *
4010 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004011 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004012 *
4013 * @hide pending API council approval
4014 */
4015 protected void onFocusLost() {
4016 resetPressedState();
4017 }
4018
4019 private void resetPressedState() {
4020 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4021 return;
4022 }
4023
4024 if (isPressed()) {
4025 setPressed(false);
4026
4027 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004028 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004029 }
4030 }
4031 }
4032
4033 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004034 * Returns true if this view has focus
4035 *
4036 * @return True if this view has focus, false otherwise.
4037 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004038 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004039 public boolean isFocused() {
4040 return (mPrivateFlags & FOCUSED) != 0;
4041 }
4042
4043 /**
4044 * Find the view in the hierarchy rooted at this view that currently has
4045 * focus.
4046 *
4047 * @return The view that currently has focus, or null if no focused view can
4048 * be found.
4049 */
4050 public View findFocus() {
4051 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4052 }
4053
4054 /**
4055 * Change whether this view is one of the set of scrollable containers in
4056 * its window. This will be used to determine whether the window can
4057 * resize or must pan when a soft input area is open -- scrollable
4058 * containers allow the window to use resize mode since the container
4059 * will appropriately shrink.
4060 */
4061 public void setScrollContainer(boolean isScrollContainer) {
4062 if (isScrollContainer) {
4063 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4064 mAttachInfo.mScrollContainers.add(this);
4065 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4066 }
4067 mPrivateFlags |= SCROLL_CONTAINER;
4068 } else {
4069 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4070 mAttachInfo.mScrollContainers.remove(this);
4071 }
4072 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4073 }
4074 }
4075
4076 /**
4077 * Returns the quality of the drawing cache.
4078 *
4079 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4080 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4081 *
4082 * @see #setDrawingCacheQuality(int)
4083 * @see #setDrawingCacheEnabled(boolean)
4084 * @see #isDrawingCacheEnabled()
4085 *
4086 * @attr ref android.R.styleable#View_drawingCacheQuality
4087 */
4088 public int getDrawingCacheQuality() {
4089 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4090 }
4091
4092 /**
4093 * Set the drawing cache quality of this view. This value is used only when the
4094 * drawing cache is enabled
4095 *
4096 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4097 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4098 *
4099 * @see #getDrawingCacheQuality()
4100 * @see #setDrawingCacheEnabled(boolean)
4101 * @see #isDrawingCacheEnabled()
4102 *
4103 * @attr ref android.R.styleable#View_drawingCacheQuality
4104 */
4105 public void setDrawingCacheQuality(int quality) {
4106 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4107 }
4108
4109 /**
4110 * Returns whether the screen should remain on, corresponding to the current
4111 * value of {@link #KEEP_SCREEN_ON}.
4112 *
4113 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4114 *
4115 * @see #setKeepScreenOn(boolean)
4116 *
4117 * @attr ref android.R.styleable#View_keepScreenOn
4118 */
4119 public boolean getKeepScreenOn() {
4120 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4121 }
4122
4123 /**
4124 * Controls whether the screen should remain on, modifying the
4125 * value of {@link #KEEP_SCREEN_ON}.
4126 *
4127 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4128 *
4129 * @see #getKeepScreenOn()
4130 *
4131 * @attr ref android.R.styleable#View_keepScreenOn
4132 */
4133 public void setKeepScreenOn(boolean keepScreenOn) {
4134 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4135 }
4136
4137 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004138 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4139 * @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 -08004140 *
4141 * @attr ref android.R.styleable#View_nextFocusLeft
4142 */
4143 public int getNextFocusLeftId() {
4144 return mNextFocusLeftId;
4145 }
4146
4147 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004148 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4149 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4150 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004151 *
4152 * @attr ref android.R.styleable#View_nextFocusLeft
4153 */
4154 public void setNextFocusLeftId(int nextFocusLeftId) {
4155 mNextFocusLeftId = nextFocusLeftId;
4156 }
4157
4158 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004159 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4160 * @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 -08004161 *
4162 * @attr ref android.R.styleable#View_nextFocusRight
4163 */
4164 public int getNextFocusRightId() {
4165 return mNextFocusRightId;
4166 }
4167
4168 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004169 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4170 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4171 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004172 *
4173 * @attr ref android.R.styleable#View_nextFocusRight
4174 */
4175 public void setNextFocusRightId(int nextFocusRightId) {
4176 mNextFocusRightId = nextFocusRightId;
4177 }
4178
4179 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004180 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4181 * @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 -08004182 *
4183 * @attr ref android.R.styleable#View_nextFocusUp
4184 */
4185 public int getNextFocusUpId() {
4186 return mNextFocusUpId;
4187 }
4188
4189 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004190 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4191 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4192 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004193 *
4194 * @attr ref android.R.styleable#View_nextFocusUp
4195 */
4196 public void setNextFocusUpId(int nextFocusUpId) {
4197 mNextFocusUpId = nextFocusUpId;
4198 }
4199
4200 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004201 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4202 * @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 -08004203 *
4204 * @attr ref android.R.styleable#View_nextFocusDown
4205 */
4206 public int getNextFocusDownId() {
4207 return mNextFocusDownId;
4208 }
4209
4210 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004211 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4212 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4213 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004214 *
4215 * @attr ref android.R.styleable#View_nextFocusDown
4216 */
4217 public void setNextFocusDownId(int nextFocusDownId) {
4218 mNextFocusDownId = nextFocusDownId;
4219 }
4220
4221 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004222 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4223 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4224 *
4225 * @attr ref android.R.styleable#View_nextFocusForward
4226 */
4227 public int getNextFocusForwardId() {
4228 return mNextFocusForwardId;
4229 }
4230
4231 /**
4232 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4233 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4234 * decide automatically.
4235 *
4236 * @attr ref android.R.styleable#View_nextFocusForward
4237 */
4238 public void setNextFocusForwardId(int nextFocusForwardId) {
4239 mNextFocusForwardId = nextFocusForwardId;
4240 }
4241
4242 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004243 * Returns the visibility of this view and all of its ancestors
4244 *
4245 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4246 */
4247 public boolean isShown() {
4248 View current = this;
4249 //noinspection ConstantConditions
4250 do {
4251 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4252 return false;
4253 }
4254 ViewParent parent = current.mParent;
4255 if (parent == null) {
4256 return false; // We are not attached to the view root
4257 }
4258 if (!(parent instanceof View)) {
4259 return true;
4260 }
4261 current = (View) parent;
4262 } while (current != null);
4263
4264 return false;
4265 }
4266
4267 /**
4268 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4269 * is set
4270 *
4271 * @param insets Insets for system windows
4272 *
4273 * @return True if this view applied the insets, false otherwise
4274 */
4275 protected boolean fitSystemWindows(Rect insets) {
4276 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4277 mPaddingLeft = insets.left;
4278 mPaddingTop = insets.top;
4279 mPaddingRight = insets.right;
4280 mPaddingBottom = insets.bottom;
4281 requestLayout();
4282 return true;
4283 }
4284 return false;
4285 }
4286
4287 /**
4288 * Returns the visibility status for this view.
4289 *
4290 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4291 * @attr ref android.R.styleable#View_visibility
4292 */
4293 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004294 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4295 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4296 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004297 })
4298 public int getVisibility() {
4299 return mViewFlags & VISIBILITY_MASK;
4300 }
4301
4302 /**
4303 * Set the enabled state of this view.
4304 *
4305 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4306 * @attr ref android.R.styleable#View_visibility
4307 */
4308 @RemotableViewMethod
4309 public void setVisibility(int visibility) {
4310 setFlags(visibility, VISIBILITY_MASK);
4311 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4312 }
4313
4314 /**
4315 * Returns the enabled status for this view. The interpretation of the
4316 * enabled state varies by subclass.
4317 *
4318 * @return True if this view is enabled, false otherwise.
4319 */
4320 @ViewDebug.ExportedProperty
4321 public boolean isEnabled() {
4322 return (mViewFlags & ENABLED_MASK) == ENABLED;
4323 }
4324
4325 /**
4326 * Set the enabled state of this view. The interpretation of the enabled
4327 * state varies by subclass.
4328 *
4329 * @param enabled True if this view is enabled, false otherwise.
4330 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004331 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004332 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004333 if (enabled == isEnabled()) return;
4334
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004335 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4336
4337 /*
4338 * The View most likely has to change its appearance, so refresh
4339 * the drawable state.
4340 */
4341 refreshDrawableState();
4342
4343 // Invalidate too, since the default behavior for views is to be
4344 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004345 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004346 }
4347
4348 /**
4349 * Set whether this view can receive the focus.
4350 *
4351 * Setting this to false will also ensure that this view is not focusable
4352 * in touch mode.
4353 *
4354 * @param focusable If true, this view can receive the focus.
4355 *
4356 * @see #setFocusableInTouchMode(boolean)
4357 * @attr ref android.R.styleable#View_focusable
4358 */
4359 public void setFocusable(boolean focusable) {
4360 if (!focusable) {
4361 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4362 }
4363 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4364 }
4365
4366 /**
4367 * Set whether this view can receive focus while in touch mode.
4368 *
4369 * Setting this to true will also ensure that this view is focusable.
4370 *
4371 * @param focusableInTouchMode If true, this view can receive the focus while
4372 * in touch mode.
4373 *
4374 * @see #setFocusable(boolean)
4375 * @attr ref android.R.styleable#View_focusableInTouchMode
4376 */
4377 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4378 // Focusable in touch mode should always be set before the focusable flag
4379 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4380 // which, in touch mode, will not successfully request focus on this view
4381 // because the focusable in touch mode flag is not set
4382 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4383 if (focusableInTouchMode) {
4384 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4385 }
4386 }
4387
4388 /**
4389 * Set whether this view should have sound effects enabled for events such as
4390 * clicking and touching.
4391 *
4392 * <p>You may wish to disable sound effects for a view if you already play sounds,
4393 * for instance, a dial key that plays dtmf tones.
4394 *
4395 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4396 * @see #isSoundEffectsEnabled()
4397 * @see #playSoundEffect(int)
4398 * @attr ref android.R.styleable#View_soundEffectsEnabled
4399 */
4400 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4401 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4402 }
4403
4404 /**
4405 * @return whether this view should have sound effects enabled for events such as
4406 * clicking and touching.
4407 *
4408 * @see #setSoundEffectsEnabled(boolean)
4409 * @see #playSoundEffect(int)
4410 * @attr ref android.R.styleable#View_soundEffectsEnabled
4411 */
4412 @ViewDebug.ExportedProperty
4413 public boolean isSoundEffectsEnabled() {
4414 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4415 }
4416
4417 /**
4418 * Set whether this view should have haptic feedback for events such as
4419 * long presses.
4420 *
4421 * <p>You may wish to disable haptic feedback if your view already controls
4422 * its own haptic feedback.
4423 *
4424 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4425 * @see #isHapticFeedbackEnabled()
4426 * @see #performHapticFeedback(int)
4427 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4428 */
4429 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4430 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4431 }
4432
4433 /**
4434 * @return whether this view should have haptic feedback enabled for events
4435 * long presses.
4436 *
4437 * @see #setHapticFeedbackEnabled(boolean)
4438 * @see #performHapticFeedback(int)
4439 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4440 */
4441 @ViewDebug.ExportedProperty
4442 public boolean isHapticFeedbackEnabled() {
4443 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4444 }
4445
4446 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004447 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004448 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004449 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4450 * {@link #LAYOUT_DIRECTION_RTL},
4451 * {@link #LAYOUT_DIRECTION_INHERIT} or
4452 * {@link #LAYOUT_DIRECTION_LOCALE}.
4453 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004454 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004455 * @hide
4456 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004457 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004458 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4459 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4460 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4461 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004462 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004463 public int getLayoutDirection() {
4464 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004465 }
4466
4467 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004468 * Set the layout direction for this view. This will propagate a reset of layout direction
4469 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004470 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004471 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4472 * {@link #LAYOUT_DIRECTION_RTL},
4473 * {@link #LAYOUT_DIRECTION_INHERIT} or
4474 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004475 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004476 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004477 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004478 * @hide
4479 */
4480 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004481 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004482 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004483 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004484 // Setting the flag will also request a layout.
4485 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4486 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004487 }
4488
4489 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004490 * Returns the resolved layout direction for this view.
4491 *
4492 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4493 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4494 *
4495 * @hide
4496 */
4497 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4498 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4499 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4500 })
4501 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004502 resolveLayoutDirectionIfNeeded();
4503 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004504 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4505 }
4506
4507 /**
4508 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4509 * layout attribute and/or the inherited value from the parent.</p>
4510 *
4511 * @return true if the layout is right-to-left.
4512 *
4513 * @hide
4514 */
4515 @ViewDebug.ExportedProperty(category = "layout")
4516 public boolean isLayoutRtl() {
4517 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4518 }
4519
4520 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004521 * If this view doesn't do any drawing on its own, set this flag to
4522 * allow further optimizations. By default, this flag is not set on
4523 * View, but could be set on some View subclasses such as ViewGroup.
4524 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004525 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4526 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004527 *
4528 * @param willNotDraw whether or not this View draw on its own
4529 */
4530 public void setWillNotDraw(boolean willNotDraw) {
4531 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4532 }
4533
4534 /**
4535 * Returns whether or not this View draws on its own.
4536 *
4537 * @return true if this view has nothing to draw, false otherwise
4538 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004539 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004540 public boolean willNotDraw() {
4541 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4542 }
4543
4544 /**
4545 * When a View's drawing cache is enabled, drawing is redirected to an
4546 * offscreen bitmap. Some views, like an ImageView, must be able to
4547 * bypass this mechanism if they already draw a single bitmap, to avoid
4548 * unnecessary usage of the memory.
4549 *
4550 * @param willNotCacheDrawing true if this view does not cache its
4551 * drawing, false otherwise
4552 */
4553 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4554 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4555 }
4556
4557 /**
4558 * Returns whether or not this View can cache its drawing or not.
4559 *
4560 * @return true if this view does not cache its drawing, false otherwise
4561 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004562 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004563 public boolean willNotCacheDrawing() {
4564 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4565 }
4566
4567 /**
4568 * Indicates whether this view reacts to click events or not.
4569 *
4570 * @return true if the view is clickable, false otherwise
4571 *
4572 * @see #setClickable(boolean)
4573 * @attr ref android.R.styleable#View_clickable
4574 */
4575 @ViewDebug.ExportedProperty
4576 public boolean isClickable() {
4577 return (mViewFlags & CLICKABLE) == CLICKABLE;
4578 }
4579
4580 /**
4581 * Enables or disables click events for this view. When a view
4582 * is clickable it will change its state to "pressed" on every click.
4583 * Subclasses should set the view clickable to visually react to
4584 * user's clicks.
4585 *
4586 * @param clickable true to make the view clickable, false otherwise
4587 *
4588 * @see #isClickable()
4589 * @attr ref android.R.styleable#View_clickable
4590 */
4591 public void setClickable(boolean clickable) {
4592 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4593 }
4594
4595 /**
4596 * Indicates whether this view reacts to long click events or not.
4597 *
4598 * @return true if the view is long clickable, false otherwise
4599 *
4600 * @see #setLongClickable(boolean)
4601 * @attr ref android.R.styleable#View_longClickable
4602 */
4603 public boolean isLongClickable() {
4604 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4605 }
4606
4607 /**
4608 * Enables or disables long click events for this view. When a view is long
4609 * clickable it reacts to the user holding down the button for a longer
4610 * duration than a tap. This event can either launch the listener or a
4611 * context menu.
4612 *
4613 * @param longClickable true to make the view long clickable, false otherwise
4614 * @see #isLongClickable()
4615 * @attr ref android.R.styleable#View_longClickable
4616 */
4617 public void setLongClickable(boolean longClickable) {
4618 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4619 }
4620
4621 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004622 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004623 *
4624 * @see #isClickable()
4625 * @see #setClickable(boolean)
4626 *
4627 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4628 * the View's internal state from a previously set "pressed" state.
4629 */
4630 public void setPressed(boolean pressed) {
4631 if (pressed) {
4632 mPrivateFlags |= PRESSED;
4633 } else {
4634 mPrivateFlags &= ~PRESSED;
4635 }
4636 refreshDrawableState();
4637 dispatchSetPressed(pressed);
4638 }
4639
4640 /**
4641 * Dispatch setPressed to all of this View's children.
4642 *
4643 * @see #setPressed(boolean)
4644 *
4645 * @param pressed The new pressed state
4646 */
4647 protected void dispatchSetPressed(boolean pressed) {
4648 }
4649
4650 /**
4651 * Indicates whether the view is currently in pressed state. Unless
4652 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4653 * the pressed state.
4654 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004655 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004656 * @see #isClickable()
4657 * @see #setClickable(boolean)
4658 *
4659 * @return true if the view is currently pressed, false otherwise
4660 */
4661 public boolean isPressed() {
4662 return (mPrivateFlags & PRESSED) == PRESSED;
4663 }
4664
4665 /**
4666 * Indicates whether this view will save its state (that is,
4667 * whether its {@link #onSaveInstanceState} method will be called).
4668 *
4669 * @return Returns true if the view state saving is enabled, else false.
4670 *
4671 * @see #setSaveEnabled(boolean)
4672 * @attr ref android.R.styleable#View_saveEnabled
4673 */
4674 public boolean isSaveEnabled() {
4675 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4676 }
4677
4678 /**
4679 * Controls whether the saving of this view's state is
4680 * enabled (that is, whether its {@link #onSaveInstanceState} method
4681 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004682 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004683 * for its state to be saved. This flag can only disable the
4684 * saving of this view; any child views may still have their state saved.
4685 *
4686 * @param enabled Set to false to <em>disable</em> state saving, or true
4687 * (the default) to allow it.
4688 *
4689 * @see #isSaveEnabled()
4690 * @see #setId(int)
4691 * @see #onSaveInstanceState()
4692 * @attr ref android.R.styleable#View_saveEnabled
4693 */
4694 public void setSaveEnabled(boolean enabled) {
4695 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4696 }
4697
Jeff Brown85a31762010-09-01 17:01:00 -07004698 /**
4699 * Gets whether the framework should discard touches when the view's
4700 * window is obscured by another visible window.
4701 * Refer to the {@link View} security documentation for more details.
4702 *
4703 * @return True if touch filtering is enabled.
4704 *
4705 * @see #setFilterTouchesWhenObscured(boolean)
4706 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4707 */
4708 @ViewDebug.ExportedProperty
4709 public boolean getFilterTouchesWhenObscured() {
4710 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4711 }
4712
4713 /**
4714 * Sets whether the framework should discard touches when the view's
4715 * window is obscured by another visible window.
4716 * Refer to the {@link View} security documentation for more details.
4717 *
4718 * @param enabled True if touch filtering should be enabled.
4719 *
4720 * @see #getFilterTouchesWhenObscured
4721 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4722 */
4723 public void setFilterTouchesWhenObscured(boolean enabled) {
4724 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4725 FILTER_TOUCHES_WHEN_OBSCURED);
4726 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004727
4728 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004729 * Indicates whether the entire hierarchy under this view will save its
4730 * state when a state saving traversal occurs from its parent. The default
4731 * is true; if false, these views will not be saved unless
4732 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4733 *
4734 * @return Returns true if the view state saving from parent is enabled, else false.
4735 *
4736 * @see #setSaveFromParentEnabled(boolean)
4737 */
4738 public boolean isSaveFromParentEnabled() {
4739 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4740 }
4741
4742 /**
4743 * Controls whether the entire hierarchy under this view will save its
4744 * state when a state saving traversal occurs from its parent. The default
4745 * is true; if false, these views will not be saved unless
4746 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4747 *
4748 * @param enabled Set to false to <em>disable</em> state saving, or true
4749 * (the default) to allow it.
4750 *
4751 * @see #isSaveFromParentEnabled()
4752 * @see #setId(int)
4753 * @see #onSaveInstanceState()
4754 */
4755 public void setSaveFromParentEnabled(boolean enabled) {
4756 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4757 }
4758
4759
4760 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004761 * Returns whether this View is able to take focus.
4762 *
4763 * @return True if this view can take focus, or false otherwise.
4764 * @attr ref android.R.styleable#View_focusable
4765 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004766 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004767 public final boolean isFocusable() {
4768 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4769 }
4770
4771 /**
4772 * When a view is focusable, it may not want to take focus when in touch mode.
4773 * For example, a button would like focus when the user is navigating via a D-pad
4774 * so that the user can click on it, but once the user starts touching the screen,
4775 * the button shouldn't take focus
4776 * @return Whether the view is focusable in touch mode.
4777 * @attr ref android.R.styleable#View_focusableInTouchMode
4778 */
4779 @ViewDebug.ExportedProperty
4780 public final boolean isFocusableInTouchMode() {
4781 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4782 }
4783
4784 /**
4785 * Find the nearest view in the specified direction that can take focus.
4786 * This does not actually give focus to that view.
4787 *
4788 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4789 *
4790 * @return The nearest focusable in the specified direction, or null if none
4791 * can be found.
4792 */
4793 public View focusSearch(int direction) {
4794 if (mParent != null) {
4795 return mParent.focusSearch(this, direction);
4796 } else {
4797 return null;
4798 }
4799 }
4800
4801 /**
4802 * This method is the last chance for the focused view and its ancestors to
4803 * respond to an arrow key. This is called when the focused view did not
4804 * consume the key internally, nor could the view system find a new view in
4805 * the requested direction to give focus to.
4806 *
4807 * @param focused The currently focused view.
4808 * @param direction The direction focus wants to move. One of FOCUS_UP,
4809 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4810 * @return True if the this view consumed this unhandled move.
4811 */
4812 public boolean dispatchUnhandledMove(View focused, int direction) {
4813 return false;
4814 }
4815
4816 /**
4817 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004818 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004819 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004820 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4821 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 * @return The user specified next view, or null if there is none.
4823 */
4824 View findUserSetNextFocus(View root, int direction) {
4825 switch (direction) {
4826 case FOCUS_LEFT:
4827 if (mNextFocusLeftId == View.NO_ID) return null;
4828 return findViewShouldExist(root, mNextFocusLeftId);
4829 case FOCUS_RIGHT:
4830 if (mNextFocusRightId == View.NO_ID) return null;
4831 return findViewShouldExist(root, mNextFocusRightId);
4832 case FOCUS_UP:
4833 if (mNextFocusUpId == View.NO_ID) return null;
4834 return findViewShouldExist(root, mNextFocusUpId);
4835 case FOCUS_DOWN:
4836 if (mNextFocusDownId == View.NO_ID) return null;
4837 return findViewShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004838 case FOCUS_FORWARD:
4839 if (mNextFocusForwardId == View.NO_ID) return null;
4840 return findViewShouldExist(root, mNextFocusForwardId);
4841 case FOCUS_BACKWARD: {
4842 final int id = mID;
4843 return root.findViewByPredicate(new Predicate<View>() {
4844 @Override
4845 public boolean apply(View t) {
4846 return t.mNextFocusForwardId == id;
4847 }
4848 });
4849 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004850 }
4851 return null;
4852 }
4853
4854 private static View findViewShouldExist(View root, int childViewId) {
4855 View result = root.findViewById(childViewId);
4856 if (result == null) {
4857 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4858 + "by user for id " + childViewId);
4859 }
4860 return result;
4861 }
4862
4863 /**
4864 * Find and return all focusable views that are descendants of this view,
4865 * possibly including this view if it is focusable itself.
4866 *
4867 * @param direction The direction of the focus
4868 * @return A list of focusable views
4869 */
4870 public ArrayList<View> getFocusables(int direction) {
4871 ArrayList<View> result = new ArrayList<View>(24);
4872 addFocusables(result, direction);
4873 return result;
4874 }
4875
4876 /**
4877 * Add any focusable views that are descendants of this view (possibly
4878 * including this view if it is focusable itself) to views. If we are in touch mode,
4879 * only add views that are also focusable in touch mode.
4880 *
4881 * @param views Focusable views found so far
4882 * @param direction The direction of the focus
4883 */
4884 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004885 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004887
svetoslavganov75986cf2009-05-14 22:28:01 -07004888 /**
4889 * Adds any focusable views that are descendants of this view (possibly
4890 * including this view if it is focusable itself) to views. This method
4891 * adds all focusable views regardless if we are in touch mode or
4892 * only views focusable in touch mode if we are in touch mode depending on
4893 * the focusable mode paramater.
4894 *
4895 * @param views Focusable views found so far or null if all we are interested is
4896 * the number of focusables.
4897 * @param direction The direction of the focus.
4898 * @param focusableMode The type of focusables to be added.
4899 *
4900 * @see #FOCUSABLES_ALL
4901 * @see #FOCUSABLES_TOUCH_MODE
4902 */
4903 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4904 if (!isFocusable()) {
4905 return;
4906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004907
svetoslavganov75986cf2009-05-14 22:28:01 -07004908 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4909 isInTouchMode() && !isFocusableInTouchMode()) {
4910 return;
4911 }
4912
4913 if (views != null) {
4914 views.add(this);
4915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004916 }
4917
4918 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004919 * Finds the Views that contain given text. The containment is case insensitive.
4920 * As View's text is considered any text content that View renders.
4921 *
4922 * @param outViews The output list of matching Views.
4923 * @param text The text to match against.
4924 */
4925 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4926 }
4927
4928 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004929 * Find and return all touchable views that are descendants of this view,
4930 * possibly including this view if it is touchable itself.
4931 *
4932 * @return A list of touchable views
4933 */
4934 public ArrayList<View> getTouchables() {
4935 ArrayList<View> result = new ArrayList<View>();
4936 addTouchables(result);
4937 return result;
4938 }
4939
4940 /**
4941 * Add any touchable views that are descendants of this view (possibly
4942 * including this view if it is touchable itself) to views.
4943 *
4944 * @param views Touchable views found so far
4945 */
4946 public void addTouchables(ArrayList<View> views) {
4947 final int viewFlags = mViewFlags;
4948
4949 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
4950 && (viewFlags & ENABLED_MASK) == ENABLED) {
4951 views.add(this);
4952 }
4953 }
4954
4955 /**
4956 * Call this to try to give focus to a specific view or to one of its
4957 * descendants.
4958 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004959 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4960 * false), or if it is focusable and it is not focusable in touch mode
4961 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004962 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004963 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004964 * have focus, and you want your parent to look for the next one.
4965 *
4966 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
4967 * {@link #FOCUS_DOWN} and <code>null</code>.
4968 *
4969 * @return Whether this view or one of its descendants actually took focus.
4970 */
4971 public final boolean requestFocus() {
4972 return requestFocus(View.FOCUS_DOWN);
4973 }
4974
4975
4976 /**
4977 * Call this to try to give focus to a specific view or to one of its
4978 * descendants and give it a hint about what direction focus is heading.
4979 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08004980 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
4981 * false), or if it is focusable and it is not focusable in touch mode
4982 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004984 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004985 * have focus, and you want your parent to look for the next one.
4986 *
4987 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
4988 * <code>null</code> set for the previously focused rectangle.
4989 *
4990 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4991 * @return Whether this view or one of its descendants actually took focus.
4992 */
4993 public final boolean requestFocus(int direction) {
4994 return requestFocus(direction, null);
4995 }
4996
4997 /**
4998 * Call this to try to give focus to a specific view or to one of its descendants
4999 * and give it hints about the direction and a specific rectangle that the focus
5000 * is coming from. The rectangle can help give larger views a finer grained hint
5001 * about where focus is coming from, and therefore, where to show selection, or
5002 * forward focus change internally.
5003 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005004 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5005 * false), or if it is focusable and it is not focusable in touch mode
5006 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005007 *
5008 * A View will not take focus if it is not visible.
5009 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005010 * A View will not take focus if one of its parents has
5011 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5012 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005013 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005014 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005015 * have focus, and you want your parent to look for the next one.
5016 *
5017 * You may wish to override this method if your custom {@link View} has an internal
5018 * {@link View} that it wishes to forward the request to.
5019 *
5020 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5021 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5022 * to give a finer grained hint about where focus is coming from. May be null
5023 * if there is no hint.
5024 * @return Whether this view or one of its descendants actually took focus.
5025 */
5026 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5027 // need to be focusable
5028 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5029 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5030 return false;
5031 }
5032
5033 // need to be focusable in touch mode if in touch mode
5034 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005035 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5036 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005037 }
5038
5039 // need to not have any parents blocking us
5040 if (hasAncestorThatBlocksDescendantFocus()) {
5041 return false;
5042 }
5043
5044 handleFocusGainInternal(direction, previouslyFocusedRect);
5045 return true;
5046 }
5047
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005048 /** Gets the ViewAncestor, or null if not attached. */
5049 /*package*/ ViewAncestor getViewAncestor() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005050 View root = getRootView();
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005051 return root != null ? (ViewAncestor)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005052 }
5053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005054 /**
5055 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5056 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5057 * touch mode to request focus when they are touched.
5058 *
5059 * @return Whether this view or one of its descendants actually took focus.
5060 *
5061 * @see #isInTouchMode()
5062 *
5063 */
5064 public final boolean requestFocusFromTouch() {
5065 // Leave touch mode if we need to
5066 if (isInTouchMode()) {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005067 ViewAncestor viewRoot = getViewAncestor();
Christopher Tate2c095f32010-10-04 14:13:40 -07005068 if (viewRoot != null) {
5069 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005070 }
5071 }
5072 return requestFocus(View.FOCUS_DOWN);
5073 }
5074
5075 /**
5076 * @return Whether any ancestor of this view blocks descendant focus.
5077 */
5078 private boolean hasAncestorThatBlocksDescendantFocus() {
5079 ViewParent ancestor = mParent;
5080 while (ancestor instanceof ViewGroup) {
5081 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5082 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5083 return true;
5084 } else {
5085 ancestor = vgAncestor.getParent();
5086 }
5087 }
5088 return false;
5089 }
5090
5091 /**
Romain Guya440b002010-02-24 15:57:54 -08005092 * @hide
5093 */
5094 public void dispatchStartTemporaryDetach() {
5095 onStartTemporaryDetach();
5096 }
5097
5098 /**
5099 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005100 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5101 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005102 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005103 */
5104 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005105 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005106 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005107 }
5108
5109 /**
5110 * @hide
5111 */
5112 public void dispatchFinishTemporaryDetach() {
5113 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005114 }
Romain Guy8506ab42009-06-11 17:35:47 -07005115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 /**
5117 * Called after {@link #onStartTemporaryDetach} when the container is done
5118 * changing the view.
5119 */
5120 public void onFinishTemporaryDetach() {
5121 }
Romain Guy8506ab42009-06-11 17:35:47 -07005122
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005123 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005124 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5125 * for this view's window. Returns null if the view is not currently attached
5126 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005127 * just use the standard high-level event callbacks like
5128 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005129 */
5130 public KeyEvent.DispatcherState getKeyDispatcherState() {
5131 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5132 }
Joe Malin32736f02011-01-19 16:14:20 -08005133
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005135 * Dispatch a key event before it is processed by any input method
5136 * associated with the view hierarchy. This can be used to intercept
5137 * key events in special situations before the IME consumes them; a
5138 * typical example would be handling the BACK key to update the application's
5139 * UI instead of allowing the IME to see it and close itself.
5140 *
5141 * @param event The key event to be dispatched.
5142 * @return True if the event was handled, false otherwise.
5143 */
5144 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5145 return onKeyPreIme(event.getKeyCode(), event);
5146 }
5147
5148 /**
5149 * Dispatch a key event to the next view on the focus path. This path runs
5150 * from the top of the view tree down to the currently focused view. If this
5151 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5152 * the next node down the focus path. This method also fires any key
5153 * listeners.
5154 *
5155 * @param event The key event to be dispatched.
5156 * @return True if the event was handled, false otherwise.
5157 */
5158 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005159 if (mInputEventConsistencyVerifier != null) {
5160 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5161 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005162
Jeff Brown21bc5c92011-02-28 18:27:14 -08005163 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005164 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005165 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5166 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5167 return true;
5168 }
5169
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005170 if (event.dispatch(this, mAttachInfo != null
5171 ? mAttachInfo.mKeyDispatchState : null, this)) {
5172 return true;
5173 }
5174
5175 if (mInputEventConsistencyVerifier != null) {
5176 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5177 }
5178 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 }
5180
5181 /**
5182 * Dispatches a key shortcut event.
5183 *
5184 * @param event The key event to be dispatched.
5185 * @return True if the event was handled by the view, false otherwise.
5186 */
5187 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5188 return onKeyShortcut(event.getKeyCode(), event);
5189 }
5190
5191 /**
5192 * Pass the touch screen motion event down to the target view, or this
5193 * view if it is the target.
5194 *
5195 * @param event The motion event to be dispatched.
5196 * @return True if the event was handled by the view, false otherwise.
5197 */
5198 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005199 if (mInputEventConsistencyVerifier != null) {
5200 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5201 }
5202
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005203 if (onFilterTouchEventForSecurity(event)) {
5204 //noinspection SimplifiableIfStatement
5205 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5206 mOnTouchListener.onTouch(this, event)) {
5207 return true;
5208 }
5209
5210 if (onTouchEvent(event)) {
5211 return true;
5212 }
Jeff Brown85a31762010-09-01 17:01:00 -07005213 }
5214
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005215 if (mInputEventConsistencyVerifier != null) {
5216 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005217 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005218 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005219 }
5220
5221 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005222 * Filter the touch event to apply security policies.
5223 *
5224 * @param event The motion event to be filtered.
5225 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005226 *
Jeff Brown85a31762010-09-01 17:01:00 -07005227 * @see #getFilterTouchesWhenObscured
5228 */
5229 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005230 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005231 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5232 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5233 // Window is obscured, drop this touch.
5234 return false;
5235 }
5236 return true;
5237 }
5238
5239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005240 * Pass a trackball motion event down to the focused view.
5241 *
5242 * @param event The motion event to be dispatched.
5243 * @return True if the event was handled by the view, false otherwise.
5244 */
5245 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005246 if (mInputEventConsistencyVerifier != null) {
5247 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5248 }
5249
Romain Guy02ccac62011-06-24 13:20:23 -07005250 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005251 }
5252
5253 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005254 * Dispatch a generic motion event.
5255 * <p>
5256 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5257 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005258 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005259 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005260 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005261 *
5262 * @param event The motion event to be dispatched.
5263 * @return True if the event was handled by the view, false otherwise.
5264 */
5265 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005266 if (mInputEventConsistencyVerifier != null) {
5267 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5268 }
5269
Jeff Browna032cc02011-03-07 16:56:21 -08005270 final int source = event.getSource();
5271 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5272 final int action = event.getAction();
5273 if (action == MotionEvent.ACTION_HOVER_ENTER
5274 || action == MotionEvent.ACTION_HOVER_MOVE
5275 || action == MotionEvent.ACTION_HOVER_EXIT) {
5276 if (dispatchHoverEvent(event)) {
Jeff Brown10b62902011-06-20 16:40:37 -07005277 // For compatibility with existing applications that handled HOVER_MOVE
5278 // events in onGenericMotionEvent, dispatch the event there. The
5279 // onHoverEvent method did not exist at the time.
5280 if (action == MotionEvent.ACTION_HOVER_MOVE) {
5281 dispatchGenericMotionEventInternal(event);
5282 }
Jeff Browna032cc02011-03-07 16:56:21 -08005283 return true;
5284 }
5285 } else if (dispatchGenericPointerEvent(event)) {
5286 return true;
5287 }
5288 } else if (dispatchGenericFocusedEvent(event)) {
5289 return true;
5290 }
5291
Jeff Brown10b62902011-06-20 16:40:37 -07005292 if (dispatchGenericMotionEventInternal(event)) {
5293 return true;
5294 }
5295
5296 if (mInputEventConsistencyVerifier != null) {
5297 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5298 }
5299 return false;
5300 }
5301
5302 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005303 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005304 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5305 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5306 return true;
5307 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005308
5309 if (onGenericMotionEvent(event)) {
5310 return true;
5311 }
5312
5313 if (mInputEventConsistencyVerifier != null) {
5314 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5315 }
5316 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005317 }
5318
5319 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005320 * Dispatch a hover event.
5321 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005322 * Do not call this method directly.
5323 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005324 * </p>
5325 *
5326 * @param event The motion event to be dispatched.
5327 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005328 */
5329 protected boolean dispatchHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005330 switch (event.getAction()) {
5331 case MotionEvent.ACTION_HOVER_ENTER:
5332 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
5333 mSendingHoverAccessibilityEvents = true;
5334 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
5335 }
5336 break;
5337 case MotionEvent.ACTION_HOVER_EXIT:
5338 if (mSendingHoverAccessibilityEvents) {
5339 mSendingHoverAccessibilityEvents = false;
5340 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
5341 }
5342 break;
5343 }
5344
Romain Guy02ccac62011-06-24 13:20:23 -07005345 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005346 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5347 && mOnHoverListener.onHover(this, event)) {
5348 return true;
5349 }
5350
Jeff Browna032cc02011-03-07 16:56:21 -08005351 return onHoverEvent(event);
5352 }
5353
5354 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005355 * Returns true if the view has a child to which it has recently sent
5356 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5357 * it does not have a hovered child, then it must be the innermost hovered view.
5358 * @hide
5359 */
5360 protected boolean hasHoveredChild() {
5361 return false;
5362 }
5363
5364 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005365 * Dispatch a generic motion event to the view under the first pointer.
5366 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005367 * Do not call this method directly.
5368 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005369 * </p>
5370 *
5371 * @param event The motion event to be dispatched.
5372 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005373 */
5374 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5375 return false;
5376 }
5377
5378 /**
5379 * Dispatch a generic motion event to the currently focused view.
5380 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005381 * Do not call this method directly.
5382 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005383 * </p>
5384 *
5385 * @param event The motion event to be dispatched.
5386 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005387 */
5388 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5389 return false;
5390 }
5391
5392 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005393 * Dispatch a pointer event.
5394 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005395 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5396 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5397 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005398 * and should not be expected to handle other pointing device features.
5399 * </p>
5400 *
5401 * @param event The motion event to be dispatched.
5402 * @return True if the event was handled by the view, false otherwise.
5403 * @hide
5404 */
5405 public final boolean dispatchPointerEvent(MotionEvent event) {
5406 if (event.isTouchEvent()) {
5407 return dispatchTouchEvent(event);
5408 } else {
5409 return dispatchGenericMotionEvent(event);
5410 }
5411 }
5412
5413 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005414 * Called when the window containing this view gains or loses window focus.
5415 * ViewGroups should override to route to their children.
5416 *
5417 * @param hasFocus True if the window containing this view now has focus,
5418 * false otherwise.
5419 */
5420 public void dispatchWindowFocusChanged(boolean hasFocus) {
5421 onWindowFocusChanged(hasFocus);
5422 }
5423
5424 /**
5425 * Called when the window containing this view gains or loses focus. Note
5426 * that this is separate from view focus: to receive key events, both
5427 * your view and its window must have focus. If a window is displayed
5428 * on top of yours that takes input focus, then your own window will lose
5429 * focus but the view focus will remain unchanged.
5430 *
5431 * @param hasWindowFocus True if the window containing this view now has
5432 * focus, false otherwise.
5433 */
5434 public void onWindowFocusChanged(boolean hasWindowFocus) {
5435 InputMethodManager imm = InputMethodManager.peekInstance();
5436 if (!hasWindowFocus) {
5437 if (isPressed()) {
5438 setPressed(false);
5439 }
5440 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5441 imm.focusOut(this);
5442 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005443 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005444 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005445 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005446 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5447 imm.focusIn(this);
5448 }
5449 refreshDrawableState();
5450 }
5451
5452 /**
5453 * Returns true if this view is in a window that currently has window focus.
5454 * Note that this is not the same as the view itself having focus.
5455 *
5456 * @return True if this view is in a window that currently has window focus.
5457 */
5458 public boolean hasWindowFocus() {
5459 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5460 }
5461
5462 /**
Adam Powell326d8082009-12-09 15:10:07 -08005463 * Dispatch a view visibility change down the view hierarchy.
5464 * ViewGroups should override to route to their children.
5465 * @param changedView The view whose visibility changed. Could be 'this' or
5466 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005467 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5468 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005469 */
5470 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5471 onVisibilityChanged(changedView, visibility);
5472 }
5473
5474 /**
5475 * Called when the visibility of the view or an ancestor of the view is changed.
5476 * @param changedView The view whose visibility changed. Could be 'this' or
5477 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005478 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5479 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005480 */
5481 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005482 if (visibility == VISIBLE) {
5483 if (mAttachInfo != null) {
5484 initialAwakenScrollBars();
5485 } else {
5486 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5487 }
5488 }
Adam Powell326d8082009-12-09 15:10:07 -08005489 }
5490
5491 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005492 * Dispatch a hint about whether this view is displayed. For instance, when
5493 * a View moves out of the screen, it might receives a display hint indicating
5494 * the view is not displayed. Applications should not <em>rely</em> on this hint
5495 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005496 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005497 * @param hint A hint about whether or not this view is displayed:
5498 * {@link #VISIBLE} or {@link #INVISIBLE}.
5499 */
5500 public void dispatchDisplayHint(int hint) {
5501 onDisplayHint(hint);
5502 }
5503
5504 /**
5505 * Gives this view a hint about whether is displayed or not. For instance, when
5506 * a View moves out of the screen, it might receives a display hint indicating
5507 * the view is not displayed. Applications should not <em>rely</em> on this hint
5508 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005509 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005510 * @param hint A hint about whether or not this view is displayed:
5511 * {@link #VISIBLE} or {@link #INVISIBLE}.
5512 */
5513 protected void onDisplayHint(int hint) {
5514 }
5515
5516 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005517 * Dispatch a window visibility change down the view hierarchy.
5518 * ViewGroups should override to route to their children.
5519 *
5520 * @param visibility The new visibility of the window.
5521 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005522 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005523 */
5524 public void dispatchWindowVisibilityChanged(int visibility) {
5525 onWindowVisibilityChanged(visibility);
5526 }
5527
5528 /**
5529 * Called when the window containing has change its visibility
5530 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5531 * that this tells you whether or not your window is being made visible
5532 * to the window manager; this does <em>not</em> tell you whether or not
5533 * your window is obscured by other windows on the screen, even if it
5534 * is itself visible.
5535 *
5536 * @param visibility The new visibility of the window.
5537 */
5538 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005539 if (visibility == VISIBLE) {
5540 initialAwakenScrollBars();
5541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005542 }
5543
5544 /**
5545 * Returns the current visibility of the window this view is attached to
5546 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5547 *
5548 * @return Returns the current visibility of the view's window.
5549 */
5550 public int getWindowVisibility() {
5551 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5552 }
5553
5554 /**
5555 * Retrieve the overall visible display size in which the window this view is
5556 * attached to has been positioned in. This takes into account screen
5557 * decorations above the window, for both cases where the window itself
5558 * is being position inside of them or the window is being placed under
5559 * then and covered insets are used for the window to position its content
5560 * inside. In effect, this tells you the available area where content can
5561 * be placed and remain visible to users.
5562 *
5563 * <p>This function requires an IPC back to the window manager to retrieve
5564 * the requested information, so should not be used in performance critical
5565 * code like drawing.
5566 *
5567 * @param outRect Filled in with the visible display frame. If the view
5568 * is not attached to a window, this is simply the raw display size.
5569 */
5570 public void getWindowVisibleDisplayFrame(Rect outRect) {
5571 if (mAttachInfo != null) {
5572 try {
5573 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5574 } catch (RemoteException e) {
5575 return;
5576 }
5577 // XXX This is really broken, and probably all needs to be done
5578 // in the window manager, and we need to know more about whether
5579 // we want the area behind or in front of the IME.
5580 final Rect insets = mAttachInfo.mVisibleInsets;
5581 outRect.left += insets.left;
5582 outRect.top += insets.top;
5583 outRect.right -= insets.right;
5584 outRect.bottom -= insets.bottom;
5585 return;
5586 }
5587 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005588 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005589 }
5590
5591 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005592 * Dispatch a notification about a resource configuration change down
5593 * the view hierarchy.
5594 * ViewGroups should override to route to their children.
5595 *
5596 * @param newConfig The new resource configuration.
5597 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005598 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005599 */
5600 public void dispatchConfigurationChanged(Configuration newConfig) {
5601 onConfigurationChanged(newConfig);
5602 }
5603
5604 /**
5605 * Called when the current configuration of the resources being used
5606 * by the application have changed. You can use this to decide when
5607 * to reload resources that can changed based on orientation and other
5608 * configuration characterstics. You only need to use this if you are
5609 * not relying on the normal {@link android.app.Activity} mechanism of
5610 * recreating the activity instance upon a configuration change.
5611 *
5612 * @param newConfig The new resource configuration.
5613 */
5614 protected void onConfigurationChanged(Configuration newConfig) {
5615 }
5616
5617 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005618 * Private function to aggregate all per-view attributes in to the view
5619 * root.
5620 */
5621 void dispatchCollectViewAttributes(int visibility) {
5622 performCollectViewAttributes(visibility);
5623 }
5624
5625 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005626 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005627 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5628 mAttachInfo.mKeepScreenOn = true;
5629 }
5630 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5631 if (mOnSystemUiVisibilityChangeListener != null) {
5632 mAttachInfo.mHasSystemUiListeners = true;
5633 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005634 }
5635 }
5636
5637 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005638 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005639 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005640 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5641 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005642 ai.mRecomputeGlobalAttributes = true;
5643 }
5644 }
5645 }
5646
5647 /**
5648 * Returns whether the device is currently in touch mode. Touch mode is entered
5649 * once the user begins interacting with the device by touch, and affects various
5650 * things like whether focus is always visible to the user.
5651 *
5652 * @return Whether the device is in touch mode.
5653 */
5654 @ViewDebug.ExportedProperty
5655 public boolean isInTouchMode() {
5656 if (mAttachInfo != null) {
5657 return mAttachInfo.mInTouchMode;
5658 } else {
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005659 return ViewAncestor.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005660 }
5661 }
5662
5663 /**
5664 * Returns the context the view is running in, through which it can
5665 * access the current theme, resources, etc.
5666 *
5667 * @return The view's Context.
5668 */
5669 @ViewDebug.CapturedViewProperty
5670 public final Context getContext() {
5671 return mContext;
5672 }
5673
5674 /**
5675 * Handle a key event before it is processed by any input method
5676 * associated with the view hierarchy. This can be used to intercept
5677 * key events in special situations before the IME consumes them; a
5678 * typical example would be handling the BACK key to update the application's
5679 * UI instead of allowing the IME to see it and close itself.
5680 *
5681 * @param keyCode The value in event.getKeyCode().
5682 * @param event Description of the key event.
5683 * @return If you handled the event, return true. If you want to allow the
5684 * event to be handled by the next receiver, return false.
5685 */
5686 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5687 return false;
5688 }
5689
5690 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005691 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5692 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005693 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5694 * is released, if the view is enabled and clickable.
5695 *
5696 * @param keyCode A key code that represents the button pressed, from
5697 * {@link android.view.KeyEvent}.
5698 * @param event The KeyEvent object that defines the button action.
5699 */
5700 public boolean onKeyDown(int keyCode, KeyEvent event) {
5701 boolean result = false;
5702
5703 switch (keyCode) {
5704 case KeyEvent.KEYCODE_DPAD_CENTER:
5705 case KeyEvent.KEYCODE_ENTER: {
5706 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5707 return true;
5708 }
5709 // Long clickable items don't necessarily have to be clickable
5710 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5711 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5712 (event.getRepeatCount() == 0)) {
5713 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005714 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005715 return true;
5716 }
5717 break;
5718 }
5719 }
5720 return result;
5721 }
5722
5723 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005724 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5725 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5726 * the event).
5727 */
5728 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5729 return false;
5730 }
5731
5732 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005733 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5734 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005735 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5736 * {@link KeyEvent#KEYCODE_ENTER} is released.
5737 *
5738 * @param keyCode A key code that represents the button pressed, from
5739 * {@link android.view.KeyEvent}.
5740 * @param event The KeyEvent object that defines the button action.
5741 */
5742 public boolean onKeyUp(int keyCode, KeyEvent event) {
5743 boolean result = false;
5744
5745 switch (keyCode) {
5746 case KeyEvent.KEYCODE_DPAD_CENTER:
5747 case KeyEvent.KEYCODE_ENTER: {
5748 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5749 return true;
5750 }
5751 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5752 setPressed(false);
5753
5754 if (!mHasPerformedLongPress) {
5755 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005756 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005757
5758 result = performClick();
5759 }
5760 }
5761 break;
5762 }
5763 }
5764 return result;
5765 }
5766
5767 /**
5768 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5769 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5770 * the event).
5771 *
5772 * @param keyCode A key code that represents the button pressed, from
5773 * {@link android.view.KeyEvent}.
5774 * @param repeatCount The number of times the action was made.
5775 * @param event The KeyEvent object that defines the button action.
5776 */
5777 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5778 return false;
5779 }
5780
5781 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005782 * Called on the focused view when a key shortcut event is not handled.
5783 * Override this method to implement local key shortcuts for the View.
5784 * Key shortcuts can also be implemented by setting the
5785 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005786 *
5787 * @param keyCode The value in event.getKeyCode().
5788 * @param event Description of the key event.
5789 * @return If you handled the event, return true. If you want to allow the
5790 * event to be handled by the next receiver, return false.
5791 */
5792 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5793 return false;
5794 }
5795
5796 /**
5797 * Check whether the called view is a text editor, in which case it
5798 * would make sense to automatically display a soft input window for
5799 * it. Subclasses should override this if they implement
5800 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005801 * a call on that method would return a non-null InputConnection, and
5802 * they are really a first-class editor that the user would normally
5803 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005804 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005805 * <p>The default implementation always returns false. This does
5806 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5807 * will not be called or the user can not otherwise perform edits on your
5808 * view; it is just a hint to the system that this is not the primary
5809 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005810 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005811 * @return Returns true if this view is a text editor, else false.
5812 */
5813 public boolean onCheckIsTextEditor() {
5814 return false;
5815 }
Romain Guy8506ab42009-06-11 17:35:47 -07005816
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005817 /**
5818 * Create a new InputConnection for an InputMethod to interact
5819 * with the view. The default implementation returns null, since it doesn't
5820 * support input methods. You can override this to implement such support.
5821 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005822 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005823 * <p>When implementing this, you probably also want to implement
5824 * {@link #onCheckIsTextEditor()} to indicate you will return a
5825 * non-null InputConnection.
5826 *
5827 * @param outAttrs Fill in with attribute information about the connection.
5828 */
5829 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5830 return null;
5831 }
5832
5833 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005834 * Called by the {@link android.view.inputmethod.InputMethodManager}
5835 * when a view who is not the current
5836 * input connection target is trying to make a call on the manager. The
5837 * default implementation returns false; you can override this to return
5838 * true for certain views if you are performing InputConnection proxying
5839 * to them.
5840 * @param view The View that is making the InputMethodManager call.
5841 * @return Return true to allow the call, false to reject.
5842 */
5843 public boolean checkInputConnectionProxy(View view) {
5844 return false;
5845 }
Romain Guy8506ab42009-06-11 17:35:47 -07005846
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005847 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005848 * Show the context menu for this view. It is not safe to hold on to the
5849 * menu after returning from this method.
5850 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005851 * You should normally not overload this method. Overload
5852 * {@link #onCreateContextMenu(ContextMenu)} or define an
5853 * {@link OnCreateContextMenuListener} to add items to the context menu.
5854 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005855 * @param menu The context menu to populate
5856 */
5857 public void createContextMenu(ContextMenu menu) {
5858 ContextMenuInfo menuInfo = getContextMenuInfo();
5859
5860 // Sets the current menu info so all items added to menu will have
5861 // my extra info set.
5862 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5863
5864 onCreateContextMenu(menu);
5865 if (mOnCreateContextMenuListener != null) {
5866 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5867 }
5868
5869 // Clear the extra information so subsequent items that aren't mine don't
5870 // have my extra info.
5871 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5872
5873 if (mParent != null) {
5874 mParent.createContextMenu(menu);
5875 }
5876 }
5877
5878 /**
5879 * Views should implement this if they have extra information to associate
5880 * with the context menu. The return result is supplied as a parameter to
5881 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5882 * callback.
5883 *
5884 * @return Extra information about the item for which the context menu
5885 * should be shown. This information will vary across different
5886 * subclasses of View.
5887 */
5888 protected ContextMenuInfo getContextMenuInfo() {
5889 return null;
5890 }
5891
5892 /**
5893 * Views should implement this if the view itself is going to add items to
5894 * the context menu.
5895 *
5896 * @param menu the context menu to populate
5897 */
5898 protected void onCreateContextMenu(ContextMenu menu) {
5899 }
5900
5901 /**
5902 * Implement this method to handle trackball motion events. The
5903 * <em>relative</em> movement of the trackball since the last event
5904 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5905 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5906 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5907 * they will often be fractional values, representing the more fine-grained
5908 * movement information available from a trackball).
5909 *
5910 * @param event The motion event.
5911 * @return True if the event was handled, false otherwise.
5912 */
5913 public boolean onTrackballEvent(MotionEvent event) {
5914 return false;
5915 }
5916
5917 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005918 * Implement this method to handle generic motion events.
5919 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005920 * Generic motion events describe joystick movements, mouse hovers, track pad
5921 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005922 * {@link MotionEvent#getSource() source} of the motion event specifies
5923 * the class of input that was received. Implementations of this method
5924 * must examine the bits in the source before processing the event.
5925 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005926 * </p><p>
5927 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5928 * are delivered to the view under the pointer. All other generic motion events are
5929 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005930 * </p>
5931 * <code>
5932 * public boolean onGenericMotionEvent(MotionEvent event) {
5933 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005934 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5935 * // process the joystick movement...
5936 * return true;
5937 * }
5938 * }
5939 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5940 * switch (event.getAction()) {
5941 * case MotionEvent.ACTION_HOVER_MOVE:
5942 * // process the mouse hover movement...
5943 * return true;
5944 * case MotionEvent.ACTION_SCROLL:
5945 * // process the scroll wheel movement...
5946 * return true;
5947 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005948 * }
5949 * return super.onGenericMotionEvent(event);
5950 * }
5951 * </code>
5952 *
5953 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08005954 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08005955 */
5956 public boolean onGenericMotionEvent(MotionEvent event) {
5957 return false;
5958 }
5959
5960 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005961 * Implement this method to handle hover events.
5962 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07005963 * This method is called whenever a pointer is hovering into, over, or out of the
5964 * bounds of a view and the view is not currently being touched.
5965 * Hover events are represented as pointer events with action
5966 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
5967 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
5968 * </p>
5969 * <ul>
5970 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
5971 * when the pointer enters the bounds of the view.</li>
5972 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
5973 * when the pointer has already entered the bounds of the view and has moved.</li>
5974 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
5975 * when the pointer has exited the bounds of the view or when the pointer is
5976 * about to go down due to a button click, tap, or similar user action that
5977 * causes the view to be touched.</li>
5978 * </ul>
5979 * <p>
5980 * The view should implement this method to return true to indicate that it is
5981 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08005982 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07005983 * The default implementation calls {@link #setHovered} to update the hovered state
5984 * of the view when a hover enter or hover exit event is received, if the view
5985 * is enabled and is clickable.
Jeff Browna032cc02011-03-07 16:56:21 -08005986 * </p>
5987 *
5988 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07005989 * @return True if the view handled the hover event.
5990 *
5991 * @see #isHovered
5992 * @see #setHovered
5993 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08005994 */
5995 public boolean onHoverEvent(MotionEvent event) {
Jeff Brown87b7f802011-06-21 18:35:45 -07005996 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07005997 switch (event.getAction()) {
5998 case MotionEvent.ACTION_HOVER_ENTER:
5999 setHovered(true);
6000 break;
6001 case MotionEvent.ACTION_HOVER_EXIT:
6002 setHovered(false);
6003 break;
6004 }
6005 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006006 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006007 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006008 }
6009
6010 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006011 * Returns true if the view should handle {@link #onHoverEvent}
6012 * by calling {@link #setHovered} to change its hovered state.
6013 *
6014 * @return True if the view is hoverable.
6015 */
6016 private boolean isHoverable() {
6017 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006018 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006019 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6020 return false;
6021 }
6022
6023 return (viewFlags & CLICKABLE) == CLICKABLE
6024 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6025 }
6026
6027 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006028 * Returns true if the view is currently hovered.
6029 *
6030 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006031 *
6032 * @see #setHovered
6033 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006034 */
Jeff Brown10b62902011-06-20 16:40:37 -07006035 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006036 public boolean isHovered() {
6037 return (mPrivateFlags & HOVERED) != 0;
6038 }
6039
6040 /**
6041 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006042 * <p>
6043 * Calling this method also changes the drawable state of the view. This
6044 * enables the view to react to hover by using different drawable resources
6045 * to change its appearance.
6046 * </p><p>
6047 * The {@link #onHoverChanged} method is called when the hovered state changes.
6048 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006049 *
6050 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006051 *
6052 * @see #isHovered
6053 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006054 */
6055 public void setHovered(boolean hovered) {
6056 if (hovered) {
6057 if ((mPrivateFlags & HOVERED) == 0) {
6058 mPrivateFlags |= HOVERED;
6059 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006060 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006061 }
6062 } else {
6063 if ((mPrivateFlags & HOVERED) != 0) {
6064 mPrivateFlags &= ~HOVERED;
6065 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006066 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006067 }
6068 }
6069 }
6070
6071 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006072 * Implement this method to handle hover state changes.
6073 * <p>
6074 * This method is called whenever the hover state changes as a result of a
6075 * call to {@link #setHovered}.
6076 * </p>
6077 *
6078 * @param hovered The current hover state, as returned by {@link #isHovered}.
6079 *
6080 * @see #isHovered
6081 * @see #setHovered
6082 */
6083 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006084 }
6085
6086 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006087 * Implement this method to handle touch screen motion events.
6088 *
6089 * @param event The motion event.
6090 * @return True if the event was handled, false otherwise.
6091 */
6092 public boolean onTouchEvent(MotionEvent event) {
6093 final int viewFlags = mViewFlags;
6094
6095 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006096 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6097 mPrivateFlags &= ~PRESSED;
6098 refreshDrawableState();
6099 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006100 // A disabled view that is clickable still consumes the touch
6101 // events, it just doesn't respond to them.
6102 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6103 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6104 }
6105
6106 if (mTouchDelegate != null) {
6107 if (mTouchDelegate.onTouchEvent(event)) {
6108 return true;
6109 }
6110 }
6111
6112 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6113 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6114 switch (event.getAction()) {
6115 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006116 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6117 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006118 // take focus if we don't have it already and we should in
6119 // touch mode.
6120 boolean focusTaken = false;
6121 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6122 focusTaken = requestFocus();
6123 }
6124
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006125 if (prepressed) {
6126 // The button is being released before we actually
6127 // showed it as pressed. Make it show the pressed
6128 // state now (before scheduling the click) to ensure
6129 // the user sees it.
6130 mPrivateFlags |= PRESSED;
6131 refreshDrawableState();
6132 }
Joe Malin32736f02011-01-19 16:14:20 -08006133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006134 if (!mHasPerformedLongPress) {
6135 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006136 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006137
6138 // Only perform take click actions if we were in the pressed state
6139 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006140 // Use a Runnable and post this rather than calling
6141 // performClick directly. This lets other visual state
6142 // of the view update before click actions start.
6143 if (mPerformClick == null) {
6144 mPerformClick = new PerformClick();
6145 }
6146 if (!post(mPerformClick)) {
6147 performClick();
6148 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006149 }
6150 }
6151
6152 if (mUnsetPressedState == null) {
6153 mUnsetPressedState = new UnsetPressedState();
6154 }
6155
Adam Powelle14579b2009-12-16 18:39:52 -08006156 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006157 postDelayed(mUnsetPressedState,
6158 ViewConfiguration.getPressedStateDuration());
6159 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006160 // If the post failed, unpress right now
6161 mUnsetPressedState.run();
6162 }
Adam Powelle14579b2009-12-16 18:39:52 -08006163 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 }
6165 break;
6166
6167 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006168 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006169
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006170 if (performButtonActionOnTouchDown(event)) {
6171 break;
6172 }
6173
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006174 // Walk up the hierarchy to determine if we're inside a scrolling container.
6175 boolean isInScrollingContainer = false;
6176 ViewParent p = getParent();
6177 while (p != null && p instanceof ViewGroup) {
6178 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6179 isInScrollingContainer = true;
6180 break;
6181 }
6182 p = p.getParent();
6183 }
6184
6185 // For views inside a scrolling container, delay the pressed feedback for
6186 // a short period in case this is a scroll.
6187 if (isInScrollingContainer) {
6188 mPrivateFlags |= PREPRESSED;
6189 if (mPendingCheckForTap == null) {
6190 mPendingCheckForTap = new CheckForTap();
6191 }
6192 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6193 } else {
6194 // Not inside a scrolling container, so show the feedback right away
6195 mPrivateFlags |= PRESSED;
6196 refreshDrawableState();
6197 checkForLongClick(0);
6198 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006199 break;
6200
6201 case MotionEvent.ACTION_CANCEL:
6202 mPrivateFlags &= ~PRESSED;
6203 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006204 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006205 break;
6206
6207 case MotionEvent.ACTION_MOVE:
6208 final int x = (int) event.getX();
6209 final int y = (int) event.getY();
6210
6211 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006212 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006213 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006214 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006215 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006216 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006217 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006218
6219 // Need to switch from pressed to not pressed
6220 mPrivateFlags &= ~PRESSED;
6221 refreshDrawableState();
6222 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006223 }
6224 break;
6225 }
6226 return true;
6227 }
6228
6229 return false;
6230 }
6231
6232 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006233 * Remove the longpress detection timer.
6234 */
6235 private void removeLongPressCallback() {
6236 if (mPendingCheckForLongPress != null) {
6237 removeCallbacks(mPendingCheckForLongPress);
6238 }
6239 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006240
6241 /**
6242 * Remove the pending click action
6243 */
6244 private void removePerformClickCallback() {
6245 if (mPerformClick != null) {
6246 removeCallbacks(mPerformClick);
6247 }
6248 }
6249
Adam Powelle14579b2009-12-16 18:39:52 -08006250 /**
Romain Guya440b002010-02-24 15:57:54 -08006251 * Remove the prepress detection timer.
6252 */
6253 private void removeUnsetPressCallback() {
6254 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6255 setPressed(false);
6256 removeCallbacks(mUnsetPressedState);
6257 }
6258 }
6259
6260 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006261 * Remove the tap detection timer.
6262 */
6263 private void removeTapCallback() {
6264 if (mPendingCheckForTap != null) {
6265 mPrivateFlags &= ~PREPRESSED;
6266 removeCallbacks(mPendingCheckForTap);
6267 }
6268 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006269
6270 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006271 * Cancels a pending long press. Your subclass can use this if you
6272 * want the context menu to come up if the user presses and holds
6273 * at the same place, but you don't want it to come up if they press
6274 * and then move around enough to cause scrolling.
6275 */
6276 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006277 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006278
6279 /*
6280 * The prepressed state handled by the tap callback is a display
6281 * construct, but the tap callback will post a long press callback
6282 * less its own timeout. Remove it here.
6283 */
6284 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006285 }
6286
6287 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006288 * Remove the pending callback for sending a
6289 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6290 */
6291 private void removeSendViewScrolledAccessibilityEventCallback() {
6292 if (mSendViewScrolledAccessibilityEvent != null) {
6293 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6294 }
6295 }
6296
6297 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 * Sets the TouchDelegate for this View.
6299 */
6300 public void setTouchDelegate(TouchDelegate delegate) {
6301 mTouchDelegate = delegate;
6302 }
6303
6304 /**
6305 * Gets the TouchDelegate for this View.
6306 */
6307 public TouchDelegate getTouchDelegate() {
6308 return mTouchDelegate;
6309 }
6310
6311 /**
6312 * Set flags controlling behavior of this view.
6313 *
6314 * @param flags Constant indicating the value which should be set
6315 * @param mask Constant indicating the bit range that should be changed
6316 */
6317 void setFlags(int flags, int mask) {
6318 int old = mViewFlags;
6319 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6320
6321 int changed = mViewFlags ^ old;
6322 if (changed == 0) {
6323 return;
6324 }
6325 int privateFlags = mPrivateFlags;
6326
6327 /* Check if the FOCUSABLE bit has changed */
6328 if (((changed & FOCUSABLE_MASK) != 0) &&
6329 ((privateFlags & HAS_BOUNDS) !=0)) {
6330 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6331 && ((privateFlags & FOCUSED) != 0)) {
6332 /* Give up focus if we are no longer focusable */
6333 clearFocus();
6334 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6335 && ((privateFlags & FOCUSED) == 0)) {
6336 /*
6337 * Tell the view system that we are now available to take focus
6338 * if no one else already has it.
6339 */
6340 if (mParent != null) mParent.focusableViewAvailable(this);
6341 }
6342 }
6343
6344 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6345 if ((changed & VISIBILITY_MASK) != 0) {
6346 /*
6347 * If this view is becoming visible, set the DRAWN flag so that
6348 * the next invalidate() will not be skipped.
6349 */
6350 mPrivateFlags |= DRAWN;
6351
6352 needGlobalAttributesUpdate(true);
6353
6354 // a view becoming visible is worth notifying the parent
6355 // about in case nothing has focus. even if this specific view
6356 // isn't focusable, it may contain something that is, so let
6357 // the root view try to give this focus if nothing else does.
6358 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6359 mParent.focusableViewAvailable(this);
6360 }
6361 }
6362 }
6363
6364 /* Check if the GONE bit has changed */
6365 if ((changed & GONE) != 0) {
6366 needGlobalAttributesUpdate(false);
6367 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006368 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006369
Romain Guyecd80ee2009-12-03 17:13:02 -08006370 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6371 if (hasFocus()) clearFocus();
6372 destroyDrawingCache();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006373 }
6374 if (mAttachInfo != null) {
6375 mAttachInfo.mViewVisibilityChanged = true;
6376 }
6377 }
6378
6379 /* Check if the VISIBLE bit has changed */
6380 if ((changed & INVISIBLE) != 0) {
6381 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006382 /*
6383 * If this view is becoming invisible, set the DRAWN flag so that
6384 * the next invalidate() will not be skipped.
6385 */
6386 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387
6388 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6389 // root view becoming invisible shouldn't clear focus
6390 if (getRootView() != this) {
6391 clearFocus();
6392 }
6393 }
6394 if (mAttachInfo != null) {
6395 mAttachInfo.mViewVisibilityChanged = true;
6396 }
6397 }
6398
Adam Powell326d8082009-12-09 15:10:07 -08006399 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006400 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006401 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6402 ((View) mParent).invalidate(true);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006403 }
Adam Powell326d8082009-12-09 15:10:07 -08006404 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6405 }
6406
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006407 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6408 destroyDrawingCache();
6409 }
6410
6411 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6412 destroyDrawingCache();
6413 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006414 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006415 }
6416
6417 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6418 destroyDrawingCache();
6419 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6420 }
6421
6422 if ((changed & DRAW_MASK) != 0) {
6423 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6424 if (mBGDrawable != null) {
6425 mPrivateFlags &= ~SKIP_DRAW;
6426 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6427 } else {
6428 mPrivateFlags |= SKIP_DRAW;
6429 }
6430 } else {
6431 mPrivateFlags &= ~SKIP_DRAW;
6432 }
6433 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006434 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006435 }
6436
6437 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006438 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 mParent.recomputeViewAttributes(this);
6440 }
6441 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006442
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006443 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006444 requestLayout();
6445 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006446 }
6447
6448 /**
6449 * Change the view's z order in the tree, so it's on top of other sibling
6450 * views
6451 */
6452 public void bringToFront() {
6453 if (mParent != null) {
6454 mParent.bringChildToFront(this);
6455 }
6456 }
6457
6458 /**
6459 * This is called in response to an internal scroll in this view (i.e., the
6460 * view scrolled its own contents). This is typically as a result of
6461 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6462 * called.
6463 *
6464 * @param l Current horizontal scroll origin.
6465 * @param t Current vertical scroll origin.
6466 * @param oldl Previous horizontal scroll origin.
6467 * @param oldt Previous vertical scroll origin.
6468 */
6469 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006470 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6471 postSendViewScrolledAccessibilityEventCallback();
6472 }
6473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006474 mBackgroundSizeChanged = true;
6475
6476 final AttachInfo ai = mAttachInfo;
6477 if (ai != null) {
6478 ai.mViewScrollChanged = true;
6479 }
6480 }
6481
6482 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006483 * Interface definition for a callback to be invoked when the layout bounds of a view
6484 * changes due to layout processing.
6485 */
6486 public interface OnLayoutChangeListener {
6487 /**
6488 * Called when the focus state of a view has changed.
6489 *
6490 * @param v The view whose state has changed.
6491 * @param left The new value of the view's left property.
6492 * @param top The new value of the view's top property.
6493 * @param right The new value of the view's right property.
6494 * @param bottom The new value of the view's bottom property.
6495 * @param oldLeft The previous value of the view's left property.
6496 * @param oldTop The previous value of the view's top property.
6497 * @param oldRight The previous value of the view's right property.
6498 * @param oldBottom The previous value of the view's bottom property.
6499 */
6500 void onLayoutChange(View v, int left, int top, int right, int bottom,
6501 int oldLeft, int oldTop, int oldRight, int oldBottom);
6502 }
6503
6504 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006505 * This is called during layout when the size of this view has changed. If
6506 * you were just added to the view hierarchy, you're called with the old
6507 * values of 0.
6508 *
6509 * @param w Current width of this view.
6510 * @param h Current height of this view.
6511 * @param oldw Old width of this view.
6512 * @param oldh Old height of this view.
6513 */
6514 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6515 }
6516
6517 /**
6518 * Called by draw to draw the child views. This may be overridden
6519 * by derived classes to gain control just before its children are drawn
6520 * (but after its own view has been drawn).
6521 * @param canvas the canvas on which to draw the view
6522 */
6523 protected void dispatchDraw(Canvas canvas) {
6524 }
6525
6526 /**
6527 * Gets the parent of this view. Note that the parent is a
6528 * ViewParent and not necessarily a View.
6529 *
6530 * @return Parent of this view.
6531 */
6532 public final ViewParent getParent() {
6533 return mParent;
6534 }
6535
6536 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006537 * Set the horizontal scrolled position of your view. This will cause a call to
6538 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6539 * invalidated.
6540 * @param value the x position to scroll to
6541 */
6542 public void setScrollX(int value) {
6543 scrollTo(value, mScrollY);
6544 }
6545
6546 /**
6547 * Set the vertical scrolled position of your view. This will cause a call to
6548 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6549 * invalidated.
6550 * @param value the y position to scroll to
6551 */
6552 public void setScrollY(int value) {
6553 scrollTo(mScrollX, value);
6554 }
6555
6556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006557 * Return the scrolled left position of this view. This is the left edge of
6558 * the displayed part of your view. You do not need to draw any pixels
6559 * farther left, since those are outside of the frame of your view on
6560 * screen.
6561 *
6562 * @return The left edge of the displayed part of your view, in pixels.
6563 */
6564 public final int getScrollX() {
6565 return mScrollX;
6566 }
6567
6568 /**
6569 * Return the scrolled top position of this view. This is the top edge of
6570 * the displayed part of your view. You do not need to draw any pixels above
6571 * it, since those are outside of the frame of your view on screen.
6572 *
6573 * @return The top edge of the displayed part of your view, in pixels.
6574 */
6575 public final int getScrollY() {
6576 return mScrollY;
6577 }
6578
6579 /**
6580 * Return the width of the your view.
6581 *
6582 * @return The width of your view, in pixels.
6583 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006584 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006585 public final int getWidth() {
6586 return mRight - mLeft;
6587 }
6588
6589 /**
6590 * Return the height of your view.
6591 *
6592 * @return The height of your view, in pixels.
6593 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006594 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006595 public final int getHeight() {
6596 return mBottom - mTop;
6597 }
6598
6599 /**
6600 * Return the visible drawing bounds of your view. Fills in the output
6601 * rectangle with the values from getScrollX(), getScrollY(),
6602 * getWidth(), and getHeight().
6603 *
6604 * @param outRect The (scrolled) drawing bounds of the view.
6605 */
6606 public void getDrawingRect(Rect outRect) {
6607 outRect.left = mScrollX;
6608 outRect.top = mScrollY;
6609 outRect.right = mScrollX + (mRight - mLeft);
6610 outRect.bottom = mScrollY + (mBottom - mTop);
6611 }
6612
6613 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006614 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6615 * raw width component (that is the result is masked by
6616 * {@link #MEASURED_SIZE_MASK}).
6617 *
6618 * @return The raw measured width of this view.
6619 */
6620 public final int getMeasuredWidth() {
6621 return mMeasuredWidth & MEASURED_SIZE_MASK;
6622 }
6623
6624 /**
6625 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006626 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006627 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006628 * This should be used during measurement and layout calculations only. Use
6629 * {@link #getWidth()} to see how wide a view is after layout.
6630 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006631 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006632 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006633 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006634 return mMeasuredWidth;
6635 }
6636
6637 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006638 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6639 * raw width component (that is the result is masked by
6640 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006641 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006642 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006643 */
6644 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006645 return mMeasuredHeight & MEASURED_SIZE_MASK;
6646 }
6647
6648 /**
6649 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006650 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006651 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6652 * This should be used during measurement and layout calculations only. Use
6653 * {@link #getHeight()} to see how wide a view is after layout.
6654 *
6655 * @return The measured width of this view as a bit mask.
6656 */
6657 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006658 return mMeasuredHeight;
6659 }
6660
6661 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006662 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6663 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6664 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6665 * and the height component is at the shifted bits
6666 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6667 */
6668 public final int getMeasuredState() {
6669 return (mMeasuredWidth&MEASURED_STATE_MASK)
6670 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6671 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6672 }
6673
6674 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006675 * The transform matrix of this view, which is calculated based on the current
6676 * roation, scale, and pivot properties.
6677 *
6678 * @see #getRotation()
6679 * @see #getScaleX()
6680 * @see #getScaleY()
6681 * @see #getPivotX()
6682 * @see #getPivotY()
6683 * @return The current transform matrix for the view
6684 */
6685 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006686 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006687 return mMatrix;
6688 }
6689
6690 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006691 * Utility function to determine if the value is far enough away from zero to be
6692 * considered non-zero.
6693 * @param value A floating point value to check for zero-ness
6694 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6695 */
6696 private static boolean nonzero(float value) {
6697 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6698 }
6699
6700 /**
Jeff Brown86671742010-09-30 20:00:15 -07006701 * Returns true if the transform matrix is the identity matrix.
6702 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006703 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006704 * @return True if the transform matrix is the identity matrix, false otherwise.
6705 */
Jeff Brown86671742010-09-30 20:00:15 -07006706 final boolean hasIdentityMatrix() {
6707 updateMatrix();
6708 return mMatrixIsIdentity;
6709 }
6710
6711 /**
6712 * Recomputes the transform matrix if necessary.
6713 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006714 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006715 if (mMatrixDirty) {
6716 // transform-related properties have changed since the last time someone
6717 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006718
6719 // Figure out if we need to update the pivot point
6720 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006721 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006722 mPrevWidth = mRight - mLeft;
6723 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006724 mPivotX = mPrevWidth / 2f;
6725 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006726 }
6727 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006728 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006729 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6730 mMatrix.setTranslate(mTranslationX, mTranslationY);
6731 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6732 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6733 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006734 if (mCamera == null) {
6735 mCamera = new Camera();
6736 matrix3D = new Matrix();
6737 }
6738 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006739 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006740 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006741 mCamera.getMatrix(matrix3D);
6742 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006743 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006744 mMatrix.postConcat(matrix3D);
6745 mCamera.restore();
6746 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006747 mMatrixDirty = false;
6748 mMatrixIsIdentity = mMatrix.isIdentity();
6749 mInverseMatrixDirty = true;
6750 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006751 }
6752
6753 /**
6754 * Utility method to retrieve the inverse of the current mMatrix property.
6755 * We cache the matrix to avoid recalculating it when transform properties
6756 * have not changed.
6757 *
6758 * @return The inverse of the current matrix of this view.
6759 */
Jeff Brown86671742010-09-30 20:00:15 -07006760 final Matrix getInverseMatrix() {
6761 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006762 if (mInverseMatrixDirty) {
6763 if (mInverseMatrix == null) {
6764 mInverseMatrix = new Matrix();
6765 }
6766 mMatrix.invert(mInverseMatrix);
6767 mInverseMatrixDirty = false;
6768 }
6769 return mInverseMatrix;
6770 }
6771
6772 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006773 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6774 * views are drawn) from the camera to this view. The camera's distance
6775 * affects 3D transformations, for instance rotations around the X and Y
6776 * axis. If the rotationX or rotationY properties are changed and this view is
6777 * large (more than half the size of the screen), it is recommended to always
6778 * use a camera distance that's greater than the height (X axis rotation) or
6779 * the width (Y axis rotation) of this view.</p>
6780 *
6781 * <p>The distance of the camera from the view plane can have an affect on the
6782 * perspective distortion of the view when it is rotated around the x or y axis.
6783 * For example, a large distance will result in a large viewing angle, and there
6784 * will not be much perspective distortion of the view as it rotates. A short
6785 * distance may cause much more perspective distortion upon rotation, and can
6786 * also result in some drawing artifacts if the rotated view ends up partially
6787 * behind the camera (which is why the recommendation is to use a distance at
6788 * least as far as the size of the view, if the view is to be rotated.)</p>
6789 *
6790 * <p>The distance is expressed in "depth pixels." The default distance depends
6791 * on the screen density. For instance, on a medium density display, the
6792 * default distance is 1280. On a high density display, the default distance
6793 * is 1920.</p>
6794 *
6795 * <p>If you want to specify a distance that leads to visually consistent
6796 * results across various densities, use the following formula:</p>
6797 * <pre>
6798 * float scale = context.getResources().getDisplayMetrics().density;
6799 * view.setCameraDistance(distance * scale);
6800 * </pre>
6801 *
6802 * <p>The density scale factor of a high density display is 1.5,
6803 * and 1920 = 1280 * 1.5.</p>
6804 *
6805 * @param distance The distance in "depth pixels", if negative the opposite
6806 * value is used
6807 *
6808 * @see #setRotationX(float)
6809 * @see #setRotationY(float)
6810 */
6811 public void setCameraDistance(float distance) {
6812 invalidateParentCaches();
6813 invalidate(false);
6814
6815 final float dpi = mResources.getDisplayMetrics().densityDpi;
6816 if (mCamera == null) {
6817 mCamera = new Camera();
6818 matrix3D = new Matrix();
6819 }
6820
6821 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6822 mMatrixDirty = true;
6823
6824 invalidate(false);
6825 }
6826
6827 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006828 * The degrees that the view is rotated around the pivot point.
6829 *
Romain Guya5364ee2011-02-24 14:46:04 -08006830 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006831 * @see #getPivotX()
6832 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006833 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006834 * @return The degrees of rotation.
6835 */
6836 public float getRotation() {
6837 return mRotation;
6838 }
6839
6840 /**
Chet Haase897247b2010-09-09 14:54:47 -07006841 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6842 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006843 *
6844 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006845 *
6846 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006847 * @see #getPivotX()
6848 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006849 * @see #setRotationX(float)
6850 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006851 *
6852 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006853 */
6854 public void setRotation(float rotation) {
6855 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006856 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006857 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006858 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006859 mRotation = rotation;
6860 mMatrixDirty = true;
6861 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006862 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006863 }
6864 }
6865
6866 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006867 * The degrees that the view is rotated around the vertical axis through the pivot point.
6868 *
6869 * @see #getPivotX()
6870 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006871 * @see #setRotationY(float)
6872 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006873 * @return The degrees of Y rotation.
6874 */
6875 public float getRotationY() {
6876 return mRotationY;
6877 }
6878
6879 /**
Chet Haase897247b2010-09-09 14:54:47 -07006880 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6881 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6882 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006883 *
6884 * When rotating large views, it is recommended to adjust the camera distance
6885 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006886 *
6887 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006888 *
6889 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006890 * @see #getPivotX()
6891 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006892 * @see #setRotation(float)
6893 * @see #setRotationX(float)
6894 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006895 *
6896 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006897 */
6898 public void setRotationY(float rotationY) {
6899 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006900 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006901 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006902 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006903 mRotationY = rotationY;
6904 mMatrixDirty = true;
6905 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006906 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006907 }
6908 }
6909
6910 /**
6911 * The degrees that the view is rotated around the horizontal axis through the pivot point.
6912 *
6913 * @see #getPivotX()
6914 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006915 * @see #setRotationX(float)
6916 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006917 * @return The degrees of X rotation.
6918 */
6919 public float getRotationX() {
6920 return mRotationX;
6921 }
6922
6923 /**
Chet Haase897247b2010-09-09 14:54:47 -07006924 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
6925 * Increasing values result in clockwise rotation from the viewpoint of looking down the
6926 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006927 *
6928 * When rotating large views, it is recommended to adjust the camera distance
6929 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006930 *
6931 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006932 *
6933 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07006934 * @see #getPivotX()
6935 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006936 * @see #setRotation(float)
6937 * @see #setRotationY(float)
6938 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006939 *
6940 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07006941 */
6942 public void setRotationX(float rotationX) {
6943 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006944 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006945 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006946 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006947 mRotationX = rotationX;
6948 mMatrixDirty = true;
6949 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006950 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006951 }
6952 }
6953
6954 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006955 * The amount that the view is scaled in x around the pivot point, as a proportion of
6956 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
6957 *
Joe Onorato93162322010-09-16 15:42:01 -04006958 * <p>By default, this is 1.0f.
6959 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006960 * @see #getPivotX()
6961 * @see #getPivotY()
6962 * @return The scaling factor.
6963 */
6964 public float getScaleX() {
6965 return mScaleX;
6966 }
6967
6968 /**
6969 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
6970 * the view's unscaled width. A value of 1 means that no scaling is applied.
6971 *
6972 * @param scaleX The scaling factor.
6973 * @see #getPivotX()
6974 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08006975 *
6976 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07006977 */
6978 public void setScaleX(float scaleX) {
6979 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006980 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006981 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006982 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006983 mScaleX = scaleX;
6984 mMatrixDirty = true;
6985 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006986 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006987 }
6988 }
6989
6990 /**
6991 * The amount that the view is scaled in y around the pivot point, as a proportion of
6992 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
6993 *
Joe Onorato93162322010-09-16 15:42:01 -04006994 * <p>By default, this is 1.0f.
6995 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006996 * @see #getPivotX()
6997 * @see #getPivotY()
6998 * @return The scaling factor.
6999 */
7000 public float getScaleY() {
7001 return mScaleY;
7002 }
7003
7004 /**
7005 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7006 * the view's unscaled width. A value of 1 means that no scaling is applied.
7007 *
7008 * @param scaleY The scaling factor.
7009 * @see #getPivotX()
7010 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007011 *
7012 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007013 */
7014 public void setScaleY(float scaleY) {
7015 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007016 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007017 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007018 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007019 mScaleY = scaleY;
7020 mMatrixDirty = true;
7021 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007022 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007023 }
7024 }
7025
7026 /**
7027 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7028 * and {@link #setScaleX(float) scaled}.
7029 *
7030 * @see #getRotation()
7031 * @see #getScaleX()
7032 * @see #getScaleY()
7033 * @see #getPivotY()
7034 * @return The x location of the pivot point.
7035 */
7036 public float getPivotX() {
7037 return mPivotX;
7038 }
7039
7040 /**
7041 * Sets the x location of the point around which the view is
7042 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007043 * By default, the pivot point is centered on the object.
7044 * Setting this property disables this behavior and causes the view to use only the
7045 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007046 *
7047 * @param pivotX The x location of the pivot point.
7048 * @see #getRotation()
7049 * @see #getScaleX()
7050 * @see #getScaleY()
7051 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007052 *
7053 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007054 */
7055 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007056 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007057 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007058 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007059 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007060 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007061 mPivotX = pivotX;
7062 mMatrixDirty = true;
7063 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007064 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007065 }
7066 }
7067
7068 /**
7069 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7070 * and {@link #setScaleY(float) scaled}.
7071 *
7072 * @see #getRotation()
7073 * @see #getScaleX()
7074 * @see #getScaleY()
7075 * @see #getPivotY()
7076 * @return The y location of the pivot point.
7077 */
7078 public float getPivotY() {
7079 return mPivotY;
7080 }
7081
7082 /**
7083 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007084 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7085 * Setting this property disables this behavior and causes the view to use only the
7086 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007087 *
7088 * @param pivotY The y location of the pivot point.
7089 * @see #getRotation()
7090 * @see #getScaleX()
7091 * @see #getScaleY()
7092 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007093 *
7094 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007095 */
7096 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007097 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007098 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007099 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007100 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007101 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007102 mPivotY = pivotY;
7103 mMatrixDirty = true;
7104 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007105 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007106 }
7107 }
7108
7109 /**
7110 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7111 * completely transparent and 1 means the view is completely opaque.
7112 *
Joe Onorato93162322010-09-16 15:42:01 -04007113 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007114 * @return The opacity of the view.
7115 */
7116 public float getAlpha() {
7117 return mAlpha;
7118 }
7119
7120 /**
Romain Guy171c5922011-01-06 10:04:23 -08007121 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7122 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007123 *
Romain Guy171c5922011-01-06 10:04:23 -08007124 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7125 * responsible for applying the opacity itself. Otherwise, calling this method is
7126 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007127 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007128 *
7129 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007130 *
Joe Malin32736f02011-01-19 16:14:20 -08007131 * @see #setLayerType(int, android.graphics.Paint)
7132 *
Chet Haase73066682010-11-29 15:55:32 -08007133 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007134 */
7135 public void setAlpha(float alpha) {
7136 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007137 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007138 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007139 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007140 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007141 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007142 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007143 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007144 invalidate(false);
7145 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007146 }
7147
7148 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007149 * Faster version of setAlpha() which performs the same steps except there are
7150 * no calls to invalidate(). The caller of this function should perform proper invalidation
7151 * on the parent and this object. The return value indicates whether the subclass handles
7152 * alpha (the return value for onSetAlpha()).
7153 *
7154 * @param alpha The new value for the alpha property
7155 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7156 */
7157 boolean setAlphaNoInvalidation(float alpha) {
7158 mAlpha = alpha;
7159 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7160 if (subclassHandlesAlpha) {
7161 mPrivateFlags |= ALPHA_SET;
7162 } else {
7163 mPrivateFlags &= ~ALPHA_SET;
7164 }
7165 return subclassHandlesAlpha;
7166 }
7167
7168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007169 * Top position of this view relative to its parent.
7170 *
7171 * @return The top of this view, in pixels.
7172 */
7173 @ViewDebug.CapturedViewProperty
7174 public final int getTop() {
7175 return mTop;
7176 }
7177
7178 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007179 * Sets the top position of this view relative to its parent. This method is meant to be called
7180 * by the layout system and should not generally be called otherwise, because the property
7181 * may be changed at any time by the layout.
7182 *
7183 * @param top The top of this view, in pixels.
7184 */
7185 public final void setTop(int top) {
7186 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007187 updateMatrix();
7188 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007189 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007190 int minTop;
7191 int yLoc;
7192 if (top < mTop) {
7193 minTop = top;
7194 yLoc = top - mTop;
7195 } else {
7196 minTop = mTop;
7197 yLoc = 0;
7198 }
Chet Haasee9140a72011-02-16 16:23:29 -08007199 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007200 }
7201 } else {
7202 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007203 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007204 }
7205
Chet Haaseed032702010-10-01 14:05:54 -07007206 int width = mRight - mLeft;
7207 int oldHeight = mBottom - mTop;
7208
Chet Haase21cd1382010-09-01 17:42:29 -07007209 mTop = top;
7210
Chet Haaseed032702010-10-01 14:05:54 -07007211 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7212
Chet Haase21cd1382010-09-01 17:42:29 -07007213 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007214 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7215 // A change in dimension means an auto-centered pivot point changes, too
7216 mMatrixDirty = true;
7217 }
Chet Haase21cd1382010-09-01 17:42:29 -07007218 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007219 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007220 }
Chet Haase55dbb652010-12-21 20:15:08 -08007221 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007222 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007223 }
7224 }
7225
7226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007227 * Bottom position of this view relative to its parent.
7228 *
7229 * @return The bottom of this view, in pixels.
7230 */
7231 @ViewDebug.CapturedViewProperty
7232 public final int getBottom() {
7233 return mBottom;
7234 }
7235
7236 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007237 * True if this view has changed since the last time being drawn.
7238 *
7239 * @return The dirty state of this view.
7240 */
7241 public boolean isDirty() {
7242 return (mPrivateFlags & DIRTY_MASK) != 0;
7243 }
7244
7245 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007246 * Sets the bottom position of this view relative to its parent. This method is meant to be
7247 * called by the layout system and should not generally be called otherwise, because the
7248 * property may be changed at any time by the layout.
7249 *
7250 * @param bottom The bottom of this view, in pixels.
7251 */
7252 public final void setBottom(int bottom) {
7253 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007254 updateMatrix();
7255 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007256 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007257 int maxBottom;
7258 if (bottom < mBottom) {
7259 maxBottom = mBottom;
7260 } else {
7261 maxBottom = bottom;
7262 }
Chet Haasee9140a72011-02-16 16:23:29 -08007263 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007264 }
7265 } else {
7266 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007267 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007268 }
7269
Chet Haaseed032702010-10-01 14:05:54 -07007270 int width = mRight - mLeft;
7271 int oldHeight = mBottom - mTop;
7272
Chet Haase21cd1382010-09-01 17:42:29 -07007273 mBottom = bottom;
7274
Chet Haaseed032702010-10-01 14:05:54 -07007275 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7276
Chet Haase21cd1382010-09-01 17:42:29 -07007277 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007278 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7279 // A change in dimension means an auto-centered pivot point changes, too
7280 mMatrixDirty = true;
7281 }
Chet Haase21cd1382010-09-01 17:42:29 -07007282 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007283 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007284 }
Chet Haase55dbb652010-12-21 20:15:08 -08007285 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007286 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007287 }
7288 }
7289
7290 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007291 * Left position of this view relative to its parent.
7292 *
7293 * @return The left edge of this view, in pixels.
7294 */
7295 @ViewDebug.CapturedViewProperty
7296 public final int getLeft() {
7297 return mLeft;
7298 }
7299
7300 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007301 * Sets the left position of this view relative to its parent. This method is meant to be called
7302 * by the layout system and should not generally be called otherwise, because the property
7303 * may be changed at any time by the layout.
7304 *
7305 * @param left The bottom of this view, in pixels.
7306 */
7307 public final void setLeft(int left) {
7308 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007309 updateMatrix();
7310 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007311 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007312 int minLeft;
7313 int xLoc;
7314 if (left < mLeft) {
7315 minLeft = left;
7316 xLoc = left - mLeft;
7317 } else {
7318 minLeft = mLeft;
7319 xLoc = 0;
7320 }
Chet Haasee9140a72011-02-16 16:23:29 -08007321 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007322 }
7323 } else {
7324 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007325 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007326 }
7327
Chet Haaseed032702010-10-01 14:05:54 -07007328 int oldWidth = mRight - mLeft;
7329 int height = mBottom - mTop;
7330
Chet Haase21cd1382010-09-01 17:42:29 -07007331 mLeft = left;
7332
Chet Haaseed032702010-10-01 14:05:54 -07007333 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7334
Chet Haase21cd1382010-09-01 17:42:29 -07007335 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007336 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7337 // A change in dimension means an auto-centered pivot point changes, too
7338 mMatrixDirty = true;
7339 }
Chet Haase21cd1382010-09-01 17:42:29 -07007340 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007341 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007342 }
Chet Haase55dbb652010-12-21 20:15:08 -08007343 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007344 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007345 }
7346 }
7347
7348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007349 * Right position of this view relative to its parent.
7350 *
7351 * @return The right edge of this view, in pixels.
7352 */
7353 @ViewDebug.CapturedViewProperty
7354 public final int getRight() {
7355 return mRight;
7356 }
7357
7358 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007359 * Sets the right position of this view relative to its parent. This method is meant to be called
7360 * by the layout system and should not generally be called otherwise, because the property
7361 * may be changed at any time by the layout.
7362 *
7363 * @param right The bottom of this view, in pixels.
7364 */
7365 public final void setRight(int right) {
7366 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007367 updateMatrix();
7368 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007369 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007370 int maxRight;
7371 if (right < mRight) {
7372 maxRight = mRight;
7373 } else {
7374 maxRight = right;
7375 }
Chet Haasee9140a72011-02-16 16:23:29 -08007376 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007377 }
7378 } else {
7379 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007380 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007381 }
7382
Chet Haaseed032702010-10-01 14:05:54 -07007383 int oldWidth = mRight - mLeft;
7384 int height = mBottom - mTop;
7385
Chet Haase21cd1382010-09-01 17:42:29 -07007386 mRight = right;
7387
Chet Haaseed032702010-10-01 14:05:54 -07007388 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7389
Chet Haase21cd1382010-09-01 17:42:29 -07007390 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007391 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7392 // A change in dimension means an auto-centered pivot point changes, too
7393 mMatrixDirty = true;
7394 }
Chet Haase21cd1382010-09-01 17:42:29 -07007395 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007396 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007397 }
Chet Haase55dbb652010-12-21 20:15:08 -08007398 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007399 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007400 }
7401 }
7402
7403 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007404 * The visual x position of this view, in pixels. This is equivalent to the
7405 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007406 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007407 *
Chet Haasedf030d22010-07-30 17:22:38 -07007408 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007409 */
Chet Haasedf030d22010-07-30 17:22:38 -07007410 public float getX() {
7411 return mLeft + mTranslationX;
7412 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007413
Chet Haasedf030d22010-07-30 17:22:38 -07007414 /**
7415 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7416 * {@link #setTranslationX(float) translationX} property to be the difference between
7417 * the x value passed in and the current {@link #getLeft() left} property.
7418 *
7419 * @param x The visual x position of this view, in pixels.
7420 */
7421 public void setX(float x) {
7422 setTranslationX(x - mLeft);
7423 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007424
Chet Haasedf030d22010-07-30 17:22:38 -07007425 /**
7426 * The visual y position of this view, in pixels. This is equivalent to the
7427 * {@link #setTranslationY(float) translationY} property plus the current
7428 * {@link #getTop() top} property.
7429 *
7430 * @return The visual y position of this view, in pixels.
7431 */
7432 public float getY() {
7433 return mTop + mTranslationY;
7434 }
7435
7436 /**
7437 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7438 * {@link #setTranslationY(float) translationY} property to be the difference between
7439 * the y value passed in and the current {@link #getTop() top} property.
7440 *
7441 * @param y The visual y position of this view, in pixels.
7442 */
7443 public void setY(float y) {
7444 setTranslationY(y - mTop);
7445 }
7446
7447
7448 /**
7449 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7450 * This position is post-layout, in addition to wherever the object's
7451 * layout placed it.
7452 *
7453 * @return The horizontal position of this view relative to its left position, in pixels.
7454 */
7455 public float getTranslationX() {
7456 return mTranslationX;
7457 }
7458
7459 /**
7460 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7461 * This effectively positions the object post-layout, in addition to wherever the object's
7462 * layout placed it.
7463 *
7464 * @param translationX The horizontal position of this view relative to its left position,
7465 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007466 *
7467 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007468 */
7469 public void setTranslationX(float translationX) {
7470 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007471 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007472 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007473 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007474 mTranslationX = translationX;
7475 mMatrixDirty = true;
7476 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007477 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007478 }
7479 }
7480
7481 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007482 * The horizontal location of this view relative to its {@link #getTop() top} position.
7483 * This position is post-layout, in addition to wherever the object's
7484 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007485 *
Chet Haasedf030d22010-07-30 17:22:38 -07007486 * @return The vertical position of this view relative to its top position,
7487 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007488 */
Chet Haasedf030d22010-07-30 17:22:38 -07007489 public float getTranslationY() {
7490 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007491 }
7492
7493 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007494 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7495 * This effectively positions the object post-layout, in addition to wherever the object's
7496 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007497 *
Chet Haasedf030d22010-07-30 17:22:38 -07007498 * @param translationY The vertical position of this view relative to its top position,
7499 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007500 *
7501 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 */
Chet Haasedf030d22010-07-30 17:22:38 -07007503 public void setTranslationY(float translationY) {
7504 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007505 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007506 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007507 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007508 mTranslationY = translationY;
7509 mMatrixDirty = true;
7510 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007511 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007512 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007513 }
7514
7515 /**
Romain Guyda489792011-02-03 01:05:15 -08007516 * @hide
7517 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007518 public void setFastTranslationX(float x) {
7519 mTranslationX = x;
7520 mMatrixDirty = true;
7521 }
7522
7523 /**
7524 * @hide
7525 */
7526 public void setFastTranslationY(float y) {
7527 mTranslationY = y;
7528 mMatrixDirty = true;
7529 }
7530
7531 /**
7532 * @hide
7533 */
Romain Guyda489792011-02-03 01:05:15 -08007534 public void setFastX(float x) {
7535 mTranslationX = x - mLeft;
7536 mMatrixDirty = true;
7537 }
7538
7539 /**
7540 * @hide
7541 */
7542 public void setFastY(float y) {
7543 mTranslationY = y - mTop;
7544 mMatrixDirty = true;
7545 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007546
Romain Guyda489792011-02-03 01:05:15 -08007547 /**
7548 * @hide
7549 */
7550 public void setFastScaleX(float x) {
7551 mScaleX = x;
7552 mMatrixDirty = true;
7553 }
7554
7555 /**
7556 * @hide
7557 */
7558 public void setFastScaleY(float y) {
7559 mScaleY = y;
7560 mMatrixDirty = true;
7561 }
7562
7563 /**
7564 * @hide
7565 */
7566 public void setFastAlpha(float alpha) {
7567 mAlpha = alpha;
7568 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007569
Romain Guyda489792011-02-03 01:05:15 -08007570 /**
7571 * @hide
7572 */
7573 public void setFastRotationY(float y) {
7574 mRotationY = y;
7575 mMatrixDirty = true;
7576 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007577
Romain Guyda489792011-02-03 01:05:15 -08007578 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007579 * Hit rectangle in parent's coordinates
7580 *
7581 * @param outRect The hit rectangle of the view.
7582 */
7583 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007584 updateMatrix();
7585 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007586 outRect.set(mLeft, mTop, mRight, mBottom);
7587 } else {
7588 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007589 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007590 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007591 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7592 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007593 }
7594 }
7595
7596 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007597 * Determines whether the given point, in local coordinates is inside the view.
7598 */
7599 /*package*/ final boolean pointInView(float localX, float localY) {
7600 return localX >= 0 && localX < (mRight - mLeft)
7601 && localY >= 0 && localY < (mBottom - mTop);
7602 }
7603
7604 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007605 * Utility method to determine whether the given point, in local coordinates,
7606 * is inside the view, where the area of the view is expanded by the slop factor.
7607 * This method is called while processing touch-move events to determine if the event
7608 * is still within the view.
7609 */
7610 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007611 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007612 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007613 }
7614
7615 /**
7616 * When a view has focus and the user navigates away from it, the next view is searched for
7617 * starting from the rectangle filled in by this method.
7618 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007619 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7620 * of the view. However, if your view maintains some idea of internal selection,
7621 * such as a cursor, or a selected row or column, you should override this method and
7622 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007623 *
7624 * @param r The rectangle to fill in, in this view's coordinates.
7625 */
7626 public void getFocusedRect(Rect r) {
7627 getDrawingRect(r);
7628 }
7629
7630 /**
7631 * If some part of this view is not clipped by any of its parents, then
7632 * return that area in r in global (root) coordinates. To convert r to local
7633 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7634 * -globalOffset.y)) If the view is completely clipped or translated out,
7635 * return false.
7636 *
7637 * @param r If true is returned, r holds the global coordinates of the
7638 * visible portion of this view.
7639 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7640 * between this view and its root. globalOffet may be null.
7641 * @return true if r is non-empty (i.e. part of the view is visible at the
7642 * root level.
7643 */
7644 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7645 int width = mRight - mLeft;
7646 int height = mBottom - mTop;
7647 if (width > 0 && height > 0) {
7648 r.set(0, 0, width, height);
7649 if (globalOffset != null) {
7650 globalOffset.set(-mScrollX, -mScrollY);
7651 }
7652 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7653 }
7654 return false;
7655 }
7656
7657 public final boolean getGlobalVisibleRect(Rect r) {
7658 return getGlobalVisibleRect(r, null);
7659 }
7660
7661 public final boolean getLocalVisibleRect(Rect r) {
7662 Point offset = new Point();
7663 if (getGlobalVisibleRect(r, offset)) {
7664 r.offset(-offset.x, -offset.y); // make r local
7665 return true;
7666 }
7667 return false;
7668 }
7669
7670 /**
7671 * Offset this view's vertical location by the specified number of pixels.
7672 *
7673 * @param offset the number of pixels to offset the view by
7674 */
7675 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007676 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007677 updateMatrix();
7678 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 final ViewParent p = mParent;
7680 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007681 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007682 int minTop;
7683 int maxBottom;
7684 int yLoc;
7685 if (offset < 0) {
7686 minTop = mTop + offset;
7687 maxBottom = mBottom;
7688 yLoc = offset;
7689 } else {
7690 minTop = mTop;
7691 maxBottom = mBottom + offset;
7692 yLoc = 0;
7693 }
7694 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7695 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007696 }
7697 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007698 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007699 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007700
Chet Haasec3aa3612010-06-17 08:50:37 -07007701 mTop += offset;
7702 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007703
Chet Haasec3aa3612010-06-17 08:50:37 -07007704 if (!mMatrixIsIdentity) {
7705 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007706 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007707 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007708 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007709 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007710 }
7711
7712 /**
7713 * Offset this view's horizontal location by the specified amount of pixels.
7714 *
7715 * @param offset the numer of pixels to offset the view by
7716 */
7717 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007718 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007719 updateMatrix();
7720 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007721 final ViewParent p = mParent;
7722 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007723 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007724 int minLeft;
7725 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007726 if (offset < 0) {
7727 minLeft = mLeft + offset;
7728 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007729 } else {
7730 minLeft = mLeft;
7731 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007732 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007733 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007734 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007735 }
7736 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007737 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007738 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007739
Chet Haasec3aa3612010-06-17 08:50:37 -07007740 mLeft += offset;
7741 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007742
Chet Haasec3aa3612010-06-17 08:50:37 -07007743 if (!mMatrixIsIdentity) {
7744 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007745 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007746 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007747 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007749 }
7750
7751 /**
7752 * Get the LayoutParams associated with this view. All views should have
7753 * layout parameters. These supply parameters to the <i>parent</i> of this
7754 * view specifying how it should be arranged. There are many subclasses of
7755 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7756 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007757 *
7758 * This method may return null if this View is not attached to a parent
7759 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7760 * was not invoked successfully. When a View is attached to a parent
7761 * ViewGroup, this method must not return null.
7762 *
7763 * @return The LayoutParams associated with this view, or null if no
7764 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007765 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007766 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007767 public ViewGroup.LayoutParams getLayoutParams() {
7768 return mLayoutParams;
7769 }
7770
7771 /**
7772 * Set the layout parameters associated with this view. These supply
7773 * parameters to the <i>parent</i> of this view specifying how it should be
7774 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7775 * correspond to the different subclasses of ViewGroup that are responsible
7776 * for arranging their children.
7777 *
Romain Guy01c174b2011-02-22 11:51:06 -08007778 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007779 */
7780 public void setLayoutParams(ViewGroup.LayoutParams params) {
7781 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007782 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007783 }
7784 mLayoutParams = params;
7785 requestLayout();
7786 }
7787
7788 /**
7789 * Set the scrolled position of your view. This will cause a call to
7790 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7791 * invalidated.
7792 * @param x the x position to scroll to
7793 * @param y the y position to scroll to
7794 */
7795 public void scrollTo(int x, int y) {
7796 if (mScrollX != x || mScrollY != y) {
7797 int oldX = mScrollX;
7798 int oldY = mScrollY;
7799 mScrollX = x;
7800 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007801 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007802 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007803 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007804 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007806 }
7807 }
7808
7809 /**
7810 * Move the scrolled position of your view. This will cause a call to
7811 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7812 * invalidated.
7813 * @param x the amount of pixels to scroll by horizontally
7814 * @param y the amount of pixels to scroll by vertically
7815 */
7816 public void scrollBy(int x, int y) {
7817 scrollTo(mScrollX + x, mScrollY + y);
7818 }
7819
7820 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007821 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7822 * animation to fade the scrollbars out after a default delay. If a subclass
7823 * provides animated scrolling, the start delay should equal the duration
7824 * of the scrolling animation.</p>
7825 *
7826 * <p>The animation starts only if at least one of the scrollbars is
7827 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7828 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7829 * this method returns true, and false otherwise. If the animation is
7830 * started, this method calls {@link #invalidate()}; in that case the
7831 * caller should not call {@link #invalidate()}.</p>
7832 *
7833 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007834 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007835 *
7836 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7837 * and {@link #scrollTo(int, int)}.</p>
7838 *
7839 * @return true if the animation is played, false otherwise
7840 *
7841 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007842 * @see #scrollBy(int, int)
7843 * @see #scrollTo(int, int)
7844 * @see #isHorizontalScrollBarEnabled()
7845 * @see #isVerticalScrollBarEnabled()
7846 * @see #setHorizontalScrollBarEnabled(boolean)
7847 * @see #setVerticalScrollBarEnabled(boolean)
7848 */
7849 protected boolean awakenScrollBars() {
7850 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007851 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007852 }
7853
7854 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007855 * Trigger the scrollbars to draw.
7856 * This method differs from awakenScrollBars() only in its default duration.
7857 * initialAwakenScrollBars() will show the scroll bars for longer than
7858 * usual to give the user more of a chance to notice them.
7859 *
7860 * @return true if the animation is played, false otherwise.
7861 */
7862 private boolean initialAwakenScrollBars() {
7863 return mScrollCache != null &&
7864 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7865 }
7866
7867 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007868 * <p>
7869 * Trigger the scrollbars to draw. When invoked this method starts an
7870 * animation to fade the scrollbars out after a fixed delay. If a subclass
7871 * provides animated scrolling, the start delay should equal the duration of
7872 * the scrolling animation.
7873 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007874 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007875 * <p>
7876 * The animation starts only if at least one of the scrollbars is enabled,
7877 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7878 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7879 * this method returns true, and false otherwise. If the animation is
7880 * started, this method calls {@link #invalidate()}; in that case the caller
7881 * should not call {@link #invalidate()}.
7882 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007883 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007884 * <p>
7885 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007886 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007887 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007888 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007889 * @param startDelay the delay, in milliseconds, after which the animation
7890 * should start; when the delay is 0, the animation starts
7891 * immediately
7892 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007893 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007894 * @see #scrollBy(int, int)
7895 * @see #scrollTo(int, int)
7896 * @see #isHorizontalScrollBarEnabled()
7897 * @see #isVerticalScrollBarEnabled()
7898 * @see #setHorizontalScrollBarEnabled(boolean)
7899 * @see #setVerticalScrollBarEnabled(boolean)
7900 */
7901 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007902 return awakenScrollBars(startDelay, true);
7903 }
Joe Malin32736f02011-01-19 16:14:20 -08007904
Mike Cleron290947b2009-09-29 18:34:32 -07007905 /**
7906 * <p>
7907 * Trigger the scrollbars to draw. When invoked this method starts an
7908 * animation to fade the scrollbars out after a fixed delay. If a subclass
7909 * provides animated scrolling, the start delay should equal the duration of
7910 * the scrolling animation.
7911 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007912 *
Mike Cleron290947b2009-09-29 18:34:32 -07007913 * <p>
7914 * The animation starts only if at least one of the scrollbars is enabled,
7915 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7916 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7917 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08007918 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07007919 * is set to true; in that case the caller
7920 * should not call {@link #invalidate()}.
7921 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007922 *
Mike Cleron290947b2009-09-29 18:34:32 -07007923 * <p>
7924 * This method should be invoked everytime a subclass directly updates the
7925 * scroll parameters.
7926 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007927 *
Mike Cleron290947b2009-09-29 18:34:32 -07007928 * @param startDelay the delay, in milliseconds, after which the animation
7929 * should start; when the delay is 0, the animation starts
7930 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08007931 *
Mike Cleron290947b2009-09-29 18:34:32 -07007932 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08007933 *
Mike Cleron290947b2009-09-29 18:34:32 -07007934 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007935 *
Mike Cleron290947b2009-09-29 18:34:32 -07007936 * @see #scrollBy(int, int)
7937 * @see #scrollTo(int, int)
7938 * @see #isHorizontalScrollBarEnabled()
7939 * @see #isVerticalScrollBarEnabled()
7940 * @see #setHorizontalScrollBarEnabled(boolean)
7941 * @see #setVerticalScrollBarEnabled(boolean)
7942 */
7943 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07007944 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08007945
Mike Cleronf116bf82009-09-27 19:14:12 -07007946 if (scrollCache == null || !scrollCache.fadeScrollBars) {
7947 return false;
7948 }
7949
7950 if (scrollCache.scrollBar == null) {
7951 scrollCache.scrollBar = new ScrollBarDrawable();
7952 }
7953
7954 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
7955
Mike Cleron290947b2009-09-29 18:34:32 -07007956 if (invalidate) {
7957 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08007958 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07007959 }
Mike Cleronf116bf82009-09-27 19:14:12 -07007960
7961 if (scrollCache.state == ScrollabilityCache.OFF) {
7962 // FIXME: this is copied from WindowManagerService.
7963 // We should get this value from the system when it
7964 // is possible to do so.
7965 final int KEY_REPEAT_FIRST_DELAY = 750;
7966 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
7967 }
7968
7969 // Tell mScrollCache when we should start fading. This may
7970 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07007971 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07007972 scrollCache.fadeStartTime = fadeStartTime;
7973 scrollCache.state = ScrollabilityCache.ON;
7974
7975 // Schedule our fader to run, unscheduling any old ones first
7976 if (mAttachInfo != null) {
7977 mAttachInfo.mHandler.removeCallbacks(scrollCache);
7978 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
7979 }
7980
7981 return true;
7982 }
7983
7984 return false;
7985 }
7986
7987 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007988 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07007989 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
7990 * in the future. This must be called from a UI thread. To call from a non-UI
7991 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007992 *
7993 * WARNING: This method is destructive to dirty.
7994 * @param dirty the rectangle representing the bounds of the dirty region
7995 */
7996 public void invalidate(Rect dirty) {
7997 if (ViewDebug.TRACE_HIERARCHY) {
7998 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
7999 }
8000
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008001 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008002 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8003 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008004 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008005 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008006 final ViewParent p = mParent;
8007 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008008 //noinspection PointlessBooleanExpression,ConstantConditions
8009 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8010 if (p != null && ai != null && ai.mHardwareAccelerated) {
8011 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008012 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008013 p.invalidateChild(this, null);
8014 return;
8015 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008017 if (p != null && ai != null) {
8018 final int scrollX = mScrollX;
8019 final int scrollY = mScrollY;
8020 final Rect r = ai.mTmpInvalRect;
8021 r.set(dirty.left - scrollX, dirty.top - scrollY,
8022 dirty.right - scrollX, dirty.bottom - scrollY);
8023 mParent.invalidateChild(this, r);
8024 }
8025 }
8026 }
8027
8028 /**
8029 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8030 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008031 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8032 * will be called at some point in the future. This must be called from
8033 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008034 * @param l the left position of the dirty region
8035 * @param t the top position of the dirty region
8036 * @param r the right position of the dirty region
8037 * @param b the bottom position of the dirty region
8038 */
8039 public void invalidate(int l, int t, int r, int b) {
8040 if (ViewDebug.TRACE_HIERARCHY) {
8041 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8042 }
8043
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008044 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008045 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8046 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008047 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008048 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008049 final ViewParent p = mParent;
8050 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008051 //noinspection PointlessBooleanExpression,ConstantConditions
8052 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8053 if (p != null && ai != null && ai.mHardwareAccelerated) {
8054 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008055 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008056 p.invalidateChild(this, null);
8057 return;
8058 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008060 if (p != null && ai != null && l < r && t < b) {
8061 final int scrollX = mScrollX;
8062 final int scrollY = mScrollY;
8063 final Rect tmpr = ai.mTmpInvalRect;
8064 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8065 p.invalidateChild(this, tmpr);
8066 }
8067 }
8068 }
8069
8070 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008071 * Invalidate the whole view. If the view is visible,
8072 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8073 * the future. This must be called from a UI thread. To call from a non-UI thread,
8074 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008075 */
8076 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008077 invalidate(true);
8078 }
Joe Malin32736f02011-01-19 16:14:20 -08008079
Chet Haaseed032702010-10-01 14:05:54 -07008080 /**
8081 * This is where the invalidate() work actually happens. A full invalidate()
8082 * causes the drawing cache to be invalidated, but this function can be called with
8083 * invalidateCache set to false to skip that invalidation step for cases that do not
8084 * need it (for example, a component that remains at the same dimensions with the same
8085 * content).
8086 *
8087 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8088 * well. This is usually true for a full invalidate, but may be set to false if the
8089 * View's contents or dimensions have not changed.
8090 */
Romain Guy849d0a32011-02-01 17:20:48 -08008091 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008092 if (ViewDebug.TRACE_HIERARCHY) {
8093 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8094 }
8095
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008096 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008097 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008098 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8099 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008100 mPrivateFlags &= ~DRAWN;
8101 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008102 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008103 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8104 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008105 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008106 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008107 //noinspection PointlessBooleanExpression,ConstantConditions
8108 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8109 if (p != null && ai != null && ai.mHardwareAccelerated) {
8110 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008111 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008112 p.invalidateChild(this, null);
8113 return;
8114 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008115 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 if (p != null && ai != null) {
8118 final Rect r = ai.mTmpInvalRect;
8119 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8120 // Don't call invalidate -- we don't want to internally scroll
8121 // our own bounds
8122 p.invalidateChild(this, r);
8123 }
8124 }
8125 }
8126
8127 /**
Romain Guyda489792011-02-03 01:05:15 -08008128 * @hide
8129 */
8130 public void fastInvalidate() {
8131 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8132 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8133 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8134 if (mParent instanceof View) {
8135 ((View) mParent).mPrivateFlags |= INVALIDATED;
8136 }
8137 mPrivateFlags &= ~DRAWN;
8138 mPrivateFlags |= INVALIDATED;
8139 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008140 if (mParent != null && mAttachInfo != null) {
8141 if (mAttachInfo.mHardwareAccelerated) {
8142 mParent.invalidateChild(this, null);
8143 } else {
8144 final Rect r = mAttachInfo.mTmpInvalRect;
8145 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8146 // Don't call invalidate -- we don't want to internally scroll
8147 // our own bounds
8148 mParent.invalidateChild(this, r);
8149 }
Romain Guyda489792011-02-03 01:05:15 -08008150 }
8151 }
8152 }
8153
8154 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008155 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008156 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8157 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008158 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8159 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008160 *
8161 * @hide
8162 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008163 protected void invalidateParentCaches() {
8164 if (mParent instanceof View) {
8165 ((View) mParent).mPrivateFlags |= INVALIDATED;
8166 }
8167 }
Joe Malin32736f02011-01-19 16:14:20 -08008168
Romain Guy0fd89bf2011-01-26 15:41:30 -08008169 /**
8170 * Used to indicate that the parent of this view should be invalidated. This functionality
8171 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8172 * which is necessary when various parent-managed properties of the view change, such as
8173 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8174 * an invalidation event to the parent.
8175 *
8176 * @hide
8177 */
8178 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008179 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008180 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008181 }
8182 }
8183
8184 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008185 * Indicates whether this View is opaque. An opaque View guarantees that it will
8186 * draw all the pixels overlapping its bounds using a fully opaque color.
8187 *
8188 * Subclasses of View should override this method whenever possible to indicate
8189 * whether an instance is opaque. Opaque Views are treated in a special way by
8190 * the View hierarchy, possibly allowing it to perform optimizations during
8191 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008192 *
Romain Guy24443ea2009-05-11 11:56:30 -07008193 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008194 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008195 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008196 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008197 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8198 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008199 }
8200
Adam Powell20232d02010-12-08 21:08:53 -08008201 /**
8202 * @hide
8203 */
8204 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008205 // Opaque if:
8206 // - Has a background
8207 // - Background is opaque
8208 // - Doesn't have scrollbars or scrollbars are inside overlay
8209
8210 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8211 mPrivateFlags |= OPAQUE_BACKGROUND;
8212 } else {
8213 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8214 }
8215
8216 final int flags = mViewFlags;
8217 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8218 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8219 mPrivateFlags |= OPAQUE_SCROLLBARS;
8220 } else {
8221 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8222 }
8223 }
8224
8225 /**
8226 * @hide
8227 */
8228 protected boolean hasOpaqueScrollbars() {
8229 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008230 }
8231
8232 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008233 * @return A handler associated with the thread running the View. This
8234 * handler can be used to pump events in the UI events queue.
8235 */
8236 public Handler getHandler() {
8237 if (mAttachInfo != null) {
8238 return mAttachInfo.mHandler;
8239 }
8240 return null;
8241 }
8242
8243 /**
8244 * Causes the Runnable to be added to the message queue.
8245 * The runnable will be run on the user interface thread.
8246 *
8247 * @param action The Runnable that will be executed.
8248 *
8249 * @return Returns true if the Runnable was successfully placed in to the
8250 * message queue. Returns false on failure, usually because the
8251 * looper processing the message queue is exiting.
8252 */
8253 public boolean post(Runnable action) {
8254 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008255 AttachInfo attachInfo = mAttachInfo;
8256 if (attachInfo != null) {
8257 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008258 } else {
8259 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008260 ViewAncestor.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008261 return true;
8262 }
8263
8264 return handler.post(action);
8265 }
8266
8267 /**
8268 * Causes the Runnable to be added to the message queue, to be run
8269 * after the specified amount of time elapses.
8270 * The runnable will be run on the user interface thread.
8271 *
8272 * @param action The Runnable that will be executed.
8273 * @param delayMillis The delay (in milliseconds) until the Runnable
8274 * will be executed.
8275 *
8276 * @return true if the Runnable was successfully placed in to the
8277 * message queue. Returns false on failure, usually because the
8278 * looper processing the message queue is exiting. Note that a
8279 * result of true does not mean the Runnable will be processed --
8280 * if the looper is quit before the delivery time of the message
8281 * occurs then the message will be dropped.
8282 */
8283 public boolean postDelayed(Runnable action, long delayMillis) {
8284 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008285 AttachInfo attachInfo = mAttachInfo;
8286 if (attachInfo != null) {
8287 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008288 } else {
8289 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008290 ViewAncestor.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008291 return true;
8292 }
8293
8294 return handler.postDelayed(action, delayMillis);
8295 }
8296
8297 /**
8298 * Removes the specified Runnable from the message queue.
8299 *
8300 * @param action The Runnable to remove from the message handling queue
8301 *
8302 * @return true if this view could ask the Handler to remove the Runnable,
8303 * false otherwise. When the returned value is true, the Runnable
8304 * may or may not have been actually removed from the message queue
8305 * (for instance, if the Runnable was not in the queue already.)
8306 */
8307 public boolean removeCallbacks(Runnable action) {
8308 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008309 AttachInfo attachInfo = mAttachInfo;
8310 if (attachInfo != null) {
8311 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008312 } else {
8313 // Assume that post will succeed later
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008314 ViewAncestor.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008315 return true;
8316 }
8317
8318 handler.removeCallbacks(action);
8319 return true;
8320 }
8321
8322 /**
8323 * Cause an invalidate to happen on a subsequent cycle through the event loop.
8324 * Use this to invalidate the View from a non-UI thread.
8325 *
8326 * @see #invalidate()
8327 */
8328 public void postInvalidate() {
8329 postInvalidateDelayed(0);
8330 }
8331
8332 /**
8333 * Cause an invalidate of the specified area to happen on a subsequent cycle
8334 * through the event loop. Use this to invalidate the View from a non-UI thread.
8335 *
8336 * @param left The left coordinate of the rectangle to invalidate.
8337 * @param top The top coordinate of the rectangle to invalidate.
8338 * @param right The right coordinate of the rectangle to invalidate.
8339 * @param bottom The bottom coordinate of the rectangle to invalidate.
8340 *
8341 * @see #invalidate(int, int, int, int)
8342 * @see #invalidate(Rect)
8343 */
8344 public void postInvalidate(int left, int top, int right, int bottom) {
8345 postInvalidateDelayed(0, left, top, right, bottom);
8346 }
8347
8348 /**
8349 * Cause an invalidate to happen on a subsequent cycle through the event
8350 * loop. Waits for the specified amount of time.
8351 *
8352 * @param delayMilliseconds the duration in milliseconds to delay the
8353 * invalidation by
8354 */
8355 public void postInvalidateDelayed(long delayMilliseconds) {
8356 // We try only with the AttachInfo because there's no point in invalidating
8357 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008358 AttachInfo attachInfo = mAttachInfo;
8359 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008360 Message msg = Message.obtain();
8361 msg.what = AttachInfo.INVALIDATE_MSG;
8362 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008363 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008364 }
8365 }
8366
8367 /**
8368 * Cause an invalidate of the specified area to happen on a subsequent cycle
8369 * through the event loop. Waits for the specified amount of time.
8370 *
8371 * @param delayMilliseconds the duration in milliseconds to delay the
8372 * invalidation by
8373 * @param left The left coordinate of the rectangle to invalidate.
8374 * @param top The top coordinate of the rectangle to invalidate.
8375 * @param right The right coordinate of the rectangle to invalidate.
8376 * @param bottom The bottom coordinate of the rectangle to invalidate.
8377 */
8378 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8379 int right, int bottom) {
8380
8381 // We try only with the AttachInfo because there's no point in invalidating
8382 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008383 AttachInfo attachInfo = mAttachInfo;
8384 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008385 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8386 info.target = this;
8387 info.left = left;
8388 info.top = top;
8389 info.right = right;
8390 info.bottom = bottom;
8391
8392 final Message msg = Message.obtain();
8393 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8394 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008395 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008396 }
8397 }
8398
8399 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008400 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8401 * This event is sent at most once every
8402 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8403 */
8404 private void postSendViewScrolledAccessibilityEventCallback() {
8405 if (mSendViewScrolledAccessibilityEvent == null) {
8406 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8407 }
8408 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8409 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8410 postDelayed(mSendViewScrolledAccessibilityEvent,
8411 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8412 }
8413 }
8414
8415 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008416 * Called by a parent to request that a child update its values for mScrollX
8417 * and mScrollY if necessary. This will typically be done if the child is
8418 * animating a scroll using a {@link android.widget.Scroller Scroller}
8419 * object.
8420 */
8421 public void computeScroll() {
8422 }
8423
8424 /**
8425 * <p>Indicate whether the horizontal edges are faded when the view is
8426 * scrolled horizontally.</p>
8427 *
8428 * @return true if the horizontal edges should are faded on scroll, false
8429 * otherwise
8430 *
8431 * @see #setHorizontalFadingEdgeEnabled(boolean)
8432 * @attr ref android.R.styleable#View_fadingEdge
8433 */
8434 public boolean isHorizontalFadingEdgeEnabled() {
8435 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8436 }
8437
8438 /**
8439 * <p>Define whether the horizontal edges should be faded when this view
8440 * is scrolled horizontally.</p>
8441 *
8442 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8443 * be faded when the view is scrolled
8444 * horizontally
8445 *
8446 * @see #isHorizontalFadingEdgeEnabled()
8447 * @attr ref android.R.styleable#View_fadingEdge
8448 */
8449 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8450 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8451 if (horizontalFadingEdgeEnabled) {
8452 initScrollCache();
8453 }
8454
8455 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8456 }
8457 }
8458
8459 /**
8460 * <p>Indicate whether the vertical edges are faded when the view is
8461 * scrolled horizontally.</p>
8462 *
8463 * @return true if the vertical edges should are faded on scroll, false
8464 * otherwise
8465 *
8466 * @see #setVerticalFadingEdgeEnabled(boolean)
8467 * @attr ref android.R.styleable#View_fadingEdge
8468 */
8469 public boolean isVerticalFadingEdgeEnabled() {
8470 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8471 }
8472
8473 /**
8474 * <p>Define whether the vertical edges should be faded when this view
8475 * is scrolled vertically.</p>
8476 *
8477 * @param verticalFadingEdgeEnabled true if the vertical edges should
8478 * be faded when the view is scrolled
8479 * vertically
8480 *
8481 * @see #isVerticalFadingEdgeEnabled()
8482 * @attr ref android.R.styleable#View_fadingEdge
8483 */
8484 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8485 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8486 if (verticalFadingEdgeEnabled) {
8487 initScrollCache();
8488 }
8489
8490 mViewFlags ^= FADING_EDGE_VERTICAL;
8491 }
8492 }
8493
8494 /**
8495 * Returns the strength, or intensity, of the top faded edge. The strength is
8496 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8497 * returns 0.0 or 1.0 but no value in between.
8498 *
8499 * Subclasses should override this method to provide a smoother fade transition
8500 * when scrolling occurs.
8501 *
8502 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8503 */
8504 protected float getTopFadingEdgeStrength() {
8505 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8506 }
8507
8508 /**
8509 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8510 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8511 * returns 0.0 or 1.0 but no value in between.
8512 *
8513 * Subclasses should override this method to provide a smoother fade transition
8514 * when scrolling occurs.
8515 *
8516 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8517 */
8518 protected float getBottomFadingEdgeStrength() {
8519 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8520 computeVerticalScrollRange() ? 1.0f : 0.0f;
8521 }
8522
8523 /**
8524 * Returns the strength, or intensity, of the left faded edge. The strength is
8525 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8526 * returns 0.0 or 1.0 but no value in between.
8527 *
8528 * Subclasses should override this method to provide a smoother fade transition
8529 * when scrolling occurs.
8530 *
8531 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8532 */
8533 protected float getLeftFadingEdgeStrength() {
8534 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8535 }
8536
8537 /**
8538 * Returns the strength, or intensity, of the right faded edge. The strength is
8539 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8540 * returns 0.0 or 1.0 but no value in between.
8541 *
8542 * Subclasses should override this method to provide a smoother fade transition
8543 * when scrolling occurs.
8544 *
8545 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8546 */
8547 protected float getRightFadingEdgeStrength() {
8548 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8549 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8550 }
8551
8552 /**
8553 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8554 * scrollbar is not drawn by default.</p>
8555 *
8556 * @return true if the horizontal scrollbar should be painted, false
8557 * otherwise
8558 *
8559 * @see #setHorizontalScrollBarEnabled(boolean)
8560 */
8561 public boolean isHorizontalScrollBarEnabled() {
8562 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8563 }
8564
8565 /**
8566 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8567 * scrollbar is not drawn by default.</p>
8568 *
8569 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8570 * be painted
8571 *
8572 * @see #isHorizontalScrollBarEnabled()
8573 */
8574 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8575 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8576 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008577 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008578 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008579 }
8580 }
8581
8582 /**
8583 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8584 * scrollbar is not drawn by default.</p>
8585 *
8586 * @return true if the vertical scrollbar should be painted, false
8587 * otherwise
8588 *
8589 * @see #setVerticalScrollBarEnabled(boolean)
8590 */
8591 public boolean isVerticalScrollBarEnabled() {
8592 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8593 }
8594
8595 /**
8596 * <p>Define whether the vertical scrollbar should be drawn or not. The
8597 * scrollbar is not drawn by default.</p>
8598 *
8599 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8600 * be painted
8601 *
8602 * @see #isVerticalScrollBarEnabled()
8603 */
8604 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8605 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8606 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008607 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008608 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008609 }
8610 }
8611
Adam Powell20232d02010-12-08 21:08:53 -08008612 /**
8613 * @hide
8614 */
8615 protected void recomputePadding() {
8616 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008617 }
Joe Malin32736f02011-01-19 16:14:20 -08008618
Mike Cleronfe81d382009-09-28 14:22:16 -07008619 /**
8620 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008621 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008622 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008623 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008624 */
8625 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8626 initScrollCache();
8627 final ScrollabilityCache scrollabilityCache = mScrollCache;
8628 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008629 if (fadeScrollbars) {
8630 scrollabilityCache.state = ScrollabilityCache.OFF;
8631 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008632 scrollabilityCache.state = ScrollabilityCache.ON;
8633 }
8634 }
Joe Malin32736f02011-01-19 16:14:20 -08008635
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008636 /**
Joe Malin32736f02011-01-19 16:14:20 -08008637 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008638 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008639 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008640 * @return true if scrollbar fading is enabled
8641 */
8642 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008643 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008644 }
Joe Malin32736f02011-01-19 16:14:20 -08008645
Mike Cleron52f0a642009-09-28 18:21:37 -07008646 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008647 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8648 * inset. When inset, they add to the padding of the view. And the scrollbars
8649 * can be drawn inside the padding area or on the edge of the view. For example,
8650 * if a view has a background drawable and you want to draw the scrollbars
8651 * inside the padding specified by the drawable, you can use
8652 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8653 * appear at the edge of the view, ignoring the padding, then you can use
8654 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8655 * @param style the style of the scrollbars. Should be one of
8656 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8657 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8658 * @see #SCROLLBARS_INSIDE_OVERLAY
8659 * @see #SCROLLBARS_INSIDE_INSET
8660 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8661 * @see #SCROLLBARS_OUTSIDE_INSET
8662 */
8663 public void setScrollBarStyle(int style) {
8664 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8665 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008666 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008667 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008668 }
8669 }
8670
8671 /**
8672 * <p>Returns the current scrollbar style.</p>
8673 * @return the current scrollbar style
8674 * @see #SCROLLBARS_INSIDE_OVERLAY
8675 * @see #SCROLLBARS_INSIDE_INSET
8676 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8677 * @see #SCROLLBARS_OUTSIDE_INSET
8678 */
8679 public int getScrollBarStyle() {
8680 return mViewFlags & SCROLLBARS_STYLE_MASK;
8681 }
8682
8683 /**
8684 * <p>Compute the horizontal range that the horizontal scrollbar
8685 * represents.</p>
8686 *
8687 * <p>The range is expressed in arbitrary units that must be the same as the
8688 * units used by {@link #computeHorizontalScrollExtent()} and
8689 * {@link #computeHorizontalScrollOffset()}.</p>
8690 *
8691 * <p>The default range is the drawing width of this view.</p>
8692 *
8693 * @return the total horizontal range represented by the horizontal
8694 * scrollbar
8695 *
8696 * @see #computeHorizontalScrollExtent()
8697 * @see #computeHorizontalScrollOffset()
8698 * @see android.widget.ScrollBarDrawable
8699 */
8700 protected int computeHorizontalScrollRange() {
8701 return getWidth();
8702 }
8703
8704 /**
8705 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8706 * within the horizontal range. This value is used to compute the position
8707 * of the thumb within the scrollbar's track.</p>
8708 *
8709 * <p>The range is expressed in arbitrary units that must be the same as the
8710 * units used by {@link #computeHorizontalScrollRange()} and
8711 * {@link #computeHorizontalScrollExtent()}.</p>
8712 *
8713 * <p>The default offset is the scroll offset of this view.</p>
8714 *
8715 * @return the horizontal offset of the scrollbar's thumb
8716 *
8717 * @see #computeHorizontalScrollRange()
8718 * @see #computeHorizontalScrollExtent()
8719 * @see android.widget.ScrollBarDrawable
8720 */
8721 protected int computeHorizontalScrollOffset() {
8722 return mScrollX;
8723 }
8724
8725 /**
8726 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8727 * within the horizontal range. This value is used to compute the length
8728 * of the thumb within the scrollbar's track.</p>
8729 *
8730 * <p>The range is expressed in arbitrary units that must be the same as the
8731 * units used by {@link #computeHorizontalScrollRange()} and
8732 * {@link #computeHorizontalScrollOffset()}.</p>
8733 *
8734 * <p>The default extent is the drawing width of this view.</p>
8735 *
8736 * @return the horizontal extent of the scrollbar's thumb
8737 *
8738 * @see #computeHorizontalScrollRange()
8739 * @see #computeHorizontalScrollOffset()
8740 * @see android.widget.ScrollBarDrawable
8741 */
8742 protected int computeHorizontalScrollExtent() {
8743 return getWidth();
8744 }
8745
8746 /**
8747 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8748 *
8749 * <p>The range is expressed in arbitrary units that must be the same as the
8750 * units used by {@link #computeVerticalScrollExtent()} and
8751 * {@link #computeVerticalScrollOffset()}.</p>
8752 *
8753 * @return the total vertical range represented by the vertical scrollbar
8754 *
8755 * <p>The default range is the drawing height of this view.</p>
8756 *
8757 * @see #computeVerticalScrollExtent()
8758 * @see #computeVerticalScrollOffset()
8759 * @see android.widget.ScrollBarDrawable
8760 */
8761 protected int computeVerticalScrollRange() {
8762 return getHeight();
8763 }
8764
8765 /**
8766 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8767 * within the horizontal range. This value is used to compute the position
8768 * of the thumb within the scrollbar's track.</p>
8769 *
8770 * <p>The range is expressed in arbitrary units that must be the same as the
8771 * units used by {@link #computeVerticalScrollRange()} and
8772 * {@link #computeVerticalScrollExtent()}.</p>
8773 *
8774 * <p>The default offset is the scroll offset of this view.</p>
8775 *
8776 * @return the vertical offset of the scrollbar's thumb
8777 *
8778 * @see #computeVerticalScrollRange()
8779 * @see #computeVerticalScrollExtent()
8780 * @see android.widget.ScrollBarDrawable
8781 */
8782 protected int computeVerticalScrollOffset() {
8783 return mScrollY;
8784 }
8785
8786 /**
8787 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8788 * within the vertical range. This value is used to compute the length
8789 * of the thumb within the scrollbar's track.</p>
8790 *
8791 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008792 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008793 * {@link #computeVerticalScrollOffset()}.</p>
8794 *
8795 * <p>The default extent is the drawing height of this view.</p>
8796 *
8797 * @return the vertical extent of the scrollbar's thumb
8798 *
8799 * @see #computeVerticalScrollRange()
8800 * @see #computeVerticalScrollOffset()
8801 * @see android.widget.ScrollBarDrawable
8802 */
8803 protected int computeVerticalScrollExtent() {
8804 return getHeight();
8805 }
8806
8807 /**
Adam Powell69159442011-06-13 17:53:06 -07008808 * Check if this view can be scrolled horizontally in a certain direction.
8809 *
8810 * @param direction Negative to check scrolling left, positive to check scrolling right.
8811 * @return true if this view can be scrolled in the specified direction, false otherwise.
8812 */
8813 public boolean canScrollHorizontally(int direction) {
8814 final int offset = computeHorizontalScrollOffset();
8815 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8816 if (range == 0) return false;
8817 if (direction < 0) {
8818 return offset > 0;
8819 } else {
8820 return offset < range - 1;
8821 }
8822 }
8823
8824 /**
8825 * Check if this view can be scrolled vertically in a certain direction.
8826 *
8827 * @param direction Negative to check scrolling up, positive to check scrolling down.
8828 * @return true if this view can be scrolled in the specified direction, false otherwise.
8829 */
8830 public boolean canScrollVertically(int direction) {
8831 final int offset = computeVerticalScrollOffset();
8832 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8833 if (range == 0) return false;
8834 if (direction < 0) {
8835 return offset > 0;
8836 } else {
8837 return offset < range - 1;
8838 }
8839 }
8840
8841 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008842 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8843 * scrollbars are painted only if they have been awakened first.</p>
8844 *
8845 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008846 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008847 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008848 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008849 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008850 // scrollbars are drawn only when the animation is running
8851 final ScrollabilityCache cache = mScrollCache;
8852 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008853
Mike Cleronf116bf82009-09-27 19:14:12 -07008854 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008855
Mike Cleronf116bf82009-09-27 19:14:12 -07008856 if (state == ScrollabilityCache.OFF) {
8857 return;
8858 }
Joe Malin32736f02011-01-19 16:14:20 -08008859
Mike Cleronf116bf82009-09-27 19:14:12 -07008860 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08008861
Mike Cleronf116bf82009-09-27 19:14:12 -07008862 if (state == ScrollabilityCache.FADING) {
8863 // We're fading -- get our fade interpolation
8864 if (cache.interpolatorValues == null) {
8865 cache.interpolatorValues = new float[1];
8866 }
Joe Malin32736f02011-01-19 16:14:20 -08008867
Mike Cleronf116bf82009-09-27 19:14:12 -07008868 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08008869
Mike Cleronf116bf82009-09-27 19:14:12 -07008870 // Stops the animation if we're done
8871 if (cache.scrollBarInterpolator.timeToValues(values) ==
8872 Interpolator.Result.FREEZE_END) {
8873 cache.state = ScrollabilityCache.OFF;
8874 } else {
8875 cache.scrollBar.setAlpha(Math.round(values[0]));
8876 }
Joe Malin32736f02011-01-19 16:14:20 -08008877
8878 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07008879 // drawing. We only want this when we're fading so that
8880 // we prevent excessive redraws
8881 invalidate = true;
8882 } else {
8883 // We're just on -- but we may have been fading before so
8884 // reset alpha
8885 cache.scrollBar.setAlpha(255);
8886 }
8887
Joe Malin32736f02011-01-19 16:14:20 -08008888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008889 final int viewFlags = mViewFlags;
8890
8891 final boolean drawHorizontalScrollBar =
8892 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8893 final boolean drawVerticalScrollBar =
8894 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
8895 && !isVerticalScrollBarHidden();
8896
8897 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
8898 final int width = mRight - mLeft;
8899 final int height = mBottom - mTop;
8900
8901 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008902
Mike Reede8853fc2009-09-04 14:01:48 -04008903 final int scrollX = mScrollX;
8904 final int scrollY = mScrollY;
8905 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
8906
Mike Cleronf116bf82009-09-27 19:14:12 -07008907 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08008908
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008909 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008910 int size = scrollBar.getSize(false);
8911 if (size <= 0) {
8912 size = cache.scrollBarSize;
8913 }
8914
Mike Cleronf116bf82009-09-27 19:14:12 -07008915 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04008916 computeHorizontalScrollOffset(),
8917 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04008918 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07008919 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08008920 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07008921 left = scrollX + (mPaddingLeft & inside);
8922 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
8923 bottom = top + size;
8924 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
8925 if (invalidate) {
8926 invalidate(left, top, right, bottom);
8927 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008928 }
8929
8930 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08008931 int size = scrollBar.getSize(true);
8932 if (size <= 0) {
8933 size = cache.scrollBarSize;
8934 }
8935
Mike Reede8853fc2009-09-04 14:01:48 -04008936 scrollBar.setParameters(computeVerticalScrollRange(),
8937 computeVerticalScrollOffset(),
8938 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08008939 switch (mVerticalScrollbarPosition) {
8940 default:
8941 case SCROLLBAR_POSITION_DEFAULT:
8942 case SCROLLBAR_POSITION_RIGHT:
8943 left = scrollX + width - size - (mUserPaddingRight & inside);
8944 break;
8945 case SCROLLBAR_POSITION_LEFT:
8946 left = scrollX + (mUserPaddingLeft & inside);
8947 break;
8948 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008949 top = scrollY + (mPaddingTop & inside);
8950 right = left + size;
8951 bottom = scrollY + height - (mUserPaddingBottom & inside);
8952 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
8953 if (invalidate) {
8954 invalidate(left, top, right, bottom);
8955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008956 }
8957 }
8958 }
8959 }
Romain Guy8506ab42009-06-11 17:35:47 -07008960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008961 /**
Romain Guy8506ab42009-06-11 17:35:47 -07008962 * 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 -08008963 * FastScroller is visible.
8964 * @return whether to temporarily hide the vertical scrollbar
8965 * @hide
8966 */
8967 protected boolean isVerticalScrollBarHidden() {
8968 return false;
8969 }
8970
8971 /**
8972 * <p>Draw the horizontal scrollbar if
8973 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
8974 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008975 * @param canvas the canvas on which to draw the scrollbar
8976 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008977 *
8978 * @see #isHorizontalScrollBarEnabled()
8979 * @see #computeHorizontalScrollRange()
8980 * @see #computeHorizontalScrollExtent()
8981 * @see #computeHorizontalScrollOffset()
8982 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07008983 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008984 */
Romain Guy8fb95422010-08-17 18:38:51 -07008985 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
8986 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008987 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008988 scrollBar.draw(canvas);
8989 }
Mike Reede8853fc2009-09-04 14:01:48 -04008990
Mike Reed4d6fe5f2009-09-03 13:29:05 -04008991 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008992 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
8993 * returns true.</p>
8994 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 * @param canvas the canvas on which to draw the scrollbar
8996 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 *
8998 * @see #isVerticalScrollBarEnabled()
8999 * @see #computeVerticalScrollRange()
9000 * @see #computeVerticalScrollExtent()
9001 * @see #computeVerticalScrollOffset()
9002 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009003 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009004 */
Romain Guy8fb95422010-08-17 18:38:51 -07009005 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9006 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009007 scrollBar.setBounds(l, t, r, b);
9008 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009009 }
9010
9011 /**
9012 * Implement this to do your drawing.
9013 *
9014 * @param canvas the canvas on which the background will be drawn
9015 */
9016 protected void onDraw(Canvas canvas) {
9017 }
9018
9019 /*
9020 * Caller is responsible for calling requestLayout if necessary.
9021 * (This allows addViewInLayout to not request a new layout.)
9022 */
9023 void assignParent(ViewParent parent) {
9024 if (mParent == null) {
9025 mParent = parent;
9026 } else if (parent == null) {
9027 mParent = null;
9028 } else {
9029 throw new RuntimeException("view " + this + " being added, but"
9030 + " it already has a parent");
9031 }
9032 }
9033
9034 /**
9035 * This is called when the view is attached to a window. At this point it
9036 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009037 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9038 * however it may be called any time before the first onDraw -- including
9039 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009040 *
9041 * @see #onDetachedFromWindow()
9042 */
9043 protected void onAttachedToWindow() {
9044 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9045 mParent.requestTransparentRegion(this);
9046 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009047 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9048 initialAwakenScrollBars();
9049 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9050 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009051 jumpDrawablesToCurrentState();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009052 resolveLayoutDirectionIfNeeded();
9053 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009054 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009055 if (isFocused()) {
9056 InputMethodManager imm = InputMethodManager.peekInstance();
9057 imm.focusIn(this);
9058 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009059 }
Cibu Johny86666632010-02-22 13:01:02 -08009060
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009061 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009062 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9063 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009064 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009065 private void resolveLayoutDirectionIfNeeded() {
9066 // Do not resolve if it is not needed
9067 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9068
9069 // Clear any previous layout direction resolution
9070 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9071
9072 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009073 switch (getLayoutDirection()) {
9074 case LAYOUT_DIRECTION_INHERIT:
Cibu Johny86666632010-02-22 13:01:02 -08009075 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07009076 if (mParent != null &&
9077 mParent instanceof ViewGroup &&
9078 ((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009079 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009080 }
9081 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009082 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009083 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009084 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009085 case LAYOUT_DIRECTION_LOCALE:
9086 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009087 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009088 }
9089 break;
9090 default:
9091 // Nothing to do, LTR by default
9092 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009093
9094 // Set to resolved
9095 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9096 }
9097
9098 private void resolvePadding() {
9099 // If the user specified the absolute padding (either with android:padding or
9100 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9101 // use the default padding or the padding from the background drawable
9102 // (stored at this point in mPadding*)
9103 switch (getResolvedLayoutDirection()) {
9104 case LAYOUT_DIRECTION_RTL:
9105 // Start user padding override Right user padding. Otherwise, if Right user
9106 // padding is not defined, use the default Right padding. If Right user padding
9107 // is defined, just use it.
9108 if (mUserPaddingStart >= 0) {
9109 mUserPaddingRight = mUserPaddingStart;
9110 } else if (mUserPaddingRight < 0) {
9111 mUserPaddingRight = mPaddingRight;
9112 }
9113 if (mUserPaddingEnd >= 0) {
9114 mUserPaddingLeft = mUserPaddingEnd;
9115 } else if (mUserPaddingLeft < 0) {
9116 mUserPaddingLeft = mPaddingLeft;
9117 }
9118 break;
9119 case LAYOUT_DIRECTION_LTR:
9120 default:
9121 // Start user padding override Left user padding. Otherwise, if Left user
9122 // padding is not defined, use the default left padding. If Left user padding
9123 // is defined, just use it.
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009124 if (mUserPaddingStart > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009125 mUserPaddingLeft = mUserPaddingStart;
9126 } else if (mUserPaddingLeft < 0) {
9127 mUserPaddingLeft = mPaddingLeft;
9128 }
Fabrice Di Megliof8ed4442011-06-21 17:53:03 -07009129 if (mUserPaddingEnd > 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009130 mUserPaddingRight = mUserPaddingEnd;
9131 } else if (mUserPaddingRight < 0) {
9132 mUserPaddingRight = mPaddingRight;
9133 }
9134 }
9135
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009136 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9137
9138 recomputePadding();
9139 }
9140
9141 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009142 * Reset the resolved layout direction.
9143 *
9144 * Subclasses need to override this method to clear cached information that depends on the
9145 * resolved layout direction, or to inform child views that inherit their layout direction.
9146 * Overrides must also call the superclass implementation at the start of their implementation.
9147 *
9148 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009149 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009150 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009151 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009152 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009153 }
9154
9155 /**
9156 * Check if a Locale is corresponding to a RTL script.
9157 *
9158 * @param locale Locale to check
9159 * @return true if a Locale is corresponding to a RTL script.
9160 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009161 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009162 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9163 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009164 }
9165
9166 /**
9167 * This is called when the view is detached from a window. At this point it
9168 * no longer has a surface for drawing.
9169 *
9170 * @see #onAttachedToWindow()
9171 */
9172 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009173 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009174
Romain Guya440b002010-02-24 15:57:54 -08009175 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009176 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009177 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009178 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009180 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009181
9182 if (mHardwareLayer != null) {
9183 mHardwareLayer.destroy();
9184 mHardwareLayer = null;
9185 }
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009186
Chet Haasedaf98e92011-01-10 14:10:36 -08009187 if (mDisplayList != null) {
9188 mDisplayList.invalidate();
9189 }
9190
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009191 if (mAttachInfo != null) {
9192 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9193 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9194 }
9195
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009196 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009197
9198 resetResolvedLayoutDirection();
9199 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009200 }
9201
9202 /**
9203 * @return The number of times this view has been attached to a window
9204 */
9205 protected int getWindowAttachCount() {
9206 return mWindowAttachCount;
9207 }
9208
9209 /**
9210 * Retrieve a unique token identifying the window this view is attached to.
9211 * @return Return the window's token for use in
9212 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9213 */
9214 public IBinder getWindowToken() {
9215 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9216 }
9217
9218 /**
9219 * Retrieve a unique token identifying the top-level "real" window of
9220 * the window that this view is attached to. That is, this is like
9221 * {@link #getWindowToken}, except if the window this view in is a panel
9222 * window (attached to another containing window), then the token of
9223 * the containing window is returned instead.
9224 *
9225 * @return Returns the associated window token, either
9226 * {@link #getWindowToken()} or the containing window's token.
9227 */
9228 public IBinder getApplicationWindowToken() {
9229 AttachInfo ai = mAttachInfo;
9230 if (ai != null) {
9231 IBinder appWindowToken = ai.mPanelParentWindowToken;
9232 if (appWindowToken == null) {
9233 appWindowToken = ai.mWindowToken;
9234 }
9235 return appWindowToken;
9236 }
9237 return null;
9238 }
9239
9240 /**
9241 * Retrieve private session object this view hierarchy is using to
9242 * communicate with the window manager.
9243 * @return the session object to communicate with the window manager
9244 */
9245 /*package*/ IWindowSession getWindowSession() {
9246 return mAttachInfo != null ? mAttachInfo.mSession : null;
9247 }
9248
9249 /**
9250 * @param info the {@link android.view.View.AttachInfo} to associated with
9251 * this view
9252 */
9253 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9254 //System.out.println("Attached! " + this);
9255 mAttachInfo = info;
9256 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009257 // We will need to evaluate the drawable state at least once.
9258 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009259 if (mFloatingTreeObserver != null) {
9260 info.mTreeObserver.merge(mFloatingTreeObserver);
9261 mFloatingTreeObserver = null;
9262 }
9263 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9264 mAttachInfo.mScrollContainers.add(this);
9265 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9266 }
9267 performCollectViewAttributes(visibility);
9268 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009269
9270 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9271 mOnAttachStateChangeListeners;
9272 if (listeners != null && listeners.size() > 0) {
9273 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9274 // perform the dispatching. The iterator is a safe guard against listeners that
9275 // could mutate the list by calling the various add/remove methods. This prevents
9276 // the array from being modified while we iterate it.
9277 for (OnAttachStateChangeListener listener : listeners) {
9278 listener.onViewAttachedToWindow(this);
9279 }
9280 }
9281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009282 int vis = info.mWindowVisibility;
9283 if (vis != GONE) {
9284 onWindowVisibilityChanged(vis);
9285 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009286 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9287 // If nobody has evaluated the drawable state yet, then do it now.
9288 refreshDrawableState();
9289 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009290 }
9291
9292 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009293 AttachInfo info = mAttachInfo;
9294 if (info != null) {
9295 int vis = info.mWindowVisibility;
9296 if (vis != GONE) {
9297 onWindowVisibilityChanged(GONE);
9298 }
9299 }
9300
9301 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009302
Adam Powell4afd62b2011-02-18 15:02:18 -08009303 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9304 mOnAttachStateChangeListeners;
9305 if (listeners != null && listeners.size() > 0) {
9306 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9307 // perform the dispatching. The iterator is a safe guard against listeners that
9308 // could mutate the list by calling the various add/remove methods. This prevents
9309 // the array from being modified while we iterate it.
9310 for (OnAttachStateChangeListener listener : listeners) {
9311 listener.onViewDetachedFromWindow(this);
9312 }
9313 }
9314
Romain Guy01d5edc2011-01-28 11:28:53 -08009315 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009316 mAttachInfo.mScrollContainers.remove(this);
9317 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9318 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009319
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009320 mAttachInfo = null;
9321 }
9322
9323 /**
9324 * Store this view hierarchy's frozen state into the given container.
9325 *
9326 * @param container The SparseArray in which to save the view's state.
9327 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009328 * @see #restoreHierarchyState(android.util.SparseArray)
9329 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9330 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009331 */
9332 public void saveHierarchyState(SparseArray<Parcelable> container) {
9333 dispatchSaveInstanceState(container);
9334 }
9335
9336 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009337 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9338 * this view and its children. May be overridden to modify how freezing happens to a
9339 * 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 -08009340 *
9341 * @param container The SparseArray in which to save the view's state.
9342 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009343 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9344 * @see #saveHierarchyState(android.util.SparseArray)
9345 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009346 */
9347 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9348 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9349 mPrivateFlags &= ~SAVE_STATE_CALLED;
9350 Parcelable state = onSaveInstanceState();
9351 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9352 throw new IllegalStateException(
9353 "Derived class did not call super.onSaveInstanceState()");
9354 }
9355 if (state != null) {
9356 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9357 // + ": " + state);
9358 container.put(mID, state);
9359 }
9360 }
9361 }
9362
9363 /**
9364 * Hook allowing a view to generate a representation of its internal state
9365 * that can later be used to create a new instance with that same state.
9366 * This state should only contain information that is not persistent or can
9367 * not be reconstructed later. For example, you will never store your
9368 * current position on screen because that will be computed again when a
9369 * new instance of the view is placed in its view hierarchy.
9370 * <p>
9371 * Some examples of things you may store here: the current cursor position
9372 * in a text view (but usually not the text itself since that is stored in a
9373 * content provider or other persistent storage), the currently selected
9374 * item in a list view.
9375 *
9376 * @return Returns a Parcelable object containing the view's current dynamic
9377 * state, or null if there is nothing interesting to save. The
9378 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009379 * @see #onRestoreInstanceState(android.os.Parcelable)
9380 * @see #saveHierarchyState(android.util.SparseArray)
9381 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009382 * @see #setSaveEnabled(boolean)
9383 */
9384 protected Parcelable onSaveInstanceState() {
9385 mPrivateFlags |= SAVE_STATE_CALLED;
9386 return BaseSavedState.EMPTY_STATE;
9387 }
9388
9389 /**
9390 * Restore this view hierarchy's frozen state from the given container.
9391 *
9392 * @param container The SparseArray which holds previously frozen states.
9393 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009394 * @see #saveHierarchyState(android.util.SparseArray)
9395 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9396 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009397 */
9398 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9399 dispatchRestoreInstanceState(container);
9400 }
9401
9402 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009403 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9404 * state for this view and its children. May be overridden to modify how restoring
9405 * happens to a view's children; for example, some views may want to not store state
9406 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009407 *
9408 * @param container The SparseArray which holds previously saved state.
9409 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009410 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9411 * @see #restoreHierarchyState(android.util.SparseArray)
9412 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009413 */
9414 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9415 if (mID != NO_ID) {
9416 Parcelable state = container.get(mID);
9417 if (state != null) {
9418 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9419 // + ": " + state);
9420 mPrivateFlags &= ~SAVE_STATE_CALLED;
9421 onRestoreInstanceState(state);
9422 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9423 throw new IllegalStateException(
9424 "Derived class did not call super.onRestoreInstanceState()");
9425 }
9426 }
9427 }
9428 }
9429
9430 /**
9431 * Hook allowing a view to re-apply a representation of its internal state that had previously
9432 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9433 * null state.
9434 *
9435 * @param state The frozen state that had previously been returned by
9436 * {@link #onSaveInstanceState}.
9437 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009438 * @see #onSaveInstanceState()
9439 * @see #restoreHierarchyState(android.util.SparseArray)
9440 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009441 */
9442 protected void onRestoreInstanceState(Parcelable state) {
9443 mPrivateFlags |= SAVE_STATE_CALLED;
9444 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009445 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9446 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009447 + "when two views of different type have the same id in the same hierarchy. "
9448 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009449 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009450 }
9451 }
9452
9453 /**
9454 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9455 *
9456 * @return the drawing start time in milliseconds
9457 */
9458 public long getDrawingTime() {
9459 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9460 }
9461
9462 /**
9463 * <p>Enables or disables the duplication of the parent's state into this view. When
9464 * duplication is enabled, this view gets its drawable state from its parent rather
9465 * than from its own internal properties.</p>
9466 *
9467 * <p>Note: in the current implementation, setting this property to true after the
9468 * view was added to a ViewGroup might have no effect at all. This property should
9469 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9470 *
9471 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9472 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009473 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009474 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9475 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009476 *
9477 * @param enabled True to enable duplication of the parent's drawable state, false
9478 * to disable it.
9479 *
9480 * @see #getDrawableState()
9481 * @see #isDuplicateParentStateEnabled()
9482 */
9483 public void setDuplicateParentStateEnabled(boolean enabled) {
9484 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9485 }
9486
9487 /**
9488 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9489 *
9490 * @return True if this view's drawable state is duplicated from the parent,
9491 * false otherwise
9492 *
9493 * @see #getDrawableState()
9494 * @see #setDuplicateParentStateEnabled(boolean)
9495 */
9496 public boolean isDuplicateParentStateEnabled() {
9497 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9498 }
9499
9500 /**
Romain Guy171c5922011-01-06 10:04:23 -08009501 * <p>Specifies the type of layer backing this view. The layer can be
9502 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9503 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009504 *
Romain Guy171c5922011-01-06 10:04:23 -08009505 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9506 * instance that controls how the layer is composed on screen. The following
9507 * properties of the paint are taken into account when composing the layer:</p>
9508 * <ul>
9509 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9510 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9511 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9512 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009513 *
Romain Guy171c5922011-01-06 10:04:23 -08009514 * <p>If this view has an alpha value set to < 1.0 by calling
9515 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9516 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9517 * equivalent to setting a hardware layer on this view and providing a paint with
9518 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009519 *
Romain Guy171c5922011-01-06 10:04:23 -08009520 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9521 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9522 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009523 *
Romain Guy171c5922011-01-06 10:04:23 -08009524 * @param layerType The ype of layer to use with this view, must be one of
9525 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9526 * {@link #LAYER_TYPE_HARDWARE}
9527 * @param paint The paint used to compose the layer. This argument is optional
9528 * and can be null. It is ignored when the layer type is
9529 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009530 *
9531 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009532 * @see #LAYER_TYPE_NONE
9533 * @see #LAYER_TYPE_SOFTWARE
9534 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009535 * @see #setAlpha(float)
9536 *
Romain Guy171c5922011-01-06 10:04:23 -08009537 * @attr ref android.R.styleable#View_layerType
9538 */
9539 public void setLayerType(int layerType, Paint paint) {
9540 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009541 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009542 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9543 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009544
Romain Guyd6cd5722011-01-17 14:42:41 -08009545 if (layerType == mLayerType) {
9546 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9547 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009548 invalidateParentCaches();
9549 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009550 }
9551 return;
9552 }
Romain Guy171c5922011-01-06 10:04:23 -08009553
9554 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009555 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009556 case LAYER_TYPE_HARDWARE:
9557 if (mHardwareLayer != null) {
9558 mHardwareLayer.destroy();
9559 mHardwareLayer = null;
9560 }
9561 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009562 case LAYER_TYPE_SOFTWARE:
9563 if (mDrawingCache != null) {
9564 mDrawingCache.recycle();
9565 mDrawingCache = null;
9566 }
Joe Malin32736f02011-01-19 16:14:20 -08009567
Romain Guy6c319ca2011-01-11 14:29:25 -08009568 if (mUnscaledDrawingCache != null) {
9569 mUnscaledDrawingCache.recycle();
9570 mUnscaledDrawingCache = null;
9571 }
9572 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009573 default:
9574 break;
Romain Guy171c5922011-01-06 10:04:23 -08009575 }
9576
9577 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009578 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9579 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9580 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009581
Romain Guy0fd89bf2011-01-26 15:41:30 -08009582 invalidateParentCaches();
9583 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009584 }
9585
9586 /**
9587 * Indicates what type of layer is currently associated with this view. By default
9588 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9589 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9590 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009591 *
Romain Guy171c5922011-01-06 10:04:23 -08009592 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9593 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009594 *
9595 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009596 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009597 * @see #LAYER_TYPE_NONE
9598 * @see #LAYER_TYPE_SOFTWARE
9599 * @see #LAYER_TYPE_HARDWARE
9600 */
9601 public int getLayerType() {
9602 return mLayerType;
9603 }
Joe Malin32736f02011-01-19 16:14:20 -08009604
Romain Guy6c319ca2011-01-11 14:29:25 -08009605 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009606 * Forces this view's layer to be created and this view to be rendered
9607 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9608 * invoking this method will have no effect.
9609 *
9610 * This method can for instance be used to render a view into its layer before
9611 * starting an animation. If this view is complex, rendering into the layer
9612 * before starting the animation will avoid skipping frames.
9613 *
9614 * @throws IllegalStateException If this view is not attached to a window
9615 *
9616 * @see #setLayerType(int, android.graphics.Paint)
9617 */
9618 public void buildLayer() {
9619 if (mLayerType == LAYER_TYPE_NONE) return;
9620
9621 if (mAttachInfo == null) {
9622 throw new IllegalStateException("This view must be attached to a window first");
9623 }
9624
9625 switch (mLayerType) {
9626 case LAYER_TYPE_HARDWARE:
9627 getHardwareLayer();
9628 break;
9629 case LAYER_TYPE_SOFTWARE:
9630 buildDrawingCache(true);
9631 break;
9632 }
9633 }
9634
9635 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009636 * <p>Returns a hardware layer that can be used to draw this view again
9637 * without executing its draw method.</p>
9638 *
9639 * @return A HardwareLayer ready to render, or null if an error occurred.
9640 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009641 HardwareLayer getHardwareLayer() {
Romain Guy6c319ca2011-01-11 14:29:25 -08009642 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null) {
9643 return null;
9644 }
9645
9646 final int width = mRight - mLeft;
9647 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009648
Romain Guy6c319ca2011-01-11 14:29:25 -08009649 if (width == 0 || height == 0) {
9650 return null;
9651 }
9652
9653 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9654 if (mHardwareLayer == null) {
9655 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9656 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009657 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009658 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9659 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009660 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009661 }
9662
Romain Guy59a12ca2011-06-09 17:48:21 -07009663 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009664 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9665 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009666 try {
9667 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009668 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009669 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009670
Chet Haase88172fe2011-03-07 17:36:33 -08009671 final int restoreCount = canvas.save();
9672
Romain Guy6c319ca2011-01-11 14:29:25 -08009673 computeScroll();
9674 canvas.translate(-mScrollX, -mScrollY);
9675
Romain Guy6c319ca2011-01-11 14:29:25 -08009676 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009677
Romain Guy6c319ca2011-01-11 14:29:25 -08009678 // Fast path for layouts with no backgrounds
9679 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9680 mPrivateFlags &= ~DIRTY_MASK;
9681 dispatchDraw(canvas);
9682 } else {
9683 draw(canvas);
9684 }
Joe Malin32736f02011-01-19 16:14:20 -08009685
Chet Haase88172fe2011-03-07 17:36:33 -08009686 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009687 } finally {
9688 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009689 mHardwareLayer.end(currentCanvas);
9690 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009691 }
9692 }
9693
9694 return mHardwareLayer;
9695 }
Romain Guy171c5922011-01-06 10:04:23 -08009696
9697 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009698 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9699 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9700 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9701 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9702 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9703 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009704 *
Romain Guy171c5922011-01-06 10:04:23 -08009705 * <p>Enabling the drawing cache is similar to
9706 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009707 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9708 * drawing cache has no effect on rendering because the system uses a different mechanism
9709 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9710 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9711 * for information on how to enable software and hardware layers.</p>
9712 *
9713 * <p>This API can be used to manually generate
9714 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9715 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009716 *
9717 * @param enabled true to enable the drawing cache, false otherwise
9718 *
9719 * @see #isDrawingCacheEnabled()
9720 * @see #getDrawingCache()
9721 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009722 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009723 */
9724 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009725 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009726 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9727 }
9728
9729 /**
9730 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9731 *
9732 * @return true if the drawing cache is enabled
9733 *
9734 * @see #setDrawingCacheEnabled(boolean)
9735 * @see #getDrawingCache()
9736 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009737 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009738 public boolean isDrawingCacheEnabled() {
9739 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9740 }
9741
9742 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009743 * Debugging utility which recursively outputs the dirty state of a view and its
9744 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009745 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009746 * @hide
9747 */
Romain Guy676b1732011-02-14 14:45:33 -08009748 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009749 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9750 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9751 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9752 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9753 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9754 if (clear) {
9755 mPrivateFlags &= clearMask;
9756 }
9757 if (this instanceof ViewGroup) {
9758 ViewGroup parent = (ViewGroup) this;
9759 final int count = parent.getChildCount();
9760 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009761 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009762 child.outputDirtyFlags(indent + " ", clear, clearMask);
9763 }
9764 }
9765 }
9766
9767 /**
9768 * This method is used by ViewGroup to cause its children to restore or recreate their
9769 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9770 * to recreate its own display list, which would happen if it went through the normal
9771 * draw/dispatchDraw mechanisms.
9772 *
9773 * @hide
9774 */
9775 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009776
9777 /**
9778 * A view that is not attached or hardware accelerated cannot create a display list.
9779 * This method checks these conditions and returns the appropriate result.
9780 *
9781 * @return true if view has the ability to create a display list, false otherwise.
9782 *
9783 * @hide
9784 */
9785 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009786 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009787 }
Joe Malin32736f02011-01-19 16:14:20 -08009788
Chet Haasedaf98e92011-01-10 14:10:36 -08009789 /**
Romain Guyb051e892010-09-28 19:09:36 -07009790 * <p>Returns a display list that can be used to draw this view again
9791 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009792 *
Romain Guyb051e892010-09-28 19:09:36 -07009793 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009794 *
9795 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009796 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009797 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009798 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009799 return null;
9800 }
9801
Chet Haasedaf98e92011-01-10 14:10:36 -08009802 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9803 mDisplayList == null || !mDisplayList.isValid() ||
9804 mRecreateDisplayList)) {
9805 // Don't need to recreate the display list, just need to tell our
9806 // children to restore/recreate theirs
9807 if (mDisplayList != null && mDisplayList.isValid() &&
9808 !mRecreateDisplayList) {
9809 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9810 mPrivateFlags &= ~DIRTY_MASK;
9811 dispatchGetDisplayList();
9812
9813 return mDisplayList;
9814 }
9815
9816 // If we got here, we're recreating it. Mark it as such to ensure that
9817 // we copy in child display lists into ours in drawChild()
9818 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009819 if (mDisplayList == null) {
Chet Haasedaf98e92011-01-10 14:10:36 -08009820 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList(this);
9821 // If we're creating a new display list, make sure our parent gets invalidated
9822 // since they will need to recreate their display list to account for this
9823 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009824 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009825 }
Romain Guyb051e892010-09-28 19:09:36 -07009826
9827 final HardwareCanvas canvas = mDisplayList.start();
9828 try {
9829 int width = mRight - mLeft;
9830 int height = mBottom - mTop;
9831
9832 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -08009833 // The dirty rect should always be null for a display list
9834 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -07009835
Chet Haase88172fe2011-03-07 17:36:33 -08009836 final int restoreCount = canvas.save();
9837
Chet Haasedaf98e92011-01-10 14:10:36 -08009838 computeScroll();
9839 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -07009840 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -07009841 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -08009842
Romain Guyb051e892010-09-28 19:09:36 -07009843 // Fast path for layouts with no backgrounds
9844 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -07009845 dispatchDraw(canvas);
9846 } else {
9847 draw(canvas);
9848 }
Joe Malin32736f02011-01-19 16:14:20 -08009849
Chet Haase88172fe2011-03-07 17:36:33 -08009850 canvas.restoreToCount(restoreCount);
Romain Guyb051e892010-09-28 19:09:36 -07009851 } finally {
9852 canvas.onPostDraw();
9853
9854 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -07009855 }
Chet Haase77785f92011-01-25 23:22:09 -08009856 } else {
9857 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9858 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -07009859 }
9860
9861 return mDisplayList;
9862 }
9863
9864 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009865 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009866 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009867 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009868 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009869 * @see #getDrawingCache(boolean)
9870 */
9871 public Bitmap getDrawingCache() {
9872 return getDrawingCache(false);
9873 }
9874
9875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009876 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
9877 * is null when caching is disabled. If caching is enabled and the cache is not ready,
9878 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
9879 * draw from the cache when the cache is enabled. To benefit from the cache, you must
9880 * request the drawing cache by calling this method and draw it on screen if the
9881 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009882 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009883 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9884 * this method will create a bitmap of the same size as this view. Because this bitmap
9885 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9886 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9887 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9888 * size than the view. This implies that your application must be able to handle this
9889 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009890 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009891 * @param autoScale Indicates whether the generated bitmap should be scaled based on
9892 * the current density of the screen when the application is in compatibility
9893 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009894 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009895 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -08009896 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009897 * @see #setDrawingCacheEnabled(boolean)
9898 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -07009899 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009900 * @see #destroyDrawingCache()
9901 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009902 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009903 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
9904 return null;
9905 }
9906 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -07009907 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 }
Romain Guy02890fd2010-08-06 17:58:44 -07009909 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009910 }
9911
9912 /**
9913 * <p>Frees the resources used by the drawing cache. If you call
9914 * {@link #buildDrawingCache()} manually without calling
9915 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9916 * should cleanup the cache with this method afterwards.</p>
9917 *
9918 * @see #setDrawingCacheEnabled(boolean)
9919 * @see #buildDrawingCache()
9920 * @see #getDrawingCache()
9921 */
9922 public void destroyDrawingCache() {
9923 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009924 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009925 mDrawingCache = null;
9926 }
Romain Guyfbd8f692009-06-26 14:51:58 -07009927 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -07009928 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -07009929 mUnscaledDrawingCache = null;
9930 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009931 }
9932
9933 /**
9934 * Setting a solid background color for the drawing cache's bitmaps will improve
9935 * perfromance and memory usage. Note, though that this should only be used if this
9936 * view will always be drawn on top of a solid color.
9937 *
9938 * @param color The background color to use for the drawing cache's bitmap
9939 *
9940 * @see #setDrawingCacheEnabled(boolean)
9941 * @see #buildDrawingCache()
9942 * @see #getDrawingCache()
9943 */
9944 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -08009945 if (color != mDrawingCacheBackgroundColor) {
9946 mDrawingCacheBackgroundColor = color;
9947 mPrivateFlags &= ~DRAWING_CACHE_VALID;
9948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009949 }
9950
9951 /**
9952 * @see #setDrawingCacheBackgroundColor(int)
9953 *
9954 * @return The background color to used for the drawing cache's bitmap
9955 */
9956 public int getDrawingCacheBackgroundColor() {
9957 return mDrawingCacheBackgroundColor;
9958 }
9959
9960 /**
Romain Guyfbd8f692009-06-26 14:51:58 -07009961 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009962 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009963 * @see #buildDrawingCache(boolean)
9964 */
9965 public void buildDrawingCache() {
9966 buildDrawingCache(false);
9967 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08009968
Romain Guyfbd8f692009-06-26 14:51:58 -07009969 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009970 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
9971 *
9972 * <p>If you call {@link #buildDrawingCache()} manually without calling
9973 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
9974 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009975 *
Romain Guyfbd8f692009-06-26 14:51:58 -07009976 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
9977 * this method will create a bitmap of the same size as this view. Because this bitmap
9978 * will be drawn scaled by the parent ViewGroup, the result on screen might show
9979 * scaling artifacts. To avoid such artifacts, you should call this method by setting
9980 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
9981 * size than the view. This implies that your application must be able to handle this
9982 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009983 *
Romain Guy0d9275e2010-10-26 14:22:30 -07009984 * <p>You should avoid calling this method when hardware acceleration is enabled. If
9985 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -08009986 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -07009987 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009988 *
9989 * @see #getDrawingCache()
9990 * @see #destroyDrawingCache()
9991 */
Romain Guyfbd8f692009-06-26 14:51:58 -07009992 public void buildDrawingCache(boolean autoScale) {
9993 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -07009994 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009995 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009996
9997 if (ViewDebug.TRACE_HIERARCHY) {
9998 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
9999 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010000
Romain Guy8506ab42009-06-11 17:35:47 -070010001 int width = mRight - mLeft;
10002 int height = mBottom - mTop;
10003
10004 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010005 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010006
Romain Guye1123222009-06-29 14:24:56 -070010007 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010008 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10009 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010010 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010011
10012 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010013 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010014 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010015
10016 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010017 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010018 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010019 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10020 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010021 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010022 return;
10023 }
10024
10025 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010026 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010027
10028 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010029 Bitmap.Config quality;
10030 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010031 // Never pick ARGB_4444 because it looks awful
10032 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010033 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10034 case DRAWING_CACHE_QUALITY_AUTO:
10035 quality = Bitmap.Config.ARGB_8888;
10036 break;
10037 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010038 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010039 break;
10040 case DRAWING_CACHE_QUALITY_HIGH:
10041 quality = Bitmap.Config.ARGB_8888;
10042 break;
10043 default:
10044 quality = Bitmap.Config.ARGB_8888;
10045 break;
10046 }
10047 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010048 // Optimization for translucent windows
10049 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010050 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010051 }
10052
10053 // Try to cleanup memory
10054 if (bitmap != null) bitmap.recycle();
10055
10056 try {
10057 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010058 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010059 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010060 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010061 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010062 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010063 }
Adam Powell26153a32010-11-08 15:22:27 -080010064 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010065 } catch (OutOfMemoryError e) {
10066 // If there is not enough memory to create the bitmap cache, just
10067 // ignore the issue as bitmap caches are not required to draw the
10068 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010069 if (autoScale) {
10070 mDrawingCache = null;
10071 } else {
10072 mUnscaledDrawingCache = null;
10073 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010074 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010075 return;
10076 }
10077
10078 clear = drawingCacheBackgroundColor != 0;
10079 }
10080
10081 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010082 if (attachInfo != null) {
10083 canvas = attachInfo.mCanvas;
10084 if (canvas == null) {
10085 canvas = new Canvas();
10086 }
10087 canvas.setBitmap(bitmap);
10088 // Temporarily clobber the cached Canvas in case one of our children
10089 // is also using a drawing cache. Without this, the children would
10090 // steal the canvas by attaching their own bitmap to it and bad, bad
10091 // thing would happen (invisible views, corrupted drawings, etc.)
10092 attachInfo.mCanvas = null;
10093 } else {
10094 // This case should hopefully never or seldom happen
10095 canvas = new Canvas(bitmap);
10096 }
10097
10098 if (clear) {
10099 bitmap.eraseColor(drawingCacheBackgroundColor);
10100 }
10101
10102 computeScroll();
10103 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010104
Romain Guye1123222009-06-29 14:24:56 -070010105 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010106 final float scale = attachInfo.mApplicationScale;
10107 canvas.scale(scale, scale);
10108 }
Joe Malin32736f02011-01-19 16:14:20 -080010109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010110 canvas.translate(-mScrollX, -mScrollY);
10111
Romain Guy5bcdff42009-05-14 21:27:18 -070010112 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010113 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10114 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010115 mPrivateFlags |= DRAWING_CACHE_VALID;
10116 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010117
10118 // Fast path for layouts with no backgrounds
10119 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10120 if (ViewDebug.TRACE_HIERARCHY) {
10121 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10122 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010123 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010124 dispatchDraw(canvas);
10125 } else {
10126 draw(canvas);
10127 }
10128
10129 canvas.restoreToCount(restoreCount);
10130
10131 if (attachInfo != null) {
10132 // Restore the cached Canvas for our siblings
10133 attachInfo.mCanvas = canvas;
10134 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010135 }
10136 }
10137
10138 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010139 * Create a snapshot of the view into a bitmap. We should probably make
10140 * some form of this public, but should think about the API.
10141 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010142 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010143 int width = mRight - mLeft;
10144 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010145
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010146 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010147 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010148 width = (int) ((width * scale) + 0.5f);
10149 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010150
Romain Guy8c11e312009-09-14 15:15:30 -070010151 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010152 if (bitmap == null) {
10153 throw new OutOfMemoryError();
10154 }
10155
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010156 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010157
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010158 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010159 if (attachInfo != null) {
10160 canvas = attachInfo.mCanvas;
10161 if (canvas == null) {
10162 canvas = new Canvas();
10163 }
10164 canvas.setBitmap(bitmap);
10165 // Temporarily clobber the cached Canvas in case one of our children
10166 // is also using a drawing cache. Without this, the children would
10167 // steal the canvas by attaching their own bitmap to it and bad, bad
10168 // things would happen (invisible views, corrupted drawings, etc.)
10169 attachInfo.mCanvas = null;
10170 } else {
10171 // This case should hopefully never or seldom happen
10172 canvas = new Canvas(bitmap);
10173 }
10174
Romain Guy5bcdff42009-05-14 21:27:18 -070010175 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010176 bitmap.eraseColor(backgroundColor);
10177 }
10178
10179 computeScroll();
10180 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010181 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010182 canvas.translate(-mScrollX, -mScrollY);
10183
Romain Guy5bcdff42009-05-14 21:27:18 -070010184 // Temporarily remove the dirty mask
10185 int flags = mPrivateFlags;
10186 mPrivateFlags &= ~DIRTY_MASK;
10187
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010188 // Fast path for layouts with no backgrounds
10189 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10190 dispatchDraw(canvas);
10191 } else {
10192 draw(canvas);
10193 }
10194
Romain Guy5bcdff42009-05-14 21:27:18 -070010195 mPrivateFlags = flags;
10196
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010197 canvas.restoreToCount(restoreCount);
10198
10199 if (attachInfo != null) {
10200 // Restore the cached Canvas for our siblings
10201 attachInfo.mCanvas = canvas;
10202 }
Romain Guy8506ab42009-06-11 17:35:47 -070010203
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010204 return bitmap;
10205 }
10206
10207 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010208 * Indicates whether this View is currently in edit mode. A View is usually
10209 * in edit mode when displayed within a developer tool. For instance, if
10210 * this View is being drawn by a visual user interface builder, this method
10211 * should return true.
10212 *
10213 * Subclasses should check the return value of this method to provide
10214 * different behaviors if their normal behavior might interfere with the
10215 * host environment. For instance: the class spawns a thread in its
10216 * constructor, the drawing code relies on device-specific features, etc.
10217 *
10218 * This method is usually checked in the drawing code of custom widgets.
10219 *
10220 * @return True if this View is in edit mode, false otherwise.
10221 */
10222 public boolean isInEditMode() {
10223 return false;
10224 }
10225
10226 /**
10227 * If the View draws content inside its padding and enables fading edges,
10228 * it needs to support padding offsets. Padding offsets are added to the
10229 * fading edges to extend the length of the fade so that it covers pixels
10230 * drawn inside the padding.
10231 *
10232 * Subclasses of this class should override this method if they need
10233 * to draw content inside the padding.
10234 *
10235 * @return True if padding offset must be applied, false otherwise.
10236 *
10237 * @see #getLeftPaddingOffset()
10238 * @see #getRightPaddingOffset()
10239 * @see #getTopPaddingOffset()
10240 * @see #getBottomPaddingOffset()
10241 *
10242 * @since CURRENT
10243 */
10244 protected boolean isPaddingOffsetRequired() {
10245 return false;
10246 }
10247
10248 /**
10249 * Amount by which to extend the left fading region. Called only when
10250 * {@link #isPaddingOffsetRequired()} returns true.
10251 *
10252 * @return The left padding offset in pixels.
10253 *
10254 * @see #isPaddingOffsetRequired()
10255 *
10256 * @since CURRENT
10257 */
10258 protected int getLeftPaddingOffset() {
10259 return 0;
10260 }
10261
10262 /**
10263 * Amount by which to extend the right fading region. Called only when
10264 * {@link #isPaddingOffsetRequired()} returns true.
10265 *
10266 * @return The right padding offset in pixels.
10267 *
10268 * @see #isPaddingOffsetRequired()
10269 *
10270 * @since CURRENT
10271 */
10272 protected int getRightPaddingOffset() {
10273 return 0;
10274 }
10275
10276 /**
10277 * Amount by which to extend the top fading region. Called only when
10278 * {@link #isPaddingOffsetRequired()} returns true.
10279 *
10280 * @return The top padding offset in pixels.
10281 *
10282 * @see #isPaddingOffsetRequired()
10283 *
10284 * @since CURRENT
10285 */
10286 protected int getTopPaddingOffset() {
10287 return 0;
10288 }
10289
10290 /**
10291 * Amount by which to extend the bottom fading region. Called only when
10292 * {@link #isPaddingOffsetRequired()} returns true.
10293 *
10294 * @return The bottom padding offset in pixels.
10295 *
10296 * @see #isPaddingOffsetRequired()
10297 *
10298 * @since CURRENT
10299 */
10300 protected int getBottomPaddingOffset() {
10301 return 0;
10302 }
10303
10304 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010305 * <p>Indicates whether this view is attached to an hardware accelerated
10306 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010307 *
Romain Guy2bffd262010-09-12 17:40:02 -070010308 * <p>Even if this method returns true, it does not mean that every call
10309 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10310 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10311 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10312 * window is hardware accelerated,
10313 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10314 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010315 *
Romain Guy2bffd262010-09-12 17:40:02 -070010316 * @return True if the view is attached to a window and the window is
10317 * hardware accelerated; false in any other case.
10318 */
10319 public boolean isHardwareAccelerated() {
10320 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10321 }
Joe Malin32736f02011-01-19 16:14:20 -080010322
Romain Guy2bffd262010-09-12 17:40:02 -070010323 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010324 * Manually render this view (and all of its children) to the given Canvas.
10325 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010326 * called. When implementing a view, implement
10327 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10328 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010329 *
10330 * @param canvas The Canvas to which the View is rendered.
10331 */
10332 public void draw(Canvas canvas) {
10333 if (ViewDebug.TRACE_HIERARCHY) {
10334 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10335 }
10336
Romain Guy5bcdff42009-05-14 21:27:18 -070010337 final int privateFlags = mPrivateFlags;
10338 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10339 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10340 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010342 /*
10343 * Draw traversal performs several drawing steps which must be executed
10344 * in the appropriate order:
10345 *
10346 * 1. Draw the background
10347 * 2. If necessary, save the canvas' layers to prepare for fading
10348 * 3. Draw view's content
10349 * 4. Draw children
10350 * 5. If necessary, draw the fading edges and restore layers
10351 * 6. Draw decorations (scrollbars for instance)
10352 */
10353
10354 // Step 1, draw the background, if needed
10355 int saveCount;
10356
Romain Guy24443ea2009-05-11 11:56:30 -070010357 if (!dirtyOpaque) {
10358 final Drawable background = mBGDrawable;
10359 if (background != null) {
10360 final int scrollX = mScrollX;
10361 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010362
Romain Guy24443ea2009-05-11 11:56:30 -070010363 if (mBackgroundSizeChanged) {
10364 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10365 mBackgroundSizeChanged = false;
10366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010367
Romain Guy24443ea2009-05-11 11:56:30 -070010368 if ((scrollX | scrollY) == 0) {
10369 background.draw(canvas);
10370 } else {
10371 canvas.translate(scrollX, scrollY);
10372 background.draw(canvas);
10373 canvas.translate(-scrollX, -scrollY);
10374 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010375 }
10376 }
10377
10378 // skip step 2 & 5 if possible (common case)
10379 final int viewFlags = mViewFlags;
10380 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10381 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10382 if (!verticalEdges && !horizontalEdges) {
10383 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010384 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010385
10386 // Step 4, draw the children
10387 dispatchDraw(canvas);
10388
10389 // Step 6, draw decorations (scrollbars)
10390 onDrawScrollBars(canvas);
10391
10392 // we're done...
10393 return;
10394 }
10395
10396 /*
10397 * Here we do the full fledged routine...
10398 * (this is an uncommon case where speed matters less,
10399 * this is why we repeat some of the tests that have been
10400 * done above)
10401 */
10402
10403 boolean drawTop = false;
10404 boolean drawBottom = false;
10405 boolean drawLeft = false;
10406 boolean drawRight = false;
10407
10408 float topFadeStrength = 0.0f;
10409 float bottomFadeStrength = 0.0f;
10410 float leftFadeStrength = 0.0f;
10411 float rightFadeStrength = 0.0f;
10412
10413 // Step 2, save the canvas' layers
10414 int paddingLeft = mPaddingLeft;
10415 int paddingTop = mPaddingTop;
10416
10417 final boolean offsetRequired = isPaddingOffsetRequired();
10418 if (offsetRequired) {
10419 paddingLeft += getLeftPaddingOffset();
10420 paddingTop += getTopPaddingOffset();
10421 }
10422
10423 int left = mScrollX + paddingLeft;
10424 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
10425 int top = mScrollY + paddingTop;
10426 int bottom = top + mBottom - mTop - mPaddingBottom - paddingTop;
10427
10428 if (offsetRequired) {
10429 right += getRightPaddingOffset();
10430 bottom += getBottomPaddingOffset();
10431 }
10432
10433 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010434 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10435 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010436
10437 // clip the fade length if top and bottom fades overlap
10438 // overlapping fades produce odd-looking artifacts
10439 if (verticalEdges && (top + length > bottom - length)) {
10440 length = (bottom - top) / 2;
10441 }
10442
10443 // also clip horizontal fades if necessary
10444 if (horizontalEdges && (left + length > right - length)) {
10445 length = (right - left) / 2;
10446 }
10447
10448 if (verticalEdges) {
10449 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010450 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010451 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010452 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010453 }
10454
10455 if (horizontalEdges) {
10456 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010457 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010458 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010459 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010460 }
10461
10462 saveCount = canvas.getSaveCount();
10463
10464 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010465 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010466 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10467
10468 if (drawTop) {
10469 canvas.saveLayer(left, top, right, top + length, null, flags);
10470 }
10471
10472 if (drawBottom) {
10473 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10474 }
10475
10476 if (drawLeft) {
10477 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10478 }
10479
10480 if (drawRight) {
10481 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10482 }
10483 } else {
10484 scrollabilityCache.setFadeColor(solidColor);
10485 }
10486
10487 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010488 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010489
10490 // Step 4, draw the children
10491 dispatchDraw(canvas);
10492
10493 // Step 5, draw the fade effect and restore layers
10494 final Paint p = scrollabilityCache.paint;
10495 final Matrix matrix = scrollabilityCache.matrix;
10496 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010497
10498 if (drawTop) {
10499 matrix.setScale(1, fadeHeight * topFadeStrength);
10500 matrix.postTranslate(left, top);
10501 fade.setLocalMatrix(matrix);
10502 canvas.drawRect(left, top, right, top + length, p);
10503 }
10504
10505 if (drawBottom) {
10506 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10507 matrix.postRotate(180);
10508 matrix.postTranslate(left, bottom);
10509 fade.setLocalMatrix(matrix);
10510 canvas.drawRect(left, bottom - length, right, bottom, p);
10511 }
10512
10513 if (drawLeft) {
10514 matrix.setScale(1, fadeHeight * leftFadeStrength);
10515 matrix.postRotate(-90);
10516 matrix.postTranslate(left, top);
10517 fade.setLocalMatrix(matrix);
10518 canvas.drawRect(left, top, left + length, bottom, p);
10519 }
10520
10521 if (drawRight) {
10522 matrix.setScale(1, fadeHeight * rightFadeStrength);
10523 matrix.postRotate(90);
10524 matrix.postTranslate(right, top);
10525 fade.setLocalMatrix(matrix);
10526 canvas.drawRect(right - length, top, right, bottom, p);
10527 }
10528
10529 canvas.restoreToCount(saveCount);
10530
10531 // Step 6, draw decorations (scrollbars)
10532 onDrawScrollBars(canvas);
10533 }
10534
10535 /**
10536 * Override this if your view is known to always be drawn on top of a solid color background,
10537 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10538 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10539 * should be set to 0xFF.
10540 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010541 * @see #setVerticalFadingEdgeEnabled(boolean)
10542 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010543 *
10544 * @return The known solid color background for this view, or 0 if the color may vary
10545 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010546 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010547 public int getSolidColor() {
10548 return 0;
10549 }
10550
10551 /**
10552 * Build a human readable string representation of the specified view flags.
10553 *
10554 * @param flags the view flags to convert to a string
10555 * @return a String representing the supplied flags
10556 */
10557 private static String printFlags(int flags) {
10558 String output = "";
10559 int numFlags = 0;
10560 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10561 output += "TAKES_FOCUS";
10562 numFlags++;
10563 }
10564
10565 switch (flags & VISIBILITY_MASK) {
10566 case INVISIBLE:
10567 if (numFlags > 0) {
10568 output += " ";
10569 }
10570 output += "INVISIBLE";
10571 // USELESS HERE numFlags++;
10572 break;
10573 case GONE:
10574 if (numFlags > 0) {
10575 output += " ";
10576 }
10577 output += "GONE";
10578 // USELESS HERE numFlags++;
10579 break;
10580 default:
10581 break;
10582 }
10583 return output;
10584 }
10585
10586 /**
10587 * Build a human readable string representation of the specified private
10588 * view flags.
10589 *
10590 * @param privateFlags the private view flags to convert to a string
10591 * @return a String representing the supplied flags
10592 */
10593 private static String printPrivateFlags(int privateFlags) {
10594 String output = "";
10595 int numFlags = 0;
10596
10597 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10598 output += "WANTS_FOCUS";
10599 numFlags++;
10600 }
10601
10602 if ((privateFlags & FOCUSED) == FOCUSED) {
10603 if (numFlags > 0) {
10604 output += " ";
10605 }
10606 output += "FOCUSED";
10607 numFlags++;
10608 }
10609
10610 if ((privateFlags & SELECTED) == SELECTED) {
10611 if (numFlags > 0) {
10612 output += " ";
10613 }
10614 output += "SELECTED";
10615 numFlags++;
10616 }
10617
10618 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10619 if (numFlags > 0) {
10620 output += " ";
10621 }
10622 output += "IS_ROOT_NAMESPACE";
10623 numFlags++;
10624 }
10625
10626 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10627 if (numFlags > 0) {
10628 output += " ";
10629 }
10630 output += "HAS_BOUNDS";
10631 numFlags++;
10632 }
10633
10634 if ((privateFlags & DRAWN) == DRAWN) {
10635 if (numFlags > 0) {
10636 output += " ";
10637 }
10638 output += "DRAWN";
10639 // USELESS HERE numFlags++;
10640 }
10641 return output;
10642 }
10643
10644 /**
10645 * <p>Indicates whether or not this view's layout will be requested during
10646 * the next hierarchy layout pass.</p>
10647 *
10648 * @return true if the layout will be forced during next layout pass
10649 */
10650 public boolean isLayoutRequested() {
10651 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10652 }
10653
10654 /**
10655 * Assign a size and position to a view and all of its
10656 * descendants
10657 *
10658 * <p>This is the second phase of the layout mechanism.
10659 * (The first is measuring). In this phase, each parent calls
10660 * layout on all of its children to position them.
10661 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010662 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010663 *
Chet Haase9c087442011-01-12 16:20:16 -080010664 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010665 * Derived classes with children should override
10666 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010667 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010668 *
10669 * @param l Left position, relative to parent
10670 * @param t Top position, relative to parent
10671 * @param r Right position, relative to parent
10672 * @param b Bottom position, relative to parent
10673 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010674 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010675 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010676 int oldL = mLeft;
10677 int oldT = mTop;
10678 int oldB = mBottom;
10679 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010680 boolean changed = setFrame(l, t, r, b);
10681 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10682 if (ViewDebug.TRACE_HIERARCHY) {
10683 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10684 }
10685
10686 onLayout(changed, l, t, r, b);
10687 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010688
10689 if (mOnLayoutChangeListeners != null) {
10690 ArrayList<OnLayoutChangeListener> listenersCopy =
10691 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10692 int numListeners = listenersCopy.size();
10693 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010694 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010695 }
10696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010697 }
10698 mPrivateFlags &= ~FORCE_LAYOUT;
10699 }
10700
10701 /**
10702 * Called from layout when this view should
10703 * assign a size and position to each of its children.
10704 *
10705 * Derived classes with children should override
10706 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010707 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010708 * @param changed This is a new size or position for this view
10709 * @param left Left position, relative to parent
10710 * @param top Top position, relative to parent
10711 * @param right Right position, relative to parent
10712 * @param bottom Bottom position, relative to parent
10713 */
10714 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10715 }
10716
10717 /**
10718 * Assign a size and position to this view.
10719 *
10720 * This is called from layout.
10721 *
10722 * @param left Left position, relative to parent
10723 * @param top Top position, relative to parent
10724 * @param right Right position, relative to parent
10725 * @param bottom Bottom position, relative to parent
10726 * @return true if the new size and position are different than the
10727 * previous ones
10728 * {@hide}
10729 */
10730 protected boolean setFrame(int left, int top, int right, int bottom) {
10731 boolean changed = false;
10732
10733 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010734 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010735 + right + "," + bottom + ")");
10736 }
10737
10738 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10739 changed = true;
10740
10741 // Remember our drawn bit
10742 int drawn = mPrivateFlags & DRAWN;
10743
10744 // Invalidate our old position
Romain Guy0fd89bf2011-01-26 15:41:30 -080010745 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010746
10747
10748 int oldWidth = mRight - mLeft;
10749 int oldHeight = mBottom - mTop;
10750
10751 mLeft = left;
10752 mTop = top;
10753 mRight = right;
10754 mBottom = bottom;
10755
10756 mPrivateFlags |= HAS_BOUNDS;
10757
10758 int newWidth = right - left;
10759 int newHeight = bottom - top;
10760
10761 if (newWidth != oldWidth || newHeight != oldHeight) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010762 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10763 // A change in dimension means an auto-centered pivot point changes, too
10764 mMatrixDirty = true;
10765 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010766 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10767 }
10768
10769 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10770 // If we are visible, force the DRAWN bit to on so that
10771 // this invalidate will go through (at least to our parent).
10772 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010773 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010774 // the DRAWN bit.
10775 mPrivateFlags |= DRAWN;
Romain Guy0fd89bf2011-01-26 15:41:30 -080010776 invalidate(true);
Chet Haasef28595e2011-01-31 18:52:12 -080010777 // parent display list may need to be recreated based on a change in the bounds
10778 // of any child
10779 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010780 }
10781
10782 // Reset drawn bit to original value (invalidate turns it off)
10783 mPrivateFlags |= drawn;
10784
10785 mBackgroundSizeChanged = true;
10786 }
10787 return changed;
10788 }
10789
10790 /**
10791 * Finalize inflating a view from XML. This is called as the last phase
10792 * of inflation, after all child views have been added.
10793 *
10794 * <p>Even if the subclass overrides onFinishInflate, they should always be
10795 * sure to call the super method, so that we get called.
10796 */
10797 protected void onFinishInflate() {
10798 }
10799
10800 /**
10801 * Returns the resources associated with this view.
10802 *
10803 * @return Resources object.
10804 */
10805 public Resources getResources() {
10806 return mResources;
10807 }
10808
10809 /**
10810 * Invalidates the specified Drawable.
10811 *
10812 * @param drawable the drawable to invalidate
10813 */
10814 public void invalidateDrawable(Drawable drawable) {
10815 if (verifyDrawable(drawable)) {
10816 final Rect dirty = drawable.getBounds();
10817 final int scrollX = mScrollX;
10818 final int scrollY = mScrollY;
10819
10820 invalidate(dirty.left + scrollX, dirty.top + scrollY,
10821 dirty.right + scrollX, dirty.bottom + scrollY);
10822 }
10823 }
10824
10825 /**
10826 * Schedules an action on a drawable to occur at a specified time.
10827 *
10828 * @param who the recipient of the action
10829 * @param what the action to run on the drawable
10830 * @param when the time at which the action must occur. Uses the
10831 * {@link SystemClock#uptimeMillis} timebase.
10832 */
10833 public void scheduleDrawable(Drawable who, Runnable what, long when) {
10834 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10835 mAttachInfo.mHandler.postAtTime(what, who, when);
10836 }
10837 }
10838
10839 /**
10840 * Cancels a scheduled action on a drawable.
10841 *
10842 * @param who the recipient of the action
10843 * @param what the action to cancel
10844 */
10845 public void unscheduleDrawable(Drawable who, Runnable what) {
10846 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
10847 mAttachInfo.mHandler.removeCallbacks(what, who);
10848 }
10849 }
10850
10851 /**
10852 * Unschedule any events associated with the given Drawable. This can be
10853 * used when selecting a new Drawable into a view, so that the previous
10854 * one is completely unscheduled.
10855 *
10856 * @param who The Drawable to unschedule.
10857 *
10858 * @see #drawableStateChanged
10859 */
10860 public void unscheduleDrawable(Drawable who) {
10861 if (mAttachInfo != null) {
10862 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
10863 }
10864 }
10865
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070010866 /**
10867 * Return the layout direction of a given Drawable.
10868 *
10869 * @param who the Drawable to query
10870 *
10871 * @hide
10872 */
10873 public int getResolvedLayoutDirection(Drawable who) {
10874 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070010875 }
10876
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010877 /**
10878 * If your view subclass is displaying its own Drawable objects, it should
10879 * override this function and return true for any Drawable it is
10880 * displaying. This allows animations for those drawables to be
10881 * scheduled.
10882 *
10883 * <p>Be sure to call through to the super class when overriding this
10884 * function.
10885 *
10886 * @param who The Drawable to verify. Return true if it is one you are
10887 * displaying, else return the result of calling through to the
10888 * super class.
10889 *
10890 * @return boolean If true than the Drawable is being displayed in the
10891 * view; else false and it is not allowed to animate.
10892 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010893 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
10894 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010895 */
10896 protected boolean verifyDrawable(Drawable who) {
10897 return who == mBGDrawable;
10898 }
10899
10900 /**
10901 * This function is called whenever the state of the view changes in such
10902 * a way that it impacts the state of drawables being shown.
10903 *
10904 * <p>Be sure to call through to the superclass when overriding this
10905 * function.
10906 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010907 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010908 */
10909 protected void drawableStateChanged() {
10910 Drawable d = mBGDrawable;
10911 if (d != null && d.isStateful()) {
10912 d.setState(getDrawableState());
10913 }
10914 }
10915
10916 /**
10917 * Call this to force a view to update its drawable state. This will cause
10918 * drawableStateChanged to be called on this view. Views that are interested
10919 * in the new state should call getDrawableState.
10920 *
10921 * @see #drawableStateChanged
10922 * @see #getDrawableState
10923 */
10924 public void refreshDrawableState() {
10925 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
10926 drawableStateChanged();
10927
10928 ViewParent parent = mParent;
10929 if (parent != null) {
10930 parent.childDrawableStateChanged(this);
10931 }
10932 }
10933
10934 /**
10935 * Return an array of resource IDs of the drawable states representing the
10936 * current state of the view.
10937 *
10938 * @return The current drawable state
10939 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010940 * @see Drawable#setState(int[])
10941 * @see #drawableStateChanged()
10942 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010943 */
10944 public final int[] getDrawableState() {
10945 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
10946 return mDrawableState;
10947 } else {
10948 mDrawableState = onCreateDrawableState(0);
10949 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
10950 return mDrawableState;
10951 }
10952 }
10953
10954 /**
10955 * Generate the new {@link android.graphics.drawable.Drawable} state for
10956 * this view. This is called by the view
10957 * system when the cached Drawable state is determined to be invalid. To
10958 * retrieve the current state, you should use {@link #getDrawableState}.
10959 *
10960 * @param extraSpace if non-zero, this is the number of extra entries you
10961 * would like in the returned array in which you can place your own
10962 * states.
10963 *
10964 * @return Returns an array holding the current {@link Drawable} state of
10965 * the view.
10966 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010967 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010968 */
10969 protected int[] onCreateDrawableState(int extraSpace) {
10970 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
10971 mParent instanceof View) {
10972 return ((View) mParent).onCreateDrawableState(extraSpace);
10973 }
10974
10975 int[] drawableState;
10976
10977 int privateFlags = mPrivateFlags;
10978
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010979 int viewStateIndex = 0;
10980 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
10981 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
10982 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070010983 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070010984 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
10985 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070010986 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
10987 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080010988 // This is set if HW acceleration is requested, even if the current
10989 // process doesn't allow it. This is just to allow app preview
10990 // windows to better match their app.
10991 viewStateIndex |= VIEW_STATE_ACCELERATED;
10992 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070010993 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010994
Christopher Tate3d4bf172011-03-28 16:16:46 -070010995 final int privateFlags2 = mPrivateFlags2;
10996 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
10997 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
10998
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010999 drawableState = VIEW_STATE_SETS[viewStateIndex];
11000
11001 //noinspection ConstantIfStatement
11002 if (false) {
11003 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11004 Log.i("View", toString()
11005 + " pressed=" + ((privateFlags & PRESSED) != 0)
11006 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11007 + " fo=" + hasFocus()
11008 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011009 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011010 + ": " + Arrays.toString(drawableState));
11011 }
11012
11013 if (extraSpace == 0) {
11014 return drawableState;
11015 }
11016
11017 final int[] fullState;
11018 if (drawableState != null) {
11019 fullState = new int[drawableState.length + extraSpace];
11020 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11021 } else {
11022 fullState = new int[extraSpace];
11023 }
11024
11025 return fullState;
11026 }
11027
11028 /**
11029 * Merge your own state values in <var>additionalState</var> into the base
11030 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011031 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011032 *
11033 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011034 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011035 * own additional state values.
11036 *
11037 * @param additionalState The additional state values you would like
11038 * added to <var>baseState</var>; this array is not modified.
11039 *
11040 * @return As a convenience, the <var>baseState</var> array you originally
11041 * passed into the function is returned.
11042 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011043 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011044 */
11045 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11046 final int N = baseState.length;
11047 int i = N - 1;
11048 while (i >= 0 && baseState[i] == 0) {
11049 i--;
11050 }
11051 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11052 return baseState;
11053 }
11054
11055 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011056 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11057 * on all Drawable objects associated with this view.
11058 */
11059 public void jumpDrawablesToCurrentState() {
11060 if (mBGDrawable != null) {
11061 mBGDrawable.jumpToCurrentState();
11062 }
11063 }
11064
11065 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 * Sets the background color for this view.
11067 * @param color the color of the background
11068 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011069 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011070 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011071 if (mBGDrawable instanceof ColorDrawable) {
11072 ((ColorDrawable) mBGDrawable).setColor(color);
11073 } else {
11074 setBackgroundDrawable(new ColorDrawable(color));
11075 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011076 }
11077
11078 /**
11079 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011080 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 * @param resid The identifier of the resource.
11082 * @attr ref android.R.styleable#View_background
11083 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011084 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011085 public void setBackgroundResource(int resid) {
11086 if (resid != 0 && resid == mBackgroundResource) {
11087 return;
11088 }
11089
11090 Drawable d= null;
11091 if (resid != 0) {
11092 d = mResources.getDrawable(resid);
11093 }
11094 setBackgroundDrawable(d);
11095
11096 mBackgroundResource = resid;
11097 }
11098
11099 /**
11100 * Set the background to a given Drawable, or remove the background. If the
11101 * background has padding, this View's padding is set to the background's
11102 * padding. However, when a background is removed, this View's padding isn't
11103 * touched. If setting the padding is desired, please use
11104 * {@link #setPadding(int, int, int, int)}.
11105 *
11106 * @param d The Drawable to use as the background, or null to remove the
11107 * background
11108 */
11109 public void setBackgroundDrawable(Drawable d) {
11110 boolean requestLayout = false;
11111
11112 mBackgroundResource = 0;
11113
11114 /*
11115 * Regardless of whether we're setting a new background or not, we want
11116 * to clear the previous drawable.
11117 */
11118 if (mBGDrawable != null) {
11119 mBGDrawable.setCallback(null);
11120 unscheduleDrawable(mBGDrawable);
11121 }
11122
11123 if (d != null) {
11124 Rect padding = sThreadLocal.get();
11125 if (padding == null) {
11126 padding = new Rect();
11127 sThreadLocal.set(padding);
11128 }
11129 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011130 switch (d.getResolvedLayoutDirectionSelf()) {
11131 case LAYOUT_DIRECTION_RTL:
11132 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11133 break;
11134 case LAYOUT_DIRECTION_LTR:
11135 default:
11136 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11137 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011138 }
11139
11140 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11141 // if it has a different minimum size, we should layout again
11142 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11143 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11144 requestLayout = true;
11145 }
11146
11147 d.setCallback(this);
11148 if (d.isStateful()) {
11149 d.setState(getDrawableState());
11150 }
11151 d.setVisible(getVisibility() == VISIBLE, false);
11152 mBGDrawable = d;
11153
11154 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11155 mPrivateFlags &= ~SKIP_DRAW;
11156 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11157 requestLayout = true;
11158 }
11159 } else {
11160 /* Remove the background */
11161 mBGDrawable = null;
11162
11163 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11164 /*
11165 * This view ONLY drew the background before and we're removing
11166 * the background, so now it won't draw anything
11167 * (hence we SKIP_DRAW)
11168 */
11169 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11170 mPrivateFlags |= SKIP_DRAW;
11171 }
11172
11173 /*
11174 * When the background is set, we try to apply its padding to this
11175 * View. When the background is removed, we don't touch this View's
11176 * padding. This is noted in the Javadocs. Hence, we don't need to
11177 * requestLayout(), the invalidate() below is sufficient.
11178 */
11179
11180 // The old background's minimum size could have affected this
11181 // View's layout, so let's requestLayout
11182 requestLayout = true;
11183 }
11184
Romain Guy8f1344f52009-05-15 16:03:59 -070011185 computeOpaqueFlags();
11186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011187 if (requestLayout) {
11188 requestLayout();
11189 }
11190
11191 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011192 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011193 }
11194
11195 /**
11196 * Gets the background drawable
11197 * @return The drawable used as the background for this view, if any.
11198 */
11199 public Drawable getBackground() {
11200 return mBGDrawable;
11201 }
11202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011203 /**
11204 * Sets the padding. The view may add on the space required to display
11205 * the scrollbars, depending on the style and visibility of the scrollbars.
11206 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11207 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11208 * from the values set in this call.
11209 *
11210 * @attr ref android.R.styleable#View_padding
11211 * @attr ref android.R.styleable#View_paddingBottom
11212 * @attr ref android.R.styleable#View_paddingLeft
11213 * @attr ref android.R.styleable#View_paddingRight
11214 * @attr ref android.R.styleable#View_paddingTop
11215 * @param left the left padding in pixels
11216 * @param top the top padding in pixels
11217 * @param right the right padding in pixels
11218 * @param bottom the bottom padding in pixels
11219 */
11220 public void setPadding(int left, int top, int right, int bottom) {
11221 boolean changed = false;
11222
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011223 mUserPaddingRelative = false;
11224
Adam Powell20232d02010-12-08 21:08:53 -080011225 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011226 mUserPaddingRight = right;
11227 mUserPaddingBottom = bottom;
11228
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011229 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011230
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011231 // Common case is there are no scroll bars.
11232 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011233 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011234 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011235 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011236 switch (mVerticalScrollbarPosition) {
11237 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011238 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11239 left += offset;
11240 } else {
11241 right += offset;
11242 }
11243 break;
Adam Powell20232d02010-12-08 21:08:53 -080011244 case SCROLLBAR_POSITION_RIGHT:
11245 right += offset;
11246 break;
11247 case SCROLLBAR_POSITION_LEFT:
11248 left += offset;
11249 break;
11250 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011251 }
Adam Powell20232d02010-12-08 21:08:53 -080011252 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011253 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11254 ? 0 : getHorizontalScrollbarHeight();
11255 }
11256 }
Romain Guy8506ab42009-06-11 17:35:47 -070011257
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011258 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 changed = true;
11260 mPaddingLeft = left;
11261 }
11262 if (mPaddingTop != top) {
11263 changed = true;
11264 mPaddingTop = top;
11265 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011266 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011268 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011269 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011270 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011272 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011273 }
11274
11275 if (changed) {
11276 requestLayout();
11277 }
11278 }
11279
11280 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011281 * Sets the relative padding. The view may add on the space required to display
11282 * the scrollbars, depending on the style and visibility of the scrollbars.
11283 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11284 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11285 * from the values set in this call.
11286 *
11287 * @attr ref android.R.styleable#View_padding
11288 * @attr ref android.R.styleable#View_paddingBottom
11289 * @attr ref android.R.styleable#View_paddingStart
11290 * @attr ref android.R.styleable#View_paddingEnd
11291 * @attr ref android.R.styleable#View_paddingTop
11292 * @param start the start padding in pixels
11293 * @param top the top padding in pixels
11294 * @param end the end padding in pixels
11295 * @param bottom the bottom padding in pixels
11296 *
11297 * @hide
11298 */
11299 public void setPaddingRelative(int start, int top, int end, int bottom) {
11300 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011301
11302 mUserPaddingStart = start;
11303 mUserPaddingEnd = end;
11304
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011305 switch(getResolvedLayoutDirection()) {
11306 case LAYOUT_DIRECTION_RTL:
11307 setPadding(end, top, start, bottom);
11308 break;
11309 case LAYOUT_DIRECTION_LTR:
11310 default:
11311 setPadding(start, top, end, bottom);
11312 }
11313 }
11314
11315 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011316 * Returns the top padding of this view.
11317 *
11318 * @return the top padding in pixels
11319 */
11320 public int getPaddingTop() {
11321 return mPaddingTop;
11322 }
11323
11324 /**
11325 * Returns the bottom padding of this view. If there are inset and enabled
11326 * scrollbars, this value may include the space required to display the
11327 * scrollbars as well.
11328 *
11329 * @return the bottom padding in pixels
11330 */
11331 public int getPaddingBottom() {
11332 return mPaddingBottom;
11333 }
11334
11335 /**
11336 * Returns the left padding of this view. If there are inset and enabled
11337 * scrollbars, this value may include the space required to display the
11338 * scrollbars as well.
11339 *
11340 * @return the left padding in pixels
11341 */
11342 public int getPaddingLeft() {
11343 return mPaddingLeft;
11344 }
11345
11346 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011347 * Returns the start padding of this view. If there are inset and enabled
11348 * scrollbars, this value may include the space required to display the
11349 * scrollbars as well.
11350 *
11351 * @return the start padding in pixels
11352 *
11353 * @hide
11354 */
11355 public int getPaddingStart() {
11356 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11357 mPaddingRight : mPaddingLeft;
11358 }
11359
11360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011361 * Returns the right padding of this view. If there are inset and enabled
11362 * scrollbars, this value may include the space required to display the
11363 * scrollbars as well.
11364 *
11365 * @return the right padding in pixels
11366 */
11367 public int getPaddingRight() {
11368 return mPaddingRight;
11369 }
11370
11371 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011372 * Returns the end padding of this view. If there are inset and enabled
11373 * scrollbars, this value may include the space required to display the
11374 * scrollbars as well.
11375 *
11376 * @return the end padding in pixels
11377 *
11378 * @hide
11379 */
11380 public int getPaddingEnd() {
11381 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11382 mPaddingLeft : mPaddingRight;
11383 }
11384
11385 /**
11386 * Return if the padding as been set thru relative values
11387 * {@link #setPaddingRelative(int, int, int, int)} or thru
11388 * @attr ref android.R.styleable#View_paddingStart or
11389 * @attr ref android.R.styleable#View_paddingEnd
11390 *
11391 * @return true if the padding is relative or false if it is not.
11392 *
11393 * @hide
11394 */
11395 public boolean isPaddingRelative() {
11396 return mUserPaddingRelative;
11397 }
11398
11399 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011400 * Changes the selection state of this view. A view can be selected or not.
11401 * Note that selection is not the same as focus. Views are typically
11402 * selected in the context of an AdapterView like ListView or GridView;
11403 * the selected view is the view that is highlighted.
11404 *
11405 * @param selected true if the view must be selected, false otherwise
11406 */
11407 public void setSelected(boolean selected) {
11408 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11409 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011410 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011411 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011412 refreshDrawableState();
11413 dispatchSetSelected(selected);
11414 }
11415 }
11416
11417 /**
11418 * Dispatch setSelected to all of this View's children.
11419 *
11420 * @see #setSelected(boolean)
11421 *
11422 * @param selected The new selected state
11423 */
11424 protected void dispatchSetSelected(boolean selected) {
11425 }
11426
11427 /**
11428 * Indicates the selection state of this view.
11429 *
11430 * @return true if the view is selected, false otherwise
11431 */
11432 @ViewDebug.ExportedProperty
11433 public boolean isSelected() {
11434 return (mPrivateFlags & SELECTED) != 0;
11435 }
11436
11437 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011438 * Changes the activated state of this view. A view can be activated or not.
11439 * Note that activation is not the same as selection. Selection is
11440 * a transient property, representing the view (hierarchy) the user is
11441 * currently interacting with. Activation is a longer-term state that the
11442 * user can move views in and out of. For example, in a list view with
11443 * single or multiple selection enabled, the views in the current selection
11444 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11445 * here.) The activated state is propagated down to children of the view it
11446 * is set on.
11447 *
11448 * @param activated true if the view must be activated, false otherwise
11449 */
11450 public void setActivated(boolean activated) {
11451 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11452 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011453 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011454 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011455 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011456 }
11457 }
11458
11459 /**
11460 * Dispatch setActivated to all of this View's children.
11461 *
11462 * @see #setActivated(boolean)
11463 *
11464 * @param activated The new activated state
11465 */
11466 protected void dispatchSetActivated(boolean activated) {
11467 }
11468
11469 /**
11470 * Indicates the activation state of this view.
11471 *
11472 * @return true if the view is activated, false otherwise
11473 */
11474 @ViewDebug.ExportedProperty
11475 public boolean isActivated() {
11476 return (mPrivateFlags & ACTIVATED) != 0;
11477 }
11478
11479 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011480 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11481 * observer can be used to get notifications when global events, like
11482 * layout, happen.
11483 *
11484 * The returned ViewTreeObserver observer is not guaranteed to remain
11485 * valid for the lifetime of this View. If the caller of this method keeps
11486 * a long-lived reference to ViewTreeObserver, it should always check for
11487 * the return value of {@link ViewTreeObserver#isAlive()}.
11488 *
11489 * @return The ViewTreeObserver for this view's hierarchy.
11490 */
11491 public ViewTreeObserver getViewTreeObserver() {
11492 if (mAttachInfo != null) {
11493 return mAttachInfo.mTreeObserver;
11494 }
11495 if (mFloatingTreeObserver == null) {
11496 mFloatingTreeObserver = new ViewTreeObserver();
11497 }
11498 return mFloatingTreeObserver;
11499 }
11500
11501 /**
11502 * <p>Finds the topmost view in the current view hierarchy.</p>
11503 *
11504 * @return the topmost view containing this view
11505 */
11506 public View getRootView() {
11507 if (mAttachInfo != null) {
11508 final View v = mAttachInfo.mRootView;
11509 if (v != null) {
11510 return v;
11511 }
11512 }
Romain Guy8506ab42009-06-11 17:35:47 -070011513
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011514 View parent = this;
11515
11516 while (parent.mParent != null && parent.mParent instanceof View) {
11517 parent = (View) parent.mParent;
11518 }
11519
11520 return parent;
11521 }
11522
11523 /**
11524 * <p>Computes the coordinates of this view on the screen. The argument
11525 * must be an array of two integers. After the method returns, the array
11526 * contains the x and y location in that order.</p>
11527 *
11528 * @param location an array of two integers in which to hold the coordinates
11529 */
11530 public void getLocationOnScreen(int[] location) {
11531 getLocationInWindow(location);
11532
11533 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011534 if (info != null) {
11535 location[0] += info.mWindowLeft;
11536 location[1] += info.mWindowTop;
11537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011538 }
11539
11540 /**
11541 * <p>Computes the coordinates of this view in its window. The argument
11542 * must be an array of two integers. After the method returns, the array
11543 * contains the x and y location in that order.</p>
11544 *
11545 * @param location an array of two integers in which to hold the coordinates
11546 */
11547 public void getLocationInWindow(int[] location) {
11548 if (location == null || location.length < 2) {
11549 throw new IllegalArgumentException("location must be an array of "
11550 + "two integers");
11551 }
11552
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011553 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11554 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011555
11556 ViewParent viewParent = mParent;
11557 while (viewParent instanceof View) {
11558 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011559 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11560 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011561 viewParent = view.mParent;
11562 }
Romain Guy8506ab42009-06-11 17:35:47 -070011563
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011564 if (viewParent instanceof ViewAncestor) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011565 // *cough*
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070011566 final ViewAncestor vr = (ViewAncestor)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011567 location[1] -= vr.mCurScrollY;
11568 }
11569 }
11570
11571 /**
11572 * {@hide}
11573 * @param id the id of the view to be found
11574 * @return the view of the specified id, null if cannot be found
11575 */
11576 protected View findViewTraversal(int id) {
11577 if (id == mID) {
11578 return this;
11579 }
11580 return null;
11581 }
11582
11583 /**
11584 * {@hide}
11585 * @param tag the tag of the view to be found
11586 * @return the view of specified tag, null if cannot be found
11587 */
11588 protected View findViewWithTagTraversal(Object tag) {
11589 if (tag != null && tag.equals(mTag)) {
11590 return this;
11591 }
11592 return null;
11593 }
11594
11595 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011596 * {@hide}
11597 * @param predicate The predicate to evaluate.
11598 * @return The first view that matches the predicate or null.
11599 */
11600 protected View findViewByPredicateTraversal(Predicate<View> predicate) {
11601 if (predicate.apply(this)) {
11602 return this;
11603 }
11604 return null;
11605 }
11606
11607 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011608 * Look for a child view with the given id. If this view has the given
11609 * id, return this view.
11610 *
11611 * @param id The id to search for.
11612 * @return The view that has the given id in the hierarchy or null
11613 */
11614 public final View findViewById(int id) {
11615 if (id < 0) {
11616 return null;
11617 }
11618 return findViewTraversal(id);
11619 }
11620
11621 /**
11622 * Look for a child view with the given tag. If this view has the given
11623 * tag, return this view.
11624 *
11625 * @param tag The tag to search for, using "tag.equals(getTag())".
11626 * @return The View that has the given tag in the hierarchy or null
11627 */
11628 public final View findViewWithTag(Object tag) {
11629 if (tag == null) {
11630 return null;
11631 }
11632 return findViewWithTagTraversal(tag);
11633 }
11634
11635 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011636 * {@hide}
11637 * Look for a child view that matches the specified predicate.
11638 * If this view matches the predicate, return this view.
11639 *
11640 * @param predicate The predicate to evaluate.
11641 * @return The first view that matches the predicate or null.
11642 */
11643 public final View findViewByPredicate(Predicate<View> predicate) {
11644 return findViewByPredicateTraversal(predicate);
11645 }
11646
11647 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011648 * Sets the identifier for this view. The identifier does not have to be
11649 * unique in this view's hierarchy. The identifier should be a positive
11650 * number.
11651 *
11652 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011653 * @see #getId()
11654 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011655 *
11656 * @param id a number used to identify the view
11657 *
11658 * @attr ref android.R.styleable#View_id
11659 */
11660 public void setId(int id) {
11661 mID = id;
11662 }
11663
11664 /**
11665 * {@hide}
11666 *
11667 * @param isRoot true if the view belongs to the root namespace, false
11668 * otherwise
11669 */
11670 public void setIsRootNamespace(boolean isRoot) {
11671 if (isRoot) {
11672 mPrivateFlags |= IS_ROOT_NAMESPACE;
11673 } else {
11674 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11675 }
11676 }
11677
11678 /**
11679 * {@hide}
11680 *
11681 * @return true if the view belongs to the root namespace, false otherwise
11682 */
11683 public boolean isRootNamespace() {
11684 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11685 }
11686
11687 /**
11688 * Returns this view's identifier.
11689 *
11690 * @return a positive integer used to identify the view or {@link #NO_ID}
11691 * if the view has no ID
11692 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011693 * @see #setId(int)
11694 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011695 * @attr ref android.R.styleable#View_id
11696 */
11697 @ViewDebug.CapturedViewProperty
11698 public int getId() {
11699 return mID;
11700 }
11701
11702 /**
11703 * Returns this view's tag.
11704 *
11705 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011706 *
11707 * @see #setTag(Object)
11708 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011709 */
11710 @ViewDebug.ExportedProperty
11711 public Object getTag() {
11712 return mTag;
11713 }
11714
11715 /**
11716 * Sets the tag associated with this view. A tag can be used to mark
11717 * a view in its hierarchy and does not have to be unique within the
11718 * hierarchy. Tags can also be used to store data within a view without
11719 * resorting to another data structure.
11720 *
11721 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011722 *
11723 * @see #getTag()
11724 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011725 */
11726 public void setTag(final Object tag) {
11727 mTag = tag;
11728 }
11729
11730 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011731 * Returns the tag associated with this view and the specified key.
11732 *
11733 * @param key The key identifying the tag
11734 *
11735 * @return the Object stored in this view as a tag
11736 *
11737 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011738 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011739 */
11740 public Object getTag(int key) {
11741 SparseArray<Object> tags = null;
11742 synchronized (sTagsLock) {
11743 if (sTags != null) {
11744 tags = sTags.get(this);
11745 }
11746 }
11747
11748 if (tags != null) return tags.get(key);
11749 return null;
11750 }
11751
11752 /**
11753 * Sets a tag associated with this view and a key. A tag can be used
11754 * to mark a view in its hierarchy and does not have to be unique within
11755 * the hierarchy. Tags can also be used to store data within a view
11756 * without resorting to another data structure.
11757 *
11758 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011759 * application to ensure it is unique (see the <a
11760 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11761 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011762 * the Android framework or not associated with any package will cause
11763 * an {@link IllegalArgumentException} to be thrown.
11764 *
11765 * @param key The key identifying the tag
11766 * @param tag An Object to tag the view with
11767 *
11768 * @throws IllegalArgumentException If they specified key is not valid
11769 *
11770 * @see #setTag(Object)
11771 * @see #getTag(int)
11772 */
11773 public void setTag(int key, final Object tag) {
11774 // If the package id is 0x00 or 0x01, it's either an undefined package
11775 // or a framework id
11776 if ((key >>> 24) < 2) {
11777 throw new IllegalArgumentException("The key must be an application-specific "
11778 + "resource id.");
11779 }
11780
11781 setTagInternal(this, key, tag);
11782 }
11783
11784 /**
11785 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
11786 * framework id.
11787 *
11788 * @hide
11789 */
11790 public void setTagInternal(int key, Object tag) {
11791 if ((key >>> 24) != 0x1) {
11792 throw new IllegalArgumentException("The key must be a framework-specific "
11793 + "resource id.");
11794 }
11795
Romain Guy8506ab42009-06-11 17:35:47 -070011796 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070011797 }
11798
11799 private static void setTagInternal(View view, int key, Object tag) {
11800 SparseArray<Object> tags = null;
11801 synchronized (sTagsLock) {
11802 if (sTags == null) {
11803 sTags = new WeakHashMap<View, SparseArray<Object>>();
11804 } else {
11805 tags = sTags.get(view);
11806 }
11807 }
11808
11809 if (tags == null) {
11810 tags = new SparseArray<Object>(2);
11811 synchronized (sTagsLock) {
11812 sTags.put(view, tags);
11813 }
11814 }
11815
11816 tags.put(key, tag);
11817 }
11818
11819 /**
Romain Guy13922e02009-05-12 17:56:14 -070011820 * @param consistency The type of consistency. See ViewDebug for more information.
11821 *
11822 * @hide
11823 */
11824 protected boolean dispatchConsistencyCheck(int consistency) {
11825 return onConsistencyCheck(consistency);
11826 }
11827
11828 /**
11829 * Method that subclasses should implement to check their consistency. The type of
11830 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070011831 *
Romain Guy13922e02009-05-12 17:56:14 -070011832 * @param consistency The type of consistency. See ViewDebug for more information.
11833 *
11834 * @throws IllegalStateException if the view is in an inconsistent state.
11835 *
11836 * @hide
11837 */
11838 protected boolean onConsistencyCheck(int consistency) {
11839 boolean result = true;
11840
11841 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
11842 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
11843
11844 if (checkLayout) {
11845 if (getParent() == null) {
11846 result = false;
11847 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11848 "View " + this + " does not have a parent.");
11849 }
11850
11851 if (mAttachInfo == null) {
11852 result = false;
11853 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11854 "View " + this + " is not attached to a window.");
11855 }
11856 }
11857
11858 if (checkDrawing) {
11859 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
11860 // from their draw() method
11861
11862 if ((mPrivateFlags & DRAWN) != DRAWN &&
11863 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
11864 result = false;
11865 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
11866 "View " + this + " was invalidated but its drawing cache is valid.");
11867 }
11868 }
11869
11870 return result;
11871 }
11872
11873 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011874 * Prints information about this view in the log output, with the tag
11875 * {@link #VIEW_LOG_TAG}.
11876 *
11877 * @hide
11878 */
11879 public void debug() {
11880 debug(0);
11881 }
11882
11883 /**
11884 * Prints information about this view in the log output, with the tag
11885 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
11886 * indentation defined by the <code>depth</code>.
11887 *
11888 * @param depth the indentation level
11889 *
11890 * @hide
11891 */
11892 protected void debug(int depth) {
11893 String output = debugIndent(depth - 1);
11894
11895 output += "+ " + this;
11896 int id = getId();
11897 if (id != -1) {
11898 output += " (id=" + id + ")";
11899 }
11900 Object tag = getTag();
11901 if (tag != null) {
11902 output += " (tag=" + tag + ")";
11903 }
11904 Log.d(VIEW_LOG_TAG, output);
11905
11906 if ((mPrivateFlags & FOCUSED) != 0) {
11907 output = debugIndent(depth) + " FOCUSED";
11908 Log.d(VIEW_LOG_TAG, output);
11909 }
11910
11911 output = debugIndent(depth);
11912 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
11913 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
11914 + "} ";
11915 Log.d(VIEW_LOG_TAG, output);
11916
11917 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
11918 || mPaddingBottom != 0) {
11919 output = debugIndent(depth);
11920 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
11921 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
11922 Log.d(VIEW_LOG_TAG, output);
11923 }
11924
11925 output = debugIndent(depth);
11926 output += "mMeasureWidth=" + mMeasuredWidth +
11927 " mMeasureHeight=" + mMeasuredHeight;
11928 Log.d(VIEW_LOG_TAG, output);
11929
11930 output = debugIndent(depth);
11931 if (mLayoutParams == null) {
11932 output += "BAD! no layout params";
11933 } else {
11934 output = mLayoutParams.debug(output);
11935 }
11936 Log.d(VIEW_LOG_TAG, output);
11937
11938 output = debugIndent(depth);
11939 output += "flags={";
11940 output += View.printFlags(mViewFlags);
11941 output += "}";
11942 Log.d(VIEW_LOG_TAG, output);
11943
11944 output = debugIndent(depth);
11945 output += "privateFlags={";
11946 output += View.printPrivateFlags(mPrivateFlags);
11947 output += "}";
11948 Log.d(VIEW_LOG_TAG, output);
11949 }
11950
11951 /**
11952 * Creates an string of whitespaces used for indentation.
11953 *
11954 * @param depth the indentation level
11955 * @return a String containing (depth * 2 + 3) * 2 white spaces
11956 *
11957 * @hide
11958 */
11959 protected static String debugIndent(int depth) {
11960 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
11961 for (int i = 0; i < (depth * 2) + 3; i++) {
11962 spaces.append(' ').append(' ');
11963 }
11964 return spaces.toString();
11965 }
11966
11967 /**
11968 * <p>Return the offset of the widget's text baseline from the widget's top
11969 * boundary. If this widget does not support baseline alignment, this
11970 * method returns -1. </p>
11971 *
11972 * @return the offset of the baseline within the widget's bounds or -1
11973 * if baseline alignment is not supported
11974 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070011975 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011976 public int getBaseline() {
11977 return -1;
11978 }
11979
11980 /**
11981 * Call this when something has changed which has invalidated the
11982 * layout of this view. This will schedule a layout pass of the view
11983 * tree.
11984 */
11985 public void requestLayout() {
11986 if (ViewDebug.TRACE_HIERARCHY) {
11987 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
11988 }
11989
11990 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080011991 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011992
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070011993 if (mLayoutParams != null && mParent != null) {
Fabrice Di Megliob76023a2011-06-20 17:41:21 -070011994 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
11995 }
11996
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011997 if (mParent != null && !mParent.isLayoutRequested()) {
11998 mParent.requestLayout();
11999 }
12000 }
12001
12002 /**
12003 * Forces this view to be laid out during the next layout pass.
12004 * This method does not call requestLayout() or forceLayout()
12005 * on the parent.
12006 */
12007 public void forceLayout() {
12008 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012009 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012010 }
12011
12012 /**
12013 * <p>
12014 * This is called to find out how big a view should be. The parent
12015 * supplies constraint information in the width and height parameters.
12016 * </p>
12017 *
12018 * <p>
12019 * The actual mesurement work of a view is performed in
12020 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12021 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12022 * </p>
12023 *
12024 *
12025 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12026 * parent
12027 * @param heightMeasureSpec Vertical space requirements as imposed by the
12028 * parent
12029 *
12030 * @see #onMeasure(int, int)
12031 */
12032 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12033 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12034 widthMeasureSpec != mOldWidthMeasureSpec ||
12035 heightMeasureSpec != mOldHeightMeasureSpec) {
12036
12037 // first clears the measured dimension flag
12038 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12039
12040 if (ViewDebug.TRACE_HIERARCHY) {
12041 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12042 }
12043
12044 // measure ourselves, this should set the measured dimension flag back
12045 onMeasure(widthMeasureSpec, heightMeasureSpec);
12046
12047 // flag not set, setMeasuredDimension() was not invoked, we raise
12048 // an exception to warn the developer
12049 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12050 throw new IllegalStateException("onMeasure() did not set the"
12051 + " measured dimension by calling"
12052 + " setMeasuredDimension()");
12053 }
12054
12055 mPrivateFlags |= LAYOUT_REQUIRED;
12056 }
12057
12058 mOldWidthMeasureSpec = widthMeasureSpec;
12059 mOldHeightMeasureSpec = heightMeasureSpec;
12060 }
12061
12062 /**
12063 * <p>
12064 * Measure the view and its content to determine the measured width and the
12065 * measured height. This method is invoked by {@link #measure(int, int)} and
12066 * should be overriden by subclasses to provide accurate and efficient
12067 * measurement of their contents.
12068 * </p>
12069 *
12070 * <p>
12071 * <strong>CONTRACT:</strong> When overriding this method, you
12072 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12073 * measured width and height of this view. Failure to do so will trigger an
12074 * <code>IllegalStateException</code>, thrown by
12075 * {@link #measure(int, int)}. Calling the superclass'
12076 * {@link #onMeasure(int, int)} is a valid use.
12077 * </p>
12078 *
12079 * <p>
12080 * The base class implementation of measure defaults to the background size,
12081 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12082 * override {@link #onMeasure(int, int)} to provide better measurements of
12083 * their content.
12084 * </p>
12085 *
12086 * <p>
12087 * If this method is overridden, it is the subclass's responsibility to make
12088 * sure the measured height and width are at least the view's minimum height
12089 * and width ({@link #getSuggestedMinimumHeight()} and
12090 * {@link #getSuggestedMinimumWidth()}).
12091 * </p>
12092 *
12093 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12094 * The requirements are encoded with
12095 * {@link android.view.View.MeasureSpec}.
12096 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12097 * The requirements are encoded with
12098 * {@link android.view.View.MeasureSpec}.
12099 *
12100 * @see #getMeasuredWidth()
12101 * @see #getMeasuredHeight()
12102 * @see #setMeasuredDimension(int, int)
12103 * @see #getSuggestedMinimumHeight()
12104 * @see #getSuggestedMinimumWidth()
12105 * @see android.view.View.MeasureSpec#getMode(int)
12106 * @see android.view.View.MeasureSpec#getSize(int)
12107 */
12108 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12109 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12110 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12111 }
12112
12113 /**
12114 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12115 * measured width and measured height. Failing to do so will trigger an
12116 * exception at measurement time.</p>
12117 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012118 * @param measuredWidth The measured width of this view. May be a complex
12119 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12120 * {@link #MEASURED_STATE_TOO_SMALL}.
12121 * @param measuredHeight The measured height of this view. May be a complex
12122 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12123 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012124 */
12125 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12126 mMeasuredWidth = measuredWidth;
12127 mMeasuredHeight = measuredHeight;
12128
12129 mPrivateFlags |= MEASURED_DIMENSION_SET;
12130 }
12131
12132 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012133 * Merge two states as returned by {@link #getMeasuredState()}.
12134 * @param curState The current state as returned from a view or the result
12135 * of combining multiple views.
12136 * @param newState The new view state to combine.
12137 * @return Returns a new integer reflecting the combination of the two
12138 * states.
12139 */
12140 public static int combineMeasuredStates(int curState, int newState) {
12141 return curState | newState;
12142 }
12143
12144 /**
12145 * Version of {@link #resolveSizeAndState(int, int, int)}
12146 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12147 */
12148 public static int resolveSize(int size, int measureSpec) {
12149 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12150 }
12151
12152 /**
12153 * Utility to reconcile a desired size and state, with constraints imposed
12154 * by a MeasureSpec. Will take the desired size, unless a different size
12155 * is imposed by the constraints. The returned value is a compound integer,
12156 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12157 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12158 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012159 *
12160 * @param size How big the view wants to be
12161 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012162 * @return Size information bit mask as defined by
12163 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012164 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012165 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012166 int result = size;
12167 int specMode = MeasureSpec.getMode(measureSpec);
12168 int specSize = MeasureSpec.getSize(measureSpec);
12169 switch (specMode) {
12170 case MeasureSpec.UNSPECIFIED:
12171 result = size;
12172 break;
12173 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012174 if (specSize < size) {
12175 result = specSize | MEASURED_STATE_TOO_SMALL;
12176 } else {
12177 result = size;
12178 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012179 break;
12180 case MeasureSpec.EXACTLY:
12181 result = specSize;
12182 break;
12183 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012184 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012185 }
12186
12187 /**
12188 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012189 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012190 * by the MeasureSpec.
12191 *
12192 * @param size Default size for this view
12193 * @param measureSpec Constraints imposed by the parent
12194 * @return The size this view should be.
12195 */
12196 public static int getDefaultSize(int size, int measureSpec) {
12197 int result = size;
12198 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012199 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012200
12201 switch (specMode) {
12202 case MeasureSpec.UNSPECIFIED:
12203 result = size;
12204 break;
12205 case MeasureSpec.AT_MOST:
12206 case MeasureSpec.EXACTLY:
12207 result = specSize;
12208 break;
12209 }
12210 return result;
12211 }
12212
12213 /**
12214 * Returns the suggested minimum height that the view should use. This
12215 * returns the maximum of the view's minimum height
12216 * and the background's minimum height
12217 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12218 * <p>
12219 * When being used in {@link #onMeasure(int, int)}, the caller should still
12220 * ensure the returned height is within the requirements of the parent.
12221 *
12222 * @return The suggested minimum height of the view.
12223 */
12224 protected int getSuggestedMinimumHeight() {
12225 int suggestedMinHeight = mMinHeight;
12226
12227 if (mBGDrawable != null) {
12228 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12229 if (suggestedMinHeight < bgMinHeight) {
12230 suggestedMinHeight = bgMinHeight;
12231 }
12232 }
12233
12234 return suggestedMinHeight;
12235 }
12236
12237 /**
12238 * Returns the suggested minimum width that the view should use. This
12239 * returns the maximum of the view's minimum width)
12240 * and the background's minimum width
12241 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12242 * <p>
12243 * When being used in {@link #onMeasure(int, int)}, the caller should still
12244 * ensure the returned width is within the requirements of the parent.
12245 *
12246 * @return The suggested minimum width of the view.
12247 */
12248 protected int getSuggestedMinimumWidth() {
12249 int suggestedMinWidth = mMinWidth;
12250
12251 if (mBGDrawable != null) {
12252 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12253 if (suggestedMinWidth < bgMinWidth) {
12254 suggestedMinWidth = bgMinWidth;
12255 }
12256 }
12257
12258 return suggestedMinWidth;
12259 }
12260
12261 /**
12262 * Sets the minimum height of the view. It is not guaranteed the view will
12263 * be able to achieve this minimum height (for example, if its parent layout
12264 * constrains it with less available height).
12265 *
12266 * @param minHeight The minimum height the view will try to be.
12267 */
12268 public void setMinimumHeight(int minHeight) {
12269 mMinHeight = minHeight;
12270 }
12271
12272 /**
12273 * Sets the minimum width of the view. It is not guaranteed the view will
12274 * be able to achieve this minimum width (for example, if its parent layout
12275 * constrains it with less available width).
12276 *
12277 * @param minWidth The minimum width the view will try to be.
12278 */
12279 public void setMinimumWidth(int minWidth) {
12280 mMinWidth = minWidth;
12281 }
12282
12283 /**
12284 * Get the animation currently associated with this view.
12285 *
12286 * @return The animation that is currently playing or
12287 * scheduled to play for this view.
12288 */
12289 public Animation getAnimation() {
12290 return mCurrentAnimation;
12291 }
12292
12293 /**
12294 * Start the specified animation now.
12295 *
12296 * @param animation the animation to start now
12297 */
12298 public void startAnimation(Animation animation) {
12299 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12300 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012301 invalidateParentCaches();
12302 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012303 }
12304
12305 /**
12306 * Cancels any animations for this view.
12307 */
12308 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012309 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012310 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012312 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012313 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012314 }
12315
12316 /**
12317 * Sets the next animation to play for this view.
12318 * If you want the animation to play immediately, use
12319 * startAnimation. This method provides allows fine-grained
12320 * control over the start time and invalidation, but you
12321 * must make sure that 1) the animation has a start time set, and
12322 * 2) the view will be invalidated when the animation is supposed to
12323 * start.
12324 *
12325 * @param animation The next animation, or null.
12326 */
12327 public void setAnimation(Animation animation) {
12328 mCurrentAnimation = animation;
12329 if (animation != null) {
12330 animation.reset();
12331 }
12332 }
12333
12334 /**
12335 * Invoked by a parent ViewGroup to notify the start of the animation
12336 * currently associated with this view. If you override this method,
12337 * always call super.onAnimationStart();
12338 *
12339 * @see #setAnimation(android.view.animation.Animation)
12340 * @see #getAnimation()
12341 */
12342 protected void onAnimationStart() {
12343 mPrivateFlags |= ANIMATION_STARTED;
12344 }
12345
12346 /**
12347 * Invoked by a parent ViewGroup to notify the end of the animation
12348 * currently associated with this view. If you override this method,
12349 * always call super.onAnimationEnd();
12350 *
12351 * @see #setAnimation(android.view.animation.Animation)
12352 * @see #getAnimation()
12353 */
12354 protected void onAnimationEnd() {
12355 mPrivateFlags &= ~ANIMATION_STARTED;
12356 }
12357
12358 /**
12359 * Invoked if there is a Transform that involves alpha. Subclass that can
12360 * draw themselves with the specified alpha should return true, and then
12361 * respect that alpha when their onDraw() is called. If this returns false
12362 * then the view may be redirected to draw into an offscreen buffer to
12363 * fulfill the request, which will look fine, but may be slower than if the
12364 * subclass handles it internally. The default implementation returns false.
12365 *
12366 * @param alpha The alpha (0..255) to apply to the view's drawing
12367 * @return true if the view can draw with the specified alpha.
12368 */
12369 protected boolean onSetAlpha(int alpha) {
12370 return false;
12371 }
12372
12373 /**
12374 * This is used by the RootView to perform an optimization when
12375 * the view hierarchy contains one or several SurfaceView.
12376 * SurfaceView is always considered transparent, but its children are not,
12377 * therefore all View objects remove themselves from the global transparent
12378 * region (passed as a parameter to this function).
12379 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012380 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012381 *
12382 * @return Returns true if the effective visibility of the view at this
12383 * point is opaque, regardless of the transparent region; returns false
12384 * if it is possible for underlying windows to be seen behind the view.
12385 *
12386 * {@hide}
12387 */
12388 public boolean gatherTransparentRegion(Region region) {
12389 final AttachInfo attachInfo = mAttachInfo;
12390 if (region != null && attachInfo != null) {
12391 final int pflags = mPrivateFlags;
12392 if ((pflags & SKIP_DRAW) == 0) {
12393 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12394 // remove it from the transparent region.
12395 final int[] location = attachInfo.mTransparentLocation;
12396 getLocationInWindow(location);
12397 region.op(location[0], location[1], location[0] + mRight - mLeft,
12398 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12399 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12400 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12401 // exists, so we remove the background drawable's non-transparent
12402 // parts from this transparent region.
12403 applyDrawableToTransparentRegion(mBGDrawable, region);
12404 }
12405 }
12406 return true;
12407 }
12408
12409 /**
12410 * Play a sound effect for this view.
12411 *
12412 * <p>The framework will play sound effects for some built in actions, such as
12413 * clicking, but you may wish to play these effects in your widget,
12414 * for instance, for internal navigation.
12415 *
12416 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12417 * {@link #isSoundEffectsEnabled()} is true.
12418 *
12419 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12420 */
12421 public void playSoundEffect(int soundConstant) {
12422 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12423 return;
12424 }
12425 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12426 }
12427
12428 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012429 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012430 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012431 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012432 *
12433 * <p>The framework will provide haptic feedback for some built in actions,
12434 * such as long presses, but you may wish to provide feedback for your
12435 * own widget.
12436 *
12437 * <p>The feedback will only be performed if
12438 * {@link #isHapticFeedbackEnabled()} is true.
12439 *
12440 * @param feedbackConstant One of the constants defined in
12441 * {@link HapticFeedbackConstants}
12442 */
12443 public boolean performHapticFeedback(int feedbackConstant) {
12444 return performHapticFeedback(feedbackConstant, 0);
12445 }
12446
12447 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012448 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012449 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012450 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012451 *
12452 * @param feedbackConstant One of the constants defined in
12453 * {@link HapticFeedbackConstants}
12454 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12455 */
12456 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12457 if (mAttachInfo == null) {
12458 return false;
12459 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012460 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012461 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012462 && !isHapticFeedbackEnabled()) {
12463 return false;
12464 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012465 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12466 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012467 }
12468
12469 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012470 * Request that the visibility of the status bar be changed.
Scott Mainec6331b2011-05-24 16:55:56 -070012471 * @param visibility Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012472 */
12473 public void setSystemUiVisibility(int visibility) {
12474 if (visibility != mSystemUiVisibility) {
12475 mSystemUiVisibility = visibility;
12476 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12477 mParent.recomputeViewAttributes(this);
12478 }
12479 }
12480 }
12481
12482 /**
12483 * Returns the status bar visibility that this view has requested.
Scott Mainec6331b2011-05-24 16:55:56 -070012484 * @return Either {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
Joe Onorato664644d2011-01-23 17:53:23 -080012485 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012486 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012487 return mSystemUiVisibility;
12488 }
12489
Scott Mainec6331b2011-05-24 16:55:56 -070012490 /**
12491 * Set a listener to receive callbacks when the visibility of the system bar changes.
12492 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12493 */
Joe Onorato664644d2011-01-23 17:53:23 -080012494 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12495 mOnSystemUiVisibilityChangeListener = l;
12496 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12497 mParent.recomputeViewAttributes(this);
12498 }
12499 }
12500
12501 /**
12502 */
12503 public void dispatchSystemUiVisibilityChanged(int visibility) {
12504 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012505 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012506 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012507 }
12508 }
12509
12510 /**
Joe Malin32736f02011-01-19 16:14:20 -080012511 * Creates an image that the system displays during the drag and drop
12512 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12513 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12514 * appearance as the given View. The default also positions the center of the drag shadow
12515 * directly under the touch point. If no View is provided (the constructor with no parameters
12516 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12517 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12518 * default is an invisible drag shadow.
12519 * <p>
12520 * You are not required to use the View you provide to the constructor as the basis of the
12521 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12522 * anything you want as the drag shadow.
12523 * </p>
12524 * <p>
12525 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12526 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12527 * size and position of the drag shadow. It uses this data to construct a
12528 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12529 * so that your application can draw the shadow image in the Canvas.
12530 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012531 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012532 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012533 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012534
12535 /**
Joe Malin32736f02011-01-19 16:14:20 -080012536 * Constructs a shadow image builder based on a View. By default, the resulting drag
12537 * shadow will have the same appearance and dimensions as the View, with the touch point
12538 * over the center of the View.
12539 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012540 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012541 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012542 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012543 }
12544
Christopher Tate17ed60c2011-01-18 12:50:26 -080012545 /**
12546 * Construct a shadow builder object with no associated View. This
12547 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12548 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12549 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012550 * reference to any View object. If they are not overridden, then the result is an
12551 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012552 */
12553 public DragShadowBuilder() {
12554 mView = new WeakReference<View>(null);
12555 }
12556
12557 /**
12558 * Returns the View object that had been passed to the
12559 * {@link #View.DragShadowBuilder(View)}
12560 * constructor. If that View parameter was {@code null} or if the
12561 * {@link #View.DragShadowBuilder()}
12562 * constructor was used to instantiate the builder object, this method will return
12563 * null.
12564 *
12565 * @return The View object associate with this builder object.
12566 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012567 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012568 final public View getView() {
12569 return mView.get();
12570 }
12571
Christopher Tate2c095f32010-10-04 14:13:40 -070012572 /**
Joe Malin32736f02011-01-19 16:14:20 -080012573 * Provides the metrics for the shadow image. These include the dimensions of
12574 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012575 * be centered under the touch location while dragging.
12576 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012577 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012578 * same as the dimensions of the View itself and centers the shadow under
12579 * the touch point.
12580 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012581 *
Joe Malin32736f02011-01-19 16:14:20 -080012582 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12583 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12584 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12585 * image.
12586 *
12587 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12588 * shadow image that should be underneath the touch point during the drag and drop
12589 * operation. Your application must set {@link android.graphics.Point#x} to the
12590 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012591 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012592 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012593 final View view = mView.get();
12594 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012595 shadowSize.set(view.getWidth(), view.getHeight());
12596 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012597 } else {
12598 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12599 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012600 }
12601
12602 /**
Joe Malin32736f02011-01-19 16:14:20 -080012603 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12604 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012605 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012606 *
Joe Malin32736f02011-01-19 16:14:20 -080012607 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012608 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012609 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012610 final View view = mView.get();
12611 if (view != null) {
12612 view.draw(canvas);
12613 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012614 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012615 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012616 }
12617 }
12618
12619 /**
Joe Malin32736f02011-01-19 16:14:20 -080012620 * Starts a drag and drop operation. When your application calls this method, it passes a
12621 * {@link android.view.View.DragShadowBuilder} object to the system. The
12622 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12623 * to get metrics for the drag shadow, and then calls the object's
12624 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12625 * <p>
12626 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12627 * drag events to all the View objects in your application that are currently visible. It does
12628 * this either by calling the View object's drag listener (an implementation of
12629 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12630 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12631 * Both are passed a {@link android.view.DragEvent} object that has a
12632 * {@link android.view.DragEvent#getAction()} value of
12633 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12634 * </p>
12635 * <p>
12636 * Your application can invoke startDrag() on any attached View object. The View object does not
12637 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12638 * be related to the View the user selected for dragging.
12639 * </p>
12640 * @param data A {@link android.content.ClipData} object pointing to the data to be
12641 * transferred by the drag and drop operation.
12642 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12643 * drag shadow.
12644 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12645 * drop operation. This Object is put into every DragEvent object sent by the system during the
12646 * current drag.
12647 * <p>
12648 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12649 * to the target Views. For example, it can contain flags that differentiate between a
12650 * a copy operation and a move operation.
12651 * </p>
12652 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12653 * so the parameter should be set to 0.
12654 * @return {@code true} if the method completes successfully, or
12655 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12656 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012657 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012658 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012659 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012660 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012661 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012662 }
12663 boolean okay = false;
12664
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012665 Point shadowSize = new Point();
12666 Point shadowTouchPoint = new Point();
12667 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012668
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012669 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12670 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12671 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012672 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012673
Chris Tatea32dcf72010-10-14 12:13:50 -070012674 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012675 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12676 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012677 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012678 Surface surface = new Surface();
12679 try {
12680 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012681 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012682 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012683 + " surface=" + surface);
12684 if (token != null) {
12685 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012686 try {
Chris Tate6b391282010-10-14 15:48:59 -070012687 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012688 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012689 } finally {
12690 surface.unlockCanvasAndPost(canvas);
12691 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012692
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012693 final ViewAncestor root = getViewAncestor();
Christopher Tate407b4e92010-11-30 17:14:08 -080012694
12695 // Cache the local state object for delivery with DragEvents
12696 root.setLocalDragState(myLocalState);
12697
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012698 // repurpose 'shadowSize' for the last touch point
12699 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012700
Christopher Tatea53146c2010-09-07 11:57:52 -070012701 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012702 shadowSize.x, shadowSize.y,
12703 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012704 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012705 }
12706 } catch (Exception e) {
12707 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12708 surface.destroy();
12709 }
12710
12711 return okay;
12712 }
12713
Christopher Tatea53146c2010-09-07 11:57:52 -070012714 /**
Joe Malin32736f02011-01-19 16:14:20 -080012715 * Handles drag events sent by the system following a call to
12716 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12717 *<p>
12718 * When the system calls this method, it passes a
12719 * {@link android.view.DragEvent} object. A call to
12720 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12721 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12722 * operation.
12723 * @param event The {@link android.view.DragEvent} sent by the system.
12724 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12725 * in DragEvent, indicating the type of drag event represented by this object.
12726 * @return {@code true} if the method was successful, otherwise {@code false}.
12727 * <p>
12728 * The method should return {@code true} in response to an action type of
12729 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12730 * operation.
12731 * </p>
12732 * <p>
12733 * The method should also return {@code true} in response to an action type of
12734 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12735 * {@code false} if it didn't.
12736 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012737 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012738 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012739 return false;
12740 }
12741
12742 /**
Joe Malin32736f02011-01-19 16:14:20 -080012743 * Detects if this View is enabled and has a drag event listener.
12744 * If both are true, then it calls the drag event listener with the
12745 * {@link android.view.DragEvent} it received. If the drag event listener returns
12746 * {@code true}, then dispatchDragEvent() returns {@code true}.
12747 * <p>
12748 * For all other cases, the method calls the
12749 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12750 * method and returns its result.
12751 * </p>
12752 * <p>
12753 * This ensures that a drag event is always consumed, even if the View does not have a drag
12754 * event listener. However, if the View has a listener and the listener returns true, then
12755 * onDragEvent() is not called.
12756 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012757 */
12758 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012759 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012760 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12761 && mOnDragListener.onDrag(this, event)) {
12762 return true;
12763 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012764 return onDragEvent(event);
12765 }
12766
Christopher Tate3d4bf172011-03-28 16:16:46 -070012767 boolean canAcceptDrag() {
12768 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12769 }
12770
Christopher Tatea53146c2010-09-07 11:57:52 -070012771 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070012772 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
12773 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070012774 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070012775 */
12776 public void onCloseSystemDialogs(String reason) {
12777 }
Joe Malin32736f02011-01-19 16:14:20 -080012778
Dianne Hackbornffa42482009-09-23 22:20:11 -070012779 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012780 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070012781 * update a Region being computed for
12782 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012783 * that any non-transparent parts of the Drawable are removed from the
12784 * given transparent region.
12785 *
12786 * @param dr The Drawable whose transparency is to be applied to the region.
12787 * @param region A Region holding the current transparency information,
12788 * where any parts of the region that are set are considered to be
12789 * transparent. On return, this region will be modified to have the
12790 * transparency information reduced by the corresponding parts of the
12791 * Drawable that are not transparent.
12792 * {@hide}
12793 */
12794 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
12795 if (DBG) {
12796 Log.i("View", "Getting transparent region for: " + this);
12797 }
12798 final Region r = dr.getTransparentRegion();
12799 final Rect db = dr.getBounds();
12800 final AttachInfo attachInfo = mAttachInfo;
12801 if (r != null && attachInfo != null) {
12802 final int w = getRight()-getLeft();
12803 final int h = getBottom()-getTop();
12804 if (db.left > 0) {
12805 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
12806 r.op(0, 0, db.left, h, Region.Op.UNION);
12807 }
12808 if (db.right < w) {
12809 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
12810 r.op(db.right, 0, w, h, Region.Op.UNION);
12811 }
12812 if (db.top > 0) {
12813 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
12814 r.op(0, 0, w, db.top, Region.Op.UNION);
12815 }
12816 if (db.bottom < h) {
12817 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
12818 r.op(0, db.bottom, w, h, Region.Op.UNION);
12819 }
12820 final int[] location = attachInfo.mTransparentLocation;
12821 getLocationInWindow(location);
12822 r.translate(location[0], location[1]);
12823 region.op(r, Region.Op.INTERSECT);
12824 } else {
12825 region.op(db, Region.Op.DIFFERENCE);
12826 }
12827 }
12828
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012829 private void checkForLongClick(int delayOffset) {
12830 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
12831 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012832
Patrick Dubroye0a799a2011-05-04 16:19:22 -070012833 if (mPendingCheckForLongPress == null) {
12834 mPendingCheckForLongPress = new CheckForLongPress();
12835 }
12836 mPendingCheckForLongPress.rememberWindowAttachCount();
12837 postDelayed(mPendingCheckForLongPress,
12838 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012839 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012840 }
12841
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012842 /**
12843 * Inflate a view from an XML resource. This convenience method wraps the {@link
12844 * LayoutInflater} class, which provides a full range of options for view inflation.
12845 *
12846 * @param context The Context object for your activity or application.
12847 * @param resource The resource ID to inflate
12848 * @param root A view group that will be the parent. Used to properly inflate the
12849 * layout_* parameters.
12850 * @see LayoutInflater
12851 */
12852 public static View inflate(Context context, int resource, ViewGroup root) {
12853 LayoutInflater factory = LayoutInflater.from(context);
12854 return factory.inflate(resource, root);
12855 }
Romain Guy33e72ae2010-07-17 12:40:29 -070012856
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012857 /**
Adam Powell637d3372010-08-25 14:37:03 -070012858 * Scroll the view with standard behavior for scrolling beyond the normal
12859 * content boundaries. Views that call this method should override
12860 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
12861 * results of an over-scroll operation.
12862 *
12863 * Views can use this method to handle any touch or fling-based scrolling.
12864 *
12865 * @param deltaX Change in X in pixels
12866 * @param deltaY Change in Y in pixels
12867 * @param scrollX Current X scroll value in pixels before applying deltaX
12868 * @param scrollY Current Y scroll value in pixels before applying deltaY
12869 * @param scrollRangeX Maximum content scroll range along the X axis
12870 * @param scrollRangeY Maximum content scroll range along the Y axis
12871 * @param maxOverScrollX Number of pixels to overscroll by in either direction
12872 * along the X axis.
12873 * @param maxOverScrollY Number of pixels to overscroll by in either direction
12874 * along the Y axis.
12875 * @param isTouchEvent true if this scroll operation is the result of a touch event.
12876 * @return true if scrolling was clamped to an over-scroll boundary along either
12877 * axis, false otherwise.
12878 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070012879 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070012880 protected boolean overScrollBy(int deltaX, int deltaY,
12881 int scrollX, int scrollY,
12882 int scrollRangeX, int scrollRangeY,
12883 int maxOverScrollX, int maxOverScrollY,
12884 boolean isTouchEvent) {
12885 final int overScrollMode = mOverScrollMode;
12886 final boolean canScrollHorizontal =
12887 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
12888 final boolean canScrollVertical =
12889 computeVerticalScrollRange() > computeVerticalScrollExtent();
12890 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12891 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
12892 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
12893 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
12894
12895 int newScrollX = scrollX + deltaX;
12896 if (!overScrollHorizontal) {
12897 maxOverScrollX = 0;
12898 }
12899
12900 int newScrollY = scrollY + deltaY;
12901 if (!overScrollVertical) {
12902 maxOverScrollY = 0;
12903 }
12904
12905 // Clamp values if at the limits and record
12906 final int left = -maxOverScrollX;
12907 final int right = maxOverScrollX + scrollRangeX;
12908 final int top = -maxOverScrollY;
12909 final int bottom = maxOverScrollY + scrollRangeY;
12910
12911 boolean clampedX = false;
12912 if (newScrollX > right) {
12913 newScrollX = right;
12914 clampedX = true;
12915 } else if (newScrollX < left) {
12916 newScrollX = left;
12917 clampedX = true;
12918 }
12919
12920 boolean clampedY = false;
12921 if (newScrollY > bottom) {
12922 newScrollY = bottom;
12923 clampedY = true;
12924 } else if (newScrollY < top) {
12925 newScrollY = top;
12926 clampedY = true;
12927 }
12928
12929 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
12930
12931 return clampedX || clampedY;
12932 }
12933
12934 /**
12935 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
12936 * respond to the results of an over-scroll operation.
12937 *
12938 * @param scrollX New X scroll value in pixels
12939 * @param scrollY New Y scroll value in pixels
12940 * @param clampedX True if scrollX was clamped to an over-scroll boundary
12941 * @param clampedY True if scrollY was clamped to an over-scroll boundary
12942 */
12943 protected void onOverScrolled(int scrollX, int scrollY,
12944 boolean clampedX, boolean clampedY) {
12945 // Intentionally empty.
12946 }
12947
12948 /**
12949 * Returns the over-scroll mode for this view. The result will be
12950 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12951 * (allow over-scrolling only if the view content is larger than the container),
12952 * or {@link #OVER_SCROLL_NEVER}.
12953 *
12954 * @return This view's over-scroll mode.
12955 */
12956 public int getOverScrollMode() {
12957 return mOverScrollMode;
12958 }
12959
12960 /**
12961 * Set the over-scroll mode for this view. Valid over-scroll modes are
12962 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
12963 * (allow over-scrolling only if the view content is larger than the container),
12964 * or {@link #OVER_SCROLL_NEVER}.
12965 *
12966 * Setting the over-scroll mode of a view will have an effect only if the
12967 * view is capable of scrolling.
12968 *
12969 * @param overScrollMode The new over-scroll mode for this view.
12970 */
12971 public void setOverScrollMode(int overScrollMode) {
12972 if (overScrollMode != OVER_SCROLL_ALWAYS &&
12973 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
12974 overScrollMode != OVER_SCROLL_NEVER) {
12975 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
12976 }
12977 mOverScrollMode = overScrollMode;
12978 }
12979
12980 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080012981 * Gets a scale factor that determines the distance the view should scroll
12982 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
12983 * @return The vertical scroll scale factor.
12984 * @hide
12985 */
12986 protected float getVerticalScrollFactor() {
12987 if (mVerticalScrollFactor == 0) {
12988 TypedValue outValue = new TypedValue();
12989 if (!mContext.getTheme().resolveAttribute(
12990 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
12991 throw new IllegalStateException(
12992 "Expected theme to define listPreferredItemHeight.");
12993 }
12994 mVerticalScrollFactor = outValue.getDimension(
12995 mContext.getResources().getDisplayMetrics());
12996 }
12997 return mVerticalScrollFactor;
12998 }
12999
13000 /**
13001 * Gets a scale factor that determines the distance the view should scroll
13002 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13003 * @return The horizontal scroll scale factor.
13004 * @hide
13005 */
13006 protected float getHorizontalScrollFactor() {
13007 // TODO: Should use something else.
13008 return getVerticalScrollFactor();
13009 }
13010
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013011 /**
13012 * Return the value specifying the text direction or policy that was set with
13013 * {@link #setTextDirection(int)}.
13014 *
13015 * @return the defined text direction. It can be one of:
13016 *
13017 * {@link #TEXT_DIRECTION_INHERIT},
13018 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13019 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013020 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013021 * {@link #TEXT_DIRECTION_LTR},
13022 * {@link #TEXT_DIRECTION_RTL},
13023 *
13024 * @hide
13025 */
13026 public int getTextDirection() {
13027 return mTextDirection;
13028 }
13029
13030 /**
13031 * Set the text direction.
13032 *
13033 * @param textDirection the direction to set. Should be one of:
13034 *
13035 * {@link #TEXT_DIRECTION_INHERIT},
13036 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13037 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013038 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013039 * {@link #TEXT_DIRECTION_LTR},
13040 * {@link #TEXT_DIRECTION_RTL},
13041 *
13042 * @hide
13043 */
13044 public void setTextDirection(int textDirection) {
13045 if (textDirection != mTextDirection) {
13046 mTextDirection = textDirection;
13047 resetResolvedTextDirection();
13048 requestLayout();
13049 }
13050 }
13051
13052 /**
13053 * Return the resolved text direction.
13054 *
13055 * @return the resolved text direction. Return one of:
13056 *
13057 * {@link #TEXT_DIRECTION_LTR},
13058 * {@link #TEXT_DIRECTION_RTL},
13059 *
13060 * @hide
13061 */
13062 public int getResolvedTextDirection() {
13063 if (!isResolvedTextDirection()) {
13064 resolveTextDirection();
13065 }
13066 return mResolvedTextDirection;
13067 }
13068
13069 /**
13070 * Resolve the text direction. Classes that extend View and want to do a specific text direction
13071 * resolution will need to implement this method and set the mResolvedTextDirection to
13072 * either TEXT_DIRECTION_LTR if direction is LTR or TEXT_DIRECTION_RTL if
13073 * direction is RTL.
13074 */
13075 protected void resolveTextDirection() {
13076 }
13077
13078 /**
13079 * Return if the text direction has been resolved or not.
13080 *
13081 * @return true, if resolved and false if not resolved
13082 *
13083 * @hide
13084 */
13085 public boolean isResolvedTextDirection() {
13086 return (mResolvedTextDirection != TEXT_DIRECTION_UNDEFINED);
13087 }
13088
13089 /**
13090 * Reset resolved text direction. Will be resolved during a call to getResolvedLayoutDirection().
13091 */
13092 protected void resetResolvedTextDirection() {
13093 mResolvedTextDirection = TEXT_DIRECTION_UNDEFINED;
13094 }
13095
Chet Haaseb39f0512011-05-24 14:36:40 -070013096 //
13097 // Properties
13098 //
13099 /**
13100 * A Property wrapper around the <code>alpha</code> functionality handled by the
13101 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13102 */
Romain Guy02ccac62011-06-24 13:20:23 -070013103 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013104 @Override
13105 public void setValue(View object, float value) {
13106 object.setAlpha(value);
13107 }
13108
13109 @Override
13110 public Float get(View object) {
13111 return object.getAlpha();
13112 }
13113 };
13114
13115 /**
13116 * A Property wrapper around the <code>translationX</code> functionality handled by the
13117 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13118 */
13119 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13120 @Override
13121 public void setValue(View object, float value) {
13122 object.setTranslationX(value);
13123 }
13124
13125 @Override
13126 public Float get(View object) {
13127 return object.getTranslationX();
13128 }
13129 };
13130
13131 /**
13132 * A Property wrapper around the <code>translationY</code> functionality handled by the
13133 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13134 */
13135 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13136 @Override
13137 public void setValue(View object, float value) {
13138 object.setTranslationY(value);
13139 }
13140
13141 @Override
13142 public Float get(View object) {
13143 return object.getTranslationY();
13144 }
13145 };
13146
13147 /**
13148 * A Property wrapper around the <code>x</code> functionality handled by the
13149 * {@link View#setX(float)} and {@link View#getX()} methods.
13150 */
13151 public static Property<View, Float> X = new FloatProperty<View>("x") {
13152 @Override
13153 public void setValue(View object, float value) {
13154 object.setX(value);
13155 }
13156
13157 @Override
13158 public Float get(View object) {
13159 return object.getX();
13160 }
13161 };
13162
13163 /**
13164 * A Property wrapper around the <code>y</code> functionality handled by the
13165 * {@link View#setY(float)} and {@link View#getY()} methods.
13166 */
13167 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13168 @Override
13169 public void setValue(View object, float value) {
13170 object.setY(value);
13171 }
13172
13173 @Override
13174 public Float get(View object) {
13175 return object.getY();
13176 }
13177 };
13178
13179 /**
13180 * A Property wrapper around the <code>rotation</code> functionality handled by the
13181 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13182 */
13183 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13184 @Override
13185 public void setValue(View object, float value) {
13186 object.setRotation(value);
13187 }
13188
13189 @Override
13190 public Float get(View object) {
13191 return object.getRotation();
13192 }
13193 };
13194
13195 /**
13196 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13197 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13198 */
13199 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13200 @Override
13201 public void setValue(View object, float value) {
13202 object.setRotationX(value);
13203 }
13204
13205 @Override
13206 public Float get(View object) {
13207 return object.getRotationX();
13208 }
13209 };
13210
13211 /**
13212 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13213 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13214 */
13215 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13216 @Override
13217 public void setValue(View object, float value) {
13218 object.setRotationY(value);
13219 }
13220
13221 @Override
13222 public Float get(View object) {
13223 return object.getRotationY();
13224 }
13225 };
13226
13227 /**
13228 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13229 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13230 */
13231 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13232 @Override
13233 public void setValue(View object, float value) {
13234 object.setScaleX(value);
13235 }
13236
13237 @Override
13238 public Float get(View object) {
13239 return object.getScaleX();
13240 }
13241 };
13242
13243 /**
13244 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13245 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13246 */
13247 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13248 @Override
13249 public void setValue(View object, float value) {
13250 object.setScaleY(value);
13251 }
13252
13253 @Override
13254 public Float get(View object) {
13255 return object.getScaleY();
13256 }
13257 };
13258
Jeff Brown33bbfd22011-02-24 20:55:35 -080013259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013260 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13261 * Each MeasureSpec represents a requirement for either the width or the height.
13262 * A MeasureSpec is comprised of a size and a mode. There are three possible
13263 * modes:
13264 * <dl>
13265 * <dt>UNSPECIFIED</dt>
13266 * <dd>
13267 * The parent has not imposed any constraint on the child. It can be whatever size
13268 * it wants.
13269 * </dd>
13270 *
13271 * <dt>EXACTLY</dt>
13272 * <dd>
13273 * The parent has determined an exact size for the child. The child is going to be
13274 * given those bounds regardless of how big it wants to be.
13275 * </dd>
13276 *
13277 * <dt>AT_MOST</dt>
13278 * <dd>
13279 * The child can be as large as it wants up to the specified size.
13280 * </dd>
13281 * </dl>
13282 *
13283 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13284 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13285 */
13286 public static class MeasureSpec {
13287 private static final int MODE_SHIFT = 30;
13288 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13289
13290 /**
13291 * Measure specification mode: The parent has not imposed any constraint
13292 * on the child. It can be whatever size it wants.
13293 */
13294 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13295
13296 /**
13297 * Measure specification mode: The parent has determined an exact size
13298 * for the child. The child is going to be given those bounds regardless
13299 * of how big it wants to be.
13300 */
13301 public static final int EXACTLY = 1 << MODE_SHIFT;
13302
13303 /**
13304 * Measure specification mode: The child can be as large as it wants up
13305 * to the specified size.
13306 */
13307 public static final int AT_MOST = 2 << MODE_SHIFT;
13308
13309 /**
13310 * Creates a measure specification based on the supplied size and mode.
13311 *
13312 * The mode must always be one of the following:
13313 * <ul>
13314 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13315 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13316 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13317 * </ul>
13318 *
13319 * @param size the size of the measure specification
13320 * @param mode the mode of the measure specification
13321 * @return the measure specification based on size and mode
13322 */
13323 public static int makeMeasureSpec(int size, int mode) {
13324 return size + mode;
13325 }
13326
13327 /**
13328 * Extracts the mode from the supplied measure specification.
13329 *
13330 * @param measureSpec the measure specification to extract the mode from
13331 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13332 * {@link android.view.View.MeasureSpec#AT_MOST} or
13333 * {@link android.view.View.MeasureSpec#EXACTLY}
13334 */
13335 public static int getMode(int measureSpec) {
13336 return (measureSpec & MODE_MASK);
13337 }
13338
13339 /**
13340 * Extracts the size from the supplied measure specification.
13341 *
13342 * @param measureSpec the measure specification to extract the size from
13343 * @return the size in pixels defined in the supplied measure specification
13344 */
13345 public static int getSize(int measureSpec) {
13346 return (measureSpec & ~MODE_MASK);
13347 }
13348
13349 /**
13350 * Returns a String representation of the specified measure
13351 * specification.
13352 *
13353 * @param measureSpec the measure specification to convert to a String
13354 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13355 */
13356 public static String toString(int measureSpec) {
13357 int mode = getMode(measureSpec);
13358 int size = getSize(measureSpec);
13359
13360 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13361
13362 if (mode == UNSPECIFIED)
13363 sb.append("UNSPECIFIED ");
13364 else if (mode == EXACTLY)
13365 sb.append("EXACTLY ");
13366 else if (mode == AT_MOST)
13367 sb.append("AT_MOST ");
13368 else
13369 sb.append(mode).append(" ");
13370
13371 sb.append(size);
13372 return sb.toString();
13373 }
13374 }
13375
13376 class CheckForLongPress implements Runnable {
13377
13378 private int mOriginalWindowAttachCount;
13379
13380 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013381 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013382 && mOriginalWindowAttachCount == mWindowAttachCount) {
13383 if (performLongClick()) {
13384 mHasPerformedLongPress = true;
13385 }
13386 }
13387 }
13388
13389 public void rememberWindowAttachCount() {
13390 mOriginalWindowAttachCount = mWindowAttachCount;
13391 }
13392 }
Joe Malin32736f02011-01-19 16:14:20 -080013393
Adam Powelle14579b2009-12-16 18:39:52 -080013394 private final class CheckForTap implements Runnable {
13395 public void run() {
13396 mPrivateFlags &= ~PREPRESSED;
13397 mPrivateFlags |= PRESSED;
13398 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013399 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013400 }
13401 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013402
Adam Powella35d7682010-03-12 14:48:13 -080013403 private final class PerformClick implements Runnable {
13404 public void run() {
13405 performClick();
13406 }
13407 }
13408
Dianne Hackborn63042d62011-01-26 18:56:29 -080013409 /** @hide */
13410 public void hackTurnOffWindowResizeAnim(boolean off) {
13411 mAttachInfo.mTurnOffWindowResizeAnim = off;
13412 }
Joe Malin32736f02011-01-19 16:14:20 -080013413
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013414 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013415 * This method returns a ViewPropertyAnimator object, which can be used to animate
13416 * specific properties on this View.
13417 *
13418 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13419 */
13420 public ViewPropertyAnimator animate() {
13421 if (mAnimator == null) {
13422 mAnimator = new ViewPropertyAnimator(this);
13423 }
13424 return mAnimator;
13425 }
13426
13427 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013428 * Interface definition for a callback to be invoked when a key event is
13429 * dispatched to this view. The callback will be invoked before the key
13430 * event is given to the view.
13431 */
13432 public interface OnKeyListener {
13433 /**
13434 * Called when a key is dispatched to a view. This allows listeners to
13435 * get a chance to respond before the target view.
13436 *
13437 * @param v The view the key has been dispatched to.
13438 * @param keyCode The code for the physical key that was pressed
13439 * @param event The KeyEvent object containing full information about
13440 * the event.
13441 * @return True if the listener has consumed the event, false otherwise.
13442 */
13443 boolean onKey(View v, int keyCode, KeyEvent event);
13444 }
13445
13446 /**
13447 * Interface definition for a callback to be invoked when a touch event is
13448 * dispatched to this view. The callback will be invoked before the touch
13449 * event is given to the view.
13450 */
13451 public interface OnTouchListener {
13452 /**
13453 * Called when a touch event is dispatched to a view. This allows listeners to
13454 * get a chance to respond before the target view.
13455 *
13456 * @param v The view the touch event has been dispatched to.
13457 * @param event The MotionEvent object containing full information about
13458 * the event.
13459 * @return True if the listener has consumed the event, false otherwise.
13460 */
13461 boolean onTouch(View v, MotionEvent event);
13462 }
13463
13464 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013465 * Interface definition for a callback to be invoked when a hover event is
13466 * dispatched to this view. The callback will be invoked before the hover
13467 * event is given to the view.
13468 */
13469 public interface OnHoverListener {
13470 /**
13471 * Called when a hover event is dispatched to a view. This allows listeners to
13472 * get a chance to respond before the target view.
13473 *
13474 * @param v The view the hover event has been dispatched to.
13475 * @param event The MotionEvent object containing full information about
13476 * the event.
13477 * @return True if the listener has consumed the event, false otherwise.
13478 */
13479 boolean onHover(View v, MotionEvent event);
13480 }
13481
13482 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013483 * Interface definition for a callback to be invoked when a generic motion event is
13484 * dispatched to this view. The callback will be invoked before the generic motion
13485 * event is given to the view.
13486 */
13487 public interface OnGenericMotionListener {
13488 /**
13489 * Called when a generic motion event is dispatched to a view. This allows listeners to
13490 * get a chance to respond before the target view.
13491 *
13492 * @param v The view the generic motion event has been dispatched to.
13493 * @param event The MotionEvent object containing full information about
13494 * the event.
13495 * @return True if the listener has consumed the event, false otherwise.
13496 */
13497 boolean onGenericMotion(View v, MotionEvent event);
13498 }
13499
13500 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013501 * Interface definition for a callback to be invoked when a view has been clicked and held.
13502 */
13503 public interface OnLongClickListener {
13504 /**
13505 * Called when a view has been clicked and held.
13506 *
13507 * @param v The view that was clicked and held.
13508 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013509 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013510 */
13511 boolean onLongClick(View v);
13512 }
13513
13514 /**
Chris Tate32affef2010-10-18 15:29:21 -070013515 * Interface definition for a callback to be invoked when a drag is being dispatched
13516 * to this view. The callback will be invoked before the hosting view's own
13517 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13518 * onDrag(event) behavior, it should return 'false' from this callback.
13519 */
13520 public interface OnDragListener {
13521 /**
13522 * Called when a drag event is dispatched to a view. This allows listeners
13523 * to get a chance to override base View behavior.
13524 *
Joe Malin32736f02011-01-19 16:14:20 -080013525 * @param v The View that received the drag event.
13526 * @param event The {@link android.view.DragEvent} object for the drag event.
13527 * @return {@code true} if the drag event was handled successfully, or {@code false}
13528 * if the drag event was not handled. Note that {@code false} will trigger the View
13529 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013530 */
13531 boolean onDrag(View v, DragEvent event);
13532 }
13533
13534 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013535 * Interface definition for a callback to be invoked when the focus state of
13536 * a view changed.
13537 */
13538 public interface OnFocusChangeListener {
13539 /**
13540 * Called when the focus state of a view has changed.
13541 *
13542 * @param v The view whose state has changed.
13543 * @param hasFocus The new focus state of v.
13544 */
13545 void onFocusChange(View v, boolean hasFocus);
13546 }
13547
13548 /**
13549 * Interface definition for a callback to be invoked when a view is clicked.
13550 */
13551 public interface OnClickListener {
13552 /**
13553 * Called when a view has been clicked.
13554 *
13555 * @param v The view that was clicked.
13556 */
13557 void onClick(View v);
13558 }
13559
13560 /**
13561 * Interface definition for a callback to be invoked when the context menu
13562 * for this view is being built.
13563 */
13564 public interface OnCreateContextMenuListener {
13565 /**
13566 * Called when the context menu for this view is being built. It is not
13567 * safe to hold onto the menu after this method returns.
13568 *
13569 * @param menu The context menu that is being built
13570 * @param v The view for which the context menu is being built
13571 * @param menuInfo Extra information about the item for which the
13572 * context menu should be shown. This information will vary
13573 * depending on the class of v.
13574 */
13575 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13576 }
13577
Joe Onorato664644d2011-01-23 17:53:23 -080013578 /**
13579 * Interface definition for a callback to be invoked when the status bar changes
13580 * visibility.
13581 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013582 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013583 */
13584 public interface OnSystemUiVisibilityChangeListener {
13585 /**
13586 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013587 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013588 *
13589 * @param visibility {@link #STATUS_BAR_VISIBLE} or {@link #STATUS_BAR_HIDDEN}.
13590 */
13591 public void onSystemUiVisibilityChange(int visibility);
13592 }
13593
Adam Powell4afd62b2011-02-18 15:02:18 -080013594 /**
13595 * Interface definition for a callback to be invoked when this view is attached
13596 * or detached from its window.
13597 */
13598 public interface OnAttachStateChangeListener {
13599 /**
13600 * Called when the view is attached to a window.
13601 * @param v The view that was attached
13602 */
13603 public void onViewAttachedToWindow(View v);
13604 /**
13605 * Called when the view is detached from a window.
13606 * @param v The view that was detached
13607 */
13608 public void onViewDetachedFromWindow(View v);
13609 }
13610
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013611 private final class UnsetPressedState implements Runnable {
13612 public void run() {
13613 setPressed(false);
13614 }
13615 }
13616
13617 /**
13618 * Base class for derived classes that want to save and restore their own
13619 * state in {@link android.view.View#onSaveInstanceState()}.
13620 */
13621 public static class BaseSavedState extends AbsSavedState {
13622 /**
13623 * Constructor used when reading from a parcel. Reads the state of the superclass.
13624 *
13625 * @param source
13626 */
13627 public BaseSavedState(Parcel source) {
13628 super(source);
13629 }
13630
13631 /**
13632 * Constructor called by derived classes when creating their SavedState objects
13633 *
13634 * @param superState The state of the superclass of this view
13635 */
13636 public BaseSavedState(Parcelable superState) {
13637 super(superState);
13638 }
13639
13640 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13641 new Parcelable.Creator<BaseSavedState>() {
13642 public BaseSavedState createFromParcel(Parcel in) {
13643 return new BaseSavedState(in);
13644 }
13645
13646 public BaseSavedState[] newArray(int size) {
13647 return new BaseSavedState[size];
13648 }
13649 };
13650 }
13651
13652 /**
13653 * A set of information given to a view when it is attached to its parent
13654 * window.
13655 */
13656 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013657 interface Callbacks {
13658 void playSoundEffect(int effectId);
13659 boolean performHapticFeedback(int effectId, boolean always);
13660 }
13661
13662 /**
13663 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13664 * to a Handler. This class contains the target (View) to invalidate and
13665 * the coordinates of the dirty rectangle.
13666 *
13667 * For performance purposes, this class also implements a pool of up to
13668 * POOL_LIMIT objects that get reused. This reduces memory allocations
13669 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013670 */
Romain Guyd928d682009-03-31 17:52:16 -070013671 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013672 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013673 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13674 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013675 public InvalidateInfo newInstance() {
13676 return new InvalidateInfo();
13677 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013678
Romain Guyd928d682009-03-31 17:52:16 -070013679 public void onAcquired(InvalidateInfo element) {
13680 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013681
Romain Guyd928d682009-03-31 17:52:16 -070013682 public void onReleased(InvalidateInfo element) {
13683 }
13684 }, POOL_LIMIT)
13685 );
13686
13687 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013688 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013689
13690 View target;
13691
13692 int left;
13693 int top;
13694 int right;
13695 int bottom;
13696
Romain Guyd928d682009-03-31 17:52:16 -070013697 public void setNextPoolable(InvalidateInfo element) {
13698 mNext = element;
13699 }
13700
13701 public InvalidateInfo getNextPoolable() {
13702 return mNext;
13703 }
13704
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013705 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013706 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013707 }
13708
13709 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013710 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013711 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013712
13713 public boolean isPooled() {
13714 return mIsPooled;
13715 }
13716
13717 public void setPooled(boolean isPooled) {
13718 mIsPooled = isPooled;
13719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013720 }
13721
13722 final IWindowSession mSession;
13723
13724 final IWindow mWindow;
13725
13726 final IBinder mWindowToken;
13727
13728 final Callbacks mRootCallbacks;
13729
Romain Guy59a12ca2011-06-09 17:48:21 -070013730 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013732 /**
13733 * The top view of the hierarchy.
13734 */
13735 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013737 IBinder mPanelParentWindowToken;
13738 Surface mSurface;
13739
Romain Guyb051e892010-09-28 19:09:36 -070013740 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013741 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013742 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013744 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013745 * Scale factor used by the compatibility mode
13746 */
13747 float mApplicationScale;
13748
13749 /**
13750 * Indicates whether the application is in compatibility mode
13751 */
13752 boolean mScalingRequired;
13753
13754 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013755 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013756 */
13757 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013758
Dianne Hackborn63042d62011-01-26 18:56:29 -080013759 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013760 * Left position of this view's window
13761 */
13762 int mWindowLeft;
13763
13764 /**
13765 * Top position of this view's window
13766 */
13767 int mWindowTop;
13768
13769 /**
Adam Powell26153a32010-11-08 15:22:27 -080013770 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070013771 */
Adam Powell26153a32010-11-08 15:22:27 -080013772 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070013773
13774 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013775 * For windows that are full-screen but using insets to layout inside
13776 * of the screen decorations, these are the current insets for the
13777 * content of the window.
13778 */
13779 final Rect mContentInsets = new Rect();
13780
13781 /**
13782 * For windows that are full-screen but using insets to layout inside
13783 * of the screen decorations, these are the current insets for the
13784 * actual visible parts of the window.
13785 */
13786 final Rect mVisibleInsets = new Rect();
13787
13788 /**
13789 * The internal insets given by this window. This value is
13790 * supplied by the client (through
13791 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
13792 * be given to the window manager when changed to be used in laying
13793 * out windows behind it.
13794 */
13795 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
13796 = new ViewTreeObserver.InternalInsetsInfo();
13797
13798 /**
13799 * All views in the window's hierarchy that serve as scroll containers,
13800 * used to determine if the window can be resized or must be panned
13801 * to adjust for a soft input area.
13802 */
13803 final ArrayList<View> mScrollContainers = new ArrayList<View>();
13804
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070013805 final KeyEvent.DispatcherState mKeyDispatchState
13806 = new KeyEvent.DispatcherState();
13807
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013808 /**
13809 * Indicates whether the view's window currently has the focus.
13810 */
13811 boolean mHasWindowFocus;
13812
13813 /**
13814 * The current visibility of the window.
13815 */
13816 int mWindowVisibility;
13817
13818 /**
13819 * Indicates the time at which drawing started to occur.
13820 */
13821 long mDrawingTime;
13822
13823 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070013824 * Indicates whether or not ignoring the DIRTY_MASK flags.
13825 */
13826 boolean mIgnoreDirtyState;
13827
13828 /**
Romain Guy02ccac62011-06-24 13:20:23 -070013829 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
13830 * to avoid clearing that flag prematurely.
13831 */
13832 boolean mSetIgnoreDirtyState = false;
13833
13834 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013835 * Indicates whether the view's window is currently in touch mode.
13836 */
13837 boolean mInTouchMode;
13838
13839 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013840 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013841 * the next time it performs a traversal
13842 */
13843 boolean mRecomputeGlobalAttributes;
13844
13845 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013846 * Set during a traveral if any views want to keep the screen on.
13847 */
13848 boolean mKeepScreenOn;
13849
13850 /**
Joe Onorato664644d2011-01-23 17:53:23 -080013851 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
13852 */
13853 int mSystemUiVisibility;
13854
13855 /**
13856 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
13857 * attached.
13858 */
13859 boolean mHasSystemUiListeners;
13860
13861 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013862 * Set if the visibility of any views has changed.
13863 */
13864 boolean mViewVisibilityChanged;
13865
13866 /**
13867 * Set to true if a view has been scrolled.
13868 */
13869 boolean mViewScrollChanged;
13870
13871 /**
13872 * Global to the view hierarchy used as a temporary for dealing with
13873 * x/y points in the transparent region computations.
13874 */
13875 final int[] mTransparentLocation = new int[2];
13876
13877 /**
13878 * Global to the view hierarchy used as a temporary for dealing with
13879 * x/y points in the ViewGroup.invalidateChild implementation.
13880 */
13881 final int[] mInvalidateChildLocation = new int[2];
13882
Chet Haasec3aa3612010-06-17 08:50:37 -070013883
13884 /**
13885 * Global to the view hierarchy used as a temporary for dealing with
13886 * x/y location when view is transformed.
13887 */
13888 final float[] mTmpTransformLocation = new float[2];
13889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013890 /**
13891 * The view tree observer used to dispatch global events like
13892 * layout, pre-draw, touch mode change, etc.
13893 */
13894 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
13895
13896 /**
13897 * A Canvas used by the view hierarchy to perform bitmap caching.
13898 */
13899 Canvas mCanvas;
13900
13901 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013902 * A Handler supplied by a view's {@link android.view.ViewAncestor}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013903 * handler can be used to pump events in the UI events queue.
13904 */
13905 final Handler mHandler;
13906
13907 /**
13908 * Identifier for messages requesting the view to be invalidated.
13909 * Such messages should be sent to {@link #mHandler}.
13910 */
13911 static final int INVALIDATE_MSG = 0x1;
13912
13913 /**
13914 * Identifier for messages requesting the view to invalidate a region.
13915 * Such messages should be sent to {@link #mHandler}.
13916 */
13917 static final int INVALIDATE_RECT_MSG = 0x2;
13918
13919 /**
13920 * Temporary for use in computing invalidate rectangles while
13921 * calling up the hierarchy.
13922 */
13923 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070013924
13925 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070013926 * Temporary for use in computing hit areas with transformed views
13927 */
13928 final RectF mTmpTransformRect = new RectF();
13929
13930 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070013931 * Temporary list for use in collecting focusable descendents of a view.
13932 */
13933 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
13934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013935 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013936 * The id of the window for accessibility purposes.
13937 */
13938 int mAccessibilityWindowId = View.NO_ID;
13939
13940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013941 * Creates a new set of attachment information with the specified
13942 * events handler and thread.
13943 *
13944 * @param handler the events handler the view must use
13945 */
13946 AttachInfo(IWindowSession session, IWindow window,
13947 Handler handler, Callbacks effectPlayer) {
13948 mSession = session;
13949 mWindow = window;
13950 mWindowToken = window.asBinder();
13951 mHandler = handler;
13952 mRootCallbacks = effectPlayer;
13953 }
13954 }
13955
13956 /**
13957 * <p>ScrollabilityCache holds various fields used by a View when scrolling
13958 * is supported. This avoids keeping too many unused fields in most
13959 * instances of View.</p>
13960 */
Mike Cleronf116bf82009-09-27 19:14:12 -070013961 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080013962
Mike Cleronf116bf82009-09-27 19:14:12 -070013963 /**
13964 * Scrollbars are not visible
13965 */
13966 public static final int OFF = 0;
13967
13968 /**
13969 * Scrollbars are visible
13970 */
13971 public static final int ON = 1;
13972
13973 /**
13974 * Scrollbars are fading away
13975 */
13976 public static final int FADING = 2;
13977
13978 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080013979
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013980 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070013981 public int scrollBarDefaultDelayBeforeFade;
13982 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013983
13984 public int scrollBarSize;
13985 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070013986 public float[] interpolatorValues;
13987 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013988
13989 public final Paint paint;
13990 public final Matrix matrix;
13991 public Shader shader;
13992
Mike Cleronf116bf82009-09-27 19:14:12 -070013993 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
13994
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080013995 private static final float[] OPAQUE = { 255 };
13996 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080013997
Mike Cleronf116bf82009-09-27 19:14:12 -070013998 /**
13999 * When fading should start. This time moves into the future every time
14000 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14001 */
14002 public long fadeStartTime;
14003
14004
14005 /**
14006 * The current state of the scrollbars: ON, OFF, or FADING
14007 */
14008 public int state = OFF;
14009
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014010 private int mLastColor;
14011
Mike Cleronf116bf82009-09-27 19:14:12 -070014012 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014013 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14014 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014015 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14016 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014017
14018 paint = new Paint();
14019 matrix = new Matrix();
14020 // use use a height of 1, and then wack the matrix each time we
14021 // actually use it.
14022 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014023
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014024 paint.setShader(shader);
14025 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014026 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014027 }
Romain Guy8506ab42009-06-11 17:35:47 -070014028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014029 public void setFadeColor(int color) {
14030 if (color != 0 && color != mLastColor) {
14031 mLastColor = color;
14032 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014033
Romain Guye55e1a72009-08-27 10:42:26 -070014034 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14035 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014037 paint.setShader(shader);
14038 // Restore the default transfer mode (src_over)
14039 paint.setXfermode(null);
14040 }
14041 }
Joe Malin32736f02011-01-19 16:14:20 -080014042
Mike Cleronf116bf82009-09-27 19:14:12 -070014043 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014044 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014045 if (now >= fadeStartTime) {
14046
14047 // the animation fades the scrollbars out by changing
14048 // the opacity (alpha) from fully opaque to fully
14049 // transparent
14050 int nextFrame = (int) now;
14051 int framesCount = 0;
14052
14053 Interpolator interpolator = scrollBarInterpolator;
14054
14055 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014056 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014057
14058 // End transparent
14059 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014060 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014061
14062 state = FADING;
14063
14064 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014065 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014066 }
14067 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014068 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014069
Svetoslav Ganova0156172011-06-26 17:55:44 -070014070 /**
14071 * Resuable callback for sending
14072 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14073 */
14074 private class SendViewScrolledAccessibilityEvent implements Runnable {
14075 public volatile boolean mIsPending;
14076
14077 public void run() {
14078 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14079 mIsPending = false;
14080 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014081 }
14082}