blob: 9efc4e53613f675c3e880a2af458f4b6e8cf36ca [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
Christopher Tatea53146c2010-09-07 11:57:52 -070019import android.content.ClipData;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
Dianne Hackborne36d6e22010-02-17 19:46:25 -080021import android.content.res.Configuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.graphics.Bitmap;
Adam Powell2b342f02010-08-18 18:14:13 -070025import android.graphics.Camera;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.graphics.Canvas;
Mike Cleronf116bf82009-09-27 19:14:12 -070027import android.graphics.Interpolator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.graphics.LinearGradient;
29import android.graphics.Matrix;
30import android.graphics.Paint;
31import android.graphics.PixelFormat;
svetoslavganov75986cf2009-05-14 22:28:01 -070032import android.graphics.Point;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.graphics.PorterDuff;
34import android.graphics.PorterDuffXfermode;
35import android.graphics.Rect;
Adam Powell6e346362010-07-23 10:18:23 -070036import android.graphics.RectF;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.graphics.Region;
38import android.graphics.Shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.graphics.drawable.ColorDrawable;
40import android.graphics.drawable.Drawable;
41import android.os.Handler;
42import android.os.IBinder;
43import android.os.Message;
44import android.os.Parcel;
45import android.os.Parcelable;
46import android.os.RemoteException;
47import android.os.SystemClock;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.util.AttributeSet;
Doug Feltcb3791202011-07-07 11:57:48 -070049import android.util.FloatProperty;
50import android.util.LocaleUtil;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.util.Log;
Romain Guyd928d682009-03-31 17:52:16 -070052import android.util.Pool;
svetoslavganov75986cf2009-05-14 22:28:01 -070053import android.util.Poolable;
Romain Guyd928d682009-03-31 17:52:16 -070054import android.util.PoolableManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.util.Pools;
Doug Feltcb3791202011-07-07 11:57:48 -070056import android.util.Property;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.util.SparseArray;
Jeff Brown33bbfd22011-02-24 20:55:35 -080058import android.util.TypedValue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.view.ContextMenu.ContextMenuInfo;
svetoslavganov75986cf2009-05-14 22:28:01 -070060import android.view.accessibility.AccessibilityEvent;
61import android.view.accessibility.AccessibilityEventSource;
62import android.view.accessibility.AccessibilityManager;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070063import android.view.accessibility.AccessibilityNodeInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064import android.view.animation.Animation;
Mike Cleron3ecd58c2009-09-28 11:39:02 -070065import android.view.animation.AnimationUtils;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.view.inputmethod.EditorInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.view.inputmethod.InputConnection;
68import android.view.inputmethod.InputMethodManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.widget.ScrollBarDrawable;
70
Doug Feltcb3791202011-07-07 11:57:48 -070071import com.android.internal.R;
72import com.android.internal.util.Predicate;
73import com.android.internal.view.menu.MenuBuilder;
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
610 * @attr ref android.R.styleable#View_saveEnabled
Chet Haase73066682010-11-29 15:55:32 -0800611 * @attr ref android.R.styleable#View_rotation
612 * @attr ref android.R.styleable#View_rotationX
613 * @attr ref android.R.styleable#View_rotationY
614 * @attr ref android.R.styleable#View_scaleX
615 * @attr ref android.R.styleable#View_scaleY
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 * @attr ref android.R.styleable#View_scrollX
617 * @attr ref android.R.styleable#View_scrollY
Romain Guyd6a463a2009-05-21 23:10:10 -0700618 * @attr ref android.R.styleable#View_scrollbarSize
619 * @attr ref android.R.styleable#View_scrollbarStyle
620 * @attr ref android.R.styleable#View_scrollbars
Mike Cleronf116bf82009-09-27 19:14:12 -0700621 * @attr ref android.R.styleable#View_scrollbarDefaultDelayBeforeFade
622 * @attr ref android.R.styleable#View_scrollbarFadeDuration
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 * @attr ref android.R.styleable#View_scrollbarTrackHorizontal
624 * @attr ref android.R.styleable#View_scrollbarThumbHorizontal
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800625 * @attr ref android.R.styleable#View_scrollbarThumbVertical
626 * @attr ref android.R.styleable#View_scrollbarTrackVertical
627 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawHorizontalTrack
628 * @attr ref android.R.styleable#View_scrollbarAlwaysDrawVerticalTrack
Romain Guyd6a463a2009-05-21 23:10:10 -0700629 * @attr ref android.R.styleable#View_soundEffectsEnabled
630 * @attr ref android.R.styleable#View_tag
Chet Haase73066682010-11-29 15:55:32 -0800631 * @attr ref android.R.styleable#View_transformPivotX
632 * @attr ref android.R.styleable#View_transformPivotY
633 * @attr ref android.R.styleable#View_translationX
634 * @attr ref android.R.styleable#View_translationY
Romain Guyd6a463a2009-05-21 23:10:10 -0700635 * @attr ref android.R.styleable#View_visibility
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 *
637 * @see android.view.ViewGroup
638 */
Adam Powell8fc54f92011-09-07 16:40:45 -0700639public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback,
640 AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800641 private static final boolean DBG = false;
642
643 /**
644 * The logging tag used by this class with android.util.Log.
645 */
646 protected static final String VIEW_LOG_TAG = "View";
647
648 /**
649 * Used to mark a View that has no ID.
650 */
651 public static final int NO_ID = -1;
652
653 /**
654 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
655 * calling setFlags.
656 */
657 private static final int NOT_FOCUSABLE = 0x00000000;
658
659 /**
660 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
661 * setFlags.
662 */
663 private static final int FOCUSABLE = 0x00000001;
664
665 /**
666 * Mask for use with setFlags indicating bits used for focus.
667 */
668 private static final int FOCUSABLE_MASK = 0x00000001;
669
670 /**
671 * This view will adjust its padding to fit sytem windows (e.g. status bar)
672 */
673 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
674
675 /**
Scott Main812634c22011-07-27 13:22:35 -0700676 * This view is visible.
677 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
678 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 */
680 public static final int VISIBLE = 0x00000000;
681
682 /**
683 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700684 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
685 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800686 */
687 public static final int INVISIBLE = 0x00000004;
688
689 /**
690 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700691 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
692 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 */
694 public static final int GONE = 0x00000008;
695
696 /**
697 * Mask for use with setFlags indicating bits used for visibility.
698 * {@hide}
699 */
700 static final int VISIBILITY_MASK = 0x0000000C;
701
702 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
703
704 /**
705 * This view is enabled. Intrepretation varies by subclass.
706 * Use with ENABLED_MASK when calling setFlags.
707 * {@hide}
708 */
709 static final int ENABLED = 0x00000000;
710
711 /**
712 * This view is disabled. Intrepretation varies by subclass.
713 * Use with ENABLED_MASK when calling setFlags.
714 * {@hide}
715 */
716 static final int DISABLED = 0x00000020;
717
718 /**
719 * Mask for use with setFlags indicating bits used for indicating whether
720 * this view is enabled
721 * {@hide}
722 */
723 static final int ENABLED_MASK = 0x00000020;
724
725 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700726 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
727 * called and further optimizations will be performed. It is okay to have
728 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 * {@hide}
730 */
731 static final int WILL_NOT_DRAW = 0x00000080;
732
733 /**
734 * Mask for use with setFlags indicating bits used for indicating whether
735 * this view is will draw
736 * {@hide}
737 */
738 static final int DRAW_MASK = 0x00000080;
739
740 /**
741 * <p>This view doesn't show scrollbars.</p>
742 * {@hide}
743 */
744 static final int SCROLLBARS_NONE = 0x00000000;
745
746 /**
747 * <p>This view shows horizontal scrollbars.</p>
748 * {@hide}
749 */
750 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
751
752 /**
753 * <p>This view shows vertical scrollbars.</p>
754 * {@hide}
755 */
756 static final int SCROLLBARS_VERTICAL = 0x00000200;
757
758 /**
759 * <p>Mask for use with setFlags indicating bits used for indicating which
760 * scrollbars are enabled.</p>
761 * {@hide}
762 */
763 static final int SCROLLBARS_MASK = 0x00000300;
764
Jeff Brown85a31762010-09-01 17:01:00 -0700765 /**
766 * Indicates that the view should filter touches when its window is obscured.
767 * Refer to the class comments for more information about this security feature.
768 * {@hide}
769 */
770 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
771
772 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773
774 /**
775 * <p>This view doesn't show fading edges.</p>
776 * {@hide}
777 */
778 static final int FADING_EDGE_NONE = 0x00000000;
779
780 /**
781 * <p>This view shows horizontal fading edges.</p>
782 * {@hide}
783 */
784 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
785
786 /**
787 * <p>This view shows vertical fading edges.</p>
788 * {@hide}
789 */
790 static final int FADING_EDGE_VERTICAL = 0x00002000;
791
792 /**
793 * <p>Mask for use with setFlags indicating bits used for indicating which
794 * fading edges are enabled.</p>
795 * {@hide}
796 */
797 static final int FADING_EDGE_MASK = 0x00003000;
798
799 /**
800 * <p>Indicates this view can be clicked. When clickable, a View reacts
801 * to clicks by notifying the OnClickListener.<p>
802 * {@hide}
803 */
804 static final int CLICKABLE = 0x00004000;
805
806 /**
807 * <p>Indicates this view is caching its drawing into a bitmap.</p>
808 * {@hide}
809 */
810 static final int DRAWING_CACHE_ENABLED = 0x00008000;
811
812 /**
813 * <p>Indicates that no icicle should be saved for this view.<p>
814 * {@hide}
815 */
816 static final int SAVE_DISABLED = 0x000010000;
817
818 /**
819 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
820 * property.</p>
821 * {@hide}
822 */
823 static final int SAVE_DISABLED_MASK = 0x000010000;
824
825 /**
826 * <p>Indicates that no drawing cache should ever be created for this view.<p>
827 * {@hide}
828 */
829 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
830
831 /**
832 * <p>Indicates this view can take / keep focus when int touch mode.</p>
833 * {@hide}
834 */
835 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
836
837 /**
838 * <p>Enables low quality mode for the drawing cache.</p>
839 */
840 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
841
842 /**
843 * <p>Enables high quality mode for the drawing cache.</p>
844 */
845 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
846
847 /**
848 * <p>Enables automatic quality mode for the drawing cache.</p>
849 */
850 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
851
852 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
853 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
854 };
855
856 /**
857 * <p>Mask for use with setFlags indicating bits used for the cache
858 * quality property.</p>
859 * {@hide}
860 */
861 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
862
863 /**
864 * <p>
865 * Indicates this view can be long clicked. When long clickable, a View
866 * reacts to long clicks by notifying the OnLongClickListener or showing a
867 * context menu.
868 * </p>
869 * {@hide}
870 */
871 static final int LONG_CLICKABLE = 0x00200000;
872
873 /**
874 * <p>Indicates that this view gets its drawable states from its direct parent
875 * and ignores its original internal states.</p>
876 *
877 * @hide
878 */
879 static final int DUPLICATE_PARENT_STATE = 0x00400000;
880
881 /**
882 * The scrollbar style to display the scrollbars inside the content area,
883 * without increasing the padding. The scrollbars will be overlaid with
884 * translucency on the view's content.
885 */
886 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
887
888 /**
889 * The scrollbar style to display the scrollbars inside the padded area,
890 * increasing the padding of the view. The scrollbars will not overlap the
891 * content area of the view.
892 */
893 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
894
895 /**
896 * The scrollbar style to display the scrollbars at the edge of the view,
897 * without increasing the padding. The scrollbars will be overlaid with
898 * translucency.
899 */
900 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
901
902 /**
903 * The scrollbar style to display the scrollbars at the edge of the view,
904 * increasing the padding of the view. The scrollbars will only overlap the
905 * background, if any.
906 */
907 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
908
909 /**
910 * Mask to check if the scrollbar style is overlay or inset.
911 * {@hide}
912 */
913 static final int SCROLLBARS_INSET_MASK = 0x01000000;
914
915 /**
916 * Mask to check if the scrollbar style is inside or outside.
917 * {@hide}
918 */
919 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
920
921 /**
922 * Mask for scrollbar style.
923 * {@hide}
924 */
925 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
926
927 /**
928 * View flag indicating that the screen should remain on while the
929 * window containing this view is visible to the user. This effectively
930 * takes care of automatically setting the WindowManager's
931 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
932 */
933 public static final int KEEP_SCREEN_ON = 0x04000000;
934
935 /**
936 * View flag indicating whether this view should have sound effects enabled
937 * for events such as clicking and touching.
938 */
939 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
940
941 /**
942 * View flag indicating whether this view should have haptic feedback
943 * enabled for events such as long presses.
944 */
945 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
946
947 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700948 * <p>Indicates that the view hierarchy should stop saving state when
949 * it reaches this view. If state saving is initiated immediately at
950 * the view, it will be allowed.
951 * {@hide}
952 */
953 static final int PARENT_SAVE_DISABLED = 0x20000000;
954
955 /**
956 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
957 * {@hide}
958 */
959 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
960
961 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800962 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700963 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800964 * {@hide}
965 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700966 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800967
968 /**
969 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700970 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800971 * {@hide}
972 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700973 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800974
975 /**
976 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700977 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800978 * {@hide}
979 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700980 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800981
982 /**
983 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700984 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800985 * {@hide}
986 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700987 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800988
989 /**
990 * Mask for use with setFlags indicating bits used for horizontalDirection.
991 * {@hide}
992 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700993 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800994
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700995 /*
996 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
997 * flag value.
998 * {@hide}
999 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001000 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
1001 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -08001002
1003 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001004 * Default horizontalDirection.
1005 * {@hide}
1006 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001007 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001008
1009 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001010 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1011 * should add all focusable Views regardless if they are focusable in touch mode.
1012 */
1013 public static final int FOCUSABLES_ALL = 0x00000000;
1014
1015 /**
1016 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1017 * should add only Views focusable in touch mode.
1018 */
1019 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1020
1021 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001022 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001023 * item.
1024 */
1025 public static final int FOCUS_BACKWARD = 0x00000001;
1026
1027 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001028 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 * item.
1030 */
1031 public static final int FOCUS_FORWARD = 0x00000002;
1032
1033 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001034 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 */
1036 public static final int FOCUS_LEFT = 0x00000011;
1037
1038 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001039 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001040 */
1041 public static final int FOCUS_UP = 0x00000021;
1042
1043 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001044 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 */
1046 public static final int FOCUS_RIGHT = 0x00000042;
1047
1048 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001049 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 */
1051 public static final int FOCUS_DOWN = 0x00000082;
1052
1053 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001054 * Bits of {@link #getMeasuredWidthAndState()} and
1055 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1056 */
1057 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1058
1059 /**
1060 * Bits of {@link #getMeasuredWidthAndState()} and
1061 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1062 */
1063 public static final int MEASURED_STATE_MASK = 0xff000000;
1064
1065 /**
1066 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1067 * for functions that combine both width and height into a single int,
1068 * such as {@link #getMeasuredState()} and the childState argument of
1069 * {@link #resolveSizeAndState(int, int, int)}.
1070 */
1071 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1072
1073 /**
1074 * Bit of {@link #getMeasuredWidthAndState()} and
1075 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1076 * is smaller that the space the view would like to have.
1077 */
1078 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1079
1080 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 * Base View state sets
1082 */
1083 // Singles
1084 /**
1085 * Indicates the view has no states set. States are used with
1086 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1087 * view depending on its state.
1088 *
1089 * @see android.graphics.drawable.Drawable
1090 * @see #getDrawableState()
1091 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001092 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001093 /**
1094 * Indicates the view is enabled. States are used with
1095 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1096 * view depending on its state.
1097 *
1098 * @see android.graphics.drawable.Drawable
1099 * @see #getDrawableState()
1100 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001101 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 /**
1103 * Indicates the view is focused. States are used with
1104 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1105 * view depending on its state.
1106 *
1107 * @see android.graphics.drawable.Drawable
1108 * @see #getDrawableState()
1109 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001110 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 /**
1112 * Indicates the view is selected. States are used with
1113 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1114 * view depending on its state.
1115 *
1116 * @see android.graphics.drawable.Drawable
1117 * @see #getDrawableState()
1118 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001119 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 /**
1121 * Indicates the view is pressed. States are used with
1122 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1123 * view depending on its state.
1124 *
1125 * @see android.graphics.drawable.Drawable
1126 * @see #getDrawableState()
1127 * @hide
1128 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001129 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 /**
1131 * Indicates the view's window has focus. States are used with
1132 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1133 * view depending on its state.
1134 *
1135 * @see android.graphics.drawable.Drawable
1136 * @see #getDrawableState()
1137 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001138 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 // Doubles
1140 /**
1141 * Indicates the view is enabled and has the focus.
1142 *
1143 * @see #ENABLED_STATE_SET
1144 * @see #FOCUSED_STATE_SET
1145 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001146 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001147 /**
1148 * Indicates the view is enabled and selected.
1149 *
1150 * @see #ENABLED_STATE_SET
1151 * @see #SELECTED_STATE_SET
1152 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001153 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 /**
1155 * Indicates the view is enabled and that its window has focus.
1156 *
1157 * @see #ENABLED_STATE_SET
1158 * @see #WINDOW_FOCUSED_STATE_SET
1159 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001160 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001161 /**
1162 * Indicates the view is focused and selected.
1163 *
1164 * @see #FOCUSED_STATE_SET
1165 * @see #SELECTED_STATE_SET
1166 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001167 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 /**
1169 * Indicates the view has the focus and that its window has the focus.
1170 *
1171 * @see #FOCUSED_STATE_SET
1172 * @see #WINDOW_FOCUSED_STATE_SET
1173 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001174 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 /**
1176 * Indicates the view is selected and that its window has the focus.
1177 *
1178 * @see #SELECTED_STATE_SET
1179 * @see #WINDOW_FOCUSED_STATE_SET
1180 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001181 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 // Triples
1183 /**
1184 * Indicates the view is enabled, focused and selected.
1185 *
1186 * @see #ENABLED_STATE_SET
1187 * @see #FOCUSED_STATE_SET
1188 * @see #SELECTED_STATE_SET
1189 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001190 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001191 /**
1192 * Indicates the view is enabled, focused and its window has the focus.
1193 *
1194 * @see #ENABLED_STATE_SET
1195 * @see #FOCUSED_STATE_SET
1196 * @see #WINDOW_FOCUSED_STATE_SET
1197 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001198 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 /**
1200 * Indicates the view is enabled, selected and its window has the focus.
1201 *
1202 * @see #ENABLED_STATE_SET
1203 * @see #SELECTED_STATE_SET
1204 * @see #WINDOW_FOCUSED_STATE_SET
1205 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001206 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 /**
1208 * Indicates the view is focused, selected and its window has the focus.
1209 *
1210 * @see #FOCUSED_STATE_SET
1211 * @see #SELECTED_STATE_SET
1212 * @see #WINDOW_FOCUSED_STATE_SET
1213 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001214 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 /**
1216 * Indicates the view is enabled, focused, selected and its window
1217 * has the focus.
1218 *
1219 * @see #ENABLED_STATE_SET
1220 * @see #FOCUSED_STATE_SET
1221 * @see #SELECTED_STATE_SET
1222 * @see #WINDOW_FOCUSED_STATE_SET
1223 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001224 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001225 /**
1226 * Indicates the view is pressed and its window has the focus.
1227 *
1228 * @see #PRESSED_STATE_SET
1229 * @see #WINDOW_FOCUSED_STATE_SET
1230 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001231 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 /**
1233 * Indicates the view is pressed and selected.
1234 *
1235 * @see #PRESSED_STATE_SET
1236 * @see #SELECTED_STATE_SET
1237 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001238 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 /**
1240 * Indicates the view is pressed, selected and its window has the focus.
1241 *
1242 * @see #PRESSED_STATE_SET
1243 * @see #SELECTED_STATE_SET
1244 * @see #WINDOW_FOCUSED_STATE_SET
1245 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001246 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 /**
1248 * Indicates the view is pressed and focused.
1249 *
1250 * @see #PRESSED_STATE_SET
1251 * @see #FOCUSED_STATE_SET
1252 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001253 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 /**
1255 * Indicates the view is pressed, focused and its window has the focus.
1256 *
1257 * @see #PRESSED_STATE_SET
1258 * @see #FOCUSED_STATE_SET
1259 * @see #WINDOW_FOCUSED_STATE_SET
1260 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001261 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 /**
1263 * Indicates the view is pressed, focused and selected.
1264 *
1265 * @see #PRESSED_STATE_SET
1266 * @see #SELECTED_STATE_SET
1267 * @see #FOCUSED_STATE_SET
1268 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001269 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 /**
1271 * Indicates the view is pressed, focused, selected and its window has the focus.
1272 *
1273 * @see #PRESSED_STATE_SET
1274 * @see #FOCUSED_STATE_SET
1275 * @see #SELECTED_STATE_SET
1276 * @see #WINDOW_FOCUSED_STATE_SET
1277 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001278 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279 /**
1280 * Indicates the view is pressed and enabled.
1281 *
1282 * @see #PRESSED_STATE_SET
1283 * @see #ENABLED_STATE_SET
1284 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001285 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
1287 * Indicates the view is pressed, enabled and its window has the focus.
1288 *
1289 * @see #PRESSED_STATE_SET
1290 * @see #ENABLED_STATE_SET
1291 * @see #WINDOW_FOCUSED_STATE_SET
1292 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001293 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 /**
1295 * Indicates the view is pressed, enabled and selected.
1296 *
1297 * @see #PRESSED_STATE_SET
1298 * @see #ENABLED_STATE_SET
1299 * @see #SELECTED_STATE_SET
1300 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001301 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 /**
1303 * Indicates the view is pressed, enabled, selected and its window has the
1304 * focus.
1305 *
1306 * @see #PRESSED_STATE_SET
1307 * @see #ENABLED_STATE_SET
1308 * @see #SELECTED_STATE_SET
1309 * @see #WINDOW_FOCUSED_STATE_SET
1310 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001311 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001312 /**
1313 * Indicates the view is pressed, enabled and focused.
1314 *
1315 * @see #PRESSED_STATE_SET
1316 * @see #ENABLED_STATE_SET
1317 * @see #FOCUSED_STATE_SET
1318 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001319 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 /**
1321 * Indicates the view is pressed, enabled, focused and its window has the
1322 * focus.
1323 *
1324 * @see #PRESSED_STATE_SET
1325 * @see #ENABLED_STATE_SET
1326 * @see #FOCUSED_STATE_SET
1327 * @see #WINDOW_FOCUSED_STATE_SET
1328 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001329 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 /**
1331 * Indicates the view is pressed, enabled, focused and selected.
1332 *
1333 * @see #PRESSED_STATE_SET
1334 * @see #ENABLED_STATE_SET
1335 * @see #SELECTED_STATE_SET
1336 * @see #FOCUSED_STATE_SET
1337 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001338 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001339 /**
1340 * Indicates the view is pressed, enabled, focused, selected and its window
1341 * has the focus.
1342 *
1343 * @see #PRESSED_STATE_SET
1344 * @see #ENABLED_STATE_SET
1345 * @see #SELECTED_STATE_SET
1346 * @see #FOCUSED_STATE_SET
1347 * @see #WINDOW_FOCUSED_STATE_SET
1348 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001349 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350
1351 /**
1352 * The order here is very important to {@link #getDrawableState()}
1353 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001354 private static final int[][] VIEW_STATE_SETS;
1355
Romain Guyb051e892010-09-28 19:09:36 -07001356 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1357 static final int VIEW_STATE_SELECTED = 1 << 1;
1358 static final int VIEW_STATE_FOCUSED = 1 << 2;
1359 static final int VIEW_STATE_ENABLED = 1 << 3;
1360 static final int VIEW_STATE_PRESSED = 1 << 4;
1361 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001362 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001363 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001364 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1365 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001366
1367 static final int[] VIEW_STATE_IDS = new int[] {
1368 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1369 R.attr.state_selected, VIEW_STATE_SELECTED,
1370 R.attr.state_focused, VIEW_STATE_FOCUSED,
1371 R.attr.state_enabled, VIEW_STATE_ENABLED,
1372 R.attr.state_pressed, VIEW_STATE_PRESSED,
1373 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001374 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001375 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001376 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1377 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 };
1379
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001380 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001381 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1382 throw new IllegalStateException(
1383 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1384 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001385 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001386 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001387 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001388 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001389 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001390 orderedIds[i * 2] = viewState;
1391 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001392 }
1393 }
1394 }
Romain Guyb051e892010-09-28 19:09:36 -07001395 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1396 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1397 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001398 int numBits = Integer.bitCount(i);
1399 int[] set = new int[numBits];
1400 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001401 for (int j = 0; j < orderedIds.length; j += 2) {
1402 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001403 set[pos++] = orderedIds[j];
1404 }
1405 }
1406 VIEW_STATE_SETS[i] = set;
1407 }
1408
1409 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1410 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1411 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1412 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1413 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1414 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1415 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1416 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1417 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1418 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1419 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1420 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1421 | VIEW_STATE_FOCUSED];
1422 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1423 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1424 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1425 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1426 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1427 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1428 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1429 | VIEW_STATE_ENABLED];
1430 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1431 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1432 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1433 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1434 | VIEW_STATE_ENABLED];
1435 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1436 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1437 | VIEW_STATE_ENABLED];
1438 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1439 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1440 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1441
1442 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1443 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1444 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1445 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1446 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1447 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1448 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1449 | VIEW_STATE_PRESSED];
1450 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1451 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1452 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1453 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1454 | VIEW_STATE_PRESSED];
1455 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1456 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1457 | VIEW_STATE_PRESSED];
1458 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1459 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1460 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1461 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1462 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1463 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1464 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1465 | VIEW_STATE_PRESSED];
1466 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1467 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1468 | VIEW_STATE_PRESSED];
1469 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1470 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1471 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1472 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1473 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1474 | VIEW_STATE_PRESSED];
1475 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1476 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1477 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1478 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1479 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1480 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1481 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1482 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1483 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1484 | VIEW_STATE_PRESSED];
1485 }
1486
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001488 * Temporary Rect currently for use in setBackground(). This will probably
1489 * be extended in the future to hold our own class with more than just
1490 * a Rect. :)
1491 */
1492 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001493
1494 /**
1495 * Map used to store views' tags.
1496 */
1497 private static WeakHashMap<View, SparseArray<Object>> sTags;
1498
1499 /**
1500 * Lock used to access sTags.
1501 */
1502 private static final Object sTagsLock = new Object();
1503
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001505 * The next available accessiiblity id.
1506 */
1507 private static int sNextAccessibilityViewId;
1508
1509 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001510 * The animation currently associated with this view.
1511 * @hide
1512 */
1513 protected Animation mCurrentAnimation = null;
1514
1515 /**
1516 * Width as measured during measure pass.
1517 * {@hide}
1518 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001519 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001520 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521
1522 /**
1523 * Height as measured during measure pass.
1524 * {@hide}
1525 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001526 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001527 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528
1529 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001530 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1531 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1532 * its display list. This flag, used only when hw accelerated, allows us to clear the
1533 * flag while retaining this information until it's needed (at getDisplayList() time and
1534 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1535 *
1536 * {@hide}
1537 */
1538 boolean mRecreateDisplayList = false;
1539
1540 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 * The view's identifier.
1542 * {@hide}
1543 *
1544 * @see #setId(int)
1545 * @see #getId()
1546 */
1547 @ViewDebug.ExportedProperty(resolveId = true)
1548 int mID = NO_ID;
1549
1550 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001551 * The stable ID of this view for accessibility porposes.
1552 */
1553 int mAccessibilityViewId = NO_ID;
1554
1555 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001556 * The view's tag.
1557 * {@hide}
1558 *
1559 * @see #setTag(Object)
1560 * @see #getTag()
1561 */
1562 protected Object mTag;
1563
1564 // for mPrivateFlags:
1565 /** {@hide} */
1566 static final int WANTS_FOCUS = 0x00000001;
1567 /** {@hide} */
1568 static final int FOCUSED = 0x00000002;
1569 /** {@hide} */
1570 static final int SELECTED = 0x00000004;
1571 /** {@hide} */
1572 static final int IS_ROOT_NAMESPACE = 0x00000008;
1573 /** {@hide} */
1574 static final int HAS_BOUNDS = 0x00000010;
1575 /** {@hide} */
1576 static final int DRAWN = 0x00000020;
1577 /**
1578 * When this flag is set, this view is running an animation on behalf of its
1579 * children and should therefore not cancel invalidate requests, even if they
1580 * lie outside of this view's bounds.
1581 *
1582 * {@hide}
1583 */
1584 static final int DRAW_ANIMATION = 0x00000040;
1585 /** {@hide} */
1586 static final int SKIP_DRAW = 0x00000080;
1587 /** {@hide} */
1588 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1589 /** {@hide} */
1590 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1591 /** {@hide} */
1592 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1593 /** {@hide} */
1594 static final int MEASURED_DIMENSION_SET = 0x00000800;
1595 /** {@hide} */
1596 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001597 /** {@hide} */
1598 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599
1600 private static final int PRESSED = 0x00004000;
1601
1602 /** {@hide} */
1603 static final int DRAWING_CACHE_VALID = 0x00008000;
1604 /**
1605 * Flag used to indicate that this view should be drawn once more (and only once
1606 * more) after its animation has completed.
1607 * {@hide}
1608 */
1609 static final int ANIMATION_STARTED = 0x00010000;
1610
1611 private static final int SAVE_STATE_CALLED = 0x00020000;
1612
1613 /**
1614 * Indicates that the View returned true when onSetAlpha() was called and that
1615 * the alpha must be restored.
1616 * {@hide}
1617 */
1618 static final int ALPHA_SET = 0x00040000;
1619
1620 /**
1621 * Set by {@link #setScrollContainer(boolean)}.
1622 */
1623 static final int SCROLL_CONTAINER = 0x00080000;
1624
1625 /**
1626 * Set by {@link #setScrollContainer(boolean)}.
1627 */
1628 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1629
1630 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001631 * View flag indicating whether this view was invalidated (fully or partially.)
1632 *
1633 * @hide
1634 */
1635 static final int DIRTY = 0x00200000;
1636
1637 /**
1638 * View flag indicating whether this view was invalidated by an opaque
1639 * invalidate request.
1640 *
1641 * @hide
1642 */
1643 static final int DIRTY_OPAQUE = 0x00400000;
1644
1645 /**
1646 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1647 *
1648 * @hide
1649 */
1650 static final int DIRTY_MASK = 0x00600000;
1651
1652 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001653 * Indicates whether the background is opaque.
1654 *
1655 * @hide
1656 */
1657 static final int OPAQUE_BACKGROUND = 0x00800000;
1658
1659 /**
1660 * Indicates whether the scrollbars are opaque.
1661 *
1662 * @hide
1663 */
1664 static final int OPAQUE_SCROLLBARS = 0x01000000;
1665
1666 /**
1667 * Indicates whether the view is opaque.
1668 *
1669 * @hide
1670 */
1671 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001672
Adam Powelle14579b2009-12-16 18:39:52 -08001673 /**
1674 * Indicates a prepressed state;
1675 * the short time between ACTION_DOWN and recognizing
1676 * a 'real' press. Prepressed is used to recognize quick taps
1677 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001678 *
Adam Powelle14579b2009-12-16 18:39:52 -08001679 * @hide
1680 */
1681 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001682
Adam Powellc9fbaab2010-02-16 17:16:19 -08001683 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001684 * Indicates whether the view is temporarily detached.
1685 *
1686 * @hide
1687 */
1688 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001689
Adam Powell8568c3a2010-04-19 14:26:11 -07001690 /**
1691 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001692 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001693 * @hide
1694 */
1695 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001696
1697 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001698 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1699 * @hide
1700 */
1701 private static final int HOVERED = 0x10000000;
1702
1703 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001704 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1705 * for transform operations
1706 *
1707 * @hide
1708 */
Adam Powellf37df072010-09-17 16:22:49 -07001709 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001710
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001711 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001712 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001713
Chet Haasefd2b0022010-08-06 13:08:56 -07001714 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001715 * Indicates that this view was specifically invalidated, not just dirtied because some
1716 * child view was invalidated. The flag is used to determine when we need to recreate
1717 * a view's display list (as opposed to just returning a reference to its existing
1718 * display list).
1719 *
1720 * @hide
1721 */
1722 static final int INVALIDATED = 0x80000000;
1723
Christopher Tate3d4bf172011-03-28 16:16:46 -07001724 /* Masks for mPrivateFlags2 */
1725
1726 /**
1727 * Indicates that this view has reported that it can accept the current drag's content.
1728 * Cleared when the drag operation concludes.
1729 * @hide
1730 */
1731 static final int DRAG_CAN_ACCEPT = 0x00000001;
1732
1733 /**
1734 * Indicates that this view is currently directly under the drag location in a
1735 * drag-and-drop operation involving content that it can accept. Cleared when
1736 * the drag exits the view, or when the drag operation concludes.
1737 * @hide
1738 */
1739 static final int DRAG_HOVERED = 0x00000002;
1740
Cibu Johny86666632010-02-22 13:01:02 -08001741 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001742 * Indicates whether the view layout direction has been resolved and drawn to the
1743 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001744 *
1745 * @hide
1746 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001747 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1748
1749 /**
1750 * Indicates whether the view layout direction has been resolved.
1751 *
1752 * @hide
1753 */
1754 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1755
Cibu Johny86666632010-02-22 13:01:02 -08001756
Christopher Tate3d4bf172011-03-28 16:16:46 -07001757 /* End of masks for mPrivateFlags2 */
1758
1759 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1760
Chet Haasedaf98e92011-01-10 14:10:36 -08001761 /**
Adam Powell637d3372010-08-25 14:37:03 -07001762 * Always allow a user to over-scroll this view, provided it is a
1763 * view that can scroll.
1764 *
1765 * @see #getOverScrollMode()
1766 * @see #setOverScrollMode(int)
1767 */
1768 public static final int OVER_SCROLL_ALWAYS = 0;
1769
1770 /**
1771 * Allow a user to over-scroll this view only if the content is large
1772 * enough to meaningfully scroll, provided it is a view that can scroll.
1773 *
1774 * @see #getOverScrollMode()
1775 * @see #setOverScrollMode(int)
1776 */
1777 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1778
1779 /**
1780 * Never allow a user to over-scroll this view.
1781 *
1782 * @see #getOverScrollMode()
1783 * @see #setOverScrollMode(int)
1784 */
1785 public static final int OVER_SCROLL_NEVER = 2;
1786
1787 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001788 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001789 *
Joe Malin32736f02011-01-19 16:14:20 -08001790 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001791 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001792 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001793
1794 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001795 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1796 *
1797 * This is for use in games, book readers, video players, or any other "immersive" application
1798 * where the usual system chrome is deemed too distracting.
1799 *
1800 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001801 *
Joe Malin32736f02011-01-19 16:14:20 -08001802 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001803 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001804 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1805
1806 /**
1807 * View has requested that the system navigation be temporarily hidden.
1808 *
1809 * This is an even less obtrusive state than that called for by
1810 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1811 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1812 * those to disappear. This is useful (in conjunction with the
1813 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1814 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1815 * window flags) for displaying content using every last pixel on the display.
1816 *
1817 * There is a limitation: because navigation controls are so important, the least user
1818 * interaction will cause them to reappear immediately.
1819 *
1820 * @see #setSystemUiVisibility(int)
1821 */
1822 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1823
1824 /**
1825 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1826 */
1827 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1828
1829 /**
1830 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1831 */
1832 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001833
1834 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001835 * @hide
1836 *
1837 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1838 * out of the public fields to keep the undefined bits out of the developer's way.
1839 *
1840 * Flag to make the status bar not expandable. Unless you also
1841 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1842 */
1843 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1844
1845 /**
1846 * @hide
1847 *
1848 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1849 * out of the public fields to keep the undefined bits out of the developer's way.
1850 *
1851 * Flag to hide notification icons and scrolling ticker text.
1852 */
1853 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1854
1855 /**
1856 * @hide
1857 *
1858 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1859 * out of the public fields to keep the undefined bits out of the developer's way.
1860 *
1861 * Flag to disable incoming notification alerts. This will not block
1862 * icons, but it will block sound, vibrating and other visual or aural notifications.
1863 */
1864 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1865
1866 /**
1867 * @hide
1868 *
1869 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1870 * out of the public fields to keep the undefined bits out of the developer's way.
1871 *
1872 * Flag to hide only the scrolling ticker. Note that
1873 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1874 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1875 */
1876 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1877
1878 /**
1879 * @hide
1880 *
1881 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1882 * out of the public fields to keep the undefined bits out of the developer's way.
1883 *
1884 * Flag to hide the center system info area.
1885 */
1886 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1887
1888 /**
1889 * @hide
1890 *
1891 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1892 * out of the public fields to keep the undefined bits out of the developer's way.
1893 *
1894 * Flag to hide only the navigation buttons. Don't use this
1895 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001896 *
1897 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001898 */
1899 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1900
1901 /**
1902 * @hide
1903 *
1904 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1905 * out of the public fields to keep the undefined bits out of the developer's way.
1906 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001907 * Flag to hide only the back button. Don't use this
1908 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1909 */
1910 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1911
1912 /**
1913 * @hide
1914 *
1915 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1916 * out of the public fields to keep the undefined bits out of the developer's way.
1917 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001918 * Flag to hide only the clock. You might use this if your activity has
1919 * its own clock making the status bar's clock redundant.
1920 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001921 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1922
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001923 /**
1924 * @hide
1925 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001926 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001927
1928 /**
Adam Powell637d3372010-08-25 14:37:03 -07001929 * Controls the over-scroll mode for this view.
1930 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1931 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1932 * and {@link #OVER_SCROLL_NEVER}.
1933 */
1934 private int mOverScrollMode;
1935
1936 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001937 * The parent this view is attached to.
1938 * {@hide}
1939 *
1940 * @see #getParent()
1941 */
1942 protected ViewParent mParent;
1943
1944 /**
1945 * {@hide}
1946 */
1947 AttachInfo mAttachInfo;
1948
1949 /**
1950 * {@hide}
1951 */
Romain Guy809a7f62009-05-14 15:44:42 -07001952 @ViewDebug.ExportedProperty(flagMapping = {
1953 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1954 name = "FORCE_LAYOUT"),
1955 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1956 name = "LAYOUT_REQUIRED"),
1957 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001958 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001959 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1960 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1961 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1962 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1963 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001965 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966
1967 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001968 * This view's request for the visibility of the status bar.
1969 * @hide
1970 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001971 @ViewDebug.ExportedProperty(flagMapping = {
1972 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1973 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1974 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1975 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1976 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1977 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1978 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1979 equals = SYSTEM_UI_FLAG_VISIBLE,
1980 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1981 })
Joe Onorato664644d2011-01-23 17:53:23 -08001982 int mSystemUiVisibility;
1983
1984 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001985 * Count of how many windows this view has been attached to.
1986 */
1987 int mWindowAttachCount;
1988
1989 /**
1990 * The layout parameters associated with this view and used by the parent
1991 * {@link android.view.ViewGroup} to determine how this view should be
1992 * laid out.
1993 * {@hide}
1994 */
1995 protected ViewGroup.LayoutParams mLayoutParams;
1996
1997 /**
1998 * The view flags hold various views states.
1999 * {@hide}
2000 */
2001 @ViewDebug.ExportedProperty
2002 int mViewFlags;
2003
2004 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002005 * The transform matrix for the View. This transform is calculated internally
2006 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2007 * is used by default. Do *not* use this variable directly; instead call
2008 * getMatrix(), which will automatically recalculate the matrix if necessary
2009 * to get the correct matrix based on the latest rotation and scale properties.
2010 */
2011 private final Matrix mMatrix = new Matrix();
2012
2013 /**
2014 * The transform matrix for the View. This transform is calculated internally
2015 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2016 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07002017 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07002018 * to get the correct matrix based on the latest rotation and scale properties.
2019 */
2020 private Matrix mInverseMatrix;
2021
2022 /**
2023 * An internal variable that tracks whether we need to recalculate the
2024 * transform matrix, based on whether the rotation or scaleX/Y properties
2025 * have changed since the matrix was last calculated.
2026 */
Chet Haasea00f3862011-02-22 06:34:40 -08002027 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002028
2029 /**
2030 * An internal variable that tracks whether we need to recalculate the
2031 * transform matrix, based on whether the rotation or scaleX/Y properties
2032 * have changed since the matrix was last calculated.
2033 */
2034 private boolean mInverseMatrixDirty = true;
2035
2036 /**
2037 * A variable that tracks whether we need to recalculate the
2038 * transform matrix, based on whether the rotation or scaleX/Y properties
2039 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07002040 * is only valid after a call to updateMatrix() or to a function that
2041 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07002042 */
Romain Guy33e72ae2010-07-17 12:40:29 -07002043 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002044
2045 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07002046 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2047 */
2048 private Camera mCamera = null;
2049
2050 /**
2051 * This matrix is used when computing the matrix for 3D rotations.
2052 */
2053 private Matrix matrix3D = null;
2054
2055 /**
2056 * These prev values are used to recalculate a centered pivot point when necessary. The
2057 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2058 * set), so thes values are only used then as well.
2059 */
2060 private int mPrevWidth = -1;
2061 private int mPrevHeight = -1;
2062
Joe Malin32736f02011-01-19 16:14:20 -08002063 private boolean mLastIsOpaque;
2064
Chet Haasefd2b0022010-08-06 13:08:56 -07002065 /**
2066 * Convenience value to check for float values that are close enough to zero to be considered
2067 * zero.
2068 */
Romain Guy2542d192010-08-18 11:47:12 -07002069 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002070
2071 /**
2072 * The degrees rotation around the vertical axis through the pivot point.
2073 */
2074 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002075 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002076
2077 /**
2078 * The degrees rotation around the horizontal axis through the pivot point.
2079 */
2080 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002081 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002082
2083 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002084 * The degrees rotation around the pivot point.
2085 */
2086 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002087 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002088
2089 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002090 * The amount of translation of the object away from its left property (post-layout).
2091 */
2092 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002093 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002094
2095 /**
2096 * The amount of translation of the object away from its top property (post-layout).
2097 */
2098 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002099 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002100
2101 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002102 * The amount of scale in the x direction around the pivot point. A
2103 * value of 1 means no scaling is applied.
2104 */
2105 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002106 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002107
2108 /**
2109 * The amount of scale in the y direction around the pivot point. A
2110 * value of 1 means no scaling is applied.
2111 */
2112 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002113 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002114
2115 /**
2116 * The amount of scale in the x direction around the pivot point. A
2117 * value of 1 means no scaling is applied.
2118 */
2119 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002120 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002121
2122 /**
2123 * The amount of scale in the y direction around the pivot point. A
2124 * value of 1 means no scaling is applied.
2125 */
2126 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002127 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002128
2129 /**
2130 * The opacity of the View. This is a value from 0 to 1, where 0 means
2131 * completely transparent and 1 means completely opaque.
2132 */
2133 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002134 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002135
2136 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 * The distance in pixels from the left edge of this view's parent
2138 * to the left edge of this view.
2139 * {@hide}
2140 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002141 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 protected int mLeft;
2143 /**
2144 * The distance in pixels from the left edge of this view's parent
2145 * to the right edge of this view.
2146 * {@hide}
2147 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002148 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002149 protected int mRight;
2150 /**
2151 * The distance in pixels from the top edge of this view's parent
2152 * to the top edge of this view.
2153 * {@hide}
2154 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002155 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 protected int mTop;
2157 /**
2158 * The distance in pixels from the top edge of this view's parent
2159 * to the bottom edge of this view.
2160 * {@hide}
2161 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002162 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 protected int mBottom;
2164
2165 /**
2166 * The offset, in pixels, by which the content of this view is scrolled
2167 * horizontally.
2168 * {@hide}
2169 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002170 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 protected int mScrollX;
2172 /**
2173 * The offset, in pixels, by which the content of this view is scrolled
2174 * vertically.
2175 * {@hide}
2176 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002177 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002178 protected int mScrollY;
2179
2180 /**
2181 * The left padding in pixels, that is the distance in pixels between the
2182 * left edge of this view and the left edge of its content.
2183 * {@hide}
2184 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002185 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 protected int mPaddingLeft;
2187 /**
2188 * The right padding in pixels, that is the distance in pixels between the
2189 * right edge of this view and the right edge of its content.
2190 * {@hide}
2191 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002192 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002193 protected int mPaddingRight;
2194 /**
2195 * The top padding in pixels, that is the distance in pixels between the
2196 * top edge of this view and the top edge of its content.
2197 * {@hide}
2198 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002199 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002200 protected int mPaddingTop;
2201 /**
2202 * The bottom padding in pixels, that is the distance in pixels between the
2203 * bottom edge of this view and the bottom edge of its content.
2204 * {@hide}
2205 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002206 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002207 protected int mPaddingBottom;
2208
2209 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002210 * Briefly describes the view and is primarily used for accessibility support.
2211 */
2212 private CharSequence mContentDescription;
2213
2214 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002215 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002216 *
2217 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002218 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002219 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002220 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221
2222 /**
2223 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002224 *
2225 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002226 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002227 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002228 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002230 /**
Adam Powell20232d02010-12-08 21:08:53 -08002231 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002232 *
2233 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002234 */
2235 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002236 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002237
2238 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002239 * Cache if the user padding is relative.
2240 *
2241 */
2242 @ViewDebug.ExportedProperty(category = "padding")
2243 boolean mUserPaddingRelative;
2244
2245 /**
2246 * Cache the paddingStart set by the user to append to the scrollbar's size.
2247 *
2248 */
2249 @ViewDebug.ExportedProperty(category = "padding")
2250 int mUserPaddingStart;
2251
2252 /**
2253 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2254 *
2255 */
2256 @ViewDebug.ExportedProperty(category = "padding")
2257 int mUserPaddingEnd;
2258
2259 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002260 * @hide
2261 */
2262 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2263 /**
2264 * @hide
2265 */
2266 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002267
2268 private Resources mResources = null;
2269
2270 private Drawable mBGDrawable;
2271
2272 private int mBackgroundResource;
2273 private boolean mBackgroundSizeChanged;
2274
2275 /**
2276 * Listener used to dispatch focus change events.
2277 * This field should be made private, so it is hidden from the SDK.
2278 * {@hide}
2279 */
2280 protected OnFocusChangeListener mOnFocusChangeListener;
2281
2282 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002283 * Listeners for layout change events.
2284 */
2285 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2286
2287 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002288 * Listeners for attach events.
2289 */
2290 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2291
2292 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002293 * Listener used to dispatch click events.
2294 * This field should be made private, so it is hidden from the SDK.
2295 * {@hide}
2296 */
2297 protected OnClickListener mOnClickListener;
2298
2299 /**
2300 * Listener used to dispatch long click events.
2301 * This field should be made private, so it is hidden from the SDK.
2302 * {@hide}
2303 */
2304 protected OnLongClickListener mOnLongClickListener;
2305
2306 /**
2307 * Listener used to build the context menu.
2308 * This field should be made private, so it is hidden from the SDK.
2309 * {@hide}
2310 */
2311 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2312
2313 private OnKeyListener mOnKeyListener;
2314
2315 private OnTouchListener mOnTouchListener;
2316
Jeff Brown10b62902011-06-20 16:40:37 -07002317 private OnHoverListener mOnHoverListener;
2318
Jeff Brown33bbfd22011-02-24 20:55:35 -08002319 private OnGenericMotionListener mOnGenericMotionListener;
2320
Chris Tate32affef2010-10-18 15:29:21 -07002321 private OnDragListener mOnDragListener;
2322
Joe Onorato664644d2011-01-23 17:53:23 -08002323 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002325 /**
2326 * The application environment this view lives in.
2327 * This field should be made private, so it is hidden from the SDK.
2328 * {@hide}
2329 */
2330 protected Context mContext;
2331
2332 private ScrollabilityCache mScrollCache;
2333
2334 private int[] mDrawableState = null;
2335
Romain Guy0211a0a2011-02-14 16:34:59 -08002336 /**
2337 * Set to true when drawing cache is enabled and cannot be created.
2338 *
2339 * @hide
2340 */
2341 public boolean mCachingFailed;
2342
Romain Guy02890fd2010-08-06 17:58:44 -07002343 private Bitmap mDrawingCache;
2344 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002345 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002346 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002347
2348 /**
2349 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2350 * the user may specify which view to go to next.
2351 */
2352 private int mNextFocusLeftId = View.NO_ID;
2353
2354 /**
2355 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2356 * the user may specify which view to go to next.
2357 */
2358 private int mNextFocusRightId = View.NO_ID;
2359
2360 /**
2361 * When this view has focus and the next focus is {@link #FOCUS_UP},
2362 * the user may specify which view to go to next.
2363 */
2364 private int mNextFocusUpId = View.NO_ID;
2365
2366 /**
2367 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2368 * the user may specify which view to go to next.
2369 */
2370 private int mNextFocusDownId = View.NO_ID;
2371
Jeff Brown4e6319b2010-12-13 10:36:51 -08002372 /**
2373 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2374 * the user may specify which view to go to next.
2375 */
2376 int mNextFocusForwardId = View.NO_ID;
2377
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002378 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002379 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002380 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002381 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002382
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 private UnsetPressedState mUnsetPressedState;
2384
2385 /**
2386 * Whether the long press's action has been invoked. The tap's action is invoked on the
2387 * up event while a long press is invoked as soon as the long press duration is reached, so
2388 * a long press could be performed before the tap is checked, in which case the tap's action
2389 * should not be invoked.
2390 */
2391 private boolean mHasPerformedLongPress;
2392
2393 /**
2394 * The minimum height of the view. We'll try our best to have the height
2395 * of this view to at least this amount.
2396 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002397 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002398 private int mMinHeight;
2399
2400 /**
2401 * The minimum width of the view. We'll try our best to have the width
2402 * of this view to at least this amount.
2403 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002404 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002405 private int mMinWidth;
2406
2407 /**
2408 * The delegate to handle touch events that are physically in this view
2409 * but should be handled by another view.
2410 */
2411 private TouchDelegate mTouchDelegate = null;
2412
2413 /**
2414 * Solid color to use as a background when creating the drawing cache. Enables
2415 * the cache to use 16 bit bitmaps instead of 32 bit.
2416 */
2417 private int mDrawingCacheBackgroundColor = 0;
2418
2419 /**
2420 * Special tree observer used when mAttachInfo is null.
2421 */
2422 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002423
Adam Powelle14579b2009-12-16 18:39:52 -08002424 /**
2425 * Cache the touch slop from the context that created the view.
2426 */
2427 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002429 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002430 * Object that handles automatic animation of view properties.
2431 */
2432 private ViewPropertyAnimator mAnimator = null;
2433
2434 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002435 * Flag indicating that a drag can cross window boundaries. When
2436 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2437 * with this flag set, all visible applications will be able to participate
2438 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002439 *
2440 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002441 */
2442 public static final int DRAG_FLAG_GLOBAL = 1;
2443
2444 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002445 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2446 */
2447 private float mVerticalScrollFactor;
2448
2449 /**
Adam Powell20232d02010-12-08 21:08:53 -08002450 * Position of the vertical scroll bar.
2451 */
2452 private int mVerticalScrollbarPosition;
2453
2454 /**
2455 * Position the scroll bar at the default position as determined by the system.
2456 */
2457 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2458
2459 /**
2460 * Position the scroll bar along the left edge.
2461 */
2462 public static final int SCROLLBAR_POSITION_LEFT = 1;
2463
2464 /**
2465 * Position the scroll bar along the right edge.
2466 */
2467 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2468
2469 /**
Romain Guy171c5922011-01-06 10:04:23 -08002470 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002471 *
2472 * @see #getLayerType()
2473 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002474 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002475 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002476 */
2477 public static final int LAYER_TYPE_NONE = 0;
2478
2479 /**
2480 * <p>Indicates that the view has a software layer. A software layer is backed
2481 * by a bitmap and causes the view to be rendered using Android's software
2482 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002483 *
Romain Guy171c5922011-01-06 10:04:23 -08002484 * <p>Software layers have various usages:</p>
2485 * <p>When the application is not using hardware acceleration, a software layer
2486 * is useful to apply a specific color filter and/or blending mode and/or
2487 * translucency to a view and all its children.</p>
2488 * <p>When the application is using hardware acceleration, a software layer
2489 * is useful to render drawing primitives not supported by the hardware
2490 * accelerated pipeline. It can also be used to cache a complex view tree
2491 * into a texture and reduce the complexity of drawing operations. For instance,
2492 * when animating a complex view tree with a translation, a software layer can
2493 * be used to render the view tree only once.</p>
2494 * <p>Software layers should be avoided when the affected view tree updates
2495 * often. Every update will require to re-render the software layer, which can
2496 * potentially be slow (particularly when hardware acceleration is turned on
2497 * since the layer will have to be uploaded into a hardware texture after every
2498 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002499 *
2500 * @see #getLayerType()
2501 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002502 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002503 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002504 */
2505 public static final int LAYER_TYPE_SOFTWARE = 1;
2506
2507 /**
2508 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2509 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2510 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2511 * rendering pipeline, but only if hardware acceleration is turned on for the
2512 * view hierarchy. When hardware acceleration is turned off, hardware layers
2513 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002514 *
Romain Guy171c5922011-01-06 10:04:23 -08002515 * <p>A hardware layer is useful to apply a specific color filter and/or
2516 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002517 * <p>A hardware layer can be used to cache a complex view tree into a
2518 * texture and reduce the complexity of drawing operations. For instance,
2519 * when animating a complex view tree with a translation, a hardware layer can
2520 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002521 * <p>A hardware layer can also be used to increase the rendering quality when
2522 * rotation transformations are applied on a view. It can also be used to
2523 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002524 *
2525 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002526 * @see #setLayerType(int, android.graphics.Paint)
2527 * @see #LAYER_TYPE_NONE
2528 * @see #LAYER_TYPE_SOFTWARE
2529 */
2530 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002531
Romain Guy3aaff3a2011-01-12 14:18:47 -08002532 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2533 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2534 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2535 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2536 })
Romain Guy171c5922011-01-06 10:04:23 -08002537 int mLayerType = LAYER_TYPE_NONE;
2538 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002539 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002540
2541 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002542 * Set to true when the view is sending hover accessibility events because it
2543 * is the innermost hovered view.
2544 */
2545 private boolean mSendingHoverAccessibilityEvents;
2546
2547 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002548 * Text direction is inherited thru {@link ViewGroup}
2549 * @hide
2550 */
2551 public static final int TEXT_DIRECTION_INHERIT = 0;
2552
2553 /**
2554 * Text direction is using "first strong algorithm". The first strong directional character
2555 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002556 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002557 *
2558 * @hide
2559 */
2560 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2561
2562 /**
2563 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2564 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002565 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002566 *
2567 * @hide
2568 */
2569 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2570
2571 /**
2572 * Text direction is forced to LTR.
2573 *
2574 * @hide
2575 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002576 public static final int TEXT_DIRECTION_LTR = 3;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002577
2578 /**
2579 * Text direction is forced to RTL.
2580 *
2581 * @hide
2582 */
Fabrice Di Meglioe3bf88d2011-09-06 11:08:45 -07002583 public static final int TEXT_DIRECTION_RTL = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002584
2585 /**
2586 * Default text direction is inherited
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07002587 *
2588 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002589 */
2590 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2591
2592 /**
2593 * The text direction that has been defined by {@link #setTextDirection(int)}.
2594 *
2595 * {@hide}
2596 */
2597 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002598 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2599 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2600 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2601 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2602 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2603 })
Doug Feltcb3791202011-07-07 11:57:48 -07002604 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002605
2606 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002607 * The resolved text direction. This needs resolution if the value is
2608 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2609 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2610 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002611 *
2612 * {@hide}
2613 */
2614 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002615 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2616 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2617 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002618 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2619 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2620 })
Doug Feltcb3791202011-07-07 11:57:48 -07002621 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002622
2623 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002624 * Consistency verifier for debugging purposes.
2625 * @hide
2626 */
2627 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2628 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2629 new InputEventConsistencyVerifier(this, 0) : null;
2630
2631 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002632 * Simple constructor to use when creating a view from code.
2633 *
2634 * @param context The Context the view is running in, through which it can
2635 * access the current theme, resources, etc.
2636 */
2637 public View(Context context) {
2638 mContext = context;
2639 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002640 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002641 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002642 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002643 mUserPaddingStart = -1;
2644 mUserPaddingEnd = -1;
2645 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002646 }
2647
2648 /**
2649 * Constructor that is called when inflating a view from XML. This is called
2650 * when a view is being constructed from an XML file, supplying attributes
2651 * that were specified in the XML file. This version uses a default style of
2652 * 0, so the only attribute values applied are those in the Context's Theme
2653 * and the given AttributeSet.
2654 *
2655 * <p>
2656 * The method onFinishInflate() will be called after all children have been
2657 * added.
2658 *
2659 * @param context The Context the view is running in, through which it can
2660 * access the current theme, resources, etc.
2661 * @param attrs The attributes of the XML tag that is inflating the view.
2662 * @see #View(Context, AttributeSet, int)
2663 */
2664 public View(Context context, AttributeSet attrs) {
2665 this(context, attrs, 0);
2666 }
2667
2668 /**
2669 * Perform inflation from XML and apply a class-specific base style. This
2670 * constructor of View allows subclasses to use their own base style when
2671 * they are inflating. For example, a Button class's constructor would call
2672 * this version of the super class constructor and supply
2673 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2674 * the theme's button style to modify all of the base view attributes (in
2675 * particular its background) as well as the Button class's attributes.
2676 *
2677 * @param context The Context the view is running in, through which it can
2678 * access the current theme, resources, etc.
2679 * @param attrs The attributes of the XML tag that is inflating the view.
2680 * @param defStyle The default style to apply to this view. If 0, no style
2681 * will be applied (beyond what is included in the theme). This may
2682 * either be an attribute resource, whose value will be retrieved
2683 * from the current theme, or an explicit style resource.
2684 * @see #View(Context, AttributeSet)
2685 */
2686 public View(Context context, AttributeSet attrs, int defStyle) {
2687 this(context);
2688
2689 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2690 defStyle, 0);
2691
2692 Drawable background = null;
2693
2694 int leftPadding = -1;
2695 int topPadding = -1;
2696 int rightPadding = -1;
2697 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002698 int startPadding = -1;
2699 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002700
2701 int padding = -1;
2702
2703 int viewFlagValues = 0;
2704 int viewFlagMasks = 0;
2705
2706 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002708 int x = 0;
2709 int y = 0;
2710
Chet Haase73066682010-11-29 15:55:32 -08002711 float tx = 0;
2712 float ty = 0;
2713 float rotation = 0;
2714 float rotationX = 0;
2715 float rotationY = 0;
2716 float sx = 1f;
2717 float sy = 1f;
2718 boolean transformSet = false;
2719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2721
Adam Powell637d3372010-08-25 14:37:03 -07002722 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002723 final int N = a.getIndexCount();
2724 for (int i = 0; i < N; i++) {
2725 int attr = a.getIndex(i);
2726 switch (attr) {
2727 case com.android.internal.R.styleable.View_background:
2728 background = a.getDrawable(attr);
2729 break;
2730 case com.android.internal.R.styleable.View_padding:
2731 padding = a.getDimensionPixelSize(attr, -1);
2732 break;
2733 case com.android.internal.R.styleable.View_paddingLeft:
2734 leftPadding = a.getDimensionPixelSize(attr, -1);
2735 break;
2736 case com.android.internal.R.styleable.View_paddingTop:
2737 topPadding = a.getDimensionPixelSize(attr, -1);
2738 break;
2739 case com.android.internal.R.styleable.View_paddingRight:
2740 rightPadding = a.getDimensionPixelSize(attr, -1);
2741 break;
2742 case com.android.internal.R.styleable.View_paddingBottom:
2743 bottomPadding = a.getDimensionPixelSize(attr, -1);
2744 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002745 case com.android.internal.R.styleable.View_paddingStart:
2746 startPadding = a.getDimensionPixelSize(attr, -1);
2747 break;
2748 case com.android.internal.R.styleable.View_paddingEnd:
2749 endPadding = a.getDimensionPixelSize(attr, -1);
2750 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002751 case com.android.internal.R.styleable.View_scrollX:
2752 x = a.getDimensionPixelOffset(attr, 0);
2753 break;
2754 case com.android.internal.R.styleable.View_scrollY:
2755 y = a.getDimensionPixelOffset(attr, 0);
2756 break;
Chet Haase73066682010-11-29 15:55:32 -08002757 case com.android.internal.R.styleable.View_alpha:
2758 setAlpha(a.getFloat(attr, 1f));
2759 break;
2760 case com.android.internal.R.styleable.View_transformPivotX:
2761 setPivotX(a.getDimensionPixelOffset(attr, 0));
2762 break;
2763 case com.android.internal.R.styleable.View_transformPivotY:
2764 setPivotY(a.getDimensionPixelOffset(attr, 0));
2765 break;
2766 case com.android.internal.R.styleable.View_translationX:
2767 tx = a.getDimensionPixelOffset(attr, 0);
2768 transformSet = true;
2769 break;
2770 case com.android.internal.R.styleable.View_translationY:
2771 ty = a.getDimensionPixelOffset(attr, 0);
2772 transformSet = true;
2773 break;
2774 case com.android.internal.R.styleable.View_rotation:
2775 rotation = a.getFloat(attr, 0);
2776 transformSet = true;
2777 break;
2778 case com.android.internal.R.styleable.View_rotationX:
2779 rotationX = a.getFloat(attr, 0);
2780 transformSet = true;
2781 break;
2782 case com.android.internal.R.styleable.View_rotationY:
2783 rotationY = a.getFloat(attr, 0);
2784 transformSet = true;
2785 break;
2786 case com.android.internal.R.styleable.View_scaleX:
2787 sx = a.getFloat(attr, 1f);
2788 transformSet = true;
2789 break;
2790 case com.android.internal.R.styleable.View_scaleY:
2791 sy = a.getFloat(attr, 1f);
2792 transformSet = true;
2793 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002794 case com.android.internal.R.styleable.View_id:
2795 mID = a.getResourceId(attr, NO_ID);
2796 break;
2797 case com.android.internal.R.styleable.View_tag:
2798 mTag = a.getText(attr);
2799 break;
2800 case com.android.internal.R.styleable.View_fitsSystemWindows:
2801 if (a.getBoolean(attr, false)) {
2802 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2803 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2804 }
2805 break;
2806 case com.android.internal.R.styleable.View_focusable:
2807 if (a.getBoolean(attr, false)) {
2808 viewFlagValues |= FOCUSABLE;
2809 viewFlagMasks |= FOCUSABLE_MASK;
2810 }
2811 break;
2812 case com.android.internal.R.styleable.View_focusableInTouchMode:
2813 if (a.getBoolean(attr, false)) {
2814 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2815 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2816 }
2817 break;
2818 case com.android.internal.R.styleable.View_clickable:
2819 if (a.getBoolean(attr, false)) {
2820 viewFlagValues |= CLICKABLE;
2821 viewFlagMasks |= CLICKABLE;
2822 }
2823 break;
2824 case com.android.internal.R.styleable.View_longClickable:
2825 if (a.getBoolean(attr, false)) {
2826 viewFlagValues |= LONG_CLICKABLE;
2827 viewFlagMasks |= LONG_CLICKABLE;
2828 }
2829 break;
2830 case com.android.internal.R.styleable.View_saveEnabled:
2831 if (!a.getBoolean(attr, true)) {
2832 viewFlagValues |= SAVE_DISABLED;
2833 viewFlagMasks |= SAVE_DISABLED_MASK;
2834 }
2835 break;
2836 case com.android.internal.R.styleable.View_duplicateParentState:
2837 if (a.getBoolean(attr, false)) {
2838 viewFlagValues |= DUPLICATE_PARENT_STATE;
2839 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2840 }
2841 break;
2842 case com.android.internal.R.styleable.View_visibility:
2843 final int visibility = a.getInt(attr, 0);
2844 if (visibility != 0) {
2845 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2846 viewFlagMasks |= VISIBILITY_MASK;
2847 }
2848 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002849 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002850 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002851 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002852 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002853 final int layoutDirection = a.getInt(attr, -1);
2854 if (layoutDirection != -1) {
2855 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002856 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002857 // Set to default (LAYOUT_DIRECTION_INHERIT)
2858 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002859 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002860 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002861 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002862 case com.android.internal.R.styleable.View_drawingCacheQuality:
2863 final int cacheQuality = a.getInt(attr, 0);
2864 if (cacheQuality != 0) {
2865 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2866 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2867 }
2868 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002869 case com.android.internal.R.styleable.View_contentDescription:
2870 mContentDescription = a.getString(attr);
2871 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002872 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2873 if (!a.getBoolean(attr, true)) {
2874 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2875 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2876 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002877 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002878 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2879 if (!a.getBoolean(attr, true)) {
2880 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2881 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2882 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002883 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 case R.styleable.View_scrollbars:
2885 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2886 if (scrollbars != SCROLLBARS_NONE) {
2887 viewFlagValues |= scrollbars;
2888 viewFlagMasks |= SCROLLBARS_MASK;
2889 initializeScrollbars(a);
2890 }
2891 break;
2892 case R.styleable.View_fadingEdge:
2893 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2894 if (fadingEdge != FADING_EDGE_NONE) {
2895 viewFlagValues |= fadingEdge;
2896 viewFlagMasks |= FADING_EDGE_MASK;
2897 initializeFadingEdge(a);
2898 }
2899 break;
2900 case R.styleable.View_scrollbarStyle:
2901 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2902 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2903 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2904 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2905 }
2906 break;
2907 case R.styleable.View_isScrollContainer:
2908 setScrollContainer = true;
2909 if (a.getBoolean(attr, false)) {
2910 setScrollContainer(true);
2911 }
2912 break;
2913 case com.android.internal.R.styleable.View_keepScreenOn:
2914 if (a.getBoolean(attr, false)) {
2915 viewFlagValues |= KEEP_SCREEN_ON;
2916 viewFlagMasks |= KEEP_SCREEN_ON;
2917 }
2918 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002919 case R.styleable.View_filterTouchesWhenObscured:
2920 if (a.getBoolean(attr, false)) {
2921 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2922 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2923 }
2924 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002925 case R.styleable.View_nextFocusLeft:
2926 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2927 break;
2928 case R.styleable.View_nextFocusRight:
2929 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2930 break;
2931 case R.styleable.View_nextFocusUp:
2932 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2933 break;
2934 case R.styleable.View_nextFocusDown:
2935 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2936 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002937 case R.styleable.View_nextFocusForward:
2938 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2939 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002940 case R.styleable.View_minWidth:
2941 mMinWidth = a.getDimensionPixelSize(attr, 0);
2942 break;
2943 case R.styleable.View_minHeight:
2944 mMinHeight = a.getDimensionPixelSize(attr, 0);
2945 break;
Romain Guy9a817362009-05-01 10:57:14 -07002946 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002947 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002948 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002949 + "be used within a restricted context");
2950 }
2951
Romain Guy9a817362009-05-01 10:57:14 -07002952 final String handlerName = a.getString(attr);
2953 if (handlerName != null) {
2954 setOnClickListener(new OnClickListener() {
2955 private Method mHandler;
2956
2957 public void onClick(View v) {
2958 if (mHandler == null) {
2959 try {
2960 mHandler = getContext().getClass().getMethod(handlerName,
2961 View.class);
2962 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002963 int id = getId();
2964 String idText = id == NO_ID ? "" : " with id '"
2965 + getContext().getResources().getResourceEntryName(
2966 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002967 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002968 handlerName + "(View) in the activity "
2969 + getContext().getClass() + " for onClick handler"
2970 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002971 }
2972 }
2973
2974 try {
2975 mHandler.invoke(getContext(), View.this);
2976 } catch (IllegalAccessException e) {
2977 throw new IllegalStateException("Could not execute non "
2978 + "public method of the activity", e);
2979 } catch (InvocationTargetException e) {
2980 throw new IllegalStateException("Could not execute "
2981 + "method of the activity", e);
2982 }
2983 }
2984 });
2985 }
2986 break;
Adam Powell637d3372010-08-25 14:37:03 -07002987 case R.styleable.View_overScrollMode:
2988 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
2989 break;
Adam Powell20232d02010-12-08 21:08:53 -08002990 case R.styleable.View_verticalScrollbarPosition:
2991 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
2992 break;
Romain Guy171c5922011-01-06 10:04:23 -08002993 case R.styleable.View_layerType:
2994 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
2995 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002996 case R.styleable.View_textDirection:
2997 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
2998 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002999 }
3000 }
3001
Adam Powell637d3372010-08-25 14:37:03 -07003002 setOverScrollMode(overScrollMode);
3003
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003004 if (background != null) {
3005 setBackgroundDrawable(background);
3006 }
3007
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003008 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3009
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003010 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3011 // layout direction). Those cached values will be used later during padding resolution.
3012 mUserPaddingStart = startPadding;
3013 mUserPaddingEnd = endPadding;
3014
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003015 if (padding >= 0) {
3016 leftPadding = padding;
3017 topPadding = padding;
3018 rightPadding = padding;
3019 bottomPadding = padding;
3020 }
3021
3022 // If the user specified the padding (either with android:padding or
3023 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3024 // use the default padding or the padding from the background drawable
3025 // (stored at this point in mPadding*)
3026 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3027 topPadding >= 0 ? topPadding : mPaddingTop,
3028 rightPadding >= 0 ? rightPadding : mPaddingRight,
3029 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3030
3031 if (viewFlagMasks != 0) {
3032 setFlags(viewFlagValues, viewFlagMasks);
3033 }
3034
3035 // Needs to be called after mViewFlags is set
3036 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3037 recomputePadding();
3038 }
3039
3040 if (x != 0 || y != 0) {
3041 scrollTo(x, y);
3042 }
3043
Chet Haase73066682010-11-29 15:55:32 -08003044 if (transformSet) {
3045 setTranslationX(tx);
3046 setTranslationY(ty);
3047 setRotation(rotation);
3048 setRotationX(rotationX);
3049 setRotationY(rotationY);
3050 setScaleX(sx);
3051 setScaleY(sy);
3052 }
3053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003054 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3055 setScrollContainer(true);
3056 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003057
3058 computeOpaqueFlags();
3059
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003060 a.recycle();
3061 }
3062
3063 /**
3064 * Non-public constructor for use in testing
3065 */
3066 View() {
3067 }
3068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003069 /**
3070 * <p>
3071 * Initializes the fading edges from a given set of styled attributes. This
3072 * method should be called by subclasses that need fading edges and when an
3073 * instance of these subclasses is created programmatically rather than
3074 * being inflated from XML. This method is automatically called when the XML
3075 * is inflated.
3076 * </p>
3077 *
3078 * @param a the styled attributes set to initialize the fading edges from
3079 */
3080 protected void initializeFadingEdge(TypedArray a) {
3081 initScrollCache();
3082
3083 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3084 R.styleable.View_fadingEdgeLength,
3085 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3086 }
3087
3088 /**
3089 * Returns the size of the vertical faded edges used to indicate that more
3090 * content in this view is visible.
3091 *
3092 * @return The size in pixels of the vertical faded edge or 0 if vertical
3093 * faded edges are not enabled for this view.
3094 * @attr ref android.R.styleable#View_fadingEdgeLength
3095 */
3096 public int getVerticalFadingEdgeLength() {
3097 if (isVerticalFadingEdgeEnabled()) {
3098 ScrollabilityCache cache = mScrollCache;
3099 if (cache != null) {
3100 return cache.fadingEdgeLength;
3101 }
3102 }
3103 return 0;
3104 }
3105
3106 /**
3107 * Set the size of the faded edge used to indicate that more content in this
3108 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003109 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3110 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3111 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003112 *
3113 * @param length The size in pixels of the faded edge used to indicate that more
3114 * content in this view is visible.
3115 */
3116 public void setFadingEdgeLength(int length) {
3117 initScrollCache();
3118 mScrollCache.fadingEdgeLength = length;
3119 }
3120
3121 /**
3122 * Returns the size of the horizontal faded edges used to indicate that more
3123 * content in this view is visible.
3124 *
3125 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3126 * faded edges are not enabled for this view.
3127 * @attr ref android.R.styleable#View_fadingEdgeLength
3128 */
3129 public int getHorizontalFadingEdgeLength() {
3130 if (isHorizontalFadingEdgeEnabled()) {
3131 ScrollabilityCache cache = mScrollCache;
3132 if (cache != null) {
3133 return cache.fadingEdgeLength;
3134 }
3135 }
3136 return 0;
3137 }
3138
3139 /**
3140 * Returns the width of the vertical scrollbar.
3141 *
3142 * @return The width in pixels of the vertical scrollbar or 0 if there
3143 * is no vertical scrollbar.
3144 */
3145 public int getVerticalScrollbarWidth() {
3146 ScrollabilityCache cache = mScrollCache;
3147 if (cache != null) {
3148 ScrollBarDrawable scrollBar = cache.scrollBar;
3149 if (scrollBar != null) {
3150 int size = scrollBar.getSize(true);
3151 if (size <= 0) {
3152 size = cache.scrollBarSize;
3153 }
3154 return size;
3155 }
3156 return 0;
3157 }
3158 return 0;
3159 }
3160
3161 /**
3162 * Returns the height of the horizontal scrollbar.
3163 *
3164 * @return The height in pixels of the horizontal scrollbar or 0 if
3165 * there is no horizontal scrollbar.
3166 */
3167 protected int getHorizontalScrollbarHeight() {
3168 ScrollabilityCache cache = mScrollCache;
3169 if (cache != null) {
3170 ScrollBarDrawable scrollBar = cache.scrollBar;
3171 if (scrollBar != null) {
3172 int size = scrollBar.getSize(false);
3173 if (size <= 0) {
3174 size = cache.scrollBarSize;
3175 }
3176 return size;
3177 }
3178 return 0;
3179 }
3180 return 0;
3181 }
3182
3183 /**
3184 * <p>
3185 * Initializes the scrollbars from a given set of styled attributes. This
3186 * method should be called by subclasses that need scrollbars and when an
3187 * instance of these subclasses is created programmatically rather than
3188 * being inflated from XML. This method is automatically called when the XML
3189 * is inflated.
3190 * </p>
3191 *
3192 * @param a the styled attributes set to initialize the scrollbars from
3193 */
3194 protected void initializeScrollbars(TypedArray a) {
3195 initScrollCache();
3196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003197 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003198
Mike Cleronf116bf82009-09-27 19:14:12 -07003199 if (scrollabilityCache.scrollBar == null) {
3200 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3201 }
Joe Malin32736f02011-01-19 16:14:20 -08003202
Romain Guy8bda2482010-03-02 11:42:11 -08003203 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003204
Mike Cleronf116bf82009-09-27 19:14:12 -07003205 if (!fadeScrollbars) {
3206 scrollabilityCache.state = ScrollabilityCache.ON;
3207 }
3208 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003209
3210
Mike Cleronf116bf82009-09-27 19:14:12 -07003211 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3212 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3213 .getScrollBarFadeDuration());
3214 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3215 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3216 ViewConfiguration.getScrollDefaultDelay());
3217
Joe Malin32736f02011-01-19 16:14:20 -08003218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003219 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3220 com.android.internal.R.styleable.View_scrollbarSize,
3221 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3222
3223 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3224 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3225
3226 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3227 if (thumb != null) {
3228 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3229 }
3230
3231 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3232 false);
3233 if (alwaysDraw) {
3234 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3235 }
3236
3237 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3238 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3239
3240 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3241 if (thumb != null) {
3242 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3243 }
3244
3245 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3246 false);
3247 if (alwaysDraw) {
3248 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3249 }
3250
3251 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003252 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003253 }
3254
3255 /**
3256 * <p>
3257 * Initalizes the scrollability cache if necessary.
3258 * </p>
3259 */
3260 private void initScrollCache() {
3261 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003262 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003263 }
3264 }
3265
3266 /**
Adam Powell20232d02010-12-08 21:08:53 -08003267 * Set the position of the vertical scroll bar. Should be one of
3268 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3269 * {@link #SCROLLBAR_POSITION_RIGHT}.
3270 *
3271 * @param position Where the vertical scroll bar should be positioned.
3272 */
3273 public void setVerticalScrollbarPosition(int position) {
3274 if (mVerticalScrollbarPosition != position) {
3275 mVerticalScrollbarPosition = position;
3276 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003277 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003278 }
3279 }
3280
3281 /**
3282 * @return The position where the vertical scroll bar will show, if applicable.
3283 * @see #setVerticalScrollbarPosition(int)
3284 */
3285 public int getVerticalScrollbarPosition() {
3286 return mVerticalScrollbarPosition;
3287 }
3288
3289 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003290 * Register a callback to be invoked when focus of this view changed.
3291 *
3292 * @param l The callback that will run.
3293 */
3294 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3295 mOnFocusChangeListener = l;
3296 }
3297
3298 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003299 * Add a listener that will be called when the bounds of the view change due to
3300 * layout processing.
3301 *
3302 * @param listener The listener that will be called when layout bounds change.
3303 */
3304 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3305 if (mOnLayoutChangeListeners == null) {
3306 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3307 }
3308 mOnLayoutChangeListeners.add(listener);
3309 }
3310
3311 /**
3312 * Remove a listener for layout changes.
3313 *
3314 * @param listener The listener for layout bounds change.
3315 */
3316 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3317 if (mOnLayoutChangeListeners == null) {
3318 return;
3319 }
3320 mOnLayoutChangeListeners.remove(listener);
3321 }
3322
3323 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003324 * Add a listener for attach state changes.
3325 *
3326 * This listener will be called whenever this view is attached or detached
3327 * from a window. Remove the listener using
3328 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3329 *
3330 * @param listener Listener to attach
3331 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3332 */
3333 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3334 if (mOnAttachStateChangeListeners == null) {
3335 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3336 }
3337 mOnAttachStateChangeListeners.add(listener);
3338 }
3339
3340 /**
3341 * Remove a listener for attach state changes. The listener will receive no further
3342 * notification of window attach/detach events.
3343 *
3344 * @param listener Listener to remove
3345 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3346 */
3347 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3348 if (mOnAttachStateChangeListeners == null) {
3349 return;
3350 }
3351 mOnAttachStateChangeListeners.remove(listener);
3352 }
3353
3354 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003355 * Returns the focus-change callback registered for this view.
3356 *
3357 * @return The callback, or null if one is not registered.
3358 */
3359 public OnFocusChangeListener getOnFocusChangeListener() {
3360 return mOnFocusChangeListener;
3361 }
3362
3363 /**
3364 * Register a callback to be invoked when this view is clicked. If this view is not
3365 * clickable, it becomes clickable.
3366 *
3367 * @param l The callback that will run
3368 *
3369 * @see #setClickable(boolean)
3370 */
3371 public void setOnClickListener(OnClickListener l) {
3372 if (!isClickable()) {
3373 setClickable(true);
3374 }
3375 mOnClickListener = l;
3376 }
3377
3378 /**
3379 * Register a callback to be invoked when this view is clicked and held. If this view is not
3380 * long clickable, it becomes long clickable.
3381 *
3382 * @param l The callback that will run
3383 *
3384 * @see #setLongClickable(boolean)
3385 */
3386 public void setOnLongClickListener(OnLongClickListener l) {
3387 if (!isLongClickable()) {
3388 setLongClickable(true);
3389 }
3390 mOnLongClickListener = l;
3391 }
3392
3393 /**
3394 * Register a callback to be invoked when the context menu for this view is
3395 * being built. If this view is not long clickable, it becomes long clickable.
3396 *
3397 * @param l The callback that will run
3398 *
3399 */
3400 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3401 if (!isLongClickable()) {
3402 setLongClickable(true);
3403 }
3404 mOnCreateContextMenuListener = l;
3405 }
3406
3407 /**
3408 * Call this view's OnClickListener, if it is defined.
3409 *
3410 * @return True there was an assigned OnClickListener that was called, false
3411 * otherwise is returned.
3412 */
3413 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003414 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3415
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003416 if (mOnClickListener != null) {
3417 playSoundEffect(SoundEffectConstants.CLICK);
3418 mOnClickListener.onClick(this);
3419 return true;
3420 }
3421
3422 return false;
3423 }
3424
3425 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003426 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3427 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003429 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003430 */
3431 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003432 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3433
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003434 boolean handled = false;
3435 if (mOnLongClickListener != null) {
3436 handled = mOnLongClickListener.onLongClick(View.this);
3437 }
3438 if (!handled) {
3439 handled = showContextMenu();
3440 }
3441 if (handled) {
3442 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3443 }
3444 return handled;
3445 }
3446
3447 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003448 * Performs button-related actions during a touch down event.
3449 *
3450 * @param event The event.
3451 * @return True if the down was consumed.
3452 *
3453 * @hide
3454 */
3455 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3456 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3457 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3458 return true;
3459 }
3460 }
3461 return false;
3462 }
3463
3464 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003465 * Bring up the context menu for this view.
3466 *
3467 * @return Whether a context menu was displayed.
3468 */
3469 public boolean showContextMenu() {
3470 return getParent().showContextMenuForChild(this);
3471 }
3472
3473 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003474 * Bring up the context menu for this view, referring to the item under the specified point.
3475 *
3476 * @param x The referenced x coordinate.
3477 * @param y The referenced y coordinate.
3478 * @param metaState The keyboard modifiers that were pressed.
3479 * @return Whether a context menu was displayed.
3480 *
3481 * @hide
3482 */
3483 public boolean showContextMenu(float x, float y, int metaState) {
3484 return showContextMenu();
3485 }
3486
3487 /**
Adam Powell6e346362010-07-23 10:18:23 -07003488 * Start an action mode.
3489 *
3490 * @param callback Callback that will control the lifecycle of the action mode
3491 * @return The new action mode if it is started, null otherwise
3492 *
3493 * @see ActionMode
3494 */
3495 public ActionMode startActionMode(ActionMode.Callback callback) {
3496 return getParent().startActionModeForChild(this, callback);
3497 }
3498
3499 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003500 * Register a callback to be invoked when a key is pressed in this view.
3501 * @param l the key listener to attach to this view
3502 */
3503 public void setOnKeyListener(OnKeyListener l) {
3504 mOnKeyListener = l;
3505 }
3506
3507 /**
3508 * Register a callback to be invoked when a touch event is sent to this view.
3509 * @param l the touch listener to attach to this view
3510 */
3511 public void setOnTouchListener(OnTouchListener l) {
3512 mOnTouchListener = l;
3513 }
3514
3515 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003516 * Register a callback to be invoked when a generic motion event is sent to this view.
3517 * @param l the generic motion listener to attach to this view
3518 */
3519 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3520 mOnGenericMotionListener = l;
3521 }
3522
3523 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003524 * Register a callback to be invoked when a hover event is sent to this view.
3525 * @param l the hover listener to attach to this view
3526 */
3527 public void setOnHoverListener(OnHoverListener l) {
3528 mOnHoverListener = l;
3529 }
3530
3531 /**
Joe Malin32736f02011-01-19 16:14:20 -08003532 * Register a drag event listener callback object for this View. The parameter is
3533 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3534 * View, the system calls the
3535 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3536 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003537 */
3538 public void setOnDragListener(OnDragListener l) {
3539 mOnDragListener = l;
3540 }
3541
3542 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003543 * Give this view focus. This will cause
3544 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003545 *
3546 * Note: this does not check whether this {@link View} should get focus, it just
3547 * gives it focus no matter what. It should only be called internally by framework
3548 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3549 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003550 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3551 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003552 * focus moved when requestFocus() is called. It may not always
3553 * apply, in which case use the default View.FOCUS_DOWN.
3554 * @param previouslyFocusedRect The rectangle of the view that had focus
3555 * prior in this View's coordinate system.
3556 */
3557 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3558 if (DBG) {
3559 System.out.println(this + " requestFocus()");
3560 }
3561
3562 if ((mPrivateFlags & FOCUSED) == 0) {
3563 mPrivateFlags |= FOCUSED;
3564
3565 if (mParent != null) {
3566 mParent.requestChildFocus(this, this);
3567 }
3568
3569 onFocusChanged(true, direction, previouslyFocusedRect);
3570 refreshDrawableState();
3571 }
3572 }
3573
3574 /**
3575 * Request that a rectangle of this view be visible on the screen,
3576 * scrolling if necessary just enough.
3577 *
3578 * <p>A View should call this if it maintains some notion of which part
3579 * of its content is interesting. For example, a text editing view
3580 * should call this when its cursor moves.
3581 *
3582 * @param rectangle The rectangle.
3583 * @return Whether any parent scrolled.
3584 */
3585 public boolean requestRectangleOnScreen(Rect rectangle) {
3586 return requestRectangleOnScreen(rectangle, false);
3587 }
3588
3589 /**
3590 * Request that a rectangle of this view be visible on the screen,
3591 * scrolling if necessary just enough.
3592 *
3593 * <p>A View should call this if it maintains some notion of which part
3594 * of its content is interesting. For example, a text editing view
3595 * should call this when its cursor moves.
3596 *
3597 * <p>When <code>immediate</code> is set to true, scrolling will not be
3598 * animated.
3599 *
3600 * @param rectangle The rectangle.
3601 * @param immediate True to forbid animated scrolling, false otherwise
3602 * @return Whether any parent scrolled.
3603 */
3604 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3605 View child = this;
3606 ViewParent parent = mParent;
3607 boolean scrolled = false;
3608 while (parent != null) {
3609 scrolled |= parent.requestChildRectangleOnScreen(child,
3610 rectangle, immediate);
3611
3612 // offset rect so next call has the rectangle in the
3613 // coordinate system of its direct child.
3614 rectangle.offset(child.getLeft(), child.getTop());
3615 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3616
3617 if (!(parent instanceof View)) {
3618 break;
3619 }
Romain Guy8506ab42009-06-11 17:35:47 -07003620
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003621 child = (View) parent;
3622 parent = child.getParent();
3623 }
3624 return scrolled;
3625 }
3626
3627 /**
3628 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003629 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003630 */
3631 public void clearFocus() {
3632 if (DBG) {
3633 System.out.println(this + " clearFocus()");
3634 }
3635
3636 if ((mPrivateFlags & FOCUSED) != 0) {
3637 mPrivateFlags &= ~FOCUSED;
3638
3639 if (mParent != null) {
3640 mParent.clearChildFocus(this);
3641 }
3642
3643 onFocusChanged(false, 0, null);
3644 refreshDrawableState();
3645 }
3646 }
3647
3648 /**
3649 * Called to clear the focus of a view that is about to be removed.
3650 * Doesn't call clearChildFocus, which prevents this view from taking
3651 * focus again before it has been removed from the parent
3652 */
3653 void clearFocusForRemoval() {
3654 if ((mPrivateFlags & FOCUSED) != 0) {
3655 mPrivateFlags &= ~FOCUSED;
3656
3657 onFocusChanged(false, 0, null);
3658 refreshDrawableState();
3659 }
3660 }
3661
3662 /**
3663 * Called internally by the view system when a new view is getting focus.
3664 * This is what clears the old focus.
3665 */
3666 void unFocus() {
3667 if (DBG) {
3668 System.out.println(this + " unFocus()");
3669 }
3670
3671 if ((mPrivateFlags & FOCUSED) != 0) {
3672 mPrivateFlags &= ~FOCUSED;
3673
3674 onFocusChanged(false, 0, null);
3675 refreshDrawableState();
3676 }
3677 }
3678
3679 /**
3680 * Returns true if this view has focus iteself, or is the ancestor of the
3681 * view that has focus.
3682 *
3683 * @return True if this view has or contains focus, false otherwise.
3684 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003685 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003686 public boolean hasFocus() {
3687 return (mPrivateFlags & FOCUSED) != 0;
3688 }
3689
3690 /**
3691 * Returns true if this view is focusable or if it contains a reachable View
3692 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3693 * is a View whose parents do not block descendants focus.
3694 *
3695 * Only {@link #VISIBLE} views are considered focusable.
3696 *
3697 * @return True if the view is focusable or if the view contains a focusable
3698 * View, false otherwise.
3699 *
3700 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3701 */
3702 public boolean hasFocusable() {
3703 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3704 }
3705
3706 /**
3707 * Called by the view system when the focus state of this view changes.
3708 * When the focus change event is caused by directional navigation, direction
3709 * and previouslyFocusedRect provide insight into where the focus is coming from.
3710 * When overriding, be sure to call up through to the super class so that
3711 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003712 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003713 * @param gainFocus True if the View has focus; false otherwise.
3714 * @param direction The direction focus has moved when requestFocus()
3715 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003716 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3717 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3718 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003719 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3720 * system, of the previously focused view. If applicable, this will be
3721 * passed in as finer grained information about where the focus is coming
3722 * from (in addition to direction). Will be <code>null</code> otherwise.
3723 */
3724 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003725 if (gainFocus) {
3726 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3727 }
3728
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003729 InputMethodManager imm = InputMethodManager.peekInstance();
3730 if (!gainFocus) {
3731 if (isPressed()) {
3732 setPressed(false);
3733 }
3734 if (imm != null && mAttachInfo != null
3735 && mAttachInfo.mHasWindowFocus) {
3736 imm.focusOut(this);
3737 }
Romain Guya2431d02009-04-30 16:30:00 -07003738 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003739 } else if (imm != null && mAttachInfo != null
3740 && mAttachInfo.mHasWindowFocus) {
3741 imm.focusIn(this);
3742 }
Romain Guy8506ab42009-06-11 17:35:47 -07003743
Romain Guy0fd89bf2011-01-26 15:41:30 -08003744 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003745 if (mOnFocusChangeListener != null) {
3746 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3747 }
Joe Malin32736f02011-01-19 16:14:20 -08003748
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003749 if (mAttachInfo != null) {
3750 mAttachInfo.mKeyDispatchState.reset(this);
3751 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003752 }
3753
3754 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003755 * Sends an accessibility event of the given type. If accessiiblity is
3756 * not enabled this method has no effect. The default implementation calls
3757 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3758 * to populate information about the event source (this View), then calls
3759 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3760 * populate the text content of the event source including its descendants,
3761 * and last calls
3762 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3763 * on its parent to resuest sending of the event to interested parties.
3764 *
3765 * @param eventType The type of the event to send.
3766 *
3767 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3768 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3769 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003770 */
3771 public void sendAccessibilityEvent(int eventType) {
3772 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3773 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3774 }
3775 }
3776
3777 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003778 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3779 * takes as an argument an empty {@link AccessibilityEvent} and does not
3780 * perfrom a check whether accessibility is enabled.
3781 *
3782 * @param event The event to send.
3783 *
3784 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003785 */
3786 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003787 if (!isShown()) {
3788 return;
3789 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003790 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003791 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003792 // In the beginning we called #isShown(), so we know that getParent() is not null.
3793 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003794 }
3795
3796 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003797 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3798 * to its children for adding their text content to the event. Note that the
3799 * event text is populated in a separate dispatch path since we add to the
3800 * event not only the text of the source but also the text of all its descendants.
3801 * </p>
3802 * A typical implementation will call
3803 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3804 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3805 * on each child. Override this method if custom population of the event text
3806 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003807 *
3808 * @param event The event.
3809 *
3810 * @return True if the event population was completed.
3811 */
3812 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003813 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003814 return false;
3815 }
3816
3817 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003818 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003819 * giving a chance to this View to populate the accessibility event with its
3820 * text content. While the implementation is free to modify other event
3821 * attributes this should be performed in
3822 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3823 * <p>
3824 * Example: Adding formatted date string to an accessibility event in addition
3825 * to the text added by the super implementation.
3826 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003827 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3828 * super.onPopulateAccessibilityEvent(event);
3829 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3830 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3831 * mCurrentDate.getTimeInMillis(), flags);
3832 * event.getText().add(selectedDateUtterance);
3833 * }
3834 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003835 *
3836 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003837 *
3838 * @see #sendAccessibilityEvent(int)
3839 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003840 */
3841 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003842 }
3843
3844 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003845 * Initializes an {@link AccessibilityEvent} with information about the
3846 * the type of the event and this View which is the event source. In other
3847 * words, the source of an accessibility event is the view whose state
3848 * change triggered firing the event.
3849 * <p>
3850 * Example: Setting the password property of an event in addition
3851 * to properties set by the super implementation.
3852 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003853 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3854 * super.onInitializeAccessibilityEvent(event);
3855 * event.setPassword(true);
3856 * }
3857 * </code></pre></p>
3858 * @param event The event to initialeze.
3859 *
3860 * @see #sendAccessibilityEvent(int)
3861 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3862 */
3863 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003864 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003865 event.setClassName(getClass().getName());
3866 event.setPackageName(getContext().getPackageName());
3867 event.setEnabled(isEnabled());
3868 event.setContentDescription(mContentDescription);
3869
Svetoslav Ganova0156172011-06-26 17:55:44 -07003870 final int eventType = event.getEventType();
3871 switch (eventType) {
3872 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3873 if (mAttachInfo != null) {
3874 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3875 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3876 FOCUSABLES_ALL);
3877 event.setItemCount(focusablesTempList.size());
3878 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3879 focusablesTempList.clear();
3880 }
3881 } break;
3882 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3883 event.setScrollX(mScrollX);
3884 event.setScrollY(mScrollY);
3885 event.setItemCount(getHeight());
3886 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003887 }
3888 }
3889
3890 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003891 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3892 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3893 * This method is responsible for obtaining an accessibility node info from a
3894 * pool of reusable instances and calling
3895 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3896 * initialize the former.
3897 * <p>
3898 * Note: The client is responsible for recycling the obtained instance by calling
3899 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3900 * </p>
3901 * @return A populated {@link AccessibilityNodeInfo}.
3902 *
3903 * @see AccessibilityNodeInfo
3904 */
3905 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3906 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3907 onInitializeAccessibilityNodeInfo(info);
3908 return info;
3909 }
3910
3911 /**
3912 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3913 * The base implementation sets:
3914 * <ul>
3915 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003916 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3917 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003918 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3919 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3920 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3921 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3922 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3923 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3924 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3925 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3926 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3927 * </ul>
3928 * <p>
3929 * Subclasses should override this method, call the super implementation,
3930 * and set additional attributes.
3931 * </p>
3932 * @param info The instance to initialize.
3933 */
3934 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3935 Rect bounds = mAttachInfo.mTmpInvalRect;
3936 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003937 info.setBoundsInParent(bounds);
3938
3939 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3940 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07003941 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003942 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3943 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003944
3945 ViewParent parent = getParent();
3946 if (parent instanceof View) {
3947 View parentView = (View) parent;
3948 info.setParent(parentView);
3949 }
3950
3951 info.setPackageName(mContext.getPackageName());
3952 info.setClassName(getClass().getName());
3953 info.setContentDescription(getContentDescription());
3954
3955 info.setEnabled(isEnabled());
3956 info.setClickable(isClickable());
3957 info.setFocusable(isFocusable());
3958 info.setFocused(isFocused());
3959 info.setSelected(isSelected());
3960 info.setLongClickable(isLongClickable());
3961
3962 // TODO: These make sense only if we are in an AdapterView but all
3963 // views can be selected. Maybe from accessiiblity perspective
3964 // we should report as selectable view in an AdapterView.
3965 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3966 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3967
3968 if (isFocusable()) {
3969 if (isFocused()) {
3970 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3971 } else {
3972 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3973 }
3974 }
3975 }
3976
3977 /**
3978 * Gets the unique identifier of this view on the screen for accessibility purposes.
3979 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3980 *
3981 * @return The view accessibility id.
3982 *
3983 * @hide
3984 */
3985 public int getAccessibilityViewId() {
3986 if (mAccessibilityViewId == NO_ID) {
3987 mAccessibilityViewId = sNextAccessibilityViewId++;
3988 }
3989 return mAccessibilityViewId;
3990 }
3991
3992 /**
3993 * Gets the unique identifier of the window in which this View reseides.
3994 *
3995 * @return The window accessibility id.
3996 *
3997 * @hide
3998 */
3999 public int getAccessibilityWindowId() {
4000 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4001 }
4002
4003 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004004 * Gets the {@link View} description. It briefly describes the view and is
4005 * primarily used for accessibility support. Set this property to enable
4006 * better accessibility support for your application. This is especially
4007 * true for views that do not have textual representation (For example,
4008 * ImageButton).
4009 *
4010 * @return The content descriptiopn.
4011 *
4012 * @attr ref android.R.styleable#View_contentDescription
4013 */
4014 public CharSequence getContentDescription() {
4015 return mContentDescription;
4016 }
4017
4018 /**
4019 * Sets the {@link View} description. It briefly describes the view and is
4020 * primarily used for accessibility support. Set this property to enable
4021 * better accessibility support for your application. This is especially
4022 * true for views that do not have textual representation (For example,
4023 * ImageButton).
4024 *
4025 * @param contentDescription The content description.
4026 *
4027 * @attr ref android.R.styleable#View_contentDescription
4028 */
4029 public void setContentDescription(CharSequence contentDescription) {
4030 mContentDescription = contentDescription;
4031 }
4032
4033 /**
Romain Guya2431d02009-04-30 16:30:00 -07004034 * Invoked whenever this view loses focus, either by losing window focus or by losing
4035 * focus within its window. This method can be used to clear any state tied to the
4036 * focus. For instance, if a button is held pressed with the trackball and the window
4037 * loses focus, this method can be used to cancel the press.
4038 *
4039 * Subclasses of View overriding this method should always call super.onFocusLost().
4040 *
4041 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004042 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004043 *
4044 * @hide pending API council approval
4045 */
4046 protected void onFocusLost() {
4047 resetPressedState();
4048 }
4049
4050 private void resetPressedState() {
4051 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4052 return;
4053 }
4054
4055 if (isPressed()) {
4056 setPressed(false);
4057
4058 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004059 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004060 }
4061 }
4062 }
4063
4064 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004065 * Returns true if this view has focus
4066 *
4067 * @return True if this view has focus, false otherwise.
4068 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004069 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004070 public boolean isFocused() {
4071 return (mPrivateFlags & FOCUSED) != 0;
4072 }
4073
4074 /**
4075 * Find the view in the hierarchy rooted at this view that currently has
4076 * focus.
4077 *
4078 * @return The view that currently has focus, or null if no focused view can
4079 * be found.
4080 */
4081 public View findFocus() {
4082 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4083 }
4084
4085 /**
4086 * Change whether this view is one of the set of scrollable containers in
4087 * its window. This will be used to determine whether the window can
4088 * resize or must pan when a soft input area is open -- scrollable
4089 * containers allow the window to use resize mode since the container
4090 * will appropriately shrink.
4091 */
4092 public void setScrollContainer(boolean isScrollContainer) {
4093 if (isScrollContainer) {
4094 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4095 mAttachInfo.mScrollContainers.add(this);
4096 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4097 }
4098 mPrivateFlags |= SCROLL_CONTAINER;
4099 } else {
4100 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4101 mAttachInfo.mScrollContainers.remove(this);
4102 }
4103 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4104 }
4105 }
4106
4107 /**
4108 * Returns the quality of the drawing cache.
4109 *
4110 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4111 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4112 *
4113 * @see #setDrawingCacheQuality(int)
4114 * @see #setDrawingCacheEnabled(boolean)
4115 * @see #isDrawingCacheEnabled()
4116 *
4117 * @attr ref android.R.styleable#View_drawingCacheQuality
4118 */
4119 public int getDrawingCacheQuality() {
4120 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4121 }
4122
4123 /**
4124 * Set the drawing cache quality of this view. This value is used only when the
4125 * drawing cache is enabled
4126 *
4127 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4128 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4129 *
4130 * @see #getDrawingCacheQuality()
4131 * @see #setDrawingCacheEnabled(boolean)
4132 * @see #isDrawingCacheEnabled()
4133 *
4134 * @attr ref android.R.styleable#View_drawingCacheQuality
4135 */
4136 public void setDrawingCacheQuality(int quality) {
4137 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4138 }
4139
4140 /**
4141 * Returns whether the screen should remain on, corresponding to the current
4142 * value of {@link #KEEP_SCREEN_ON}.
4143 *
4144 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4145 *
4146 * @see #setKeepScreenOn(boolean)
4147 *
4148 * @attr ref android.R.styleable#View_keepScreenOn
4149 */
4150 public boolean getKeepScreenOn() {
4151 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4152 }
4153
4154 /**
4155 * Controls whether the screen should remain on, modifying the
4156 * value of {@link #KEEP_SCREEN_ON}.
4157 *
4158 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4159 *
4160 * @see #getKeepScreenOn()
4161 *
4162 * @attr ref android.R.styleable#View_keepScreenOn
4163 */
4164 public void setKeepScreenOn(boolean keepScreenOn) {
4165 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4166 }
4167
4168 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004169 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4170 * @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 -08004171 *
4172 * @attr ref android.R.styleable#View_nextFocusLeft
4173 */
4174 public int getNextFocusLeftId() {
4175 return mNextFocusLeftId;
4176 }
4177
4178 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004179 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4180 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4181 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004182 *
4183 * @attr ref android.R.styleable#View_nextFocusLeft
4184 */
4185 public void setNextFocusLeftId(int nextFocusLeftId) {
4186 mNextFocusLeftId = nextFocusLeftId;
4187 }
4188
4189 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004190 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4191 * @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 -08004192 *
4193 * @attr ref android.R.styleable#View_nextFocusRight
4194 */
4195 public int getNextFocusRightId() {
4196 return mNextFocusRightId;
4197 }
4198
4199 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004200 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4201 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4202 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004203 *
4204 * @attr ref android.R.styleable#View_nextFocusRight
4205 */
4206 public void setNextFocusRightId(int nextFocusRightId) {
4207 mNextFocusRightId = nextFocusRightId;
4208 }
4209
4210 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004211 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4212 * @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 -08004213 *
4214 * @attr ref android.R.styleable#View_nextFocusUp
4215 */
4216 public int getNextFocusUpId() {
4217 return mNextFocusUpId;
4218 }
4219
4220 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004221 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4222 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4223 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004224 *
4225 * @attr ref android.R.styleable#View_nextFocusUp
4226 */
4227 public void setNextFocusUpId(int nextFocusUpId) {
4228 mNextFocusUpId = nextFocusUpId;
4229 }
4230
4231 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004232 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4233 * @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 -08004234 *
4235 * @attr ref android.R.styleable#View_nextFocusDown
4236 */
4237 public int getNextFocusDownId() {
4238 return mNextFocusDownId;
4239 }
4240
4241 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004242 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4243 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4244 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004245 *
4246 * @attr ref android.R.styleable#View_nextFocusDown
4247 */
4248 public void setNextFocusDownId(int nextFocusDownId) {
4249 mNextFocusDownId = nextFocusDownId;
4250 }
4251
4252 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004253 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4254 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4255 *
4256 * @attr ref android.R.styleable#View_nextFocusForward
4257 */
4258 public int getNextFocusForwardId() {
4259 return mNextFocusForwardId;
4260 }
4261
4262 /**
4263 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4264 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4265 * decide automatically.
4266 *
4267 * @attr ref android.R.styleable#View_nextFocusForward
4268 */
4269 public void setNextFocusForwardId(int nextFocusForwardId) {
4270 mNextFocusForwardId = nextFocusForwardId;
4271 }
4272
4273 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004274 * Returns the visibility of this view and all of its ancestors
4275 *
4276 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4277 */
4278 public boolean isShown() {
4279 View current = this;
4280 //noinspection ConstantConditions
4281 do {
4282 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4283 return false;
4284 }
4285 ViewParent parent = current.mParent;
4286 if (parent == null) {
4287 return false; // We are not attached to the view root
4288 }
4289 if (!(parent instanceof View)) {
4290 return true;
4291 }
4292 current = (View) parent;
4293 } while (current != null);
4294
4295 return false;
4296 }
4297
4298 /**
4299 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4300 * is set
4301 *
4302 * @param insets Insets for system windows
4303 *
4304 * @return True if this view applied the insets, false otherwise
4305 */
4306 protected boolean fitSystemWindows(Rect insets) {
4307 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4308 mPaddingLeft = insets.left;
4309 mPaddingTop = insets.top;
4310 mPaddingRight = insets.right;
4311 mPaddingBottom = insets.bottom;
4312 requestLayout();
4313 return true;
4314 }
4315 return false;
4316 }
4317
4318 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004319 * Set whether or not this view should account for system screen decorations
4320 * such as the status bar and inset its content. This allows this view to be
4321 * positioned in absolute screen coordinates and remain visible to the user.
4322 *
4323 * <p>This should only be used by top-level window decor views.
4324 *
4325 * @param fitSystemWindows true to inset content for system screen decorations, false for
4326 * default behavior.
4327 *
4328 * @attr ref android.R.styleable#View_fitsSystemWindows
4329 */
4330 public void setFitsSystemWindows(boolean fitSystemWindows) {
4331 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4332 }
4333
4334 /**
4335 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4336 * will account for system screen decorations such as the status bar and inset its
4337 * content. This allows the view to be positioned in absolute screen coordinates
4338 * and remain visible to the user.
4339 *
4340 * @return true if this view will adjust its content bounds for system screen decorations.
4341 *
4342 * @attr ref android.R.styleable#View_fitsSystemWindows
4343 */
4344 public boolean fitsSystemWindows() {
4345 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4346 }
4347
4348 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004349 * Returns the visibility status for this view.
4350 *
4351 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4352 * @attr ref android.R.styleable#View_visibility
4353 */
4354 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004355 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4356 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4357 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004358 })
4359 public int getVisibility() {
4360 return mViewFlags & VISIBILITY_MASK;
4361 }
4362
4363 /**
4364 * Set the enabled state of this view.
4365 *
4366 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4367 * @attr ref android.R.styleable#View_visibility
4368 */
4369 @RemotableViewMethod
4370 public void setVisibility(int visibility) {
4371 setFlags(visibility, VISIBILITY_MASK);
4372 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4373 }
4374
4375 /**
4376 * Returns the enabled status for this view. The interpretation of the
4377 * enabled state varies by subclass.
4378 *
4379 * @return True if this view is enabled, false otherwise.
4380 */
4381 @ViewDebug.ExportedProperty
4382 public boolean isEnabled() {
4383 return (mViewFlags & ENABLED_MASK) == ENABLED;
4384 }
4385
4386 /**
4387 * Set the enabled state of this view. The interpretation of the enabled
4388 * state varies by subclass.
4389 *
4390 * @param enabled True if this view is enabled, false otherwise.
4391 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004392 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004393 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004394 if (enabled == isEnabled()) return;
4395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004396 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4397
4398 /*
4399 * The View most likely has to change its appearance, so refresh
4400 * the drawable state.
4401 */
4402 refreshDrawableState();
4403
4404 // Invalidate too, since the default behavior for views is to be
4405 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004406 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004407 }
4408
4409 /**
4410 * Set whether this view can receive the focus.
4411 *
4412 * Setting this to false will also ensure that this view is not focusable
4413 * in touch mode.
4414 *
4415 * @param focusable If true, this view can receive the focus.
4416 *
4417 * @see #setFocusableInTouchMode(boolean)
4418 * @attr ref android.R.styleable#View_focusable
4419 */
4420 public void setFocusable(boolean focusable) {
4421 if (!focusable) {
4422 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4423 }
4424 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4425 }
4426
4427 /**
4428 * Set whether this view can receive focus while in touch mode.
4429 *
4430 * Setting this to true will also ensure that this view is focusable.
4431 *
4432 * @param focusableInTouchMode If true, this view can receive the focus while
4433 * in touch mode.
4434 *
4435 * @see #setFocusable(boolean)
4436 * @attr ref android.R.styleable#View_focusableInTouchMode
4437 */
4438 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4439 // Focusable in touch mode should always be set before the focusable flag
4440 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4441 // which, in touch mode, will not successfully request focus on this view
4442 // because the focusable in touch mode flag is not set
4443 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4444 if (focusableInTouchMode) {
4445 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4446 }
4447 }
4448
4449 /**
4450 * Set whether this view should have sound effects enabled for events such as
4451 * clicking and touching.
4452 *
4453 * <p>You may wish to disable sound effects for a view if you already play sounds,
4454 * for instance, a dial key that plays dtmf tones.
4455 *
4456 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4457 * @see #isSoundEffectsEnabled()
4458 * @see #playSoundEffect(int)
4459 * @attr ref android.R.styleable#View_soundEffectsEnabled
4460 */
4461 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4462 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4463 }
4464
4465 /**
4466 * @return whether this view should have sound effects enabled for events such as
4467 * clicking and touching.
4468 *
4469 * @see #setSoundEffectsEnabled(boolean)
4470 * @see #playSoundEffect(int)
4471 * @attr ref android.R.styleable#View_soundEffectsEnabled
4472 */
4473 @ViewDebug.ExportedProperty
4474 public boolean isSoundEffectsEnabled() {
4475 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4476 }
4477
4478 /**
4479 * Set whether this view should have haptic feedback for events such as
4480 * long presses.
4481 *
4482 * <p>You may wish to disable haptic feedback if your view already controls
4483 * its own haptic feedback.
4484 *
4485 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4486 * @see #isHapticFeedbackEnabled()
4487 * @see #performHapticFeedback(int)
4488 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4489 */
4490 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4491 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4492 }
4493
4494 /**
4495 * @return whether this view should have haptic feedback enabled for events
4496 * long presses.
4497 *
4498 * @see #setHapticFeedbackEnabled(boolean)
4499 * @see #performHapticFeedback(int)
4500 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4501 */
4502 @ViewDebug.ExportedProperty
4503 public boolean isHapticFeedbackEnabled() {
4504 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4505 }
4506
4507 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004508 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004509 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004510 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4511 * {@link #LAYOUT_DIRECTION_RTL},
4512 * {@link #LAYOUT_DIRECTION_INHERIT} or
4513 * {@link #LAYOUT_DIRECTION_LOCALE}.
4514 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004515 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004516 * @hide
4517 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004518 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004519 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4520 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4521 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4522 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004523 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004524 public int getLayoutDirection() {
4525 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004526 }
4527
4528 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004529 * Set the layout direction for this view. This will propagate a reset of layout direction
4530 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004531 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004532 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4533 * {@link #LAYOUT_DIRECTION_RTL},
4534 * {@link #LAYOUT_DIRECTION_INHERIT} or
4535 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004536 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004537 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004538 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004539 * @hide
4540 */
4541 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004542 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004543 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004544 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004545 // Setting the flag will also request a layout.
4546 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4547 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004548 }
4549
4550 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004551 * Returns the resolved layout direction for this view.
4552 *
4553 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4554 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4555 *
4556 * @hide
4557 */
4558 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4559 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4560 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4561 })
4562 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004563 resolveLayoutDirectionIfNeeded();
4564 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004565 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4566 }
4567
4568 /**
4569 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4570 * layout attribute and/or the inherited value from the parent.</p>
4571 *
4572 * @return true if the layout is right-to-left.
4573 *
4574 * @hide
4575 */
4576 @ViewDebug.ExportedProperty(category = "layout")
4577 public boolean isLayoutRtl() {
4578 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4579 }
4580
4581 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004582 * If this view doesn't do any drawing on its own, set this flag to
4583 * allow further optimizations. By default, this flag is not set on
4584 * View, but could be set on some View subclasses such as ViewGroup.
4585 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004586 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4587 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004588 *
4589 * @param willNotDraw whether or not this View draw on its own
4590 */
4591 public void setWillNotDraw(boolean willNotDraw) {
4592 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4593 }
4594
4595 /**
4596 * Returns whether or not this View draws on its own.
4597 *
4598 * @return true if this view has nothing to draw, false otherwise
4599 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004600 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004601 public boolean willNotDraw() {
4602 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4603 }
4604
4605 /**
4606 * When a View's drawing cache is enabled, drawing is redirected to an
4607 * offscreen bitmap. Some views, like an ImageView, must be able to
4608 * bypass this mechanism if they already draw a single bitmap, to avoid
4609 * unnecessary usage of the memory.
4610 *
4611 * @param willNotCacheDrawing true if this view does not cache its
4612 * drawing, false otherwise
4613 */
4614 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4615 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4616 }
4617
4618 /**
4619 * Returns whether or not this View can cache its drawing or not.
4620 *
4621 * @return true if this view does not cache its drawing, false otherwise
4622 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004623 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004624 public boolean willNotCacheDrawing() {
4625 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4626 }
4627
4628 /**
4629 * Indicates whether this view reacts to click events or not.
4630 *
4631 * @return true if the view is clickable, false otherwise
4632 *
4633 * @see #setClickable(boolean)
4634 * @attr ref android.R.styleable#View_clickable
4635 */
4636 @ViewDebug.ExportedProperty
4637 public boolean isClickable() {
4638 return (mViewFlags & CLICKABLE) == CLICKABLE;
4639 }
4640
4641 /**
4642 * Enables or disables click events for this view. When a view
4643 * is clickable it will change its state to "pressed" on every click.
4644 * Subclasses should set the view clickable to visually react to
4645 * user's clicks.
4646 *
4647 * @param clickable true to make the view clickable, false otherwise
4648 *
4649 * @see #isClickable()
4650 * @attr ref android.R.styleable#View_clickable
4651 */
4652 public void setClickable(boolean clickable) {
4653 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4654 }
4655
4656 /**
4657 * Indicates whether this view reacts to long click events or not.
4658 *
4659 * @return true if the view is long clickable, false otherwise
4660 *
4661 * @see #setLongClickable(boolean)
4662 * @attr ref android.R.styleable#View_longClickable
4663 */
4664 public boolean isLongClickable() {
4665 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4666 }
4667
4668 /**
4669 * Enables or disables long click events for this view. When a view is long
4670 * clickable it reacts to the user holding down the button for a longer
4671 * duration than a tap. This event can either launch the listener or a
4672 * context menu.
4673 *
4674 * @param longClickable true to make the view long clickable, false otherwise
4675 * @see #isLongClickable()
4676 * @attr ref android.R.styleable#View_longClickable
4677 */
4678 public void setLongClickable(boolean longClickable) {
4679 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4680 }
4681
4682 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004683 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004684 *
4685 * @see #isClickable()
4686 * @see #setClickable(boolean)
4687 *
4688 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4689 * the View's internal state from a previously set "pressed" state.
4690 */
4691 public void setPressed(boolean pressed) {
4692 if (pressed) {
4693 mPrivateFlags |= PRESSED;
4694 } else {
4695 mPrivateFlags &= ~PRESSED;
4696 }
4697 refreshDrawableState();
4698 dispatchSetPressed(pressed);
4699 }
4700
4701 /**
4702 * Dispatch setPressed to all of this View's children.
4703 *
4704 * @see #setPressed(boolean)
4705 *
4706 * @param pressed The new pressed state
4707 */
4708 protected void dispatchSetPressed(boolean pressed) {
4709 }
4710
4711 /**
4712 * Indicates whether the view is currently in pressed state. Unless
4713 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4714 * the pressed state.
4715 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004716 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004717 * @see #isClickable()
4718 * @see #setClickable(boolean)
4719 *
4720 * @return true if the view is currently pressed, false otherwise
4721 */
4722 public boolean isPressed() {
4723 return (mPrivateFlags & PRESSED) == PRESSED;
4724 }
4725
4726 /**
4727 * Indicates whether this view will save its state (that is,
4728 * whether its {@link #onSaveInstanceState} method will be called).
4729 *
4730 * @return Returns true if the view state saving is enabled, else false.
4731 *
4732 * @see #setSaveEnabled(boolean)
4733 * @attr ref android.R.styleable#View_saveEnabled
4734 */
4735 public boolean isSaveEnabled() {
4736 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4737 }
4738
4739 /**
4740 * Controls whether the saving of this view's state is
4741 * enabled (that is, whether its {@link #onSaveInstanceState} method
4742 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004743 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004744 * for its state to be saved. This flag can only disable the
4745 * saving of this view; any child views may still have their state saved.
4746 *
4747 * @param enabled Set to false to <em>disable</em> state saving, or true
4748 * (the default) to allow it.
4749 *
4750 * @see #isSaveEnabled()
4751 * @see #setId(int)
4752 * @see #onSaveInstanceState()
4753 * @attr ref android.R.styleable#View_saveEnabled
4754 */
4755 public void setSaveEnabled(boolean enabled) {
4756 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4757 }
4758
Jeff Brown85a31762010-09-01 17:01:00 -07004759 /**
4760 * Gets whether the framework should discard touches when the view's
4761 * window is obscured by another visible window.
4762 * Refer to the {@link View} security documentation for more details.
4763 *
4764 * @return True if touch filtering is enabled.
4765 *
4766 * @see #setFilterTouchesWhenObscured(boolean)
4767 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4768 */
4769 @ViewDebug.ExportedProperty
4770 public boolean getFilterTouchesWhenObscured() {
4771 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4772 }
4773
4774 /**
4775 * Sets whether the framework should discard touches when the view's
4776 * window is obscured by another visible window.
4777 * Refer to the {@link View} security documentation for more details.
4778 *
4779 * @param enabled True if touch filtering should be enabled.
4780 *
4781 * @see #getFilterTouchesWhenObscured
4782 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4783 */
4784 public void setFilterTouchesWhenObscured(boolean enabled) {
4785 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4786 FILTER_TOUCHES_WHEN_OBSCURED);
4787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004788
4789 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004790 * Indicates whether the entire hierarchy under this view will save its
4791 * state when a state saving traversal occurs from its parent. The default
4792 * is true; if false, these views will not be saved unless
4793 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4794 *
4795 * @return Returns true if the view state saving from parent is enabled, else false.
4796 *
4797 * @see #setSaveFromParentEnabled(boolean)
4798 */
4799 public boolean isSaveFromParentEnabled() {
4800 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4801 }
4802
4803 /**
4804 * Controls whether the entire hierarchy under this view will save its
4805 * state when a state saving traversal occurs from its parent. The default
4806 * is true; if false, these views will not be saved unless
4807 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4808 *
4809 * @param enabled Set to false to <em>disable</em> state saving, or true
4810 * (the default) to allow it.
4811 *
4812 * @see #isSaveFromParentEnabled()
4813 * @see #setId(int)
4814 * @see #onSaveInstanceState()
4815 */
4816 public void setSaveFromParentEnabled(boolean enabled) {
4817 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4818 }
4819
4820
4821 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004822 * Returns whether this View is able to take focus.
4823 *
4824 * @return True if this view can take focus, or false otherwise.
4825 * @attr ref android.R.styleable#View_focusable
4826 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004827 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004828 public final boolean isFocusable() {
4829 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4830 }
4831
4832 /**
4833 * When a view is focusable, it may not want to take focus when in touch mode.
4834 * For example, a button would like focus when the user is navigating via a D-pad
4835 * so that the user can click on it, but once the user starts touching the screen,
4836 * the button shouldn't take focus
4837 * @return Whether the view is focusable in touch mode.
4838 * @attr ref android.R.styleable#View_focusableInTouchMode
4839 */
4840 @ViewDebug.ExportedProperty
4841 public final boolean isFocusableInTouchMode() {
4842 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4843 }
4844
4845 /**
4846 * Find the nearest view in the specified direction that can take focus.
4847 * This does not actually give focus to that view.
4848 *
4849 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4850 *
4851 * @return The nearest focusable in the specified direction, or null if none
4852 * can be found.
4853 */
4854 public View focusSearch(int direction) {
4855 if (mParent != null) {
4856 return mParent.focusSearch(this, direction);
4857 } else {
4858 return null;
4859 }
4860 }
4861
4862 /**
4863 * This method is the last chance for the focused view and its ancestors to
4864 * respond to an arrow key. This is called when the focused view did not
4865 * consume the key internally, nor could the view system find a new view in
4866 * the requested direction to give focus to.
4867 *
4868 * @param focused The currently focused view.
4869 * @param direction The direction focus wants to move. One of FOCUS_UP,
4870 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4871 * @return True if the this view consumed this unhandled move.
4872 */
4873 public boolean dispatchUnhandledMove(View focused, int direction) {
4874 return false;
4875 }
4876
4877 /**
4878 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004879 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004880 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004881 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4882 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004883 * @return The user specified next view, or null if there is none.
4884 */
4885 View findUserSetNextFocus(View root, int direction) {
4886 switch (direction) {
4887 case FOCUS_LEFT:
4888 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004889 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004890 case FOCUS_RIGHT:
4891 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004892 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004893 case FOCUS_UP:
4894 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004895 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004896 case FOCUS_DOWN:
4897 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004898 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004899 case FOCUS_FORWARD:
4900 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004901 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004902 case FOCUS_BACKWARD: {
4903 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004904 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08004905 @Override
4906 public boolean apply(View t) {
4907 return t.mNextFocusForwardId == id;
4908 }
4909 });
4910 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004911 }
4912 return null;
4913 }
4914
Jeff Brown4dfbec22011-08-15 14:55:37 -07004915 private View findViewInsideOutShouldExist(View root, final int childViewId) {
4916 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
4917 @Override
4918 public boolean apply(View t) {
4919 return t.mID == childViewId;
4920 }
4921 });
4922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 if (result == null) {
4924 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4925 + "by user for id " + childViewId);
4926 }
4927 return result;
4928 }
4929
4930 /**
4931 * Find and return all focusable views that are descendants of this view,
4932 * possibly including this view if it is focusable itself.
4933 *
4934 * @param direction The direction of the focus
4935 * @return A list of focusable views
4936 */
4937 public ArrayList<View> getFocusables(int direction) {
4938 ArrayList<View> result = new ArrayList<View>(24);
4939 addFocusables(result, direction);
4940 return result;
4941 }
4942
4943 /**
4944 * Add any focusable views that are descendants of this view (possibly
4945 * including this view if it is focusable itself) to views. If we are in touch mode,
4946 * only add views that are also focusable in touch mode.
4947 *
4948 * @param views Focusable views found so far
4949 * @param direction The direction of the focus
4950 */
4951 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004952 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004954
svetoslavganov75986cf2009-05-14 22:28:01 -07004955 /**
4956 * Adds any focusable views that are descendants of this view (possibly
4957 * including this view if it is focusable itself) to views. This method
4958 * adds all focusable views regardless if we are in touch mode or
4959 * only views focusable in touch mode if we are in touch mode depending on
4960 * the focusable mode paramater.
4961 *
4962 * @param views Focusable views found so far or null if all we are interested is
4963 * the number of focusables.
4964 * @param direction The direction of the focus.
4965 * @param focusableMode The type of focusables to be added.
4966 *
4967 * @see #FOCUSABLES_ALL
4968 * @see #FOCUSABLES_TOUCH_MODE
4969 */
4970 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4971 if (!isFocusable()) {
4972 return;
4973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004974
svetoslavganov75986cf2009-05-14 22:28:01 -07004975 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4976 isInTouchMode() && !isFocusableInTouchMode()) {
4977 return;
4978 }
4979
4980 if (views != null) {
4981 views.add(this);
4982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004983 }
4984
4985 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004986 * Finds the Views that contain given text. The containment is case insensitive.
4987 * As View's text is considered any text content that View renders.
4988 *
4989 * @param outViews The output list of matching Views.
4990 * @param text The text to match against.
4991 */
4992 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
4993 }
4994
4995 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004996 * Find and return all touchable views that are descendants of this view,
4997 * possibly including this view if it is touchable itself.
4998 *
4999 * @return A list of touchable views
5000 */
5001 public ArrayList<View> getTouchables() {
5002 ArrayList<View> result = new ArrayList<View>();
5003 addTouchables(result);
5004 return result;
5005 }
5006
5007 /**
5008 * Add any touchable views that are descendants of this view (possibly
5009 * including this view if it is touchable itself) to views.
5010 *
5011 * @param views Touchable views found so far
5012 */
5013 public void addTouchables(ArrayList<View> views) {
5014 final int viewFlags = mViewFlags;
5015
5016 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5017 && (viewFlags & ENABLED_MASK) == ENABLED) {
5018 views.add(this);
5019 }
5020 }
5021
5022 /**
5023 * Call this to try to give focus to a specific view or to one of its
5024 * descendants.
5025 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005026 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5027 * false), or if it is focusable and it is not focusable in touch mode
5028 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005029 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005030 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005031 * have focus, and you want your parent to look for the next one.
5032 *
5033 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5034 * {@link #FOCUS_DOWN} and <code>null</code>.
5035 *
5036 * @return Whether this view or one of its descendants actually took focus.
5037 */
5038 public final boolean requestFocus() {
5039 return requestFocus(View.FOCUS_DOWN);
5040 }
5041
5042
5043 /**
5044 * Call this to try to give focus to a specific view or to one of its
5045 * descendants and give it a hint about what direction focus is heading.
5046 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005047 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5048 * false), or if it is focusable and it is not focusable in touch mode
5049 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005050 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005051 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005052 * have focus, and you want your parent to look for the next one.
5053 *
5054 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5055 * <code>null</code> set for the previously focused rectangle.
5056 *
5057 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5058 * @return Whether this view or one of its descendants actually took focus.
5059 */
5060 public final boolean requestFocus(int direction) {
5061 return requestFocus(direction, null);
5062 }
5063
5064 /**
5065 * Call this to try to give focus to a specific view or to one of its descendants
5066 * and give it hints about the direction and a specific rectangle that the focus
5067 * is coming from. The rectangle can help give larger views a finer grained hint
5068 * about where focus is coming from, and therefore, where to show selection, or
5069 * forward focus change internally.
5070 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005071 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5072 * false), or if it is focusable and it is not focusable in touch mode
5073 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005074 *
5075 * A View will not take focus if it is not visible.
5076 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005077 * A View will not take focus if one of its parents has
5078 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5079 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005080 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005081 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005082 * have focus, and you want your parent to look for the next one.
5083 *
5084 * You may wish to override this method if your custom {@link View} has an internal
5085 * {@link View} that it wishes to forward the request to.
5086 *
5087 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5088 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5089 * to give a finer grained hint about where focus is coming from. May be null
5090 * if there is no hint.
5091 * @return Whether this view or one of its descendants actually took focus.
5092 */
5093 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5094 // need to be focusable
5095 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5096 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5097 return false;
5098 }
5099
5100 // need to be focusable in touch mode if in touch mode
5101 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005102 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5103 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005104 }
5105
5106 // need to not have any parents blocking us
5107 if (hasAncestorThatBlocksDescendantFocus()) {
5108 return false;
5109 }
5110
5111 handleFocusGainInternal(direction, previouslyFocusedRect);
5112 return true;
5113 }
5114
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005115 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005116 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005117 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005118 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005119 }
5120
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005121 /**
5122 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5123 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5124 * touch mode to request focus when they are touched.
5125 *
5126 * @return Whether this view or one of its descendants actually took focus.
5127 *
5128 * @see #isInTouchMode()
5129 *
5130 */
5131 public final boolean requestFocusFromTouch() {
5132 // Leave touch mode if we need to
5133 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005134 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005135 if (viewRoot != null) {
5136 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005137 }
5138 }
5139 return requestFocus(View.FOCUS_DOWN);
5140 }
5141
5142 /**
5143 * @return Whether any ancestor of this view blocks descendant focus.
5144 */
5145 private boolean hasAncestorThatBlocksDescendantFocus() {
5146 ViewParent ancestor = mParent;
5147 while (ancestor instanceof ViewGroup) {
5148 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5149 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5150 return true;
5151 } else {
5152 ancestor = vgAncestor.getParent();
5153 }
5154 }
5155 return false;
5156 }
5157
5158 /**
Romain Guya440b002010-02-24 15:57:54 -08005159 * @hide
5160 */
5161 public void dispatchStartTemporaryDetach() {
5162 onStartTemporaryDetach();
5163 }
5164
5165 /**
5166 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005167 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5168 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005169 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005170 */
5171 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005172 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005173 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005174 }
5175
5176 /**
5177 * @hide
5178 */
5179 public void dispatchFinishTemporaryDetach() {
5180 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005181 }
Romain Guy8506ab42009-06-11 17:35:47 -07005182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005183 /**
5184 * Called after {@link #onStartTemporaryDetach} when the container is done
5185 * changing the view.
5186 */
5187 public void onFinishTemporaryDetach() {
5188 }
Romain Guy8506ab42009-06-11 17:35:47 -07005189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005190 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005191 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5192 * for this view's window. Returns null if the view is not currently attached
5193 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005194 * just use the standard high-level event callbacks like
5195 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005196 */
5197 public KeyEvent.DispatcherState getKeyDispatcherState() {
5198 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5199 }
Joe Malin32736f02011-01-19 16:14:20 -08005200
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 * Dispatch a key event before it is processed by any input method
5203 * associated with the view hierarchy. This can be used to intercept
5204 * key events in special situations before the IME consumes them; a
5205 * typical example would be handling the BACK key to update the application's
5206 * UI instead of allowing the IME to see it and close itself.
5207 *
5208 * @param event The key event to be dispatched.
5209 * @return True if the event was handled, false otherwise.
5210 */
5211 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5212 return onKeyPreIme(event.getKeyCode(), event);
5213 }
5214
5215 /**
5216 * Dispatch a key event to the next view on the focus path. This path runs
5217 * from the top of the view tree down to the currently focused view. If this
5218 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5219 * the next node down the focus path. This method also fires any key
5220 * listeners.
5221 *
5222 * @param event The key event to be dispatched.
5223 * @return True if the event was handled, false otherwise.
5224 */
5225 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005226 if (mInputEventConsistencyVerifier != null) {
5227 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5228 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005229
Jeff Brown21bc5c92011-02-28 18:27:14 -08005230 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005231 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005232 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5233 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5234 return true;
5235 }
5236
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005237 if (event.dispatch(this, mAttachInfo != null
5238 ? mAttachInfo.mKeyDispatchState : null, this)) {
5239 return true;
5240 }
5241
5242 if (mInputEventConsistencyVerifier != null) {
5243 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5244 }
5245 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005246 }
5247
5248 /**
5249 * Dispatches a key shortcut event.
5250 *
5251 * @param event The key event to be dispatched.
5252 * @return True if the event was handled by the view, false otherwise.
5253 */
5254 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5255 return onKeyShortcut(event.getKeyCode(), event);
5256 }
5257
5258 /**
5259 * Pass the touch screen motion event down to the target view, or this
5260 * view if it is the target.
5261 *
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 dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005266 if (mInputEventConsistencyVerifier != null) {
5267 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5268 }
5269
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005270 if (onFilterTouchEventForSecurity(event)) {
5271 //noinspection SimplifiableIfStatement
5272 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5273 mOnTouchListener.onTouch(this, event)) {
5274 return true;
5275 }
5276
5277 if (onTouchEvent(event)) {
5278 return true;
5279 }
Jeff Brown85a31762010-09-01 17:01:00 -07005280 }
5281
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005282 if (mInputEventConsistencyVerifier != null) {
5283 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005284 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005285 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005286 }
5287
5288 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005289 * Filter the touch event to apply security policies.
5290 *
5291 * @param event The motion event to be filtered.
5292 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005293 *
Jeff Brown85a31762010-09-01 17:01:00 -07005294 * @see #getFilterTouchesWhenObscured
5295 */
5296 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005297 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005298 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5299 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5300 // Window is obscured, drop this touch.
5301 return false;
5302 }
5303 return true;
5304 }
5305
5306 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005307 * Pass a trackball motion event down to the focused view.
5308 *
5309 * @param event The motion event to be dispatched.
5310 * @return True if the event was handled by the view, false otherwise.
5311 */
5312 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005313 if (mInputEventConsistencyVerifier != null) {
5314 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5315 }
5316
Romain Guy02ccac62011-06-24 13:20:23 -07005317 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005318 }
5319
5320 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005321 * Dispatch a generic motion event.
5322 * <p>
5323 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5324 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005325 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005326 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005327 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005328 *
5329 * @param event The motion event to be dispatched.
5330 * @return True if the event was handled by the view, false otherwise.
5331 */
5332 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005333 if (mInputEventConsistencyVerifier != null) {
5334 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5335 }
5336
Jeff Browna032cc02011-03-07 16:56:21 -08005337 final int source = event.getSource();
5338 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5339 final int action = event.getAction();
5340 if (action == MotionEvent.ACTION_HOVER_ENTER
5341 || action == MotionEvent.ACTION_HOVER_MOVE
5342 || action == MotionEvent.ACTION_HOVER_EXIT) {
5343 if (dispatchHoverEvent(event)) {
5344 return true;
5345 }
5346 } else if (dispatchGenericPointerEvent(event)) {
5347 return true;
5348 }
5349 } else if (dispatchGenericFocusedEvent(event)) {
5350 return true;
5351 }
5352
Jeff Brown10b62902011-06-20 16:40:37 -07005353 if (dispatchGenericMotionEventInternal(event)) {
5354 return true;
5355 }
5356
5357 if (mInputEventConsistencyVerifier != null) {
5358 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5359 }
5360 return false;
5361 }
5362
5363 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005364 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005365 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5366 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5367 return true;
5368 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005369
5370 if (onGenericMotionEvent(event)) {
5371 return true;
5372 }
5373
5374 if (mInputEventConsistencyVerifier != null) {
5375 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5376 }
5377 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005378 }
5379
5380 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005381 * Dispatch a hover event.
5382 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005383 * Do not call this method directly.
5384 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005385 * </p>
5386 *
5387 * @param event The motion event to be dispatched.
5388 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005389 */
5390 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005391 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005392 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5393 && mOnHoverListener.onHover(this, event)) {
5394 return true;
5395 }
5396
Jeff Browna032cc02011-03-07 16:56:21 -08005397 return onHoverEvent(event);
5398 }
5399
5400 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005401 * Returns true if the view has a child to which it has recently sent
5402 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5403 * it does not have a hovered child, then it must be the innermost hovered view.
5404 * @hide
5405 */
5406 protected boolean hasHoveredChild() {
5407 return false;
5408 }
5409
5410 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005411 * Dispatch a generic motion event to the view under the first pointer.
5412 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005413 * Do not call this method directly.
5414 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005415 * </p>
5416 *
5417 * @param event The motion event to be dispatched.
5418 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005419 */
5420 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5421 return false;
5422 }
5423
5424 /**
5425 * Dispatch a generic motion event to the currently focused view.
5426 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005427 * Do not call this method directly.
5428 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005429 * </p>
5430 *
5431 * @param event The motion event to be dispatched.
5432 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005433 */
5434 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5435 return false;
5436 }
5437
5438 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005439 * Dispatch a pointer event.
5440 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005441 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5442 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5443 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005444 * and should not be expected to handle other pointing device features.
5445 * </p>
5446 *
5447 * @param event The motion event to be dispatched.
5448 * @return True if the event was handled by the view, false otherwise.
5449 * @hide
5450 */
5451 public final boolean dispatchPointerEvent(MotionEvent event) {
5452 if (event.isTouchEvent()) {
5453 return dispatchTouchEvent(event);
5454 } else {
5455 return dispatchGenericMotionEvent(event);
5456 }
5457 }
5458
5459 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005460 * Called when the window containing this view gains or loses window focus.
5461 * ViewGroups should override to route to their children.
5462 *
5463 * @param hasFocus True if the window containing this view now has focus,
5464 * false otherwise.
5465 */
5466 public void dispatchWindowFocusChanged(boolean hasFocus) {
5467 onWindowFocusChanged(hasFocus);
5468 }
5469
5470 /**
5471 * Called when the window containing this view gains or loses focus. Note
5472 * that this is separate from view focus: to receive key events, both
5473 * your view and its window must have focus. If a window is displayed
5474 * on top of yours that takes input focus, then your own window will lose
5475 * focus but the view focus will remain unchanged.
5476 *
5477 * @param hasWindowFocus True if the window containing this view now has
5478 * focus, false otherwise.
5479 */
5480 public void onWindowFocusChanged(boolean hasWindowFocus) {
5481 InputMethodManager imm = InputMethodManager.peekInstance();
5482 if (!hasWindowFocus) {
5483 if (isPressed()) {
5484 setPressed(false);
5485 }
5486 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5487 imm.focusOut(this);
5488 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005489 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005490 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005491 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005492 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5493 imm.focusIn(this);
5494 }
5495 refreshDrawableState();
5496 }
5497
5498 /**
5499 * Returns true if this view is in a window that currently has window focus.
5500 * Note that this is not the same as the view itself having focus.
5501 *
5502 * @return True if this view is in a window that currently has window focus.
5503 */
5504 public boolean hasWindowFocus() {
5505 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5506 }
5507
5508 /**
Adam Powell326d8082009-12-09 15:10:07 -08005509 * Dispatch a view visibility change down the view hierarchy.
5510 * ViewGroups should override to route to their children.
5511 * @param changedView The view whose visibility changed. Could be 'this' or
5512 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005513 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5514 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005515 */
5516 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5517 onVisibilityChanged(changedView, visibility);
5518 }
5519
5520 /**
5521 * Called when the visibility of the view or an ancestor of the view is changed.
5522 * @param changedView The view whose visibility changed. Could be 'this' or
5523 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005524 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5525 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005526 */
5527 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005528 if (visibility == VISIBLE) {
5529 if (mAttachInfo != null) {
5530 initialAwakenScrollBars();
5531 } else {
5532 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5533 }
5534 }
Adam Powell326d8082009-12-09 15:10:07 -08005535 }
5536
5537 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005538 * Dispatch a hint about whether this view is displayed. For instance, when
5539 * a View moves out of the screen, it might receives a display hint indicating
5540 * the view is not displayed. Applications should not <em>rely</em> on this hint
5541 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005542 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005543 * @param hint A hint about whether or not this view is displayed:
5544 * {@link #VISIBLE} or {@link #INVISIBLE}.
5545 */
5546 public void dispatchDisplayHint(int hint) {
5547 onDisplayHint(hint);
5548 }
5549
5550 /**
5551 * Gives this view a hint about whether is displayed or not. For instance, when
5552 * a View moves out of the screen, it might receives a display hint indicating
5553 * the view is not displayed. Applications should not <em>rely</em> on this hint
5554 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005555 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005556 * @param hint A hint about whether or not this view is displayed:
5557 * {@link #VISIBLE} or {@link #INVISIBLE}.
5558 */
5559 protected void onDisplayHint(int hint) {
5560 }
5561
5562 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005563 * Dispatch a window visibility change down the view hierarchy.
5564 * ViewGroups should override to route to their children.
5565 *
5566 * @param visibility The new visibility of the window.
5567 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005568 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005569 */
5570 public void dispatchWindowVisibilityChanged(int visibility) {
5571 onWindowVisibilityChanged(visibility);
5572 }
5573
5574 /**
5575 * Called when the window containing has change its visibility
5576 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5577 * that this tells you whether or not your window is being made visible
5578 * to the window manager; this does <em>not</em> tell you whether or not
5579 * your window is obscured by other windows on the screen, even if it
5580 * is itself visible.
5581 *
5582 * @param visibility The new visibility of the window.
5583 */
5584 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005585 if (visibility == VISIBLE) {
5586 initialAwakenScrollBars();
5587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005588 }
5589
5590 /**
5591 * Returns the current visibility of the window this view is attached to
5592 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5593 *
5594 * @return Returns the current visibility of the view's window.
5595 */
5596 public int getWindowVisibility() {
5597 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5598 }
5599
5600 /**
5601 * Retrieve the overall visible display size in which the window this view is
5602 * attached to has been positioned in. This takes into account screen
5603 * decorations above the window, for both cases where the window itself
5604 * is being position inside of them or the window is being placed under
5605 * then and covered insets are used for the window to position its content
5606 * inside. In effect, this tells you the available area where content can
5607 * be placed and remain visible to users.
5608 *
5609 * <p>This function requires an IPC back to the window manager to retrieve
5610 * the requested information, so should not be used in performance critical
5611 * code like drawing.
5612 *
5613 * @param outRect Filled in with the visible display frame. If the view
5614 * is not attached to a window, this is simply the raw display size.
5615 */
5616 public void getWindowVisibleDisplayFrame(Rect outRect) {
5617 if (mAttachInfo != null) {
5618 try {
5619 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5620 } catch (RemoteException e) {
5621 return;
5622 }
5623 // XXX This is really broken, and probably all needs to be done
5624 // in the window manager, and we need to know more about whether
5625 // we want the area behind or in front of the IME.
5626 final Rect insets = mAttachInfo.mVisibleInsets;
5627 outRect.left += insets.left;
5628 outRect.top += insets.top;
5629 outRect.right -= insets.right;
5630 outRect.bottom -= insets.bottom;
5631 return;
5632 }
5633 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005634 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005635 }
5636
5637 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005638 * Dispatch a notification about a resource configuration change down
5639 * the view hierarchy.
5640 * ViewGroups should override to route to their children.
5641 *
5642 * @param newConfig The new resource configuration.
5643 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005644 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005645 */
5646 public void dispatchConfigurationChanged(Configuration newConfig) {
5647 onConfigurationChanged(newConfig);
5648 }
5649
5650 /**
5651 * Called when the current configuration of the resources being used
5652 * by the application have changed. You can use this to decide when
5653 * to reload resources that can changed based on orientation and other
5654 * configuration characterstics. You only need to use this if you are
5655 * not relying on the normal {@link android.app.Activity} mechanism of
5656 * recreating the activity instance upon a configuration change.
5657 *
5658 * @param newConfig The new resource configuration.
5659 */
5660 protected void onConfigurationChanged(Configuration newConfig) {
5661 }
5662
5663 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005664 * Private function to aggregate all per-view attributes in to the view
5665 * root.
5666 */
5667 void dispatchCollectViewAttributes(int visibility) {
5668 performCollectViewAttributes(visibility);
5669 }
5670
5671 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005672 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005673 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5674 mAttachInfo.mKeepScreenOn = true;
5675 }
5676 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5677 if (mOnSystemUiVisibilityChangeListener != null) {
5678 mAttachInfo.mHasSystemUiListeners = true;
5679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005680 }
5681 }
5682
5683 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005684 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005685 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005686 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5687 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005688 ai.mRecomputeGlobalAttributes = true;
5689 }
5690 }
5691 }
5692
5693 /**
5694 * Returns whether the device is currently in touch mode. Touch mode is entered
5695 * once the user begins interacting with the device by touch, and affects various
5696 * things like whether focus is always visible to the user.
5697 *
5698 * @return Whether the device is in touch mode.
5699 */
5700 @ViewDebug.ExportedProperty
5701 public boolean isInTouchMode() {
5702 if (mAttachInfo != null) {
5703 return mAttachInfo.mInTouchMode;
5704 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005705 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005706 }
5707 }
5708
5709 /**
5710 * Returns the context the view is running in, through which it can
5711 * access the current theme, resources, etc.
5712 *
5713 * @return The view's Context.
5714 */
5715 @ViewDebug.CapturedViewProperty
5716 public final Context getContext() {
5717 return mContext;
5718 }
5719
5720 /**
5721 * Handle a key event before it is processed by any input method
5722 * associated with the view hierarchy. This can be used to intercept
5723 * key events in special situations before the IME consumes them; a
5724 * typical example would be handling the BACK key to update the application's
5725 * UI instead of allowing the IME to see it and close itself.
5726 *
5727 * @param keyCode The value in event.getKeyCode().
5728 * @param event Description of the key event.
5729 * @return If you handled the event, return true. If you want to allow the
5730 * event to be handled by the next receiver, return false.
5731 */
5732 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5733 return false;
5734 }
5735
5736 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005737 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5738 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005739 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5740 * is released, if the view is enabled and clickable.
5741 *
5742 * @param keyCode A key code that represents the button pressed, from
5743 * {@link android.view.KeyEvent}.
5744 * @param event The KeyEvent object that defines the button action.
5745 */
5746 public boolean onKeyDown(int keyCode, KeyEvent event) {
5747 boolean result = false;
5748
5749 switch (keyCode) {
5750 case KeyEvent.KEYCODE_DPAD_CENTER:
5751 case KeyEvent.KEYCODE_ENTER: {
5752 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5753 return true;
5754 }
5755 // Long clickable items don't necessarily have to be clickable
5756 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5757 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5758 (event.getRepeatCount() == 0)) {
5759 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005760 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005761 return true;
5762 }
5763 break;
5764 }
5765 }
5766 return result;
5767 }
5768
5769 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005770 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5771 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5772 * the event).
5773 */
5774 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5775 return false;
5776 }
5777
5778 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005779 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5780 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005781 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5782 * {@link KeyEvent#KEYCODE_ENTER} is released.
5783 *
5784 * @param keyCode A key code that represents the button pressed, from
5785 * {@link android.view.KeyEvent}.
5786 * @param event The KeyEvent object that defines the button action.
5787 */
5788 public boolean onKeyUp(int keyCode, KeyEvent event) {
5789 boolean result = false;
5790
5791 switch (keyCode) {
5792 case KeyEvent.KEYCODE_DPAD_CENTER:
5793 case KeyEvent.KEYCODE_ENTER: {
5794 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5795 return true;
5796 }
5797 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5798 setPressed(false);
5799
5800 if (!mHasPerformedLongPress) {
5801 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005802 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005803
5804 result = performClick();
5805 }
5806 }
5807 break;
5808 }
5809 }
5810 return result;
5811 }
5812
5813 /**
5814 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5815 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5816 * the event).
5817 *
5818 * @param keyCode A key code that represents the button pressed, from
5819 * {@link android.view.KeyEvent}.
5820 * @param repeatCount The number of times the action was made.
5821 * @param event The KeyEvent object that defines the button action.
5822 */
5823 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5824 return false;
5825 }
5826
5827 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005828 * Called on the focused view when a key shortcut event is not handled.
5829 * Override this method to implement local key shortcuts for the View.
5830 * Key shortcuts can also be implemented by setting the
5831 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005832 *
5833 * @param keyCode The value in event.getKeyCode().
5834 * @param event Description of the key event.
5835 * @return If you handled the event, return true. If you want to allow the
5836 * event to be handled by the next receiver, return false.
5837 */
5838 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5839 return false;
5840 }
5841
5842 /**
5843 * Check whether the called view is a text editor, in which case it
5844 * would make sense to automatically display a soft input window for
5845 * it. Subclasses should override this if they implement
5846 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005847 * a call on that method would return a non-null InputConnection, and
5848 * they are really a first-class editor that the user would normally
5849 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005850 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005851 * <p>The default implementation always returns false. This does
5852 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5853 * will not be called or the user can not otherwise perform edits on your
5854 * view; it is just a hint to the system that this is not the primary
5855 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005856 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005857 * @return Returns true if this view is a text editor, else false.
5858 */
5859 public boolean onCheckIsTextEditor() {
5860 return false;
5861 }
Romain Guy8506ab42009-06-11 17:35:47 -07005862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005863 /**
5864 * Create a new InputConnection for an InputMethod to interact
5865 * with the view. The default implementation returns null, since it doesn't
5866 * support input methods. You can override this to implement such support.
5867 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 * <p>When implementing this, you probably also want to implement
5870 * {@link #onCheckIsTextEditor()} to indicate you will return a
5871 * non-null InputConnection.
5872 *
5873 * @param outAttrs Fill in with attribute information about the connection.
5874 */
5875 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5876 return null;
5877 }
5878
5879 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005880 * Called by the {@link android.view.inputmethod.InputMethodManager}
5881 * when a view who is not the current
5882 * input connection target is trying to make a call on the manager. The
5883 * default implementation returns false; you can override this to return
5884 * true for certain views if you are performing InputConnection proxying
5885 * to them.
5886 * @param view The View that is making the InputMethodManager call.
5887 * @return Return true to allow the call, false to reject.
5888 */
5889 public boolean checkInputConnectionProxy(View view) {
5890 return false;
5891 }
Romain Guy8506ab42009-06-11 17:35:47 -07005892
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005893 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005894 * Show the context menu for this view. It is not safe to hold on to the
5895 * menu after returning from this method.
5896 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005897 * You should normally not overload this method. Overload
5898 * {@link #onCreateContextMenu(ContextMenu)} or define an
5899 * {@link OnCreateContextMenuListener} to add items to the context menu.
5900 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005901 * @param menu The context menu to populate
5902 */
5903 public void createContextMenu(ContextMenu menu) {
5904 ContextMenuInfo menuInfo = getContextMenuInfo();
5905
5906 // Sets the current menu info so all items added to menu will have
5907 // my extra info set.
5908 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5909
5910 onCreateContextMenu(menu);
5911 if (mOnCreateContextMenuListener != null) {
5912 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5913 }
5914
5915 // Clear the extra information so subsequent items that aren't mine don't
5916 // have my extra info.
5917 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5918
5919 if (mParent != null) {
5920 mParent.createContextMenu(menu);
5921 }
5922 }
5923
5924 /**
5925 * Views should implement this if they have extra information to associate
5926 * with the context menu. The return result is supplied as a parameter to
5927 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5928 * callback.
5929 *
5930 * @return Extra information about the item for which the context menu
5931 * should be shown. This information will vary across different
5932 * subclasses of View.
5933 */
5934 protected ContextMenuInfo getContextMenuInfo() {
5935 return null;
5936 }
5937
5938 /**
5939 * Views should implement this if the view itself is going to add items to
5940 * the context menu.
5941 *
5942 * @param menu the context menu to populate
5943 */
5944 protected void onCreateContextMenu(ContextMenu menu) {
5945 }
5946
5947 /**
5948 * Implement this method to handle trackball motion events. The
5949 * <em>relative</em> movement of the trackball since the last event
5950 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5951 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5952 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5953 * they will often be fractional values, representing the more fine-grained
5954 * movement information available from a trackball).
5955 *
5956 * @param event The motion event.
5957 * @return True if the event was handled, false otherwise.
5958 */
5959 public boolean onTrackballEvent(MotionEvent event) {
5960 return false;
5961 }
5962
5963 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005964 * Implement this method to handle generic motion events.
5965 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005966 * Generic motion events describe joystick movements, mouse hovers, track pad
5967 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005968 * {@link MotionEvent#getSource() source} of the motion event specifies
5969 * the class of input that was received. Implementations of this method
5970 * must examine the bits in the source before processing the event.
5971 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005972 * </p><p>
5973 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5974 * are delivered to the view under the pointer. All other generic motion events are
5975 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005976 * </p>
5977 * <code>
5978 * public boolean onGenericMotionEvent(MotionEvent event) {
5979 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005980 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5981 * // process the joystick movement...
5982 * return true;
5983 * }
5984 * }
5985 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5986 * switch (event.getAction()) {
5987 * case MotionEvent.ACTION_HOVER_MOVE:
5988 * // process the mouse hover movement...
5989 * return true;
5990 * case MotionEvent.ACTION_SCROLL:
5991 * // process the scroll wheel movement...
5992 * return true;
5993 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08005994 * }
5995 * return super.onGenericMotionEvent(event);
5996 * }
5997 * </code>
5998 *
5999 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006000 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006001 */
6002 public boolean onGenericMotionEvent(MotionEvent event) {
6003 return false;
6004 }
6005
6006 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006007 * Implement this method to handle hover events.
6008 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006009 * This method is called whenever a pointer is hovering into, over, or out of the
6010 * bounds of a view and the view is not currently being touched.
6011 * Hover events are represented as pointer events with action
6012 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6013 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6014 * </p>
6015 * <ul>
6016 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6017 * when the pointer enters the bounds of the view.</li>
6018 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6019 * when the pointer has already entered the bounds of the view and has moved.</li>
6020 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6021 * when the pointer has exited the bounds of the view or when the pointer is
6022 * about to go down due to a button click, tap, or similar user action that
6023 * causes the view to be touched.</li>
6024 * </ul>
6025 * <p>
6026 * The view should implement this method to return true to indicate that it is
6027 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006028 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006029 * The default implementation calls {@link #setHovered} to update the hovered state
6030 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006031 * is enabled and is clickable. The default implementation also sends hover
6032 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006033 * </p>
6034 *
6035 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006036 * @return True if the view handled the hover event.
6037 *
6038 * @see #isHovered
6039 * @see #setHovered
6040 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006041 */
6042 public boolean onHoverEvent(MotionEvent event) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006043 // The root view may receive hover (or touch) events that are outside the bounds of
6044 // the window. This code ensures that we only send accessibility events for
6045 // hovers that are actually within the bounds of the root view.
6046 final int action = event.getAction();
6047 if (!mSendingHoverAccessibilityEvents) {
6048 if ((action == MotionEvent.ACTION_HOVER_ENTER
6049 || action == MotionEvent.ACTION_HOVER_MOVE)
6050 && !hasHoveredChild()
6051 && pointInView(event.getX(), event.getY())) {
6052 mSendingHoverAccessibilityEvents = true;
6053 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6054 }
6055 } else {
6056 if (action == MotionEvent.ACTION_HOVER_EXIT
6057 || (action == MotionEvent.ACTION_HOVER_MOVE
6058 && !pointInView(event.getX(), event.getY()))) {
6059 mSendingHoverAccessibilityEvents = false;
6060 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6061 }
Jeff Browna1b24182011-07-28 13:38:24 -07006062 }
6063
Jeff Brown87b7f802011-06-21 18:35:45 -07006064 if (isHoverable()) {
Svetoslav Ganov4c1c1012011-08-31 18:39:18 -07006065 switch (action) {
Jeff Brown10b62902011-06-20 16:40:37 -07006066 case MotionEvent.ACTION_HOVER_ENTER:
6067 setHovered(true);
6068 break;
6069 case MotionEvent.ACTION_HOVER_EXIT:
6070 setHovered(false);
6071 break;
6072 }
Jeff Browna1b24182011-07-28 13:38:24 -07006073
6074 // Dispatch the event to onGenericMotionEvent before returning true.
6075 // This is to provide compatibility with existing applications that
6076 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6077 // break because of the new default handling for hoverable views
6078 // in onHoverEvent.
6079 // Note that onGenericMotionEvent will be called by default when
6080 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6081 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006082 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006083 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006084 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006085 }
6086
6087 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006088 * Returns true if the view should handle {@link #onHoverEvent}
6089 * by calling {@link #setHovered} to change its hovered state.
6090 *
6091 * @return True if the view is hoverable.
6092 */
6093 private boolean isHoverable() {
6094 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006095 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006096 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6097 return false;
6098 }
6099
6100 return (viewFlags & CLICKABLE) == CLICKABLE
6101 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6102 }
6103
6104 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006105 * Returns true if the view is currently hovered.
6106 *
6107 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006108 *
6109 * @see #setHovered
6110 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006111 */
Jeff Brown10b62902011-06-20 16:40:37 -07006112 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006113 public boolean isHovered() {
6114 return (mPrivateFlags & HOVERED) != 0;
6115 }
6116
6117 /**
6118 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006119 * <p>
6120 * Calling this method also changes the drawable state of the view. This
6121 * enables the view to react to hover by using different drawable resources
6122 * to change its appearance.
6123 * </p><p>
6124 * The {@link #onHoverChanged} method is called when the hovered state changes.
6125 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006126 *
6127 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006128 *
6129 * @see #isHovered
6130 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006131 */
6132 public void setHovered(boolean hovered) {
6133 if (hovered) {
6134 if ((mPrivateFlags & HOVERED) == 0) {
6135 mPrivateFlags |= HOVERED;
6136 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006137 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006138 }
6139 } else {
6140 if ((mPrivateFlags & HOVERED) != 0) {
6141 mPrivateFlags &= ~HOVERED;
6142 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006143 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006144 }
6145 }
6146 }
6147
6148 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006149 * Implement this method to handle hover state changes.
6150 * <p>
6151 * This method is called whenever the hover state changes as a result of a
6152 * call to {@link #setHovered}.
6153 * </p>
6154 *
6155 * @param hovered The current hover state, as returned by {@link #isHovered}.
6156 *
6157 * @see #isHovered
6158 * @see #setHovered
6159 */
6160 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006161 }
6162
6163 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006164 * Implement this method to handle touch screen motion events.
6165 *
6166 * @param event The motion event.
6167 * @return True if the event was handled, false otherwise.
6168 */
6169 public boolean onTouchEvent(MotionEvent event) {
6170 final int viewFlags = mViewFlags;
6171
6172 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006173 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6174 mPrivateFlags &= ~PRESSED;
6175 refreshDrawableState();
6176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006177 // A disabled view that is clickable still consumes the touch
6178 // events, it just doesn't respond to them.
6179 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6180 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6181 }
6182
6183 if (mTouchDelegate != null) {
6184 if (mTouchDelegate.onTouchEvent(event)) {
6185 return true;
6186 }
6187 }
6188
6189 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6190 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6191 switch (event.getAction()) {
6192 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006193 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6194 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006195 // take focus if we don't have it already and we should in
6196 // touch mode.
6197 boolean focusTaken = false;
6198 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6199 focusTaken = requestFocus();
6200 }
6201
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006202 if (prepressed) {
6203 // The button is being released before we actually
6204 // showed it as pressed. Make it show the pressed
6205 // state now (before scheduling the click) to ensure
6206 // the user sees it.
6207 mPrivateFlags |= PRESSED;
6208 refreshDrawableState();
6209 }
Joe Malin32736f02011-01-19 16:14:20 -08006210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006211 if (!mHasPerformedLongPress) {
6212 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006213 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006214
6215 // Only perform take click actions if we were in the pressed state
6216 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006217 // Use a Runnable and post this rather than calling
6218 // performClick directly. This lets other visual state
6219 // of the view update before click actions start.
6220 if (mPerformClick == null) {
6221 mPerformClick = new PerformClick();
6222 }
6223 if (!post(mPerformClick)) {
6224 performClick();
6225 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006226 }
6227 }
6228
6229 if (mUnsetPressedState == null) {
6230 mUnsetPressedState = new UnsetPressedState();
6231 }
6232
Adam Powelle14579b2009-12-16 18:39:52 -08006233 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006234 postDelayed(mUnsetPressedState,
6235 ViewConfiguration.getPressedStateDuration());
6236 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006237 // If the post failed, unpress right now
6238 mUnsetPressedState.run();
6239 }
Adam Powelle14579b2009-12-16 18:39:52 -08006240 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006241 }
6242 break;
6243
6244 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006245 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006246
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006247 if (performButtonActionOnTouchDown(event)) {
6248 break;
6249 }
6250
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006251 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006252 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006253
6254 // For views inside a scrolling container, delay the pressed feedback for
6255 // a short period in case this is a scroll.
6256 if (isInScrollingContainer) {
6257 mPrivateFlags |= PREPRESSED;
6258 if (mPendingCheckForTap == null) {
6259 mPendingCheckForTap = new CheckForTap();
6260 }
6261 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6262 } else {
6263 // Not inside a scrolling container, so show the feedback right away
6264 mPrivateFlags |= PRESSED;
6265 refreshDrawableState();
6266 checkForLongClick(0);
6267 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006268 break;
6269
6270 case MotionEvent.ACTION_CANCEL:
6271 mPrivateFlags &= ~PRESSED;
6272 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006273 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 break;
6275
6276 case MotionEvent.ACTION_MOVE:
6277 final int x = (int) event.getX();
6278 final int y = (int) event.getY();
6279
6280 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006281 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006282 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006283 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006284 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006285 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006286 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006287
6288 // Need to switch from pressed to not pressed
6289 mPrivateFlags &= ~PRESSED;
6290 refreshDrawableState();
6291 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006292 }
6293 break;
6294 }
6295 return true;
6296 }
6297
6298 return false;
6299 }
6300
6301 /**
Adam Powell10298662011-08-14 18:26:30 -07006302 * @hide
6303 */
6304 public boolean isInScrollingContainer() {
6305 ViewParent p = getParent();
6306 while (p != null && p instanceof ViewGroup) {
6307 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6308 return true;
6309 }
6310 p = p.getParent();
6311 }
6312 return false;
6313 }
6314
6315 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006316 * Remove the longpress detection timer.
6317 */
6318 private void removeLongPressCallback() {
6319 if (mPendingCheckForLongPress != null) {
6320 removeCallbacks(mPendingCheckForLongPress);
6321 }
6322 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006323
6324 /**
6325 * Remove the pending click action
6326 */
6327 private void removePerformClickCallback() {
6328 if (mPerformClick != null) {
6329 removeCallbacks(mPerformClick);
6330 }
6331 }
6332
Adam Powelle14579b2009-12-16 18:39:52 -08006333 /**
Romain Guya440b002010-02-24 15:57:54 -08006334 * Remove the prepress detection timer.
6335 */
6336 private void removeUnsetPressCallback() {
6337 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6338 setPressed(false);
6339 removeCallbacks(mUnsetPressedState);
6340 }
6341 }
6342
6343 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006344 * Remove the tap detection timer.
6345 */
6346 private void removeTapCallback() {
6347 if (mPendingCheckForTap != null) {
6348 mPrivateFlags &= ~PREPRESSED;
6349 removeCallbacks(mPendingCheckForTap);
6350 }
6351 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006352
6353 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006354 * Cancels a pending long press. Your subclass can use this if you
6355 * want the context menu to come up if the user presses and holds
6356 * at the same place, but you don't want it to come up if they press
6357 * and then move around enough to cause scrolling.
6358 */
6359 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006360 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006361
6362 /*
6363 * The prepressed state handled by the tap callback is a display
6364 * construct, but the tap callback will post a long press callback
6365 * less its own timeout. Remove it here.
6366 */
6367 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006368 }
6369
6370 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006371 * Remove the pending callback for sending a
6372 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6373 */
6374 private void removeSendViewScrolledAccessibilityEventCallback() {
6375 if (mSendViewScrolledAccessibilityEvent != null) {
6376 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6377 }
6378 }
6379
6380 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006381 * Sets the TouchDelegate for this View.
6382 */
6383 public void setTouchDelegate(TouchDelegate delegate) {
6384 mTouchDelegate = delegate;
6385 }
6386
6387 /**
6388 * Gets the TouchDelegate for this View.
6389 */
6390 public TouchDelegate getTouchDelegate() {
6391 return mTouchDelegate;
6392 }
6393
6394 /**
6395 * Set flags controlling behavior of this view.
6396 *
6397 * @param flags Constant indicating the value which should be set
6398 * @param mask Constant indicating the bit range that should be changed
6399 */
6400 void setFlags(int flags, int mask) {
6401 int old = mViewFlags;
6402 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6403
6404 int changed = mViewFlags ^ old;
6405 if (changed == 0) {
6406 return;
6407 }
6408 int privateFlags = mPrivateFlags;
6409
6410 /* Check if the FOCUSABLE bit has changed */
6411 if (((changed & FOCUSABLE_MASK) != 0) &&
6412 ((privateFlags & HAS_BOUNDS) !=0)) {
6413 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6414 && ((privateFlags & FOCUSED) != 0)) {
6415 /* Give up focus if we are no longer focusable */
6416 clearFocus();
6417 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6418 && ((privateFlags & FOCUSED) == 0)) {
6419 /*
6420 * Tell the view system that we are now available to take focus
6421 * if no one else already has it.
6422 */
6423 if (mParent != null) mParent.focusableViewAvailable(this);
6424 }
6425 }
6426
6427 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6428 if ((changed & VISIBILITY_MASK) != 0) {
6429 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006430 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006431 * it was not visible. Marking it drawn ensures that the invalidation will
6432 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006433 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006434 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006435 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006436
6437 needGlobalAttributesUpdate(true);
6438
6439 // a view becoming visible is worth notifying the parent
6440 // about in case nothing has focus. even if this specific view
6441 // isn't focusable, it may contain something that is, so let
6442 // the root view try to give this focus if nothing else does.
6443 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6444 mParent.focusableViewAvailable(this);
6445 }
6446 }
6447 }
6448
6449 /* Check if the GONE bit has changed */
6450 if ((changed & GONE) != 0) {
6451 needGlobalAttributesUpdate(false);
6452 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006453
Romain Guyecd80ee2009-12-03 17:13:02 -08006454 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6455 if (hasFocus()) clearFocus();
6456 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006457 if (mParent instanceof View) {
6458 // GONE views noop invalidation, so invalidate the parent
6459 ((View) mParent).invalidate(true);
6460 }
6461 // Mark the view drawn to ensure that it gets invalidated properly the next
6462 // time it is visible and gets invalidated
6463 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006464 }
6465 if (mAttachInfo != null) {
6466 mAttachInfo.mViewVisibilityChanged = true;
6467 }
6468 }
6469
6470 /* Check if the VISIBLE bit has changed */
6471 if ((changed & INVISIBLE) != 0) {
6472 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006473 /*
6474 * If this view is becoming invisible, set the DRAWN flag so that
6475 * the next invalidate() will not be skipped.
6476 */
6477 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006478
6479 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6480 // root view becoming invisible shouldn't clear focus
6481 if (getRootView() != this) {
6482 clearFocus();
6483 }
6484 }
6485 if (mAttachInfo != null) {
6486 mAttachInfo.mViewVisibilityChanged = true;
6487 }
6488 }
6489
Adam Powell326d8082009-12-09 15:10:07 -08006490 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006491 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006492 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6493 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006494 } else if (mParent != null) {
6495 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006496 }
Adam Powell326d8082009-12-09 15:10:07 -08006497 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6498 }
6499
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006500 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6501 destroyDrawingCache();
6502 }
6503
6504 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6505 destroyDrawingCache();
6506 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006507 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006508 }
6509
6510 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6511 destroyDrawingCache();
6512 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6513 }
6514
6515 if ((changed & DRAW_MASK) != 0) {
6516 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6517 if (mBGDrawable != null) {
6518 mPrivateFlags &= ~SKIP_DRAW;
6519 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6520 } else {
6521 mPrivateFlags |= SKIP_DRAW;
6522 }
6523 } else {
6524 mPrivateFlags &= ~SKIP_DRAW;
6525 }
6526 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006527 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006528 }
6529
6530 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006531 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006532 mParent.recomputeViewAttributes(this);
6533 }
6534 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006535
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006536 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006537 requestLayout();
6538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006539 }
6540
6541 /**
6542 * Change the view's z order in the tree, so it's on top of other sibling
6543 * views
6544 */
6545 public void bringToFront() {
6546 if (mParent != null) {
6547 mParent.bringChildToFront(this);
6548 }
6549 }
6550
6551 /**
6552 * This is called in response to an internal scroll in this view (i.e., the
6553 * view scrolled its own contents). This is typically as a result of
6554 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6555 * called.
6556 *
6557 * @param l Current horizontal scroll origin.
6558 * @param t Current vertical scroll origin.
6559 * @param oldl Previous horizontal scroll origin.
6560 * @param oldt Previous vertical scroll origin.
6561 */
6562 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006563 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6564 postSendViewScrolledAccessibilityEventCallback();
6565 }
6566
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006567 mBackgroundSizeChanged = true;
6568
6569 final AttachInfo ai = mAttachInfo;
6570 if (ai != null) {
6571 ai.mViewScrollChanged = true;
6572 }
6573 }
6574
6575 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006576 * Interface definition for a callback to be invoked when the layout bounds of a view
6577 * changes due to layout processing.
6578 */
6579 public interface OnLayoutChangeListener {
6580 /**
6581 * Called when the focus state of a view has changed.
6582 *
6583 * @param v The view whose state has changed.
6584 * @param left The new value of the view's left property.
6585 * @param top The new value of the view's top property.
6586 * @param right The new value of the view's right property.
6587 * @param bottom The new value of the view's bottom property.
6588 * @param oldLeft The previous value of the view's left property.
6589 * @param oldTop The previous value of the view's top property.
6590 * @param oldRight The previous value of the view's right property.
6591 * @param oldBottom The previous value of the view's bottom property.
6592 */
6593 void onLayoutChange(View v, int left, int top, int right, int bottom,
6594 int oldLeft, int oldTop, int oldRight, int oldBottom);
6595 }
6596
6597 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006598 * This is called during layout when the size of this view has changed. If
6599 * you were just added to the view hierarchy, you're called with the old
6600 * values of 0.
6601 *
6602 * @param w Current width of this view.
6603 * @param h Current height of this view.
6604 * @param oldw Old width of this view.
6605 * @param oldh Old height of this view.
6606 */
6607 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6608 }
6609
6610 /**
6611 * Called by draw to draw the child views. This may be overridden
6612 * by derived classes to gain control just before its children are drawn
6613 * (but after its own view has been drawn).
6614 * @param canvas the canvas on which to draw the view
6615 */
6616 protected void dispatchDraw(Canvas canvas) {
6617 }
6618
6619 /**
6620 * Gets the parent of this view. Note that the parent is a
6621 * ViewParent and not necessarily a View.
6622 *
6623 * @return Parent of this view.
6624 */
6625 public final ViewParent getParent() {
6626 return mParent;
6627 }
6628
6629 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006630 * Set the horizontal scrolled position of your view. This will cause a call to
6631 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6632 * invalidated.
6633 * @param value the x position to scroll to
6634 */
6635 public void setScrollX(int value) {
6636 scrollTo(value, mScrollY);
6637 }
6638
6639 /**
6640 * Set the vertical scrolled position of your view. This will cause a call to
6641 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6642 * invalidated.
6643 * @param value the y position to scroll to
6644 */
6645 public void setScrollY(int value) {
6646 scrollTo(mScrollX, value);
6647 }
6648
6649 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006650 * Return the scrolled left position of this view. This is the left edge of
6651 * the displayed part of your view. You do not need to draw any pixels
6652 * farther left, since those are outside of the frame of your view on
6653 * screen.
6654 *
6655 * @return The left edge of the displayed part of your view, in pixels.
6656 */
6657 public final int getScrollX() {
6658 return mScrollX;
6659 }
6660
6661 /**
6662 * Return the scrolled top position of this view. This is the top edge of
6663 * the displayed part of your view. You do not need to draw any pixels above
6664 * it, since those are outside of the frame of your view on screen.
6665 *
6666 * @return The top edge of the displayed part of your view, in pixels.
6667 */
6668 public final int getScrollY() {
6669 return mScrollY;
6670 }
6671
6672 /**
6673 * Return the width of the your view.
6674 *
6675 * @return The width of your view, in pixels.
6676 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006677 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006678 public final int getWidth() {
6679 return mRight - mLeft;
6680 }
6681
6682 /**
6683 * Return the height of your view.
6684 *
6685 * @return The height of your view, in pixels.
6686 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006687 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006688 public final int getHeight() {
6689 return mBottom - mTop;
6690 }
6691
6692 /**
6693 * Return the visible drawing bounds of your view. Fills in the output
6694 * rectangle with the values from getScrollX(), getScrollY(),
6695 * getWidth(), and getHeight().
6696 *
6697 * @param outRect The (scrolled) drawing bounds of the view.
6698 */
6699 public void getDrawingRect(Rect outRect) {
6700 outRect.left = mScrollX;
6701 outRect.top = mScrollY;
6702 outRect.right = mScrollX + (mRight - mLeft);
6703 outRect.bottom = mScrollY + (mBottom - mTop);
6704 }
6705
6706 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006707 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6708 * raw width component (that is the result is masked by
6709 * {@link #MEASURED_SIZE_MASK}).
6710 *
6711 * @return The raw measured width of this view.
6712 */
6713 public final int getMeasuredWidth() {
6714 return mMeasuredWidth & MEASURED_SIZE_MASK;
6715 }
6716
6717 /**
6718 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006719 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006720 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006721 * This should be used during measurement and layout calculations only. Use
6722 * {@link #getWidth()} to see how wide a view is after layout.
6723 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006724 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006725 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006726 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 return mMeasuredWidth;
6728 }
6729
6730 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006731 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6732 * raw width component (that is the result is masked by
6733 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006734 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006735 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006736 */
6737 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006738 return mMeasuredHeight & MEASURED_SIZE_MASK;
6739 }
6740
6741 /**
6742 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006743 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006744 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6745 * This should be used during measurement and layout calculations only. Use
6746 * {@link #getHeight()} to see how wide a view is after layout.
6747 *
6748 * @return The measured width of this view as a bit mask.
6749 */
6750 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006751 return mMeasuredHeight;
6752 }
6753
6754 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006755 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6756 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6757 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6758 * and the height component is at the shifted bits
6759 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6760 */
6761 public final int getMeasuredState() {
6762 return (mMeasuredWidth&MEASURED_STATE_MASK)
6763 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6764 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6765 }
6766
6767 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006768 * The transform matrix of this view, which is calculated based on the current
6769 * roation, scale, and pivot properties.
6770 *
6771 * @see #getRotation()
6772 * @see #getScaleX()
6773 * @see #getScaleY()
6774 * @see #getPivotX()
6775 * @see #getPivotY()
6776 * @return The current transform matrix for the view
6777 */
6778 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006779 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006780 return mMatrix;
6781 }
6782
6783 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006784 * Utility function to determine if the value is far enough away from zero to be
6785 * considered non-zero.
6786 * @param value A floating point value to check for zero-ness
6787 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6788 */
6789 private static boolean nonzero(float value) {
6790 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6791 }
6792
6793 /**
Jeff Brown86671742010-09-30 20:00:15 -07006794 * Returns true if the transform matrix is the identity matrix.
6795 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006796 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006797 * @return True if the transform matrix is the identity matrix, false otherwise.
6798 */
Jeff Brown86671742010-09-30 20:00:15 -07006799 final boolean hasIdentityMatrix() {
6800 updateMatrix();
6801 return mMatrixIsIdentity;
6802 }
6803
6804 /**
6805 * Recomputes the transform matrix if necessary.
6806 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006807 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006808 if (mMatrixDirty) {
6809 // transform-related properties have changed since the last time someone
6810 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006811
6812 // Figure out if we need to update the pivot point
6813 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006814 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006815 mPrevWidth = mRight - mLeft;
6816 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006817 mPivotX = mPrevWidth / 2f;
6818 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006819 }
6820 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006821 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006822 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6823 mMatrix.setTranslate(mTranslationX, mTranslationY);
6824 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6825 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6826 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006827 if (mCamera == null) {
6828 mCamera = new Camera();
6829 matrix3D = new Matrix();
6830 }
6831 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006832 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006833 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006834 mCamera.getMatrix(matrix3D);
6835 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006836 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006837 mMatrix.postConcat(matrix3D);
6838 mCamera.restore();
6839 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006840 mMatrixDirty = false;
6841 mMatrixIsIdentity = mMatrix.isIdentity();
6842 mInverseMatrixDirty = true;
6843 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006844 }
6845
6846 /**
6847 * Utility method to retrieve the inverse of the current mMatrix property.
6848 * We cache the matrix to avoid recalculating it when transform properties
6849 * have not changed.
6850 *
6851 * @return The inverse of the current matrix of this view.
6852 */
Jeff Brown86671742010-09-30 20:00:15 -07006853 final Matrix getInverseMatrix() {
6854 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006855 if (mInverseMatrixDirty) {
6856 if (mInverseMatrix == null) {
6857 mInverseMatrix = new Matrix();
6858 }
6859 mMatrix.invert(mInverseMatrix);
6860 mInverseMatrixDirty = false;
6861 }
6862 return mInverseMatrix;
6863 }
6864
6865 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006866 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6867 * views are drawn) from the camera to this view. The camera's distance
6868 * affects 3D transformations, for instance rotations around the X and Y
6869 * axis. If the rotationX or rotationY properties are changed and this view is
6870 * large (more than half the size of the screen), it is recommended to always
6871 * use a camera distance that's greater than the height (X axis rotation) or
6872 * the width (Y axis rotation) of this view.</p>
6873 *
6874 * <p>The distance of the camera from the view plane can have an affect on the
6875 * perspective distortion of the view when it is rotated around the x or y axis.
6876 * For example, a large distance will result in a large viewing angle, and there
6877 * will not be much perspective distortion of the view as it rotates. A short
6878 * distance may cause much more perspective distortion upon rotation, and can
6879 * also result in some drawing artifacts if the rotated view ends up partially
6880 * behind the camera (which is why the recommendation is to use a distance at
6881 * least as far as the size of the view, if the view is to be rotated.)</p>
6882 *
6883 * <p>The distance is expressed in "depth pixels." The default distance depends
6884 * on the screen density. For instance, on a medium density display, the
6885 * default distance is 1280. On a high density display, the default distance
6886 * is 1920.</p>
6887 *
6888 * <p>If you want to specify a distance that leads to visually consistent
6889 * results across various densities, use the following formula:</p>
6890 * <pre>
6891 * float scale = context.getResources().getDisplayMetrics().density;
6892 * view.setCameraDistance(distance * scale);
6893 * </pre>
6894 *
6895 * <p>The density scale factor of a high density display is 1.5,
6896 * and 1920 = 1280 * 1.5.</p>
6897 *
6898 * @param distance The distance in "depth pixels", if negative the opposite
6899 * value is used
6900 *
6901 * @see #setRotationX(float)
6902 * @see #setRotationY(float)
6903 */
6904 public void setCameraDistance(float distance) {
6905 invalidateParentCaches();
6906 invalidate(false);
6907
6908 final float dpi = mResources.getDisplayMetrics().densityDpi;
6909 if (mCamera == null) {
6910 mCamera = new Camera();
6911 matrix3D = new Matrix();
6912 }
6913
6914 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6915 mMatrixDirty = true;
6916
6917 invalidate(false);
6918 }
6919
6920 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006921 * The degrees that the view is rotated around the pivot point.
6922 *
Romain Guya5364ee2011-02-24 14:46:04 -08006923 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006924 * @see #getPivotX()
6925 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006926 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006927 * @return The degrees of rotation.
6928 */
6929 public float getRotation() {
6930 return mRotation;
6931 }
6932
6933 /**
Chet Haase897247b2010-09-09 14:54:47 -07006934 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6935 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006936 *
6937 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006938 *
6939 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006940 * @see #getPivotX()
6941 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006942 * @see #setRotationX(float)
6943 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006944 *
6945 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006946 */
6947 public void setRotation(float rotation) {
6948 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006949 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006950 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006951 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006952 mRotation = rotation;
6953 mMatrixDirty = true;
6954 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006955 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006956 }
6957 }
6958
6959 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006960 * The degrees that the view is rotated around the vertical axis through the pivot point.
6961 *
6962 * @see #getPivotX()
6963 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006964 * @see #setRotationY(float)
6965 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006966 * @return The degrees of Y rotation.
6967 */
6968 public float getRotationY() {
6969 return mRotationY;
6970 }
6971
6972 /**
Chet Haase897247b2010-09-09 14:54:47 -07006973 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6974 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6975 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006976 *
6977 * When rotating large views, it is recommended to adjust the camera distance
6978 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006979 *
6980 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006981 *
6982 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006983 * @see #getPivotX()
6984 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006985 * @see #setRotation(float)
6986 * @see #setRotationX(float)
6987 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006988 *
6989 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006990 */
6991 public void setRotationY(float rotationY) {
6992 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006993 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07006994 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006995 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07006996 mRotationY = rotationY;
6997 mMatrixDirty = true;
6998 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006999 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007000 }
7001 }
7002
7003 /**
7004 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7005 *
7006 * @see #getPivotX()
7007 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007008 * @see #setRotationX(float)
7009 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007010 * @return The degrees of X rotation.
7011 */
7012 public float getRotationX() {
7013 return mRotationX;
7014 }
7015
7016 /**
Chet Haase897247b2010-09-09 14:54:47 -07007017 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7018 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7019 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007020 *
7021 * When rotating large views, it is recommended to adjust the camera distance
7022 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007023 *
7024 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007025 *
7026 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007027 * @see #getPivotX()
7028 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007029 * @see #setRotation(float)
7030 * @see #setRotationY(float)
7031 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007032 *
7033 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007034 */
7035 public void setRotationX(float rotationX) {
7036 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007037 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007038 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007039 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007040 mRotationX = rotationX;
7041 mMatrixDirty = true;
7042 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007043 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007044 }
7045 }
7046
7047 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007048 * The amount that the view is scaled in x around the pivot point, as a proportion of
7049 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7050 *
Joe Onorato93162322010-09-16 15:42:01 -04007051 * <p>By default, this is 1.0f.
7052 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007053 * @see #getPivotX()
7054 * @see #getPivotY()
7055 * @return The scaling factor.
7056 */
7057 public float getScaleX() {
7058 return mScaleX;
7059 }
7060
7061 /**
7062 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7063 * the view's unscaled width. A value of 1 means that no scaling is applied.
7064 *
7065 * @param scaleX The scaling factor.
7066 * @see #getPivotX()
7067 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007068 *
7069 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007070 */
7071 public void setScaleX(float scaleX) {
7072 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007073 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007074 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007075 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007076 mScaleX = scaleX;
7077 mMatrixDirty = true;
7078 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007079 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007080 }
7081 }
7082
7083 /**
7084 * The amount that the view is scaled in y around the pivot point, as a proportion of
7085 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7086 *
Joe Onorato93162322010-09-16 15:42:01 -04007087 * <p>By default, this is 1.0f.
7088 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007089 * @see #getPivotX()
7090 * @see #getPivotY()
7091 * @return The scaling factor.
7092 */
7093 public float getScaleY() {
7094 return mScaleY;
7095 }
7096
7097 /**
7098 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7099 * the view's unscaled width. A value of 1 means that no scaling is applied.
7100 *
7101 * @param scaleY The scaling factor.
7102 * @see #getPivotX()
7103 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007104 *
7105 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007106 */
7107 public void setScaleY(float scaleY) {
7108 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007109 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007110 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007111 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007112 mScaleY = scaleY;
7113 mMatrixDirty = true;
7114 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007115 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007116 }
7117 }
7118
7119 /**
7120 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7121 * and {@link #setScaleX(float) scaled}.
7122 *
7123 * @see #getRotation()
7124 * @see #getScaleX()
7125 * @see #getScaleY()
7126 * @see #getPivotY()
7127 * @return The x location of the pivot point.
7128 */
7129 public float getPivotX() {
7130 return mPivotX;
7131 }
7132
7133 /**
7134 * Sets the x location of the point around which the view is
7135 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007136 * By default, the pivot point is centered on the object.
7137 * Setting this property disables this behavior and causes the view to use only the
7138 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007139 *
7140 * @param pivotX The x location of the pivot point.
7141 * @see #getRotation()
7142 * @see #getScaleX()
7143 * @see #getScaleY()
7144 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007145 *
7146 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007147 */
7148 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007149 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007150 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007151 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007152 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007153 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007154 mPivotX = pivotX;
7155 mMatrixDirty = true;
7156 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007157 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007158 }
7159 }
7160
7161 /**
7162 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7163 * and {@link #setScaleY(float) scaled}.
7164 *
7165 * @see #getRotation()
7166 * @see #getScaleX()
7167 * @see #getScaleY()
7168 * @see #getPivotY()
7169 * @return The y location of the pivot point.
7170 */
7171 public float getPivotY() {
7172 return mPivotY;
7173 }
7174
7175 /**
7176 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007177 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7178 * Setting this property disables this behavior and causes the view to use only the
7179 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007180 *
7181 * @param pivotY The y location of the pivot point.
7182 * @see #getRotation()
7183 * @see #getScaleX()
7184 * @see #getScaleY()
7185 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007186 *
7187 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007188 */
7189 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007190 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007191 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007192 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007193 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007194 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007195 mPivotY = pivotY;
7196 mMatrixDirty = true;
7197 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007198 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007199 }
7200 }
7201
7202 /**
7203 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7204 * completely transparent and 1 means the view is completely opaque.
7205 *
Joe Onorato93162322010-09-16 15:42:01 -04007206 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007207 * @return The opacity of the view.
7208 */
7209 public float getAlpha() {
7210 return mAlpha;
7211 }
7212
7213 /**
Romain Guy171c5922011-01-06 10:04:23 -08007214 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7215 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007216 *
Romain Guy171c5922011-01-06 10:04:23 -08007217 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7218 * responsible for applying the opacity itself. Otherwise, calling this method is
7219 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007220 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007221 *
7222 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007223 *
Joe Malin32736f02011-01-19 16:14:20 -08007224 * @see #setLayerType(int, android.graphics.Paint)
7225 *
Chet Haase73066682010-11-29 15:55:32 -08007226 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 */
7228 public void setAlpha(float alpha) {
7229 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007230 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007231 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007232 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007233 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007234 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007235 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007236 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007237 invalidate(false);
7238 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007239 }
7240
7241 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007242 * Faster version of setAlpha() which performs the same steps except there are
7243 * no calls to invalidate(). The caller of this function should perform proper invalidation
7244 * on the parent and this object. The return value indicates whether the subclass handles
7245 * alpha (the return value for onSetAlpha()).
7246 *
7247 * @param alpha The new value for the alpha property
7248 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7249 */
7250 boolean setAlphaNoInvalidation(float alpha) {
7251 mAlpha = alpha;
7252 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7253 if (subclassHandlesAlpha) {
7254 mPrivateFlags |= ALPHA_SET;
7255 } else {
7256 mPrivateFlags &= ~ALPHA_SET;
7257 }
7258 return subclassHandlesAlpha;
7259 }
7260
7261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007262 * Top position of this view relative to its parent.
7263 *
7264 * @return The top of this view, in pixels.
7265 */
7266 @ViewDebug.CapturedViewProperty
7267 public final int getTop() {
7268 return mTop;
7269 }
7270
7271 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007272 * Sets the top position of this view relative to its parent. This method is meant to be called
7273 * by the layout system and should not generally be called otherwise, because the property
7274 * may be changed at any time by the layout.
7275 *
7276 * @param top The top of this view, in pixels.
7277 */
7278 public final void setTop(int top) {
7279 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007280 updateMatrix();
7281 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007282 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007283 int minTop;
7284 int yLoc;
7285 if (top < mTop) {
7286 minTop = top;
7287 yLoc = top - mTop;
7288 } else {
7289 minTop = mTop;
7290 yLoc = 0;
7291 }
Chet Haasee9140a72011-02-16 16:23:29 -08007292 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007293 }
7294 } else {
7295 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007296 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007297 }
7298
Chet Haaseed032702010-10-01 14:05:54 -07007299 int width = mRight - mLeft;
7300 int oldHeight = mBottom - mTop;
7301
Chet Haase21cd1382010-09-01 17:42:29 -07007302 mTop = top;
7303
Chet Haaseed032702010-10-01 14:05:54 -07007304 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7305
Chet Haase21cd1382010-09-01 17:42:29 -07007306 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007307 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7308 // A change in dimension means an auto-centered pivot point changes, too
7309 mMatrixDirty = true;
7310 }
Chet Haase21cd1382010-09-01 17:42:29 -07007311 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007312 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007313 }
Chet Haase55dbb652010-12-21 20:15:08 -08007314 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007315 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007316 }
7317 }
7318
7319 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007320 * Bottom position of this view relative to its parent.
7321 *
7322 * @return The bottom of this view, in pixels.
7323 */
7324 @ViewDebug.CapturedViewProperty
7325 public final int getBottom() {
7326 return mBottom;
7327 }
7328
7329 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007330 * True if this view has changed since the last time being drawn.
7331 *
7332 * @return The dirty state of this view.
7333 */
7334 public boolean isDirty() {
7335 return (mPrivateFlags & DIRTY_MASK) != 0;
7336 }
7337
7338 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007339 * Sets the bottom position of this view relative to its parent. This method is meant to be
7340 * called by the layout system and should not generally be called otherwise, because the
7341 * property may be changed at any time by the layout.
7342 *
7343 * @param bottom The bottom of this view, in pixels.
7344 */
7345 public final void setBottom(int bottom) {
7346 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007347 updateMatrix();
7348 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007349 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007350 int maxBottom;
7351 if (bottom < mBottom) {
7352 maxBottom = mBottom;
7353 } else {
7354 maxBottom = bottom;
7355 }
Chet Haasee9140a72011-02-16 16:23:29 -08007356 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007357 }
7358 } else {
7359 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007360 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007361 }
7362
Chet Haaseed032702010-10-01 14:05:54 -07007363 int width = mRight - mLeft;
7364 int oldHeight = mBottom - mTop;
7365
Chet Haase21cd1382010-09-01 17:42:29 -07007366 mBottom = bottom;
7367
Chet Haaseed032702010-10-01 14:05:54 -07007368 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7369
Chet Haase21cd1382010-09-01 17:42:29 -07007370 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007371 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7372 // A change in dimension means an auto-centered pivot point changes, too
7373 mMatrixDirty = true;
7374 }
Chet Haase21cd1382010-09-01 17:42:29 -07007375 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007376 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007377 }
Chet Haase55dbb652010-12-21 20:15:08 -08007378 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007379 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007380 }
7381 }
7382
7383 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007384 * Left position of this view relative to its parent.
7385 *
7386 * @return The left edge of this view, in pixels.
7387 */
7388 @ViewDebug.CapturedViewProperty
7389 public final int getLeft() {
7390 return mLeft;
7391 }
7392
7393 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007394 * Sets the left position of this view relative to its parent. This method is meant to be called
7395 * by the layout system and should not generally be called otherwise, because the property
7396 * may be changed at any time by the layout.
7397 *
7398 * @param left The bottom of this view, in pixels.
7399 */
7400 public final void setLeft(int left) {
7401 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007402 updateMatrix();
7403 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007404 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007405 int minLeft;
7406 int xLoc;
7407 if (left < mLeft) {
7408 minLeft = left;
7409 xLoc = left - mLeft;
7410 } else {
7411 minLeft = mLeft;
7412 xLoc = 0;
7413 }
Chet Haasee9140a72011-02-16 16:23:29 -08007414 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007415 }
7416 } else {
7417 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007418 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007419 }
7420
Chet Haaseed032702010-10-01 14:05:54 -07007421 int oldWidth = mRight - mLeft;
7422 int height = mBottom - mTop;
7423
Chet Haase21cd1382010-09-01 17:42:29 -07007424 mLeft = left;
7425
Chet Haaseed032702010-10-01 14:05:54 -07007426 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7427
Chet Haase21cd1382010-09-01 17:42:29 -07007428 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007429 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7430 // A change in dimension means an auto-centered pivot point changes, too
7431 mMatrixDirty = true;
7432 }
Chet Haase21cd1382010-09-01 17:42:29 -07007433 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007434 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007435 }
Chet Haase55dbb652010-12-21 20:15:08 -08007436 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007437 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007438 }
7439 }
7440
7441 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007442 * Right position of this view relative to its parent.
7443 *
7444 * @return The right edge of this view, in pixels.
7445 */
7446 @ViewDebug.CapturedViewProperty
7447 public final int getRight() {
7448 return mRight;
7449 }
7450
7451 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007452 * Sets the right position of this view relative to its parent. This method is meant to be called
7453 * by the layout system and should not generally be called otherwise, because the property
7454 * may be changed at any time by the layout.
7455 *
7456 * @param right The bottom of this view, in pixels.
7457 */
7458 public final void setRight(int right) {
7459 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007460 updateMatrix();
7461 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007462 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007463 int maxRight;
7464 if (right < mRight) {
7465 maxRight = mRight;
7466 } else {
7467 maxRight = right;
7468 }
Chet Haasee9140a72011-02-16 16:23:29 -08007469 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007470 }
7471 } else {
7472 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007473 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007474 }
7475
Chet Haaseed032702010-10-01 14:05:54 -07007476 int oldWidth = mRight - mLeft;
7477 int height = mBottom - mTop;
7478
Chet Haase21cd1382010-09-01 17:42:29 -07007479 mRight = right;
7480
Chet Haaseed032702010-10-01 14:05:54 -07007481 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7482
Chet Haase21cd1382010-09-01 17:42:29 -07007483 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007484 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7485 // A change in dimension means an auto-centered pivot point changes, too
7486 mMatrixDirty = true;
7487 }
Chet Haase21cd1382010-09-01 17:42:29 -07007488 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007489 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007490 }
Chet Haase55dbb652010-12-21 20:15:08 -08007491 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007492 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007493 }
7494 }
7495
7496 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007497 * The visual x position of this view, in pixels. This is equivalent to the
7498 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007499 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007500 *
Chet Haasedf030d22010-07-30 17:22:38 -07007501 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007502 */
Chet Haasedf030d22010-07-30 17:22:38 -07007503 public float getX() {
7504 return mLeft + mTranslationX;
7505 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007506
Chet Haasedf030d22010-07-30 17:22:38 -07007507 /**
7508 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7509 * {@link #setTranslationX(float) translationX} property to be the difference between
7510 * the x value passed in and the current {@link #getLeft() left} property.
7511 *
7512 * @param x The visual x position of this view, in pixels.
7513 */
7514 public void setX(float x) {
7515 setTranslationX(x - mLeft);
7516 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007517
Chet Haasedf030d22010-07-30 17:22:38 -07007518 /**
7519 * The visual y position of this view, in pixels. This is equivalent to the
7520 * {@link #setTranslationY(float) translationY} property plus the current
7521 * {@link #getTop() top} property.
7522 *
7523 * @return The visual y position of this view, in pixels.
7524 */
7525 public float getY() {
7526 return mTop + mTranslationY;
7527 }
7528
7529 /**
7530 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7531 * {@link #setTranslationY(float) translationY} property to be the difference between
7532 * the y value passed in and the current {@link #getTop() top} property.
7533 *
7534 * @param y The visual y position of this view, in pixels.
7535 */
7536 public void setY(float y) {
7537 setTranslationY(y - mTop);
7538 }
7539
7540
7541 /**
7542 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7543 * This position is post-layout, in addition to wherever the object's
7544 * layout placed it.
7545 *
7546 * @return The horizontal position of this view relative to its left position, in pixels.
7547 */
7548 public float getTranslationX() {
7549 return mTranslationX;
7550 }
7551
7552 /**
7553 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7554 * This effectively positions the object post-layout, in addition to wherever the object's
7555 * layout placed it.
7556 *
7557 * @param translationX The horizontal position of this view relative to its left position,
7558 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007559 *
7560 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007561 */
7562 public void setTranslationX(float translationX) {
7563 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007564 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007565 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007566 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007567 mTranslationX = translationX;
7568 mMatrixDirty = true;
7569 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007570 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007571 }
7572 }
7573
7574 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007575 * The horizontal location of this view relative to its {@link #getTop() top} position.
7576 * This position is post-layout, in addition to wherever the object's
7577 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007578 *
Chet Haasedf030d22010-07-30 17:22:38 -07007579 * @return The vertical position of this view relative to its top position,
7580 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007581 */
Chet Haasedf030d22010-07-30 17:22:38 -07007582 public float getTranslationY() {
7583 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 }
7585
7586 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007587 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7588 * This effectively positions the object post-layout, in addition to wherever the object's
7589 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007590 *
Chet Haasedf030d22010-07-30 17:22:38 -07007591 * @param translationY The vertical position of this view relative to its top position,
7592 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007593 *
7594 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007595 */
Chet Haasedf030d22010-07-30 17:22:38 -07007596 public void setTranslationY(float translationY) {
7597 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007598 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007599 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007600 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007601 mTranslationY = translationY;
7602 mMatrixDirty = true;
7603 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007604 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007605 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007606 }
7607
7608 /**
Romain Guyda489792011-02-03 01:05:15 -08007609 * @hide
7610 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007611 public void setFastTranslationX(float x) {
7612 mTranslationX = x;
7613 mMatrixDirty = true;
7614 }
7615
7616 /**
7617 * @hide
7618 */
7619 public void setFastTranslationY(float y) {
7620 mTranslationY = y;
7621 mMatrixDirty = true;
7622 }
7623
7624 /**
7625 * @hide
7626 */
Romain Guyda489792011-02-03 01:05:15 -08007627 public void setFastX(float x) {
7628 mTranslationX = x - mLeft;
7629 mMatrixDirty = true;
7630 }
7631
7632 /**
7633 * @hide
7634 */
7635 public void setFastY(float y) {
7636 mTranslationY = y - mTop;
7637 mMatrixDirty = true;
7638 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007639
Romain Guyda489792011-02-03 01:05:15 -08007640 /**
7641 * @hide
7642 */
7643 public void setFastScaleX(float x) {
7644 mScaleX = x;
7645 mMatrixDirty = true;
7646 }
7647
7648 /**
7649 * @hide
7650 */
7651 public void setFastScaleY(float y) {
7652 mScaleY = y;
7653 mMatrixDirty = true;
7654 }
7655
7656 /**
7657 * @hide
7658 */
7659 public void setFastAlpha(float alpha) {
7660 mAlpha = alpha;
7661 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007662
Romain Guyda489792011-02-03 01:05:15 -08007663 /**
7664 * @hide
7665 */
7666 public void setFastRotationY(float y) {
7667 mRotationY = y;
7668 mMatrixDirty = true;
7669 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007670
Romain Guyda489792011-02-03 01:05:15 -08007671 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007672 * Hit rectangle in parent's coordinates
7673 *
7674 * @param outRect The hit rectangle of the view.
7675 */
7676 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007677 updateMatrix();
7678 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007679 outRect.set(mLeft, mTop, mRight, mBottom);
7680 } else {
7681 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007682 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007683 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007684 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7685 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007686 }
7687 }
7688
7689 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007690 * Determines whether the given point, in local coordinates is inside the view.
7691 */
7692 /*package*/ final boolean pointInView(float localX, float localY) {
7693 return localX >= 0 && localX < (mRight - mLeft)
7694 && localY >= 0 && localY < (mBottom - mTop);
7695 }
7696
7697 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007698 * Utility method to determine whether the given point, in local coordinates,
7699 * is inside the view, where the area of the view is expanded by the slop factor.
7700 * This method is called while processing touch-move events to determine if the event
7701 * is still within the view.
7702 */
7703 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007704 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007705 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007706 }
7707
7708 /**
7709 * When a view has focus and the user navigates away from it, the next view is searched for
7710 * starting from the rectangle filled in by this method.
7711 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007712 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7713 * of the view. However, if your view maintains some idea of internal selection,
7714 * such as a cursor, or a selected row or column, you should override this method and
7715 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007716 *
7717 * @param r The rectangle to fill in, in this view's coordinates.
7718 */
7719 public void getFocusedRect(Rect r) {
7720 getDrawingRect(r);
7721 }
7722
7723 /**
7724 * If some part of this view is not clipped by any of its parents, then
7725 * return that area in r in global (root) coordinates. To convert r to local
7726 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7727 * -globalOffset.y)) If the view is completely clipped or translated out,
7728 * return false.
7729 *
7730 * @param r If true is returned, r holds the global coordinates of the
7731 * visible portion of this view.
7732 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7733 * between this view and its root. globalOffet may be null.
7734 * @return true if r is non-empty (i.e. part of the view is visible at the
7735 * root level.
7736 */
7737 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7738 int width = mRight - mLeft;
7739 int height = mBottom - mTop;
7740 if (width > 0 && height > 0) {
7741 r.set(0, 0, width, height);
7742 if (globalOffset != null) {
7743 globalOffset.set(-mScrollX, -mScrollY);
7744 }
7745 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7746 }
7747 return false;
7748 }
7749
7750 public final boolean getGlobalVisibleRect(Rect r) {
7751 return getGlobalVisibleRect(r, null);
7752 }
7753
7754 public final boolean getLocalVisibleRect(Rect r) {
7755 Point offset = new Point();
7756 if (getGlobalVisibleRect(r, offset)) {
7757 r.offset(-offset.x, -offset.y); // make r local
7758 return true;
7759 }
7760 return false;
7761 }
7762
7763 /**
7764 * Offset this view's vertical location by the specified number of pixels.
7765 *
7766 * @param offset the number of pixels to offset the view by
7767 */
7768 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007769 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007770 updateMatrix();
7771 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007772 final ViewParent p = mParent;
7773 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007774 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007775 int minTop;
7776 int maxBottom;
7777 int yLoc;
7778 if (offset < 0) {
7779 minTop = mTop + offset;
7780 maxBottom = mBottom;
7781 yLoc = offset;
7782 } else {
7783 minTop = mTop;
7784 maxBottom = mBottom + offset;
7785 yLoc = 0;
7786 }
7787 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7788 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007789 }
7790 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007791 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007792 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007793
Chet Haasec3aa3612010-06-17 08:50:37 -07007794 mTop += offset;
7795 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007796
Chet Haasec3aa3612010-06-17 08:50:37 -07007797 if (!mMatrixIsIdentity) {
7798 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007799 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007801 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007802 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007803 }
7804
7805 /**
7806 * Offset this view's horizontal location by the specified amount of pixels.
7807 *
7808 * @param offset the numer of pixels to offset the view by
7809 */
7810 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007811 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007812 updateMatrix();
7813 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007814 final ViewParent p = mParent;
7815 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007816 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007817 int minLeft;
7818 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007819 if (offset < 0) {
7820 minLeft = mLeft + offset;
7821 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007822 } else {
7823 minLeft = mLeft;
7824 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007825 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007826 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007827 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007828 }
7829 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007830 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007831 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007832
Chet Haasec3aa3612010-06-17 08:50:37 -07007833 mLeft += offset;
7834 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007835
Chet Haasec3aa3612010-06-17 08:50:37 -07007836 if (!mMatrixIsIdentity) {
7837 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007838 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007839 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007840 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007841 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007842 }
7843
7844 /**
7845 * Get the LayoutParams associated with this view. All views should have
7846 * layout parameters. These supply parameters to the <i>parent</i> of this
7847 * view specifying how it should be arranged. There are many subclasses of
7848 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7849 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007850 *
7851 * This method may return null if this View is not attached to a parent
7852 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7853 * was not invoked successfully. When a View is attached to a parent
7854 * ViewGroup, this method must not return null.
7855 *
7856 * @return The LayoutParams associated with this view, or null if no
7857 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007858 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007859 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007860 public ViewGroup.LayoutParams getLayoutParams() {
7861 return mLayoutParams;
7862 }
7863
7864 /**
7865 * Set the layout parameters associated with this view. These supply
7866 * parameters to the <i>parent</i> of this view specifying how it should be
7867 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7868 * correspond to the different subclasses of ViewGroup that are responsible
7869 * for arranging their children.
7870 *
Romain Guy01c174b2011-02-22 11:51:06 -08007871 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007872 */
7873 public void setLayoutParams(ViewGroup.LayoutParams params) {
7874 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007875 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007876 }
7877 mLayoutParams = params;
7878 requestLayout();
7879 }
7880
7881 /**
7882 * Set the scrolled position of your view. This will cause a call to
7883 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7884 * invalidated.
7885 * @param x the x position to scroll to
7886 * @param y the y position to scroll to
7887 */
7888 public void scrollTo(int x, int y) {
7889 if (mScrollX != x || mScrollY != y) {
7890 int oldX = mScrollX;
7891 int oldY = mScrollY;
7892 mScrollX = x;
7893 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007894 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007895 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007896 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007897 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007899 }
7900 }
7901
7902 /**
7903 * Move the scrolled position of your view. This will cause a call to
7904 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7905 * invalidated.
7906 * @param x the amount of pixels to scroll by horizontally
7907 * @param y the amount of pixels to scroll by vertically
7908 */
7909 public void scrollBy(int x, int y) {
7910 scrollTo(mScrollX + x, mScrollY + y);
7911 }
7912
7913 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007914 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7915 * animation to fade the scrollbars out after a default delay. If a subclass
7916 * provides animated scrolling, the start delay should equal the duration
7917 * of the scrolling animation.</p>
7918 *
7919 * <p>The animation starts only if at least one of the scrollbars is
7920 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7921 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7922 * this method returns true, and false otherwise. If the animation is
7923 * started, this method calls {@link #invalidate()}; in that case the
7924 * caller should not call {@link #invalidate()}.</p>
7925 *
7926 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007927 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007928 *
7929 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7930 * and {@link #scrollTo(int, int)}.</p>
7931 *
7932 * @return true if the animation is played, false otherwise
7933 *
7934 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007935 * @see #scrollBy(int, int)
7936 * @see #scrollTo(int, int)
7937 * @see #isHorizontalScrollBarEnabled()
7938 * @see #isVerticalScrollBarEnabled()
7939 * @see #setHorizontalScrollBarEnabled(boolean)
7940 * @see #setVerticalScrollBarEnabled(boolean)
7941 */
7942 protected boolean awakenScrollBars() {
7943 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007944 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007945 }
7946
7947 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007948 * Trigger the scrollbars to draw.
7949 * This method differs from awakenScrollBars() only in its default duration.
7950 * initialAwakenScrollBars() will show the scroll bars for longer than
7951 * usual to give the user more of a chance to notice them.
7952 *
7953 * @return true if the animation is played, false otherwise.
7954 */
7955 private boolean initialAwakenScrollBars() {
7956 return mScrollCache != null &&
7957 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7958 }
7959
7960 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007961 * <p>
7962 * Trigger the scrollbars to draw. When invoked this method starts an
7963 * animation to fade the scrollbars out after a fixed delay. If a subclass
7964 * provides animated scrolling, the start delay should equal the duration of
7965 * the scrolling animation.
7966 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007967 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007968 * <p>
7969 * The animation starts only if at least one of the scrollbars is enabled,
7970 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7971 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7972 * this method returns true, and false otherwise. If the animation is
7973 * started, this method calls {@link #invalidate()}; in that case the caller
7974 * should not call {@link #invalidate()}.
7975 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007976 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007977 * <p>
7978 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007979 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007980 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007981 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007982 * @param startDelay the delay, in milliseconds, after which the animation
7983 * should start; when the delay is 0, the animation starts
7984 * immediately
7985 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007986 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007987 * @see #scrollBy(int, int)
7988 * @see #scrollTo(int, int)
7989 * @see #isHorizontalScrollBarEnabled()
7990 * @see #isVerticalScrollBarEnabled()
7991 * @see #setHorizontalScrollBarEnabled(boolean)
7992 * @see #setVerticalScrollBarEnabled(boolean)
7993 */
7994 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07007995 return awakenScrollBars(startDelay, true);
7996 }
Joe Malin32736f02011-01-19 16:14:20 -08007997
Mike Cleron290947b2009-09-29 18:34:32 -07007998 /**
7999 * <p>
8000 * Trigger the scrollbars to draw. When invoked this method starts an
8001 * animation to fade the scrollbars out after a fixed delay. If a subclass
8002 * provides animated scrolling, the start delay should equal the duration of
8003 * the scrolling animation.
8004 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008005 *
Mike Cleron290947b2009-09-29 18:34:32 -07008006 * <p>
8007 * The animation starts only if at least one of the scrollbars is enabled,
8008 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8009 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8010 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008011 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008012 * is set to true; in that case the caller
8013 * should not call {@link #invalidate()}.
8014 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008015 *
Mike Cleron290947b2009-09-29 18:34:32 -07008016 * <p>
8017 * This method should be invoked everytime a subclass directly updates the
8018 * scroll parameters.
8019 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008020 *
Mike Cleron290947b2009-09-29 18:34:32 -07008021 * @param startDelay the delay, in milliseconds, after which the animation
8022 * should start; when the delay is 0, the animation starts
8023 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008024 *
Mike Cleron290947b2009-09-29 18:34:32 -07008025 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008026 *
Mike Cleron290947b2009-09-29 18:34:32 -07008027 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008028 *
Mike Cleron290947b2009-09-29 18:34:32 -07008029 * @see #scrollBy(int, int)
8030 * @see #scrollTo(int, int)
8031 * @see #isHorizontalScrollBarEnabled()
8032 * @see #isVerticalScrollBarEnabled()
8033 * @see #setHorizontalScrollBarEnabled(boolean)
8034 * @see #setVerticalScrollBarEnabled(boolean)
8035 */
8036 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008037 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008038
Mike Cleronf116bf82009-09-27 19:14:12 -07008039 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8040 return false;
8041 }
8042
8043 if (scrollCache.scrollBar == null) {
8044 scrollCache.scrollBar = new ScrollBarDrawable();
8045 }
8046
8047 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8048
Mike Cleron290947b2009-09-29 18:34:32 -07008049 if (invalidate) {
8050 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008051 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008052 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008053
8054 if (scrollCache.state == ScrollabilityCache.OFF) {
8055 // FIXME: this is copied from WindowManagerService.
8056 // We should get this value from the system when it
8057 // is possible to do so.
8058 final int KEY_REPEAT_FIRST_DELAY = 750;
8059 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8060 }
8061
8062 // Tell mScrollCache when we should start fading. This may
8063 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008064 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008065 scrollCache.fadeStartTime = fadeStartTime;
8066 scrollCache.state = ScrollabilityCache.ON;
8067
8068 // Schedule our fader to run, unscheduling any old ones first
8069 if (mAttachInfo != null) {
8070 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8071 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8072 }
8073
8074 return true;
8075 }
8076
8077 return false;
8078 }
8079
8080 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008081 * Do not invalidate views which are not visible and which are not running an animation. They
8082 * will not get drawn and they should not set dirty flags as if they will be drawn
8083 */
8084 private boolean skipInvalidate() {
8085 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8086 (!(mParent instanceof ViewGroup) ||
8087 !((ViewGroup) mParent).isViewTransitioning(this));
8088 }
8089 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008090 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008091 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8092 * in the future. This must be called from a UI thread. To call from a non-UI
8093 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008094 *
8095 * WARNING: This method is destructive to dirty.
8096 * @param dirty the rectangle representing the bounds of the dirty region
8097 */
8098 public void invalidate(Rect dirty) {
8099 if (ViewDebug.TRACE_HIERARCHY) {
8100 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8101 }
8102
Chet Haaseaceafe62011-08-26 15:44:33 -07008103 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008104 return;
8105 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008106 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008107 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8108 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008109 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008110 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008111 final ViewParent p = mParent;
8112 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008113 //noinspection PointlessBooleanExpression,ConstantConditions
8114 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8115 if (p != null && ai != null && ai.mHardwareAccelerated) {
8116 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008117 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008118 p.invalidateChild(this, null);
8119 return;
8120 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008121 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008122 if (p != null && ai != null) {
8123 final int scrollX = mScrollX;
8124 final int scrollY = mScrollY;
8125 final Rect r = ai.mTmpInvalRect;
8126 r.set(dirty.left - scrollX, dirty.top - scrollY,
8127 dirty.right - scrollX, dirty.bottom - scrollY);
8128 mParent.invalidateChild(this, r);
8129 }
8130 }
8131 }
8132
8133 /**
8134 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8135 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008136 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8137 * will be called at some point in the future. This must be called from
8138 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008139 * @param l the left position of the dirty region
8140 * @param t the top position of the dirty region
8141 * @param r the right position of the dirty region
8142 * @param b the bottom position of the dirty region
8143 */
8144 public void invalidate(int l, int t, int r, int b) {
8145 if (ViewDebug.TRACE_HIERARCHY) {
8146 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8147 }
8148
Chet Haaseaceafe62011-08-26 15:44:33 -07008149 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008150 return;
8151 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008152 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008153 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8154 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008155 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008156 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008157 final ViewParent p = mParent;
8158 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008159 //noinspection PointlessBooleanExpression,ConstantConditions
8160 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8161 if (p != null && ai != null && ai.mHardwareAccelerated) {
8162 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008163 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008164 p.invalidateChild(this, null);
8165 return;
8166 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008168 if (p != null && ai != null && l < r && t < b) {
8169 final int scrollX = mScrollX;
8170 final int scrollY = mScrollY;
8171 final Rect tmpr = ai.mTmpInvalRect;
8172 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8173 p.invalidateChild(this, tmpr);
8174 }
8175 }
8176 }
8177
8178 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008179 * Invalidate the whole view. If the view is visible,
8180 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8181 * the future. This must be called from a UI thread. To call from a non-UI thread,
8182 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008183 */
8184 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008185 invalidate(true);
8186 }
Joe Malin32736f02011-01-19 16:14:20 -08008187
Chet Haaseed032702010-10-01 14:05:54 -07008188 /**
8189 * This is where the invalidate() work actually happens. A full invalidate()
8190 * causes the drawing cache to be invalidated, but this function can be called with
8191 * invalidateCache set to false to skip that invalidation step for cases that do not
8192 * need it (for example, a component that remains at the same dimensions with the same
8193 * content).
8194 *
8195 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8196 * well. This is usually true for a full invalidate, but may be set to false if the
8197 * View's contents or dimensions have not changed.
8198 */
Romain Guy849d0a32011-02-01 17:20:48 -08008199 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008200 if (ViewDebug.TRACE_HIERARCHY) {
8201 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8202 }
8203
Chet Haaseaceafe62011-08-26 15:44:33 -07008204 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008205 return;
8206 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008207 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008208 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008209 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8210 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008211 mPrivateFlags &= ~DRAWN;
8212 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008213 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008214 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8215 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008216 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008217 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008218 //noinspection PointlessBooleanExpression,ConstantConditions
8219 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8220 if (p != null && ai != null && ai.mHardwareAccelerated) {
8221 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008222 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008223 p.invalidateChild(this, null);
8224 return;
8225 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008226 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008227
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008228 if (p != null && ai != null) {
8229 final Rect r = ai.mTmpInvalRect;
8230 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8231 // Don't call invalidate -- we don't want to internally scroll
8232 // our own bounds
8233 p.invalidateChild(this, r);
8234 }
8235 }
8236 }
8237
8238 /**
Romain Guyda489792011-02-03 01:05:15 -08008239 * @hide
8240 */
8241 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008242 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008243 return;
8244 }
Romain Guyda489792011-02-03 01:05:15 -08008245 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8246 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8247 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8248 if (mParent instanceof View) {
8249 ((View) mParent).mPrivateFlags |= INVALIDATED;
8250 }
8251 mPrivateFlags &= ~DRAWN;
8252 mPrivateFlags |= INVALIDATED;
8253 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008254 if (mParent != null && mAttachInfo != null) {
8255 if (mAttachInfo.mHardwareAccelerated) {
8256 mParent.invalidateChild(this, null);
8257 } else {
8258 final Rect r = mAttachInfo.mTmpInvalRect;
8259 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8260 // Don't call invalidate -- we don't want to internally scroll
8261 // our own bounds
8262 mParent.invalidateChild(this, r);
8263 }
Romain Guyda489792011-02-03 01:05:15 -08008264 }
8265 }
8266 }
8267
8268 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008269 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008270 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8271 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008272 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8273 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008274 *
8275 * @hide
8276 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008277 protected void invalidateParentCaches() {
8278 if (mParent instanceof View) {
8279 ((View) mParent).mPrivateFlags |= INVALIDATED;
8280 }
8281 }
Joe Malin32736f02011-01-19 16:14:20 -08008282
Romain Guy0fd89bf2011-01-26 15:41:30 -08008283 /**
8284 * Used to indicate that the parent of this view should be invalidated. This functionality
8285 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8286 * which is necessary when various parent-managed properties of the view change, such as
8287 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8288 * an invalidation event to the parent.
8289 *
8290 * @hide
8291 */
8292 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008293 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008294 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008295 }
8296 }
8297
8298 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008299 * Indicates whether this View is opaque. An opaque View guarantees that it will
8300 * draw all the pixels overlapping its bounds using a fully opaque color.
8301 *
8302 * Subclasses of View should override this method whenever possible to indicate
8303 * whether an instance is opaque. Opaque Views are treated in a special way by
8304 * the View hierarchy, possibly allowing it to perform optimizations during
8305 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008306 *
Romain Guy24443ea2009-05-11 11:56:30 -07008307 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008308 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008309 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008310 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008311 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8312 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008313 }
8314
Adam Powell20232d02010-12-08 21:08:53 -08008315 /**
8316 * @hide
8317 */
8318 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008319 // Opaque if:
8320 // - Has a background
8321 // - Background is opaque
8322 // - Doesn't have scrollbars or scrollbars are inside overlay
8323
8324 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8325 mPrivateFlags |= OPAQUE_BACKGROUND;
8326 } else {
8327 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8328 }
8329
8330 final int flags = mViewFlags;
8331 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8332 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8333 mPrivateFlags |= OPAQUE_SCROLLBARS;
8334 } else {
8335 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8336 }
8337 }
8338
8339 /**
8340 * @hide
8341 */
8342 protected boolean hasOpaqueScrollbars() {
8343 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008344 }
8345
8346 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008347 * @return A handler associated with the thread running the View. This
8348 * handler can be used to pump events in the UI events queue.
8349 */
8350 public Handler getHandler() {
8351 if (mAttachInfo != null) {
8352 return mAttachInfo.mHandler;
8353 }
8354 return null;
8355 }
8356
8357 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008358 * <p>Causes the Runnable to be added to the message queue.
8359 * The runnable will be run on the user interface thread.</p>
8360 *
8361 * <p>This method can be invoked from outside of the UI thread
8362 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008363 *
8364 * @param action The Runnable that will be executed.
8365 *
8366 * @return Returns true if the Runnable was successfully placed in to the
8367 * message queue. Returns false on failure, usually because the
8368 * looper processing the message queue is exiting.
8369 */
8370 public boolean post(Runnable action) {
8371 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008372 AttachInfo attachInfo = mAttachInfo;
8373 if (attachInfo != null) {
8374 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008375 } else {
8376 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008377 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008378 return true;
8379 }
8380
8381 return handler.post(action);
8382 }
8383
8384 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008385 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008386 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008387 * The runnable will be run on the user interface thread.</p>
8388 *
8389 * <p>This method can be invoked from outside of the UI thread
8390 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008391 *
8392 * @param action The Runnable that will be executed.
8393 * @param delayMillis The delay (in milliseconds) until the Runnable
8394 * will be executed.
8395 *
8396 * @return true if the Runnable was successfully placed in to the
8397 * message queue. Returns false on failure, usually because the
8398 * looper processing the message queue is exiting. Note that a
8399 * result of true does not mean the Runnable will be processed --
8400 * if the looper is quit before the delivery time of the message
8401 * occurs then the message will be dropped.
8402 */
8403 public boolean postDelayed(Runnable action, long delayMillis) {
8404 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008405 AttachInfo attachInfo = mAttachInfo;
8406 if (attachInfo != null) {
8407 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008408 } else {
8409 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008410 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008411 return true;
8412 }
8413
8414 return handler.postDelayed(action, delayMillis);
8415 }
8416
8417 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008418 * <p>Removes the specified Runnable from the message queue.</p>
8419 *
8420 * <p>This method can be invoked from outside of the UI thread
8421 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008422 *
8423 * @param action The Runnable to remove from the message handling queue
8424 *
8425 * @return true if this view could ask the Handler to remove the Runnable,
8426 * false otherwise. When the returned value is true, the Runnable
8427 * may or may not have been actually removed from the message queue
8428 * (for instance, if the Runnable was not in the queue already.)
8429 */
8430 public boolean removeCallbacks(Runnable action) {
8431 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008432 AttachInfo attachInfo = mAttachInfo;
8433 if (attachInfo != null) {
8434 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008435 } else {
8436 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008437 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008438 return true;
8439 }
8440
8441 handler.removeCallbacks(action);
8442 return true;
8443 }
8444
8445 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008446 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8447 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008448 *
Romain Guye63a4f32011-08-11 11:33:31 -07008449 * <p>This method can be invoked from outside of the UI thread
8450 * only when this View is attached to a window.</p>
8451 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008452 * @see #invalidate()
8453 */
8454 public void postInvalidate() {
8455 postInvalidateDelayed(0);
8456 }
8457
8458 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008459 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8460 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8461 *
8462 * <p>This method can be invoked from outside of the UI thread
8463 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008464 *
8465 * @param left The left coordinate of the rectangle to invalidate.
8466 * @param top The top coordinate of the rectangle to invalidate.
8467 * @param right The right coordinate of the rectangle to invalidate.
8468 * @param bottom The bottom coordinate of the rectangle to invalidate.
8469 *
8470 * @see #invalidate(int, int, int, int)
8471 * @see #invalidate(Rect)
8472 */
8473 public void postInvalidate(int left, int top, int right, int bottom) {
8474 postInvalidateDelayed(0, left, top, right, bottom);
8475 }
8476
8477 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008478 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8479 * loop. Waits for the specified amount of time.</p>
8480 *
8481 * <p>This method can be invoked from outside of the UI thread
8482 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008483 *
8484 * @param delayMilliseconds the duration in milliseconds to delay the
8485 * invalidation by
8486 */
8487 public void postInvalidateDelayed(long delayMilliseconds) {
8488 // We try only with the AttachInfo because there's no point in invalidating
8489 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008490 AttachInfo attachInfo = mAttachInfo;
8491 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008492 Message msg = Message.obtain();
8493 msg.what = AttachInfo.INVALIDATE_MSG;
8494 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008495 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008496 }
8497 }
8498
8499 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008500 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8501 * through the event loop. Waits for the specified amount of time.</p>
8502 *
8503 * <p>This method can be invoked from outside of the UI thread
8504 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008505 *
8506 * @param delayMilliseconds the duration in milliseconds to delay the
8507 * invalidation by
8508 * @param left The left coordinate of the rectangle to invalidate.
8509 * @param top The top coordinate of the rectangle to invalidate.
8510 * @param right The right coordinate of the rectangle to invalidate.
8511 * @param bottom The bottom coordinate of the rectangle to invalidate.
8512 */
8513 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8514 int right, int bottom) {
8515
8516 // We try only with the AttachInfo because there's no point in invalidating
8517 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008518 AttachInfo attachInfo = mAttachInfo;
8519 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008520 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8521 info.target = this;
8522 info.left = left;
8523 info.top = top;
8524 info.right = right;
8525 info.bottom = bottom;
8526
8527 final Message msg = Message.obtain();
8528 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8529 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008530 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008531 }
8532 }
8533
8534 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008535 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8536 * This event is sent at most once every
8537 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8538 */
8539 private void postSendViewScrolledAccessibilityEventCallback() {
8540 if (mSendViewScrolledAccessibilityEvent == null) {
8541 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8542 }
8543 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8544 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8545 postDelayed(mSendViewScrolledAccessibilityEvent,
8546 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8547 }
8548 }
8549
8550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008551 * Called by a parent to request that a child update its values for mScrollX
8552 * and mScrollY if necessary. This will typically be done if the child is
8553 * animating a scroll using a {@link android.widget.Scroller Scroller}
8554 * object.
8555 */
8556 public void computeScroll() {
8557 }
8558
8559 /**
8560 * <p>Indicate whether the horizontal edges are faded when the view is
8561 * scrolled horizontally.</p>
8562 *
8563 * @return true if the horizontal edges should are faded on scroll, false
8564 * otherwise
8565 *
8566 * @see #setHorizontalFadingEdgeEnabled(boolean)
8567 * @attr ref android.R.styleable#View_fadingEdge
8568 */
8569 public boolean isHorizontalFadingEdgeEnabled() {
8570 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8571 }
8572
8573 /**
8574 * <p>Define whether the horizontal edges should be faded when this view
8575 * is scrolled horizontally.</p>
8576 *
8577 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8578 * be faded when the view is scrolled
8579 * horizontally
8580 *
8581 * @see #isHorizontalFadingEdgeEnabled()
8582 * @attr ref android.R.styleable#View_fadingEdge
8583 */
8584 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8585 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8586 if (horizontalFadingEdgeEnabled) {
8587 initScrollCache();
8588 }
8589
8590 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8591 }
8592 }
8593
8594 /**
8595 * <p>Indicate whether the vertical edges are faded when the view is
8596 * scrolled horizontally.</p>
8597 *
8598 * @return true if the vertical edges should are faded on scroll, false
8599 * otherwise
8600 *
8601 * @see #setVerticalFadingEdgeEnabled(boolean)
8602 * @attr ref android.R.styleable#View_fadingEdge
8603 */
8604 public boolean isVerticalFadingEdgeEnabled() {
8605 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8606 }
8607
8608 /**
8609 * <p>Define whether the vertical edges should be faded when this view
8610 * is scrolled vertically.</p>
8611 *
8612 * @param verticalFadingEdgeEnabled true if the vertical edges should
8613 * be faded when the view is scrolled
8614 * vertically
8615 *
8616 * @see #isVerticalFadingEdgeEnabled()
8617 * @attr ref android.R.styleable#View_fadingEdge
8618 */
8619 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8620 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8621 if (verticalFadingEdgeEnabled) {
8622 initScrollCache();
8623 }
8624
8625 mViewFlags ^= FADING_EDGE_VERTICAL;
8626 }
8627 }
8628
8629 /**
8630 * Returns the strength, or intensity, of the top faded edge. The strength is
8631 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8632 * returns 0.0 or 1.0 but no value in between.
8633 *
8634 * Subclasses should override this method to provide a smoother fade transition
8635 * when scrolling occurs.
8636 *
8637 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8638 */
8639 protected float getTopFadingEdgeStrength() {
8640 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8641 }
8642
8643 /**
8644 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8645 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8646 * returns 0.0 or 1.0 but no value in between.
8647 *
8648 * Subclasses should override this method to provide a smoother fade transition
8649 * when scrolling occurs.
8650 *
8651 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8652 */
8653 protected float getBottomFadingEdgeStrength() {
8654 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8655 computeVerticalScrollRange() ? 1.0f : 0.0f;
8656 }
8657
8658 /**
8659 * Returns the strength, or intensity, of the left faded edge. The strength is
8660 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8661 * returns 0.0 or 1.0 but no value in between.
8662 *
8663 * Subclasses should override this method to provide a smoother fade transition
8664 * when scrolling occurs.
8665 *
8666 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8667 */
8668 protected float getLeftFadingEdgeStrength() {
8669 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8670 }
8671
8672 /**
8673 * Returns the strength, or intensity, of the right faded edge. The strength is
8674 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8675 * returns 0.0 or 1.0 but no value in between.
8676 *
8677 * Subclasses should override this method to provide a smoother fade transition
8678 * when scrolling occurs.
8679 *
8680 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8681 */
8682 protected float getRightFadingEdgeStrength() {
8683 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8684 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8685 }
8686
8687 /**
8688 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8689 * scrollbar is not drawn by default.</p>
8690 *
8691 * @return true if the horizontal scrollbar should be painted, false
8692 * otherwise
8693 *
8694 * @see #setHorizontalScrollBarEnabled(boolean)
8695 */
8696 public boolean isHorizontalScrollBarEnabled() {
8697 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8698 }
8699
8700 /**
8701 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8702 * scrollbar is not drawn by default.</p>
8703 *
8704 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8705 * be painted
8706 *
8707 * @see #isHorizontalScrollBarEnabled()
8708 */
8709 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8710 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8711 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008712 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008713 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008714 }
8715 }
8716
8717 /**
8718 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8719 * scrollbar is not drawn by default.</p>
8720 *
8721 * @return true if the vertical scrollbar should be painted, false
8722 * otherwise
8723 *
8724 * @see #setVerticalScrollBarEnabled(boolean)
8725 */
8726 public boolean isVerticalScrollBarEnabled() {
8727 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8728 }
8729
8730 /**
8731 * <p>Define whether the vertical scrollbar should be drawn or not. The
8732 * scrollbar is not drawn by default.</p>
8733 *
8734 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8735 * be painted
8736 *
8737 * @see #isVerticalScrollBarEnabled()
8738 */
8739 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8740 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8741 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008742 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008743 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008744 }
8745 }
8746
Adam Powell20232d02010-12-08 21:08:53 -08008747 /**
8748 * @hide
8749 */
8750 protected void recomputePadding() {
8751 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008752 }
Joe Malin32736f02011-01-19 16:14:20 -08008753
Mike Cleronfe81d382009-09-28 14:22:16 -07008754 /**
8755 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008756 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008757 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008758 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008759 */
8760 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8761 initScrollCache();
8762 final ScrollabilityCache scrollabilityCache = mScrollCache;
8763 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008764 if (fadeScrollbars) {
8765 scrollabilityCache.state = ScrollabilityCache.OFF;
8766 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008767 scrollabilityCache.state = ScrollabilityCache.ON;
8768 }
8769 }
Joe Malin32736f02011-01-19 16:14:20 -08008770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008771 /**
Joe Malin32736f02011-01-19 16:14:20 -08008772 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008773 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008774 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008775 * @return true if scrollbar fading is enabled
8776 */
8777 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008778 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008779 }
Joe Malin32736f02011-01-19 16:14:20 -08008780
Mike Cleron52f0a642009-09-28 18:21:37 -07008781 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008782 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8783 * inset. When inset, they add to the padding of the view. And the scrollbars
8784 * can be drawn inside the padding area or on the edge of the view. For example,
8785 * if a view has a background drawable and you want to draw the scrollbars
8786 * inside the padding specified by the drawable, you can use
8787 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8788 * appear at the edge of the view, ignoring the padding, then you can use
8789 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8790 * @param style the style of the scrollbars. Should be one of
8791 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8792 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8793 * @see #SCROLLBARS_INSIDE_OVERLAY
8794 * @see #SCROLLBARS_INSIDE_INSET
8795 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8796 * @see #SCROLLBARS_OUTSIDE_INSET
8797 */
8798 public void setScrollBarStyle(int style) {
8799 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8800 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008801 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008802 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008803 }
8804 }
8805
8806 /**
8807 * <p>Returns the current scrollbar style.</p>
8808 * @return the current scrollbar style
8809 * @see #SCROLLBARS_INSIDE_OVERLAY
8810 * @see #SCROLLBARS_INSIDE_INSET
8811 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8812 * @see #SCROLLBARS_OUTSIDE_INSET
8813 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07008814 @ViewDebug.ExportedProperty(mapping = {
8815 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
8816 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
8817 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
8818 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
8819 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008820 public int getScrollBarStyle() {
8821 return mViewFlags & SCROLLBARS_STYLE_MASK;
8822 }
8823
8824 /**
8825 * <p>Compute the horizontal range that the horizontal scrollbar
8826 * represents.</p>
8827 *
8828 * <p>The range is expressed in arbitrary units that must be the same as the
8829 * units used by {@link #computeHorizontalScrollExtent()} and
8830 * {@link #computeHorizontalScrollOffset()}.</p>
8831 *
8832 * <p>The default range is the drawing width of this view.</p>
8833 *
8834 * @return the total horizontal range represented by the horizontal
8835 * scrollbar
8836 *
8837 * @see #computeHorizontalScrollExtent()
8838 * @see #computeHorizontalScrollOffset()
8839 * @see android.widget.ScrollBarDrawable
8840 */
8841 protected int computeHorizontalScrollRange() {
8842 return getWidth();
8843 }
8844
8845 /**
8846 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8847 * within the horizontal range. This value is used to compute the position
8848 * of the thumb within the scrollbar's track.</p>
8849 *
8850 * <p>The range is expressed in arbitrary units that must be the same as the
8851 * units used by {@link #computeHorizontalScrollRange()} and
8852 * {@link #computeHorizontalScrollExtent()}.</p>
8853 *
8854 * <p>The default offset is the scroll offset of this view.</p>
8855 *
8856 * @return the horizontal offset of the scrollbar's thumb
8857 *
8858 * @see #computeHorizontalScrollRange()
8859 * @see #computeHorizontalScrollExtent()
8860 * @see android.widget.ScrollBarDrawable
8861 */
8862 protected int computeHorizontalScrollOffset() {
8863 return mScrollX;
8864 }
8865
8866 /**
8867 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8868 * within the horizontal range. This value is used to compute the length
8869 * of the thumb within the scrollbar's track.</p>
8870 *
8871 * <p>The range is expressed in arbitrary units that must be the same as the
8872 * units used by {@link #computeHorizontalScrollRange()} and
8873 * {@link #computeHorizontalScrollOffset()}.</p>
8874 *
8875 * <p>The default extent is the drawing width of this view.</p>
8876 *
8877 * @return the horizontal extent of the scrollbar's thumb
8878 *
8879 * @see #computeHorizontalScrollRange()
8880 * @see #computeHorizontalScrollOffset()
8881 * @see android.widget.ScrollBarDrawable
8882 */
8883 protected int computeHorizontalScrollExtent() {
8884 return getWidth();
8885 }
8886
8887 /**
8888 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8889 *
8890 * <p>The range is expressed in arbitrary units that must be the same as the
8891 * units used by {@link #computeVerticalScrollExtent()} and
8892 * {@link #computeVerticalScrollOffset()}.</p>
8893 *
8894 * @return the total vertical range represented by the vertical scrollbar
8895 *
8896 * <p>The default range is the drawing height of this view.</p>
8897 *
8898 * @see #computeVerticalScrollExtent()
8899 * @see #computeVerticalScrollOffset()
8900 * @see android.widget.ScrollBarDrawable
8901 */
8902 protected int computeVerticalScrollRange() {
8903 return getHeight();
8904 }
8905
8906 /**
8907 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8908 * within the horizontal range. This value is used to compute the position
8909 * of the thumb within the scrollbar's track.</p>
8910 *
8911 * <p>The range is expressed in arbitrary units that must be the same as the
8912 * units used by {@link #computeVerticalScrollRange()} and
8913 * {@link #computeVerticalScrollExtent()}.</p>
8914 *
8915 * <p>The default offset is the scroll offset of this view.</p>
8916 *
8917 * @return the vertical offset of the scrollbar's thumb
8918 *
8919 * @see #computeVerticalScrollRange()
8920 * @see #computeVerticalScrollExtent()
8921 * @see android.widget.ScrollBarDrawable
8922 */
8923 protected int computeVerticalScrollOffset() {
8924 return mScrollY;
8925 }
8926
8927 /**
8928 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8929 * within the vertical range. This value is used to compute the length
8930 * of the thumb within the scrollbar's track.</p>
8931 *
8932 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008933 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008934 * {@link #computeVerticalScrollOffset()}.</p>
8935 *
8936 * <p>The default extent is the drawing height of this view.</p>
8937 *
8938 * @return the vertical extent of the scrollbar's thumb
8939 *
8940 * @see #computeVerticalScrollRange()
8941 * @see #computeVerticalScrollOffset()
8942 * @see android.widget.ScrollBarDrawable
8943 */
8944 protected int computeVerticalScrollExtent() {
8945 return getHeight();
8946 }
8947
8948 /**
Adam Powell69159442011-06-13 17:53:06 -07008949 * Check if this view can be scrolled horizontally in a certain direction.
8950 *
8951 * @param direction Negative to check scrolling left, positive to check scrolling right.
8952 * @return true if this view can be scrolled in the specified direction, false otherwise.
8953 */
8954 public boolean canScrollHorizontally(int direction) {
8955 final int offset = computeHorizontalScrollOffset();
8956 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8957 if (range == 0) return false;
8958 if (direction < 0) {
8959 return offset > 0;
8960 } else {
8961 return offset < range - 1;
8962 }
8963 }
8964
8965 /**
8966 * Check if this view can be scrolled vertically in a certain direction.
8967 *
8968 * @param direction Negative to check scrolling up, positive to check scrolling down.
8969 * @return true if this view can be scrolled in the specified direction, false otherwise.
8970 */
8971 public boolean canScrollVertically(int direction) {
8972 final int offset = computeVerticalScrollOffset();
8973 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8974 if (range == 0) return false;
8975 if (direction < 0) {
8976 return offset > 0;
8977 } else {
8978 return offset < range - 1;
8979 }
8980 }
8981
8982 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008983 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8984 * scrollbars are painted only if they have been awakened first.</p>
8985 *
8986 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008987 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008988 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008990 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008991 // scrollbars are drawn only when the animation is running
8992 final ScrollabilityCache cache = mScrollCache;
8993 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08008994
Mike Cleronf116bf82009-09-27 19:14:12 -07008995 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08008996
Mike Cleronf116bf82009-09-27 19:14:12 -07008997 if (state == ScrollabilityCache.OFF) {
8998 return;
8999 }
Joe Malin32736f02011-01-19 16:14:20 -08009000
Mike Cleronf116bf82009-09-27 19:14:12 -07009001 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009002
Mike Cleronf116bf82009-09-27 19:14:12 -07009003 if (state == ScrollabilityCache.FADING) {
9004 // We're fading -- get our fade interpolation
9005 if (cache.interpolatorValues == null) {
9006 cache.interpolatorValues = new float[1];
9007 }
Joe Malin32736f02011-01-19 16:14:20 -08009008
Mike Cleronf116bf82009-09-27 19:14:12 -07009009 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009010
Mike Cleronf116bf82009-09-27 19:14:12 -07009011 // Stops the animation if we're done
9012 if (cache.scrollBarInterpolator.timeToValues(values) ==
9013 Interpolator.Result.FREEZE_END) {
9014 cache.state = ScrollabilityCache.OFF;
9015 } else {
9016 cache.scrollBar.setAlpha(Math.round(values[0]));
9017 }
Joe Malin32736f02011-01-19 16:14:20 -08009018
9019 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009020 // drawing. We only want this when we're fading so that
9021 // we prevent excessive redraws
9022 invalidate = true;
9023 } else {
9024 // We're just on -- but we may have been fading before so
9025 // reset alpha
9026 cache.scrollBar.setAlpha(255);
9027 }
9028
Joe Malin32736f02011-01-19 16:14:20 -08009029
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009030 final int viewFlags = mViewFlags;
9031
9032 final boolean drawHorizontalScrollBar =
9033 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9034 final boolean drawVerticalScrollBar =
9035 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9036 && !isVerticalScrollBarHidden();
9037
9038 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9039 final int width = mRight - mLeft;
9040 final int height = mBottom - mTop;
9041
9042 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009043
Mike Reede8853fc2009-09-04 14:01:48 -04009044 final int scrollX = mScrollX;
9045 final int scrollY = mScrollY;
9046 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9047
Mike Cleronf116bf82009-09-27 19:14:12 -07009048 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009049
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009050 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009051 int size = scrollBar.getSize(false);
9052 if (size <= 0) {
9053 size = cache.scrollBarSize;
9054 }
9055
Mike Cleronf116bf82009-09-27 19:14:12 -07009056 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009057 computeHorizontalScrollOffset(),
9058 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009059 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009060 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009061 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009062 left = scrollX + (mPaddingLeft & inside);
9063 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9064 bottom = top + size;
9065 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9066 if (invalidate) {
9067 invalidate(left, top, right, bottom);
9068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009069 }
9070
9071 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009072 int size = scrollBar.getSize(true);
9073 if (size <= 0) {
9074 size = cache.scrollBarSize;
9075 }
9076
Mike Reede8853fc2009-09-04 14:01:48 -04009077 scrollBar.setParameters(computeVerticalScrollRange(),
9078 computeVerticalScrollOffset(),
9079 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009080 switch (mVerticalScrollbarPosition) {
9081 default:
9082 case SCROLLBAR_POSITION_DEFAULT:
9083 case SCROLLBAR_POSITION_RIGHT:
9084 left = scrollX + width - size - (mUserPaddingRight & inside);
9085 break;
9086 case SCROLLBAR_POSITION_LEFT:
9087 left = scrollX + (mUserPaddingLeft & inside);
9088 break;
9089 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009090 top = scrollY + (mPaddingTop & inside);
9091 right = left + size;
9092 bottom = scrollY + height - (mUserPaddingBottom & inside);
9093 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9094 if (invalidate) {
9095 invalidate(left, top, right, bottom);
9096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009097 }
9098 }
9099 }
9100 }
Romain Guy8506ab42009-06-11 17:35:47 -07009101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009102 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009103 * 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 -08009104 * FastScroller is visible.
9105 * @return whether to temporarily hide the vertical scrollbar
9106 * @hide
9107 */
9108 protected boolean isVerticalScrollBarHidden() {
9109 return false;
9110 }
9111
9112 /**
9113 * <p>Draw the horizontal scrollbar if
9114 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9115 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009116 * @param canvas the canvas on which to draw the scrollbar
9117 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009118 *
9119 * @see #isHorizontalScrollBarEnabled()
9120 * @see #computeHorizontalScrollRange()
9121 * @see #computeHorizontalScrollExtent()
9122 * @see #computeHorizontalScrollOffset()
9123 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009124 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009125 */
Romain Guy8fb95422010-08-17 18:38:51 -07009126 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9127 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009128 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009129 scrollBar.draw(canvas);
9130 }
Mike Reede8853fc2009-09-04 14:01:48 -04009131
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009132 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009133 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9134 * returns true.</p>
9135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009136 * @param canvas the canvas on which to draw the scrollbar
9137 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009138 *
9139 * @see #isVerticalScrollBarEnabled()
9140 * @see #computeVerticalScrollRange()
9141 * @see #computeVerticalScrollExtent()
9142 * @see #computeVerticalScrollOffset()
9143 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009144 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009145 */
Romain Guy8fb95422010-08-17 18:38:51 -07009146 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9147 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009148 scrollBar.setBounds(l, t, r, b);
9149 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009150 }
9151
9152 /**
9153 * Implement this to do your drawing.
9154 *
9155 * @param canvas the canvas on which the background will be drawn
9156 */
9157 protected void onDraw(Canvas canvas) {
9158 }
9159
9160 /*
9161 * Caller is responsible for calling requestLayout if necessary.
9162 * (This allows addViewInLayout to not request a new layout.)
9163 */
9164 void assignParent(ViewParent parent) {
9165 if (mParent == null) {
9166 mParent = parent;
9167 } else if (parent == null) {
9168 mParent = null;
9169 } else {
9170 throw new RuntimeException("view " + this + " being added, but"
9171 + " it already has a parent");
9172 }
9173 }
9174
9175 /**
9176 * This is called when the view is attached to a window. At this point it
9177 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009178 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9179 * however it may be called any time before the first onDraw -- including
9180 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009181 *
9182 * @see #onDetachedFromWindow()
9183 */
9184 protected void onAttachedToWindow() {
9185 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9186 mParent.requestTransparentRegion(this);
9187 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009188 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9189 initialAwakenScrollBars();
9190 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9191 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009192 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009193 // Order is important here: LayoutDirection MUST be resolved before Padding
9194 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009195 resolveLayoutDirectionIfNeeded();
9196 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009197 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009198 if (isFocused()) {
9199 InputMethodManager imm = InputMethodManager.peekInstance();
9200 imm.focusIn(this);
9201 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009202 }
Cibu Johny86666632010-02-22 13:01:02 -08009203
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009204 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009205 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9206 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009207 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009208 private void resolveLayoutDirectionIfNeeded() {
9209 // Do not resolve if it is not needed
9210 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9211
9212 // Clear any previous layout direction resolution
9213 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9214
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009215 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9216 // TextDirectionHeuristic
9217 resetResolvedTextDirection();
9218
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009219 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009220 switch (getLayoutDirection()) {
9221 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009222 // We cannot do the resolution if there is no parent
9223 if (mParent == null) return;
9224
Cibu Johny86666632010-02-22 13:01:02 -08009225 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009226 if (mParent instanceof ViewGroup) {
9227 ViewGroup viewGroup = ((ViewGroup) mParent);
9228
9229 // Check if the parent view group can resolve
9230 if (! viewGroup.canResolveLayoutDirection()) {
9231 return;
9232 }
9233 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9234 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9235 }
Cibu Johny86666632010-02-22 13:01:02 -08009236 }
9237 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009238 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009239 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009240 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009241 case LAYOUT_DIRECTION_LOCALE:
9242 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009243 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009244 }
9245 break;
9246 default:
9247 // Nothing to do, LTR by default
9248 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009249
9250 // Set to resolved
9251 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9252 }
9253
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009254 /**
9255 * @hide
9256 */
9257 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009258 // If the user specified the absolute padding (either with android:padding or
9259 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9260 // use the default padding or the padding from the background drawable
9261 // (stored at this point in mPadding*)
9262 switch (getResolvedLayoutDirection()) {
9263 case LAYOUT_DIRECTION_RTL:
9264 // Start user padding override Right user padding. Otherwise, if Right user
9265 // padding is not defined, use the default Right padding. If Right user padding
9266 // is defined, just use it.
9267 if (mUserPaddingStart >= 0) {
9268 mUserPaddingRight = mUserPaddingStart;
9269 } else if (mUserPaddingRight < 0) {
9270 mUserPaddingRight = mPaddingRight;
9271 }
9272 if (mUserPaddingEnd >= 0) {
9273 mUserPaddingLeft = mUserPaddingEnd;
9274 } else if (mUserPaddingLeft < 0) {
9275 mUserPaddingLeft = mPaddingLeft;
9276 }
9277 break;
9278 case LAYOUT_DIRECTION_LTR:
9279 default:
9280 // Start user padding override Left user padding. Otherwise, if Left user
9281 // padding is not defined, use the default left padding. If Left user padding
9282 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009283 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009284 mUserPaddingLeft = mUserPaddingStart;
9285 } else if (mUserPaddingLeft < 0) {
9286 mUserPaddingLeft = mPaddingLeft;
9287 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009288 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009289 mUserPaddingRight = mUserPaddingEnd;
9290 } else if (mUserPaddingRight < 0) {
9291 mUserPaddingRight = mPaddingRight;
9292 }
9293 }
9294
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009295 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9296
9297 recomputePadding();
9298 }
9299
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009300 /**
9301 * Return true if layout direction resolution can be done
9302 *
9303 * @hide
9304 */
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009305 protected boolean canResolveLayoutDirection() {
9306 switch (getLayoutDirection()) {
9307 case LAYOUT_DIRECTION_INHERIT:
9308 return (mParent != null);
9309 default:
9310 return true;
9311 }
9312 }
9313
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009314 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009315 * Reset the resolved layout direction.
9316 *
9317 * Subclasses need to override this method to clear cached information that depends on the
9318 * resolved layout direction, or to inform child views that inherit their layout direction.
9319 * Overrides must also call the superclass implementation at the start of their implementation.
9320 *
9321 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009322 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009323 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009324 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009325 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009326 }
9327
9328 /**
9329 * Check if a Locale is corresponding to a RTL script.
9330 *
9331 * @param locale Locale to check
9332 * @return true if a Locale is corresponding to a RTL script.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -07009333 *
9334 * @hide
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009335 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009336 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009337 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9338 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009339 }
9340
9341 /**
9342 * This is called when the view is detached from a window. At this point it
9343 * no longer has a surface for drawing.
9344 *
9345 * @see #onAttachedToWindow()
9346 */
9347 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009348 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009349
Romain Guya440b002010-02-24 15:57:54 -08009350 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009351 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009352 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009353 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009354
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009355 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009356
Romain Guy6d7475d2011-07-27 16:28:21 -07009357 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009358
Chet Haasedaf98e92011-01-10 14:10:36 -08009359 if (mDisplayList != null) {
9360 mDisplayList.invalidate();
9361 }
9362
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009363 if (mAttachInfo != null) {
9364 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009365 }
9366
Patrick Dubroyec84c3a2011-01-13 17:55:37 -08009367 mCurrentAnimation = null;
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009368
9369 resetResolvedLayoutDirection();
9370 resetResolvedTextDirection();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009371 }
9372
9373 /**
9374 * @return The number of times this view has been attached to a window
9375 */
9376 protected int getWindowAttachCount() {
9377 return mWindowAttachCount;
9378 }
9379
9380 /**
9381 * Retrieve a unique token identifying the window this view is attached to.
9382 * @return Return the window's token for use in
9383 * {@link WindowManager.LayoutParams#token WindowManager.LayoutParams.token}.
9384 */
9385 public IBinder getWindowToken() {
9386 return mAttachInfo != null ? mAttachInfo.mWindowToken : null;
9387 }
9388
9389 /**
9390 * Retrieve a unique token identifying the top-level "real" window of
9391 * the window that this view is attached to. That is, this is like
9392 * {@link #getWindowToken}, except if the window this view in is a panel
9393 * window (attached to another containing window), then the token of
9394 * the containing window is returned instead.
9395 *
9396 * @return Returns the associated window token, either
9397 * {@link #getWindowToken()} or the containing window's token.
9398 */
9399 public IBinder getApplicationWindowToken() {
9400 AttachInfo ai = mAttachInfo;
9401 if (ai != null) {
9402 IBinder appWindowToken = ai.mPanelParentWindowToken;
9403 if (appWindowToken == null) {
9404 appWindowToken = ai.mWindowToken;
9405 }
9406 return appWindowToken;
9407 }
9408 return null;
9409 }
9410
9411 /**
9412 * Retrieve private session object this view hierarchy is using to
9413 * communicate with the window manager.
9414 * @return the session object to communicate with the window manager
9415 */
9416 /*package*/ IWindowSession getWindowSession() {
9417 return mAttachInfo != null ? mAttachInfo.mSession : null;
9418 }
9419
9420 /**
9421 * @param info the {@link android.view.View.AttachInfo} to associated with
9422 * this view
9423 */
9424 void dispatchAttachedToWindow(AttachInfo info, int visibility) {
9425 //System.out.println("Attached! " + this);
9426 mAttachInfo = info;
9427 mWindowAttachCount++;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009428 // We will need to evaluate the drawable state at least once.
9429 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009430 if (mFloatingTreeObserver != null) {
9431 info.mTreeObserver.merge(mFloatingTreeObserver);
9432 mFloatingTreeObserver = null;
9433 }
9434 if ((mPrivateFlags&SCROLL_CONTAINER) != 0) {
9435 mAttachInfo.mScrollContainers.add(this);
9436 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
9437 }
9438 performCollectViewAttributes(visibility);
9439 onAttachedToWindow();
Adam Powell4afd62b2011-02-18 15:02:18 -08009440
9441 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9442 mOnAttachStateChangeListeners;
9443 if (listeners != null && listeners.size() > 0) {
9444 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9445 // perform the dispatching. The iterator is a safe guard against listeners that
9446 // could mutate the list by calling the various add/remove methods. This prevents
9447 // the array from being modified while we iterate it.
9448 for (OnAttachStateChangeListener listener : listeners) {
9449 listener.onViewAttachedToWindow(this);
9450 }
9451 }
9452
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009453 int vis = info.mWindowVisibility;
9454 if (vis != GONE) {
9455 onWindowVisibilityChanged(vis);
9456 }
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08009457 if ((mPrivateFlags&DRAWABLE_STATE_DIRTY) != 0) {
9458 // If nobody has evaluated the drawable state yet, then do it now.
9459 refreshDrawableState();
9460 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009461 }
9462
9463 void dispatchDetachedFromWindow() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009464 AttachInfo info = mAttachInfo;
9465 if (info != null) {
9466 int vis = info.mWindowVisibility;
9467 if (vis != GONE) {
9468 onWindowVisibilityChanged(GONE);
9469 }
9470 }
9471
9472 onDetachedFromWindow();
Romain Guy01d5edc2011-01-28 11:28:53 -08009473
Adam Powell4afd62b2011-02-18 15:02:18 -08009474 final CopyOnWriteArrayList<OnAttachStateChangeListener> listeners =
9475 mOnAttachStateChangeListeners;
9476 if (listeners != null && listeners.size() > 0) {
9477 // NOTE: because of the use of CopyOnWriteArrayList, we *must* use an iterator to
9478 // perform the dispatching. The iterator is a safe guard against listeners that
9479 // could mutate the list by calling the various add/remove methods. This prevents
9480 // the array from being modified while we iterate it.
9481 for (OnAttachStateChangeListener listener : listeners) {
9482 listener.onViewDetachedFromWindow(this);
9483 }
9484 }
9485
Romain Guy01d5edc2011-01-28 11:28:53 -08009486 if ((mPrivateFlags & SCROLL_CONTAINER_ADDED) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009487 mAttachInfo.mScrollContainers.remove(this);
9488 mPrivateFlags &= ~SCROLL_CONTAINER_ADDED;
9489 }
Romain Guy01d5edc2011-01-28 11:28:53 -08009490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009491 mAttachInfo = null;
9492 }
9493
9494 /**
9495 * Store this view hierarchy's frozen state into the given container.
9496 *
9497 * @param container The SparseArray in which to save the view's state.
9498 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009499 * @see #restoreHierarchyState(android.util.SparseArray)
9500 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9501 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009502 */
9503 public void saveHierarchyState(SparseArray<Parcelable> container) {
9504 dispatchSaveInstanceState(container);
9505 }
9506
9507 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009508 * Called by {@link #saveHierarchyState(android.util.SparseArray)} to store the state for
9509 * this view and its children. May be overridden to modify how freezing happens to a
9510 * 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 -08009511 *
9512 * @param container The SparseArray in which to save the view's state.
9513 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009514 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9515 * @see #saveHierarchyState(android.util.SparseArray)
9516 * @see #onSaveInstanceState()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009517 */
9518 protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) {
9519 if (mID != NO_ID && (mViewFlags & SAVE_DISABLED_MASK) == 0) {
9520 mPrivateFlags &= ~SAVE_STATE_CALLED;
9521 Parcelable state = onSaveInstanceState();
9522 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9523 throw new IllegalStateException(
9524 "Derived class did not call super.onSaveInstanceState()");
9525 }
9526 if (state != null) {
9527 // Log.i("View", "Freezing #" + Integer.toHexString(mID)
9528 // + ": " + state);
9529 container.put(mID, state);
9530 }
9531 }
9532 }
9533
9534 /**
9535 * Hook allowing a view to generate a representation of its internal state
9536 * that can later be used to create a new instance with that same state.
9537 * This state should only contain information that is not persistent or can
9538 * not be reconstructed later. For example, you will never store your
9539 * current position on screen because that will be computed again when a
9540 * new instance of the view is placed in its view hierarchy.
9541 * <p>
9542 * Some examples of things you may store here: the current cursor position
9543 * in a text view (but usually not the text itself since that is stored in a
9544 * content provider or other persistent storage), the currently selected
9545 * item in a list view.
9546 *
9547 * @return Returns a Parcelable object containing the view's current dynamic
9548 * state, or null if there is nothing interesting to save. The
9549 * default implementation returns null.
Romain Guy5c22a8c2011-05-13 11:48:45 -07009550 * @see #onRestoreInstanceState(android.os.Parcelable)
9551 * @see #saveHierarchyState(android.util.SparseArray)
9552 * @see #dispatchSaveInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009553 * @see #setSaveEnabled(boolean)
9554 */
9555 protected Parcelable onSaveInstanceState() {
9556 mPrivateFlags |= SAVE_STATE_CALLED;
9557 return BaseSavedState.EMPTY_STATE;
9558 }
9559
9560 /**
9561 * Restore this view hierarchy's frozen state from the given container.
9562 *
9563 * @param container The SparseArray which holds previously frozen states.
9564 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009565 * @see #saveHierarchyState(android.util.SparseArray)
9566 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
9567 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009568 */
9569 public void restoreHierarchyState(SparseArray<Parcelable> container) {
9570 dispatchRestoreInstanceState(container);
9571 }
9572
9573 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07009574 * Called by {@link #restoreHierarchyState(android.util.SparseArray)} to retrieve the
9575 * state for this view and its children. May be overridden to modify how restoring
9576 * happens to a view's children; for example, some views may want to not store state
9577 * for their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009578 *
9579 * @param container The SparseArray which holds previously saved state.
9580 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009581 * @see #dispatchSaveInstanceState(android.util.SparseArray)
9582 * @see #restoreHierarchyState(android.util.SparseArray)
9583 * @see #onRestoreInstanceState(android.os.Parcelable)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009584 */
9585 protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) {
9586 if (mID != NO_ID) {
9587 Parcelable state = container.get(mID);
9588 if (state != null) {
9589 // Log.i("View", "Restoreing #" + Integer.toHexString(mID)
9590 // + ": " + state);
9591 mPrivateFlags &= ~SAVE_STATE_CALLED;
9592 onRestoreInstanceState(state);
9593 if ((mPrivateFlags & SAVE_STATE_CALLED) == 0) {
9594 throw new IllegalStateException(
9595 "Derived class did not call super.onRestoreInstanceState()");
9596 }
9597 }
9598 }
9599 }
9600
9601 /**
9602 * Hook allowing a view to re-apply a representation of its internal state that had previously
9603 * been generated by {@link #onSaveInstanceState}. This function will never be called with a
9604 * null state.
9605 *
9606 * @param state The frozen state that had previously been returned by
9607 * {@link #onSaveInstanceState}.
9608 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07009609 * @see #onSaveInstanceState()
9610 * @see #restoreHierarchyState(android.util.SparseArray)
9611 * @see #dispatchRestoreInstanceState(android.util.SparseArray)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009612 */
9613 protected void onRestoreInstanceState(Parcelable state) {
9614 mPrivateFlags |= SAVE_STATE_CALLED;
9615 if (state != BaseSavedState.EMPTY_STATE && state != null) {
Romain Guy237c1ce2009-12-08 11:30:25 -08009616 throw new IllegalArgumentException("Wrong state class, expecting View State but "
9617 + "received " + state.getClass().toString() + " instead. This usually happens "
Joe Malin32736f02011-01-19 16:14:20 -08009618 + "when two views of different type have the same id in the same hierarchy. "
9619 + "This view's id is " + ViewDebug.resolveId(mContext, getId()) + ". Make sure "
Romain Guy237c1ce2009-12-08 11:30:25 -08009620 + "other views do not use the same id.");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009621 }
9622 }
9623
9624 /**
9625 * <p>Return the time at which the drawing of the view hierarchy started.</p>
9626 *
9627 * @return the drawing start time in milliseconds
9628 */
9629 public long getDrawingTime() {
9630 return mAttachInfo != null ? mAttachInfo.mDrawingTime : 0;
9631 }
9632
9633 /**
9634 * <p>Enables or disables the duplication of the parent's state into this view. When
9635 * duplication is enabled, this view gets its drawable state from its parent rather
9636 * than from its own internal properties.</p>
9637 *
9638 * <p>Note: in the current implementation, setting this property to true after the
9639 * view was added to a ViewGroup might have no effect at all. This property should
9640 * always be used from XML or set to true before adding this view to a ViewGroup.</p>
9641 *
9642 * <p>Note: if this view's parent addStateFromChildren property is enabled and this
9643 * property is enabled, an exception will be thrown.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009644 *
Gilles Debunnefb817032011-01-13 13:52:49 -08009645 * <p>Note: if the child view uses and updates additionnal states which are unknown to the
9646 * parent, these states should not be affected by this method.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009647 *
9648 * @param enabled True to enable duplication of the parent's drawable state, false
9649 * to disable it.
9650 *
9651 * @see #getDrawableState()
9652 * @see #isDuplicateParentStateEnabled()
9653 */
9654 public void setDuplicateParentStateEnabled(boolean enabled) {
9655 setFlags(enabled ? DUPLICATE_PARENT_STATE : 0, DUPLICATE_PARENT_STATE);
9656 }
9657
9658 /**
9659 * <p>Indicates whether this duplicates its drawable state from its parent.</p>
9660 *
9661 * @return True if this view's drawable state is duplicated from the parent,
9662 * false otherwise
9663 *
9664 * @see #getDrawableState()
9665 * @see #setDuplicateParentStateEnabled(boolean)
9666 */
9667 public boolean isDuplicateParentStateEnabled() {
9668 return (mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE;
9669 }
9670
9671 /**
Romain Guy171c5922011-01-06 10:04:23 -08009672 * <p>Specifies the type of layer backing this view. The layer can be
9673 * {@link #LAYER_TYPE_NONE disabled}, {@link #LAYER_TYPE_SOFTWARE software} or
9674 * {@link #LAYER_TYPE_HARDWARE hardware}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009675 *
Romain Guy171c5922011-01-06 10:04:23 -08009676 * <p>A layer is associated with an optional {@link android.graphics.Paint}
9677 * instance that controls how the layer is composed on screen. The following
9678 * properties of the paint are taken into account when composing the layer:</p>
9679 * <ul>
9680 * <li>{@link android.graphics.Paint#getAlpha() Translucency (alpha)}</li>
9681 * <li>{@link android.graphics.Paint#getXfermode() Blending mode}</li>
9682 * <li>{@link android.graphics.Paint#getColorFilter() Color filter}</li>
9683 * </ul>
Joe Malin32736f02011-01-19 16:14:20 -08009684 *
Romain Guy171c5922011-01-06 10:04:23 -08009685 * <p>If this view has an alpha value set to < 1.0 by calling
9686 * {@link #setAlpha(float)}, the alpha value of the layer's paint is replaced by
9687 * this view's alpha value. Calling {@link #setAlpha(float)} is therefore
9688 * equivalent to setting a hardware layer on this view and providing a paint with
9689 * the desired alpha value.<p>
Joe Malin32736f02011-01-19 16:14:20 -08009690 *
Romain Guy171c5922011-01-06 10:04:23 -08009691 * <p>Refer to the documentation of {@link #LAYER_TYPE_NONE disabled},
9692 * {@link #LAYER_TYPE_SOFTWARE software} and {@link #LAYER_TYPE_HARDWARE hardware}
9693 * for more information on when and how to use layers.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009694 *
Romain Guy171c5922011-01-06 10:04:23 -08009695 * @param layerType The ype of layer to use with this view, must be one of
9696 * {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9697 * {@link #LAYER_TYPE_HARDWARE}
9698 * @param paint The paint used to compose the layer. This argument is optional
9699 * and can be null. It is ignored when the layer type is
9700 * {@link #LAYER_TYPE_NONE}
Joe Malin32736f02011-01-19 16:14:20 -08009701 *
9702 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08009703 * @see #LAYER_TYPE_NONE
9704 * @see #LAYER_TYPE_SOFTWARE
9705 * @see #LAYER_TYPE_HARDWARE
Joe Malin32736f02011-01-19 16:14:20 -08009706 * @see #setAlpha(float)
9707 *
Romain Guy171c5922011-01-06 10:04:23 -08009708 * @attr ref android.R.styleable#View_layerType
9709 */
9710 public void setLayerType(int layerType, Paint paint) {
9711 if (layerType < LAYER_TYPE_NONE || layerType > LAYER_TYPE_HARDWARE) {
Joe Malin32736f02011-01-19 16:14:20 -08009712 throw new IllegalArgumentException("Layer type can only be one of: LAYER_TYPE_NONE, "
Romain Guy171c5922011-01-06 10:04:23 -08009713 + "LAYER_TYPE_SOFTWARE or LAYER_TYPE_HARDWARE");
9714 }
Chet Haasedaf98e92011-01-10 14:10:36 -08009715
Romain Guyd6cd5722011-01-17 14:42:41 -08009716 if (layerType == mLayerType) {
9717 if (layerType != LAYER_TYPE_NONE && paint != mLayerPaint) {
9718 mLayerPaint = paint == null ? new Paint() : paint;
Romain Guy0fd89bf2011-01-26 15:41:30 -08009719 invalidateParentCaches();
9720 invalidate(true);
Romain Guyd6cd5722011-01-17 14:42:41 -08009721 }
9722 return;
9723 }
Romain Guy171c5922011-01-06 10:04:23 -08009724
9725 // Destroy any previous software drawing cache if needed
Romain Guy6c319ca2011-01-11 14:29:25 -08009726 switch (mLayerType) {
Chet Haase6f33e812011-05-17 12:42:19 -07009727 case LAYER_TYPE_HARDWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009728 destroyLayer();
Chet Haase6f33e812011-05-17 12:42:19 -07009729 // fall through - unaccelerated views may use software layer mechanism instead
Romain Guy6c319ca2011-01-11 14:29:25 -08009730 case LAYER_TYPE_SOFTWARE:
Romain Guy6d7475d2011-07-27 16:28:21 -07009731 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009732 break;
Romain Guy6c319ca2011-01-11 14:29:25 -08009733 default:
9734 break;
Romain Guy171c5922011-01-06 10:04:23 -08009735 }
9736
9737 mLayerType = layerType;
Romain Guy3a3133d2011-02-01 22:59:58 -08009738 final boolean layerDisabled = mLayerType == LAYER_TYPE_NONE;
9739 mLayerPaint = layerDisabled ? null : (paint == null ? new Paint() : paint);
9740 mLocalDirtyRect = layerDisabled ? null : new Rect();
Romain Guy171c5922011-01-06 10:04:23 -08009741
Romain Guy0fd89bf2011-01-26 15:41:30 -08009742 invalidateParentCaches();
9743 invalidate(true);
Romain Guy171c5922011-01-06 10:04:23 -08009744 }
9745
9746 /**
9747 * Indicates what type of layer is currently associated with this view. By default
9748 * a view does not have a layer, and the layer type is {@link #LAYER_TYPE_NONE}.
9749 * Refer to the documentation of {@link #setLayerType(int, android.graphics.Paint)}
9750 * for more information on the different types of layers.
Joe Malin32736f02011-01-19 16:14:20 -08009751 *
Romain Guy171c5922011-01-06 10:04:23 -08009752 * @return {@link #LAYER_TYPE_NONE}, {@link #LAYER_TYPE_SOFTWARE} or
9753 * {@link #LAYER_TYPE_HARDWARE}
Joe Malin32736f02011-01-19 16:14:20 -08009754 *
9755 * @see #setLayerType(int, android.graphics.Paint)
Romain Guyf1ae1062011-03-02 18:16:04 -08009756 * @see #buildLayer()
Romain Guy171c5922011-01-06 10:04:23 -08009757 * @see #LAYER_TYPE_NONE
9758 * @see #LAYER_TYPE_SOFTWARE
9759 * @see #LAYER_TYPE_HARDWARE
9760 */
9761 public int getLayerType() {
9762 return mLayerType;
9763 }
Joe Malin32736f02011-01-19 16:14:20 -08009764
Romain Guy6c319ca2011-01-11 14:29:25 -08009765 /**
Romain Guyf1ae1062011-03-02 18:16:04 -08009766 * Forces this view's layer to be created and this view to be rendered
9767 * into its layer. If this view's layer type is set to {@link #LAYER_TYPE_NONE},
9768 * invoking this method will have no effect.
9769 *
9770 * This method can for instance be used to render a view into its layer before
9771 * starting an animation. If this view is complex, rendering into the layer
9772 * before starting the animation will avoid skipping frames.
9773 *
9774 * @throws IllegalStateException If this view is not attached to a window
9775 *
9776 * @see #setLayerType(int, android.graphics.Paint)
9777 */
9778 public void buildLayer() {
9779 if (mLayerType == LAYER_TYPE_NONE) return;
9780
9781 if (mAttachInfo == null) {
9782 throw new IllegalStateException("This view must be attached to a window first");
9783 }
9784
9785 switch (mLayerType) {
9786 case LAYER_TYPE_HARDWARE:
9787 getHardwareLayer();
9788 break;
9789 case LAYER_TYPE_SOFTWARE:
9790 buildDrawingCache(true);
9791 break;
9792 }
9793 }
9794
9795 /**
Romain Guy6c319ca2011-01-11 14:29:25 -08009796 * <p>Returns a hardware layer that can be used to draw this view again
9797 * without executing its draw method.</p>
9798 *
9799 * @return A HardwareLayer ready to render, or null if an error occurred.
9800 */
Romain Guy5e7f7662011-01-24 22:35:56 -08009801 HardwareLayer getHardwareLayer() {
Romain Guyea835032011-07-28 19:24:37 -07009802 if (mAttachInfo == null || mAttachInfo.mHardwareRenderer == null ||
9803 !mAttachInfo.mHardwareRenderer.isEnabled()) {
Romain Guy6c319ca2011-01-11 14:29:25 -08009804 return null;
9805 }
9806
9807 final int width = mRight - mLeft;
9808 final int height = mBottom - mTop;
Joe Malin32736f02011-01-19 16:14:20 -08009809
Romain Guy6c319ca2011-01-11 14:29:25 -08009810 if (width == 0 || height == 0) {
9811 return null;
9812 }
9813
9814 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || mHardwareLayer == null) {
9815 if (mHardwareLayer == null) {
9816 mHardwareLayer = mAttachInfo.mHardwareRenderer.createHardwareLayer(
9817 width, height, isOpaque());
Romain Guy62687ec2011-02-02 15:44:19 -08009818 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009819 } else if (mHardwareLayer.getWidth() != width || mHardwareLayer.getHeight() != height) {
9820 mHardwareLayer.resize(width, height);
Romain Guy62687ec2011-02-02 15:44:19 -08009821 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009822 }
9823
Romain Guy59a12ca2011-06-09 17:48:21 -07009824 HardwareCanvas currentCanvas = mAttachInfo.mHardwareCanvas;
Romain Guy5e7f7662011-01-24 22:35:56 -08009825 final HardwareCanvas canvas = mHardwareLayer.start(currentCanvas);
9826 mAttachInfo.mHardwareCanvas = canvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009827 try {
9828 canvas.setViewport(width, height);
Romain Guy3a3133d2011-02-01 22:59:58 -08009829 canvas.onPreDraw(mLocalDirtyRect);
Romain Guy62687ec2011-02-02 15:44:19 -08009830 mLocalDirtyRect.setEmpty();
Romain Guy6c319ca2011-01-11 14:29:25 -08009831
Chet Haase88172fe2011-03-07 17:36:33 -08009832 final int restoreCount = canvas.save();
9833
Romain Guy6c319ca2011-01-11 14:29:25 -08009834 computeScroll();
9835 canvas.translate(-mScrollX, -mScrollY);
9836
Romain Guy6c319ca2011-01-11 14:29:25 -08009837 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Joe Malin32736f02011-01-19 16:14:20 -08009838
Romain Guy6c319ca2011-01-11 14:29:25 -08009839 // Fast path for layouts with no backgrounds
9840 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
9841 mPrivateFlags &= ~DIRTY_MASK;
9842 dispatchDraw(canvas);
9843 } else {
9844 draw(canvas);
9845 }
Joe Malin32736f02011-01-19 16:14:20 -08009846
Chet Haase88172fe2011-03-07 17:36:33 -08009847 canvas.restoreToCount(restoreCount);
Romain Guy6c319ca2011-01-11 14:29:25 -08009848 } finally {
9849 canvas.onPostDraw();
Romain Guy5e7f7662011-01-24 22:35:56 -08009850 mHardwareLayer.end(currentCanvas);
9851 mAttachInfo.mHardwareCanvas = currentCanvas;
Romain Guy6c319ca2011-01-11 14:29:25 -08009852 }
9853 }
9854
9855 return mHardwareLayer;
9856 }
Romain Guy171c5922011-01-06 10:04:23 -08009857
Romain Guy65b345f2011-07-27 18:51:50 -07009858 boolean destroyLayer() {
Romain Guy6d7475d2011-07-27 16:28:21 -07009859 if (mHardwareLayer != null) {
9860 mHardwareLayer.destroy();
9861 mHardwareLayer = null;
Romain Guy65b345f2011-07-27 18:51:50 -07009862 return true;
Romain Guy6d7475d2011-07-27 16:28:21 -07009863 }
Romain Guy65b345f2011-07-27 18:51:50 -07009864 return false;
Romain Guy6d7475d2011-07-27 16:28:21 -07009865 }
9866
Romain Guy171c5922011-01-06 10:04:23 -08009867 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009868 * <p>Enables or disables the drawing cache. When the drawing cache is enabled, the next call
9869 * to {@link #getDrawingCache()} or {@link #buildDrawingCache()} will draw the view in a
9870 * bitmap. Calling {@link #draw(android.graphics.Canvas)} will not draw from the cache when
9871 * the cache is enabled. To benefit from the cache, you must request the drawing cache by
9872 * calling {@link #getDrawingCache()} and draw it on screen if the returned bitmap is not
9873 * null.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009874 *
Romain Guy171c5922011-01-06 10:04:23 -08009875 * <p>Enabling the drawing cache is similar to
9876 * {@link #setLayerType(int, android.graphics.Paint) setting a layer} when hardware
Chet Haasedaf98e92011-01-10 14:10:36 -08009877 * acceleration is turned off. When hardware acceleration is turned on, enabling the
9878 * drawing cache has no effect on rendering because the system uses a different mechanism
9879 * for acceleration which ignores the flag. If you want to use a Bitmap for the view, even
9880 * when hardware acceleration is enabled, see {@link #setLayerType(int, android.graphics.Paint)}
9881 * for information on how to enable software and hardware layers.</p>
9882 *
9883 * <p>This API can be used to manually generate
9884 * a bitmap copy of this view, by setting the flag to <code>true</code> and calling
9885 * {@link #getDrawingCache()}.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009886 *
9887 * @param enabled true to enable the drawing cache, false otherwise
9888 *
9889 * @see #isDrawingCacheEnabled()
9890 * @see #getDrawingCache()
9891 * @see #buildDrawingCache()
Joe Malin32736f02011-01-19 16:14:20 -08009892 * @see #setLayerType(int, android.graphics.Paint)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009893 */
9894 public void setDrawingCacheEnabled(boolean enabled) {
Romain Guy0211a0a2011-02-14 16:34:59 -08009895 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009896 setFlags(enabled ? DRAWING_CACHE_ENABLED : 0, DRAWING_CACHE_ENABLED);
9897 }
9898
9899 /**
9900 * <p>Indicates whether the drawing cache is enabled for this view.</p>
9901 *
9902 * @return true if the drawing cache is enabled
9903 *
9904 * @see #setDrawingCacheEnabled(boolean)
9905 * @see #getDrawingCache()
9906 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07009907 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009908 public boolean isDrawingCacheEnabled() {
9909 return (mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED;
9910 }
9911
9912 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08009913 * Debugging utility which recursively outputs the dirty state of a view and its
9914 * descendants.
Joe Malin32736f02011-01-19 16:14:20 -08009915 *
Chet Haasedaf98e92011-01-10 14:10:36 -08009916 * @hide
9917 */
Romain Guy676b1732011-02-14 14:45:33 -08009918 @SuppressWarnings({"UnusedDeclaration"})
Chet Haasedaf98e92011-01-10 14:10:36 -08009919 public void outputDirtyFlags(String indent, boolean clear, int clearMask) {
9920 Log.d("View", indent + this + " DIRTY(" + (mPrivateFlags & View.DIRTY_MASK) +
9921 ") DRAWN(" + (mPrivateFlags & DRAWN) + ")" + " CACHE_VALID(" +
9922 (mPrivateFlags & View.DRAWING_CACHE_VALID) +
9923 ") INVALIDATED(" + (mPrivateFlags & INVALIDATED) + ")");
9924 if (clear) {
9925 mPrivateFlags &= clearMask;
9926 }
9927 if (this instanceof ViewGroup) {
9928 ViewGroup parent = (ViewGroup) this;
9929 final int count = parent.getChildCount();
9930 for (int i = 0; i < count; i++) {
Romain Guy7d7b5492011-01-24 16:33:45 -08009931 final View child = parent.getChildAt(i);
Chet Haasedaf98e92011-01-10 14:10:36 -08009932 child.outputDirtyFlags(indent + " ", clear, clearMask);
9933 }
9934 }
9935 }
9936
9937 /**
9938 * This method is used by ViewGroup to cause its children to restore or recreate their
9939 * display lists. It is called by getDisplayList() when the parent ViewGroup does not need
9940 * to recreate its own display list, which would happen if it went through the normal
9941 * draw/dispatchDraw mechanisms.
9942 *
9943 * @hide
9944 */
9945 protected void dispatchGetDisplayList() {}
Chet Haasef4ac5472011-01-27 10:30:25 -08009946
9947 /**
9948 * A view that is not attached or hardware accelerated cannot create a display list.
9949 * This method checks these conditions and returns the appropriate result.
9950 *
9951 * @return true if view has the ability to create a display list, false otherwise.
9952 *
9953 * @hide
9954 */
9955 public boolean canHaveDisplayList() {
Romain Guy676b1732011-02-14 14:45:33 -08009956 return !(mAttachInfo == null || mAttachInfo.mHardwareRenderer == null);
Chet Haasef4ac5472011-01-27 10:30:25 -08009957 }
Joe Malin32736f02011-01-19 16:14:20 -08009958
Chet Haasedaf98e92011-01-10 14:10:36 -08009959 /**
Romain Guyb051e892010-09-28 19:09:36 -07009960 * <p>Returns a display list that can be used to draw this view again
9961 * without executing its draw method.</p>
Joe Malin32736f02011-01-19 16:14:20 -08009962 *
Romain Guyb051e892010-09-28 19:09:36 -07009963 * @return A DisplayList ready to replay, or null if caching is not enabled.
Chet Haasedaf98e92011-01-10 14:10:36 -08009964 *
9965 * @hide
Romain Guyb051e892010-09-28 19:09:36 -07009966 */
Chet Haasedaf98e92011-01-10 14:10:36 -08009967 public DisplayList getDisplayList() {
Chet Haasef4ac5472011-01-27 10:30:25 -08009968 if (!canHaveDisplayList()) {
Romain Guyb051e892010-09-28 19:09:36 -07009969 return null;
9970 }
9971
Chet Haasedaf98e92011-01-10 14:10:36 -08009972 if (((mPrivateFlags & DRAWING_CACHE_VALID) == 0 ||
9973 mDisplayList == null || !mDisplayList.isValid() ||
9974 mRecreateDisplayList)) {
9975 // Don't need to recreate the display list, just need to tell our
9976 // children to restore/recreate theirs
9977 if (mDisplayList != null && mDisplayList.isValid() &&
9978 !mRecreateDisplayList) {
9979 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
9980 mPrivateFlags &= ~DIRTY_MASK;
9981 dispatchGetDisplayList();
9982
9983 return mDisplayList;
9984 }
9985
9986 // If we got here, we're recreating it. Mark it as such to ensure that
9987 // we copy in child display lists into ours in drawChild()
9988 mRecreateDisplayList = true;
Chet Haase9e90a992011-01-04 16:23:21 -08009989 if (mDisplayList == null) {
Jeff Brown162a0212011-07-21 17:02:54 -07009990 mDisplayList = mAttachInfo.mHardwareRenderer.createDisplayList();
Chet Haasedaf98e92011-01-10 14:10:36 -08009991 // If we're creating a new display list, make sure our parent gets invalidated
9992 // since they will need to recreate their display list to account for this
9993 // new child display list.
Romain Guy0fd89bf2011-01-26 15:41:30 -08009994 invalidateParentCaches();
Chet Haase9e90a992011-01-04 16:23:21 -08009995 }
Romain Guyb051e892010-09-28 19:09:36 -07009996
9997 final HardwareCanvas canvas = mDisplayList.start();
9998 try {
9999 int width = mRight - mLeft;
10000 int height = mBottom - mTop;
10001
10002 canvas.setViewport(width, height);
Romain Guy7d7b5492011-01-24 16:33:45 -080010003 // The dirty rect should always be null for a display list
10004 canvas.onPreDraw(null);
Romain Guyb051e892010-09-28 19:09:36 -070010005
Chet Haasedaf98e92011-01-10 14:10:36 -080010006 computeScroll();
10007 canvas.translate(-mScrollX, -mScrollY);
Romain Guy2fe9a8f2010-10-04 20:17:01 -070010008 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
Romain Guya9489272011-06-22 20:58:11 -070010009 mPrivateFlags &= ~DIRTY_MASK;
Joe Malin32736f02011-01-19 16:14:20 -080010010
Romain Guyb051e892010-09-28 19:09:36 -070010011 // Fast path for layouts with no backgrounds
10012 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
Romain Guyb051e892010-09-28 19:09:36 -070010013 dispatchDraw(canvas);
10014 } else {
10015 draw(canvas);
10016 }
Romain Guyb051e892010-09-28 19:09:36 -070010017 } finally {
10018 canvas.onPostDraw();
10019
10020 mDisplayList.end();
Romain Guyb051e892010-09-28 19:09:36 -070010021 }
Chet Haase77785f92011-01-25 23:22:09 -080010022 } else {
10023 mPrivateFlags |= DRAWN | DRAWING_CACHE_VALID;
10024 mPrivateFlags &= ~DIRTY_MASK;
Romain Guyb051e892010-09-28 19:09:36 -070010025 }
10026
10027 return mDisplayList;
10028 }
10029
10030 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010031 * <p>Calling this method is equivalent to calling <code>getDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010032 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010033 * @return A non-scaled bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010034 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010035 * @see #getDrawingCache(boolean)
10036 */
10037 public Bitmap getDrawingCache() {
10038 return getDrawingCache(false);
10039 }
10040
10041 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010042 * <p>Returns the bitmap in which this view drawing is cached. The returned bitmap
10043 * is null when caching is disabled. If caching is enabled and the cache is not ready,
10044 * this method will create it. Calling {@link #draw(android.graphics.Canvas)} will not
10045 * draw from the cache when the cache is enabled. To benefit from the cache, you must
10046 * request the drawing cache by calling this method and draw it on screen if the
10047 * returned bitmap is not null.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010048 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010049 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10050 * this method will create a bitmap of the same size as this view. Because this bitmap
10051 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10052 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10053 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10054 * size than the view. This implies that your application must be able to handle this
10055 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010056 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010057 * @param autoScale Indicates whether the generated bitmap should be scaled based on
10058 * the current density of the screen when the application is in compatibility
10059 * mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010060 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010061 * @return A bitmap representing this view or null if cache is disabled.
Joe Malin32736f02011-01-19 16:14:20 -080010062 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010063 * @see #setDrawingCacheEnabled(boolean)
10064 * @see #isDrawingCacheEnabled()
Romain Guyfbd8f692009-06-26 14:51:58 -070010065 * @see #buildDrawingCache(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010066 * @see #destroyDrawingCache()
10067 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010068 public Bitmap getDrawingCache(boolean autoScale) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010069 if ((mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING) {
10070 return null;
10071 }
10072 if ((mViewFlags & DRAWING_CACHE_ENABLED) == DRAWING_CACHE_ENABLED) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010073 buildDrawingCache(autoScale);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010074 }
Romain Guy02890fd2010-08-06 17:58:44 -070010075 return autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010076 }
10077
10078 /**
10079 * <p>Frees the resources used by the drawing cache. If you call
10080 * {@link #buildDrawingCache()} manually without calling
10081 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10082 * should cleanup the cache with this method afterwards.</p>
10083 *
10084 * @see #setDrawingCacheEnabled(boolean)
10085 * @see #buildDrawingCache()
10086 * @see #getDrawingCache()
10087 */
10088 public void destroyDrawingCache() {
10089 if (mDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010090 mDrawingCache.recycle();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010091 mDrawingCache = null;
10092 }
Romain Guyfbd8f692009-06-26 14:51:58 -070010093 if (mUnscaledDrawingCache != null) {
Romain Guy02890fd2010-08-06 17:58:44 -070010094 mUnscaledDrawingCache.recycle();
Romain Guyfbd8f692009-06-26 14:51:58 -070010095 mUnscaledDrawingCache = null;
10096 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010097 }
10098
10099 /**
10100 * Setting a solid background color for the drawing cache's bitmaps will improve
10101 * perfromance and memory usage. Note, though that this should only be used if this
10102 * view will always be drawn on top of a solid color.
10103 *
10104 * @param color The background color to use for the drawing cache's bitmap
10105 *
10106 * @see #setDrawingCacheEnabled(boolean)
10107 * @see #buildDrawingCache()
10108 * @see #getDrawingCache()
10109 */
10110 public void setDrawingCacheBackgroundColor(int color) {
Romain Guy52e2ef82010-01-14 12:11:48 -080010111 if (color != mDrawingCacheBackgroundColor) {
10112 mDrawingCacheBackgroundColor = color;
10113 mPrivateFlags &= ~DRAWING_CACHE_VALID;
10114 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010115 }
10116
10117 /**
10118 * @see #setDrawingCacheBackgroundColor(int)
10119 *
10120 * @return The background color to used for the drawing cache's bitmap
10121 */
10122 public int getDrawingCacheBackgroundColor() {
10123 return mDrawingCacheBackgroundColor;
10124 }
10125
10126 /**
Romain Guyfbd8f692009-06-26 14:51:58 -070010127 * <p>Calling this method is equivalent to calling <code>buildDrawingCache(false)</code>.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010128 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010129 * @see #buildDrawingCache(boolean)
10130 */
10131 public void buildDrawingCache() {
10132 buildDrawingCache(false);
10133 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -080010134
Romain Guyfbd8f692009-06-26 14:51:58 -070010135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010136 * <p>Forces the drawing cache to be built if the drawing cache is invalid.</p>
10137 *
10138 * <p>If you call {@link #buildDrawingCache()} manually without calling
10139 * {@link #setDrawingCacheEnabled(boolean) setDrawingCacheEnabled(true)}, you
10140 * should cleanup the cache by calling {@link #destroyDrawingCache()} afterwards.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010141 *
Romain Guyfbd8f692009-06-26 14:51:58 -070010142 * <p>Note about auto scaling in compatibility mode: When auto scaling is not enabled,
10143 * this method will create a bitmap of the same size as this view. Because this bitmap
10144 * will be drawn scaled by the parent ViewGroup, the result on screen might show
10145 * scaling artifacts. To avoid such artifacts, you should call this method by setting
10146 * the auto scaling to true. Doing so, however, will generate a bitmap of a different
10147 * size than the view. This implies that your application must be able to handle this
10148 * size.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010149 *
Romain Guy0d9275e2010-10-26 14:22:30 -070010150 * <p>You should avoid calling this method when hardware acceleration is enabled. If
10151 * you do not need the drawing cache bitmap, calling this method will increase memory
Joe Malin32736f02011-01-19 16:14:20 -080010152 * usage and cause the view to be rendered in software once, thus negatively impacting
Romain Guy0d9275e2010-10-26 14:22:30 -070010153 * performance.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010154 *
10155 * @see #getDrawingCache()
10156 * @see #destroyDrawingCache()
10157 */
Romain Guyfbd8f692009-06-26 14:51:58 -070010158 public void buildDrawingCache(boolean autoScale) {
10159 if ((mPrivateFlags & DRAWING_CACHE_VALID) == 0 || (autoScale ?
Romain Guy02890fd2010-08-06 17:58:44 -070010160 mDrawingCache == null : mUnscaledDrawingCache == null)) {
Romain Guy0211a0a2011-02-14 16:34:59 -080010161 mCachingFailed = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010162
10163 if (ViewDebug.TRACE_HIERARCHY) {
10164 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.BUILD_CACHE);
10165 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010166
Romain Guy8506ab42009-06-11 17:35:47 -070010167 int width = mRight - mLeft;
10168 int height = mBottom - mTop;
10169
10170 final AttachInfo attachInfo = mAttachInfo;
Romain Guye1123222009-06-29 14:24:56 -070010171 final boolean scalingRequired = attachInfo != null && attachInfo.mScalingRequired;
Romain Guyfbd8f692009-06-26 14:51:58 -070010172
Romain Guye1123222009-06-29 14:24:56 -070010173 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010174 width = (int) ((width * attachInfo.mApplicationScale) + 0.5f);
10175 height = (int) ((height * attachInfo.mApplicationScale) + 0.5f);
Romain Guy8506ab42009-06-11 17:35:47 -070010176 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010177
10178 final int drawingCacheBackgroundColor = mDrawingCacheBackgroundColor;
Romain Guy35b38ce2009-10-07 13:38:55 -070010179 final boolean opaque = drawingCacheBackgroundColor != 0 || isOpaque();
Adam Powell26153a32010-11-08 15:22:27 -080010180 final boolean use32BitCache = attachInfo != null && attachInfo.mUse32BitDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010181
10182 if (width <= 0 || height <= 0 ||
Romain Guy35b38ce2009-10-07 13:38:55 -070010183 // Projected bitmap size in bytes
Adam Powell26153a32010-11-08 15:22:27 -080010184 (width * height * (opaque && !use32BitCache ? 2 : 4) >
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010185 ViewConfiguration.get(mContext).getScaledMaximumDrawingCacheSize())) {
10186 destroyDrawingCache();
Romain Guy0211a0a2011-02-14 16:34:59 -080010187 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010188 return;
10189 }
10190
10191 boolean clear = true;
Romain Guy02890fd2010-08-06 17:58:44 -070010192 Bitmap bitmap = autoScale ? mDrawingCache : mUnscaledDrawingCache;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010193
10194 if (bitmap == null || bitmap.getWidth() != width || bitmap.getHeight() != height) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010195 Bitmap.Config quality;
10196 if (!opaque) {
Romain Guy676b1732011-02-14 14:45:33 -080010197 // Never pick ARGB_4444 because it looks awful
10198 // Keep the DRAWING_CACHE_QUALITY_LOW flag just in case
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010199 switch (mViewFlags & DRAWING_CACHE_QUALITY_MASK) {
10200 case DRAWING_CACHE_QUALITY_AUTO:
10201 quality = Bitmap.Config.ARGB_8888;
10202 break;
10203 case DRAWING_CACHE_QUALITY_LOW:
Romain Guy676b1732011-02-14 14:45:33 -080010204 quality = Bitmap.Config.ARGB_8888;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010205 break;
10206 case DRAWING_CACHE_QUALITY_HIGH:
10207 quality = Bitmap.Config.ARGB_8888;
10208 break;
10209 default:
10210 quality = Bitmap.Config.ARGB_8888;
10211 break;
10212 }
10213 } else {
Romain Guy35b38ce2009-10-07 13:38:55 -070010214 // Optimization for translucent windows
10215 // If the window is translucent, use a 32 bits bitmap to benefit from memcpy()
Adam Powell26153a32010-11-08 15:22:27 -080010216 quality = use32BitCache ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010217 }
10218
10219 // Try to cleanup memory
10220 if (bitmap != null) bitmap.recycle();
10221
10222 try {
10223 bitmap = Bitmap.createBitmap(width, height, quality);
Dianne Hackborn11ea3342009-07-22 21:48:55 -070010224 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Romain Guyfbd8f692009-06-26 14:51:58 -070010225 if (autoScale) {
Romain Guy02890fd2010-08-06 17:58:44 -070010226 mDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010227 } else {
Romain Guy02890fd2010-08-06 17:58:44 -070010228 mUnscaledDrawingCache = bitmap;
Romain Guyfbd8f692009-06-26 14:51:58 -070010229 }
Adam Powell26153a32010-11-08 15:22:27 -080010230 if (opaque && use32BitCache) bitmap.setHasAlpha(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010231 } catch (OutOfMemoryError e) {
10232 // If there is not enough memory to create the bitmap cache, just
10233 // ignore the issue as bitmap caches are not required to draw the
10234 // view hierarchy
Romain Guyfbd8f692009-06-26 14:51:58 -070010235 if (autoScale) {
10236 mDrawingCache = null;
10237 } else {
10238 mUnscaledDrawingCache = null;
10239 }
Romain Guy0211a0a2011-02-14 16:34:59 -080010240 mCachingFailed = true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010241 return;
10242 }
10243
10244 clear = drawingCacheBackgroundColor != 0;
10245 }
10246
10247 Canvas canvas;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010248 if (attachInfo != null) {
10249 canvas = attachInfo.mCanvas;
10250 if (canvas == null) {
10251 canvas = new Canvas();
10252 }
10253 canvas.setBitmap(bitmap);
10254 // Temporarily clobber the cached Canvas in case one of our children
10255 // is also using a drawing cache. Without this, the children would
10256 // steal the canvas by attaching their own bitmap to it and bad, bad
10257 // thing would happen (invisible views, corrupted drawings, etc.)
10258 attachInfo.mCanvas = null;
10259 } else {
10260 // This case should hopefully never or seldom happen
10261 canvas = new Canvas(bitmap);
10262 }
10263
10264 if (clear) {
10265 bitmap.eraseColor(drawingCacheBackgroundColor);
10266 }
10267
10268 computeScroll();
10269 final int restoreCount = canvas.save();
Joe Malin32736f02011-01-19 16:14:20 -080010270
Romain Guye1123222009-06-29 14:24:56 -070010271 if (autoScale && scalingRequired) {
Romain Guyfbd8f692009-06-26 14:51:58 -070010272 final float scale = attachInfo.mApplicationScale;
10273 canvas.scale(scale, scale);
10274 }
Joe Malin32736f02011-01-19 16:14:20 -080010275
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010276 canvas.translate(-mScrollX, -mScrollY);
10277
Romain Guy5bcdff42009-05-14 21:27:18 -070010278 mPrivateFlags |= DRAWN;
Romain Guy171c5922011-01-06 10:04:23 -080010279 if (mAttachInfo == null || !mAttachInfo.mHardwareAccelerated ||
10280 mLayerType != LAYER_TYPE_NONE) {
Romain Guy0d9275e2010-10-26 14:22:30 -070010281 mPrivateFlags |= DRAWING_CACHE_VALID;
10282 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010283
10284 // Fast path for layouts with no backgrounds
10285 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10286 if (ViewDebug.TRACE_HIERARCHY) {
10287 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10288 }
Romain Guy5bcdff42009-05-14 21:27:18 -070010289 mPrivateFlags &= ~DIRTY_MASK;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010290 dispatchDraw(canvas);
10291 } else {
10292 draw(canvas);
10293 }
10294
10295 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010296 canvas.setBitmap(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010297
10298 if (attachInfo != null) {
10299 // Restore the cached Canvas for our siblings
10300 attachInfo.mCanvas = canvas;
10301 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010302 }
10303 }
10304
10305 /**
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010306 * Create a snapshot of the view into a bitmap. We should probably make
10307 * some form of this public, but should think about the API.
10308 */
Romain Guy223ff5c2010-03-02 17:07:47 -080010309 Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) {
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010310 int width = mRight - mLeft;
10311 int height = mBottom - mTop;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010312
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010313 final AttachInfo attachInfo = mAttachInfo;
Romain Guy8c11e312009-09-14 15:15:30 -070010314 final float scale = attachInfo != null ? attachInfo.mApplicationScale : 1.0f;
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010315 width = (int) ((width * scale) + 0.5f);
10316 height = (int) ((height * scale) + 0.5f);
Joe Malin32736f02011-01-19 16:14:20 -080010317
Romain Guy8c11e312009-09-14 15:15:30 -070010318 Bitmap bitmap = Bitmap.createBitmap(width > 0 ? width : 1, height > 0 ? height : 1, quality);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010319 if (bitmap == null) {
10320 throw new OutOfMemoryError();
10321 }
10322
Romain Guyc529d8d2011-09-06 15:01:39 -070010323 Resources resources = getResources();
10324 if (resources != null) {
10325 bitmap.setDensity(resources.getDisplayMetrics().densityDpi);
10326 }
Joe Malin32736f02011-01-19 16:14:20 -080010327
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010328 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010329 if (attachInfo != null) {
10330 canvas = attachInfo.mCanvas;
10331 if (canvas == null) {
10332 canvas = new Canvas();
10333 }
10334 canvas.setBitmap(bitmap);
10335 // Temporarily clobber the cached Canvas in case one of our children
10336 // is also using a drawing cache. Without this, the children would
10337 // steal the canvas by attaching their own bitmap to it and bad, bad
10338 // things would happen (invisible views, corrupted drawings, etc.)
10339 attachInfo.mCanvas = null;
10340 } else {
10341 // This case should hopefully never or seldom happen
10342 canvas = new Canvas(bitmap);
10343 }
10344
Romain Guy5bcdff42009-05-14 21:27:18 -070010345 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010346 bitmap.eraseColor(backgroundColor);
10347 }
10348
10349 computeScroll();
10350 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010351 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010352 canvas.translate(-mScrollX, -mScrollY);
10353
Romain Guy5bcdff42009-05-14 21:27:18 -070010354 // Temporarily remove the dirty mask
10355 int flags = mPrivateFlags;
10356 mPrivateFlags &= ~DIRTY_MASK;
10357
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010358 // Fast path for layouts with no backgrounds
10359 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10360 dispatchDraw(canvas);
10361 } else {
10362 draw(canvas);
10363 }
10364
Romain Guy5bcdff42009-05-14 21:27:18 -070010365 mPrivateFlags = flags;
10366
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010367 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010368 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010369
10370 if (attachInfo != null) {
10371 // Restore the cached Canvas for our siblings
10372 attachInfo.mCanvas = canvas;
10373 }
Romain Guy8506ab42009-06-11 17:35:47 -070010374
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010375 return bitmap;
10376 }
10377
10378 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010379 * Indicates whether this View is currently in edit mode. A View is usually
10380 * in edit mode when displayed within a developer tool. For instance, if
10381 * this View is being drawn by a visual user interface builder, this method
10382 * should return true.
10383 *
10384 * Subclasses should check the return value of this method to provide
10385 * different behaviors if their normal behavior might interfere with the
10386 * host environment. For instance: the class spawns a thread in its
10387 * constructor, the drawing code relies on device-specific features, etc.
10388 *
10389 * This method is usually checked in the drawing code of custom widgets.
10390 *
10391 * @return True if this View is in edit mode, false otherwise.
10392 */
10393 public boolean isInEditMode() {
10394 return false;
10395 }
10396
10397 /**
10398 * If the View draws content inside its padding and enables fading edges,
10399 * it needs to support padding offsets. Padding offsets are added to the
10400 * fading edges to extend the length of the fade so that it covers pixels
10401 * drawn inside the padding.
10402 *
10403 * Subclasses of this class should override this method if they need
10404 * to draw content inside the padding.
10405 *
10406 * @return True if padding offset must be applied, false otherwise.
10407 *
10408 * @see #getLeftPaddingOffset()
10409 * @see #getRightPaddingOffset()
10410 * @see #getTopPaddingOffset()
10411 * @see #getBottomPaddingOffset()
10412 *
10413 * @since CURRENT
10414 */
10415 protected boolean isPaddingOffsetRequired() {
10416 return false;
10417 }
10418
10419 /**
10420 * Amount by which to extend the left fading region. Called only when
10421 * {@link #isPaddingOffsetRequired()} returns true.
10422 *
10423 * @return The left padding offset in pixels.
10424 *
10425 * @see #isPaddingOffsetRequired()
10426 *
10427 * @since CURRENT
10428 */
10429 protected int getLeftPaddingOffset() {
10430 return 0;
10431 }
10432
10433 /**
10434 * Amount by which to extend the right fading region. Called only when
10435 * {@link #isPaddingOffsetRequired()} returns true.
10436 *
10437 * @return The right padding offset in pixels.
10438 *
10439 * @see #isPaddingOffsetRequired()
10440 *
10441 * @since CURRENT
10442 */
10443 protected int getRightPaddingOffset() {
10444 return 0;
10445 }
10446
10447 /**
10448 * Amount by which to extend the top fading region. Called only when
10449 * {@link #isPaddingOffsetRequired()} returns true.
10450 *
10451 * @return The top padding offset in pixels.
10452 *
10453 * @see #isPaddingOffsetRequired()
10454 *
10455 * @since CURRENT
10456 */
10457 protected int getTopPaddingOffset() {
10458 return 0;
10459 }
10460
10461 /**
10462 * Amount by which to extend the bottom fading region. Called only when
10463 * {@link #isPaddingOffsetRequired()} returns true.
10464 *
10465 * @return The bottom padding offset in pixels.
10466 *
10467 * @see #isPaddingOffsetRequired()
10468 *
10469 * @since CURRENT
10470 */
10471 protected int getBottomPaddingOffset() {
10472 return 0;
10473 }
10474
10475 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010476 * @hide
10477 * @param offsetRequired
10478 */
10479 protected int getFadeTop(boolean offsetRequired) {
10480 int top = mPaddingTop;
10481 if (offsetRequired) top += getTopPaddingOffset();
10482 return top;
10483 }
10484
10485 /**
10486 * @hide
10487 * @param offsetRequired
10488 */
10489 protected int getFadeHeight(boolean offsetRequired) {
10490 int padding = mPaddingTop;
10491 if (offsetRequired) padding += getTopPaddingOffset();
10492 return mBottom - mTop - mPaddingBottom - padding;
10493 }
10494
10495 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010496 * <p>Indicates whether this view is attached to an hardware accelerated
10497 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010498 *
Romain Guy2bffd262010-09-12 17:40:02 -070010499 * <p>Even if this method returns true, it does not mean that every call
10500 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10501 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10502 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10503 * window is hardware accelerated,
10504 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10505 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010506 *
Romain Guy2bffd262010-09-12 17:40:02 -070010507 * @return True if the view is attached to a window and the window is
10508 * hardware accelerated; false in any other case.
10509 */
10510 public boolean isHardwareAccelerated() {
10511 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10512 }
Joe Malin32736f02011-01-19 16:14:20 -080010513
Romain Guy2bffd262010-09-12 17:40:02 -070010514 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010515 * Manually render this view (and all of its children) to the given Canvas.
10516 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010517 * called. When implementing a view, implement
10518 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10519 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010520 *
10521 * @param canvas The Canvas to which the View is rendered.
10522 */
10523 public void draw(Canvas canvas) {
10524 if (ViewDebug.TRACE_HIERARCHY) {
10525 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10526 }
10527
Romain Guy5bcdff42009-05-14 21:27:18 -070010528 final int privateFlags = mPrivateFlags;
10529 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10530 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10531 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010532
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010533 /*
10534 * Draw traversal performs several drawing steps which must be executed
10535 * in the appropriate order:
10536 *
10537 * 1. Draw the background
10538 * 2. If necessary, save the canvas' layers to prepare for fading
10539 * 3. Draw view's content
10540 * 4. Draw children
10541 * 5. If necessary, draw the fading edges and restore layers
10542 * 6. Draw decorations (scrollbars for instance)
10543 */
10544
10545 // Step 1, draw the background, if needed
10546 int saveCount;
10547
Romain Guy24443ea2009-05-11 11:56:30 -070010548 if (!dirtyOpaque) {
10549 final Drawable background = mBGDrawable;
10550 if (background != null) {
10551 final int scrollX = mScrollX;
10552 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010553
Romain Guy24443ea2009-05-11 11:56:30 -070010554 if (mBackgroundSizeChanged) {
10555 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10556 mBackgroundSizeChanged = false;
10557 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010558
Romain Guy24443ea2009-05-11 11:56:30 -070010559 if ((scrollX | scrollY) == 0) {
10560 background.draw(canvas);
10561 } else {
10562 canvas.translate(scrollX, scrollY);
10563 background.draw(canvas);
10564 canvas.translate(-scrollX, -scrollY);
10565 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010566 }
10567 }
10568
10569 // skip step 2 & 5 if possible (common case)
10570 final int viewFlags = mViewFlags;
10571 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10572 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10573 if (!verticalEdges && !horizontalEdges) {
10574 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010575 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010576
10577 // Step 4, draw the children
10578 dispatchDraw(canvas);
10579
10580 // Step 6, draw decorations (scrollbars)
10581 onDrawScrollBars(canvas);
10582
10583 // we're done...
10584 return;
10585 }
10586
10587 /*
10588 * Here we do the full fledged routine...
10589 * (this is an uncommon case where speed matters less,
10590 * this is why we repeat some of the tests that have been
10591 * done above)
10592 */
10593
10594 boolean drawTop = false;
10595 boolean drawBottom = false;
10596 boolean drawLeft = false;
10597 boolean drawRight = false;
10598
10599 float topFadeStrength = 0.0f;
10600 float bottomFadeStrength = 0.0f;
10601 float leftFadeStrength = 0.0f;
10602 float rightFadeStrength = 0.0f;
10603
10604 // Step 2, save the canvas' layers
10605 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010606
10607 final boolean offsetRequired = isPaddingOffsetRequired();
10608 if (offsetRequired) {
10609 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010610 }
10611
10612 int left = mScrollX + paddingLeft;
10613 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010614 int top = mScrollY + getFadeTop(offsetRequired);
10615 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010616
10617 if (offsetRequired) {
10618 right += getRightPaddingOffset();
10619 bottom += getBottomPaddingOffset();
10620 }
10621
10622 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010623 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10624 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010625
10626 // clip the fade length if top and bottom fades overlap
10627 // overlapping fades produce odd-looking artifacts
10628 if (verticalEdges && (top + length > bottom - length)) {
10629 length = (bottom - top) / 2;
10630 }
10631
10632 // also clip horizontal fades if necessary
10633 if (horizontalEdges && (left + length > right - length)) {
10634 length = (right - left) / 2;
10635 }
10636
10637 if (verticalEdges) {
10638 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010639 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010640 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010641 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010642 }
10643
10644 if (horizontalEdges) {
10645 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010646 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010647 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010648 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010649 }
10650
10651 saveCount = canvas.getSaveCount();
10652
10653 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010654 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010655 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10656
10657 if (drawTop) {
10658 canvas.saveLayer(left, top, right, top + length, null, flags);
10659 }
10660
10661 if (drawBottom) {
10662 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10663 }
10664
10665 if (drawLeft) {
10666 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10667 }
10668
10669 if (drawRight) {
10670 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10671 }
10672 } else {
10673 scrollabilityCache.setFadeColor(solidColor);
10674 }
10675
10676 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010677 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010678
10679 // Step 4, draw the children
10680 dispatchDraw(canvas);
10681
10682 // Step 5, draw the fade effect and restore layers
10683 final Paint p = scrollabilityCache.paint;
10684 final Matrix matrix = scrollabilityCache.matrix;
10685 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010686
10687 if (drawTop) {
10688 matrix.setScale(1, fadeHeight * topFadeStrength);
10689 matrix.postTranslate(left, top);
10690 fade.setLocalMatrix(matrix);
10691 canvas.drawRect(left, top, right, top + length, p);
10692 }
10693
10694 if (drawBottom) {
10695 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10696 matrix.postRotate(180);
10697 matrix.postTranslate(left, bottom);
10698 fade.setLocalMatrix(matrix);
10699 canvas.drawRect(left, bottom - length, right, bottom, p);
10700 }
10701
10702 if (drawLeft) {
10703 matrix.setScale(1, fadeHeight * leftFadeStrength);
10704 matrix.postRotate(-90);
10705 matrix.postTranslate(left, top);
10706 fade.setLocalMatrix(matrix);
10707 canvas.drawRect(left, top, left + length, bottom, p);
10708 }
10709
10710 if (drawRight) {
10711 matrix.setScale(1, fadeHeight * rightFadeStrength);
10712 matrix.postRotate(90);
10713 matrix.postTranslate(right, top);
10714 fade.setLocalMatrix(matrix);
10715 canvas.drawRect(right - length, top, right, bottom, p);
10716 }
10717
10718 canvas.restoreToCount(saveCount);
10719
10720 // Step 6, draw decorations (scrollbars)
10721 onDrawScrollBars(canvas);
10722 }
10723
10724 /**
10725 * Override this if your view is known to always be drawn on top of a solid color background,
10726 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10727 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10728 * should be set to 0xFF.
10729 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010730 * @see #setVerticalFadingEdgeEnabled(boolean)
10731 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010732 *
10733 * @return The known solid color background for this view, or 0 if the color may vary
10734 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010735 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010736 public int getSolidColor() {
10737 return 0;
10738 }
10739
10740 /**
10741 * Build a human readable string representation of the specified view flags.
10742 *
10743 * @param flags the view flags to convert to a string
10744 * @return a String representing the supplied flags
10745 */
10746 private static String printFlags(int flags) {
10747 String output = "";
10748 int numFlags = 0;
10749 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10750 output += "TAKES_FOCUS";
10751 numFlags++;
10752 }
10753
10754 switch (flags & VISIBILITY_MASK) {
10755 case INVISIBLE:
10756 if (numFlags > 0) {
10757 output += " ";
10758 }
10759 output += "INVISIBLE";
10760 // USELESS HERE numFlags++;
10761 break;
10762 case GONE:
10763 if (numFlags > 0) {
10764 output += " ";
10765 }
10766 output += "GONE";
10767 // USELESS HERE numFlags++;
10768 break;
10769 default:
10770 break;
10771 }
10772 return output;
10773 }
10774
10775 /**
10776 * Build a human readable string representation of the specified private
10777 * view flags.
10778 *
10779 * @param privateFlags the private view flags to convert to a string
10780 * @return a String representing the supplied flags
10781 */
10782 private static String printPrivateFlags(int privateFlags) {
10783 String output = "";
10784 int numFlags = 0;
10785
10786 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10787 output += "WANTS_FOCUS";
10788 numFlags++;
10789 }
10790
10791 if ((privateFlags & FOCUSED) == FOCUSED) {
10792 if (numFlags > 0) {
10793 output += " ";
10794 }
10795 output += "FOCUSED";
10796 numFlags++;
10797 }
10798
10799 if ((privateFlags & SELECTED) == SELECTED) {
10800 if (numFlags > 0) {
10801 output += " ";
10802 }
10803 output += "SELECTED";
10804 numFlags++;
10805 }
10806
10807 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10808 if (numFlags > 0) {
10809 output += " ";
10810 }
10811 output += "IS_ROOT_NAMESPACE";
10812 numFlags++;
10813 }
10814
10815 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10816 if (numFlags > 0) {
10817 output += " ";
10818 }
10819 output += "HAS_BOUNDS";
10820 numFlags++;
10821 }
10822
10823 if ((privateFlags & DRAWN) == DRAWN) {
10824 if (numFlags > 0) {
10825 output += " ";
10826 }
10827 output += "DRAWN";
10828 // USELESS HERE numFlags++;
10829 }
10830 return output;
10831 }
10832
10833 /**
10834 * <p>Indicates whether or not this view's layout will be requested during
10835 * the next hierarchy layout pass.</p>
10836 *
10837 * @return true if the layout will be forced during next layout pass
10838 */
10839 public boolean isLayoutRequested() {
10840 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10841 }
10842
10843 /**
10844 * Assign a size and position to a view and all of its
10845 * descendants
10846 *
10847 * <p>This is the second phase of the layout mechanism.
10848 * (The first is measuring). In this phase, each parent calls
10849 * layout on all of its children to position them.
10850 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010851 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010852 *
Chet Haase9c087442011-01-12 16:20:16 -080010853 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 * Derived classes with children should override
10855 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010856 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010857 *
10858 * @param l Left position, relative to parent
10859 * @param t Top position, relative to parent
10860 * @param r Right position, relative to parent
10861 * @param b Bottom position, relative to parent
10862 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010863 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010864 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010865 int oldL = mLeft;
10866 int oldT = mTop;
10867 int oldB = mBottom;
10868 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010869 boolean changed = setFrame(l, t, r, b);
10870 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10871 if (ViewDebug.TRACE_HIERARCHY) {
10872 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10873 }
10874
10875 onLayout(changed, l, t, r, b);
10876 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010877
10878 if (mOnLayoutChangeListeners != null) {
10879 ArrayList<OnLayoutChangeListener> listenersCopy =
10880 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10881 int numListeners = listenersCopy.size();
10882 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010883 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010884 }
10885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010886 }
10887 mPrivateFlags &= ~FORCE_LAYOUT;
10888 }
10889
10890 /**
10891 * Called from layout when this view should
10892 * assign a size and position to each of its children.
10893 *
10894 * Derived classes with children should override
10895 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010896 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010897 * @param changed This is a new size or position for this view
10898 * @param left Left position, relative to parent
10899 * @param top Top position, relative to parent
10900 * @param right Right position, relative to parent
10901 * @param bottom Bottom position, relative to parent
10902 */
10903 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10904 }
10905
10906 /**
10907 * Assign a size and position to this view.
10908 *
10909 * This is called from layout.
10910 *
10911 * @param left Left position, relative to parent
10912 * @param top Top position, relative to parent
10913 * @param right Right position, relative to parent
10914 * @param bottom Bottom position, relative to parent
10915 * @return true if the new size and position are different than the
10916 * previous ones
10917 * {@hide}
10918 */
10919 protected boolean setFrame(int left, int top, int right, int bottom) {
10920 boolean changed = false;
10921
10922 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010923 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010924 + right + "," + bottom + ")");
10925 }
10926
10927 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10928 changed = true;
10929
10930 // Remember our drawn bit
10931 int drawn = mPrivateFlags & DRAWN;
10932
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010933 int oldWidth = mRight - mLeft;
10934 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070010935 int newWidth = right - left;
10936 int newHeight = bottom - top;
10937 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
10938
10939 // Invalidate our old position
10940 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010941
10942 mLeft = left;
10943 mTop = top;
10944 mRight = right;
10945 mBottom = bottom;
10946
10947 mPrivateFlags |= HAS_BOUNDS;
10948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010949
Chet Haase75755e22011-07-18 17:48:25 -070010950 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010951 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10952 // A change in dimension means an auto-centered pivot point changes, too
10953 mMatrixDirty = true;
10954 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010955 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10956 }
10957
10958 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10959 // If we are visible, force the DRAWN bit to on so that
10960 // this invalidate will go through (at least to our parent).
10961 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010962 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010963 // the DRAWN bit.
10964 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070010965 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080010966 // parent display list may need to be recreated based on a change in the bounds
10967 // of any child
10968 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010969 }
10970
10971 // Reset drawn bit to original value (invalidate turns it off)
10972 mPrivateFlags |= drawn;
10973
10974 mBackgroundSizeChanged = true;
10975 }
10976 return changed;
10977 }
10978
10979 /**
10980 * Finalize inflating a view from XML. This is called as the last phase
10981 * of inflation, after all child views have been added.
10982 *
10983 * <p>Even if the subclass overrides onFinishInflate, they should always be
10984 * sure to call the super method, so that we get called.
10985 */
10986 protected void onFinishInflate() {
10987 }
10988
10989 /**
10990 * Returns the resources associated with this view.
10991 *
10992 * @return Resources object.
10993 */
10994 public Resources getResources() {
10995 return mResources;
10996 }
10997
10998 /**
10999 * Invalidates the specified Drawable.
11000 *
11001 * @param drawable the drawable to invalidate
11002 */
11003 public void invalidateDrawable(Drawable drawable) {
11004 if (verifyDrawable(drawable)) {
11005 final Rect dirty = drawable.getBounds();
11006 final int scrollX = mScrollX;
11007 final int scrollY = mScrollY;
11008
11009 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11010 dirty.right + scrollX, dirty.bottom + scrollY);
11011 }
11012 }
11013
11014 /**
11015 * Schedules an action on a drawable to occur at a specified time.
11016 *
11017 * @param who the recipient of the action
11018 * @param what the action to run on the drawable
11019 * @param when the time at which the action must occur. Uses the
11020 * {@link SystemClock#uptimeMillis} timebase.
11021 */
11022 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11023 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11024 mAttachInfo.mHandler.postAtTime(what, who, when);
11025 }
11026 }
11027
11028 /**
11029 * Cancels a scheduled action on a drawable.
11030 *
11031 * @param who the recipient of the action
11032 * @param what the action to cancel
11033 */
11034 public void unscheduleDrawable(Drawable who, Runnable what) {
11035 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11036 mAttachInfo.mHandler.removeCallbacks(what, who);
11037 }
11038 }
11039
11040 /**
11041 * Unschedule any events associated with the given Drawable. This can be
11042 * used when selecting a new Drawable into a view, so that the previous
11043 * one is completely unscheduled.
11044 *
11045 * @param who The Drawable to unschedule.
11046 *
11047 * @see #drawableStateChanged
11048 */
11049 public void unscheduleDrawable(Drawable who) {
11050 if (mAttachInfo != null) {
11051 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11052 }
11053 }
11054
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011055 /**
11056 * Return the layout direction of a given Drawable.
11057 *
11058 * @param who the Drawable to query
11059 *
11060 * @hide
11061 */
11062 public int getResolvedLayoutDirection(Drawable who) {
11063 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011064 }
11065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011066 /**
11067 * If your view subclass is displaying its own Drawable objects, it should
11068 * override this function and return true for any Drawable it is
11069 * displaying. This allows animations for those drawables to be
11070 * scheduled.
11071 *
11072 * <p>Be sure to call through to the super class when overriding this
11073 * function.
11074 *
11075 * @param who The Drawable to verify. Return true if it is one you are
11076 * displaying, else return the result of calling through to the
11077 * super class.
11078 *
11079 * @return boolean If true than the Drawable is being displayed in the
11080 * view; else false and it is not allowed to animate.
11081 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011082 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11083 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011084 */
11085 protected boolean verifyDrawable(Drawable who) {
11086 return who == mBGDrawable;
11087 }
11088
11089 /**
11090 * This function is called whenever the state of the view changes in such
11091 * a way that it impacts the state of drawables being shown.
11092 *
11093 * <p>Be sure to call through to the superclass when overriding this
11094 * function.
11095 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011096 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011097 */
11098 protected void drawableStateChanged() {
11099 Drawable d = mBGDrawable;
11100 if (d != null && d.isStateful()) {
11101 d.setState(getDrawableState());
11102 }
11103 }
11104
11105 /**
11106 * Call this to force a view to update its drawable state. This will cause
11107 * drawableStateChanged to be called on this view. Views that are interested
11108 * in the new state should call getDrawableState.
11109 *
11110 * @see #drawableStateChanged
11111 * @see #getDrawableState
11112 */
11113 public void refreshDrawableState() {
11114 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11115 drawableStateChanged();
11116
11117 ViewParent parent = mParent;
11118 if (parent != null) {
11119 parent.childDrawableStateChanged(this);
11120 }
11121 }
11122
11123 /**
11124 * Return an array of resource IDs of the drawable states representing the
11125 * current state of the view.
11126 *
11127 * @return The current drawable state
11128 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011129 * @see Drawable#setState(int[])
11130 * @see #drawableStateChanged()
11131 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011132 */
11133 public final int[] getDrawableState() {
11134 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11135 return mDrawableState;
11136 } else {
11137 mDrawableState = onCreateDrawableState(0);
11138 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11139 return mDrawableState;
11140 }
11141 }
11142
11143 /**
11144 * Generate the new {@link android.graphics.drawable.Drawable} state for
11145 * this view. This is called by the view
11146 * system when the cached Drawable state is determined to be invalid. To
11147 * retrieve the current state, you should use {@link #getDrawableState}.
11148 *
11149 * @param extraSpace if non-zero, this is the number of extra entries you
11150 * would like in the returned array in which you can place your own
11151 * states.
11152 *
11153 * @return Returns an array holding the current {@link Drawable} state of
11154 * the view.
11155 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011156 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011157 */
11158 protected int[] onCreateDrawableState(int extraSpace) {
11159 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11160 mParent instanceof View) {
11161 return ((View) mParent).onCreateDrawableState(extraSpace);
11162 }
11163
11164 int[] drawableState;
11165
11166 int privateFlags = mPrivateFlags;
11167
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011168 int viewStateIndex = 0;
11169 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11170 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11171 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011172 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011173 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11174 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011175 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11176 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011177 // This is set if HW acceleration is requested, even if the current
11178 // process doesn't allow it. This is just to allow app preview
11179 // windows to better match their app.
11180 viewStateIndex |= VIEW_STATE_ACCELERATED;
11181 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011182 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011183
Christopher Tate3d4bf172011-03-28 16:16:46 -070011184 final int privateFlags2 = mPrivateFlags2;
11185 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11186 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11187
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011188 drawableState = VIEW_STATE_SETS[viewStateIndex];
11189
11190 //noinspection ConstantIfStatement
11191 if (false) {
11192 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11193 Log.i("View", toString()
11194 + " pressed=" + ((privateFlags & PRESSED) != 0)
11195 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11196 + " fo=" + hasFocus()
11197 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011198 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011199 + ": " + Arrays.toString(drawableState));
11200 }
11201
11202 if (extraSpace == 0) {
11203 return drawableState;
11204 }
11205
11206 final int[] fullState;
11207 if (drawableState != null) {
11208 fullState = new int[drawableState.length + extraSpace];
11209 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11210 } else {
11211 fullState = new int[extraSpace];
11212 }
11213
11214 return fullState;
11215 }
11216
11217 /**
11218 * Merge your own state values in <var>additionalState</var> into the base
11219 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011220 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011221 *
11222 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011223 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011224 * own additional state values.
11225 *
11226 * @param additionalState The additional state values you would like
11227 * added to <var>baseState</var>; this array is not modified.
11228 *
11229 * @return As a convenience, the <var>baseState</var> array you originally
11230 * passed into the function is returned.
11231 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011232 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011233 */
11234 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11235 final int N = baseState.length;
11236 int i = N - 1;
11237 while (i >= 0 && baseState[i] == 0) {
11238 i--;
11239 }
11240 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11241 return baseState;
11242 }
11243
11244 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011245 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11246 * on all Drawable objects associated with this view.
11247 */
11248 public void jumpDrawablesToCurrentState() {
11249 if (mBGDrawable != null) {
11250 mBGDrawable.jumpToCurrentState();
11251 }
11252 }
11253
11254 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011255 * Sets the background color for this view.
11256 * @param color the color of the background
11257 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011258 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011259 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011260 if (mBGDrawable instanceof ColorDrawable) {
11261 ((ColorDrawable) mBGDrawable).setColor(color);
11262 } else {
11263 setBackgroundDrawable(new ColorDrawable(color));
11264 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011265 }
11266
11267 /**
11268 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011269 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011270 * @param resid The identifier of the resource.
11271 * @attr ref android.R.styleable#View_background
11272 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011273 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011274 public void setBackgroundResource(int resid) {
11275 if (resid != 0 && resid == mBackgroundResource) {
11276 return;
11277 }
11278
11279 Drawable d= null;
11280 if (resid != 0) {
11281 d = mResources.getDrawable(resid);
11282 }
11283 setBackgroundDrawable(d);
11284
11285 mBackgroundResource = resid;
11286 }
11287
11288 /**
11289 * Set the background to a given Drawable, or remove the background. If the
11290 * background has padding, this View's padding is set to the background's
11291 * padding. However, when a background is removed, this View's padding isn't
11292 * touched. If setting the padding is desired, please use
11293 * {@link #setPadding(int, int, int, int)}.
11294 *
11295 * @param d The Drawable to use as the background, or null to remove the
11296 * background
11297 */
11298 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011299 if (d == mBGDrawable) {
11300 return;
11301 }
11302
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011303 boolean requestLayout = false;
11304
11305 mBackgroundResource = 0;
11306
11307 /*
11308 * Regardless of whether we're setting a new background or not, we want
11309 * to clear the previous drawable.
11310 */
11311 if (mBGDrawable != null) {
11312 mBGDrawable.setCallback(null);
11313 unscheduleDrawable(mBGDrawable);
11314 }
11315
11316 if (d != null) {
11317 Rect padding = sThreadLocal.get();
11318 if (padding == null) {
11319 padding = new Rect();
11320 sThreadLocal.set(padding);
11321 }
11322 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011323 switch (d.getResolvedLayoutDirectionSelf()) {
11324 case LAYOUT_DIRECTION_RTL:
11325 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11326 break;
11327 case LAYOUT_DIRECTION_LTR:
11328 default:
11329 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11330 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011331 }
11332
11333 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11334 // if it has a different minimum size, we should layout again
11335 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11336 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11337 requestLayout = true;
11338 }
11339
11340 d.setCallback(this);
11341 if (d.isStateful()) {
11342 d.setState(getDrawableState());
11343 }
11344 d.setVisible(getVisibility() == VISIBLE, false);
11345 mBGDrawable = d;
11346
11347 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11348 mPrivateFlags &= ~SKIP_DRAW;
11349 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11350 requestLayout = true;
11351 }
11352 } else {
11353 /* Remove the background */
11354 mBGDrawable = null;
11355
11356 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11357 /*
11358 * This view ONLY drew the background before and we're removing
11359 * the background, so now it won't draw anything
11360 * (hence we SKIP_DRAW)
11361 */
11362 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11363 mPrivateFlags |= SKIP_DRAW;
11364 }
11365
11366 /*
11367 * When the background is set, we try to apply its padding to this
11368 * View. When the background is removed, we don't touch this View's
11369 * padding. This is noted in the Javadocs. Hence, we don't need to
11370 * requestLayout(), the invalidate() below is sufficient.
11371 */
11372
11373 // The old background's minimum size could have affected this
11374 // View's layout, so let's requestLayout
11375 requestLayout = true;
11376 }
11377
Romain Guy8f1344f52009-05-15 16:03:59 -070011378 computeOpaqueFlags();
11379
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011380 if (requestLayout) {
11381 requestLayout();
11382 }
11383
11384 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011385 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011386 }
11387
11388 /**
11389 * Gets the background drawable
11390 * @return The drawable used as the background for this view, if any.
11391 */
11392 public Drawable getBackground() {
11393 return mBGDrawable;
11394 }
11395
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011396 /**
11397 * Sets the padding. The view may add on the space required to display
11398 * the scrollbars, depending on the style and visibility of the scrollbars.
11399 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11400 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11401 * from the values set in this call.
11402 *
11403 * @attr ref android.R.styleable#View_padding
11404 * @attr ref android.R.styleable#View_paddingBottom
11405 * @attr ref android.R.styleable#View_paddingLeft
11406 * @attr ref android.R.styleable#View_paddingRight
11407 * @attr ref android.R.styleable#View_paddingTop
11408 * @param left the left padding in pixels
11409 * @param top the top padding in pixels
11410 * @param right the right padding in pixels
11411 * @param bottom the bottom padding in pixels
11412 */
11413 public void setPadding(int left, int top, int right, int bottom) {
11414 boolean changed = false;
11415
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011416 mUserPaddingRelative = false;
11417
Adam Powell20232d02010-12-08 21:08:53 -080011418 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011419 mUserPaddingRight = right;
11420 mUserPaddingBottom = bottom;
11421
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011422 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011423
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011424 // Common case is there are no scroll bars.
11425 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011426 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011427 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011428 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011429 switch (mVerticalScrollbarPosition) {
11430 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011431 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11432 left += offset;
11433 } else {
11434 right += offset;
11435 }
11436 break;
Adam Powell20232d02010-12-08 21:08:53 -080011437 case SCROLLBAR_POSITION_RIGHT:
11438 right += offset;
11439 break;
11440 case SCROLLBAR_POSITION_LEFT:
11441 left += offset;
11442 break;
11443 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011444 }
Adam Powell20232d02010-12-08 21:08:53 -080011445 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011446 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11447 ? 0 : getHorizontalScrollbarHeight();
11448 }
11449 }
Romain Guy8506ab42009-06-11 17:35:47 -070011450
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011451 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011452 changed = true;
11453 mPaddingLeft = left;
11454 }
11455 if (mPaddingTop != top) {
11456 changed = true;
11457 mPaddingTop = top;
11458 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011459 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011460 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011461 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011462 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011463 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011464 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011465 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011466 }
11467
11468 if (changed) {
11469 requestLayout();
11470 }
11471 }
11472
11473 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011474 * Sets the relative padding. The view may add on the space required to display
11475 * the scrollbars, depending on the style and visibility of the scrollbars.
11476 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11477 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11478 * from the values set in this call.
11479 *
11480 * @attr ref android.R.styleable#View_padding
11481 * @attr ref android.R.styleable#View_paddingBottom
11482 * @attr ref android.R.styleable#View_paddingStart
11483 * @attr ref android.R.styleable#View_paddingEnd
11484 * @attr ref android.R.styleable#View_paddingTop
11485 * @param start the start padding in pixels
11486 * @param top the top padding in pixels
11487 * @param end the end padding in pixels
11488 * @param bottom the bottom padding in pixels
11489 *
11490 * @hide
11491 */
11492 public void setPaddingRelative(int start, int top, int end, int bottom) {
11493 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011494
11495 mUserPaddingStart = start;
11496 mUserPaddingEnd = end;
11497
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011498 switch(getResolvedLayoutDirection()) {
11499 case LAYOUT_DIRECTION_RTL:
11500 setPadding(end, top, start, bottom);
11501 break;
11502 case LAYOUT_DIRECTION_LTR:
11503 default:
11504 setPadding(start, top, end, bottom);
11505 }
11506 }
11507
11508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011509 * Returns the top padding of this view.
11510 *
11511 * @return the top padding in pixels
11512 */
11513 public int getPaddingTop() {
11514 return mPaddingTop;
11515 }
11516
11517 /**
11518 * Returns the bottom padding of this view. If there are inset and enabled
11519 * scrollbars, this value may include the space required to display the
11520 * scrollbars as well.
11521 *
11522 * @return the bottom padding in pixels
11523 */
11524 public int getPaddingBottom() {
11525 return mPaddingBottom;
11526 }
11527
11528 /**
11529 * Returns the left padding of this view. If there are inset and enabled
11530 * scrollbars, this value may include the space required to display the
11531 * scrollbars as well.
11532 *
11533 * @return the left padding in pixels
11534 */
11535 public int getPaddingLeft() {
11536 return mPaddingLeft;
11537 }
11538
11539 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011540 * Returns the start padding of this view. If there are inset and enabled
11541 * scrollbars, this value may include the space required to display the
11542 * scrollbars as well.
11543 *
11544 * @return the start padding in pixels
11545 *
11546 * @hide
11547 */
11548 public int getPaddingStart() {
11549 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11550 mPaddingRight : mPaddingLeft;
11551 }
11552
11553 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011554 * Returns the right padding of this view. If there are inset and enabled
11555 * scrollbars, this value may include the space required to display the
11556 * scrollbars as well.
11557 *
11558 * @return the right padding in pixels
11559 */
11560 public int getPaddingRight() {
11561 return mPaddingRight;
11562 }
11563
11564 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011565 * Returns the end padding of this view. If there are inset and enabled
11566 * scrollbars, this value may include the space required to display the
11567 * scrollbars as well.
11568 *
11569 * @return the end padding in pixels
11570 *
11571 * @hide
11572 */
11573 public int getPaddingEnd() {
11574 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11575 mPaddingLeft : mPaddingRight;
11576 }
11577
11578 /**
11579 * Return if the padding as been set thru relative values
11580 * {@link #setPaddingRelative(int, int, int, int)} or thru
11581 * @attr ref android.R.styleable#View_paddingStart or
11582 * @attr ref android.R.styleable#View_paddingEnd
11583 *
11584 * @return true if the padding is relative or false if it is not.
11585 *
11586 * @hide
11587 */
11588 public boolean isPaddingRelative() {
11589 return mUserPaddingRelative;
11590 }
11591
11592 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011593 * Changes the selection state of this view. A view can be selected or not.
11594 * Note that selection is not the same as focus. Views are typically
11595 * selected in the context of an AdapterView like ListView or GridView;
11596 * the selected view is the view that is highlighted.
11597 *
11598 * @param selected true if the view must be selected, false otherwise
11599 */
11600 public void setSelected(boolean selected) {
11601 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11602 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011603 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011604 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011605 refreshDrawableState();
11606 dispatchSetSelected(selected);
11607 }
11608 }
11609
11610 /**
11611 * Dispatch setSelected to all of this View's children.
11612 *
11613 * @see #setSelected(boolean)
11614 *
11615 * @param selected The new selected state
11616 */
11617 protected void dispatchSetSelected(boolean selected) {
11618 }
11619
11620 /**
11621 * Indicates the selection state of this view.
11622 *
11623 * @return true if the view is selected, false otherwise
11624 */
11625 @ViewDebug.ExportedProperty
11626 public boolean isSelected() {
11627 return (mPrivateFlags & SELECTED) != 0;
11628 }
11629
11630 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011631 * Changes the activated state of this view. A view can be activated or not.
11632 * Note that activation is not the same as selection. Selection is
11633 * a transient property, representing the view (hierarchy) the user is
11634 * currently interacting with. Activation is a longer-term state that the
11635 * user can move views in and out of. For example, in a list view with
11636 * single or multiple selection enabled, the views in the current selection
11637 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11638 * here.) The activated state is propagated down to children of the view it
11639 * is set on.
11640 *
11641 * @param activated true if the view must be activated, false otherwise
11642 */
11643 public void setActivated(boolean activated) {
11644 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11645 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011646 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011647 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011648 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011649 }
11650 }
11651
11652 /**
11653 * Dispatch setActivated to all of this View's children.
11654 *
11655 * @see #setActivated(boolean)
11656 *
11657 * @param activated The new activated state
11658 */
11659 protected void dispatchSetActivated(boolean activated) {
11660 }
11661
11662 /**
11663 * Indicates the activation state of this view.
11664 *
11665 * @return true if the view is activated, false otherwise
11666 */
11667 @ViewDebug.ExportedProperty
11668 public boolean isActivated() {
11669 return (mPrivateFlags & ACTIVATED) != 0;
11670 }
11671
11672 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011673 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11674 * observer can be used to get notifications when global events, like
11675 * layout, happen.
11676 *
11677 * The returned ViewTreeObserver observer is not guaranteed to remain
11678 * valid for the lifetime of this View. If the caller of this method keeps
11679 * a long-lived reference to ViewTreeObserver, it should always check for
11680 * the return value of {@link ViewTreeObserver#isAlive()}.
11681 *
11682 * @return The ViewTreeObserver for this view's hierarchy.
11683 */
11684 public ViewTreeObserver getViewTreeObserver() {
11685 if (mAttachInfo != null) {
11686 return mAttachInfo.mTreeObserver;
11687 }
11688 if (mFloatingTreeObserver == null) {
11689 mFloatingTreeObserver = new ViewTreeObserver();
11690 }
11691 return mFloatingTreeObserver;
11692 }
11693
11694 /**
11695 * <p>Finds the topmost view in the current view hierarchy.</p>
11696 *
11697 * @return the topmost view containing this view
11698 */
11699 public View getRootView() {
11700 if (mAttachInfo != null) {
11701 final View v = mAttachInfo.mRootView;
11702 if (v != null) {
11703 return v;
11704 }
11705 }
Romain Guy8506ab42009-06-11 17:35:47 -070011706
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011707 View parent = this;
11708
11709 while (parent.mParent != null && parent.mParent instanceof View) {
11710 parent = (View) parent.mParent;
11711 }
11712
11713 return parent;
11714 }
11715
11716 /**
11717 * <p>Computes the coordinates of this view on the screen. The argument
11718 * must be an array of two integers. After the method returns, the array
11719 * contains the x and y location in that order.</p>
11720 *
11721 * @param location an array of two integers in which to hold the coordinates
11722 */
11723 public void getLocationOnScreen(int[] location) {
11724 getLocationInWindow(location);
11725
11726 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011727 if (info != null) {
11728 location[0] += info.mWindowLeft;
11729 location[1] += info.mWindowTop;
11730 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011731 }
11732
11733 /**
11734 * <p>Computes the coordinates of this view in its window. The argument
11735 * must be an array of two integers. After the method returns, the array
11736 * contains the x and y location in that order.</p>
11737 *
11738 * @param location an array of two integers in which to hold the coordinates
11739 */
11740 public void getLocationInWindow(int[] location) {
11741 if (location == null || location.length < 2) {
11742 throw new IllegalArgumentException("location must be an array of "
11743 + "two integers");
11744 }
11745
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011746 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11747 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011748
11749 ViewParent viewParent = mParent;
11750 while (viewParent instanceof View) {
11751 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011752 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11753 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011754 viewParent = view.mParent;
11755 }
Romain Guy8506ab42009-06-11 17:35:47 -070011756
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011757 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011758 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011759 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011760 location[1] -= vr.mCurScrollY;
11761 }
11762 }
11763
11764 /**
11765 * {@hide}
11766 * @param id the id of the view to be found
11767 * @return the view of the specified id, null if cannot be found
11768 */
11769 protected View findViewTraversal(int id) {
11770 if (id == mID) {
11771 return this;
11772 }
11773 return null;
11774 }
11775
11776 /**
11777 * {@hide}
11778 * @param tag the tag of the view to be found
11779 * @return the view of specified tag, null if cannot be found
11780 */
11781 protected View findViewWithTagTraversal(Object tag) {
11782 if (tag != null && tag.equals(mTag)) {
11783 return this;
11784 }
11785 return null;
11786 }
11787
11788 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011789 * {@hide}
11790 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070011791 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080011792 * @return The first view that matches the predicate or null.
11793 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070011794 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080011795 if (predicate.apply(this)) {
11796 return this;
11797 }
11798 return null;
11799 }
11800
11801 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011802 * Look for a child view with the given id. If this view has the given
11803 * id, return this view.
11804 *
11805 * @param id The id to search for.
11806 * @return The view that has the given id in the hierarchy or null
11807 */
11808 public final View findViewById(int id) {
11809 if (id < 0) {
11810 return null;
11811 }
11812 return findViewTraversal(id);
11813 }
11814
11815 /**
11816 * Look for a child view with the given tag. If this view has the given
11817 * tag, return this view.
11818 *
11819 * @param tag The tag to search for, using "tag.equals(getTag())".
11820 * @return The View that has the given tag in the hierarchy or null
11821 */
11822 public final View findViewWithTag(Object tag) {
11823 if (tag == null) {
11824 return null;
11825 }
11826 return findViewWithTagTraversal(tag);
11827 }
11828
11829 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011830 * {@hide}
11831 * Look for a child view that matches the specified predicate.
11832 * If this view matches the predicate, return this view.
11833 *
11834 * @param predicate The predicate to evaluate.
11835 * @return The first view that matches the predicate or null.
11836 */
11837 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070011838 return findViewByPredicateTraversal(predicate, null);
11839 }
11840
11841 /**
11842 * {@hide}
11843 * Look for a child view that matches the specified predicate,
11844 * starting with the specified view and its descendents and then
11845 * recusively searching the ancestors and siblings of that view
11846 * until this view is reached.
11847 *
11848 * This method is useful in cases where the predicate does not match
11849 * a single unique view (perhaps multiple views use the same id)
11850 * and we are trying to find the view that is "closest" in scope to the
11851 * starting view.
11852 *
11853 * @param start The view to start from.
11854 * @param predicate The predicate to evaluate.
11855 * @return The first view that matches the predicate or null.
11856 */
11857 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
11858 View childToSkip = null;
11859 for (;;) {
11860 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
11861 if (view != null || start == this) {
11862 return view;
11863 }
11864
11865 ViewParent parent = start.getParent();
11866 if (parent == null || !(parent instanceof View)) {
11867 return null;
11868 }
11869
11870 childToSkip = start;
11871 start = (View) parent;
11872 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080011873 }
11874
11875 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011876 * Sets the identifier for this view. The identifier does not have to be
11877 * unique in this view's hierarchy. The identifier should be a positive
11878 * number.
11879 *
11880 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011881 * @see #getId()
11882 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011883 *
11884 * @param id a number used to identify the view
11885 *
11886 * @attr ref android.R.styleable#View_id
11887 */
11888 public void setId(int id) {
11889 mID = id;
11890 }
11891
11892 /**
11893 * {@hide}
11894 *
11895 * @param isRoot true if the view belongs to the root namespace, false
11896 * otherwise
11897 */
11898 public void setIsRootNamespace(boolean isRoot) {
11899 if (isRoot) {
11900 mPrivateFlags |= IS_ROOT_NAMESPACE;
11901 } else {
11902 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11903 }
11904 }
11905
11906 /**
11907 * {@hide}
11908 *
11909 * @return true if the view belongs to the root namespace, false otherwise
11910 */
11911 public boolean isRootNamespace() {
11912 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11913 }
11914
11915 /**
11916 * Returns this view's identifier.
11917 *
11918 * @return a positive integer used to identify the view or {@link #NO_ID}
11919 * if the view has no ID
11920 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011921 * @see #setId(int)
11922 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011923 * @attr ref android.R.styleable#View_id
11924 */
11925 @ViewDebug.CapturedViewProperty
11926 public int getId() {
11927 return mID;
11928 }
11929
11930 /**
11931 * Returns this view's tag.
11932 *
11933 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011934 *
11935 * @see #setTag(Object)
11936 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011937 */
11938 @ViewDebug.ExportedProperty
11939 public Object getTag() {
11940 return mTag;
11941 }
11942
11943 /**
11944 * Sets the tag associated with this view. A tag can be used to mark
11945 * a view in its hierarchy and does not have to be unique within the
11946 * hierarchy. Tags can also be used to store data within a view without
11947 * resorting to another data structure.
11948 *
11949 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011950 *
11951 * @see #getTag()
11952 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011953 */
11954 public void setTag(final Object tag) {
11955 mTag = tag;
11956 }
11957
11958 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011959 * Returns the tag associated with this view and the specified key.
11960 *
11961 * @param key The key identifying the tag
11962 *
11963 * @return the Object stored in this view as a tag
11964 *
11965 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011966 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011967 */
11968 public Object getTag(int key) {
11969 SparseArray<Object> tags = null;
11970 synchronized (sTagsLock) {
11971 if (sTags != null) {
11972 tags = sTags.get(this);
11973 }
11974 }
11975
11976 if (tags != null) return tags.get(key);
11977 return null;
11978 }
11979
11980 /**
11981 * Sets a tag associated with this view and a key. A tag can be used
11982 * to mark a view in its hierarchy and does not have to be unique within
11983 * the hierarchy. Tags can also be used to store data within a view
11984 * without resorting to another data structure.
11985 *
11986 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011987 * application to ensure it is unique (see the <a
11988 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11989 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011990 * the Android framework or not associated with any package will cause
11991 * an {@link IllegalArgumentException} to be thrown.
11992 *
11993 * @param key The key identifying the tag
11994 * @param tag An Object to tag the view with
11995 *
11996 * @throws IllegalArgumentException If they specified key is not valid
11997 *
11998 * @see #setTag(Object)
11999 * @see #getTag(int)
12000 */
12001 public void setTag(int key, final Object tag) {
12002 // If the package id is 0x00 or 0x01, it's either an undefined package
12003 // or a framework id
12004 if ((key >>> 24) < 2) {
12005 throw new IllegalArgumentException("The key must be an application-specific "
12006 + "resource id.");
12007 }
12008
12009 setTagInternal(this, key, tag);
12010 }
12011
12012 /**
12013 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12014 * framework id.
12015 *
12016 * @hide
12017 */
12018 public void setTagInternal(int key, Object tag) {
12019 if ((key >>> 24) != 0x1) {
12020 throw new IllegalArgumentException("The key must be a framework-specific "
12021 + "resource id.");
12022 }
12023
Romain Guy8506ab42009-06-11 17:35:47 -070012024 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012025 }
12026
12027 private static void setTagInternal(View view, int key, Object tag) {
12028 SparseArray<Object> tags = null;
12029 synchronized (sTagsLock) {
12030 if (sTags == null) {
12031 sTags = new WeakHashMap<View, SparseArray<Object>>();
12032 } else {
12033 tags = sTags.get(view);
12034 }
12035 }
12036
12037 if (tags == null) {
12038 tags = new SparseArray<Object>(2);
12039 synchronized (sTagsLock) {
12040 sTags.put(view, tags);
12041 }
12042 }
12043
12044 tags.put(key, tag);
12045 }
12046
12047 /**
Romain Guy13922e02009-05-12 17:56:14 -070012048 * @param consistency The type of consistency. See ViewDebug for more information.
12049 *
12050 * @hide
12051 */
12052 protected boolean dispatchConsistencyCheck(int consistency) {
12053 return onConsistencyCheck(consistency);
12054 }
12055
12056 /**
12057 * Method that subclasses should implement to check their consistency. The type of
12058 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012059 *
Romain Guy13922e02009-05-12 17:56:14 -070012060 * @param consistency The type of consistency. See ViewDebug for more information.
12061 *
12062 * @throws IllegalStateException if the view is in an inconsistent state.
12063 *
12064 * @hide
12065 */
12066 protected boolean onConsistencyCheck(int consistency) {
12067 boolean result = true;
12068
12069 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12070 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12071
12072 if (checkLayout) {
12073 if (getParent() == null) {
12074 result = false;
12075 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12076 "View " + this + " does not have a parent.");
12077 }
12078
12079 if (mAttachInfo == null) {
12080 result = false;
12081 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12082 "View " + this + " is not attached to a window.");
12083 }
12084 }
12085
12086 if (checkDrawing) {
12087 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12088 // from their draw() method
12089
12090 if ((mPrivateFlags & DRAWN) != DRAWN &&
12091 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12092 result = false;
12093 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12094 "View " + this + " was invalidated but its drawing cache is valid.");
12095 }
12096 }
12097
12098 return result;
12099 }
12100
12101 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012102 * Prints information about this view in the log output, with the tag
12103 * {@link #VIEW_LOG_TAG}.
12104 *
12105 * @hide
12106 */
12107 public void debug() {
12108 debug(0);
12109 }
12110
12111 /**
12112 * Prints information about this view in the log output, with the tag
12113 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12114 * indentation defined by the <code>depth</code>.
12115 *
12116 * @param depth the indentation level
12117 *
12118 * @hide
12119 */
12120 protected void debug(int depth) {
12121 String output = debugIndent(depth - 1);
12122
12123 output += "+ " + this;
12124 int id = getId();
12125 if (id != -1) {
12126 output += " (id=" + id + ")";
12127 }
12128 Object tag = getTag();
12129 if (tag != null) {
12130 output += " (tag=" + tag + ")";
12131 }
12132 Log.d(VIEW_LOG_TAG, output);
12133
12134 if ((mPrivateFlags & FOCUSED) != 0) {
12135 output = debugIndent(depth) + " FOCUSED";
12136 Log.d(VIEW_LOG_TAG, output);
12137 }
12138
12139 output = debugIndent(depth);
12140 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12141 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12142 + "} ";
12143 Log.d(VIEW_LOG_TAG, output);
12144
12145 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12146 || mPaddingBottom != 0) {
12147 output = debugIndent(depth);
12148 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12149 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12150 Log.d(VIEW_LOG_TAG, output);
12151 }
12152
12153 output = debugIndent(depth);
12154 output += "mMeasureWidth=" + mMeasuredWidth +
12155 " mMeasureHeight=" + mMeasuredHeight;
12156 Log.d(VIEW_LOG_TAG, output);
12157
12158 output = debugIndent(depth);
12159 if (mLayoutParams == null) {
12160 output += "BAD! no layout params";
12161 } else {
12162 output = mLayoutParams.debug(output);
12163 }
12164 Log.d(VIEW_LOG_TAG, output);
12165
12166 output = debugIndent(depth);
12167 output += "flags={";
12168 output += View.printFlags(mViewFlags);
12169 output += "}";
12170 Log.d(VIEW_LOG_TAG, output);
12171
12172 output = debugIndent(depth);
12173 output += "privateFlags={";
12174 output += View.printPrivateFlags(mPrivateFlags);
12175 output += "}";
12176 Log.d(VIEW_LOG_TAG, output);
12177 }
12178
12179 /**
12180 * Creates an string of whitespaces used for indentation.
12181 *
12182 * @param depth the indentation level
12183 * @return a String containing (depth * 2 + 3) * 2 white spaces
12184 *
12185 * @hide
12186 */
12187 protected static String debugIndent(int depth) {
12188 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12189 for (int i = 0; i < (depth * 2) + 3; i++) {
12190 spaces.append(' ').append(' ');
12191 }
12192 return spaces.toString();
12193 }
12194
12195 /**
12196 * <p>Return the offset of the widget's text baseline from the widget's top
12197 * boundary. If this widget does not support baseline alignment, this
12198 * method returns -1. </p>
12199 *
12200 * @return the offset of the baseline within the widget's bounds or -1
12201 * if baseline alignment is not supported
12202 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012203 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012204 public int getBaseline() {
12205 return -1;
12206 }
12207
12208 /**
12209 * Call this when something has changed which has invalidated the
12210 * layout of this view. This will schedule a layout pass of the view
12211 * tree.
12212 */
12213 public void requestLayout() {
12214 if (ViewDebug.TRACE_HIERARCHY) {
12215 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12216 }
12217
12218 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012219 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012220
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012221 if (mParent != null) {
12222 if (mLayoutParams != null) {
12223 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12224 }
12225 if (!mParent.isLayoutRequested()) {
12226 mParent.requestLayout();
12227 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012228 }
12229 }
12230
12231 /**
12232 * Forces this view to be laid out during the next layout pass.
12233 * This method does not call requestLayout() or forceLayout()
12234 * on the parent.
12235 */
12236 public void forceLayout() {
12237 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012238 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012239 }
12240
12241 /**
12242 * <p>
12243 * This is called to find out how big a view should be. The parent
12244 * supplies constraint information in the width and height parameters.
12245 * </p>
12246 *
12247 * <p>
12248 * The actual mesurement work of a view is performed in
12249 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12250 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12251 * </p>
12252 *
12253 *
12254 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12255 * parent
12256 * @param heightMeasureSpec Vertical space requirements as imposed by the
12257 * parent
12258 *
12259 * @see #onMeasure(int, int)
12260 */
12261 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12262 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12263 widthMeasureSpec != mOldWidthMeasureSpec ||
12264 heightMeasureSpec != mOldHeightMeasureSpec) {
12265
12266 // first clears the measured dimension flag
12267 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12268
12269 if (ViewDebug.TRACE_HIERARCHY) {
12270 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12271 }
12272
12273 // measure ourselves, this should set the measured dimension flag back
12274 onMeasure(widthMeasureSpec, heightMeasureSpec);
12275
12276 // flag not set, setMeasuredDimension() was not invoked, we raise
12277 // an exception to warn the developer
12278 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12279 throw new IllegalStateException("onMeasure() did not set the"
12280 + " measured dimension by calling"
12281 + " setMeasuredDimension()");
12282 }
12283
12284 mPrivateFlags |= LAYOUT_REQUIRED;
12285 }
12286
12287 mOldWidthMeasureSpec = widthMeasureSpec;
12288 mOldHeightMeasureSpec = heightMeasureSpec;
12289 }
12290
12291 /**
12292 * <p>
12293 * Measure the view and its content to determine the measured width and the
12294 * measured height. This method is invoked by {@link #measure(int, int)} and
12295 * should be overriden by subclasses to provide accurate and efficient
12296 * measurement of their contents.
12297 * </p>
12298 *
12299 * <p>
12300 * <strong>CONTRACT:</strong> When overriding this method, you
12301 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12302 * measured width and height of this view. Failure to do so will trigger an
12303 * <code>IllegalStateException</code>, thrown by
12304 * {@link #measure(int, int)}. Calling the superclass'
12305 * {@link #onMeasure(int, int)} is a valid use.
12306 * </p>
12307 *
12308 * <p>
12309 * The base class implementation of measure defaults to the background size,
12310 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12311 * override {@link #onMeasure(int, int)} to provide better measurements of
12312 * their content.
12313 * </p>
12314 *
12315 * <p>
12316 * If this method is overridden, it is the subclass's responsibility to make
12317 * sure the measured height and width are at least the view's minimum height
12318 * and width ({@link #getSuggestedMinimumHeight()} and
12319 * {@link #getSuggestedMinimumWidth()}).
12320 * </p>
12321 *
12322 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12323 * The requirements are encoded with
12324 * {@link android.view.View.MeasureSpec}.
12325 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12326 * The requirements are encoded with
12327 * {@link android.view.View.MeasureSpec}.
12328 *
12329 * @see #getMeasuredWidth()
12330 * @see #getMeasuredHeight()
12331 * @see #setMeasuredDimension(int, int)
12332 * @see #getSuggestedMinimumHeight()
12333 * @see #getSuggestedMinimumWidth()
12334 * @see android.view.View.MeasureSpec#getMode(int)
12335 * @see android.view.View.MeasureSpec#getSize(int)
12336 */
12337 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12338 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12339 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12340 }
12341
12342 /**
12343 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12344 * measured width and measured height. Failing to do so will trigger an
12345 * exception at measurement time.</p>
12346 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012347 * @param measuredWidth The measured width of this view. May be a complex
12348 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12349 * {@link #MEASURED_STATE_TOO_SMALL}.
12350 * @param measuredHeight The measured height of this view. May be a complex
12351 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12352 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012353 */
12354 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12355 mMeasuredWidth = measuredWidth;
12356 mMeasuredHeight = measuredHeight;
12357
12358 mPrivateFlags |= MEASURED_DIMENSION_SET;
12359 }
12360
12361 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012362 * Merge two states as returned by {@link #getMeasuredState()}.
12363 * @param curState The current state as returned from a view or the result
12364 * of combining multiple views.
12365 * @param newState The new view state to combine.
12366 * @return Returns a new integer reflecting the combination of the two
12367 * states.
12368 */
12369 public static int combineMeasuredStates(int curState, int newState) {
12370 return curState | newState;
12371 }
12372
12373 /**
12374 * Version of {@link #resolveSizeAndState(int, int, int)}
12375 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12376 */
12377 public static int resolveSize(int size, int measureSpec) {
12378 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12379 }
12380
12381 /**
12382 * Utility to reconcile a desired size and state, with constraints imposed
12383 * by a MeasureSpec. Will take the desired size, unless a different size
12384 * is imposed by the constraints. The returned value is a compound integer,
12385 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12386 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12387 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012388 *
12389 * @param size How big the view wants to be
12390 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012391 * @return Size information bit mask as defined by
12392 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012393 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012394 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012395 int result = size;
12396 int specMode = MeasureSpec.getMode(measureSpec);
12397 int specSize = MeasureSpec.getSize(measureSpec);
12398 switch (specMode) {
12399 case MeasureSpec.UNSPECIFIED:
12400 result = size;
12401 break;
12402 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012403 if (specSize < size) {
12404 result = specSize | MEASURED_STATE_TOO_SMALL;
12405 } else {
12406 result = size;
12407 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012408 break;
12409 case MeasureSpec.EXACTLY:
12410 result = specSize;
12411 break;
12412 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012413 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012414 }
12415
12416 /**
12417 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012418 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012419 * by the MeasureSpec.
12420 *
12421 * @param size Default size for this view
12422 * @param measureSpec Constraints imposed by the parent
12423 * @return The size this view should be.
12424 */
12425 public static int getDefaultSize(int size, int measureSpec) {
12426 int result = size;
12427 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012428 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012429
12430 switch (specMode) {
12431 case MeasureSpec.UNSPECIFIED:
12432 result = size;
12433 break;
12434 case MeasureSpec.AT_MOST:
12435 case MeasureSpec.EXACTLY:
12436 result = specSize;
12437 break;
12438 }
12439 return result;
12440 }
12441
12442 /**
12443 * Returns the suggested minimum height that the view should use. This
12444 * returns the maximum of the view's minimum height
12445 * and the background's minimum height
12446 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12447 * <p>
12448 * When being used in {@link #onMeasure(int, int)}, the caller should still
12449 * ensure the returned height is within the requirements of the parent.
12450 *
12451 * @return The suggested minimum height of the view.
12452 */
12453 protected int getSuggestedMinimumHeight() {
12454 int suggestedMinHeight = mMinHeight;
12455
12456 if (mBGDrawable != null) {
12457 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12458 if (suggestedMinHeight < bgMinHeight) {
12459 suggestedMinHeight = bgMinHeight;
12460 }
12461 }
12462
12463 return suggestedMinHeight;
12464 }
12465
12466 /**
12467 * Returns the suggested minimum width that the view should use. This
12468 * returns the maximum of the view's minimum width)
12469 * and the background's minimum width
12470 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12471 * <p>
12472 * When being used in {@link #onMeasure(int, int)}, the caller should still
12473 * ensure the returned width is within the requirements of the parent.
12474 *
12475 * @return The suggested minimum width of the view.
12476 */
12477 protected int getSuggestedMinimumWidth() {
12478 int suggestedMinWidth = mMinWidth;
12479
12480 if (mBGDrawable != null) {
12481 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12482 if (suggestedMinWidth < bgMinWidth) {
12483 suggestedMinWidth = bgMinWidth;
12484 }
12485 }
12486
12487 return suggestedMinWidth;
12488 }
12489
12490 /**
12491 * Sets the minimum height of the view. It is not guaranteed the view will
12492 * be able to achieve this minimum height (for example, if its parent layout
12493 * constrains it with less available height).
12494 *
12495 * @param minHeight The minimum height the view will try to be.
12496 */
12497 public void setMinimumHeight(int minHeight) {
12498 mMinHeight = minHeight;
12499 }
12500
12501 /**
12502 * Sets the minimum width of the view. It is not guaranteed the view will
12503 * be able to achieve this minimum width (for example, if its parent layout
12504 * constrains it with less available width).
12505 *
12506 * @param minWidth The minimum width the view will try to be.
12507 */
12508 public void setMinimumWidth(int minWidth) {
12509 mMinWidth = minWidth;
12510 }
12511
12512 /**
12513 * Get the animation currently associated with this view.
12514 *
12515 * @return The animation that is currently playing or
12516 * scheduled to play for this view.
12517 */
12518 public Animation getAnimation() {
12519 return mCurrentAnimation;
12520 }
12521
12522 /**
12523 * Start the specified animation now.
12524 *
12525 * @param animation the animation to start now
12526 */
12527 public void startAnimation(Animation animation) {
12528 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12529 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012530 invalidateParentCaches();
12531 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012532 }
12533
12534 /**
12535 * Cancels any animations for this view.
12536 */
12537 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012538 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012539 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012540 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012541 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012542 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012543 }
12544
12545 /**
12546 * Sets the next animation to play for this view.
12547 * If you want the animation to play immediately, use
12548 * startAnimation. This method provides allows fine-grained
12549 * control over the start time and invalidation, but you
12550 * must make sure that 1) the animation has a start time set, and
12551 * 2) the view will be invalidated when the animation is supposed to
12552 * start.
12553 *
12554 * @param animation The next animation, or null.
12555 */
12556 public void setAnimation(Animation animation) {
12557 mCurrentAnimation = animation;
12558 if (animation != null) {
12559 animation.reset();
12560 }
12561 }
12562
12563 /**
12564 * Invoked by a parent ViewGroup to notify the start of the animation
12565 * currently associated with this view. If you override this method,
12566 * always call super.onAnimationStart();
12567 *
12568 * @see #setAnimation(android.view.animation.Animation)
12569 * @see #getAnimation()
12570 */
12571 protected void onAnimationStart() {
12572 mPrivateFlags |= ANIMATION_STARTED;
12573 }
12574
12575 /**
12576 * Invoked by a parent ViewGroup to notify the end of the animation
12577 * currently associated with this view. If you override this method,
12578 * always call super.onAnimationEnd();
12579 *
12580 * @see #setAnimation(android.view.animation.Animation)
12581 * @see #getAnimation()
12582 */
12583 protected void onAnimationEnd() {
12584 mPrivateFlags &= ~ANIMATION_STARTED;
12585 }
12586
12587 /**
12588 * Invoked if there is a Transform that involves alpha. Subclass that can
12589 * draw themselves with the specified alpha should return true, and then
12590 * respect that alpha when their onDraw() is called. If this returns false
12591 * then the view may be redirected to draw into an offscreen buffer to
12592 * fulfill the request, which will look fine, but may be slower than if the
12593 * subclass handles it internally. The default implementation returns false.
12594 *
12595 * @param alpha The alpha (0..255) to apply to the view's drawing
12596 * @return true if the view can draw with the specified alpha.
12597 */
12598 protected boolean onSetAlpha(int alpha) {
12599 return false;
12600 }
12601
12602 /**
12603 * This is used by the RootView to perform an optimization when
12604 * the view hierarchy contains one or several SurfaceView.
12605 * SurfaceView is always considered transparent, but its children are not,
12606 * therefore all View objects remove themselves from the global transparent
12607 * region (passed as a parameter to this function).
12608 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012609 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012610 *
12611 * @return Returns true if the effective visibility of the view at this
12612 * point is opaque, regardless of the transparent region; returns false
12613 * if it is possible for underlying windows to be seen behind the view.
12614 *
12615 * {@hide}
12616 */
12617 public boolean gatherTransparentRegion(Region region) {
12618 final AttachInfo attachInfo = mAttachInfo;
12619 if (region != null && attachInfo != null) {
12620 final int pflags = mPrivateFlags;
12621 if ((pflags & SKIP_DRAW) == 0) {
12622 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12623 // remove it from the transparent region.
12624 final int[] location = attachInfo.mTransparentLocation;
12625 getLocationInWindow(location);
12626 region.op(location[0], location[1], location[0] + mRight - mLeft,
12627 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12628 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12629 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12630 // exists, so we remove the background drawable's non-transparent
12631 // parts from this transparent region.
12632 applyDrawableToTransparentRegion(mBGDrawable, region);
12633 }
12634 }
12635 return true;
12636 }
12637
12638 /**
12639 * Play a sound effect for this view.
12640 *
12641 * <p>The framework will play sound effects for some built in actions, such as
12642 * clicking, but you may wish to play these effects in your widget,
12643 * for instance, for internal navigation.
12644 *
12645 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12646 * {@link #isSoundEffectsEnabled()} is true.
12647 *
12648 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12649 */
12650 public void playSoundEffect(int soundConstant) {
12651 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12652 return;
12653 }
12654 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12655 }
12656
12657 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012658 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012659 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012660 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012661 *
12662 * <p>The framework will provide haptic feedback for some built in actions,
12663 * such as long presses, but you may wish to provide feedback for your
12664 * own widget.
12665 *
12666 * <p>The feedback will only be performed if
12667 * {@link #isHapticFeedbackEnabled()} is true.
12668 *
12669 * @param feedbackConstant One of the constants defined in
12670 * {@link HapticFeedbackConstants}
12671 */
12672 public boolean performHapticFeedback(int feedbackConstant) {
12673 return performHapticFeedback(feedbackConstant, 0);
12674 }
12675
12676 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012677 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012678 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012679 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012680 *
12681 * @param feedbackConstant One of the constants defined in
12682 * {@link HapticFeedbackConstants}
12683 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12684 */
12685 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12686 if (mAttachInfo == null) {
12687 return false;
12688 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012689 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012690 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012691 && !isHapticFeedbackEnabled()) {
12692 return false;
12693 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012694 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12695 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012696 }
12697
12698 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012699 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012700 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12701 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Daniel Sandlerfad95552011-08-17 14:06:16 -040012702 *
12703 * This value will be re-applied immediately, even if the flags have not changed, so a view may
12704 * easily reassert a particular SystemUiVisibility condition even if the system UI itself has
12705 * since countermanded the original request.
Joe Onorato664644d2011-01-23 17:53:23 -080012706 */
12707 public void setSystemUiVisibility(int visibility) {
Daniel Sandlerfad95552011-08-17 14:06:16 -040012708 mSystemUiVisibility = visibility;
12709 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12710 mParent.recomputeViewAttributes(this);
Joe Onorato664644d2011-01-23 17:53:23 -080012711 }
12712 }
12713
12714 /**
12715 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012716 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12717 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012718 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012719 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012720 return mSystemUiVisibility;
12721 }
12722
Scott Mainec6331b2011-05-24 16:55:56 -070012723 /**
12724 * Set a listener to receive callbacks when the visibility of the system bar changes.
12725 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12726 */
Joe Onorato664644d2011-01-23 17:53:23 -080012727 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12728 mOnSystemUiVisibilityChangeListener = l;
12729 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12730 mParent.recomputeViewAttributes(this);
12731 }
12732 }
12733
12734 /**
12735 */
12736 public void dispatchSystemUiVisibilityChanged(int visibility) {
12737 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012738 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012739 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012740 }
12741 }
12742
12743 /**
Joe Malin32736f02011-01-19 16:14:20 -080012744 * Creates an image that the system displays during the drag and drop
12745 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12746 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12747 * appearance as the given View. The default also positions the center of the drag shadow
12748 * directly under the touch point. If no View is provided (the constructor with no parameters
12749 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12750 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12751 * default is an invisible drag shadow.
12752 * <p>
12753 * You are not required to use the View you provide to the constructor as the basis of the
12754 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12755 * anything you want as the drag shadow.
12756 * </p>
12757 * <p>
12758 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12759 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12760 * size and position of the drag shadow. It uses this data to construct a
12761 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12762 * so that your application can draw the shadow image in the Canvas.
12763 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012764 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012765 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012766 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012767
12768 /**
Joe Malin32736f02011-01-19 16:14:20 -080012769 * Constructs a shadow image builder based on a View. By default, the resulting drag
12770 * shadow will have the same appearance and dimensions as the View, with the touch point
12771 * over the center of the View.
12772 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012773 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012774 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012775 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012776 }
12777
Christopher Tate17ed60c2011-01-18 12:50:26 -080012778 /**
12779 * Construct a shadow builder object with no associated View. This
12780 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12781 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12782 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012783 * reference to any View object. If they are not overridden, then the result is an
12784 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012785 */
12786 public DragShadowBuilder() {
12787 mView = new WeakReference<View>(null);
12788 }
12789
12790 /**
12791 * Returns the View object that had been passed to the
12792 * {@link #View.DragShadowBuilder(View)}
12793 * constructor. If that View parameter was {@code null} or if the
12794 * {@link #View.DragShadowBuilder()}
12795 * constructor was used to instantiate the builder object, this method will return
12796 * null.
12797 *
12798 * @return The View object associate with this builder object.
12799 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012800 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012801 final public View getView() {
12802 return mView.get();
12803 }
12804
Christopher Tate2c095f32010-10-04 14:13:40 -070012805 /**
Joe Malin32736f02011-01-19 16:14:20 -080012806 * Provides the metrics for the shadow image. These include the dimensions of
12807 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012808 * be centered under the touch location while dragging.
12809 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012810 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012811 * same as the dimensions of the View itself and centers the shadow under
12812 * the touch point.
12813 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012814 *
Joe Malin32736f02011-01-19 16:14:20 -080012815 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12816 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12817 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12818 * image.
12819 *
12820 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12821 * shadow image that should be underneath the touch point during the drag and drop
12822 * operation. Your application must set {@link android.graphics.Point#x} to the
12823 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012824 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012825 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012826 final View view = mView.get();
12827 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012828 shadowSize.set(view.getWidth(), view.getHeight());
12829 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012830 } else {
12831 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12832 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012833 }
12834
12835 /**
Joe Malin32736f02011-01-19 16:14:20 -080012836 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12837 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012838 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012839 *
Joe Malin32736f02011-01-19 16:14:20 -080012840 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012841 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012842 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012843 final View view = mView.get();
12844 if (view != null) {
12845 view.draw(canvas);
12846 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012847 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012848 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012849 }
12850 }
12851
12852 /**
Joe Malin32736f02011-01-19 16:14:20 -080012853 * Starts a drag and drop operation. When your application calls this method, it passes a
12854 * {@link android.view.View.DragShadowBuilder} object to the system. The
12855 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12856 * to get metrics for the drag shadow, and then calls the object's
12857 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12858 * <p>
12859 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12860 * drag events to all the View objects in your application that are currently visible. It does
12861 * this either by calling the View object's drag listener (an implementation of
12862 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12863 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12864 * Both are passed a {@link android.view.DragEvent} object that has a
12865 * {@link android.view.DragEvent#getAction()} value of
12866 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12867 * </p>
12868 * <p>
12869 * Your application can invoke startDrag() on any attached View object. The View object does not
12870 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12871 * be related to the View the user selected for dragging.
12872 * </p>
12873 * @param data A {@link android.content.ClipData} object pointing to the data to be
12874 * transferred by the drag and drop operation.
12875 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12876 * drag shadow.
12877 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12878 * drop operation. This Object is put into every DragEvent object sent by the system during the
12879 * current drag.
12880 * <p>
12881 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12882 * to the target Views. For example, it can contain flags that differentiate between a
12883 * a copy operation and a move operation.
12884 * </p>
12885 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12886 * so the parameter should be set to 0.
12887 * @return {@code true} if the method completes successfully, or
12888 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12889 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012890 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012891 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012892 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012893 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012894 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012895 }
12896 boolean okay = false;
12897
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012898 Point shadowSize = new Point();
12899 Point shadowTouchPoint = new Point();
12900 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012901
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012902 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12903 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12904 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012905 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012906
Chris Tatea32dcf72010-10-14 12:13:50 -070012907 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012908 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12909 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012910 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012911 Surface surface = new Surface();
12912 try {
12913 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012914 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012915 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012916 + " surface=" + surface);
12917 if (token != null) {
12918 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012919 try {
Chris Tate6b391282010-10-14 15:48:59 -070012920 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012921 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012922 } finally {
12923 surface.unlockCanvasAndPost(canvas);
12924 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012925
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012926 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080012927
12928 // Cache the local state object for delivery with DragEvents
12929 root.setLocalDragState(myLocalState);
12930
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012931 // repurpose 'shadowSize' for the last touch point
12932 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012933
Christopher Tatea53146c2010-09-07 11:57:52 -070012934 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012935 shadowSize.x, shadowSize.y,
12936 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012937 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012938 }
12939 } catch (Exception e) {
12940 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12941 surface.destroy();
12942 }
12943
12944 return okay;
12945 }
12946
Christopher Tatea53146c2010-09-07 11:57:52 -070012947 /**
Joe Malin32736f02011-01-19 16:14:20 -080012948 * Handles drag events sent by the system following a call to
12949 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12950 *<p>
12951 * When the system calls this method, it passes a
12952 * {@link android.view.DragEvent} object. A call to
12953 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12954 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12955 * operation.
12956 * @param event The {@link android.view.DragEvent} sent by the system.
12957 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12958 * in DragEvent, indicating the type of drag event represented by this object.
12959 * @return {@code true} if the method was successful, otherwise {@code false}.
12960 * <p>
12961 * The method should return {@code true} in response to an action type of
12962 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12963 * operation.
12964 * </p>
12965 * <p>
12966 * The method should also return {@code true} in response to an action type of
12967 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12968 * {@code false} if it didn't.
12969 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012970 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012971 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012972 return false;
12973 }
12974
12975 /**
Joe Malin32736f02011-01-19 16:14:20 -080012976 * Detects if this View is enabled and has a drag event listener.
12977 * If both are true, then it calls the drag event listener with the
12978 * {@link android.view.DragEvent} it received. If the drag event listener returns
12979 * {@code true}, then dispatchDragEvent() returns {@code true}.
12980 * <p>
12981 * For all other cases, the method calls the
12982 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12983 * method and returns its result.
12984 * </p>
12985 * <p>
12986 * This ensures that a drag event is always consumed, even if the View does not have a drag
12987 * event listener. However, if the View has a listener and the listener returns true, then
12988 * onDragEvent() is not called.
12989 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012990 */
12991 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012992 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012993 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12994 && mOnDragListener.onDrag(this, event)) {
12995 return true;
12996 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012997 return onDragEvent(event);
12998 }
12999
Christopher Tate3d4bf172011-03-28 16:16:46 -070013000 boolean canAcceptDrag() {
13001 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
13002 }
13003
Christopher Tatea53146c2010-09-07 11:57:52 -070013004 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013005 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13006 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013007 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013008 */
13009 public void onCloseSystemDialogs(String reason) {
13010 }
Joe Malin32736f02011-01-19 16:14:20 -080013011
Dianne Hackbornffa42482009-09-23 22:20:11 -070013012 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013014 * update a Region being computed for
13015 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013016 * that any non-transparent parts of the Drawable are removed from the
13017 * given transparent region.
13018 *
13019 * @param dr The Drawable whose transparency is to be applied to the region.
13020 * @param region A Region holding the current transparency information,
13021 * where any parts of the region that are set are considered to be
13022 * transparent. On return, this region will be modified to have the
13023 * transparency information reduced by the corresponding parts of the
13024 * Drawable that are not transparent.
13025 * {@hide}
13026 */
13027 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13028 if (DBG) {
13029 Log.i("View", "Getting transparent region for: " + this);
13030 }
13031 final Region r = dr.getTransparentRegion();
13032 final Rect db = dr.getBounds();
13033 final AttachInfo attachInfo = mAttachInfo;
13034 if (r != null && attachInfo != null) {
13035 final int w = getRight()-getLeft();
13036 final int h = getBottom()-getTop();
13037 if (db.left > 0) {
13038 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13039 r.op(0, 0, db.left, h, Region.Op.UNION);
13040 }
13041 if (db.right < w) {
13042 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13043 r.op(db.right, 0, w, h, Region.Op.UNION);
13044 }
13045 if (db.top > 0) {
13046 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13047 r.op(0, 0, w, db.top, Region.Op.UNION);
13048 }
13049 if (db.bottom < h) {
13050 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13051 r.op(0, db.bottom, w, h, Region.Op.UNION);
13052 }
13053 final int[] location = attachInfo.mTransparentLocation;
13054 getLocationInWindow(location);
13055 r.translate(location[0], location[1]);
13056 region.op(r, Region.Op.INTERSECT);
13057 } else {
13058 region.op(db, Region.Op.DIFFERENCE);
13059 }
13060 }
13061
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013062 private void checkForLongClick(int delayOffset) {
13063 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13064 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013065
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013066 if (mPendingCheckForLongPress == null) {
13067 mPendingCheckForLongPress = new CheckForLongPress();
13068 }
13069 mPendingCheckForLongPress.rememberWindowAttachCount();
13070 postDelayed(mPendingCheckForLongPress,
13071 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013073 }
13074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013075 /**
13076 * Inflate a view from an XML resource. This convenience method wraps the {@link
13077 * LayoutInflater} class, which provides a full range of options for view inflation.
13078 *
13079 * @param context The Context object for your activity or application.
13080 * @param resource The resource ID to inflate
13081 * @param root A view group that will be the parent. Used to properly inflate the
13082 * layout_* parameters.
13083 * @see LayoutInflater
13084 */
13085 public static View inflate(Context context, int resource, ViewGroup root) {
13086 LayoutInflater factory = LayoutInflater.from(context);
13087 return factory.inflate(resource, root);
13088 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013090 /**
Adam Powell637d3372010-08-25 14:37:03 -070013091 * Scroll the view with standard behavior for scrolling beyond the normal
13092 * content boundaries. Views that call this method should override
13093 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13094 * results of an over-scroll operation.
13095 *
13096 * Views can use this method to handle any touch or fling-based scrolling.
13097 *
13098 * @param deltaX Change in X in pixels
13099 * @param deltaY Change in Y in pixels
13100 * @param scrollX Current X scroll value in pixels before applying deltaX
13101 * @param scrollY Current Y scroll value in pixels before applying deltaY
13102 * @param scrollRangeX Maximum content scroll range along the X axis
13103 * @param scrollRangeY Maximum content scroll range along the Y axis
13104 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13105 * along the X axis.
13106 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13107 * along the Y axis.
13108 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13109 * @return true if scrolling was clamped to an over-scroll boundary along either
13110 * axis, false otherwise.
13111 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013112 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013113 protected boolean overScrollBy(int deltaX, int deltaY,
13114 int scrollX, int scrollY,
13115 int scrollRangeX, int scrollRangeY,
13116 int maxOverScrollX, int maxOverScrollY,
13117 boolean isTouchEvent) {
13118 final int overScrollMode = mOverScrollMode;
13119 final boolean canScrollHorizontal =
13120 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13121 final boolean canScrollVertical =
13122 computeVerticalScrollRange() > computeVerticalScrollExtent();
13123 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13124 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13125 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13126 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13127
13128 int newScrollX = scrollX + deltaX;
13129 if (!overScrollHorizontal) {
13130 maxOverScrollX = 0;
13131 }
13132
13133 int newScrollY = scrollY + deltaY;
13134 if (!overScrollVertical) {
13135 maxOverScrollY = 0;
13136 }
13137
13138 // Clamp values if at the limits and record
13139 final int left = -maxOverScrollX;
13140 final int right = maxOverScrollX + scrollRangeX;
13141 final int top = -maxOverScrollY;
13142 final int bottom = maxOverScrollY + scrollRangeY;
13143
13144 boolean clampedX = false;
13145 if (newScrollX > right) {
13146 newScrollX = right;
13147 clampedX = true;
13148 } else if (newScrollX < left) {
13149 newScrollX = left;
13150 clampedX = true;
13151 }
13152
13153 boolean clampedY = false;
13154 if (newScrollY > bottom) {
13155 newScrollY = bottom;
13156 clampedY = true;
13157 } else if (newScrollY < top) {
13158 newScrollY = top;
13159 clampedY = true;
13160 }
13161
13162 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13163
13164 return clampedX || clampedY;
13165 }
13166
13167 /**
13168 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13169 * respond to the results of an over-scroll operation.
13170 *
13171 * @param scrollX New X scroll value in pixels
13172 * @param scrollY New Y scroll value in pixels
13173 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13174 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13175 */
13176 protected void onOverScrolled(int scrollX, int scrollY,
13177 boolean clampedX, boolean clampedY) {
13178 // Intentionally empty.
13179 }
13180
13181 /**
13182 * Returns the over-scroll mode for this view. The result will be
13183 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13184 * (allow over-scrolling only if the view content is larger than the container),
13185 * or {@link #OVER_SCROLL_NEVER}.
13186 *
13187 * @return This view's over-scroll mode.
13188 */
13189 public int getOverScrollMode() {
13190 return mOverScrollMode;
13191 }
13192
13193 /**
13194 * Set the over-scroll mode for this view. Valid over-scroll modes are
13195 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13196 * (allow over-scrolling only if the view content is larger than the container),
13197 * or {@link #OVER_SCROLL_NEVER}.
13198 *
13199 * Setting the over-scroll mode of a view will have an effect only if the
13200 * view is capable of scrolling.
13201 *
13202 * @param overScrollMode The new over-scroll mode for this view.
13203 */
13204 public void setOverScrollMode(int overScrollMode) {
13205 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13206 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13207 overScrollMode != OVER_SCROLL_NEVER) {
13208 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13209 }
13210 mOverScrollMode = overScrollMode;
13211 }
13212
13213 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013214 * Gets a scale factor that determines the distance the view should scroll
13215 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13216 * @return The vertical scroll scale factor.
13217 * @hide
13218 */
13219 protected float getVerticalScrollFactor() {
13220 if (mVerticalScrollFactor == 0) {
13221 TypedValue outValue = new TypedValue();
13222 if (!mContext.getTheme().resolveAttribute(
13223 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13224 throw new IllegalStateException(
13225 "Expected theme to define listPreferredItemHeight.");
13226 }
13227 mVerticalScrollFactor = outValue.getDimension(
13228 mContext.getResources().getDisplayMetrics());
13229 }
13230 return mVerticalScrollFactor;
13231 }
13232
13233 /**
13234 * Gets a scale factor that determines the distance the view should scroll
13235 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13236 * @return The horizontal scroll scale factor.
13237 * @hide
13238 */
13239 protected float getHorizontalScrollFactor() {
13240 // TODO: Should use something else.
13241 return getVerticalScrollFactor();
13242 }
13243
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013244 /**
13245 * Return the value specifying the text direction or policy that was set with
13246 * {@link #setTextDirection(int)}.
13247 *
13248 * @return the defined text direction. It can be one of:
13249 *
13250 * {@link #TEXT_DIRECTION_INHERIT},
13251 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13252 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013253 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013254 * {@link #TEXT_DIRECTION_LTR},
13255 * {@link #TEXT_DIRECTION_RTL},
13256 *
13257 * @hide
13258 */
13259 public int getTextDirection() {
13260 return mTextDirection;
13261 }
13262
13263 /**
13264 * Set the text direction.
13265 *
13266 * @param textDirection the direction to set. Should be one of:
13267 *
13268 * {@link #TEXT_DIRECTION_INHERIT},
13269 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13270 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013271 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013272 * {@link #TEXT_DIRECTION_LTR},
13273 * {@link #TEXT_DIRECTION_RTL},
13274 *
13275 * @hide
13276 */
13277 public void setTextDirection(int textDirection) {
13278 if (textDirection != mTextDirection) {
13279 mTextDirection = textDirection;
13280 resetResolvedTextDirection();
13281 requestLayout();
13282 }
13283 }
13284
13285 /**
13286 * Return the resolved text direction.
13287 *
13288 * @return the resolved text direction. Return one of:
13289 *
Doug Feltcb3791202011-07-07 11:57:48 -070013290 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13291 * {@link #TEXT_DIRECTION_ANY_RTL},
13292 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013293 * {@link #TEXT_DIRECTION_LTR},
13294 * {@link #TEXT_DIRECTION_RTL},
13295 *
13296 * @hide
13297 */
13298 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013299 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013300 resolveTextDirection();
13301 }
13302 return mResolvedTextDirection;
13303 }
13304
13305 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013306 * Resolve the text direction.
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013307 *
13308 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013309 */
13310 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013311 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13312 mResolvedTextDirection = mTextDirection;
13313 return;
13314 }
13315 if (mParent != null && mParent instanceof ViewGroup) {
13316 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13317 return;
13318 }
13319 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013320 }
13321
13322 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013323 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio2273b1e2011-09-07 15:17:40 -070013324 *
13325 * @hide
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013326 */
13327 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013328 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013329 }
13330
Chet Haaseb39f0512011-05-24 14:36:40 -070013331 //
13332 // Properties
13333 //
13334 /**
13335 * A Property wrapper around the <code>alpha</code> functionality handled by the
13336 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13337 */
Romain Guy02ccac62011-06-24 13:20:23 -070013338 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013339 @Override
13340 public void setValue(View object, float value) {
13341 object.setAlpha(value);
13342 }
13343
13344 @Override
13345 public Float get(View object) {
13346 return object.getAlpha();
13347 }
13348 };
13349
13350 /**
13351 * A Property wrapper around the <code>translationX</code> functionality handled by the
13352 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13353 */
13354 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13355 @Override
13356 public void setValue(View object, float value) {
13357 object.setTranslationX(value);
13358 }
13359
13360 @Override
13361 public Float get(View object) {
13362 return object.getTranslationX();
13363 }
13364 };
13365
13366 /**
13367 * A Property wrapper around the <code>translationY</code> functionality handled by the
13368 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13369 */
13370 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13371 @Override
13372 public void setValue(View object, float value) {
13373 object.setTranslationY(value);
13374 }
13375
13376 @Override
13377 public Float get(View object) {
13378 return object.getTranslationY();
13379 }
13380 };
13381
13382 /**
13383 * A Property wrapper around the <code>x</code> functionality handled by the
13384 * {@link View#setX(float)} and {@link View#getX()} methods.
13385 */
13386 public static Property<View, Float> X = new FloatProperty<View>("x") {
13387 @Override
13388 public void setValue(View object, float value) {
13389 object.setX(value);
13390 }
13391
13392 @Override
13393 public Float get(View object) {
13394 return object.getX();
13395 }
13396 };
13397
13398 /**
13399 * A Property wrapper around the <code>y</code> functionality handled by the
13400 * {@link View#setY(float)} and {@link View#getY()} methods.
13401 */
13402 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13403 @Override
13404 public void setValue(View object, float value) {
13405 object.setY(value);
13406 }
13407
13408 @Override
13409 public Float get(View object) {
13410 return object.getY();
13411 }
13412 };
13413
13414 /**
13415 * A Property wrapper around the <code>rotation</code> functionality handled by the
13416 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13417 */
13418 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13419 @Override
13420 public void setValue(View object, float value) {
13421 object.setRotation(value);
13422 }
13423
13424 @Override
13425 public Float get(View object) {
13426 return object.getRotation();
13427 }
13428 };
13429
13430 /**
13431 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13432 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13433 */
13434 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13435 @Override
13436 public void setValue(View object, float value) {
13437 object.setRotationX(value);
13438 }
13439
13440 @Override
13441 public Float get(View object) {
13442 return object.getRotationX();
13443 }
13444 };
13445
13446 /**
13447 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13448 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13449 */
13450 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13451 @Override
13452 public void setValue(View object, float value) {
13453 object.setRotationY(value);
13454 }
13455
13456 @Override
13457 public Float get(View object) {
13458 return object.getRotationY();
13459 }
13460 };
13461
13462 /**
13463 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13464 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13465 */
13466 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13467 @Override
13468 public void setValue(View object, float value) {
13469 object.setScaleX(value);
13470 }
13471
13472 @Override
13473 public Float get(View object) {
13474 return object.getScaleX();
13475 }
13476 };
13477
13478 /**
13479 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13480 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13481 */
13482 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13483 @Override
13484 public void setValue(View object, float value) {
13485 object.setScaleY(value);
13486 }
13487
13488 @Override
13489 public Float get(View object) {
13490 return object.getScaleY();
13491 }
13492 };
13493
Jeff Brown33bbfd22011-02-24 20:55:35 -080013494 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013495 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13496 * Each MeasureSpec represents a requirement for either the width or the height.
13497 * A MeasureSpec is comprised of a size and a mode. There are three possible
13498 * modes:
13499 * <dl>
13500 * <dt>UNSPECIFIED</dt>
13501 * <dd>
13502 * The parent has not imposed any constraint on the child. It can be whatever size
13503 * it wants.
13504 * </dd>
13505 *
13506 * <dt>EXACTLY</dt>
13507 * <dd>
13508 * The parent has determined an exact size for the child. The child is going to be
13509 * given those bounds regardless of how big it wants to be.
13510 * </dd>
13511 *
13512 * <dt>AT_MOST</dt>
13513 * <dd>
13514 * The child can be as large as it wants up to the specified size.
13515 * </dd>
13516 * </dl>
13517 *
13518 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13519 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13520 */
13521 public static class MeasureSpec {
13522 private static final int MODE_SHIFT = 30;
13523 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13524
13525 /**
13526 * Measure specification mode: The parent has not imposed any constraint
13527 * on the child. It can be whatever size it wants.
13528 */
13529 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13530
13531 /**
13532 * Measure specification mode: The parent has determined an exact size
13533 * for the child. The child is going to be given those bounds regardless
13534 * of how big it wants to be.
13535 */
13536 public static final int EXACTLY = 1 << MODE_SHIFT;
13537
13538 /**
13539 * Measure specification mode: The child can be as large as it wants up
13540 * to the specified size.
13541 */
13542 public static final int AT_MOST = 2 << MODE_SHIFT;
13543
13544 /**
13545 * Creates a measure specification based on the supplied size and mode.
13546 *
13547 * The mode must always be one of the following:
13548 * <ul>
13549 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13550 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13551 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13552 * </ul>
13553 *
13554 * @param size the size of the measure specification
13555 * @param mode the mode of the measure specification
13556 * @return the measure specification based on size and mode
13557 */
13558 public static int makeMeasureSpec(int size, int mode) {
13559 return size + mode;
13560 }
13561
13562 /**
13563 * Extracts the mode from the supplied measure specification.
13564 *
13565 * @param measureSpec the measure specification to extract the mode from
13566 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13567 * {@link android.view.View.MeasureSpec#AT_MOST} or
13568 * {@link android.view.View.MeasureSpec#EXACTLY}
13569 */
13570 public static int getMode(int measureSpec) {
13571 return (measureSpec & MODE_MASK);
13572 }
13573
13574 /**
13575 * Extracts the size from the supplied measure specification.
13576 *
13577 * @param measureSpec the measure specification to extract the size from
13578 * @return the size in pixels defined in the supplied measure specification
13579 */
13580 public static int getSize(int measureSpec) {
13581 return (measureSpec & ~MODE_MASK);
13582 }
13583
13584 /**
13585 * Returns a String representation of the specified measure
13586 * specification.
13587 *
13588 * @param measureSpec the measure specification to convert to a String
13589 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13590 */
13591 public static String toString(int measureSpec) {
13592 int mode = getMode(measureSpec);
13593 int size = getSize(measureSpec);
13594
13595 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13596
13597 if (mode == UNSPECIFIED)
13598 sb.append("UNSPECIFIED ");
13599 else if (mode == EXACTLY)
13600 sb.append("EXACTLY ");
13601 else if (mode == AT_MOST)
13602 sb.append("AT_MOST ");
13603 else
13604 sb.append(mode).append(" ");
13605
13606 sb.append(size);
13607 return sb.toString();
13608 }
13609 }
13610
13611 class CheckForLongPress implements Runnable {
13612
13613 private int mOriginalWindowAttachCount;
13614
13615 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013616 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013617 && mOriginalWindowAttachCount == mWindowAttachCount) {
13618 if (performLongClick()) {
13619 mHasPerformedLongPress = true;
13620 }
13621 }
13622 }
13623
13624 public void rememberWindowAttachCount() {
13625 mOriginalWindowAttachCount = mWindowAttachCount;
13626 }
13627 }
Joe Malin32736f02011-01-19 16:14:20 -080013628
Adam Powelle14579b2009-12-16 18:39:52 -080013629 private final class CheckForTap implements Runnable {
13630 public void run() {
13631 mPrivateFlags &= ~PREPRESSED;
13632 mPrivateFlags |= PRESSED;
13633 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013634 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013635 }
13636 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013637
Adam Powella35d7682010-03-12 14:48:13 -080013638 private final class PerformClick implements Runnable {
13639 public void run() {
13640 performClick();
13641 }
13642 }
13643
Dianne Hackborn63042d62011-01-26 18:56:29 -080013644 /** @hide */
13645 public void hackTurnOffWindowResizeAnim(boolean off) {
13646 mAttachInfo.mTurnOffWindowResizeAnim = off;
13647 }
Joe Malin32736f02011-01-19 16:14:20 -080013648
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013649 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013650 * This method returns a ViewPropertyAnimator object, which can be used to animate
13651 * specific properties on this View.
13652 *
13653 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13654 */
13655 public ViewPropertyAnimator animate() {
13656 if (mAnimator == null) {
13657 mAnimator = new ViewPropertyAnimator(this);
13658 }
13659 return mAnimator;
13660 }
13661
13662 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013663 * Interface definition for a callback to be invoked when a key event is
13664 * dispatched to this view. The callback will be invoked before the key
13665 * event is given to the view.
13666 */
13667 public interface OnKeyListener {
13668 /**
13669 * Called when a key is dispatched to a view. This allows listeners to
13670 * get a chance to respond before the target view.
13671 *
13672 * @param v The view the key has been dispatched to.
13673 * @param keyCode The code for the physical key that was pressed
13674 * @param event The KeyEvent object containing full information about
13675 * the event.
13676 * @return True if the listener has consumed the event, false otherwise.
13677 */
13678 boolean onKey(View v, int keyCode, KeyEvent event);
13679 }
13680
13681 /**
13682 * Interface definition for a callback to be invoked when a touch event is
13683 * dispatched to this view. The callback will be invoked before the touch
13684 * event is given to the view.
13685 */
13686 public interface OnTouchListener {
13687 /**
13688 * Called when a touch event is dispatched to a view. This allows listeners to
13689 * get a chance to respond before the target view.
13690 *
13691 * @param v The view the touch event has been dispatched to.
13692 * @param event The MotionEvent object containing full information about
13693 * the event.
13694 * @return True if the listener has consumed the event, false otherwise.
13695 */
13696 boolean onTouch(View v, MotionEvent event);
13697 }
13698
13699 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013700 * Interface definition for a callback to be invoked when a hover event is
13701 * dispatched to this view. The callback will be invoked before the hover
13702 * event is given to the view.
13703 */
13704 public interface OnHoverListener {
13705 /**
13706 * Called when a hover event is dispatched to a view. This allows listeners to
13707 * get a chance to respond before the target view.
13708 *
13709 * @param v The view the hover event has been dispatched to.
13710 * @param event The MotionEvent object containing full information about
13711 * the event.
13712 * @return True if the listener has consumed the event, false otherwise.
13713 */
13714 boolean onHover(View v, MotionEvent event);
13715 }
13716
13717 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013718 * Interface definition for a callback to be invoked when a generic motion event is
13719 * dispatched to this view. The callback will be invoked before the generic motion
13720 * event is given to the view.
13721 */
13722 public interface OnGenericMotionListener {
13723 /**
13724 * Called when a generic motion event is dispatched to a view. This allows listeners to
13725 * get a chance to respond before the target view.
13726 *
13727 * @param v The view the generic motion event has been dispatched to.
13728 * @param event The MotionEvent object containing full information about
13729 * the event.
13730 * @return True if the listener has consumed the event, false otherwise.
13731 */
13732 boolean onGenericMotion(View v, MotionEvent event);
13733 }
13734
13735 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013736 * Interface definition for a callback to be invoked when a view has been clicked and held.
13737 */
13738 public interface OnLongClickListener {
13739 /**
13740 * Called when a view has been clicked and held.
13741 *
13742 * @param v The view that was clicked and held.
13743 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013744 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013745 */
13746 boolean onLongClick(View v);
13747 }
13748
13749 /**
Chris Tate32affef2010-10-18 15:29:21 -070013750 * Interface definition for a callback to be invoked when a drag is being dispatched
13751 * to this view. The callback will be invoked before the hosting view's own
13752 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13753 * onDrag(event) behavior, it should return 'false' from this callback.
13754 */
13755 public interface OnDragListener {
13756 /**
13757 * Called when a drag event is dispatched to a view. This allows listeners
13758 * to get a chance to override base View behavior.
13759 *
Joe Malin32736f02011-01-19 16:14:20 -080013760 * @param v The View that received the drag event.
13761 * @param event The {@link android.view.DragEvent} object for the drag event.
13762 * @return {@code true} if the drag event was handled successfully, or {@code false}
13763 * if the drag event was not handled. Note that {@code false} will trigger the View
13764 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013765 */
13766 boolean onDrag(View v, DragEvent event);
13767 }
13768
13769 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013770 * Interface definition for a callback to be invoked when the focus state of
13771 * a view changed.
13772 */
13773 public interface OnFocusChangeListener {
13774 /**
13775 * Called when the focus state of a view has changed.
13776 *
13777 * @param v The view whose state has changed.
13778 * @param hasFocus The new focus state of v.
13779 */
13780 void onFocusChange(View v, boolean hasFocus);
13781 }
13782
13783 /**
13784 * Interface definition for a callback to be invoked when a view is clicked.
13785 */
13786 public interface OnClickListener {
13787 /**
13788 * Called when a view has been clicked.
13789 *
13790 * @param v The view that was clicked.
13791 */
13792 void onClick(View v);
13793 }
13794
13795 /**
13796 * Interface definition for a callback to be invoked when the context menu
13797 * for this view is being built.
13798 */
13799 public interface OnCreateContextMenuListener {
13800 /**
13801 * Called when the context menu for this view is being built. It is not
13802 * safe to hold onto the menu after this method returns.
13803 *
13804 * @param menu The context menu that is being built
13805 * @param v The view for which the context menu is being built
13806 * @param menuInfo Extra information about the item for which the
13807 * context menu should be shown. This information will vary
13808 * depending on the class of v.
13809 */
13810 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13811 }
13812
Joe Onorato664644d2011-01-23 17:53:23 -080013813 /**
13814 * Interface definition for a callback to be invoked when the status bar changes
13815 * visibility.
13816 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013817 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013818 */
13819 public interface OnSystemUiVisibilityChangeListener {
13820 /**
13821 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013822 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013823 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040013824 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13825 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013826 */
13827 public void onSystemUiVisibilityChange(int visibility);
13828 }
13829
Adam Powell4afd62b2011-02-18 15:02:18 -080013830 /**
13831 * Interface definition for a callback to be invoked when this view is attached
13832 * or detached from its window.
13833 */
13834 public interface OnAttachStateChangeListener {
13835 /**
13836 * Called when the view is attached to a window.
13837 * @param v The view that was attached
13838 */
13839 public void onViewAttachedToWindow(View v);
13840 /**
13841 * Called when the view is detached from a window.
13842 * @param v The view that was detached
13843 */
13844 public void onViewDetachedFromWindow(View v);
13845 }
13846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013847 private final class UnsetPressedState implements Runnable {
13848 public void run() {
13849 setPressed(false);
13850 }
13851 }
13852
13853 /**
13854 * Base class for derived classes that want to save and restore their own
13855 * state in {@link android.view.View#onSaveInstanceState()}.
13856 */
13857 public static class BaseSavedState extends AbsSavedState {
13858 /**
13859 * Constructor used when reading from a parcel. Reads the state of the superclass.
13860 *
13861 * @param source
13862 */
13863 public BaseSavedState(Parcel source) {
13864 super(source);
13865 }
13866
13867 /**
13868 * Constructor called by derived classes when creating their SavedState objects
13869 *
13870 * @param superState The state of the superclass of this view
13871 */
13872 public BaseSavedState(Parcelable superState) {
13873 super(superState);
13874 }
13875
13876 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13877 new Parcelable.Creator<BaseSavedState>() {
13878 public BaseSavedState createFromParcel(Parcel in) {
13879 return new BaseSavedState(in);
13880 }
13881
13882 public BaseSavedState[] newArray(int size) {
13883 return new BaseSavedState[size];
13884 }
13885 };
13886 }
13887
13888 /**
13889 * A set of information given to a view when it is attached to its parent
13890 * window.
13891 */
13892 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013893 interface Callbacks {
13894 void playSoundEffect(int effectId);
13895 boolean performHapticFeedback(int effectId, boolean always);
13896 }
13897
13898 /**
13899 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13900 * to a Handler. This class contains the target (View) to invalidate and
13901 * the coordinates of the dirty rectangle.
13902 *
13903 * For performance purposes, this class also implements a pool of up to
13904 * POOL_LIMIT objects that get reused. This reduces memory allocations
13905 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013906 */
Romain Guyd928d682009-03-31 17:52:16 -070013907 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013908 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013909 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13910 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013911 public InvalidateInfo newInstance() {
13912 return new InvalidateInfo();
13913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013914
Romain Guyd928d682009-03-31 17:52:16 -070013915 public void onAcquired(InvalidateInfo element) {
13916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013917
Romain Guyd928d682009-03-31 17:52:16 -070013918 public void onReleased(InvalidateInfo element) {
Romain Guy40c18f52011-09-01 17:01:18 -070013919 element.target = null;
Romain Guyd928d682009-03-31 17:52:16 -070013920 }
13921 }, POOL_LIMIT)
13922 );
13923
13924 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013925 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013926
13927 View target;
13928
13929 int left;
13930 int top;
13931 int right;
13932 int bottom;
13933
Romain Guyd928d682009-03-31 17:52:16 -070013934 public void setNextPoolable(InvalidateInfo element) {
13935 mNext = element;
13936 }
13937
13938 public InvalidateInfo getNextPoolable() {
13939 return mNext;
13940 }
13941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013942 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013943 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013944 }
13945
13946 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013947 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013948 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013949
13950 public boolean isPooled() {
13951 return mIsPooled;
13952 }
13953
13954 public void setPooled(boolean isPooled) {
13955 mIsPooled = isPooled;
13956 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013957 }
13958
13959 final IWindowSession mSession;
13960
13961 final IWindow mWindow;
13962
13963 final IBinder mWindowToken;
13964
13965 final Callbacks mRootCallbacks;
13966
Romain Guy59a12ca2011-06-09 17:48:21 -070013967 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013968
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013969 /**
13970 * The top view of the hierarchy.
13971 */
13972 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013973
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013974 IBinder mPanelParentWindowToken;
13975 Surface mSurface;
13976
Romain Guyb051e892010-09-28 19:09:36 -070013977 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013978 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013979 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013981 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013982 * Scale factor used by the compatibility mode
13983 */
13984 float mApplicationScale;
13985
13986 /**
13987 * Indicates whether the application is in compatibility mode
13988 */
13989 boolean mScalingRequired;
13990
13991 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013992 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013993 */
13994 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013995
Dianne Hackborn63042d62011-01-26 18:56:29 -080013996 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013997 * Left position of this view's window
13998 */
13999 int mWindowLeft;
14000
14001 /**
14002 * Top position of this view's window
14003 */
14004 int mWindowTop;
14005
14006 /**
Adam Powell26153a32010-11-08 15:22:27 -080014007 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014008 */
Adam Powell26153a32010-11-08 15:22:27 -080014009 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014010
14011 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014012 * For windows that are full-screen but using insets to layout inside
14013 * of the screen decorations, these are the current insets for the
14014 * content of the window.
14015 */
14016 final Rect mContentInsets = new Rect();
14017
14018 /**
14019 * For windows that are full-screen but using insets to layout inside
14020 * of the screen decorations, these are the current insets for the
14021 * actual visible parts of the window.
14022 */
14023 final Rect mVisibleInsets = new Rect();
14024
14025 /**
14026 * The internal insets given by this window. This value is
14027 * supplied by the client (through
14028 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14029 * be given to the window manager when changed to be used in laying
14030 * out windows behind it.
14031 */
14032 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14033 = new ViewTreeObserver.InternalInsetsInfo();
14034
14035 /**
14036 * All views in the window's hierarchy that serve as scroll containers,
14037 * used to determine if the window can be resized or must be panned
14038 * to adjust for a soft input area.
14039 */
14040 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14041
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014042 final KeyEvent.DispatcherState mKeyDispatchState
14043 = new KeyEvent.DispatcherState();
14044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014045 /**
14046 * Indicates whether the view's window currently has the focus.
14047 */
14048 boolean mHasWindowFocus;
14049
14050 /**
14051 * The current visibility of the window.
14052 */
14053 int mWindowVisibility;
14054
14055 /**
14056 * Indicates the time at which drawing started to occur.
14057 */
14058 long mDrawingTime;
14059
14060 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014061 * Indicates whether or not ignoring the DIRTY_MASK flags.
14062 */
14063 boolean mIgnoreDirtyState;
14064
14065 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014066 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14067 * to avoid clearing that flag prematurely.
14068 */
14069 boolean mSetIgnoreDirtyState = false;
14070
14071 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014072 * Indicates whether the view's window is currently in touch mode.
14073 */
14074 boolean mInTouchMode;
14075
14076 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014077 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014078 * the next time it performs a traversal
14079 */
14080 boolean mRecomputeGlobalAttributes;
14081
14082 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014083 * Set during a traveral if any views want to keep the screen on.
14084 */
14085 boolean mKeepScreenOn;
14086
14087 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014088 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14089 */
14090 int mSystemUiVisibility;
14091
14092 /**
14093 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14094 * attached.
14095 */
14096 boolean mHasSystemUiListeners;
14097
14098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014099 * Set if the visibility of any views has changed.
14100 */
14101 boolean mViewVisibilityChanged;
14102
14103 /**
14104 * Set to true if a view has been scrolled.
14105 */
14106 boolean mViewScrollChanged;
14107
14108 /**
14109 * Global to the view hierarchy used as a temporary for dealing with
14110 * x/y points in the transparent region computations.
14111 */
14112 final int[] mTransparentLocation = new int[2];
14113
14114 /**
14115 * Global to the view hierarchy used as a temporary for dealing with
14116 * x/y points in the ViewGroup.invalidateChild implementation.
14117 */
14118 final int[] mInvalidateChildLocation = new int[2];
14119
Chet Haasec3aa3612010-06-17 08:50:37 -070014120
14121 /**
14122 * Global to the view hierarchy used as a temporary for dealing with
14123 * x/y location when view is transformed.
14124 */
14125 final float[] mTmpTransformLocation = new float[2];
14126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014127 /**
14128 * The view tree observer used to dispatch global events like
14129 * layout, pre-draw, touch mode change, etc.
14130 */
14131 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14132
14133 /**
14134 * A Canvas used by the view hierarchy to perform bitmap caching.
14135 */
14136 Canvas mCanvas;
14137
14138 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014139 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014140 * handler can be used to pump events in the UI events queue.
14141 */
14142 final Handler mHandler;
14143
14144 /**
14145 * Identifier for messages requesting the view to be invalidated.
14146 * Such messages should be sent to {@link #mHandler}.
14147 */
14148 static final int INVALIDATE_MSG = 0x1;
14149
14150 /**
14151 * Identifier for messages requesting the view to invalidate a region.
14152 * Such messages should be sent to {@link #mHandler}.
14153 */
14154 static final int INVALIDATE_RECT_MSG = 0x2;
14155
14156 /**
14157 * Temporary for use in computing invalidate rectangles while
14158 * calling up the hierarchy.
14159 */
14160 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014161
14162 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014163 * Temporary for use in computing hit areas with transformed views
14164 */
14165 final RectF mTmpTransformRect = new RectF();
14166
14167 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014168 * Temporary list for use in collecting focusable descendents of a view.
14169 */
14170 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14171
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014172 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014173 * The id of the window for accessibility purposes.
14174 */
14175 int mAccessibilityWindowId = View.NO_ID;
14176
14177 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014178 * Creates a new set of attachment information with the specified
14179 * events handler and thread.
14180 *
14181 * @param handler the events handler the view must use
14182 */
14183 AttachInfo(IWindowSession session, IWindow window,
14184 Handler handler, Callbacks effectPlayer) {
14185 mSession = session;
14186 mWindow = window;
14187 mWindowToken = window.asBinder();
14188 mHandler = handler;
14189 mRootCallbacks = effectPlayer;
14190 }
14191 }
14192
14193 /**
14194 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14195 * is supported. This avoids keeping too many unused fields in most
14196 * instances of View.</p>
14197 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014198 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014199
Mike Cleronf116bf82009-09-27 19:14:12 -070014200 /**
14201 * Scrollbars are not visible
14202 */
14203 public static final int OFF = 0;
14204
14205 /**
14206 * Scrollbars are visible
14207 */
14208 public static final int ON = 1;
14209
14210 /**
14211 * Scrollbars are fading away
14212 */
14213 public static final int FADING = 2;
14214
14215 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014218 public int scrollBarDefaultDelayBeforeFade;
14219 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014220
14221 public int scrollBarSize;
14222 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014223 public float[] interpolatorValues;
14224 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014225
14226 public final Paint paint;
14227 public final Matrix matrix;
14228 public Shader shader;
14229
Mike Cleronf116bf82009-09-27 19:14:12 -070014230 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14231
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014232 private static final float[] OPAQUE = { 255 };
14233 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014234
Mike Cleronf116bf82009-09-27 19:14:12 -070014235 /**
14236 * When fading should start. This time moves into the future every time
14237 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14238 */
14239 public long fadeStartTime;
14240
14241
14242 /**
14243 * The current state of the scrollbars: ON, OFF, or FADING
14244 */
14245 public int state = OFF;
14246
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014247 private int mLastColor;
14248
Mike Cleronf116bf82009-09-27 19:14:12 -070014249 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014250 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14251 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014252 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14253 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014254
14255 paint = new Paint();
14256 matrix = new Matrix();
14257 // use use a height of 1, and then wack the matrix each time we
14258 // actually use it.
14259 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014260
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014261 paint.setShader(shader);
14262 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014263 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014264 }
Romain Guy8506ab42009-06-11 17:35:47 -070014265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 public void setFadeColor(int color) {
14267 if (color != 0 && color != mLastColor) {
14268 mLastColor = color;
14269 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014270
Romain Guye55e1a72009-08-27 10:42:26 -070014271 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14272 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014274 paint.setShader(shader);
14275 // Restore the default transfer mode (src_over)
14276 paint.setXfermode(null);
14277 }
14278 }
Joe Malin32736f02011-01-19 16:14:20 -080014279
Mike Cleronf116bf82009-09-27 19:14:12 -070014280 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014281 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014282 if (now >= fadeStartTime) {
14283
14284 // the animation fades the scrollbars out by changing
14285 // the opacity (alpha) from fully opaque to fully
14286 // transparent
14287 int nextFrame = (int) now;
14288 int framesCount = 0;
14289
14290 Interpolator interpolator = scrollBarInterpolator;
14291
14292 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014293 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014294
14295 // End transparent
14296 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014297 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014298
14299 state = FADING;
14300
14301 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014302 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014303 }
14304 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014305 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014306
Svetoslav Ganova0156172011-06-26 17:55:44 -070014307 /**
14308 * Resuable callback for sending
14309 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14310 */
14311 private class SendViewScrolledAccessibilityEvent implements Runnable {
14312 public volatile boolean mIsPending;
14313
14314 public void run() {
14315 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14316 mIsPending = false;
14317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014318 }
14319}