blob: 14677e115485dcc18b48ba00b65cc0d2e834ada8 [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 */
Fabrice Di Meglio6a036402011-05-23 14:43:23 -0700639public class View implements Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 private static final boolean DBG = false;
641
642 /**
643 * The logging tag used by this class with android.util.Log.
644 */
645 protected static final String VIEW_LOG_TAG = "View";
646
647 /**
648 * Used to mark a View that has no ID.
649 */
650 public static final int NO_ID = -1;
651
652 /**
653 * This view does not want keystrokes. Use with TAKES_FOCUS_MASK when
654 * calling setFlags.
655 */
656 private static final int NOT_FOCUSABLE = 0x00000000;
657
658 /**
659 * This view wants keystrokes. Use with TAKES_FOCUS_MASK when calling
660 * setFlags.
661 */
662 private static final int FOCUSABLE = 0x00000001;
663
664 /**
665 * Mask for use with setFlags indicating bits used for focus.
666 */
667 private static final int FOCUSABLE_MASK = 0x00000001;
668
669 /**
670 * This view will adjust its padding to fit sytem windows (e.g. status bar)
671 */
672 private static final int FITS_SYSTEM_WINDOWS = 0x00000002;
673
674 /**
Scott Main812634c22011-07-27 13:22:35 -0700675 * This view is visible.
676 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
677 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 */
679 public static final int VISIBLE = 0x00000000;
680
681 /**
682 * This view is invisible, but it still takes up space for layout purposes.
Scott Main812634c22011-07-27 13:22:35 -0700683 * Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
684 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800685 */
686 public static final int INVISIBLE = 0x00000004;
687
688 /**
689 * This view is invisible, and it doesn't take any space for layout
Scott Main812634c22011-07-27 13:22:35 -0700690 * purposes. Use with {@link #setVisibility} and <a href="#attr_android:visibility">{@code
691 * android:visibility}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 */
693 public static final int GONE = 0x00000008;
694
695 /**
696 * Mask for use with setFlags indicating bits used for visibility.
697 * {@hide}
698 */
699 static final int VISIBILITY_MASK = 0x0000000C;
700
701 private static final int[] VISIBILITY_FLAGS = {VISIBLE, INVISIBLE, GONE};
702
703 /**
704 * This view is enabled. Intrepretation varies by subclass.
705 * Use with ENABLED_MASK when calling setFlags.
706 * {@hide}
707 */
708 static final int ENABLED = 0x00000000;
709
710 /**
711 * This view is disabled. Intrepretation varies by subclass.
712 * Use with ENABLED_MASK when calling setFlags.
713 * {@hide}
714 */
715 static final int DISABLED = 0x00000020;
716
717 /**
718 * Mask for use with setFlags indicating bits used for indicating whether
719 * this view is enabled
720 * {@hide}
721 */
722 static final int ENABLED_MASK = 0x00000020;
723
724 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -0700725 * This view won't draw. {@link #onDraw(android.graphics.Canvas)} won't be
726 * called and further optimizations will be performed. It is okay to have
727 * this flag set and a background. Use with DRAW_MASK when calling setFlags.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 * {@hide}
729 */
730 static final int WILL_NOT_DRAW = 0x00000080;
731
732 /**
733 * Mask for use with setFlags indicating bits used for indicating whether
734 * this view is will draw
735 * {@hide}
736 */
737 static final int DRAW_MASK = 0x00000080;
738
739 /**
740 * <p>This view doesn't show scrollbars.</p>
741 * {@hide}
742 */
743 static final int SCROLLBARS_NONE = 0x00000000;
744
745 /**
746 * <p>This view shows horizontal scrollbars.</p>
747 * {@hide}
748 */
749 static final int SCROLLBARS_HORIZONTAL = 0x00000100;
750
751 /**
752 * <p>This view shows vertical scrollbars.</p>
753 * {@hide}
754 */
755 static final int SCROLLBARS_VERTICAL = 0x00000200;
756
757 /**
758 * <p>Mask for use with setFlags indicating bits used for indicating which
759 * scrollbars are enabled.</p>
760 * {@hide}
761 */
762 static final int SCROLLBARS_MASK = 0x00000300;
763
Jeff Brown85a31762010-09-01 17:01:00 -0700764 /**
765 * Indicates that the view should filter touches when its window is obscured.
766 * Refer to the class comments for more information about this security feature.
767 * {@hide}
768 */
769 static final int FILTER_TOUCHES_WHEN_OBSCURED = 0x00000400;
770
771 // note flag value 0x00000800 is now available for next flags...
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772
773 /**
774 * <p>This view doesn't show fading edges.</p>
775 * {@hide}
776 */
777 static final int FADING_EDGE_NONE = 0x00000000;
778
779 /**
780 * <p>This view shows horizontal fading edges.</p>
781 * {@hide}
782 */
783 static final int FADING_EDGE_HORIZONTAL = 0x00001000;
784
785 /**
786 * <p>This view shows vertical fading edges.</p>
787 * {@hide}
788 */
789 static final int FADING_EDGE_VERTICAL = 0x00002000;
790
791 /**
792 * <p>Mask for use with setFlags indicating bits used for indicating which
793 * fading edges are enabled.</p>
794 * {@hide}
795 */
796 static final int FADING_EDGE_MASK = 0x00003000;
797
798 /**
799 * <p>Indicates this view can be clicked. When clickable, a View reacts
800 * to clicks by notifying the OnClickListener.<p>
801 * {@hide}
802 */
803 static final int CLICKABLE = 0x00004000;
804
805 /**
806 * <p>Indicates this view is caching its drawing into a bitmap.</p>
807 * {@hide}
808 */
809 static final int DRAWING_CACHE_ENABLED = 0x00008000;
810
811 /**
812 * <p>Indicates that no icicle should be saved for this view.<p>
813 * {@hide}
814 */
815 static final int SAVE_DISABLED = 0x000010000;
816
817 /**
818 * <p>Mask for use with setFlags indicating bits used for the saveEnabled
819 * property.</p>
820 * {@hide}
821 */
822 static final int SAVE_DISABLED_MASK = 0x000010000;
823
824 /**
825 * <p>Indicates that no drawing cache should ever be created for this view.<p>
826 * {@hide}
827 */
828 static final int WILL_NOT_CACHE_DRAWING = 0x000020000;
829
830 /**
831 * <p>Indicates this view can take / keep focus when int touch mode.</p>
832 * {@hide}
833 */
834 static final int FOCUSABLE_IN_TOUCH_MODE = 0x00040000;
835
836 /**
837 * <p>Enables low quality mode for the drawing cache.</p>
838 */
839 public static final int DRAWING_CACHE_QUALITY_LOW = 0x00080000;
840
841 /**
842 * <p>Enables high quality mode for the drawing cache.</p>
843 */
844 public static final int DRAWING_CACHE_QUALITY_HIGH = 0x00100000;
845
846 /**
847 * <p>Enables automatic quality mode for the drawing cache.</p>
848 */
849 public static final int DRAWING_CACHE_QUALITY_AUTO = 0x00000000;
850
851 private static final int[] DRAWING_CACHE_QUALITY_FLAGS = {
852 DRAWING_CACHE_QUALITY_AUTO, DRAWING_CACHE_QUALITY_LOW, DRAWING_CACHE_QUALITY_HIGH
853 };
854
855 /**
856 * <p>Mask for use with setFlags indicating bits used for the cache
857 * quality property.</p>
858 * {@hide}
859 */
860 static final int DRAWING_CACHE_QUALITY_MASK = 0x00180000;
861
862 /**
863 * <p>
864 * Indicates this view can be long clicked. When long clickable, a View
865 * reacts to long clicks by notifying the OnLongClickListener or showing a
866 * context menu.
867 * </p>
868 * {@hide}
869 */
870 static final int LONG_CLICKABLE = 0x00200000;
871
872 /**
873 * <p>Indicates that this view gets its drawable states from its direct parent
874 * and ignores its original internal states.</p>
875 *
876 * @hide
877 */
878 static final int DUPLICATE_PARENT_STATE = 0x00400000;
879
880 /**
881 * The scrollbar style to display the scrollbars inside the content area,
882 * without increasing the padding. The scrollbars will be overlaid with
883 * translucency on the view's content.
884 */
885 public static final int SCROLLBARS_INSIDE_OVERLAY = 0;
886
887 /**
888 * The scrollbar style to display the scrollbars inside the padded area,
889 * increasing the padding of the view. The scrollbars will not overlap the
890 * content area of the view.
891 */
892 public static final int SCROLLBARS_INSIDE_INSET = 0x01000000;
893
894 /**
895 * The scrollbar style to display the scrollbars at the edge of the view,
896 * without increasing the padding. The scrollbars will be overlaid with
897 * translucency.
898 */
899 public static final int SCROLLBARS_OUTSIDE_OVERLAY = 0x02000000;
900
901 /**
902 * The scrollbar style to display the scrollbars at the edge of the view,
903 * increasing the padding of the view. The scrollbars will only overlap the
904 * background, if any.
905 */
906 public static final int SCROLLBARS_OUTSIDE_INSET = 0x03000000;
907
908 /**
909 * Mask to check if the scrollbar style is overlay or inset.
910 * {@hide}
911 */
912 static final int SCROLLBARS_INSET_MASK = 0x01000000;
913
914 /**
915 * Mask to check if the scrollbar style is inside or outside.
916 * {@hide}
917 */
918 static final int SCROLLBARS_OUTSIDE_MASK = 0x02000000;
919
920 /**
921 * Mask for scrollbar style.
922 * {@hide}
923 */
924 static final int SCROLLBARS_STYLE_MASK = 0x03000000;
925
926 /**
927 * View flag indicating that the screen should remain on while the
928 * window containing this view is visible to the user. This effectively
929 * takes care of automatically setting the WindowManager's
930 * {@link WindowManager.LayoutParams#FLAG_KEEP_SCREEN_ON}.
931 */
932 public static final int KEEP_SCREEN_ON = 0x04000000;
933
934 /**
935 * View flag indicating whether this view should have sound effects enabled
936 * for events such as clicking and touching.
937 */
938 public static final int SOUND_EFFECTS_ENABLED = 0x08000000;
939
940 /**
941 * View flag indicating whether this view should have haptic feedback
942 * enabled for events such as long presses.
943 */
944 public static final int HAPTIC_FEEDBACK_ENABLED = 0x10000000;
945
946 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -0700947 * <p>Indicates that the view hierarchy should stop saving state when
948 * it reaches this view. If state saving is initiated immediately at
949 * the view, it will be allowed.
950 * {@hide}
951 */
952 static final int PARENT_SAVE_DISABLED = 0x20000000;
953
954 /**
955 * <p>Mask for use with setFlags indicating bits used for PARENT_SAVE_DISABLED.</p>
956 * {@hide}
957 */
958 static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
959
960 /**
Cibu Johny7632cb92010-02-22 13:01:02 -0800961 * Horizontal direction of this view is from Left to Right.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700962 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800963 * {@hide}
964 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700965 public static final int LAYOUT_DIRECTION_LTR = 0x00000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800966
967 /**
968 * Horizontal direction of this view is from Right to Left.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700969 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800970 * {@hide}
971 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700972 public static final int LAYOUT_DIRECTION_RTL = 0x40000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800973
974 /**
975 * Horizontal direction of this view is inherited from its parent.
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700976 * Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800977 * {@hide}
978 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700979 public static final int LAYOUT_DIRECTION_INHERIT = 0x80000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800980
981 /**
982 * Horizontal direction of this view is from deduced from the default language
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700983 * script for the locale. Use with {@link #setLayoutDirection}.
Cibu Johny7632cb92010-02-22 13:01:02 -0800984 * {@hide}
985 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700986 public static final int LAYOUT_DIRECTION_LOCALE = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800987
988 /**
989 * Mask for use with setFlags indicating bits used for horizontalDirection.
990 * {@hide}
991 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700992 static final int LAYOUT_DIRECTION_MASK = 0xC0000000;
Cibu Johny7632cb92010-02-22 13:01:02 -0800993
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -0700994 /*
995 * Array of horizontal direction flags for mapping attribute "horizontalDirection" to correct
996 * flag value.
997 * {@hide}
998 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -0700999 private static final int[] LAYOUT_DIRECTION_FLAGS = {LAYOUT_DIRECTION_LTR,
1000 LAYOUT_DIRECTION_RTL, LAYOUT_DIRECTION_INHERIT, LAYOUT_DIRECTION_LOCALE};
Cibu Johny7632cb92010-02-22 13:01:02 -08001001
1002 /**
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001003 * Default horizontalDirection.
1004 * {@hide}
1005 */
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07001006 private static final int LAYOUT_DIRECTION_DEFAULT = LAYOUT_DIRECTION_INHERIT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07001007
1008 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07001009 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1010 * should add all focusable Views regardless if they are focusable in touch mode.
1011 */
1012 public static final int FOCUSABLES_ALL = 0x00000000;
1013
1014 /**
1015 * View flag indicating whether {@link #addFocusables(ArrayList, int, int)}
1016 * should add only Views focusable in touch mode.
1017 */
1018 public static final int FOCUSABLES_TOUCH_MODE = 0x00000001;
1019
1020 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001021 * Use with {@link #focusSearch(int)}. Move focus to the previous selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 * item.
1023 */
1024 public static final int FOCUS_BACKWARD = 0x00000001;
1025
1026 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001027 * Use with {@link #focusSearch(int)}. Move focus to the next selectable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 * item.
1029 */
1030 public static final int FOCUS_FORWARD = 0x00000002;
1031
1032 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001033 * Use with {@link #focusSearch(int)}. Move focus to the left.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034 */
1035 public static final int FOCUS_LEFT = 0x00000011;
1036
1037 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001038 * Use with {@link #focusSearch(int)}. Move focus up.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001039 */
1040 public static final int FOCUS_UP = 0x00000021;
1041
1042 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001043 * Use with {@link #focusSearch(int)}. Move focus to the right.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001044 */
1045 public static final int FOCUS_RIGHT = 0x00000042;
1046
1047 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07001048 * Use with {@link #focusSearch(int)}. Move focus down.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001049 */
1050 public static final int FOCUS_DOWN = 0x00000082;
1051
1052 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08001053 * Bits of {@link #getMeasuredWidthAndState()} and
1054 * {@link #getMeasuredWidthAndState()} that provide the actual measured size.
1055 */
1056 public static final int MEASURED_SIZE_MASK = 0x00ffffff;
1057
1058 /**
1059 * Bits of {@link #getMeasuredWidthAndState()} and
1060 * {@link #getMeasuredWidthAndState()} that provide the additional state bits.
1061 */
1062 public static final int MEASURED_STATE_MASK = 0xff000000;
1063
1064 /**
1065 * Bit shift of {@link #MEASURED_STATE_MASK} to get to the height bits
1066 * for functions that combine both width and height into a single int,
1067 * such as {@link #getMeasuredState()} and the childState argument of
1068 * {@link #resolveSizeAndState(int, int, int)}.
1069 */
1070 public static final int MEASURED_HEIGHT_STATE_SHIFT = 16;
1071
1072 /**
1073 * Bit of {@link #getMeasuredWidthAndState()} and
1074 * {@link #getMeasuredWidthAndState()} that indicates the measured size
1075 * is smaller that the space the view would like to have.
1076 */
1077 public static final int MEASURED_STATE_TOO_SMALL = 0x01000000;
1078
1079 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 * Base View state sets
1081 */
1082 // Singles
1083 /**
1084 * Indicates the view has no states set. States are used with
1085 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1086 * view depending on its state.
1087 *
1088 * @see android.graphics.drawable.Drawable
1089 * @see #getDrawableState()
1090 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001091 protected static final int[] EMPTY_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 /**
1093 * Indicates the view is enabled. States are used with
1094 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1095 * view depending on its state.
1096 *
1097 * @see android.graphics.drawable.Drawable
1098 * @see #getDrawableState()
1099 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001100 protected static final int[] ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001101 /**
1102 * Indicates the view is focused. States are used with
1103 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1104 * view depending on its state.
1105 *
1106 * @see android.graphics.drawable.Drawable
1107 * @see #getDrawableState()
1108 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001109 protected static final int[] FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 /**
1111 * Indicates the view is selected. States are used with
1112 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1113 * view depending on its state.
1114 *
1115 * @see android.graphics.drawable.Drawable
1116 * @see #getDrawableState()
1117 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001118 protected static final int[] SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 /**
1120 * Indicates the view is pressed. States are used with
1121 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1122 * view depending on its state.
1123 *
1124 * @see android.graphics.drawable.Drawable
1125 * @see #getDrawableState()
1126 * @hide
1127 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001128 protected static final int[] PRESSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001129 /**
1130 * Indicates the view's window has focus. States are used with
1131 * {@link android.graphics.drawable.Drawable} to change the drawing of the
1132 * view depending on its state.
1133 *
1134 * @see android.graphics.drawable.Drawable
1135 * @see #getDrawableState()
1136 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001137 protected static final int[] WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 // Doubles
1139 /**
1140 * Indicates the view is enabled and has the focus.
1141 *
1142 * @see #ENABLED_STATE_SET
1143 * @see #FOCUSED_STATE_SET
1144 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001145 protected static final int[] ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 /**
1147 * Indicates the view is enabled and selected.
1148 *
1149 * @see #ENABLED_STATE_SET
1150 * @see #SELECTED_STATE_SET
1151 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001152 protected static final int[] ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 /**
1154 * Indicates the view is enabled and that its window has focus.
1155 *
1156 * @see #ENABLED_STATE_SET
1157 * @see #WINDOW_FOCUSED_STATE_SET
1158 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001159 protected static final int[] ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 /**
1161 * Indicates the view is focused and selected.
1162 *
1163 * @see #FOCUSED_STATE_SET
1164 * @see #SELECTED_STATE_SET
1165 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001166 protected static final int[] FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 /**
1168 * Indicates the view has the focus and that its window has the focus.
1169 *
1170 * @see #FOCUSED_STATE_SET
1171 * @see #WINDOW_FOCUSED_STATE_SET
1172 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001173 protected static final int[] FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001174 /**
1175 * Indicates the view is selected and that its window has the focus.
1176 *
1177 * @see #SELECTED_STATE_SET
1178 * @see #WINDOW_FOCUSED_STATE_SET
1179 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001180 protected static final int[] SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001181 // Triples
1182 /**
1183 * Indicates the view is enabled, focused and selected.
1184 *
1185 * @see #ENABLED_STATE_SET
1186 * @see #FOCUSED_STATE_SET
1187 * @see #SELECTED_STATE_SET
1188 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001189 protected static final int[] ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001190 /**
1191 * Indicates the view is enabled, focused and its window has the focus.
1192 *
1193 * @see #ENABLED_STATE_SET
1194 * @see #FOCUSED_STATE_SET
1195 * @see #WINDOW_FOCUSED_STATE_SET
1196 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001197 protected static final int[] ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001198 /**
1199 * Indicates the view is enabled, selected and its window has the focus.
1200 *
1201 * @see #ENABLED_STATE_SET
1202 * @see #SELECTED_STATE_SET
1203 * @see #WINDOW_FOCUSED_STATE_SET
1204 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001205 protected static final int[] ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 /**
1207 * Indicates the view is focused, selected and its window has the focus.
1208 *
1209 * @see #FOCUSED_STATE_SET
1210 * @see #SELECTED_STATE_SET
1211 * @see #WINDOW_FOCUSED_STATE_SET
1212 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001213 protected static final int[] FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 /**
1215 * Indicates the view is enabled, focused, selected and its window
1216 * has the focus.
1217 *
1218 * @see #ENABLED_STATE_SET
1219 * @see #FOCUSED_STATE_SET
1220 * @see #SELECTED_STATE_SET
1221 * @see #WINDOW_FOCUSED_STATE_SET
1222 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001223 protected static final int[] ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001224 /**
1225 * Indicates the view is pressed and its window has the focus.
1226 *
1227 * @see #PRESSED_STATE_SET
1228 * @see #WINDOW_FOCUSED_STATE_SET
1229 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001230 protected static final int[] PRESSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 /**
1232 * Indicates the view is pressed and selected.
1233 *
1234 * @see #PRESSED_STATE_SET
1235 * @see #SELECTED_STATE_SET
1236 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001237 protected static final int[] PRESSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 /**
1239 * Indicates the view is pressed, selected and its window has the focus.
1240 *
1241 * @see #PRESSED_STATE_SET
1242 * @see #SELECTED_STATE_SET
1243 * @see #WINDOW_FOCUSED_STATE_SET
1244 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001245 protected static final int[] PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001246 /**
1247 * Indicates the view is pressed and focused.
1248 *
1249 * @see #PRESSED_STATE_SET
1250 * @see #FOCUSED_STATE_SET
1251 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001252 protected static final int[] PRESSED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 /**
1254 * Indicates the view is pressed, focused and its window has the focus.
1255 *
1256 * @see #PRESSED_STATE_SET
1257 * @see #FOCUSED_STATE_SET
1258 * @see #WINDOW_FOCUSED_STATE_SET
1259 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001260 protected static final int[] PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 /**
1262 * Indicates the view is pressed, focused and selected.
1263 *
1264 * @see #PRESSED_STATE_SET
1265 * @see #SELECTED_STATE_SET
1266 * @see #FOCUSED_STATE_SET
1267 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001268 protected static final int[] PRESSED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 /**
1270 * Indicates the view is pressed, focused, selected and its window has the focus.
1271 *
1272 * @see #PRESSED_STATE_SET
1273 * @see #FOCUSED_STATE_SET
1274 * @see #SELECTED_STATE_SET
1275 * @see #WINDOW_FOCUSED_STATE_SET
1276 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001277 protected static final int[] PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 /**
1279 * Indicates the view is pressed and enabled.
1280 *
1281 * @see #PRESSED_STATE_SET
1282 * @see #ENABLED_STATE_SET
1283 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001284 protected static final int[] PRESSED_ENABLED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 /**
1286 * Indicates the view is pressed, enabled and its window has the focus.
1287 *
1288 * @see #PRESSED_STATE_SET
1289 * @see #ENABLED_STATE_SET
1290 * @see #WINDOW_FOCUSED_STATE_SET
1291 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001292 protected static final int[] PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 /**
1294 * Indicates the view is pressed, enabled and selected.
1295 *
1296 * @see #PRESSED_STATE_SET
1297 * @see #ENABLED_STATE_SET
1298 * @see #SELECTED_STATE_SET
1299 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001300 protected static final int[] PRESSED_ENABLED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001301 /**
1302 * Indicates the view is pressed, enabled, selected and its window has the
1303 * focus.
1304 *
1305 * @see #PRESSED_STATE_SET
1306 * @see #ENABLED_STATE_SET
1307 * @see #SELECTED_STATE_SET
1308 * @see #WINDOW_FOCUSED_STATE_SET
1309 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001310 protected static final int[] PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
1312 * Indicates the view is pressed, enabled and focused.
1313 *
1314 * @see #PRESSED_STATE_SET
1315 * @see #ENABLED_STATE_SET
1316 * @see #FOCUSED_STATE_SET
1317 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001318 protected static final int[] PRESSED_ENABLED_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 /**
1320 * Indicates the view is pressed, enabled, focused and its window has the
1321 * focus.
1322 *
1323 * @see #PRESSED_STATE_SET
1324 * @see #ENABLED_STATE_SET
1325 * @see #FOCUSED_STATE_SET
1326 * @see #WINDOW_FOCUSED_STATE_SET
1327 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001328 protected static final int[] PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 /**
1330 * Indicates the view is pressed, enabled, focused and selected.
1331 *
1332 * @see #PRESSED_STATE_SET
1333 * @see #ENABLED_STATE_SET
1334 * @see #SELECTED_STATE_SET
1335 * @see #FOCUSED_STATE_SET
1336 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001337 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 /**
1339 * Indicates the view is pressed, enabled, focused, selected and its window
1340 * has the focus.
1341 *
1342 * @see #PRESSED_STATE_SET
1343 * @see #ENABLED_STATE_SET
1344 * @see #SELECTED_STATE_SET
1345 * @see #FOCUSED_STATE_SET
1346 * @see #WINDOW_FOCUSED_STATE_SET
1347 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001348 protected static final int[] PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349
1350 /**
1351 * The order here is very important to {@link #getDrawableState()}
1352 */
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001353 private static final int[][] VIEW_STATE_SETS;
1354
Romain Guyb051e892010-09-28 19:09:36 -07001355 static final int VIEW_STATE_WINDOW_FOCUSED = 1;
1356 static final int VIEW_STATE_SELECTED = 1 << 1;
1357 static final int VIEW_STATE_FOCUSED = 1 << 2;
1358 static final int VIEW_STATE_ENABLED = 1 << 3;
1359 static final int VIEW_STATE_PRESSED = 1 << 4;
1360 static final int VIEW_STATE_ACTIVATED = 1 << 5;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001361 static final int VIEW_STATE_ACCELERATED = 1 << 6;
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001362 static final int VIEW_STATE_HOVERED = 1 << 7;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001363 static final int VIEW_STATE_DRAG_CAN_ACCEPT = 1 << 8;
1364 static final int VIEW_STATE_DRAG_HOVERED = 1 << 9;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001365
1366 static final int[] VIEW_STATE_IDS = new int[] {
1367 R.attr.state_window_focused, VIEW_STATE_WINDOW_FOCUSED,
1368 R.attr.state_selected, VIEW_STATE_SELECTED,
1369 R.attr.state_focused, VIEW_STATE_FOCUSED,
1370 R.attr.state_enabled, VIEW_STATE_ENABLED,
1371 R.attr.state_pressed, VIEW_STATE_PRESSED,
1372 R.attr.state_activated, VIEW_STATE_ACTIVATED,
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001373 R.attr.state_accelerated, VIEW_STATE_ACCELERATED,
PY Laligandc33d8d49e2011-03-14 18:22:53 -07001374 R.attr.state_hovered, VIEW_STATE_HOVERED,
Christopher Tate3d4bf172011-03-28 16:16:46 -07001375 R.attr.state_drag_can_accept, VIEW_STATE_DRAG_CAN_ACCEPT,
1376 R.attr.state_drag_hovered, VIEW_STATE_DRAG_HOVERED,
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 };
1378
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001379 static {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -08001380 if ((VIEW_STATE_IDS.length/2) != R.styleable.ViewDrawableStates.length) {
1381 throw new IllegalStateException(
1382 "VIEW_STATE_IDs array length does not match ViewDrawableStates style array");
1383 }
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001384 int[] orderedIds = new int[VIEW_STATE_IDS.length];
Romain Guyb051e892010-09-28 19:09:36 -07001385 for (int i = 0; i < R.styleable.ViewDrawableStates.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001386 int viewState = R.styleable.ViewDrawableStates[i];
Romain Guyb051e892010-09-28 19:09:36 -07001387 for (int j = 0; j<VIEW_STATE_IDS.length; j += 2) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001388 if (VIEW_STATE_IDS[j] == viewState) {
Romain Guyb051e892010-09-28 19:09:36 -07001389 orderedIds[i * 2] = viewState;
1390 orderedIds[i * 2 + 1] = VIEW_STATE_IDS[j + 1];
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001391 }
1392 }
1393 }
Romain Guyb051e892010-09-28 19:09:36 -07001394 final int NUM_BITS = VIEW_STATE_IDS.length / 2;
1395 VIEW_STATE_SETS = new int[1 << NUM_BITS][];
1396 for (int i = 0; i < VIEW_STATE_SETS.length; i++) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001397 int numBits = Integer.bitCount(i);
1398 int[] set = new int[numBits];
1399 int pos = 0;
Romain Guyb051e892010-09-28 19:09:36 -07001400 for (int j = 0; j < orderedIds.length; j += 2) {
1401 if ((i & orderedIds[j+1]) != 0) {
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001402 set[pos++] = orderedIds[j];
1403 }
1404 }
1405 VIEW_STATE_SETS[i] = set;
1406 }
1407
1408 EMPTY_STATE_SET = VIEW_STATE_SETS[0];
1409 WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_WINDOW_FOCUSED];
1410 SELECTED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_SELECTED];
1411 SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1412 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED];
1413 FOCUSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_FOCUSED];
1414 FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1415 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED];
1416 FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1417 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED];
1418 FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1419 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1420 | VIEW_STATE_FOCUSED];
1421 ENABLED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_ENABLED];
1422 ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1423 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED];
1424 ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1425 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED];
1426 ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1427 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1428 | VIEW_STATE_ENABLED];
1429 ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1430 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED];
1431 ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1432 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1433 | VIEW_STATE_ENABLED];
1434 ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1435 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1436 | VIEW_STATE_ENABLED];
1437 ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1438 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1439 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED];
1440
1441 PRESSED_STATE_SET = VIEW_STATE_SETS[VIEW_STATE_PRESSED];
1442 PRESSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1443 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_PRESSED];
1444 PRESSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1445 VIEW_STATE_SELECTED | VIEW_STATE_PRESSED];
1446 PRESSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1447 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1448 | VIEW_STATE_PRESSED];
1449 PRESSED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1450 VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1451 PRESSED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1452 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1453 | VIEW_STATE_PRESSED];
1454 PRESSED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1455 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1456 | VIEW_STATE_PRESSED];
1457 PRESSED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1458 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1459 | VIEW_STATE_FOCUSED | VIEW_STATE_PRESSED];
1460 PRESSED_ENABLED_STATE_SET = VIEW_STATE_SETS[
1461 VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1462 PRESSED_ENABLED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1463 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_ENABLED
1464 | VIEW_STATE_PRESSED];
1465 PRESSED_ENABLED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1466 VIEW_STATE_SELECTED | VIEW_STATE_ENABLED
1467 | VIEW_STATE_PRESSED];
1468 PRESSED_ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1469 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1470 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1471 PRESSED_ENABLED_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1472 VIEW_STATE_FOCUSED | VIEW_STATE_ENABLED
1473 | VIEW_STATE_PRESSED];
1474 PRESSED_ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1475 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_FOCUSED
1476 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1477 PRESSED_ENABLED_FOCUSED_SELECTED_STATE_SET = VIEW_STATE_SETS[
1478 VIEW_STATE_SELECTED | VIEW_STATE_FOCUSED
1479 | VIEW_STATE_ENABLED | VIEW_STATE_PRESSED];
1480 PRESSED_ENABLED_FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET = VIEW_STATE_SETS[
1481 VIEW_STATE_WINDOW_FOCUSED | VIEW_STATE_SELECTED
1482 | VIEW_STATE_FOCUSED| VIEW_STATE_ENABLED
1483 | VIEW_STATE_PRESSED];
1484 }
1485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001486 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001487 * Temporary Rect currently for use in setBackground(). This will probably
1488 * be extended in the future to hold our own class with more than just
1489 * a Rect. :)
1490 */
1491 static final ThreadLocal<Rect> sThreadLocal = new ThreadLocal<Rect>();
Romain Guyd90a3312009-05-06 14:54:28 -07001492
1493 /**
1494 * Map used to store views' tags.
1495 */
1496 private static WeakHashMap<View, SparseArray<Object>> sTags;
1497
1498 /**
1499 * Lock used to access sTags.
1500 */
1501 private static final Object sTagsLock = new Object();
1502
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001503 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001504 * The next available accessiiblity id.
1505 */
1506 private static int sNextAccessibilityViewId;
1507
1508 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001509 * The animation currently associated with this view.
1510 * @hide
1511 */
1512 protected Animation mCurrentAnimation = null;
1513
1514 /**
1515 * Width as measured during measure pass.
1516 * {@hide}
1517 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001518 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001519 int mMeasuredWidth;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520
1521 /**
1522 * Height as measured during measure pass.
1523 * {@hide}
1524 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07001525 @ViewDebug.ExportedProperty(category = "measurement")
Romain Guy676b1732011-02-14 14:45:33 -08001526 int mMeasuredHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527
1528 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001529 * Flag to indicate that this view was marked INVALIDATED, or had its display list
1530 * invalidated, prior to the current drawing iteration. If true, the view must re-draw
1531 * its display list. This flag, used only when hw accelerated, allows us to clear the
1532 * flag while retaining this information until it's needed (at getDisplayList() time and
1533 * in drawChild(), when we decide to draw a view's children's display lists into our own).
1534 *
1535 * {@hide}
1536 */
1537 boolean mRecreateDisplayList = false;
1538
1539 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001540 * The view's identifier.
1541 * {@hide}
1542 *
1543 * @see #setId(int)
1544 * @see #getId()
1545 */
1546 @ViewDebug.ExportedProperty(resolveId = true)
1547 int mID = NO_ID;
1548
1549 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07001550 * The stable ID of this view for accessibility porposes.
1551 */
1552 int mAccessibilityViewId = NO_ID;
1553
1554 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 * The view's tag.
1556 * {@hide}
1557 *
1558 * @see #setTag(Object)
1559 * @see #getTag()
1560 */
1561 protected Object mTag;
1562
1563 // for mPrivateFlags:
1564 /** {@hide} */
1565 static final int WANTS_FOCUS = 0x00000001;
1566 /** {@hide} */
1567 static final int FOCUSED = 0x00000002;
1568 /** {@hide} */
1569 static final int SELECTED = 0x00000004;
1570 /** {@hide} */
1571 static final int IS_ROOT_NAMESPACE = 0x00000008;
1572 /** {@hide} */
1573 static final int HAS_BOUNDS = 0x00000010;
1574 /** {@hide} */
1575 static final int DRAWN = 0x00000020;
1576 /**
1577 * When this flag is set, this view is running an animation on behalf of its
1578 * children and should therefore not cancel invalidate requests, even if they
1579 * lie outside of this view's bounds.
1580 *
1581 * {@hide}
1582 */
1583 static final int DRAW_ANIMATION = 0x00000040;
1584 /** {@hide} */
1585 static final int SKIP_DRAW = 0x00000080;
1586 /** {@hide} */
1587 static final int ONLY_DRAWS_BACKGROUND = 0x00000100;
1588 /** {@hide} */
1589 static final int REQUEST_TRANSPARENT_REGIONS = 0x00000200;
1590 /** {@hide} */
1591 static final int DRAWABLE_STATE_DIRTY = 0x00000400;
1592 /** {@hide} */
1593 static final int MEASURED_DIMENSION_SET = 0x00000800;
1594 /** {@hide} */
1595 static final int FORCE_LAYOUT = 0x00001000;
Konstantin Lopyrevc6dc4572010-08-06 15:01:52 -07001596 /** {@hide} */
1597 static final int LAYOUT_REQUIRED = 0x00002000;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001598
1599 private static final int PRESSED = 0x00004000;
1600
1601 /** {@hide} */
1602 static final int DRAWING_CACHE_VALID = 0x00008000;
1603 /**
1604 * Flag used to indicate that this view should be drawn once more (and only once
1605 * more) after its animation has completed.
1606 * {@hide}
1607 */
1608 static final int ANIMATION_STARTED = 0x00010000;
1609
1610 private static final int SAVE_STATE_CALLED = 0x00020000;
1611
1612 /**
1613 * Indicates that the View returned true when onSetAlpha() was called and that
1614 * the alpha must be restored.
1615 * {@hide}
1616 */
1617 static final int ALPHA_SET = 0x00040000;
1618
1619 /**
1620 * Set by {@link #setScrollContainer(boolean)}.
1621 */
1622 static final int SCROLL_CONTAINER = 0x00080000;
1623
1624 /**
1625 * Set by {@link #setScrollContainer(boolean)}.
1626 */
1627 static final int SCROLL_CONTAINER_ADDED = 0x00100000;
1628
1629 /**
Romain Guy809a7f62009-05-14 15:44:42 -07001630 * View flag indicating whether this view was invalidated (fully or partially.)
1631 *
1632 * @hide
1633 */
1634 static final int DIRTY = 0x00200000;
1635
1636 /**
1637 * View flag indicating whether this view was invalidated by an opaque
1638 * invalidate request.
1639 *
1640 * @hide
1641 */
1642 static final int DIRTY_OPAQUE = 0x00400000;
1643
1644 /**
1645 * Mask for {@link #DIRTY} and {@link #DIRTY_OPAQUE}.
1646 *
1647 * @hide
1648 */
1649 static final int DIRTY_MASK = 0x00600000;
1650
1651 /**
Romain Guy8f1344f52009-05-15 16:03:59 -07001652 * Indicates whether the background is opaque.
1653 *
1654 * @hide
1655 */
1656 static final int OPAQUE_BACKGROUND = 0x00800000;
1657
1658 /**
1659 * Indicates whether the scrollbars are opaque.
1660 *
1661 * @hide
1662 */
1663 static final int OPAQUE_SCROLLBARS = 0x01000000;
1664
1665 /**
1666 * Indicates whether the view is opaque.
1667 *
1668 * @hide
1669 */
1670 static final int OPAQUE_MASK = 0x01800000;
Joe Malin32736f02011-01-19 16:14:20 -08001671
Adam Powelle14579b2009-12-16 18:39:52 -08001672 /**
1673 * Indicates a prepressed state;
1674 * the short time between ACTION_DOWN and recognizing
1675 * a 'real' press. Prepressed is used to recognize quick taps
1676 * even when they are shorter than ViewConfiguration.getTapTimeout().
Joe Malin32736f02011-01-19 16:14:20 -08001677 *
Adam Powelle14579b2009-12-16 18:39:52 -08001678 * @hide
1679 */
1680 private static final int PREPRESSED = 0x02000000;
Joe Malin32736f02011-01-19 16:14:20 -08001681
Adam Powellc9fbaab2010-02-16 17:16:19 -08001682 /**
Romain Guy8afa5152010-02-26 11:56:30 -08001683 * Indicates whether the view is temporarily detached.
1684 *
1685 * @hide
1686 */
1687 static final int CANCEL_NEXT_UP_EVENT = 0x04000000;
Joe Malin32736f02011-01-19 16:14:20 -08001688
Adam Powell8568c3a2010-04-19 14:26:11 -07001689 /**
1690 * Indicates that we should awaken scroll bars once attached
Joe Malin32736f02011-01-19 16:14:20 -08001691 *
Adam Powell8568c3a2010-04-19 14:26:11 -07001692 * @hide
1693 */
1694 private static final int AWAKEN_SCROLL_BARS_ON_ATTACH = 0x08000000;
Romain Guy8f1344f52009-05-15 16:03:59 -07001695
1696 /**
Jeff Browna032cc02011-03-07 16:56:21 -08001697 * Indicates that the view has received HOVER_ENTER. Cleared on HOVER_EXIT.
1698 * @hide
1699 */
1700 private static final int HOVERED = 0x10000000;
1701
1702 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07001703 * Indicates that pivotX or pivotY were explicitly set and we should not assume the center
1704 * for transform operations
1705 *
1706 * @hide
1707 */
Adam Powellf37df072010-09-17 16:22:49 -07001708 private static final int PIVOT_EXPLICITLY_SET = 0x20000000;
Chet Haasefd2b0022010-08-06 13:08:56 -07001709
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001710 /** {@hide} */
Adam Powellf37df072010-09-17 16:22:49 -07001711 static final int ACTIVATED = 0x40000000;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -07001712
Chet Haasefd2b0022010-08-06 13:08:56 -07001713 /**
Chet Haasedaf98e92011-01-10 14:10:36 -08001714 * Indicates that this view was specifically invalidated, not just dirtied because some
1715 * child view was invalidated. The flag is used to determine when we need to recreate
1716 * a view's display list (as opposed to just returning a reference to its existing
1717 * display list).
1718 *
1719 * @hide
1720 */
1721 static final int INVALIDATED = 0x80000000;
1722
Christopher Tate3d4bf172011-03-28 16:16:46 -07001723 /* Masks for mPrivateFlags2 */
1724
1725 /**
1726 * Indicates that this view has reported that it can accept the current drag's content.
1727 * Cleared when the drag operation concludes.
1728 * @hide
1729 */
1730 static final int DRAG_CAN_ACCEPT = 0x00000001;
1731
1732 /**
1733 * Indicates that this view is currently directly under the drag location in a
1734 * drag-and-drop operation involving content that it can accept. Cleared when
1735 * the drag exits the view, or when the drag operation concludes.
1736 * @hide
1737 */
1738 static final int DRAG_HOVERED = 0x00000002;
1739
Cibu Johny86666632010-02-22 13:01:02 -08001740 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001741 * Indicates whether the view layout direction has been resolved and drawn to the
1742 * right-to-left direction.
Cibu Johny86666632010-02-22 13:01:02 -08001743 *
1744 * @hide
1745 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07001746 static final int LAYOUT_DIRECTION_RESOLVED_RTL = 0x00000004;
1747
1748 /**
1749 * Indicates whether the view layout direction has been resolved.
1750 *
1751 * @hide
1752 */
1753 static final int LAYOUT_DIRECTION_RESOLVED = 0x00000008;
1754
Cibu Johny86666632010-02-22 13:01:02 -08001755
Christopher Tate3d4bf172011-03-28 16:16:46 -07001756 /* End of masks for mPrivateFlags2 */
1757
1758 static final int DRAG_MASK = DRAG_CAN_ACCEPT | DRAG_HOVERED;
1759
Chet Haasedaf98e92011-01-10 14:10:36 -08001760 /**
Adam Powell637d3372010-08-25 14:37:03 -07001761 * Always allow a user to over-scroll this view, provided it is a
1762 * view that can scroll.
1763 *
1764 * @see #getOverScrollMode()
1765 * @see #setOverScrollMode(int)
1766 */
1767 public static final int OVER_SCROLL_ALWAYS = 0;
1768
1769 /**
1770 * Allow a user to over-scroll this view only if the content is large
1771 * enough to meaningfully scroll, provided it is a view that can scroll.
1772 *
1773 * @see #getOverScrollMode()
1774 * @see #setOverScrollMode(int)
1775 */
1776 public static final int OVER_SCROLL_IF_CONTENT_SCROLLS = 1;
1777
1778 /**
1779 * Never allow a user to over-scroll this view.
1780 *
1781 * @see #getOverScrollMode()
1782 * @see #setOverScrollMode(int)
1783 */
1784 public static final int OVER_SCROLL_NEVER = 2;
1785
1786 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001787 * View has requested the system UI (status bar) to be visible (the default).
Joe Onorato664644d2011-01-23 17:53:23 -08001788 *
Joe Malin32736f02011-01-19 16:14:20 -08001789 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001790 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001791 public static final int SYSTEM_UI_FLAG_VISIBLE = 0;
Joe Onorato664644d2011-01-23 17:53:23 -08001792
1793 /**
Daniel Sandler60ee2562011-07-22 12:34:33 -04001794 * View has requested the system UI to enter an unobtrusive "low profile" mode.
1795 *
1796 * This is for use in games, book readers, video players, or any other "immersive" application
1797 * where the usual system chrome is deemed too distracting.
1798 *
1799 * In low profile mode, the status bar and/or navigation icons may dim.
Joe Onorato664644d2011-01-23 17:53:23 -08001800 *
Joe Malin32736f02011-01-19 16:14:20 -08001801 * @see #setSystemUiVisibility(int)
Joe Onorato664644d2011-01-23 17:53:23 -08001802 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001803 public static final int SYSTEM_UI_FLAG_LOW_PROFILE = 0x00000001;
1804
1805 /**
1806 * View has requested that the system navigation be temporarily hidden.
1807 *
1808 * This is an even less obtrusive state than that called for by
1809 * {@link #SYSTEM_UI_FLAG_LOW_PROFILE}; on devices that draw essential navigation controls
1810 * (Home, Back, and the like) on screen, <code>SYSTEM_UI_FLAG_HIDE_NAVIGATION</code> will cause
1811 * those to disappear. This is useful (in conjunction with the
1812 * {@link android.view.WindowManager.LayoutParams#FLAG_FULLSCREEN FLAG_FULLSCREEN} and
1813 * {@link android.view.WindowManager.LayoutParams#FLAG_LAYOUT_IN_SCREEN FLAG_LAYOUT_IN_SCREEN}
1814 * window flags) for displaying content using every last pixel on the display.
1815 *
1816 * There is a limitation: because navigation controls are so important, the least user
1817 * interaction will cause them to reappear immediately.
1818 *
1819 * @see #setSystemUiVisibility(int)
1820 */
1821 public static final int SYSTEM_UI_FLAG_HIDE_NAVIGATION = 0x00000002;
1822
1823 /**
1824 * @deprecated Use {@link #SYSTEM_UI_FLAG_LOW_PROFILE} instead.
1825 */
1826 public static final int STATUS_BAR_HIDDEN = SYSTEM_UI_FLAG_LOW_PROFILE;
1827
1828 /**
1829 * @deprecated Use {@link #SYSTEM_UI_FLAG_VISIBLE} instead.
1830 */
1831 public static final int STATUS_BAR_VISIBLE = SYSTEM_UI_FLAG_VISIBLE;
Joe Onorato664644d2011-01-23 17:53:23 -08001832
1833 /**
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001834 * @hide
1835 *
1836 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1837 * out of the public fields to keep the undefined bits out of the developer's way.
1838 *
1839 * Flag to make the status bar not expandable. Unless you also
1840 * set {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS}, new notifications will continue to show.
1841 */
1842 public static final int STATUS_BAR_DISABLE_EXPAND = 0x00010000;
1843
1844 /**
1845 * @hide
1846 *
1847 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1848 * out of the public fields to keep the undefined bits out of the developer's way.
1849 *
1850 * Flag to hide notification icons and scrolling ticker text.
1851 */
1852 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ICONS = 0x00020000;
1853
1854 /**
1855 * @hide
1856 *
1857 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1858 * out of the public fields to keep the undefined bits out of the developer's way.
1859 *
1860 * Flag to disable incoming notification alerts. This will not block
1861 * icons, but it will block sound, vibrating and other visual or aural notifications.
1862 */
1863 public static final int STATUS_BAR_DISABLE_NOTIFICATION_ALERTS = 0x00040000;
1864
1865 /**
1866 * @hide
1867 *
1868 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1869 * out of the public fields to keep the undefined bits out of the developer's way.
1870 *
1871 * Flag to hide only the scrolling ticker. Note that
1872 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_ICONS} implies
1873 * {@link #STATUS_BAR_DISABLE_NOTIFICATION_TICKER}.
1874 */
1875 public static final int STATUS_BAR_DISABLE_NOTIFICATION_TICKER = 0x00080000;
1876
1877 /**
1878 * @hide
1879 *
1880 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1881 * out of the public fields to keep the undefined bits out of the developer's way.
1882 *
1883 * Flag to hide the center system info area.
1884 */
1885 public static final int STATUS_BAR_DISABLE_SYSTEM_INFO = 0x00100000;
1886
1887 /**
1888 * @hide
1889 *
1890 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1891 * out of the public fields to keep the undefined bits out of the developer's way.
1892 *
1893 * Flag to hide only the navigation buttons. Don't use this
1894 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
Joe Onorato6478adc2011-01-27 21:15:01 -08001895 *
1896 * THIS DOES NOT DISABLE THE BACK BUTTON
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001897 */
1898 public static final int STATUS_BAR_DISABLE_NAVIGATION = 0x00200000;
1899
1900 /**
1901 * @hide
1902 *
1903 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1904 * out of the public fields to keep the undefined bits out of the developer's way.
1905 *
Joe Onorato6478adc2011-01-27 21:15:01 -08001906 * Flag to hide only the back button. Don't use this
1907 * unless you're a special part of the system UI (i.e., setup wizard, keyguard).
1908 */
1909 public static final int STATUS_BAR_DISABLE_BACK = 0x00400000;
1910
1911 /**
1912 * @hide
1913 *
1914 * NOTE: This flag may only be used in subtreeSystemUiVisibility. It is masked
1915 * out of the public fields to keep the undefined bits out of the developer's way.
1916 *
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001917 * Flag to hide only the clock. You might use this if your activity has
1918 * its own clock making the status bar's clock redundant.
1919 */
Joe Onorato6478adc2011-01-27 21:15:01 -08001920 public static final int STATUS_BAR_DISABLE_CLOCK = 0x00800000;
1921
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001922 /**
1923 * @hide
1924 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001925 public static final int PUBLIC_STATUS_BAR_VISIBILITY_MASK = 0x0000FFFF;
Joe Onorato7bb8eeb2011-01-27 16:00:58 -08001926
1927 /**
Adam Powell637d3372010-08-25 14:37:03 -07001928 * Controls the over-scroll mode for this view.
1929 * See {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)},
1930 * {@link #OVER_SCROLL_ALWAYS}, {@link #OVER_SCROLL_IF_CONTENT_SCROLLS},
1931 * and {@link #OVER_SCROLL_NEVER}.
1932 */
1933 private int mOverScrollMode;
1934
1935 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 * The parent this view is attached to.
1937 * {@hide}
1938 *
1939 * @see #getParent()
1940 */
1941 protected ViewParent mParent;
1942
1943 /**
1944 * {@hide}
1945 */
1946 AttachInfo mAttachInfo;
1947
1948 /**
1949 * {@hide}
1950 */
Romain Guy809a7f62009-05-14 15:44:42 -07001951 @ViewDebug.ExportedProperty(flagMapping = {
1952 @ViewDebug.FlagToString(mask = FORCE_LAYOUT, equals = FORCE_LAYOUT,
1953 name = "FORCE_LAYOUT"),
1954 @ViewDebug.FlagToString(mask = LAYOUT_REQUIRED, equals = LAYOUT_REQUIRED,
1955 name = "LAYOUT_REQUIRED"),
1956 @ViewDebug.FlagToString(mask = DRAWING_CACHE_VALID, equals = DRAWING_CACHE_VALID,
Romain Guy5bcdff42009-05-14 21:27:18 -07001957 name = "DRAWING_CACHE_INVALID", outputIf = false),
Romain Guy809a7f62009-05-14 15:44:42 -07001958 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "DRAWN", outputIf = true),
1959 @ViewDebug.FlagToString(mask = DRAWN, equals = DRAWN, name = "NOT_DRAWN", outputIf = false),
1960 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY_OPAQUE, name = "DIRTY_OPAQUE"),
1961 @ViewDebug.FlagToString(mask = DIRTY_MASK, equals = DIRTY, name = "DIRTY")
1962 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001963 int mPrivateFlags;
Christopher Tate3d4bf172011-03-28 16:16:46 -07001964 int mPrivateFlags2;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001965
1966 /**
Joe Onorato664644d2011-01-23 17:53:23 -08001967 * This view's request for the visibility of the status bar.
1968 * @hide
1969 */
Daniel Sandler60ee2562011-07-22 12:34:33 -04001970 @ViewDebug.ExportedProperty(flagMapping = {
1971 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_LOW_PROFILE,
1972 equals = SYSTEM_UI_FLAG_LOW_PROFILE,
1973 name = "SYSTEM_UI_FLAG_LOW_PROFILE", outputIf = true),
1974 @ViewDebug.FlagToString(mask = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1975 equals = SYSTEM_UI_FLAG_HIDE_NAVIGATION,
1976 name = "SYSTEM_UI_FLAG_HIDE_NAVIGATION", outputIf = true),
1977 @ViewDebug.FlagToString(mask = PUBLIC_STATUS_BAR_VISIBILITY_MASK,
1978 equals = SYSTEM_UI_FLAG_VISIBLE,
1979 name = "SYSTEM_UI_FLAG_VISIBLE", outputIf = true)
1980 })
Joe Onorato664644d2011-01-23 17:53:23 -08001981 int mSystemUiVisibility;
1982
1983 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001984 * Count of how many windows this view has been attached to.
1985 */
1986 int mWindowAttachCount;
1987
1988 /**
1989 * The layout parameters associated with this view and used by the parent
1990 * {@link android.view.ViewGroup} to determine how this view should be
1991 * laid out.
1992 * {@hide}
1993 */
1994 protected ViewGroup.LayoutParams mLayoutParams;
1995
1996 /**
1997 * The view flags hold various views states.
1998 * {@hide}
1999 */
2000 @ViewDebug.ExportedProperty
2001 int mViewFlags;
2002
2003 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002004 * The transform matrix for the View. This transform is calculated internally
2005 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2006 * is used by default. Do *not* use this variable directly; instead call
2007 * getMatrix(), which will automatically recalculate the matrix if necessary
2008 * to get the correct matrix based on the latest rotation and scale properties.
2009 */
2010 private final Matrix mMatrix = new Matrix();
2011
2012 /**
2013 * The transform matrix for the View. This transform is calculated internally
2014 * based on the rotation, scaleX, and scaleY properties. The identity matrix
2015 * is used by default. Do *not* use this variable directly; instead call
Jeff Brown86671742010-09-30 20:00:15 -07002016 * getInverseMatrix(), which will automatically recalculate the matrix if necessary
Chet Haasec3aa3612010-06-17 08:50:37 -07002017 * to get the correct matrix based on the latest rotation and scale properties.
2018 */
2019 private Matrix mInverseMatrix;
2020
2021 /**
2022 * An internal variable that tracks whether we need to recalculate the
2023 * transform matrix, based on whether the rotation or scaleX/Y properties
2024 * have changed since the matrix was last calculated.
2025 */
Chet Haasea00f3862011-02-22 06:34:40 -08002026 boolean mMatrixDirty = false;
Chet Haasec3aa3612010-06-17 08:50:37 -07002027
2028 /**
2029 * An internal variable that tracks whether we need to recalculate the
2030 * transform matrix, based on whether the rotation or scaleX/Y properties
2031 * have changed since the matrix was last calculated.
2032 */
2033 private boolean mInverseMatrixDirty = true;
2034
2035 /**
2036 * A variable that tracks whether we need to recalculate the
2037 * transform matrix, based on whether the rotation or scaleX/Y properties
2038 * have changed since the matrix was last calculated. This variable
Jeff Brown86671742010-09-30 20:00:15 -07002039 * is only valid after a call to updateMatrix() or to a function that
2040 * calls it such as getMatrix(), hasIdentityMatrix() and getInverseMatrix().
Chet Haasec3aa3612010-06-17 08:50:37 -07002041 */
Romain Guy33e72ae2010-07-17 12:40:29 -07002042 private boolean mMatrixIsIdentity = true;
Chet Haasec3aa3612010-06-17 08:50:37 -07002043
2044 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07002045 * The Camera object is used to compute a 3D matrix when rotationX or rotationY are set.
2046 */
2047 private Camera mCamera = null;
2048
2049 /**
2050 * This matrix is used when computing the matrix for 3D rotations.
2051 */
2052 private Matrix matrix3D = null;
2053
2054 /**
2055 * These prev values are used to recalculate a centered pivot point when necessary. The
2056 * pivot point is only used in matrix operations (when rotation, scale, or translation are
2057 * set), so thes values are only used then as well.
2058 */
2059 private int mPrevWidth = -1;
2060 private int mPrevHeight = -1;
2061
Joe Malin32736f02011-01-19 16:14:20 -08002062 private boolean mLastIsOpaque;
2063
Chet Haasefd2b0022010-08-06 13:08:56 -07002064 /**
2065 * Convenience value to check for float values that are close enough to zero to be considered
2066 * zero.
2067 */
Romain Guy2542d192010-08-18 11:47:12 -07002068 private static final float NONZERO_EPSILON = .001f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002069
2070 /**
2071 * The degrees rotation around the vertical axis through the pivot point.
2072 */
2073 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002074 float mRotationY = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002075
2076 /**
2077 * The degrees rotation around the horizontal axis through the pivot point.
2078 */
2079 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002080 float mRotationX = 0f;
Chet Haasefd2b0022010-08-06 13:08:56 -07002081
2082 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002083 * The degrees rotation around the pivot point.
2084 */
2085 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002086 float mRotation = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002087
2088 /**
Chet Haasedf030d22010-07-30 17:22:38 -07002089 * The amount of translation of the object away from its left property (post-layout).
2090 */
2091 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002092 float mTranslationX = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002093
2094 /**
2095 * The amount of translation of the object away from its top property (post-layout).
2096 */
2097 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002098 float mTranslationY = 0f;
Chet Haasedf030d22010-07-30 17:22:38 -07002099
2100 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07002101 * The amount of scale in the x direction around the pivot point. A
2102 * value of 1 means no scaling is applied.
2103 */
2104 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002105 float mScaleX = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002106
2107 /**
2108 * The amount of scale in the y direction around the pivot point. A
2109 * value of 1 means no scaling is applied.
2110 */
2111 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002112 float mScaleY = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002113
2114 /**
2115 * The amount of scale in the x direction around the pivot point. A
2116 * value of 1 means no scaling is applied.
2117 */
2118 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002119 float mPivotX = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002120
2121 /**
2122 * The amount of scale in the y direction around the pivot point. A
2123 * value of 1 means no scaling is applied.
2124 */
2125 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002126 float mPivotY = 0f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002127
2128 /**
2129 * The opacity of the View. This is a value from 0 to 1, where 0 means
2130 * completely transparent and 1 means completely opaque.
2131 */
2132 @ViewDebug.ExportedProperty
Chet Haasea00f3862011-02-22 06:34:40 -08002133 float mAlpha = 1f;
Chet Haasec3aa3612010-06-17 08:50:37 -07002134
2135 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002136 * The distance in pixels from the left edge of this view's parent
2137 * to the left edge of this view.
2138 * {@hide}
2139 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002140 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002141 protected int mLeft;
2142 /**
2143 * The distance in pixels from the left edge of this view's parent
2144 * to the right edge of this view.
2145 * {@hide}
2146 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002147 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 protected int mRight;
2149 /**
2150 * The distance in pixels from the top edge of this view's parent
2151 * to the top edge of this view.
2152 * {@hide}
2153 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002154 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 protected int mTop;
2156 /**
2157 * The distance in pixels from the top edge of this view's parent
2158 * to the bottom edge of this view.
2159 * {@hide}
2160 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002161 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 protected int mBottom;
2163
2164 /**
2165 * The offset, in pixels, by which the content of this view is scrolled
2166 * horizontally.
2167 * {@hide}
2168 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002169 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 protected int mScrollX;
2171 /**
2172 * The offset, in pixels, by which the content of this view is scrolled
2173 * vertically.
2174 * {@hide}
2175 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002176 @ViewDebug.ExportedProperty(category = "scrolling")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002177 protected int mScrollY;
2178
2179 /**
2180 * The left padding in pixels, that is the distance in pixels between the
2181 * left edge of this view and the left edge of its content.
2182 * {@hide}
2183 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002184 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 protected int mPaddingLeft;
2186 /**
2187 * The right padding in pixels, that is the distance in pixels between the
2188 * right edge of this view and the right edge of its content.
2189 * {@hide}
2190 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002191 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002192 protected int mPaddingRight;
2193 /**
2194 * The top padding in pixels, that is the distance in pixels between the
2195 * top edge of this view and the top edge of its content.
2196 * {@hide}
2197 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002198 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002199 protected int mPaddingTop;
2200 /**
2201 * The bottom padding in pixels, that is the distance in pixels between the
2202 * bottom edge of this view and the bottom edge of its content.
2203 * {@hide}
2204 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002205 @ViewDebug.ExportedProperty(category = "padding")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002206 protected int mPaddingBottom;
2207
2208 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07002209 * Briefly describes the view and is primarily used for accessibility support.
2210 */
2211 private CharSequence mContentDescription;
2212
2213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002214 * Cache the paddingRight set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002215 *
2216 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002217 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002218 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002219 protected int mUserPaddingRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002220
2221 /**
2222 * Cache the paddingBottom set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002223 *
2224 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002225 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002226 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002227 protected int mUserPaddingBottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002228
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002229 /**
Adam Powell20232d02010-12-08 21:08:53 -08002230 * Cache the paddingLeft set by the user to append to the scrollbar's size.
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002231 *
2232 * @hide
Adam Powell20232d02010-12-08 21:08:53 -08002233 */
2234 @ViewDebug.ExportedProperty(category = "padding")
Fabrice Di Meglio54d69622011-07-15 16:46:44 -07002235 protected int mUserPaddingLeft;
Adam Powell20232d02010-12-08 21:08:53 -08002236
2237 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002238 * Cache if the user padding is relative.
2239 *
2240 */
2241 @ViewDebug.ExportedProperty(category = "padding")
2242 boolean mUserPaddingRelative;
2243
2244 /**
2245 * Cache the paddingStart set by the user to append to the scrollbar's size.
2246 *
2247 */
2248 @ViewDebug.ExportedProperty(category = "padding")
2249 int mUserPaddingStart;
2250
2251 /**
2252 * Cache the paddingEnd set by the user to append to the scrollbar's size.
2253 *
2254 */
2255 @ViewDebug.ExportedProperty(category = "padding")
2256 int mUserPaddingEnd;
2257
2258 /**
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07002259 * @hide
2260 */
2261 int mOldWidthMeasureSpec = Integer.MIN_VALUE;
2262 /**
2263 * @hide
2264 */
2265 int mOldHeightMeasureSpec = Integer.MIN_VALUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266
2267 private Resources mResources = null;
2268
2269 private Drawable mBGDrawable;
2270
2271 private int mBackgroundResource;
2272 private boolean mBackgroundSizeChanged;
2273
2274 /**
2275 * Listener used to dispatch focus change events.
2276 * This field should be made private, so it is hidden from the SDK.
2277 * {@hide}
2278 */
2279 protected OnFocusChangeListener mOnFocusChangeListener;
2280
2281 /**
Chet Haase21cd1382010-09-01 17:42:29 -07002282 * Listeners for layout change events.
2283 */
2284 private ArrayList<OnLayoutChangeListener> mOnLayoutChangeListeners;
2285
2286 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08002287 * Listeners for attach events.
2288 */
2289 private CopyOnWriteArrayList<OnAttachStateChangeListener> mOnAttachStateChangeListeners;
2290
2291 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 * Listener used to dispatch click events.
2293 * This field should be made private, so it is hidden from the SDK.
2294 * {@hide}
2295 */
2296 protected OnClickListener mOnClickListener;
2297
2298 /**
2299 * Listener used to dispatch long click events.
2300 * This field should be made private, so it is hidden from the SDK.
2301 * {@hide}
2302 */
2303 protected OnLongClickListener mOnLongClickListener;
2304
2305 /**
2306 * Listener used to build the context menu.
2307 * This field should be made private, so it is hidden from the SDK.
2308 * {@hide}
2309 */
2310 protected OnCreateContextMenuListener mOnCreateContextMenuListener;
2311
2312 private OnKeyListener mOnKeyListener;
2313
2314 private OnTouchListener mOnTouchListener;
2315
Jeff Brown10b62902011-06-20 16:40:37 -07002316 private OnHoverListener mOnHoverListener;
2317
Jeff Brown33bbfd22011-02-24 20:55:35 -08002318 private OnGenericMotionListener mOnGenericMotionListener;
2319
Chris Tate32affef2010-10-18 15:29:21 -07002320 private OnDragListener mOnDragListener;
2321
Joe Onorato664644d2011-01-23 17:53:23 -08002322 private OnSystemUiVisibilityChangeListener mOnSystemUiVisibilityChangeListener;
2323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002324 /**
2325 * The application environment this view lives in.
2326 * This field should be made private, so it is hidden from the SDK.
2327 * {@hide}
2328 */
2329 protected Context mContext;
2330
2331 private ScrollabilityCache mScrollCache;
2332
2333 private int[] mDrawableState = null;
2334
Romain Guy0211a0a2011-02-14 16:34:59 -08002335 /**
2336 * Set to true when drawing cache is enabled and cannot be created.
2337 *
2338 * @hide
2339 */
2340 public boolean mCachingFailed;
2341
Romain Guy02890fd2010-08-06 17:58:44 -07002342 private Bitmap mDrawingCache;
2343 private Bitmap mUnscaledDrawingCache;
Romain Guy6c319ca2011-01-11 14:29:25 -08002344 private HardwareLayer mHardwareLayer;
Romain Guy65b345f2011-07-27 18:51:50 -07002345 DisplayList mDisplayList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002346
2347 /**
2348 * When this view has focus and the next focus is {@link #FOCUS_LEFT},
2349 * the user may specify which view to go to next.
2350 */
2351 private int mNextFocusLeftId = View.NO_ID;
2352
2353 /**
2354 * When this view has focus and the next focus is {@link #FOCUS_RIGHT},
2355 * the user may specify which view to go to next.
2356 */
2357 private int mNextFocusRightId = View.NO_ID;
2358
2359 /**
2360 * When this view has focus and the next focus is {@link #FOCUS_UP},
2361 * the user may specify which view to go to next.
2362 */
2363 private int mNextFocusUpId = View.NO_ID;
2364
2365 /**
2366 * When this view has focus and the next focus is {@link #FOCUS_DOWN},
2367 * the user may specify which view to go to next.
2368 */
2369 private int mNextFocusDownId = View.NO_ID;
2370
Jeff Brown4e6319b2010-12-13 10:36:51 -08002371 /**
2372 * When this view has focus and the next focus is {@link #FOCUS_FORWARD},
2373 * the user may specify which view to go to next.
2374 */
2375 int mNextFocusForwardId = View.NO_ID;
2376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002377 private CheckForLongPress mPendingCheckForLongPress;
Adam Powelle14579b2009-12-16 18:39:52 -08002378 private CheckForTap mPendingCheckForTap = null;
Adam Powella35d7682010-03-12 14:48:13 -08002379 private PerformClick mPerformClick;
Svetoslav Ganova0156172011-06-26 17:55:44 -07002380 private SendViewScrolledAccessibilityEvent mSendViewScrolledAccessibilityEvent;
Joe Malin32736f02011-01-19 16:14:20 -08002381
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002382 private UnsetPressedState mUnsetPressedState;
2383
2384 /**
2385 * Whether the long press's action has been invoked. The tap's action is invoked on the
2386 * up event while a long press is invoked as soon as the long press duration is reached, so
2387 * a long press could be performed before the tap is checked, in which case the tap's action
2388 * should not be invoked.
2389 */
2390 private boolean mHasPerformedLongPress;
2391
2392 /**
2393 * The minimum height of the view. We'll try our best to have the height
2394 * of this view to at least this amount.
2395 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002396 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002397 private int mMinHeight;
2398
2399 /**
2400 * The minimum width of the view. We'll try our best to have the width
2401 * of this view to at least this amount.
2402 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07002403 @ViewDebug.ExportedProperty(category = "measurement")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002404 private int mMinWidth;
2405
2406 /**
2407 * The delegate to handle touch events that are physically in this view
2408 * but should be handled by another view.
2409 */
2410 private TouchDelegate mTouchDelegate = null;
2411
2412 /**
2413 * Solid color to use as a background when creating the drawing cache. Enables
2414 * the cache to use 16 bit bitmaps instead of 32 bit.
2415 */
2416 private int mDrawingCacheBackgroundColor = 0;
2417
2418 /**
2419 * Special tree observer used when mAttachInfo is null.
2420 */
2421 private ViewTreeObserver mFloatingTreeObserver;
Joe Malin32736f02011-01-19 16:14:20 -08002422
Adam Powelle14579b2009-12-16 18:39:52 -08002423 /**
2424 * Cache the touch slop from the context that created the view.
2425 */
2426 private int mTouchSlop;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002428 /**
Chet Haasea00f3862011-02-22 06:34:40 -08002429 * Object that handles automatic animation of view properties.
2430 */
2431 private ViewPropertyAnimator mAnimator = null;
2432
2433 /**
Christopher Tate251602f2011-01-28 17:54:12 -08002434 * Flag indicating that a drag can cross window boundaries. When
2435 * {@link #startDrag(ClipData, DragShadowBuilder, Object, int)} is called
2436 * with this flag set, all visible applications will be able to participate
2437 * in the drag operation and receive the dragged content.
Christopher Tate7f9ff9d2011-02-14 17:31:13 -08002438 *
2439 * @hide
Christopher Tate02d2b3b2011-01-10 20:43:53 -08002440 */
2441 public static final int DRAG_FLAG_GLOBAL = 1;
2442
2443 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08002444 * Vertical scroll factor cached by {@link #getVerticalScrollFactor}.
2445 */
2446 private float mVerticalScrollFactor;
2447
2448 /**
Adam Powell20232d02010-12-08 21:08:53 -08002449 * Position of the vertical scroll bar.
2450 */
2451 private int mVerticalScrollbarPosition;
2452
2453 /**
2454 * Position the scroll bar at the default position as determined by the system.
2455 */
2456 public static final int SCROLLBAR_POSITION_DEFAULT = 0;
2457
2458 /**
2459 * Position the scroll bar along the left edge.
2460 */
2461 public static final int SCROLLBAR_POSITION_LEFT = 1;
2462
2463 /**
2464 * Position the scroll bar along the right edge.
2465 */
2466 public static final int SCROLLBAR_POSITION_RIGHT = 2;
2467
2468 /**
Romain Guy171c5922011-01-06 10:04:23 -08002469 * Indicates that the view does not have a layer.
Joe Malin32736f02011-01-19 16:14:20 -08002470 *
2471 * @see #getLayerType()
2472 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002473 * @see #LAYER_TYPE_SOFTWARE
Joe Malin32736f02011-01-19 16:14:20 -08002474 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002475 */
2476 public static final int LAYER_TYPE_NONE = 0;
2477
2478 /**
2479 * <p>Indicates that the view has a software layer. A software layer is backed
2480 * by a bitmap and causes the view to be rendered using Android's software
2481 * rendering pipeline, even if hardware acceleration is enabled.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002482 *
Romain Guy171c5922011-01-06 10:04:23 -08002483 * <p>Software layers have various usages:</p>
2484 * <p>When the application is not using hardware acceleration, a software layer
2485 * is useful to apply a specific color filter and/or blending mode and/or
2486 * translucency to a view and all its children.</p>
2487 * <p>When the application is using hardware acceleration, a software layer
2488 * is useful to render drawing primitives not supported by the hardware
2489 * accelerated pipeline. It can also be used to cache a complex view tree
2490 * into a texture and reduce the complexity of drawing operations. For instance,
2491 * when animating a complex view tree with a translation, a software layer can
2492 * be used to render the view tree only once.</p>
2493 * <p>Software layers should be avoided when the affected view tree updates
2494 * often. Every update will require to re-render the software layer, which can
2495 * potentially be slow (particularly when hardware acceleration is turned on
2496 * since the layer will have to be uploaded into a hardware texture after every
2497 * update.)</p>
Joe Malin32736f02011-01-19 16:14:20 -08002498 *
2499 * @see #getLayerType()
2500 * @see #setLayerType(int, android.graphics.Paint)
Romain Guy171c5922011-01-06 10:04:23 -08002501 * @see #LAYER_TYPE_NONE
Joe Malin32736f02011-01-19 16:14:20 -08002502 * @see #LAYER_TYPE_HARDWARE
Romain Guy171c5922011-01-06 10:04:23 -08002503 */
2504 public static final int LAYER_TYPE_SOFTWARE = 1;
2505
2506 /**
2507 * <p>Indicates that the view has a hardware layer. A hardware layer is backed
2508 * by a hardware specific texture (generally Frame Buffer Objects or FBO on
2509 * OpenGL hardware) and causes the view to be rendered using Android's hardware
2510 * rendering pipeline, but only if hardware acceleration is turned on for the
2511 * view hierarchy. When hardware acceleration is turned off, hardware layers
2512 * behave exactly as {@link #LAYER_TYPE_SOFTWARE software layers}.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002513 *
Romain Guy171c5922011-01-06 10:04:23 -08002514 * <p>A hardware layer is useful to apply a specific color filter and/or
2515 * blending mode and/or translucency to a view and all its children.</p>
Romain Guy6c319ca2011-01-11 14:29:25 -08002516 * <p>A hardware layer can be used to cache a complex view tree into a
2517 * texture and reduce the complexity of drawing operations. For instance,
2518 * when animating a complex view tree with a translation, a hardware layer can
2519 * be used to render the view tree only once.</p>
Romain Guy171c5922011-01-06 10:04:23 -08002520 * <p>A hardware layer can also be used to increase the rendering quality when
2521 * rotation transformations are applied on a view. It can also be used to
2522 * prevent potential clipping issues when applying 3D transforms on a view.</p>
Joe Malin32736f02011-01-19 16:14:20 -08002523 *
2524 * @see #getLayerType()
Romain Guy171c5922011-01-06 10:04:23 -08002525 * @see #setLayerType(int, android.graphics.Paint)
2526 * @see #LAYER_TYPE_NONE
2527 * @see #LAYER_TYPE_SOFTWARE
2528 */
2529 public static final int LAYER_TYPE_HARDWARE = 2;
Joe Malin32736f02011-01-19 16:14:20 -08002530
Romain Guy3aaff3a2011-01-12 14:18:47 -08002531 @ViewDebug.ExportedProperty(category = "drawing", mapping = {
2532 @ViewDebug.IntToString(from = LAYER_TYPE_NONE, to = "NONE"),
2533 @ViewDebug.IntToString(from = LAYER_TYPE_SOFTWARE, to = "SOFTWARE"),
2534 @ViewDebug.IntToString(from = LAYER_TYPE_HARDWARE, to = "HARDWARE")
2535 })
Romain Guy171c5922011-01-06 10:04:23 -08002536 int mLayerType = LAYER_TYPE_NONE;
2537 Paint mLayerPaint;
Romain Guy3a3133d2011-02-01 22:59:58 -08002538 Rect mLocalDirtyRect;
Romain Guy171c5922011-01-06 10:04:23 -08002539
2540 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07002541 * Set to true when the view is sending hover accessibility events because it
2542 * is the innermost hovered view.
2543 */
2544 private boolean mSendingHoverAccessibilityEvents;
2545
2546 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002547 * Text direction is inherited thru {@link ViewGroup}
2548 * @hide
2549 */
2550 public static final int TEXT_DIRECTION_INHERIT = 0;
2551
2552 /**
2553 * Text direction is using "first strong algorithm". The first strong directional character
2554 * determines the paragraph direction. If there is no strong directional character, the
Doug Feltcb3791202011-07-07 11:57:48 -07002555 * paragraph direction is the view's resolved ayout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002556 *
2557 * @hide
2558 */
2559 public static final int TEXT_DIRECTION_FIRST_STRONG = 1;
2560
2561 /**
2562 * Text direction is using "any-RTL" algorithm. The paragraph direction is RTL if it contains
2563 * any strong RTL character, otherwise it is LTR if it contains any strong LTR characters.
Doug Feltcb3791202011-07-07 11:57:48 -07002564 * If there are neither, the paragraph direction is the view's resolved layout direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002565 *
2566 * @hide
2567 */
2568 public static final int TEXT_DIRECTION_ANY_RTL = 2;
2569
2570 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002571 * Text direction is the same as the one held by a 60% majority of the characters. If there is
2572 * no majority then the paragraph direction is the resolved layout direction of the View.
2573 *
2574 * @hide
2575 */
2576 public static final int TEXT_DIRECTION_CHAR_COUNT = 3;
2577
2578 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002579 * Text direction is forced to LTR.
2580 *
2581 * @hide
2582 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002583 public static final int TEXT_DIRECTION_LTR = 4;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002584
2585 /**
2586 * Text direction is forced to RTL.
2587 *
2588 * @hide
2589 */
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002590 public static final int TEXT_DIRECTION_RTL = 5;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002591
2592 /**
2593 * Default text direction is inherited
2594 */
2595 protected static int DEFAULT_TEXT_DIRECTION = TEXT_DIRECTION_INHERIT;
2596
2597 /**
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002598 * Default threshold for "char count" heuristic.
2599 */
2600 protected static float DEFAULT_TEXT_DIRECTION_CHAR_COUNT_THRESHOLD = 0.6f;
2601
2602 /**
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002603 * The text direction that has been defined by {@link #setTextDirection(int)}.
2604 *
2605 * {@hide}
2606 */
2607 @ViewDebug.ExportedProperty(category = "text", mapping = {
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002608 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2609 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2610 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
Fabrice Di Meglio8f502652011-06-29 20:40:43 -07002611 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002612 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2613 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2614 })
Doug Feltcb3791202011-07-07 11:57:48 -07002615 private int mTextDirection = DEFAULT_TEXT_DIRECTION;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002616
2617 /**
Doug Feltcb3791202011-07-07 11:57:48 -07002618 * The resolved text direction. This needs resolution if the value is
2619 * TEXT_DIRECTION_INHERIT. The resolution matches mTextDirection if that is
2620 * not TEXT_DIRECTION_INHERIT, otherwise resolution proceeds up the parent
2621 * chain of the view.
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002622 *
2623 * {@hide}
2624 */
2625 @ViewDebug.ExportedProperty(category = "text", mapping = {
Doug Feltcb3791202011-07-07 11:57:48 -07002626 @ViewDebug.IntToString(from = TEXT_DIRECTION_INHERIT, to = "INHERIT"),
2627 @ViewDebug.IntToString(from = TEXT_DIRECTION_FIRST_STRONG, to = "FIRST_STRONG"),
2628 @ViewDebug.IntToString(from = TEXT_DIRECTION_ANY_RTL, to = "ANY_RTL"),
2629 @ViewDebug.IntToString(from = TEXT_DIRECTION_CHAR_COUNT, to = "CHAR_COUNT"),
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002630 @ViewDebug.IntToString(from = TEXT_DIRECTION_LTR, to = "LTR"),
2631 @ViewDebug.IntToString(from = TEXT_DIRECTION_RTL, to = "RTL")
2632 })
Doug Feltcb3791202011-07-07 11:57:48 -07002633 private int mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07002634
2635 /**
Jeff Brown21bc5c92011-02-28 18:27:14 -08002636 * Consistency verifier for debugging purposes.
2637 * @hide
2638 */
2639 protected final InputEventConsistencyVerifier mInputEventConsistencyVerifier =
2640 InputEventConsistencyVerifier.isInstrumentationEnabled() ?
2641 new InputEventConsistencyVerifier(this, 0) : null;
2642
2643 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002644 * Simple constructor to use when creating a view from code.
2645 *
2646 * @param context The Context the view is running in, through which it can
2647 * access the current theme, resources, etc.
2648 */
2649 public View(Context context) {
2650 mContext = context;
2651 mResources = context != null ? context.getResources() : null;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002652 mViewFlags = SOUND_EFFECTS_ENABLED | HAPTIC_FEEDBACK_ENABLED | LAYOUT_DIRECTION_INHERIT;
Adam Powelle14579b2009-12-16 18:39:52 -08002653 mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
Adam Powell637d3372010-08-25 14:37:03 -07002654 setOverScrollMode(OVER_SCROLL_IF_CONTENT_SCROLLS);
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07002655 mUserPaddingStart = -1;
2656 mUserPaddingEnd = -1;
2657 mUserPaddingRelative = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002658 }
2659
2660 /**
2661 * Constructor that is called when inflating a view from XML. This is called
2662 * when a view is being constructed from an XML file, supplying attributes
2663 * that were specified in the XML file. This version uses a default style of
2664 * 0, so the only attribute values applied are those in the Context's Theme
2665 * and the given AttributeSet.
2666 *
2667 * <p>
2668 * The method onFinishInflate() will be called after all children have been
2669 * added.
2670 *
2671 * @param context The Context the view is running in, through which it can
2672 * access the current theme, resources, etc.
2673 * @param attrs The attributes of the XML tag that is inflating the view.
2674 * @see #View(Context, AttributeSet, int)
2675 */
2676 public View(Context context, AttributeSet attrs) {
2677 this(context, attrs, 0);
2678 }
2679
2680 /**
2681 * Perform inflation from XML and apply a class-specific base style. This
2682 * constructor of View allows subclasses to use their own base style when
2683 * they are inflating. For example, a Button class's constructor would call
2684 * this version of the super class constructor and supply
2685 * <code>R.attr.buttonStyle</code> for <var>defStyle</var>; this allows
2686 * the theme's button style to modify all of the base view attributes (in
2687 * particular its background) as well as the Button class's attributes.
2688 *
2689 * @param context The Context the view is running in, through which it can
2690 * access the current theme, resources, etc.
2691 * @param attrs The attributes of the XML tag that is inflating the view.
2692 * @param defStyle The default style to apply to this view. If 0, no style
2693 * will be applied (beyond what is included in the theme). This may
2694 * either be an attribute resource, whose value will be retrieved
2695 * from the current theme, or an explicit style resource.
2696 * @see #View(Context, AttributeSet)
2697 */
2698 public View(Context context, AttributeSet attrs, int defStyle) {
2699 this(context);
2700
2701 TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View,
2702 defStyle, 0);
2703
2704 Drawable background = null;
2705
2706 int leftPadding = -1;
2707 int topPadding = -1;
2708 int rightPadding = -1;
2709 int bottomPadding = -1;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002710 int startPadding = -1;
2711 int endPadding = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002712
2713 int padding = -1;
2714
2715 int viewFlagValues = 0;
2716 int viewFlagMasks = 0;
2717
2718 boolean setScrollContainer = false;
Romain Guy8506ab42009-06-11 17:35:47 -07002719
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720 int x = 0;
2721 int y = 0;
2722
Chet Haase73066682010-11-29 15:55:32 -08002723 float tx = 0;
2724 float ty = 0;
2725 float rotation = 0;
2726 float rotationX = 0;
2727 float rotationY = 0;
2728 float sx = 1f;
2729 float sy = 1f;
2730 boolean transformSet = false;
2731
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002732 int scrollbarStyle = SCROLLBARS_INSIDE_OVERLAY;
2733
Adam Powell637d3372010-08-25 14:37:03 -07002734 int overScrollMode = mOverScrollMode;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002735 final int N = a.getIndexCount();
2736 for (int i = 0; i < N; i++) {
2737 int attr = a.getIndex(i);
2738 switch (attr) {
2739 case com.android.internal.R.styleable.View_background:
2740 background = a.getDrawable(attr);
2741 break;
2742 case com.android.internal.R.styleable.View_padding:
2743 padding = a.getDimensionPixelSize(attr, -1);
2744 break;
2745 case com.android.internal.R.styleable.View_paddingLeft:
2746 leftPadding = a.getDimensionPixelSize(attr, -1);
2747 break;
2748 case com.android.internal.R.styleable.View_paddingTop:
2749 topPadding = a.getDimensionPixelSize(attr, -1);
2750 break;
2751 case com.android.internal.R.styleable.View_paddingRight:
2752 rightPadding = a.getDimensionPixelSize(attr, -1);
2753 break;
2754 case com.android.internal.R.styleable.View_paddingBottom:
2755 bottomPadding = a.getDimensionPixelSize(attr, -1);
2756 break;
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07002757 case com.android.internal.R.styleable.View_paddingStart:
2758 startPadding = a.getDimensionPixelSize(attr, -1);
2759 break;
2760 case com.android.internal.R.styleable.View_paddingEnd:
2761 endPadding = a.getDimensionPixelSize(attr, -1);
2762 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002763 case com.android.internal.R.styleable.View_scrollX:
2764 x = a.getDimensionPixelOffset(attr, 0);
2765 break;
2766 case com.android.internal.R.styleable.View_scrollY:
2767 y = a.getDimensionPixelOffset(attr, 0);
2768 break;
Chet Haase73066682010-11-29 15:55:32 -08002769 case com.android.internal.R.styleable.View_alpha:
2770 setAlpha(a.getFloat(attr, 1f));
2771 break;
2772 case com.android.internal.R.styleable.View_transformPivotX:
2773 setPivotX(a.getDimensionPixelOffset(attr, 0));
2774 break;
2775 case com.android.internal.R.styleable.View_transformPivotY:
2776 setPivotY(a.getDimensionPixelOffset(attr, 0));
2777 break;
2778 case com.android.internal.R.styleable.View_translationX:
2779 tx = a.getDimensionPixelOffset(attr, 0);
2780 transformSet = true;
2781 break;
2782 case com.android.internal.R.styleable.View_translationY:
2783 ty = a.getDimensionPixelOffset(attr, 0);
2784 transformSet = true;
2785 break;
2786 case com.android.internal.R.styleable.View_rotation:
2787 rotation = a.getFloat(attr, 0);
2788 transformSet = true;
2789 break;
2790 case com.android.internal.R.styleable.View_rotationX:
2791 rotationX = a.getFloat(attr, 0);
2792 transformSet = true;
2793 break;
2794 case com.android.internal.R.styleable.View_rotationY:
2795 rotationY = a.getFloat(attr, 0);
2796 transformSet = true;
2797 break;
2798 case com.android.internal.R.styleable.View_scaleX:
2799 sx = a.getFloat(attr, 1f);
2800 transformSet = true;
2801 break;
2802 case com.android.internal.R.styleable.View_scaleY:
2803 sy = a.getFloat(attr, 1f);
2804 transformSet = true;
2805 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002806 case com.android.internal.R.styleable.View_id:
2807 mID = a.getResourceId(attr, NO_ID);
2808 break;
2809 case com.android.internal.R.styleable.View_tag:
2810 mTag = a.getText(attr);
2811 break;
2812 case com.android.internal.R.styleable.View_fitsSystemWindows:
2813 if (a.getBoolean(attr, false)) {
2814 viewFlagValues |= FITS_SYSTEM_WINDOWS;
2815 viewFlagMasks |= FITS_SYSTEM_WINDOWS;
2816 }
2817 break;
2818 case com.android.internal.R.styleable.View_focusable:
2819 if (a.getBoolean(attr, false)) {
2820 viewFlagValues |= FOCUSABLE;
2821 viewFlagMasks |= FOCUSABLE_MASK;
2822 }
2823 break;
2824 case com.android.internal.R.styleable.View_focusableInTouchMode:
2825 if (a.getBoolean(attr, false)) {
2826 viewFlagValues |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE;
2827 viewFlagMasks |= FOCUSABLE_IN_TOUCH_MODE | FOCUSABLE_MASK;
2828 }
2829 break;
2830 case com.android.internal.R.styleable.View_clickable:
2831 if (a.getBoolean(attr, false)) {
2832 viewFlagValues |= CLICKABLE;
2833 viewFlagMasks |= CLICKABLE;
2834 }
2835 break;
2836 case com.android.internal.R.styleable.View_longClickable:
2837 if (a.getBoolean(attr, false)) {
2838 viewFlagValues |= LONG_CLICKABLE;
2839 viewFlagMasks |= LONG_CLICKABLE;
2840 }
2841 break;
2842 case com.android.internal.R.styleable.View_saveEnabled:
2843 if (!a.getBoolean(attr, true)) {
2844 viewFlagValues |= SAVE_DISABLED;
2845 viewFlagMasks |= SAVE_DISABLED_MASK;
2846 }
2847 break;
2848 case com.android.internal.R.styleable.View_duplicateParentState:
2849 if (a.getBoolean(attr, false)) {
2850 viewFlagValues |= DUPLICATE_PARENT_STATE;
2851 viewFlagMasks |= DUPLICATE_PARENT_STATE;
2852 }
2853 break;
2854 case com.android.internal.R.styleable.View_visibility:
2855 final int visibility = a.getInt(attr, 0);
2856 if (visibility != 0) {
2857 viewFlagValues |= VISIBILITY_FLAGS[visibility];
2858 viewFlagMasks |= VISIBILITY_MASK;
2859 }
2860 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002861 case com.android.internal.R.styleable.View_layoutDirection:
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002862 // Clear any HORIZONTAL_DIRECTION flag already set
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002863 viewFlagValues &= ~LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002864 // Set the HORIZONTAL_DIRECTION flags depending on the value of the attribute
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002865 final int layoutDirection = a.getInt(attr, -1);
2866 if (layoutDirection != -1) {
2867 viewFlagValues |= LAYOUT_DIRECTION_FLAGS[layoutDirection];
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002868 } else {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002869 // Set to default (LAYOUT_DIRECTION_INHERIT)
2870 viewFlagValues |= LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002871 }
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07002872 viewFlagMasks |= LAYOUT_DIRECTION_MASK;
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07002873 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002874 case com.android.internal.R.styleable.View_drawingCacheQuality:
2875 final int cacheQuality = a.getInt(attr, 0);
2876 if (cacheQuality != 0) {
2877 viewFlagValues |= DRAWING_CACHE_QUALITY_FLAGS[cacheQuality];
2878 viewFlagMasks |= DRAWING_CACHE_QUALITY_MASK;
2879 }
2880 break;
svetoslavganov75986cf2009-05-14 22:28:01 -07002881 case com.android.internal.R.styleable.View_contentDescription:
2882 mContentDescription = a.getString(attr);
2883 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002884 case com.android.internal.R.styleable.View_soundEffectsEnabled:
2885 if (!a.getBoolean(attr, true)) {
2886 viewFlagValues &= ~SOUND_EFFECTS_ENABLED;
2887 viewFlagMasks |= SOUND_EFFECTS_ENABLED;
2888 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002889 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002890 case com.android.internal.R.styleable.View_hapticFeedbackEnabled:
2891 if (!a.getBoolean(attr, true)) {
2892 viewFlagValues &= ~HAPTIC_FEEDBACK_ENABLED;
2893 viewFlagMasks |= HAPTIC_FEEDBACK_ENABLED;
2894 }
Karl Rosaen61ab2702009-06-23 11:10:25 -07002895 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002896 case R.styleable.View_scrollbars:
2897 final int scrollbars = a.getInt(attr, SCROLLBARS_NONE);
2898 if (scrollbars != SCROLLBARS_NONE) {
2899 viewFlagValues |= scrollbars;
2900 viewFlagMasks |= SCROLLBARS_MASK;
2901 initializeScrollbars(a);
2902 }
2903 break;
2904 case R.styleable.View_fadingEdge:
2905 final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
2906 if (fadingEdge != FADING_EDGE_NONE) {
2907 viewFlagValues |= fadingEdge;
2908 viewFlagMasks |= FADING_EDGE_MASK;
2909 initializeFadingEdge(a);
2910 }
2911 break;
2912 case R.styleable.View_scrollbarStyle:
2913 scrollbarStyle = a.getInt(attr, SCROLLBARS_INSIDE_OVERLAY);
2914 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
2915 viewFlagValues |= scrollbarStyle & SCROLLBARS_STYLE_MASK;
2916 viewFlagMasks |= SCROLLBARS_STYLE_MASK;
2917 }
2918 break;
2919 case R.styleable.View_isScrollContainer:
2920 setScrollContainer = true;
2921 if (a.getBoolean(attr, false)) {
2922 setScrollContainer(true);
2923 }
2924 break;
2925 case com.android.internal.R.styleable.View_keepScreenOn:
2926 if (a.getBoolean(attr, false)) {
2927 viewFlagValues |= KEEP_SCREEN_ON;
2928 viewFlagMasks |= KEEP_SCREEN_ON;
2929 }
2930 break;
Jeff Brown85a31762010-09-01 17:01:00 -07002931 case R.styleable.View_filterTouchesWhenObscured:
2932 if (a.getBoolean(attr, false)) {
2933 viewFlagValues |= FILTER_TOUCHES_WHEN_OBSCURED;
2934 viewFlagMasks |= FILTER_TOUCHES_WHEN_OBSCURED;
2935 }
2936 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002937 case R.styleable.View_nextFocusLeft:
2938 mNextFocusLeftId = a.getResourceId(attr, View.NO_ID);
2939 break;
2940 case R.styleable.View_nextFocusRight:
2941 mNextFocusRightId = a.getResourceId(attr, View.NO_ID);
2942 break;
2943 case R.styleable.View_nextFocusUp:
2944 mNextFocusUpId = a.getResourceId(attr, View.NO_ID);
2945 break;
2946 case R.styleable.View_nextFocusDown:
2947 mNextFocusDownId = a.getResourceId(attr, View.NO_ID);
2948 break;
Jeff Brown4e6319b2010-12-13 10:36:51 -08002949 case R.styleable.View_nextFocusForward:
2950 mNextFocusForwardId = a.getResourceId(attr, View.NO_ID);
2951 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002952 case R.styleable.View_minWidth:
2953 mMinWidth = a.getDimensionPixelSize(attr, 0);
2954 break;
2955 case R.styleable.View_minHeight:
2956 mMinHeight = a.getDimensionPixelSize(attr, 0);
2957 break;
Romain Guy9a817362009-05-01 10:57:14 -07002958 case R.styleable.View_onClick:
Romain Guy870e09f2009-07-06 16:35:25 -07002959 if (context.isRestricted()) {
Joe Malin32736f02011-01-19 16:14:20 -08002960 throw new IllegalStateException("The android:onClick attribute cannot "
Romain Guy870e09f2009-07-06 16:35:25 -07002961 + "be used within a restricted context");
2962 }
2963
Romain Guy9a817362009-05-01 10:57:14 -07002964 final String handlerName = a.getString(attr);
2965 if (handlerName != null) {
2966 setOnClickListener(new OnClickListener() {
2967 private Method mHandler;
2968
2969 public void onClick(View v) {
2970 if (mHandler == null) {
2971 try {
2972 mHandler = getContext().getClass().getMethod(handlerName,
2973 View.class);
2974 } catch (NoSuchMethodException e) {
Joe Onorato42e14d72010-03-11 14:51:17 -08002975 int id = getId();
2976 String idText = id == NO_ID ? "" : " with id '"
2977 + getContext().getResources().getResourceEntryName(
2978 id) + "'";
Romain Guy9a817362009-05-01 10:57:14 -07002979 throw new IllegalStateException("Could not find a method " +
Joe Onorato42e14d72010-03-11 14:51:17 -08002980 handlerName + "(View) in the activity "
2981 + getContext().getClass() + " for onClick handler"
2982 + " on view " + View.this.getClass() + idText, e);
Romain Guy9a817362009-05-01 10:57:14 -07002983 }
2984 }
2985
2986 try {
2987 mHandler.invoke(getContext(), View.this);
2988 } catch (IllegalAccessException e) {
2989 throw new IllegalStateException("Could not execute non "
2990 + "public method of the activity", e);
2991 } catch (InvocationTargetException e) {
2992 throw new IllegalStateException("Could not execute "
2993 + "method of the activity", e);
2994 }
2995 }
2996 });
2997 }
2998 break;
Adam Powell637d3372010-08-25 14:37:03 -07002999 case R.styleable.View_overScrollMode:
3000 overScrollMode = a.getInt(attr, OVER_SCROLL_IF_CONTENT_SCROLLS);
3001 break;
Adam Powell20232d02010-12-08 21:08:53 -08003002 case R.styleable.View_verticalScrollbarPosition:
3003 mVerticalScrollbarPosition = a.getInt(attr, SCROLLBAR_POSITION_DEFAULT);
3004 break;
Romain Guy171c5922011-01-06 10:04:23 -08003005 case R.styleable.View_layerType:
3006 setLayerType(a.getInt(attr, LAYER_TYPE_NONE), null);
3007 break;
Fabrice Di Meglio22268862011-06-27 18:13:18 -07003008 case R.styleable.View_textDirection:
3009 mTextDirection = a.getInt(attr, DEFAULT_TEXT_DIRECTION);
3010 break;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003011 }
3012 }
3013
Adam Powell637d3372010-08-25 14:37:03 -07003014 setOverScrollMode(overScrollMode);
3015
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003016 if (background != null) {
3017 setBackgroundDrawable(background);
3018 }
3019
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003020 mUserPaddingRelative = (startPadding >= 0 || endPadding >= 0);
3021
Fabrice Di Megliof9e36502011-06-21 18:41:48 -07003022 // Cache user padding as we cannot fully resolve padding here (we dont have yet the resolved
3023 // layout direction). Those cached values will be used later during padding resolution.
3024 mUserPaddingStart = startPadding;
3025 mUserPaddingEnd = endPadding;
3026
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003027 if (padding >= 0) {
3028 leftPadding = padding;
3029 topPadding = padding;
3030 rightPadding = padding;
3031 bottomPadding = padding;
3032 }
3033
3034 // If the user specified the padding (either with android:padding or
3035 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
3036 // use the default padding or the padding from the background drawable
3037 // (stored at this point in mPadding*)
3038 setPadding(leftPadding >= 0 ? leftPadding : mPaddingLeft,
3039 topPadding >= 0 ? topPadding : mPaddingTop,
3040 rightPadding >= 0 ? rightPadding : mPaddingRight,
3041 bottomPadding >= 0 ? bottomPadding : mPaddingBottom);
3042
3043 if (viewFlagMasks != 0) {
3044 setFlags(viewFlagValues, viewFlagMasks);
3045 }
3046
3047 // Needs to be called after mViewFlags is set
3048 if (scrollbarStyle != SCROLLBARS_INSIDE_OVERLAY) {
3049 recomputePadding();
3050 }
3051
3052 if (x != 0 || y != 0) {
3053 scrollTo(x, y);
3054 }
3055
Chet Haase73066682010-11-29 15:55:32 -08003056 if (transformSet) {
3057 setTranslationX(tx);
3058 setTranslationY(ty);
3059 setRotation(rotation);
3060 setRotationX(rotationX);
3061 setRotationY(rotationY);
3062 setScaleX(sx);
3063 setScaleY(sy);
3064 }
3065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003066 if (!setScrollContainer && (viewFlagValues&SCROLLBARS_VERTICAL) != 0) {
3067 setScrollContainer(true);
3068 }
Romain Guy8f1344f52009-05-15 16:03:59 -07003069
3070 computeOpaqueFlags();
3071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003072 a.recycle();
3073 }
3074
3075 /**
3076 * Non-public constructor for use in testing
3077 */
3078 View() {
3079 }
3080
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003081 /**
3082 * <p>
3083 * Initializes the fading edges from a given set of styled attributes. This
3084 * method should be called by subclasses that need fading edges and when an
3085 * instance of these subclasses is created programmatically rather than
3086 * being inflated from XML. This method is automatically called when the XML
3087 * is inflated.
3088 * </p>
3089 *
3090 * @param a the styled attributes set to initialize the fading edges from
3091 */
3092 protected void initializeFadingEdge(TypedArray a) {
3093 initScrollCache();
3094
3095 mScrollCache.fadingEdgeLength = a.getDimensionPixelSize(
3096 R.styleable.View_fadingEdgeLength,
3097 ViewConfiguration.get(mContext).getScaledFadingEdgeLength());
3098 }
3099
3100 /**
3101 * Returns the size of the vertical faded edges used to indicate that more
3102 * content in this view is visible.
3103 *
3104 * @return The size in pixels of the vertical faded edge or 0 if vertical
3105 * faded edges are not enabled for this view.
3106 * @attr ref android.R.styleable#View_fadingEdgeLength
3107 */
3108 public int getVerticalFadingEdgeLength() {
3109 if (isVerticalFadingEdgeEnabled()) {
3110 ScrollabilityCache cache = mScrollCache;
3111 if (cache != null) {
3112 return cache.fadingEdgeLength;
3113 }
3114 }
3115 return 0;
3116 }
3117
3118 /**
3119 * Set the size of the faded edge used to indicate that more content in this
3120 * view is available. Will not change whether the fading edge is enabled; use
Romain Guy5c22a8c2011-05-13 11:48:45 -07003121 * {@link #setVerticalFadingEdgeEnabled(boolean)} or
3122 * {@link #setHorizontalFadingEdgeEnabled(boolean)} to enable the fading edge
3123 * for the vertical or horizontal fading edges.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003124 *
3125 * @param length The size in pixels of the faded edge used to indicate that more
3126 * content in this view is visible.
3127 */
3128 public void setFadingEdgeLength(int length) {
3129 initScrollCache();
3130 mScrollCache.fadingEdgeLength = length;
3131 }
3132
3133 /**
3134 * Returns the size of the horizontal faded edges used to indicate that more
3135 * content in this view is visible.
3136 *
3137 * @return The size in pixels of the horizontal faded edge or 0 if horizontal
3138 * faded edges are not enabled for this view.
3139 * @attr ref android.R.styleable#View_fadingEdgeLength
3140 */
3141 public int getHorizontalFadingEdgeLength() {
3142 if (isHorizontalFadingEdgeEnabled()) {
3143 ScrollabilityCache cache = mScrollCache;
3144 if (cache != null) {
3145 return cache.fadingEdgeLength;
3146 }
3147 }
3148 return 0;
3149 }
3150
3151 /**
3152 * Returns the width of the vertical scrollbar.
3153 *
3154 * @return The width in pixels of the vertical scrollbar or 0 if there
3155 * is no vertical scrollbar.
3156 */
3157 public int getVerticalScrollbarWidth() {
3158 ScrollabilityCache cache = mScrollCache;
3159 if (cache != null) {
3160 ScrollBarDrawable scrollBar = cache.scrollBar;
3161 if (scrollBar != null) {
3162 int size = scrollBar.getSize(true);
3163 if (size <= 0) {
3164 size = cache.scrollBarSize;
3165 }
3166 return size;
3167 }
3168 return 0;
3169 }
3170 return 0;
3171 }
3172
3173 /**
3174 * Returns the height of the horizontal scrollbar.
3175 *
3176 * @return The height in pixels of the horizontal scrollbar or 0 if
3177 * there is no horizontal scrollbar.
3178 */
3179 protected int getHorizontalScrollbarHeight() {
3180 ScrollabilityCache cache = mScrollCache;
3181 if (cache != null) {
3182 ScrollBarDrawable scrollBar = cache.scrollBar;
3183 if (scrollBar != null) {
3184 int size = scrollBar.getSize(false);
3185 if (size <= 0) {
3186 size = cache.scrollBarSize;
3187 }
3188 return size;
3189 }
3190 return 0;
3191 }
3192 return 0;
3193 }
3194
3195 /**
3196 * <p>
3197 * Initializes the scrollbars from a given set of styled attributes. This
3198 * method should be called by subclasses that need scrollbars and when an
3199 * instance of these subclasses is created programmatically rather than
3200 * being inflated from XML. This method is automatically called when the XML
3201 * is inflated.
3202 * </p>
3203 *
3204 * @param a the styled attributes set to initialize the scrollbars from
3205 */
3206 protected void initializeScrollbars(TypedArray a) {
3207 initScrollCache();
3208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003209 final ScrollabilityCache scrollabilityCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08003210
Mike Cleronf116bf82009-09-27 19:14:12 -07003211 if (scrollabilityCache.scrollBar == null) {
3212 scrollabilityCache.scrollBar = new ScrollBarDrawable();
3213 }
Joe Malin32736f02011-01-19 16:14:20 -08003214
Romain Guy8bda2482010-03-02 11:42:11 -08003215 final boolean fadeScrollbars = a.getBoolean(R.styleable.View_fadeScrollbars, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003216
Mike Cleronf116bf82009-09-27 19:14:12 -07003217 if (!fadeScrollbars) {
3218 scrollabilityCache.state = ScrollabilityCache.ON;
3219 }
3220 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Joe Malin32736f02011-01-19 16:14:20 -08003221
3222
Mike Cleronf116bf82009-09-27 19:14:12 -07003223 scrollabilityCache.scrollBarFadeDuration = a.getInt(
3224 R.styleable.View_scrollbarFadeDuration, ViewConfiguration
3225 .getScrollBarFadeDuration());
3226 scrollabilityCache.scrollBarDefaultDelayBeforeFade = a.getInt(
3227 R.styleable.View_scrollbarDefaultDelayBeforeFade,
3228 ViewConfiguration.getScrollDefaultDelay());
3229
Joe Malin32736f02011-01-19 16:14:20 -08003230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003231 scrollabilityCache.scrollBarSize = a.getDimensionPixelSize(
3232 com.android.internal.R.styleable.View_scrollbarSize,
3233 ViewConfiguration.get(mContext).getScaledScrollBarSize());
3234
3235 Drawable track = a.getDrawable(R.styleable.View_scrollbarTrackHorizontal);
3236 scrollabilityCache.scrollBar.setHorizontalTrackDrawable(track);
3237
3238 Drawable thumb = a.getDrawable(R.styleable.View_scrollbarThumbHorizontal);
3239 if (thumb != null) {
3240 scrollabilityCache.scrollBar.setHorizontalThumbDrawable(thumb);
3241 }
3242
3243 boolean alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawHorizontalTrack,
3244 false);
3245 if (alwaysDraw) {
3246 scrollabilityCache.scrollBar.setAlwaysDrawHorizontalTrack(true);
3247 }
3248
3249 track = a.getDrawable(R.styleable.View_scrollbarTrackVertical);
3250 scrollabilityCache.scrollBar.setVerticalTrackDrawable(track);
3251
3252 thumb = a.getDrawable(R.styleable.View_scrollbarThumbVertical);
3253 if (thumb != null) {
3254 scrollabilityCache.scrollBar.setVerticalThumbDrawable(thumb);
3255 }
3256
3257 alwaysDraw = a.getBoolean(R.styleable.View_scrollbarAlwaysDrawVerticalTrack,
3258 false);
3259 if (alwaysDraw) {
3260 scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true);
3261 }
3262
3263 // Re-apply user/background padding so that scrollbar(s) get added
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003264 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003265 }
3266
3267 /**
3268 * <p>
3269 * Initalizes the scrollability cache if necessary.
3270 * </p>
3271 */
3272 private void initScrollCache() {
3273 if (mScrollCache == null) {
Mike Cleronf116bf82009-09-27 19:14:12 -07003274 mScrollCache = new ScrollabilityCache(ViewConfiguration.get(mContext), this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003275 }
3276 }
3277
3278 /**
Adam Powell20232d02010-12-08 21:08:53 -08003279 * Set the position of the vertical scroll bar. Should be one of
3280 * {@link #SCROLLBAR_POSITION_DEFAULT}, {@link #SCROLLBAR_POSITION_LEFT} or
3281 * {@link #SCROLLBAR_POSITION_RIGHT}.
3282 *
3283 * @param position Where the vertical scroll bar should be positioned.
3284 */
3285 public void setVerticalScrollbarPosition(int position) {
3286 if (mVerticalScrollbarPosition != position) {
3287 mVerticalScrollbarPosition = position;
3288 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07003289 resolvePadding();
Adam Powell20232d02010-12-08 21:08:53 -08003290 }
3291 }
3292
3293 /**
3294 * @return The position where the vertical scroll bar will show, if applicable.
3295 * @see #setVerticalScrollbarPosition(int)
3296 */
3297 public int getVerticalScrollbarPosition() {
3298 return mVerticalScrollbarPosition;
3299 }
3300
3301 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003302 * Register a callback to be invoked when focus of this view changed.
3303 *
3304 * @param l The callback that will run.
3305 */
3306 public void setOnFocusChangeListener(OnFocusChangeListener l) {
3307 mOnFocusChangeListener = l;
3308 }
3309
3310 /**
Chet Haase21cd1382010-09-01 17:42:29 -07003311 * Add a listener that will be called when the bounds of the view change due to
3312 * layout processing.
3313 *
3314 * @param listener The listener that will be called when layout bounds change.
3315 */
3316 public void addOnLayoutChangeListener(OnLayoutChangeListener listener) {
3317 if (mOnLayoutChangeListeners == null) {
3318 mOnLayoutChangeListeners = new ArrayList<OnLayoutChangeListener>();
3319 }
3320 mOnLayoutChangeListeners.add(listener);
3321 }
3322
3323 /**
3324 * Remove a listener for layout changes.
3325 *
3326 * @param listener The listener for layout bounds change.
3327 */
3328 public void removeOnLayoutChangeListener(OnLayoutChangeListener listener) {
3329 if (mOnLayoutChangeListeners == null) {
3330 return;
3331 }
3332 mOnLayoutChangeListeners.remove(listener);
3333 }
3334
3335 /**
Adam Powell4afd62b2011-02-18 15:02:18 -08003336 * Add a listener for attach state changes.
3337 *
3338 * This listener will be called whenever this view is attached or detached
3339 * from a window. Remove the listener using
3340 * {@link #removeOnAttachStateChangeListener(OnAttachStateChangeListener)}.
3341 *
3342 * @param listener Listener to attach
3343 * @see #removeOnAttachStateChangeListener(OnAttachStateChangeListener)
3344 */
3345 public void addOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3346 if (mOnAttachStateChangeListeners == null) {
3347 mOnAttachStateChangeListeners = new CopyOnWriteArrayList<OnAttachStateChangeListener>();
3348 }
3349 mOnAttachStateChangeListeners.add(listener);
3350 }
3351
3352 /**
3353 * Remove a listener for attach state changes. The listener will receive no further
3354 * notification of window attach/detach events.
3355 *
3356 * @param listener Listener to remove
3357 * @see #addOnAttachStateChangeListener(OnAttachStateChangeListener)
3358 */
3359 public void removeOnAttachStateChangeListener(OnAttachStateChangeListener listener) {
3360 if (mOnAttachStateChangeListeners == null) {
3361 return;
3362 }
3363 mOnAttachStateChangeListeners.remove(listener);
3364 }
3365
3366 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003367 * Returns the focus-change callback registered for this view.
3368 *
3369 * @return The callback, or null if one is not registered.
3370 */
3371 public OnFocusChangeListener getOnFocusChangeListener() {
3372 return mOnFocusChangeListener;
3373 }
3374
3375 /**
3376 * Register a callback to be invoked when this view is clicked. If this view is not
3377 * clickable, it becomes clickable.
3378 *
3379 * @param l The callback that will run
3380 *
3381 * @see #setClickable(boolean)
3382 */
3383 public void setOnClickListener(OnClickListener l) {
3384 if (!isClickable()) {
3385 setClickable(true);
3386 }
3387 mOnClickListener = l;
3388 }
3389
3390 /**
3391 * Register a callback to be invoked when this view is clicked and held. If this view is not
3392 * long clickable, it becomes long clickable.
3393 *
3394 * @param l The callback that will run
3395 *
3396 * @see #setLongClickable(boolean)
3397 */
3398 public void setOnLongClickListener(OnLongClickListener l) {
3399 if (!isLongClickable()) {
3400 setLongClickable(true);
3401 }
3402 mOnLongClickListener = l;
3403 }
3404
3405 /**
3406 * Register a callback to be invoked when the context menu for this view is
3407 * being built. If this view is not long clickable, it becomes long clickable.
3408 *
3409 * @param l The callback that will run
3410 *
3411 */
3412 public void setOnCreateContextMenuListener(OnCreateContextMenuListener l) {
3413 if (!isLongClickable()) {
3414 setLongClickable(true);
3415 }
3416 mOnCreateContextMenuListener = l;
3417 }
3418
3419 /**
3420 * Call this view's OnClickListener, if it is defined.
3421 *
3422 * @return True there was an assigned OnClickListener that was called, false
3423 * otherwise is returned.
3424 */
3425 public boolean performClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003426 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_CLICKED);
3427
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003428 if (mOnClickListener != null) {
3429 playSoundEffect(SoundEffectConstants.CLICK);
3430 mOnClickListener.onClick(this);
3431 return true;
3432 }
3433
3434 return false;
3435 }
3436
3437 /**
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003438 * Call this view's OnLongClickListener, if it is defined. Invokes the context menu if the
3439 * OnLongClickListener did not consume the event.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003440 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07003441 * @return True if one of the above receivers consumed the event, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003442 */
3443 public boolean performLongClick() {
svetoslavganov75986cf2009-05-14 22:28:01 -07003444 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_LONG_CLICKED);
3445
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003446 boolean handled = false;
3447 if (mOnLongClickListener != null) {
3448 handled = mOnLongClickListener.onLongClick(View.this);
3449 }
3450 if (!handled) {
3451 handled = showContextMenu();
3452 }
3453 if (handled) {
3454 performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
3455 }
3456 return handled;
3457 }
3458
3459 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003460 * Performs button-related actions during a touch down event.
3461 *
3462 * @param event The event.
3463 * @return True if the down was consumed.
3464 *
3465 * @hide
3466 */
3467 protected boolean performButtonActionOnTouchDown(MotionEvent event) {
3468 if ((event.getButtonState() & MotionEvent.BUTTON_SECONDARY) != 0) {
3469 if (showContextMenu(event.getX(), event.getY(), event.getMetaState())) {
3470 return true;
3471 }
3472 }
3473 return false;
3474 }
3475
3476 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003477 * Bring up the context menu for this view.
3478 *
3479 * @return Whether a context menu was displayed.
3480 */
3481 public boolean showContextMenu() {
3482 return getParent().showContextMenuForChild(this);
3483 }
3484
3485 /**
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07003486 * Bring up the context menu for this view, referring to the item under the specified point.
3487 *
3488 * @param x The referenced x coordinate.
3489 * @param y The referenced y coordinate.
3490 * @param metaState The keyboard modifiers that were pressed.
3491 * @return Whether a context menu was displayed.
3492 *
3493 * @hide
3494 */
3495 public boolean showContextMenu(float x, float y, int metaState) {
3496 return showContextMenu();
3497 }
3498
3499 /**
Adam Powell6e346362010-07-23 10:18:23 -07003500 * Start an action mode.
3501 *
3502 * @param callback Callback that will control the lifecycle of the action mode
3503 * @return The new action mode if it is started, null otherwise
3504 *
3505 * @see ActionMode
3506 */
3507 public ActionMode startActionMode(ActionMode.Callback callback) {
3508 return getParent().startActionModeForChild(this, callback);
3509 }
3510
3511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003512 * Register a callback to be invoked when a key is pressed in this view.
3513 * @param l the key listener to attach to this view
3514 */
3515 public void setOnKeyListener(OnKeyListener l) {
3516 mOnKeyListener = l;
3517 }
3518
3519 /**
3520 * Register a callback to be invoked when a touch event is sent to this view.
3521 * @param l the touch listener to attach to this view
3522 */
3523 public void setOnTouchListener(OnTouchListener l) {
3524 mOnTouchListener = l;
3525 }
3526
3527 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08003528 * Register a callback to be invoked when a generic motion event is sent to this view.
3529 * @param l the generic motion listener to attach to this view
3530 */
3531 public void setOnGenericMotionListener(OnGenericMotionListener l) {
3532 mOnGenericMotionListener = l;
3533 }
3534
3535 /**
Jeff Brown53ca3f12011-06-27 18:36:00 -07003536 * Register a callback to be invoked when a hover event is sent to this view.
3537 * @param l the hover listener to attach to this view
3538 */
3539 public void setOnHoverListener(OnHoverListener l) {
3540 mOnHoverListener = l;
3541 }
3542
3543 /**
Joe Malin32736f02011-01-19 16:14:20 -08003544 * Register a drag event listener callback object for this View. The parameter is
3545 * an implementation of {@link android.view.View.OnDragListener}. To send a drag event to a
3546 * View, the system calls the
3547 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent)} method.
3548 * @param l An implementation of {@link android.view.View.OnDragListener}.
Chris Tate32affef2010-10-18 15:29:21 -07003549 */
3550 public void setOnDragListener(OnDragListener l) {
3551 mOnDragListener = l;
3552 }
3553
3554 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07003555 * Give this view focus. This will cause
3556 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003557 *
3558 * Note: this does not check whether this {@link View} should get focus, it just
3559 * gives it focus no matter what. It should only be called internally by framework
3560 * code that knows what it is doing, namely {@link #requestFocus(int, Rect)}.
3561 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08003562 * @param direction values are {@link View#FOCUS_UP}, {@link View#FOCUS_DOWN},
3563 * {@link View#FOCUS_LEFT} or {@link View#FOCUS_RIGHT}. This is the direction which
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003564 * focus moved when requestFocus() is called. It may not always
3565 * apply, in which case use the default View.FOCUS_DOWN.
3566 * @param previouslyFocusedRect The rectangle of the view that had focus
3567 * prior in this View's coordinate system.
3568 */
3569 void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) {
3570 if (DBG) {
3571 System.out.println(this + " requestFocus()");
3572 }
3573
3574 if ((mPrivateFlags & FOCUSED) == 0) {
3575 mPrivateFlags |= FOCUSED;
3576
3577 if (mParent != null) {
3578 mParent.requestChildFocus(this, this);
3579 }
3580
3581 onFocusChanged(true, direction, previouslyFocusedRect);
3582 refreshDrawableState();
3583 }
3584 }
3585
3586 /**
3587 * Request that a rectangle of this view be visible on the screen,
3588 * scrolling if necessary just enough.
3589 *
3590 * <p>A View should call this if it maintains some notion of which part
3591 * of its content is interesting. For example, a text editing view
3592 * should call this when its cursor moves.
3593 *
3594 * @param rectangle The rectangle.
3595 * @return Whether any parent scrolled.
3596 */
3597 public boolean requestRectangleOnScreen(Rect rectangle) {
3598 return requestRectangleOnScreen(rectangle, false);
3599 }
3600
3601 /**
3602 * Request that a rectangle of this view be visible on the screen,
3603 * scrolling if necessary just enough.
3604 *
3605 * <p>A View should call this if it maintains some notion of which part
3606 * of its content is interesting. For example, a text editing view
3607 * should call this when its cursor moves.
3608 *
3609 * <p>When <code>immediate</code> is set to true, scrolling will not be
3610 * animated.
3611 *
3612 * @param rectangle The rectangle.
3613 * @param immediate True to forbid animated scrolling, false otherwise
3614 * @return Whether any parent scrolled.
3615 */
3616 public boolean requestRectangleOnScreen(Rect rectangle, boolean immediate) {
3617 View child = this;
3618 ViewParent parent = mParent;
3619 boolean scrolled = false;
3620 while (parent != null) {
3621 scrolled |= parent.requestChildRectangleOnScreen(child,
3622 rectangle, immediate);
3623
3624 // offset rect so next call has the rectangle in the
3625 // coordinate system of its direct child.
3626 rectangle.offset(child.getLeft(), child.getTop());
3627 rectangle.offset(-child.getScrollX(), -child.getScrollY());
3628
3629 if (!(parent instanceof View)) {
3630 break;
3631 }
Romain Guy8506ab42009-06-11 17:35:47 -07003632
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003633 child = (View) parent;
3634 parent = child.getParent();
3635 }
3636 return scrolled;
3637 }
3638
3639 /**
3640 * Called when this view wants to give up focus. This will cause
Romain Guy5c22a8c2011-05-13 11:48:45 -07003641 * {@link #onFocusChanged(boolean, int, android.graphics.Rect)} to be called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003642 */
3643 public void clearFocus() {
3644 if (DBG) {
3645 System.out.println(this + " clearFocus()");
3646 }
3647
3648 if ((mPrivateFlags & FOCUSED) != 0) {
3649 mPrivateFlags &= ~FOCUSED;
3650
3651 if (mParent != null) {
3652 mParent.clearChildFocus(this);
3653 }
3654
3655 onFocusChanged(false, 0, null);
3656 refreshDrawableState();
3657 }
3658 }
3659
3660 /**
3661 * Called to clear the focus of a view that is about to be removed.
3662 * Doesn't call clearChildFocus, which prevents this view from taking
3663 * focus again before it has been removed from the parent
3664 */
3665 void clearFocusForRemoval() {
3666 if ((mPrivateFlags & FOCUSED) != 0) {
3667 mPrivateFlags &= ~FOCUSED;
3668
3669 onFocusChanged(false, 0, null);
3670 refreshDrawableState();
3671 }
3672 }
3673
3674 /**
3675 * Called internally by the view system when a new view is getting focus.
3676 * This is what clears the old focus.
3677 */
3678 void unFocus() {
3679 if (DBG) {
3680 System.out.println(this + " unFocus()");
3681 }
3682
3683 if ((mPrivateFlags & FOCUSED) != 0) {
3684 mPrivateFlags &= ~FOCUSED;
3685
3686 onFocusChanged(false, 0, null);
3687 refreshDrawableState();
3688 }
3689 }
3690
3691 /**
3692 * Returns true if this view has focus iteself, or is the ancestor of the
3693 * view that has focus.
3694 *
3695 * @return True if this view has or contains focus, false otherwise.
3696 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07003697 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003698 public boolean hasFocus() {
3699 return (mPrivateFlags & FOCUSED) != 0;
3700 }
3701
3702 /**
3703 * Returns true if this view is focusable or if it contains a reachable View
3704 * for which {@link #hasFocusable()} returns true. A "reachable hasFocusable()"
3705 * is a View whose parents do not block descendants focus.
3706 *
3707 * Only {@link #VISIBLE} views are considered focusable.
3708 *
3709 * @return True if the view is focusable or if the view contains a focusable
3710 * View, false otherwise.
3711 *
3712 * @see ViewGroup#FOCUS_BLOCK_DESCENDANTS
3713 */
3714 public boolean hasFocusable() {
3715 return (mViewFlags & VISIBILITY_MASK) == VISIBLE && isFocusable();
3716 }
3717
3718 /**
3719 * Called by the view system when the focus state of this view changes.
3720 * When the focus change event is caused by directional navigation, direction
3721 * and previouslyFocusedRect provide insight into where the focus is coming from.
3722 * When overriding, be sure to call up through to the super class so that
3723 * the standard focus handling will occur.
Romain Guy8506ab42009-06-11 17:35:47 -07003724 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003725 * @param gainFocus True if the View has focus; false otherwise.
3726 * @param direction The direction focus has moved when requestFocus()
3727 * is called to give this view focus. Values are
Jeff Brown4e6319b2010-12-13 10:36:51 -08003728 * {@link #FOCUS_UP}, {@link #FOCUS_DOWN}, {@link #FOCUS_LEFT},
3729 * {@link #FOCUS_RIGHT}, {@link #FOCUS_FORWARD}, or {@link #FOCUS_BACKWARD}.
3730 * It may not always apply, in which case use the default.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003731 * @param previouslyFocusedRect The rectangle, in this view's coordinate
3732 * system, of the previously focused view. If applicable, this will be
3733 * passed in as finer grained information about where the focus is coming
3734 * from (in addition to direction). Will be <code>null</code> otherwise.
3735 */
3736 protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) {
svetoslavganov75986cf2009-05-14 22:28:01 -07003737 if (gainFocus) {
3738 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_FOCUSED);
3739 }
3740
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003741 InputMethodManager imm = InputMethodManager.peekInstance();
3742 if (!gainFocus) {
3743 if (isPressed()) {
3744 setPressed(false);
3745 }
3746 if (imm != null && mAttachInfo != null
3747 && mAttachInfo.mHasWindowFocus) {
3748 imm.focusOut(this);
3749 }
Romain Guya2431d02009-04-30 16:30:00 -07003750 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003751 } else if (imm != null && mAttachInfo != null
3752 && mAttachInfo.mHasWindowFocus) {
3753 imm.focusIn(this);
3754 }
Romain Guy8506ab42009-06-11 17:35:47 -07003755
Romain Guy0fd89bf2011-01-26 15:41:30 -08003756 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003757 if (mOnFocusChangeListener != null) {
3758 mOnFocusChangeListener.onFocusChange(this, gainFocus);
3759 }
Joe Malin32736f02011-01-19 16:14:20 -08003760
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07003761 if (mAttachInfo != null) {
3762 mAttachInfo.mKeyDispatchState.reset(this);
3763 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003764 }
3765
3766 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003767 * Sends an accessibility event of the given type. If accessiiblity is
3768 * not enabled this method has no effect. The default implementation calls
3769 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)} first
3770 * to populate information about the event source (this View), then calls
3771 * {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)} to
3772 * populate the text content of the event source including its descendants,
3773 * and last calls
3774 * {@link ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)}
3775 * on its parent to resuest sending of the event to interested parties.
3776 *
3777 * @param eventType The type of the event to send.
3778 *
3779 * @see #onInitializeAccessibilityEvent(AccessibilityEvent)
3780 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3781 * @see ViewParent#requestSendAccessibilityEvent(View, AccessibilityEvent)
svetoslavganov75986cf2009-05-14 22:28:01 -07003782 */
3783 public void sendAccessibilityEvent(int eventType) {
3784 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
3785 sendAccessibilityEventUnchecked(AccessibilityEvent.obtain(eventType));
3786 }
3787 }
3788
3789 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003790 * This method behaves exactly as {@link #sendAccessibilityEvent(int)} but
3791 * takes as an argument an empty {@link AccessibilityEvent} and does not
3792 * perfrom a check whether accessibility is enabled.
3793 *
3794 * @param event The event to send.
3795 *
3796 * @see #sendAccessibilityEvent(int)
svetoslavganov75986cf2009-05-14 22:28:01 -07003797 */
3798 public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
Svetoslav Ganov9cd1eca2011-01-13 14:24:02 -08003799 if (!isShown()) {
3800 return;
3801 }
Svetoslav Ganov30401322011-05-12 18:53:45 -07003802 onInitializeAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003803 dispatchPopulateAccessibilityEvent(event);
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003804 // In the beginning we called #isShown(), so we know that getParent() is not null.
3805 getParent().requestSendAccessibilityEvent(this, event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003806 }
3807
3808 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003809 * Dispatches an {@link AccessibilityEvent} to the {@link View} first and then
3810 * to its children for adding their text content to the event. Note that the
3811 * event text is populated in a separate dispatch path since we add to the
3812 * event not only the text of the source but also the text of all its descendants.
3813 * </p>
3814 * A typical implementation will call
3815 * {@link #onPopulateAccessibilityEvent(AccessibilityEvent)} on the this view
3816 * and then call the {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
3817 * on each child. Override this method if custom population of the event text
3818 * content is required.
svetoslavganov75986cf2009-05-14 22:28:01 -07003819 *
3820 * @param event The event.
3821 *
3822 * @return True if the event population was completed.
3823 */
3824 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003825 onPopulateAccessibilityEvent(event);
svetoslavganov75986cf2009-05-14 22:28:01 -07003826 return false;
3827 }
3828
3829 /**
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003830 * Called from {@link #dispatchPopulateAccessibilityEvent(AccessibilityEvent)}
Svetoslav Ganov30401322011-05-12 18:53:45 -07003831 * giving a chance to this View to populate the accessibility event with its
3832 * text content. While the implementation is free to modify other event
3833 * attributes this should be performed in
3834 * {@link #onInitializeAccessibilityEvent(AccessibilityEvent)}.
3835 * <p>
3836 * Example: Adding formatted date string to an accessibility event in addition
3837 * to the text added by the super implementation.
3838 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003839 * public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
3840 * super.onPopulateAccessibilityEvent(event);
3841 * final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY;
3842 * String selectedDateUtterance = DateUtils.formatDateTime(mContext,
3843 * mCurrentDate.getTimeInMillis(), flags);
3844 * event.getText().add(selectedDateUtterance);
3845 * }
3846 * </code></pre></p>
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003847 *
3848 * @param event The accessibility event which to populate.
Svetoslav Ganov30401322011-05-12 18:53:45 -07003849 *
3850 * @see #sendAccessibilityEvent(int)
3851 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003852 */
3853 public void onPopulateAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov736c2752011-04-22 18:30:36 -07003854 }
3855
3856 /**
Svetoslav Ganov30401322011-05-12 18:53:45 -07003857 * Initializes an {@link AccessibilityEvent} with information about the
3858 * the type of the event and this View which is the event source. In other
3859 * words, the source of an accessibility event is the view whose state
3860 * change triggered firing the event.
3861 * <p>
3862 * Example: Setting the password property of an event in addition
3863 * to properties set by the super implementation.
3864 * </p><p><pre><code>
Svetoslav Ganov30401322011-05-12 18:53:45 -07003865 * public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
3866 * super.onInitializeAccessibilityEvent(event);
3867 * event.setPassword(true);
3868 * }
3869 * </code></pre></p>
3870 * @param event The event to initialeze.
3871 *
3872 * @see #sendAccessibilityEvent(int)
3873 * @see #dispatchPopulateAccessibilityEvent(AccessibilityEvent)
3874 */
3875 public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003876 event.setSource(this);
Svetoslav Ganov30401322011-05-12 18:53:45 -07003877 event.setClassName(getClass().getName());
3878 event.setPackageName(getContext().getPackageName());
3879 event.setEnabled(isEnabled());
3880 event.setContentDescription(mContentDescription);
3881
Svetoslav Ganova0156172011-06-26 17:55:44 -07003882 final int eventType = event.getEventType();
3883 switch (eventType) {
3884 case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
3885 if (mAttachInfo != null) {
3886 ArrayList<View> focusablesTempList = mAttachInfo.mFocusablesTempList;
3887 getRootView().addFocusables(focusablesTempList, View.FOCUS_FORWARD,
3888 FOCUSABLES_ALL);
3889 event.setItemCount(focusablesTempList.size());
3890 event.setCurrentItemIndex(focusablesTempList.indexOf(this));
3891 focusablesTempList.clear();
3892 }
3893 } break;
3894 case AccessibilityEvent.TYPE_VIEW_SCROLLED: {
3895 event.setScrollX(mScrollX);
3896 event.setScrollY(mScrollY);
3897 event.setItemCount(getHeight());
3898 } break;
Svetoslav Ganov30401322011-05-12 18:53:45 -07003899 }
3900 }
3901
3902 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003903 * Returns an {@link AccessibilityNodeInfo} representing this view from the
3904 * point of view of an {@link android.accessibilityservice.AccessibilityService}.
3905 * This method is responsible for obtaining an accessibility node info from a
3906 * pool of reusable instances and calling
3907 * {@link #onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo)} on this view to
3908 * initialize the former.
3909 * <p>
3910 * Note: The client is responsible for recycling the obtained instance by calling
3911 * {@link AccessibilityNodeInfo#recycle()} to minimize object creation.
3912 * </p>
3913 * @return A populated {@link AccessibilityNodeInfo}.
3914 *
3915 * @see AccessibilityNodeInfo
3916 */
3917 public AccessibilityNodeInfo createAccessibilityNodeInfo() {
3918 AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain(this);
3919 onInitializeAccessibilityNodeInfo(info);
3920 return info;
3921 }
3922
3923 /**
3924 * Initializes an {@link AccessibilityNodeInfo} with information about this view.
3925 * The base implementation sets:
3926 * <ul>
3927 * <li>{@link AccessibilityNodeInfo#setParent(View)},</li>
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003928 * <li>{@link AccessibilityNodeInfo#setBoundsInParent(Rect)},</li>
3929 * <li>{@link AccessibilityNodeInfo#setBoundsInScreen(Rect)},</li>
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003930 * <li>{@link AccessibilityNodeInfo#setPackageName(CharSequence)},</li>
3931 * <li>{@link AccessibilityNodeInfo#setClassName(CharSequence)},</li>
3932 * <li>{@link AccessibilityNodeInfo#setContentDescription(CharSequence)},</li>
3933 * <li>{@link AccessibilityNodeInfo#setEnabled(boolean)},</li>
3934 * <li>{@link AccessibilityNodeInfo#setClickable(boolean)},</li>
3935 * <li>{@link AccessibilityNodeInfo#setFocusable(boolean)},</li>
3936 * <li>{@link AccessibilityNodeInfo#setFocused(boolean)},</li>
3937 * <li>{@link AccessibilityNodeInfo#setLongClickable(boolean)},</li>
3938 * <li>{@link AccessibilityNodeInfo#setSelected(boolean)},</li>
3939 * </ul>
3940 * <p>
3941 * Subclasses should override this method, call the super implementation,
3942 * and set additional attributes.
3943 * </p>
3944 * @param info The instance to initialize.
3945 */
3946 public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
3947 Rect bounds = mAttachInfo.mTmpInvalRect;
3948 getDrawingRect(bounds);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003949 info.setBoundsInParent(bounds);
3950
3951 int[] locationOnScreen = mAttachInfo.mInvalidateChildLocation;
3952 getLocationOnScreen(locationOnScreen);
Alan Viverette326804f2011-07-22 16:20:41 -07003953 bounds.offsetTo(0, 0);
Svetoslav Ganoveeee4d22011-06-10 20:51:30 -07003954 bounds.offset(locationOnScreen[0], locationOnScreen[1]);
3955 info.setBoundsInScreen(bounds);
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07003956
3957 ViewParent parent = getParent();
3958 if (parent instanceof View) {
3959 View parentView = (View) parent;
3960 info.setParent(parentView);
3961 }
3962
3963 info.setPackageName(mContext.getPackageName());
3964 info.setClassName(getClass().getName());
3965 info.setContentDescription(getContentDescription());
3966
3967 info.setEnabled(isEnabled());
3968 info.setClickable(isClickable());
3969 info.setFocusable(isFocusable());
3970 info.setFocused(isFocused());
3971 info.setSelected(isSelected());
3972 info.setLongClickable(isLongClickable());
3973
3974 // TODO: These make sense only if we are in an AdapterView but all
3975 // views can be selected. Maybe from accessiiblity perspective
3976 // we should report as selectable view in an AdapterView.
3977 info.addAction(AccessibilityNodeInfo.ACTION_SELECT);
3978 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_SELECTION);
3979
3980 if (isFocusable()) {
3981 if (isFocused()) {
3982 info.addAction(AccessibilityNodeInfo.ACTION_CLEAR_FOCUS);
3983 } else {
3984 info.addAction(AccessibilityNodeInfo.ACTION_FOCUS);
3985 }
3986 }
3987 }
3988
3989 /**
3990 * Gets the unique identifier of this view on the screen for accessibility purposes.
3991 * If this {@link View} is not attached to any window, {@value #NO_ID} is returned.
3992 *
3993 * @return The view accessibility id.
3994 *
3995 * @hide
3996 */
3997 public int getAccessibilityViewId() {
3998 if (mAccessibilityViewId == NO_ID) {
3999 mAccessibilityViewId = sNextAccessibilityViewId++;
4000 }
4001 return mAccessibilityViewId;
4002 }
4003
4004 /**
4005 * Gets the unique identifier of the window in which this View reseides.
4006 *
4007 * @return The window accessibility id.
4008 *
4009 * @hide
4010 */
4011 public int getAccessibilityWindowId() {
4012 return mAttachInfo != null ? mAttachInfo.mAccessibilityWindowId : NO_ID;
4013 }
4014
4015 /**
svetoslavganov75986cf2009-05-14 22:28:01 -07004016 * Gets the {@link View} description. It briefly describes the view and is
4017 * primarily used for accessibility support. Set this property to enable
4018 * better accessibility support for your application. This is especially
4019 * true for views that do not have textual representation (For example,
4020 * ImageButton).
4021 *
4022 * @return The content descriptiopn.
4023 *
4024 * @attr ref android.R.styleable#View_contentDescription
4025 */
4026 public CharSequence getContentDescription() {
4027 return mContentDescription;
4028 }
4029
4030 /**
4031 * Sets the {@link View} description. It briefly describes the view and is
4032 * primarily used for accessibility support. Set this property to enable
4033 * better accessibility support for your application. This is especially
4034 * true for views that do not have textual representation (For example,
4035 * ImageButton).
4036 *
4037 * @param contentDescription The content description.
4038 *
4039 * @attr ref android.R.styleable#View_contentDescription
4040 */
4041 public void setContentDescription(CharSequence contentDescription) {
4042 mContentDescription = contentDescription;
4043 }
4044
4045 /**
Romain Guya2431d02009-04-30 16:30:00 -07004046 * Invoked whenever this view loses focus, either by losing window focus or by losing
4047 * focus within its window. This method can be used to clear any state tied to the
4048 * focus. For instance, if a button is held pressed with the trackball and the window
4049 * loses focus, this method can be used to cancel the press.
4050 *
4051 * Subclasses of View overriding this method should always call super.onFocusLost().
4052 *
4053 * @see #onFocusChanged(boolean, int, android.graphics.Rect)
Romain Guy8506ab42009-06-11 17:35:47 -07004054 * @see #onWindowFocusChanged(boolean)
Romain Guya2431d02009-04-30 16:30:00 -07004055 *
4056 * @hide pending API council approval
4057 */
4058 protected void onFocusLost() {
4059 resetPressedState();
4060 }
4061
4062 private void resetPressedState() {
4063 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
4064 return;
4065 }
4066
4067 if (isPressed()) {
4068 setPressed(false);
4069
4070 if (!mHasPerformedLongPress) {
Maryam Garrett1549dd12009-12-15 16:06:36 -05004071 removeLongPressCallback();
Romain Guya2431d02009-04-30 16:30:00 -07004072 }
4073 }
4074 }
4075
4076 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004077 * Returns true if this view has focus
4078 *
4079 * @return True if this view has focus, false otherwise.
4080 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004081 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004082 public boolean isFocused() {
4083 return (mPrivateFlags & FOCUSED) != 0;
4084 }
4085
4086 /**
4087 * Find the view in the hierarchy rooted at this view that currently has
4088 * focus.
4089 *
4090 * @return The view that currently has focus, or null if no focused view can
4091 * be found.
4092 */
4093 public View findFocus() {
4094 return (mPrivateFlags & FOCUSED) != 0 ? this : null;
4095 }
4096
4097 /**
4098 * Change whether this view is one of the set of scrollable containers in
4099 * its window. This will be used to determine whether the window can
4100 * resize or must pan when a soft input area is open -- scrollable
4101 * containers allow the window to use resize mode since the container
4102 * will appropriately shrink.
4103 */
4104 public void setScrollContainer(boolean isScrollContainer) {
4105 if (isScrollContainer) {
4106 if (mAttachInfo != null && (mPrivateFlags&SCROLL_CONTAINER_ADDED) == 0) {
4107 mAttachInfo.mScrollContainers.add(this);
4108 mPrivateFlags |= SCROLL_CONTAINER_ADDED;
4109 }
4110 mPrivateFlags |= SCROLL_CONTAINER;
4111 } else {
4112 if ((mPrivateFlags&SCROLL_CONTAINER_ADDED) != 0) {
4113 mAttachInfo.mScrollContainers.remove(this);
4114 }
4115 mPrivateFlags &= ~(SCROLL_CONTAINER|SCROLL_CONTAINER_ADDED);
4116 }
4117 }
4118
4119 /**
4120 * Returns the quality of the drawing cache.
4121 *
4122 * @return One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4123 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4124 *
4125 * @see #setDrawingCacheQuality(int)
4126 * @see #setDrawingCacheEnabled(boolean)
4127 * @see #isDrawingCacheEnabled()
4128 *
4129 * @attr ref android.R.styleable#View_drawingCacheQuality
4130 */
4131 public int getDrawingCacheQuality() {
4132 return mViewFlags & DRAWING_CACHE_QUALITY_MASK;
4133 }
4134
4135 /**
4136 * Set the drawing cache quality of this view. This value is used only when the
4137 * drawing cache is enabled
4138 *
4139 * @param quality One of {@link #DRAWING_CACHE_QUALITY_AUTO},
4140 * {@link #DRAWING_CACHE_QUALITY_LOW}, or {@link #DRAWING_CACHE_QUALITY_HIGH}
4141 *
4142 * @see #getDrawingCacheQuality()
4143 * @see #setDrawingCacheEnabled(boolean)
4144 * @see #isDrawingCacheEnabled()
4145 *
4146 * @attr ref android.R.styleable#View_drawingCacheQuality
4147 */
4148 public void setDrawingCacheQuality(int quality) {
4149 setFlags(quality, DRAWING_CACHE_QUALITY_MASK);
4150 }
4151
4152 /**
4153 * Returns whether the screen should remain on, corresponding to the current
4154 * value of {@link #KEEP_SCREEN_ON}.
4155 *
4156 * @return Returns true if {@link #KEEP_SCREEN_ON} is set.
4157 *
4158 * @see #setKeepScreenOn(boolean)
4159 *
4160 * @attr ref android.R.styleable#View_keepScreenOn
4161 */
4162 public boolean getKeepScreenOn() {
4163 return (mViewFlags & KEEP_SCREEN_ON) != 0;
4164 }
4165
4166 /**
4167 * Controls whether the screen should remain on, modifying the
4168 * value of {@link #KEEP_SCREEN_ON}.
4169 *
4170 * @param keepScreenOn Supply true to set {@link #KEEP_SCREEN_ON}.
4171 *
4172 * @see #getKeepScreenOn()
4173 *
4174 * @attr ref android.R.styleable#View_keepScreenOn
4175 */
4176 public void setKeepScreenOn(boolean keepScreenOn) {
4177 setFlags(keepScreenOn ? KEEP_SCREEN_ON : 0, KEEP_SCREEN_ON);
4178 }
4179
4180 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004181 * Gets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4182 * @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 -08004183 *
4184 * @attr ref android.R.styleable#View_nextFocusLeft
4185 */
4186 public int getNextFocusLeftId() {
4187 return mNextFocusLeftId;
4188 }
4189
4190 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004191 * Sets the id of the view to use when the next focus is {@link #FOCUS_LEFT}.
4192 * @param nextFocusLeftId The next focus ID, or {@link #NO_ID} if the framework should
4193 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004194 *
4195 * @attr ref android.R.styleable#View_nextFocusLeft
4196 */
4197 public void setNextFocusLeftId(int nextFocusLeftId) {
4198 mNextFocusLeftId = nextFocusLeftId;
4199 }
4200
4201 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004202 * Gets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4203 * @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 -08004204 *
4205 * @attr ref android.R.styleable#View_nextFocusRight
4206 */
4207 public int getNextFocusRightId() {
4208 return mNextFocusRightId;
4209 }
4210
4211 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004212 * Sets the id of the view to use when the next focus is {@link #FOCUS_RIGHT}.
4213 * @param nextFocusRightId The next focus ID, or {@link #NO_ID} if the framework should
4214 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004215 *
4216 * @attr ref android.R.styleable#View_nextFocusRight
4217 */
4218 public void setNextFocusRightId(int nextFocusRightId) {
4219 mNextFocusRightId = nextFocusRightId;
4220 }
4221
4222 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004223 * Gets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4224 * @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 -08004225 *
4226 * @attr ref android.R.styleable#View_nextFocusUp
4227 */
4228 public int getNextFocusUpId() {
4229 return mNextFocusUpId;
4230 }
4231
4232 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004233 * Sets the id of the view to use when the next focus is {@link #FOCUS_UP}.
4234 * @param nextFocusUpId The next focus ID, or {@link #NO_ID} if the framework should
4235 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004236 *
4237 * @attr ref android.R.styleable#View_nextFocusUp
4238 */
4239 public void setNextFocusUpId(int nextFocusUpId) {
4240 mNextFocusUpId = nextFocusUpId;
4241 }
4242
4243 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004244 * Gets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4245 * @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 -08004246 *
4247 * @attr ref android.R.styleable#View_nextFocusDown
4248 */
4249 public int getNextFocusDownId() {
4250 return mNextFocusDownId;
4251 }
4252
4253 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004254 * Sets the id of the view to use when the next focus is {@link #FOCUS_DOWN}.
4255 * @param nextFocusDownId The next focus ID, or {@link #NO_ID} if the framework should
4256 * decide automatically.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004257 *
4258 * @attr ref android.R.styleable#View_nextFocusDown
4259 */
4260 public void setNextFocusDownId(int nextFocusDownId) {
4261 mNextFocusDownId = nextFocusDownId;
4262 }
4263
4264 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -08004265 * Gets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4266 * @return The next focus ID, or {@link #NO_ID} if the framework should decide automatically.
4267 *
4268 * @attr ref android.R.styleable#View_nextFocusForward
4269 */
4270 public int getNextFocusForwardId() {
4271 return mNextFocusForwardId;
4272 }
4273
4274 /**
4275 * Sets the id of the view to use when the next focus is {@link #FOCUS_FORWARD}.
4276 * @param nextFocusForwardId The next focus ID, or {@link #NO_ID} if the framework should
4277 * decide automatically.
4278 *
4279 * @attr ref android.R.styleable#View_nextFocusForward
4280 */
4281 public void setNextFocusForwardId(int nextFocusForwardId) {
4282 mNextFocusForwardId = nextFocusForwardId;
4283 }
4284
4285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004286 * Returns the visibility of this view and all of its ancestors
4287 *
4288 * @return True if this view and all of its ancestors are {@link #VISIBLE}
4289 */
4290 public boolean isShown() {
4291 View current = this;
4292 //noinspection ConstantConditions
4293 do {
4294 if ((current.mViewFlags & VISIBILITY_MASK) != VISIBLE) {
4295 return false;
4296 }
4297 ViewParent parent = current.mParent;
4298 if (parent == null) {
4299 return false; // We are not attached to the view root
4300 }
4301 if (!(parent instanceof View)) {
4302 return true;
4303 }
4304 current = (View) parent;
4305 } while (current != null);
4306
4307 return false;
4308 }
4309
4310 /**
4311 * Apply the insets for system windows to this view, if the FITS_SYSTEM_WINDOWS flag
4312 * is set
4313 *
4314 * @param insets Insets for system windows
4315 *
4316 * @return True if this view applied the insets, false otherwise
4317 */
4318 protected boolean fitSystemWindows(Rect insets) {
4319 if ((mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS) {
4320 mPaddingLeft = insets.left;
4321 mPaddingTop = insets.top;
4322 mPaddingRight = insets.right;
4323 mPaddingBottom = insets.bottom;
4324 requestLayout();
4325 return true;
4326 }
4327 return false;
4328 }
4329
4330 /**
Adam Powell0bd1d0a2011-07-22 19:35:06 -07004331 * Set whether or not this view should account for system screen decorations
4332 * such as the status bar and inset its content. This allows this view to be
4333 * positioned in absolute screen coordinates and remain visible to the user.
4334 *
4335 * <p>This should only be used by top-level window decor views.
4336 *
4337 * @param fitSystemWindows true to inset content for system screen decorations, false for
4338 * default behavior.
4339 *
4340 * @attr ref android.R.styleable#View_fitsSystemWindows
4341 */
4342 public void setFitsSystemWindows(boolean fitSystemWindows) {
4343 setFlags(fitSystemWindows ? FITS_SYSTEM_WINDOWS : 0, FITS_SYSTEM_WINDOWS);
4344 }
4345
4346 /**
4347 * Check for the FITS_SYSTEM_WINDOWS flag. If this method returns true, this view
4348 * will account for system screen decorations such as the status bar and inset its
4349 * content. This allows the view to be positioned in absolute screen coordinates
4350 * and remain visible to the user.
4351 *
4352 * @return true if this view will adjust its content bounds for system screen decorations.
4353 *
4354 * @attr ref android.R.styleable#View_fitsSystemWindows
4355 */
4356 public boolean fitsSystemWindows() {
4357 return (mViewFlags & FITS_SYSTEM_WINDOWS) == FITS_SYSTEM_WINDOWS;
4358 }
4359
4360 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004361 * Returns the visibility status for this view.
4362 *
4363 * @return One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4364 * @attr ref android.R.styleable#View_visibility
4365 */
4366 @ViewDebug.ExportedProperty(mapping = {
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -07004367 @ViewDebug.IntToString(from = VISIBLE, to = "VISIBLE"),
4368 @ViewDebug.IntToString(from = INVISIBLE, to = "INVISIBLE"),
4369 @ViewDebug.IntToString(from = GONE, to = "GONE")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004370 })
4371 public int getVisibility() {
4372 return mViewFlags & VISIBILITY_MASK;
4373 }
4374
4375 /**
4376 * Set the enabled state of this view.
4377 *
4378 * @param visibility One of {@link #VISIBLE}, {@link #INVISIBLE}, or {@link #GONE}.
4379 * @attr ref android.R.styleable#View_visibility
4380 */
4381 @RemotableViewMethod
4382 public void setVisibility(int visibility) {
4383 setFlags(visibility, VISIBILITY_MASK);
4384 if (mBGDrawable != null) mBGDrawable.setVisible(visibility == VISIBLE, false);
4385 }
4386
4387 /**
4388 * Returns the enabled status for this view. The interpretation of the
4389 * enabled state varies by subclass.
4390 *
4391 * @return True if this view is enabled, false otherwise.
4392 */
4393 @ViewDebug.ExportedProperty
4394 public boolean isEnabled() {
4395 return (mViewFlags & ENABLED_MASK) == ENABLED;
4396 }
4397
4398 /**
4399 * Set the enabled state of this view. The interpretation of the enabled
4400 * state varies by subclass.
4401 *
4402 * @param enabled True if this view is enabled, false otherwise.
4403 */
Jeff Sharkey2b95c242010-02-08 17:40:30 -08004404 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004405 public void setEnabled(boolean enabled) {
Amith Yamasania2ef00b2009-07-30 16:14:34 -07004406 if (enabled == isEnabled()) return;
4407
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004408 setFlags(enabled ? ENABLED : DISABLED, ENABLED_MASK);
4409
4410 /*
4411 * The View most likely has to change its appearance, so refresh
4412 * the drawable state.
4413 */
4414 refreshDrawableState();
4415
4416 // Invalidate too, since the default behavior for views is to be
4417 // be drawn at 50% alpha rather than to change the drawable.
Romain Guy0fd89bf2011-01-26 15:41:30 -08004418 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004419 }
4420
4421 /**
4422 * Set whether this view can receive the focus.
4423 *
4424 * Setting this to false will also ensure that this view is not focusable
4425 * in touch mode.
4426 *
4427 * @param focusable If true, this view can receive the focus.
4428 *
4429 * @see #setFocusableInTouchMode(boolean)
4430 * @attr ref android.R.styleable#View_focusable
4431 */
4432 public void setFocusable(boolean focusable) {
4433 if (!focusable) {
4434 setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
4435 }
4436 setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
4437 }
4438
4439 /**
4440 * Set whether this view can receive focus while in touch mode.
4441 *
4442 * Setting this to true will also ensure that this view is focusable.
4443 *
4444 * @param focusableInTouchMode If true, this view can receive the focus while
4445 * in touch mode.
4446 *
4447 * @see #setFocusable(boolean)
4448 * @attr ref android.R.styleable#View_focusableInTouchMode
4449 */
4450 public void setFocusableInTouchMode(boolean focusableInTouchMode) {
4451 // Focusable in touch mode should always be set before the focusable flag
4452 // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
4453 // which, in touch mode, will not successfully request focus on this view
4454 // because the focusable in touch mode flag is not set
4455 setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
4456 if (focusableInTouchMode) {
4457 setFlags(FOCUSABLE, FOCUSABLE_MASK);
4458 }
4459 }
4460
4461 /**
4462 * Set whether this view should have sound effects enabled for events such as
4463 * clicking and touching.
4464 *
4465 * <p>You may wish to disable sound effects for a view if you already play sounds,
4466 * for instance, a dial key that plays dtmf tones.
4467 *
4468 * @param soundEffectsEnabled whether sound effects are enabled for this view.
4469 * @see #isSoundEffectsEnabled()
4470 * @see #playSoundEffect(int)
4471 * @attr ref android.R.styleable#View_soundEffectsEnabled
4472 */
4473 public void setSoundEffectsEnabled(boolean soundEffectsEnabled) {
4474 setFlags(soundEffectsEnabled ? SOUND_EFFECTS_ENABLED: 0, SOUND_EFFECTS_ENABLED);
4475 }
4476
4477 /**
4478 * @return whether this view should have sound effects enabled for events such as
4479 * clicking and touching.
4480 *
4481 * @see #setSoundEffectsEnabled(boolean)
4482 * @see #playSoundEffect(int)
4483 * @attr ref android.R.styleable#View_soundEffectsEnabled
4484 */
4485 @ViewDebug.ExportedProperty
4486 public boolean isSoundEffectsEnabled() {
4487 return SOUND_EFFECTS_ENABLED == (mViewFlags & SOUND_EFFECTS_ENABLED);
4488 }
4489
4490 /**
4491 * Set whether this view should have haptic feedback for events such as
4492 * long presses.
4493 *
4494 * <p>You may wish to disable haptic feedback if your view already controls
4495 * its own haptic feedback.
4496 *
4497 * @param hapticFeedbackEnabled whether haptic feedback enabled for this view.
4498 * @see #isHapticFeedbackEnabled()
4499 * @see #performHapticFeedback(int)
4500 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4501 */
4502 public void setHapticFeedbackEnabled(boolean hapticFeedbackEnabled) {
4503 setFlags(hapticFeedbackEnabled ? HAPTIC_FEEDBACK_ENABLED: 0, HAPTIC_FEEDBACK_ENABLED);
4504 }
4505
4506 /**
4507 * @return whether this view should have haptic feedback enabled for events
4508 * long presses.
4509 *
4510 * @see #setHapticFeedbackEnabled(boolean)
4511 * @see #performHapticFeedback(int)
4512 * @attr ref android.R.styleable#View_hapticFeedbackEnabled
4513 */
4514 @ViewDebug.ExportedProperty
4515 public boolean isHapticFeedbackEnabled() {
4516 return HAPTIC_FEEDBACK_ENABLED == (mViewFlags & HAPTIC_FEEDBACK_ENABLED);
4517 }
4518
4519 /**
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004520 * Returns the layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004521 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004522 * @return One of {@link #LAYOUT_DIRECTION_LTR},
4523 * {@link #LAYOUT_DIRECTION_RTL},
4524 * {@link #LAYOUT_DIRECTION_INHERIT} or
4525 * {@link #LAYOUT_DIRECTION_LOCALE}.
4526 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004527 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004528 * @hide
4529 */
Fabrice Di Megliobce84d22011-06-02 15:57:01 -07004530 @ViewDebug.ExportedProperty(category = "layout", mapping = {
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004531 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "LTR"),
4532 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RTL"),
4533 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_INHERIT, to = "INHERIT"),
4534 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LOCALE, to = "LOCALE")
Cibu Johny7632cb92010-02-22 13:01:02 -08004535 })
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004536 public int getLayoutDirection() {
4537 return mViewFlags & LAYOUT_DIRECTION_MASK;
Cibu Johny7632cb92010-02-22 13:01:02 -08004538 }
4539
4540 /**
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004541 * Set the layout direction for this view. This will propagate a reset of layout direction
4542 * resolution to the view's children and resolve layout direction for this view.
Cibu Johny7632cb92010-02-22 13:01:02 -08004543 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004544 * @param layoutDirection One of {@link #LAYOUT_DIRECTION_LTR},
4545 * {@link #LAYOUT_DIRECTION_RTL},
4546 * {@link #LAYOUT_DIRECTION_INHERIT} or
4547 * {@link #LAYOUT_DIRECTION_LOCALE}.
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004548 *
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004549 * @attr ref android.R.styleable#View_layoutDirection
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004550 *
Cibu Johny7632cb92010-02-22 13:01:02 -08004551 * @hide
4552 */
4553 @RemotableViewMethod
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07004554 public void setLayoutDirection(int layoutDirection) {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004555 if (getLayoutDirection() != layoutDirection) {
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07004556 resetResolvedLayoutDirection();
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07004557 // Setting the flag will also request a layout.
4558 setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
4559 }
Cibu Johny7632cb92010-02-22 13:01:02 -08004560 }
4561
4562 /**
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004563 * Returns the resolved layout direction for this view.
4564 *
4565 * @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
4566 * {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
4567 *
4568 * @hide
4569 */
4570 @ViewDebug.ExportedProperty(category = "layout", mapping = {
4571 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
4572 @ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
4573 })
4574 public int getResolvedLayoutDirection() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07004575 resolveLayoutDirectionIfNeeded();
4576 return ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED_RTL) == LAYOUT_DIRECTION_RESOLVED_RTL) ?
Fabrice Di Meglioc0053222011-06-13 12:16:51 -07004577 LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
4578 }
4579
4580 /**
4581 * <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
4582 * layout attribute and/or the inherited value from the parent.</p>
4583 *
4584 * @return true if the layout is right-to-left.
4585 *
4586 * @hide
4587 */
4588 @ViewDebug.ExportedProperty(category = "layout")
4589 public boolean isLayoutRtl() {
4590 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
4591 }
4592
4593 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004594 * If this view doesn't do any drawing on its own, set this flag to
4595 * allow further optimizations. By default, this flag is not set on
4596 * View, but could be set on some View subclasses such as ViewGroup.
4597 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004598 * Typically, if you override {@link #onDraw(android.graphics.Canvas)}
4599 * you should clear this flag.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004600 *
4601 * @param willNotDraw whether or not this View draw on its own
4602 */
4603 public void setWillNotDraw(boolean willNotDraw) {
4604 setFlags(willNotDraw ? WILL_NOT_DRAW : 0, DRAW_MASK);
4605 }
4606
4607 /**
4608 * Returns whether or not this View draws on its own.
4609 *
4610 * @return true if this view has nothing to draw, false otherwise
4611 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004612 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004613 public boolean willNotDraw() {
4614 return (mViewFlags & DRAW_MASK) == WILL_NOT_DRAW;
4615 }
4616
4617 /**
4618 * When a View's drawing cache is enabled, drawing is redirected to an
4619 * offscreen bitmap. Some views, like an ImageView, must be able to
4620 * bypass this mechanism if they already draw a single bitmap, to avoid
4621 * unnecessary usage of the memory.
4622 *
4623 * @param willNotCacheDrawing true if this view does not cache its
4624 * drawing, false otherwise
4625 */
4626 public void setWillNotCacheDrawing(boolean willNotCacheDrawing) {
4627 setFlags(willNotCacheDrawing ? WILL_NOT_CACHE_DRAWING : 0, WILL_NOT_CACHE_DRAWING);
4628 }
4629
4630 /**
4631 * Returns whether or not this View can cache its drawing or not.
4632 *
4633 * @return true if this view does not cache its drawing, false otherwise
4634 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004635 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004636 public boolean willNotCacheDrawing() {
4637 return (mViewFlags & WILL_NOT_CACHE_DRAWING) == WILL_NOT_CACHE_DRAWING;
4638 }
4639
4640 /**
4641 * Indicates whether this view reacts to click events or not.
4642 *
4643 * @return true if the view is clickable, false otherwise
4644 *
4645 * @see #setClickable(boolean)
4646 * @attr ref android.R.styleable#View_clickable
4647 */
4648 @ViewDebug.ExportedProperty
4649 public boolean isClickable() {
4650 return (mViewFlags & CLICKABLE) == CLICKABLE;
4651 }
4652
4653 /**
4654 * Enables or disables click events for this view. When a view
4655 * is clickable it will change its state to "pressed" on every click.
4656 * Subclasses should set the view clickable to visually react to
4657 * user's clicks.
4658 *
4659 * @param clickable true to make the view clickable, false otherwise
4660 *
4661 * @see #isClickable()
4662 * @attr ref android.R.styleable#View_clickable
4663 */
4664 public void setClickable(boolean clickable) {
4665 setFlags(clickable ? CLICKABLE : 0, CLICKABLE);
4666 }
4667
4668 /**
4669 * Indicates whether this view reacts to long click events or not.
4670 *
4671 * @return true if the view is long clickable, false otherwise
4672 *
4673 * @see #setLongClickable(boolean)
4674 * @attr ref android.R.styleable#View_longClickable
4675 */
4676 public boolean isLongClickable() {
4677 return (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
4678 }
4679
4680 /**
4681 * Enables or disables long click events for this view. When a view is long
4682 * clickable it reacts to the user holding down the button for a longer
4683 * duration than a tap. This event can either launch the listener or a
4684 * context menu.
4685 *
4686 * @param longClickable true to make the view long clickable, false otherwise
4687 * @see #isLongClickable()
4688 * @attr ref android.R.styleable#View_longClickable
4689 */
4690 public void setLongClickable(boolean longClickable) {
4691 setFlags(longClickable ? LONG_CLICKABLE : 0, LONG_CLICKABLE);
4692 }
4693
4694 /**
Chet Haase49afa5b2010-08-23 11:39:53 -07004695 * Sets the pressed state for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004696 *
4697 * @see #isClickable()
4698 * @see #setClickable(boolean)
4699 *
4700 * @param pressed Pass true to set the View's internal state to "pressed", or false to reverts
4701 * the View's internal state from a previously set "pressed" state.
4702 */
4703 public void setPressed(boolean pressed) {
4704 if (pressed) {
4705 mPrivateFlags |= PRESSED;
4706 } else {
4707 mPrivateFlags &= ~PRESSED;
4708 }
4709 refreshDrawableState();
4710 dispatchSetPressed(pressed);
4711 }
4712
4713 /**
4714 * Dispatch setPressed to all of this View's children.
4715 *
4716 * @see #setPressed(boolean)
4717 *
4718 * @param pressed The new pressed state
4719 */
4720 protected void dispatchSetPressed(boolean pressed) {
4721 }
4722
4723 /**
4724 * Indicates whether the view is currently in pressed state. Unless
4725 * {@link #setPressed(boolean)} is explicitly called, only clickable views can enter
4726 * the pressed state.
4727 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07004728 * @see #setPressed(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004729 * @see #isClickable()
4730 * @see #setClickable(boolean)
4731 *
4732 * @return true if the view is currently pressed, false otherwise
4733 */
4734 public boolean isPressed() {
4735 return (mPrivateFlags & PRESSED) == PRESSED;
4736 }
4737
4738 /**
4739 * Indicates whether this view will save its state (that is,
4740 * whether its {@link #onSaveInstanceState} method will be called).
4741 *
4742 * @return Returns true if the view state saving is enabled, else false.
4743 *
4744 * @see #setSaveEnabled(boolean)
4745 * @attr ref android.R.styleable#View_saveEnabled
4746 */
4747 public boolean isSaveEnabled() {
4748 return (mViewFlags & SAVE_DISABLED_MASK) != SAVE_DISABLED;
4749 }
4750
4751 /**
4752 * Controls whether the saving of this view's state is
4753 * enabled (that is, whether its {@link #onSaveInstanceState} method
4754 * will be called). Note that even if freezing is enabled, the
Romain Guy5c22a8c2011-05-13 11:48:45 -07004755 * view still must have an id assigned to it (via {@link #setId(int)})
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004756 * for its state to be saved. This flag can only disable the
4757 * saving of this view; any child views may still have their state saved.
4758 *
4759 * @param enabled Set to false to <em>disable</em> state saving, or true
4760 * (the default) to allow it.
4761 *
4762 * @see #isSaveEnabled()
4763 * @see #setId(int)
4764 * @see #onSaveInstanceState()
4765 * @attr ref android.R.styleable#View_saveEnabled
4766 */
4767 public void setSaveEnabled(boolean enabled) {
4768 setFlags(enabled ? 0 : SAVE_DISABLED, SAVE_DISABLED_MASK);
4769 }
4770
Jeff Brown85a31762010-09-01 17:01:00 -07004771 /**
4772 * Gets whether the framework should discard touches when the view's
4773 * window is obscured by another visible window.
4774 * Refer to the {@link View} security documentation for more details.
4775 *
4776 * @return True if touch filtering is enabled.
4777 *
4778 * @see #setFilterTouchesWhenObscured(boolean)
4779 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4780 */
4781 @ViewDebug.ExportedProperty
4782 public boolean getFilterTouchesWhenObscured() {
4783 return (mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0;
4784 }
4785
4786 /**
4787 * Sets whether the framework should discard touches when the view's
4788 * window is obscured by another visible window.
4789 * Refer to the {@link View} security documentation for more details.
4790 *
4791 * @param enabled True if touch filtering should be enabled.
4792 *
4793 * @see #getFilterTouchesWhenObscured
4794 * @attr ref android.R.styleable#View_filterTouchesWhenObscured
4795 */
4796 public void setFilterTouchesWhenObscured(boolean enabled) {
4797 setFlags(enabled ? 0 : FILTER_TOUCHES_WHEN_OBSCURED,
4798 FILTER_TOUCHES_WHEN_OBSCURED);
4799 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004800
4801 /**
Dianne Hackbornb4bc78b2010-05-12 18:59:50 -07004802 * Indicates whether the entire hierarchy under this view will save its
4803 * state when a state saving traversal occurs from its parent. The default
4804 * is true; if false, these views will not be saved unless
4805 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4806 *
4807 * @return Returns true if the view state saving from parent is enabled, else false.
4808 *
4809 * @see #setSaveFromParentEnabled(boolean)
4810 */
4811 public boolean isSaveFromParentEnabled() {
4812 return (mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED;
4813 }
4814
4815 /**
4816 * Controls whether the entire hierarchy under this view will save its
4817 * state when a state saving traversal occurs from its parent. The default
4818 * is true; if false, these views will not be saved unless
4819 * {@link #saveHierarchyState(SparseArray)} is called directly on this view.
4820 *
4821 * @param enabled Set to false to <em>disable</em> state saving, or true
4822 * (the default) to allow it.
4823 *
4824 * @see #isSaveFromParentEnabled()
4825 * @see #setId(int)
4826 * @see #onSaveInstanceState()
4827 */
4828 public void setSaveFromParentEnabled(boolean enabled) {
4829 setFlags(enabled ? 0 : PARENT_SAVE_DISABLED, PARENT_SAVE_DISABLED_MASK);
4830 }
4831
4832
4833 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004834 * Returns whether this View is able to take focus.
4835 *
4836 * @return True if this view can take focus, or false otherwise.
4837 * @attr ref android.R.styleable#View_focusable
4838 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07004839 @ViewDebug.ExportedProperty(category = "focus")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004840 public final boolean isFocusable() {
4841 return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
4842 }
4843
4844 /**
4845 * When a view is focusable, it may not want to take focus when in touch mode.
4846 * For example, a button would like focus when the user is navigating via a D-pad
4847 * so that the user can click on it, but once the user starts touching the screen,
4848 * the button shouldn't take focus
4849 * @return Whether the view is focusable in touch mode.
4850 * @attr ref android.R.styleable#View_focusableInTouchMode
4851 */
4852 @ViewDebug.ExportedProperty
4853 public final boolean isFocusableInTouchMode() {
4854 return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
4855 }
4856
4857 /**
4858 * Find the nearest view in the specified direction that can take focus.
4859 * This does not actually give focus to that view.
4860 *
4861 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
4862 *
4863 * @return The nearest focusable in the specified direction, or null if none
4864 * can be found.
4865 */
4866 public View focusSearch(int direction) {
4867 if (mParent != null) {
4868 return mParent.focusSearch(this, direction);
4869 } else {
4870 return null;
4871 }
4872 }
4873
4874 /**
4875 * This method is the last chance for the focused view and its ancestors to
4876 * respond to an arrow key. This is called when the focused view did not
4877 * consume the key internally, nor could the view system find a new view in
4878 * the requested direction to give focus to.
4879 *
4880 * @param focused The currently focused view.
4881 * @param direction The direction focus wants to move. One of FOCUS_UP,
4882 * FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT.
4883 * @return True if the this view consumed this unhandled move.
4884 */
4885 public boolean dispatchUnhandledMove(View focused, int direction) {
4886 return false;
4887 }
4888
4889 /**
4890 * If a user manually specified the next view id for a particular direction,
Jeff Brown4e6319b2010-12-13 10:36:51 -08004891 * use the root to look up the view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004892 * @param root The root view of the hierarchy containing this view.
Jeff Brown4e6319b2010-12-13 10:36:51 -08004893 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, FOCUS_RIGHT, FOCUS_FORWARD,
4894 * or FOCUS_BACKWARD.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004895 * @return The user specified next view, or null if there is none.
4896 */
4897 View findUserSetNextFocus(View root, int direction) {
4898 switch (direction) {
4899 case FOCUS_LEFT:
4900 if (mNextFocusLeftId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004901 return findViewInsideOutShouldExist(root, mNextFocusLeftId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004902 case FOCUS_RIGHT:
4903 if (mNextFocusRightId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004904 return findViewInsideOutShouldExist(root, mNextFocusRightId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004905 case FOCUS_UP:
4906 if (mNextFocusUpId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004907 return findViewInsideOutShouldExist(root, mNextFocusUpId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004908 case FOCUS_DOWN:
4909 if (mNextFocusDownId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004910 return findViewInsideOutShouldExist(root, mNextFocusDownId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004911 case FOCUS_FORWARD:
4912 if (mNextFocusForwardId == View.NO_ID) return null;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004913 return findViewInsideOutShouldExist(root, mNextFocusForwardId);
Jeff Brown4e6319b2010-12-13 10:36:51 -08004914 case FOCUS_BACKWARD: {
4915 final int id = mID;
Jeff Brown4dfbec22011-08-15 14:55:37 -07004916 return root.findViewByPredicateInsideOut(this, new Predicate<View>() {
Jeff Brown4e6319b2010-12-13 10:36:51 -08004917 @Override
4918 public boolean apply(View t) {
4919 return t.mNextFocusForwardId == id;
4920 }
4921 });
4922 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004923 }
4924 return null;
4925 }
4926
Jeff Brown4dfbec22011-08-15 14:55:37 -07004927 private View findViewInsideOutShouldExist(View root, final int childViewId) {
4928 View result = root.findViewByPredicateInsideOut(this, new Predicate<View>() {
4929 @Override
4930 public boolean apply(View t) {
4931 return t.mID == childViewId;
4932 }
4933 });
4934
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004935 if (result == null) {
4936 Log.w(VIEW_LOG_TAG, "couldn't find next focus view specified "
4937 + "by user for id " + childViewId);
4938 }
4939 return result;
4940 }
4941
4942 /**
4943 * Find and return all focusable views that are descendants of this view,
4944 * possibly including this view if it is focusable itself.
4945 *
4946 * @param direction The direction of the focus
4947 * @return A list of focusable views
4948 */
4949 public ArrayList<View> getFocusables(int direction) {
4950 ArrayList<View> result = new ArrayList<View>(24);
4951 addFocusables(result, direction);
4952 return result;
4953 }
4954
4955 /**
4956 * Add any focusable views that are descendants of this view (possibly
4957 * including this view if it is focusable itself) to views. If we are in touch mode,
4958 * only add views that are also focusable in touch mode.
4959 *
4960 * @param views Focusable views found so far
4961 * @param direction The direction of the focus
4962 */
4963 public void addFocusables(ArrayList<View> views, int direction) {
svetoslavganov75986cf2009-05-14 22:28:01 -07004964 addFocusables(views, direction, FOCUSABLES_TOUCH_MODE);
4965 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004966
svetoslavganov75986cf2009-05-14 22:28:01 -07004967 /**
4968 * Adds any focusable views that are descendants of this view (possibly
4969 * including this view if it is focusable itself) to views. This method
4970 * adds all focusable views regardless if we are in touch mode or
4971 * only views focusable in touch mode if we are in touch mode depending on
4972 * the focusable mode paramater.
4973 *
4974 * @param views Focusable views found so far or null if all we are interested is
4975 * the number of focusables.
4976 * @param direction The direction of the focus.
4977 * @param focusableMode The type of focusables to be added.
4978 *
4979 * @see #FOCUSABLES_ALL
4980 * @see #FOCUSABLES_TOUCH_MODE
4981 */
4982 public void addFocusables(ArrayList<View> views, int direction, int focusableMode) {
4983 if (!isFocusable()) {
4984 return;
4985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004986
svetoslavganov75986cf2009-05-14 22:28:01 -07004987 if ((focusableMode & FOCUSABLES_TOUCH_MODE) == FOCUSABLES_TOUCH_MODE &&
4988 isInTouchMode() && !isFocusableInTouchMode()) {
4989 return;
4990 }
4991
4992 if (views != null) {
4993 views.add(this);
4994 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004995 }
4996
4997 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07004998 * Finds the Views that contain given text. The containment is case insensitive.
4999 * As View's text is considered any text content that View renders.
5000 *
5001 * @param outViews The output list of matching Views.
5002 * @param text The text to match against.
5003 */
5004 public void findViewsWithText(ArrayList<View> outViews, CharSequence text) {
5005 }
5006
5007 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005008 * Find and return all touchable views that are descendants of this view,
5009 * possibly including this view if it is touchable itself.
5010 *
5011 * @return A list of touchable views
5012 */
5013 public ArrayList<View> getTouchables() {
5014 ArrayList<View> result = new ArrayList<View>();
5015 addTouchables(result);
5016 return result;
5017 }
5018
5019 /**
5020 * Add any touchable views that are descendants of this view (possibly
5021 * including this view if it is touchable itself) to views.
5022 *
5023 * @param views Touchable views found so far
5024 */
5025 public void addTouchables(ArrayList<View> views) {
5026 final int viewFlags = mViewFlags;
5027
5028 if (((viewFlags & CLICKABLE) == CLICKABLE || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)
5029 && (viewFlags & ENABLED_MASK) == ENABLED) {
5030 views.add(this);
5031 }
5032 }
5033
5034 /**
5035 * Call this to try to give focus to a specific view or to one of its
5036 * descendants.
5037 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005038 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5039 * false), or if it is focusable and it is not focusable in touch mode
5040 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005041 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005042 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005043 * have focus, and you want your parent to look for the next one.
5044 *
5045 * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
5046 * {@link #FOCUS_DOWN} and <code>null</code>.
5047 *
5048 * @return Whether this view or one of its descendants actually took focus.
5049 */
5050 public final boolean requestFocus() {
5051 return requestFocus(View.FOCUS_DOWN);
5052 }
5053
5054
5055 /**
5056 * Call this to try to give focus to a specific view or to one of its
5057 * descendants and give it a hint about what direction focus is heading.
5058 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005059 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5060 * false), or if it is focusable and it is not focusable in touch mode
5061 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005062 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005063 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005064 * have focus, and you want your parent to look for the next one.
5065 *
5066 * This is equivalent to calling {@link #requestFocus(int, Rect)} with
5067 * <code>null</code> set for the previously focused rectangle.
5068 *
5069 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5070 * @return Whether this view or one of its descendants actually took focus.
5071 */
5072 public final boolean requestFocus(int direction) {
5073 return requestFocus(direction, null);
5074 }
5075
5076 /**
5077 * Call this to try to give focus to a specific view or to one of its descendants
5078 * and give it hints about the direction and a specific rectangle that the focus
5079 * is coming from. The rectangle can help give larger views a finer grained hint
5080 * about where focus is coming from, and therefore, where to show selection, or
5081 * forward focus change internally.
5082 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005083 * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
5084 * false), or if it is focusable and it is not focusable in touch mode
5085 * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005086 *
5087 * A View will not take focus if it is not visible.
5088 *
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08005089 * A View will not take focus if one of its parents has
5090 * {@link android.view.ViewGroup#getDescendantFocusability()} equal to
5091 * {@link ViewGroup#FOCUS_BLOCK_DESCENDANTS}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005092 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005093 * See also {@link #focusSearch(int)}, which is what you call to say that you
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005094 * have focus, and you want your parent to look for the next one.
5095 *
5096 * You may wish to override this method if your custom {@link View} has an internal
5097 * {@link View} that it wishes to forward the request to.
5098 *
5099 * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
5100 * @param previouslyFocusedRect The rectangle (in this View's coordinate system)
5101 * to give a finer grained hint about where focus is coming from. May be null
5102 * if there is no hint.
5103 * @return Whether this view or one of its descendants actually took focus.
5104 */
5105 public boolean requestFocus(int direction, Rect previouslyFocusedRect) {
5106 // need to be focusable
5107 if ((mViewFlags & FOCUSABLE_MASK) != FOCUSABLE ||
5108 (mViewFlags & VISIBILITY_MASK) != VISIBLE) {
5109 return false;
5110 }
5111
5112 // need to be focusable in touch mode if in touch mode
5113 if (isInTouchMode() &&
Svetoslav Ganov8643aa02011-04-20 12:12:33 -07005114 (FOCUSABLE_IN_TOUCH_MODE != (mViewFlags & FOCUSABLE_IN_TOUCH_MODE))) {
5115 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005116 }
5117
5118 // need to not have any parents blocking us
5119 if (hasAncestorThatBlocksDescendantFocus()) {
5120 return false;
5121 }
5122
5123 handleFocusGainInternal(direction, previouslyFocusedRect);
5124 return true;
5125 }
5126
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07005127 /** Gets the ViewAncestor, or null if not attached. */
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005128 /*package*/ ViewRootImpl getViewRootImpl() {
Christopher Tate2c095f32010-10-04 14:13:40 -07005129 View root = getRootView();
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005130 return root != null ? (ViewRootImpl)root.getParent() : null;
Christopher Tate2c095f32010-10-04 14:13:40 -07005131 }
5132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005133 /**
5134 * Call this to try to give focus to a specific view or to one of its descendants. This is a
5135 * special variant of {@link #requestFocus() } that will allow views that are not focuable in
5136 * touch mode to request focus when they are touched.
5137 *
5138 * @return Whether this view or one of its descendants actually took focus.
5139 *
5140 * @see #isInTouchMode()
5141 *
5142 */
5143 public final boolean requestFocusFromTouch() {
5144 // Leave touch mode if we need to
5145 if (isInTouchMode()) {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005146 ViewRootImpl viewRoot = getViewRootImpl();
Christopher Tate2c095f32010-10-04 14:13:40 -07005147 if (viewRoot != null) {
5148 viewRoot.ensureTouchMode(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005149 }
5150 }
5151 return requestFocus(View.FOCUS_DOWN);
5152 }
5153
5154 /**
5155 * @return Whether any ancestor of this view blocks descendant focus.
5156 */
5157 private boolean hasAncestorThatBlocksDescendantFocus() {
5158 ViewParent ancestor = mParent;
5159 while (ancestor instanceof ViewGroup) {
5160 final ViewGroup vgAncestor = (ViewGroup) ancestor;
5161 if (vgAncestor.getDescendantFocusability() == ViewGroup.FOCUS_BLOCK_DESCENDANTS) {
5162 return true;
5163 } else {
5164 ancestor = vgAncestor.getParent();
5165 }
5166 }
5167 return false;
5168 }
5169
5170 /**
Romain Guya440b002010-02-24 15:57:54 -08005171 * @hide
5172 */
5173 public void dispatchStartTemporaryDetach() {
5174 onStartTemporaryDetach();
5175 }
5176
5177 /**
5178 * This is called when a container is going to temporarily detach a child, with
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005179 * {@link ViewGroup#detachViewFromParent(View) ViewGroup.detachViewFromParent}.
5180 * It will either be followed by {@link #onFinishTemporaryDetach()} or
Romain Guya440b002010-02-24 15:57:54 -08005181 * {@link #onDetachedFromWindow()} when the container is done.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005182 */
5183 public void onStartTemporaryDetach() {
Romain Guya440b002010-02-24 15:57:54 -08005184 removeUnsetPressCallback();
Romain Guy8afa5152010-02-26 11:56:30 -08005185 mPrivateFlags |= CANCEL_NEXT_UP_EVENT;
Romain Guya440b002010-02-24 15:57:54 -08005186 }
5187
5188 /**
5189 * @hide
5190 */
5191 public void dispatchFinishTemporaryDetach() {
5192 onFinishTemporaryDetach();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005193 }
Romain Guy8506ab42009-06-11 17:35:47 -07005194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005195 /**
5196 * Called after {@link #onStartTemporaryDetach} when the container is done
5197 * changing the view.
5198 */
5199 public void onFinishTemporaryDetach() {
5200 }
Romain Guy8506ab42009-06-11 17:35:47 -07005201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005202 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005203 * Return the global {@link KeyEvent.DispatcherState KeyEvent.DispatcherState}
5204 * for this view's window. Returns null if the view is not currently attached
5205 * to the window. Normally you will not need to use this directly, but
Romain Guy5c22a8c2011-05-13 11:48:45 -07005206 * just use the standard high-level event callbacks like
5207 * {@link #onKeyDown(int, KeyEvent)}.
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005208 */
5209 public KeyEvent.DispatcherState getKeyDispatcherState() {
5210 return mAttachInfo != null ? mAttachInfo.mKeyDispatchState : null;
5211 }
Joe Malin32736f02011-01-19 16:14:20 -08005212
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005213 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005214 * Dispatch a key event before it is processed by any input method
5215 * associated with the view hierarchy. This can be used to intercept
5216 * key events in special situations before the IME consumes them; a
5217 * typical example would be handling the BACK key to update the application's
5218 * UI instead of allowing the IME to see it and close itself.
5219 *
5220 * @param event The key event to be dispatched.
5221 * @return True if the event was handled, false otherwise.
5222 */
5223 public boolean dispatchKeyEventPreIme(KeyEvent event) {
5224 return onKeyPreIme(event.getKeyCode(), event);
5225 }
5226
5227 /**
5228 * Dispatch a key event to the next view on the focus path. This path runs
5229 * from the top of the view tree down to the currently focused view. If this
5230 * view has focus, it will dispatch to itself. Otherwise it will dispatch
5231 * the next node down the focus path. This method also fires any key
5232 * listeners.
5233 *
5234 * @param event The key event to be dispatched.
5235 * @return True if the event was handled, false otherwise.
5236 */
5237 public boolean dispatchKeyEvent(KeyEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005238 if (mInputEventConsistencyVerifier != null) {
5239 mInputEventConsistencyVerifier.onKeyEvent(event, 0);
5240 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005241
Jeff Brown21bc5c92011-02-28 18:27:14 -08005242 // Give any attached key listener a first crack at the event.
Romain Guyf607bdc2010-09-10 19:20:06 -07005243 //noinspection SimplifiableIfStatement
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005244 if (mOnKeyListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5245 && mOnKeyListener.onKey(this, event.getKeyCode(), event)) {
5246 return true;
5247 }
5248
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005249 if (event.dispatch(this, mAttachInfo != null
5250 ? mAttachInfo.mKeyDispatchState : null, this)) {
5251 return true;
5252 }
5253
5254 if (mInputEventConsistencyVerifier != null) {
5255 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5256 }
5257 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005258 }
5259
5260 /**
5261 * Dispatches a key shortcut event.
5262 *
5263 * @param event The key event to be dispatched.
5264 * @return True if the event was handled by the view, false otherwise.
5265 */
5266 public boolean dispatchKeyShortcutEvent(KeyEvent event) {
5267 return onKeyShortcut(event.getKeyCode(), event);
5268 }
5269
5270 /**
5271 * Pass the touch screen motion event down to the target view, or this
5272 * view if it is the target.
5273 *
5274 * @param event The motion event to be dispatched.
5275 * @return True if the event was handled by the view, false otherwise.
5276 */
5277 public boolean dispatchTouchEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005278 if (mInputEventConsistencyVerifier != null) {
5279 mInputEventConsistencyVerifier.onTouchEvent(event, 0);
5280 }
5281
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005282 if (onFilterTouchEventForSecurity(event)) {
5283 //noinspection SimplifiableIfStatement
5284 if (mOnTouchListener != null && (mViewFlags & ENABLED_MASK) == ENABLED &&
5285 mOnTouchListener.onTouch(this, event)) {
5286 return true;
5287 }
5288
5289 if (onTouchEvent(event)) {
5290 return true;
5291 }
Jeff Brown85a31762010-09-01 17:01:00 -07005292 }
5293
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005294 if (mInputEventConsistencyVerifier != null) {
5295 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005296 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005297 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005298 }
5299
5300 /**
Jeff Brown85a31762010-09-01 17:01:00 -07005301 * Filter the touch event to apply security policies.
5302 *
5303 * @param event The motion event to be filtered.
5304 * @return True if the event should be dispatched, false if the event should be dropped.
Joe Malin32736f02011-01-19 16:14:20 -08005305 *
Jeff Brown85a31762010-09-01 17:01:00 -07005306 * @see #getFilterTouchesWhenObscured
5307 */
5308 public boolean onFilterTouchEventForSecurity(MotionEvent event) {
Romain Guyf607bdc2010-09-10 19:20:06 -07005309 //noinspection RedundantIfStatement
Jeff Brown85a31762010-09-01 17:01:00 -07005310 if ((mViewFlags & FILTER_TOUCHES_WHEN_OBSCURED) != 0
5311 && (event.getFlags() & MotionEvent.FLAG_WINDOW_IS_OBSCURED) != 0) {
5312 // Window is obscured, drop this touch.
5313 return false;
5314 }
5315 return true;
5316 }
5317
5318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005319 * Pass a trackball motion event down to the focused view.
5320 *
5321 * @param event The motion event to be dispatched.
5322 * @return True if the event was handled by the view, false otherwise.
5323 */
5324 public boolean dispatchTrackballEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005325 if (mInputEventConsistencyVerifier != null) {
5326 mInputEventConsistencyVerifier.onTrackballEvent(event, 0);
5327 }
5328
Romain Guy02ccac62011-06-24 13:20:23 -07005329 return onTrackballEvent(event);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005330 }
5331
5332 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005333 * Dispatch a generic motion event.
5334 * <p>
5335 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5336 * are delivered to the view under the pointer. All other generic motion events are
Jeff Browna032cc02011-03-07 16:56:21 -08005337 * delivered to the focused view. Hover events are handled specially and are delivered
Romain Guy5c22a8c2011-05-13 11:48:45 -07005338 * to {@link #onHoverEvent(MotionEvent)}.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005339 * </p>
Jeff Browncb1404e2011-01-15 18:14:15 -08005340 *
5341 * @param event The motion event to be dispatched.
5342 * @return True if the event was handled by the view, false otherwise.
5343 */
5344 public boolean dispatchGenericMotionEvent(MotionEvent event) {
Jeff Brown21bc5c92011-02-28 18:27:14 -08005345 if (mInputEventConsistencyVerifier != null) {
5346 mInputEventConsistencyVerifier.onGenericMotionEvent(event, 0);
5347 }
5348
Jeff Browna032cc02011-03-07 16:56:21 -08005349 final int source = event.getSource();
5350 if ((source & InputDevice.SOURCE_CLASS_POINTER) != 0) {
5351 final int action = event.getAction();
5352 if (action == MotionEvent.ACTION_HOVER_ENTER
5353 || action == MotionEvent.ACTION_HOVER_MOVE
5354 || action == MotionEvent.ACTION_HOVER_EXIT) {
5355 if (dispatchHoverEvent(event)) {
5356 return true;
5357 }
5358 } else if (dispatchGenericPointerEvent(event)) {
5359 return true;
5360 }
5361 } else if (dispatchGenericFocusedEvent(event)) {
5362 return true;
5363 }
5364
Jeff Brown10b62902011-06-20 16:40:37 -07005365 if (dispatchGenericMotionEventInternal(event)) {
5366 return true;
5367 }
5368
5369 if (mInputEventConsistencyVerifier != null) {
5370 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5371 }
5372 return false;
5373 }
5374
5375 private boolean dispatchGenericMotionEventInternal(MotionEvent event) {
Romain Guy7b5b6ab2011-03-14 18:05:08 -07005376 //noinspection SimplifiableIfStatement
Jeff Brown33bbfd22011-02-24 20:55:35 -08005377 if (mOnGenericMotionListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5378 && mOnGenericMotionListener.onGenericMotion(this, event)) {
5379 return true;
5380 }
Jeff Brownbbdc50b2011-04-19 23:46:52 -07005381
5382 if (onGenericMotionEvent(event)) {
5383 return true;
5384 }
5385
5386 if (mInputEventConsistencyVerifier != null) {
5387 mInputEventConsistencyVerifier.onUnhandledEvent(event, 0);
5388 }
5389 return false;
Jeff Browncb1404e2011-01-15 18:14:15 -08005390 }
5391
5392 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005393 * Dispatch a hover event.
5394 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005395 * Do not call this method directly.
5396 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005397 * </p>
5398 *
5399 * @param event The motion event to be dispatched.
5400 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005401 */
5402 protected boolean dispatchHoverEvent(MotionEvent event) {
Romain Guy02ccac62011-06-24 13:20:23 -07005403 //noinspection SimplifiableIfStatement
Jeff Brown10b62902011-06-20 16:40:37 -07005404 if (mOnHoverListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
5405 && mOnHoverListener.onHover(this, event)) {
5406 return true;
5407 }
5408
Jeff Browna032cc02011-03-07 16:56:21 -08005409 return onHoverEvent(event);
5410 }
5411
5412 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07005413 * Returns true if the view has a child to which it has recently sent
5414 * {@link MotionEvent#ACTION_HOVER_ENTER}. If this view is hovered and
5415 * it does not have a hovered child, then it must be the innermost hovered view.
5416 * @hide
5417 */
5418 protected boolean hasHoveredChild() {
5419 return false;
5420 }
5421
5422 /**
Jeff Browna032cc02011-03-07 16:56:21 -08005423 * Dispatch a generic motion event to the view under the first pointer.
5424 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005425 * Do not call this method directly.
5426 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005427 * </p>
5428 *
5429 * @param event The motion event to be dispatched.
5430 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005431 */
5432 protected boolean dispatchGenericPointerEvent(MotionEvent event) {
5433 return false;
5434 }
5435
5436 /**
5437 * Dispatch a generic motion event to the currently focused view.
5438 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005439 * Do not call this method directly.
5440 * Call {@link #dispatchGenericMotionEvent(MotionEvent)} instead.
Jeff Browna032cc02011-03-07 16:56:21 -08005441 * </p>
5442 *
5443 * @param event The motion event to be dispatched.
5444 * @return True if the event was handled by the view, false otherwise.
Jeff Browna032cc02011-03-07 16:56:21 -08005445 */
5446 protected boolean dispatchGenericFocusedEvent(MotionEvent event) {
5447 return false;
5448 }
5449
5450 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -08005451 * Dispatch a pointer event.
5452 * <p>
Romain Guy5c22a8c2011-05-13 11:48:45 -07005453 * Dispatches touch related pointer events to {@link #onTouchEvent(MotionEvent)} and all
5454 * other events to {@link #onGenericMotionEvent(MotionEvent)}. This separation of concerns
5455 * reinforces the invariant that {@link #onTouchEvent(MotionEvent)} is really about touches
Jeff Brown33bbfd22011-02-24 20:55:35 -08005456 * and should not be expected to handle other pointing device features.
5457 * </p>
5458 *
5459 * @param event The motion event to be dispatched.
5460 * @return True if the event was handled by the view, false otherwise.
5461 * @hide
5462 */
5463 public final boolean dispatchPointerEvent(MotionEvent event) {
5464 if (event.isTouchEvent()) {
5465 return dispatchTouchEvent(event);
5466 } else {
5467 return dispatchGenericMotionEvent(event);
5468 }
5469 }
5470
5471 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005472 * Called when the window containing this view gains or loses window focus.
5473 * ViewGroups should override to route to their children.
5474 *
5475 * @param hasFocus True if the window containing this view now has focus,
5476 * false otherwise.
5477 */
5478 public void dispatchWindowFocusChanged(boolean hasFocus) {
5479 onWindowFocusChanged(hasFocus);
5480 }
5481
5482 /**
5483 * Called when the window containing this view gains or loses focus. Note
5484 * that this is separate from view focus: to receive key events, both
5485 * your view and its window must have focus. If a window is displayed
5486 * on top of yours that takes input focus, then your own window will lose
5487 * focus but the view focus will remain unchanged.
5488 *
5489 * @param hasWindowFocus True if the window containing this view now has
5490 * focus, false otherwise.
5491 */
5492 public void onWindowFocusChanged(boolean hasWindowFocus) {
5493 InputMethodManager imm = InputMethodManager.peekInstance();
5494 if (!hasWindowFocus) {
5495 if (isPressed()) {
5496 setPressed(false);
5497 }
5498 if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5499 imm.focusOut(this);
5500 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05005501 removeLongPressCallback();
Tony Wu26edf202010-09-13 19:54:00 +08005502 removeTapCallback();
Romain Guya2431d02009-04-30 16:30:00 -07005503 onFocusLost();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005504 } else if (imm != null && (mPrivateFlags & FOCUSED) != 0) {
5505 imm.focusIn(this);
5506 }
5507 refreshDrawableState();
5508 }
5509
5510 /**
5511 * Returns true if this view is in a window that currently has window focus.
5512 * Note that this is not the same as the view itself having focus.
5513 *
5514 * @return True if this view is in a window that currently has window focus.
5515 */
5516 public boolean hasWindowFocus() {
5517 return mAttachInfo != null && mAttachInfo.mHasWindowFocus;
5518 }
5519
5520 /**
Adam Powell326d8082009-12-09 15:10:07 -08005521 * Dispatch a view visibility change down the view hierarchy.
5522 * ViewGroups should override to route to their children.
5523 * @param changedView The view whose visibility changed. Could be 'this' or
5524 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005525 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5526 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005527 */
5528 protected void dispatchVisibilityChanged(View changedView, int visibility) {
5529 onVisibilityChanged(changedView, visibility);
5530 }
5531
5532 /**
5533 * Called when the visibility of the view or an ancestor of the view is changed.
5534 * @param changedView The view whose visibility changed. Could be 'this' or
5535 * an ancestor view.
Romain Guy43c9cdf2010-01-27 13:53:55 -08005536 * @param visibility The new visibility of changedView: {@link #VISIBLE},
5537 * {@link #INVISIBLE} or {@link #GONE}.
Adam Powell326d8082009-12-09 15:10:07 -08005538 */
5539 protected void onVisibilityChanged(View changedView, int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005540 if (visibility == VISIBLE) {
5541 if (mAttachInfo != null) {
5542 initialAwakenScrollBars();
5543 } else {
5544 mPrivateFlags |= AWAKEN_SCROLL_BARS_ON_ATTACH;
5545 }
5546 }
Adam Powell326d8082009-12-09 15:10:07 -08005547 }
5548
5549 /**
Romain Guy43c9cdf2010-01-27 13:53:55 -08005550 * Dispatch a hint about whether this view is displayed. For instance, when
5551 * a View moves out of the screen, it might receives a display hint indicating
5552 * the view is not displayed. Applications should not <em>rely</em> on this hint
5553 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005554 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005555 * @param hint A hint about whether or not this view is displayed:
5556 * {@link #VISIBLE} or {@link #INVISIBLE}.
5557 */
5558 public void dispatchDisplayHint(int hint) {
5559 onDisplayHint(hint);
5560 }
5561
5562 /**
5563 * Gives this view a hint about whether is displayed or not. For instance, when
5564 * a View moves out of the screen, it might receives a display hint indicating
5565 * the view is not displayed. Applications should not <em>rely</em> on this hint
5566 * as there is no guarantee that they will receive one.
Joe Malin32736f02011-01-19 16:14:20 -08005567 *
Romain Guy43c9cdf2010-01-27 13:53:55 -08005568 * @param hint A hint about whether or not this view is displayed:
5569 * {@link #VISIBLE} or {@link #INVISIBLE}.
5570 */
5571 protected void onDisplayHint(int hint) {
5572 }
5573
5574 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005575 * Dispatch a window visibility change down the view hierarchy.
5576 * ViewGroups should override to route to their children.
5577 *
5578 * @param visibility The new visibility of the window.
5579 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005580 * @see #onWindowVisibilityChanged(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005581 */
5582 public void dispatchWindowVisibilityChanged(int visibility) {
5583 onWindowVisibilityChanged(visibility);
5584 }
5585
5586 /**
5587 * Called when the window containing has change its visibility
5588 * (between {@link #GONE}, {@link #INVISIBLE}, and {@link #VISIBLE}). Note
5589 * that this tells you whether or not your window is being made visible
5590 * to the window manager; this does <em>not</em> tell you whether or not
5591 * your window is obscured by other windows on the screen, even if it
5592 * is itself visible.
5593 *
5594 * @param visibility The new visibility of the window.
5595 */
5596 protected void onWindowVisibilityChanged(int visibility) {
Adam Powell8568c3a2010-04-19 14:26:11 -07005597 if (visibility == VISIBLE) {
5598 initialAwakenScrollBars();
5599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005600 }
5601
5602 /**
5603 * Returns the current visibility of the window this view is attached to
5604 * (either {@link #GONE}, {@link #INVISIBLE}, or {@link #VISIBLE}).
5605 *
5606 * @return Returns the current visibility of the view's window.
5607 */
5608 public int getWindowVisibility() {
5609 return mAttachInfo != null ? mAttachInfo.mWindowVisibility : GONE;
5610 }
5611
5612 /**
5613 * Retrieve the overall visible display size in which the window this view is
5614 * attached to has been positioned in. This takes into account screen
5615 * decorations above the window, for both cases where the window itself
5616 * is being position inside of them or the window is being placed under
5617 * then and covered insets are used for the window to position its content
5618 * inside. In effect, this tells you the available area where content can
5619 * be placed and remain visible to users.
5620 *
5621 * <p>This function requires an IPC back to the window manager to retrieve
5622 * the requested information, so should not be used in performance critical
5623 * code like drawing.
5624 *
5625 * @param outRect Filled in with the visible display frame. If the view
5626 * is not attached to a window, this is simply the raw display size.
5627 */
5628 public void getWindowVisibleDisplayFrame(Rect outRect) {
5629 if (mAttachInfo != null) {
5630 try {
5631 mAttachInfo.mSession.getDisplayFrame(mAttachInfo.mWindow, outRect);
5632 } catch (RemoteException e) {
5633 return;
5634 }
5635 // XXX This is really broken, and probably all needs to be done
5636 // in the window manager, and we need to know more about whether
5637 // we want the area behind or in front of the IME.
5638 final Rect insets = mAttachInfo.mVisibleInsets;
5639 outRect.left += insets.left;
5640 outRect.top += insets.top;
5641 outRect.right -= insets.right;
5642 outRect.bottom -= insets.bottom;
5643 return;
5644 }
5645 Display d = WindowManagerImpl.getDefault().getDefaultDisplay();
Dianne Hackborn44bc17c2011-04-20 18:18:51 -07005646 d.getRectSize(outRect);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005647 }
5648
5649 /**
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005650 * Dispatch a notification about a resource configuration change down
5651 * the view hierarchy.
5652 * ViewGroups should override to route to their children.
5653 *
5654 * @param newConfig The new resource configuration.
5655 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07005656 * @see #onConfigurationChanged(android.content.res.Configuration)
Dianne Hackborne36d6e22010-02-17 19:46:25 -08005657 */
5658 public void dispatchConfigurationChanged(Configuration newConfig) {
5659 onConfigurationChanged(newConfig);
5660 }
5661
5662 /**
5663 * Called when the current configuration of the resources being used
5664 * by the application have changed. You can use this to decide when
5665 * to reload resources that can changed based on orientation and other
5666 * configuration characterstics. You only need to use this if you are
5667 * not relying on the normal {@link android.app.Activity} mechanism of
5668 * recreating the activity instance upon a configuration change.
5669 *
5670 * @param newConfig The new resource configuration.
5671 */
5672 protected void onConfigurationChanged(Configuration newConfig) {
5673 }
5674
5675 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005676 * Private function to aggregate all per-view attributes in to the view
5677 * root.
5678 */
5679 void dispatchCollectViewAttributes(int visibility) {
5680 performCollectViewAttributes(visibility);
5681 }
5682
5683 void performCollectViewAttributes(int visibility) {
Romain Guyd30b36d2011-01-26 10:54:43 -08005684 if ((visibility & VISIBILITY_MASK) == VISIBLE && mAttachInfo != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005685 if ((mViewFlags & KEEP_SCREEN_ON) == KEEP_SCREEN_ON) {
5686 mAttachInfo.mKeepScreenOn = true;
5687 }
5688 mAttachInfo.mSystemUiVisibility |= mSystemUiVisibility;
5689 if (mOnSystemUiVisibilityChangeListener != null) {
5690 mAttachInfo.mHasSystemUiListeners = true;
5691 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005692 }
5693 }
5694
5695 void needGlobalAttributesUpdate(boolean force) {
Joe Onorato664644d2011-01-23 17:53:23 -08005696 final AttachInfo ai = mAttachInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005697 if (ai != null) {
Joe Onorato664644d2011-01-23 17:53:23 -08005698 if (force || ai.mKeepScreenOn || (ai.mSystemUiVisibility != 0)
5699 || ai.mHasSystemUiListeners) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005700 ai.mRecomputeGlobalAttributes = true;
5701 }
5702 }
5703 }
5704
5705 /**
5706 * Returns whether the device is currently in touch mode. Touch mode is entered
5707 * once the user begins interacting with the device by touch, and affects various
5708 * things like whether focus is always visible to the user.
5709 *
5710 * @return Whether the device is in touch mode.
5711 */
5712 @ViewDebug.ExportedProperty
5713 public boolean isInTouchMode() {
5714 if (mAttachInfo != null) {
5715 return mAttachInfo.mInTouchMode;
5716 } else {
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07005717 return ViewRootImpl.isInTouchMode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005718 }
5719 }
5720
5721 /**
5722 * Returns the context the view is running in, through which it can
5723 * access the current theme, resources, etc.
5724 *
5725 * @return The view's Context.
5726 */
5727 @ViewDebug.CapturedViewProperty
5728 public final Context getContext() {
5729 return mContext;
5730 }
5731
5732 /**
5733 * Handle a key event before it is processed by any input method
5734 * associated with the view hierarchy. This can be used to intercept
5735 * key events in special situations before the IME consumes them; a
5736 * typical example would be handling the BACK key to update the application's
5737 * UI instead of allowing the IME to see it and close itself.
5738 *
5739 * @param keyCode The value in event.getKeyCode().
5740 * @param event Description of the key event.
5741 * @return If you handled the event, return true. If you want to allow the
5742 * event to be handled by the next receiver, return false.
5743 */
5744 public boolean onKeyPreIme(int keyCode, KeyEvent event) {
5745 return false;
5746 }
5747
5748 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005749 * Default implementation of {@link KeyEvent.Callback#onKeyDown(int, KeyEvent)
5750 * KeyEvent.Callback.onKeyDown()}: perform press of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005751 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or {@link KeyEvent#KEYCODE_ENTER}
5752 * is released, if the view is enabled and clickable.
5753 *
5754 * @param keyCode A key code that represents the button pressed, from
5755 * {@link android.view.KeyEvent}.
5756 * @param event The KeyEvent object that defines the button action.
5757 */
5758 public boolean onKeyDown(int keyCode, KeyEvent event) {
5759 boolean result = false;
5760
5761 switch (keyCode) {
5762 case KeyEvent.KEYCODE_DPAD_CENTER:
5763 case KeyEvent.KEYCODE_ENTER: {
5764 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5765 return true;
5766 }
5767 // Long clickable items don't necessarily have to be clickable
5768 if (((mViewFlags & CLICKABLE) == CLICKABLE ||
5769 (mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) &&
5770 (event.getRepeatCount() == 0)) {
5771 setPressed(true);
Patrick Dubroye0a799a2011-05-04 16:19:22 -07005772 checkForLongClick(0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005773 return true;
5774 }
5775 break;
5776 }
5777 }
5778 return result;
5779 }
5780
5781 /**
Dianne Hackborn83fe3f52009-09-12 23:38:30 -07005782 * Default implementation of {@link KeyEvent.Callback#onKeyLongPress(int, KeyEvent)
5783 * KeyEvent.Callback.onKeyLongPress()}: always returns false (doesn't handle
5784 * the event).
5785 */
5786 public boolean onKeyLongPress(int keyCode, KeyEvent event) {
5787 return false;
5788 }
5789
5790 /**
Jeff Brown995e7742010-12-22 16:59:36 -08005791 * Default implementation of {@link KeyEvent.Callback#onKeyUp(int, KeyEvent)
5792 * KeyEvent.Callback.onKeyUp()}: perform clicking of the view
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005793 * when {@link KeyEvent#KEYCODE_DPAD_CENTER} or
5794 * {@link KeyEvent#KEYCODE_ENTER} is released.
5795 *
5796 * @param keyCode A key code that represents the button pressed, from
5797 * {@link android.view.KeyEvent}.
5798 * @param event The KeyEvent object that defines the button action.
5799 */
5800 public boolean onKeyUp(int keyCode, KeyEvent event) {
5801 boolean result = false;
5802
5803 switch (keyCode) {
5804 case KeyEvent.KEYCODE_DPAD_CENTER:
5805 case KeyEvent.KEYCODE_ENTER: {
5806 if ((mViewFlags & ENABLED_MASK) == DISABLED) {
5807 return true;
5808 }
5809 if ((mViewFlags & CLICKABLE) == CLICKABLE && isPressed()) {
5810 setPressed(false);
5811
5812 if (!mHasPerformedLongPress) {
5813 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05005814 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005815
5816 result = performClick();
5817 }
5818 }
5819 break;
5820 }
5821 }
5822 return result;
5823 }
5824
5825 /**
5826 * Default implementation of {@link KeyEvent.Callback#onKeyMultiple(int, int, KeyEvent)
5827 * KeyEvent.Callback.onKeyMultiple()}: always returns false (doesn't handle
5828 * the event).
5829 *
5830 * @param keyCode A key code that represents the button pressed, from
5831 * {@link android.view.KeyEvent}.
5832 * @param repeatCount The number of times the action was made.
5833 * @param event The KeyEvent object that defines the button action.
5834 */
5835 public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
5836 return false;
5837 }
5838
5839 /**
Jeff Brown64da12a2011-01-04 19:57:47 -08005840 * Called on the focused view when a key shortcut event is not handled.
5841 * Override this method to implement local key shortcuts for the View.
5842 * Key shortcuts can also be implemented by setting the
5843 * {@link MenuItem#setShortcut(char, char) shortcut} property of menu items.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005844 *
5845 * @param keyCode The value in event.getKeyCode().
5846 * @param event Description of the key event.
5847 * @return If you handled the event, return true. If you want to allow the
5848 * event to be handled by the next receiver, return false.
5849 */
5850 public boolean onKeyShortcut(int keyCode, KeyEvent event) {
5851 return false;
5852 }
5853
5854 /**
5855 * Check whether the called view is a text editor, in which case it
5856 * would make sense to automatically display a soft input window for
5857 * it. Subclasses should override this if they implement
5858 * {@link #onCreateInputConnection(EditorInfo)} to return true if
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005859 * a call on that method would return a non-null InputConnection, and
5860 * they are really a first-class editor that the user would normally
5861 * start typing on when the go into a window containing your view.
Romain Guy8506ab42009-06-11 17:35:47 -07005862 *
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005863 * <p>The default implementation always returns false. This does
5864 * <em>not</em> mean that its {@link #onCreateInputConnection(EditorInfo)}
5865 * will not be called or the user can not otherwise perform edits on your
5866 * view; it is just a hint to the system that this is not the primary
5867 * purpose of this view.
Romain Guy8506ab42009-06-11 17:35:47 -07005868 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005869 * @return Returns true if this view is a text editor, else false.
5870 */
5871 public boolean onCheckIsTextEditor() {
5872 return false;
5873 }
Romain Guy8506ab42009-06-11 17:35:47 -07005874
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005875 /**
5876 * Create a new InputConnection for an InputMethod to interact
5877 * with the view. The default implementation returns null, since it doesn't
5878 * support input methods. You can override this to implement such support.
5879 * This is only needed for views that take focus and text input.
Romain Guy8506ab42009-06-11 17:35:47 -07005880 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005881 * <p>When implementing this, you probably also want to implement
5882 * {@link #onCheckIsTextEditor()} to indicate you will return a
5883 * non-null InputConnection.
5884 *
5885 * @param outAttrs Fill in with attribute information about the connection.
5886 */
5887 public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
5888 return null;
5889 }
5890
5891 /**
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005892 * Called by the {@link android.view.inputmethod.InputMethodManager}
5893 * when a view who is not the current
5894 * input connection target is trying to make a call on the manager. The
5895 * default implementation returns false; you can override this to return
5896 * true for certain views if you are performing InputConnection proxying
5897 * to them.
5898 * @param view The View that is making the InputMethodManager call.
5899 * @return Return true to allow the call, false to reject.
5900 */
5901 public boolean checkInputConnectionProxy(View view) {
5902 return false;
5903 }
Romain Guy8506ab42009-06-11 17:35:47 -07005904
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -07005905 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005906 * Show the context menu for this view. It is not safe to hold on to the
5907 * menu after returning from this method.
5908 *
Gilles Debunnef788a9f2010-07-22 10:17:23 -07005909 * You should normally not overload this method. Overload
5910 * {@link #onCreateContextMenu(ContextMenu)} or define an
5911 * {@link OnCreateContextMenuListener} to add items to the context menu.
5912 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005913 * @param menu The context menu to populate
5914 */
5915 public void createContextMenu(ContextMenu menu) {
5916 ContextMenuInfo menuInfo = getContextMenuInfo();
5917
5918 // Sets the current menu info so all items added to menu will have
5919 // my extra info set.
5920 ((MenuBuilder)menu).setCurrentMenuInfo(menuInfo);
5921
5922 onCreateContextMenu(menu);
5923 if (mOnCreateContextMenuListener != null) {
5924 mOnCreateContextMenuListener.onCreateContextMenu(menu, this, menuInfo);
5925 }
5926
5927 // Clear the extra information so subsequent items that aren't mine don't
5928 // have my extra info.
5929 ((MenuBuilder)menu).setCurrentMenuInfo(null);
5930
5931 if (mParent != null) {
5932 mParent.createContextMenu(menu);
5933 }
5934 }
5935
5936 /**
5937 * Views should implement this if they have extra information to associate
5938 * with the context menu. The return result is supplied as a parameter to
5939 * the {@link OnCreateContextMenuListener#onCreateContextMenu(ContextMenu, View, ContextMenuInfo)}
5940 * callback.
5941 *
5942 * @return Extra information about the item for which the context menu
5943 * should be shown. This information will vary across different
5944 * subclasses of View.
5945 */
5946 protected ContextMenuInfo getContextMenuInfo() {
5947 return null;
5948 }
5949
5950 /**
5951 * Views should implement this if the view itself is going to add items to
5952 * the context menu.
5953 *
5954 * @param menu the context menu to populate
5955 */
5956 protected void onCreateContextMenu(ContextMenu menu) {
5957 }
5958
5959 /**
5960 * Implement this method to handle trackball motion events. The
5961 * <em>relative</em> movement of the trackball since the last event
5962 * can be retrieve with {@link MotionEvent#getX MotionEvent.getX()} and
5963 * {@link MotionEvent#getY MotionEvent.getY()}. These are normalized so
5964 * that a movement of 1 corresponds to the user pressing one DPAD key (so
5965 * they will often be fractional values, representing the more fine-grained
5966 * movement information available from a trackball).
5967 *
5968 * @param event The motion event.
5969 * @return True if the event was handled, false otherwise.
5970 */
5971 public boolean onTrackballEvent(MotionEvent event) {
5972 return false;
5973 }
5974
5975 /**
Jeff Browncb1404e2011-01-15 18:14:15 -08005976 * Implement this method to handle generic motion events.
5977 * <p>
Jeff Brown33bbfd22011-02-24 20:55:35 -08005978 * Generic motion events describe joystick movements, mouse hovers, track pad
5979 * touches, scroll wheel movements and other input events. The
Jeff Browncb1404e2011-01-15 18:14:15 -08005980 * {@link MotionEvent#getSource() source} of the motion event specifies
5981 * the class of input that was received. Implementations of this method
5982 * must examine the bits in the source before processing the event.
5983 * The following code example shows how this is done.
Jeff Brown33bbfd22011-02-24 20:55:35 -08005984 * </p><p>
5985 * Generic motion events with source class {@link InputDevice#SOURCE_CLASS_POINTER}
5986 * are delivered to the view under the pointer. All other generic motion events are
5987 * delivered to the focused view.
Jeff Browncb1404e2011-01-15 18:14:15 -08005988 * </p>
5989 * <code>
5990 * public boolean onGenericMotionEvent(MotionEvent event) {
5991 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_JOYSTICK) != 0) {
Jeff Brown33bbfd22011-02-24 20:55:35 -08005992 * if (event.getAction() == MotionEvent.ACTION_MOVE) {
5993 * // process the joystick movement...
5994 * return true;
5995 * }
5996 * }
5997 * if ((event.getSource() &amp; InputDevice.SOURCE_CLASS_POINTER) != 0) {
5998 * switch (event.getAction()) {
5999 * case MotionEvent.ACTION_HOVER_MOVE:
6000 * // process the mouse hover movement...
6001 * return true;
6002 * case MotionEvent.ACTION_SCROLL:
6003 * // process the scroll wheel movement...
6004 * return true;
6005 * }
Jeff Browncb1404e2011-01-15 18:14:15 -08006006 * }
6007 * return super.onGenericMotionEvent(event);
6008 * }
6009 * </code>
6010 *
6011 * @param event The generic motion event being processed.
Jeff Browna032cc02011-03-07 16:56:21 -08006012 * @return True if the event was handled, false otherwise.
Jeff Browncb1404e2011-01-15 18:14:15 -08006013 */
6014 public boolean onGenericMotionEvent(MotionEvent event) {
6015 return false;
6016 }
6017
6018 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006019 * Implement this method to handle hover events.
6020 * <p>
Jeff Brown10b62902011-06-20 16:40:37 -07006021 * This method is called whenever a pointer is hovering into, over, or out of the
6022 * bounds of a view and the view is not currently being touched.
6023 * Hover events are represented as pointer events with action
6024 * {@link MotionEvent#ACTION_HOVER_ENTER}, {@link MotionEvent#ACTION_HOVER_MOVE},
6025 * or {@link MotionEvent#ACTION_HOVER_EXIT}.
6026 * </p>
6027 * <ul>
6028 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_ENTER}
6029 * when the pointer enters the bounds of the view.</li>
6030 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_MOVE}
6031 * when the pointer has already entered the bounds of the view and has moved.</li>
6032 * <li>The view receives a hover event with action {@link MotionEvent#ACTION_HOVER_EXIT}
6033 * when the pointer has exited the bounds of the view or when the pointer is
6034 * about to go down due to a button click, tap, or similar user action that
6035 * causes the view to be touched.</li>
6036 * </ul>
6037 * <p>
6038 * The view should implement this method to return true to indicate that it is
6039 * handling the hover event, such as by changing its drawable state.
Jeff Browna032cc02011-03-07 16:56:21 -08006040 * </p><p>
Jeff Brown10b62902011-06-20 16:40:37 -07006041 * The default implementation calls {@link #setHovered} to update the hovered state
6042 * of the view when a hover enter or hover exit event is received, if the view
Jeff Browna1b24182011-07-28 13:38:24 -07006043 * is enabled and is clickable. The default implementation also sends hover
6044 * accessibility events.
Jeff Browna032cc02011-03-07 16:56:21 -08006045 * </p>
6046 *
6047 * @param event The motion event that describes the hover.
Jeff Brown10b62902011-06-20 16:40:37 -07006048 * @return True if the view handled the hover event.
6049 *
6050 * @see #isHovered
6051 * @see #setHovered
6052 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006053 */
6054 public boolean onHoverEvent(MotionEvent event) {
Jeff Browna1b24182011-07-28 13:38:24 -07006055 switch (event.getAction()) {
6056 case MotionEvent.ACTION_HOVER_ENTER:
6057 if (!hasHoveredChild() && !mSendingHoverAccessibilityEvents) {
6058 mSendingHoverAccessibilityEvents = true;
6059 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_ENTER);
6060 }
6061 break;
6062 case MotionEvent.ACTION_HOVER_EXIT:
6063 if (mSendingHoverAccessibilityEvents) {
6064 mSendingHoverAccessibilityEvents = false;
6065 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_HOVER_EXIT);
6066 }
6067 break;
6068 }
6069
Jeff Brown87b7f802011-06-21 18:35:45 -07006070 if (isHoverable()) {
Jeff Brown10b62902011-06-20 16:40:37 -07006071 switch (event.getAction()) {
6072 case MotionEvent.ACTION_HOVER_ENTER:
6073 setHovered(true);
6074 break;
6075 case MotionEvent.ACTION_HOVER_EXIT:
6076 setHovered(false);
6077 break;
6078 }
Jeff Browna1b24182011-07-28 13:38:24 -07006079
6080 // Dispatch the event to onGenericMotionEvent before returning true.
6081 // This is to provide compatibility with existing applications that
6082 // handled HOVER_MOVE events in onGenericMotionEvent and that would
6083 // break because of the new default handling for hoverable views
6084 // in onHoverEvent.
6085 // Note that onGenericMotionEvent will be called by default when
6086 // onHoverEvent returns false (refer to dispatchGenericMotionEvent).
6087 dispatchGenericMotionEventInternal(event);
Jeff Brown10b62902011-06-20 16:40:37 -07006088 return true;
Jeff Browna032cc02011-03-07 16:56:21 -08006089 }
Svetoslav Ganov736c2752011-04-22 18:30:36 -07006090 return false;
Jeff Browna032cc02011-03-07 16:56:21 -08006091 }
6092
6093 /**
Jeff Brown87b7f802011-06-21 18:35:45 -07006094 * Returns true if the view should handle {@link #onHoverEvent}
6095 * by calling {@link #setHovered} to change its hovered state.
6096 *
6097 * @return True if the view is hoverable.
6098 */
6099 private boolean isHoverable() {
6100 final int viewFlags = mViewFlags;
Romain Guy02ccac62011-06-24 13:20:23 -07006101 //noinspection SimplifiableIfStatement
Jeff Brown87b7f802011-06-21 18:35:45 -07006102 if ((viewFlags & ENABLED_MASK) == DISABLED) {
6103 return false;
6104 }
6105
6106 return (viewFlags & CLICKABLE) == CLICKABLE
6107 || (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE;
6108 }
6109
6110 /**
Jeff Browna032cc02011-03-07 16:56:21 -08006111 * Returns true if the view is currently hovered.
6112 *
6113 * @return True if the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006114 *
6115 * @see #setHovered
6116 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006117 */
Jeff Brown10b62902011-06-20 16:40:37 -07006118 @ViewDebug.ExportedProperty
Jeff Browna032cc02011-03-07 16:56:21 -08006119 public boolean isHovered() {
6120 return (mPrivateFlags & HOVERED) != 0;
6121 }
6122
6123 /**
6124 * Sets whether the view is currently hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006125 * <p>
6126 * Calling this method also changes the drawable state of the view. This
6127 * enables the view to react to hover by using different drawable resources
6128 * to change its appearance.
6129 * </p><p>
6130 * The {@link #onHoverChanged} method is called when the hovered state changes.
6131 * </p>
Jeff Browna032cc02011-03-07 16:56:21 -08006132 *
6133 * @param hovered True if the view is hovered.
Jeff Brown10b62902011-06-20 16:40:37 -07006134 *
6135 * @see #isHovered
6136 * @see #onHoverChanged
Jeff Browna032cc02011-03-07 16:56:21 -08006137 */
6138 public void setHovered(boolean hovered) {
6139 if (hovered) {
6140 if ((mPrivateFlags & HOVERED) == 0) {
6141 mPrivateFlags |= HOVERED;
6142 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006143 onHoverChanged(true);
Jeff Browna032cc02011-03-07 16:56:21 -08006144 }
6145 } else {
6146 if ((mPrivateFlags & HOVERED) != 0) {
6147 mPrivateFlags &= ~HOVERED;
6148 refreshDrawableState();
Jeff Brown10b62902011-06-20 16:40:37 -07006149 onHoverChanged(false);
Jeff Browna032cc02011-03-07 16:56:21 -08006150 }
6151 }
6152 }
6153
6154 /**
Jeff Brown10b62902011-06-20 16:40:37 -07006155 * Implement this method to handle hover state changes.
6156 * <p>
6157 * This method is called whenever the hover state changes as a result of a
6158 * call to {@link #setHovered}.
6159 * </p>
6160 *
6161 * @param hovered The current hover state, as returned by {@link #isHovered}.
6162 *
6163 * @see #isHovered
6164 * @see #setHovered
6165 */
6166 public void onHoverChanged(boolean hovered) {
Jeff Brown10b62902011-06-20 16:40:37 -07006167 }
6168
6169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006170 * Implement this method to handle touch screen motion events.
6171 *
6172 * @param event The motion event.
6173 * @return True if the event was handled, false otherwise.
6174 */
6175 public boolean onTouchEvent(MotionEvent event) {
6176 final int viewFlags = mViewFlags;
6177
6178 if ((viewFlags & ENABLED_MASK) == DISABLED) {
Svetoslav Ganov77b80c02011-03-15 20:52:58 -07006179 if (event.getAction() == MotionEvent.ACTION_UP && (mPrivateFlags & PRESSED) != 0) {
6180 mPrivateFlags &= ~PRESSED;
6181 refreshDrawableState();
6182 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006183 // A disabled view that is clickable still consumes the touch
6184 // events, it just doesn't respond to them.
6185 return (((viewFlags & CLICKABLE) == CLICKABLE ||
6186 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE));
6187 }
6188
6189 if (mTouchDelegate != null) {
6190 if (mTouchDelegate.onTouchEvent(event)) {
6191 return true;
6192 }
6193 }
6194
6195 if (((viewFlags & CLICKABLE) == CLICKABLE ||
6196 (viewFlags & LONG_CLICKABLE) == LONG_CLICKABLE)) {
6197 switch (event.getAction()) {
6198 case MotionEvent.ACTION_UP:
Adam Powelle14579b2009-12-16 18:39:52 -08006199 boolean prepressed = (mPrivateFlags & PREPRESSED) != 0;
6200 if ((mPrivateFlags & PRESSED) != 0 || prepressed) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006201 // take focus if we don't have it already and we should in
6202 // touch mode.
6203 boolean focusTaken = false;
6204 if (isFocusable() && isFocusableInTouchMode() && !isFocused()) {
6205 focusTaken = requestFocus();
6206 }
6207
Dianne Hackbornbe1f6222011-01-20 15:24:28 -08006208 if (prepressed) {
6209 // The button is being released before we actually
6210 // showed it as pressed. Make it show the pressed
6211 // state now (before scheduling the click) to ensure
6212 // the user sees it.
6213 mPrivateFlags |= PRESSED;
6214 refreshDrawableState();
6215 }
Joe Malin32736f02011-01-19 16:14:20 -08006216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006217 if (!mHasPerformedLongPress) {
6218 // This is a tap, so remove the longpress check
Maryam Garrett1549dd12009-12-15 16:06:36 -05006219 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006220
6221 // Only perform take click actions if we were in the pressed state
6222 if (!focusTaken) {
Adam Powella35d7682010-03-12 14:48:13 -08006223 // Use a Runnable and post this rather than calling
6224 // performClick directly. This lets other visual state
6225 // of the view update before click actions start.
6226 if (mPerformClick == null) {
6227 mPerformClick = new PerformClick();
6228 }
6229 if (!post(mPerformClick)) {
6230 performClick();
6231 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006232 }
6233 }
6234
6235 if (mUnsetPressedState == null) {
6236 mUnsetPressedState = new UnsetPressedState();
6237 }
6238
Adam Powelle14579b2009-12-16 18:39:52 -08006239 if (prepressed) {
Adam Powelle14579b2009-12-16 18:39:52 -08006240 postDelayed(mUnsetPressedState,
6241 ViewConfiguration.getPressedStateDuration());
6242 } else if (!post(mUnsetPressedState)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006243 // If the post failed, unpress right now
6244 mUnsetPressedState.run();
6245 }
Adam Powelle14579b2009-12-16 18:39:52 -08006246 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006247 }
6248 break;
6249
6250 case MotionEvent.ACTION_DOWN:
Adam Powell3b023392010-03-11 16:30:28 -08006251 mHasPerformedLongPress = false;
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006252
Jeff Brownfe9f8ab2011-05-06 18:20:01 -07006253 if (performButtonActionOnTouchDown(event)) {
6254 break;
6255 }
6256
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006257 // Walk up the hierarchy to determine if we're inside a scrolling container.
Adam Powell10298662011-08-14 18:26:30 -07006258 boolean isInScrollingContainer = isInScrollingContainer();
Patrick Dubroye0a799a2011-05-04 16:19:22 -07006259
6260 // For views inside a scrolling container, delay the pressed feedback for
6261 // a short period in case this is a scroll.
6262 if (isInScrollingContainer) {
6263 mPrivateFlags |= PREPRESSED;
6264 if (mPendingCheckForTap == null) {
6265 mPendingCheckForTap = new CheckForTap();
6266 }
6267 postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
6268 } else {
6269 // Not inside a scrolling container, so show the feedback right away
6270 mPrivateFlags |= PRESSED;
6271 refreshDrawableState();
6272 checkForLongClick(0);
6273 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006274 break;
6275
6276 case MotionEvent.ACTION_CANCEL:
6277 mPrivateFlags &= ~PRESSED;
6278 refreshDrawableState();
Adam Powelle14579b2009-12-16 18:39:52 -08006279 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006280 break;
6281
6282 case MotionEvent.ACTION_MOVE:
6283 final int x = (int) event.getX();
6284 final int y = (int) event.getY();
6285
6286 // Be lenient about moving outside of buttons
Chet Haasec3aa3612010-06-17 08:50:37 -07006287 if (!pointInView(x, y, mTouchSlop)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006288 // Outside button
Adam Powelle14579b2009-12-16 18:39:52 -08006289 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006290 if ((mPrivateFlags & PRESSED) != 0) {
Adam Powelle14579b2009-12-16 18:39:52 -08006291 // Remove any future long press/tap checks
Maryam Garrett1549dd12009-12-15 16:06:36 -05006292 removeLongPressCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006293
6294 // Need to switch from pressed to not pressed
6295 mPrivateFlags &= ~PRESSED;
6296 refreshDrawableState();
6297 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006298 }
6299 break;
6300 }
6301 return true;
6302 }
6303
6304 return false;
6305 }
6306
6307 /**
Adam Powell10298662011-08-14 18:26:30 -07006308 * @hide
6309 */
6310 public boolean isInScrollingContainer() {
6311 ViewParent p = getParent();
6312 while (p != null && p instanceof ViewGroup) {
6313 if (((ViewGroup) p).shouldDelayChildPressedState()) {
6314 return true;
6315 }
6316 p = p.getParent();
6317 }
6318 return false;
6319 }
6320
6321 /**
Maryam Garrett1549dd12009-12-15 16:06:36 -05006322 * Remove the longpress detection timer.
6323 */
6324 private void removeLongPressCallback() {
6325 if (mPendingCheckForLongPress != null) {
6326 removeCallbacks(mPendingCheckForLongPress);
6327 }
6328 }
Adam Powell3cb8b632011-01-21 15:34:14 -08006329
6330 /**
6331 * Remove the pending click action
6332 */
6333 private void removePerformClickCallback() {
6334 if (mPerformClick != null) {
6335 removeCallbacks(mPerformClick);
6336 }
6337 }
6338
Adam Powelle14579b2009-12-16 18:39:52 -08006339 /**
Romain Guya440b002010-02-24 15:57:54 -08006340 * Remove the prepress detection timer.
6341 */
6342 private void removeUnsetPressCallback() {
6343 if ((mPrivateFlags & PRESSED) != 0 && mUnsetPressedState != null) {
6344 setPressed(false);
6345 removeCallbacks(mUnsetPressedState);
6346 }
6347 }
6348
6349 /**
Adam Powelle14579b2009-12-16 18:39:52 -08006350 * Remove the tap detection timer.
6351 */
6352 private void removeTapCallback() {
6353 if (mPendingCheckForTap != null) {
6354 mPrivateFlags &= ~PREPRESSED;
6355 removeCallbacks(mPendingCheckForTap);
6356 }
6357 }
Maryam Garrett1549dd12009-12-15 16:06:36 -05006358
6359 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006360 * Cancels a pending long press. Your subclass can use this if you
6361 * want the context menu to come up if the user presses and holds
6362 * at the same place, but you don't want it to come up if they press
6363 * and then move around enough to cause scrolling.
6364 */
6365 public void cancelLongPress() {
Maryam Garrett1549dd12009-12-15 16:06:36 -05006366 removeLongPressCallback();
Adam Powell732ebb12010-02-02 15:28:14 -08006367
6368 /*
6369 * The prepressed state handled by the tap callback is a display
6370 * construct, but the tap callback will post a long press callback
6371 * less its own timeout. Remove it here.
6372 */
6373 removeTapCallback();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006374 }
6375
6376 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07006377 * Remove the pending callback for sending a
6378 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
6379 */
6380 private void removeSendViewScrolledAccessibilityEventCallback() {
6381 if (mSendViewScrolledAccessibilityEvent != null) {
6382 removeCallbacks(mSendViewScrolledAccessibilityEvent);
6383 }
6384 }
6385
6386 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006387 * Sets the TouchDelegate for this View.
6388 */
6389 public void setTouchDelegate(TouchDelegate delegate) {
6390 mTouchDelegate = delegate;
6391 }
6392
6393 /**
6394 * Gets the TouchDelegate for this View.
6395 */
6396 public TouchDelegate getTouchDelegate() {
6397 return mTouchDelegate;
6398 }
6399
6400 /**
6401 * Set flags controlling behavior of this view.
6402 *
6403 * @param flags Constant indicating the value which should be set
6404 * @param mask Constant indicating the bit range that should be changed
6405 */
6406 void setFlags(int flags, int mask) {
6407 int old = mViewFlags;
6408 mViewFlags = (mViewFlags & ~mask) | (flags & mask);
6409
6410 int changed = mViewFlags ^ old;
6411 if (changed == 0) {
6412 return;
6413 }
6414 int privateFlags = mPrivateFlags;
6415
6416 /* Check if the FOCUSABLE bit has changed */
6417 if (((changed & FOCUSABLE_MASK) != 0) &&
6418 ((privateFlags & HAS_BOUNDS) !=0)) {
6419 if (((old & FOCUSABLE_MASK) == FOCUSABLE)
6420 && ((privateFlags & FOCUSED) != 0)) {
6421 /* Give up focus if we are no longer focusable */
6422 clearFocus();
6423 } else if (((old & FOCUSABLE_MASK) == NOT_FOCUSABLE)
6424 && ((privateFlags & FOCUSED) == 0)) {
6425 /*
6426 * Tell the view system that we are now available to take focus
6427 * if no one else already has it.
6428 */
6429 if (mParent != null) mParent.focusableViewAvailable(this);
6430 }
6431 }
6432
6433 if ((flags & VISIBILITY_MASK) == VISIBLE) {
6434 if ((changed & VISIBILITY_MASK) != 0) {
6435 /*
Chet Haase4324ead2011-08-24 21:31:03 -07006436 * If this view is becoming visible, invalidate it in case it changed while
Chet Haaseaceafe62011-08-26 15:44:33 -07006437 * it was not visible. Marking it drawn ensures that the invalidation will
6438 * go through.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006439 */
Chet Haaseaceafe62011-08-26 15:44:33 -07006440 mPrivateFlags |= DRAWN;
Chet Haase4324ead2011-08-24 21:31:03 -07006441 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006442
6443 needGlobalAttributesUpdate(true);
6444
6445 // a view becoming visible is worth notifying the parent
6446 // about in case nothing has focus. even if this specific view
6447 // isn't focusable, it may contain something that is, so let
6448 // the root view try to give this focus if nothing else does.
6449 if ((mParent != null) && (mBottom > mTop) && (mRight > mLeft)) {
6450 mParent.focusableViewAvailable(this);
6451 }
6452 }
6453 }
6454
6455 /* Check if the GONE bit has changed */
6456 if ((changed & GONE) != 0) {
6457 needGlobalAttributesUpdate(false);
6458 requestLayout();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006459
Romain Guyecd80ee2009-12-03 17:13:02 -08006460 if (((mViewFlags & VISIBILITY_MASK) == GONE)) {
6461 if (hasFocus()) clearFocus();
6462 destroyDrawingCache();
Chet Haaseaceafe62011-08-26 15:44:33 -07006463 if (mParent instanceof View) {
6464 // GONE views noop invalidation, so invalidate the parent
6465 ((View) mParent).invalidate(true);
6466 }
6467 // Mark the view drawn to ensure that it gets invalidated properly the next
6468 // time it is visible and gets invalidated
6469 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006470 }
6471 if (mAttachInfo != null) {
6472 mAttachInfo.mViewVisibilityChanged = true;
6473 }
6474 }
6475
6476 /* Check if the VISIBLE bit has changed */
6477 if ((changed & INVISIBLE) != 0) {
6478 needGlobalAttributesUpdate(false);
Chet Haasec8a9a702011-06-17 12:13:42 -07006479 /*
6480 * If this view is becoming invisible, set the DRAWN flag so that
6481 * the next invalidate() will not be skipped.
6482 */
6483 mPrivateFlags |= DRAWN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006484
6485 if (((mViewFlags & VISIBILITY_MASK) == INVISIBLE) && hasFocus()) {
6486 // root view becoming invisible shouldn't clear focus
6487 if (getRootView() != this) {
6488 clearFocus();
6489 }
6490 }
6491 if (mAttachInfo != null) {
6492 mAttachInfo.mViewVisibilityChanged = true;
6493 }
6494 }
6495
Adam Powell326d8082009-12-09 15:10:07 -08006496 if ((changed & VISIBILITY_MASK) != 0) {
Chet Haase5e25c2c2010-09-16 11:15:56 -07006497 if (mParent instanceof ViewGroup) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006498 ((ViewGroup) mParent).onChildVisibilityChanged(this, (flags & VISIBILITY_MASK));
6499 ((View) mParent).invalidate(true);
Chet Haasee4e6e202011-08-29 14:34:30 -07006500 } else if (mParent != null) {
6501 mParent.invalidateChild(this, null);
Chet Haase5e25c2c2010-09-16 11:15:56 -07006502 }
Adam Powell326d8082009-12-09 15:10:07 -08006503 dispatchVisibilityChanged(this, (flags & VISIBILITY_MASK));
6504 }
6505
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006506 if ((changed & WILL_NOT_CACHE_DRAWING) != 0) {
6507 destroyDrawingCache();
6508 }
6509
6510 if ((changed & DRAWING_CACHE_ENABLED) != 0) {
6511 destroyDrawingCache();
6512 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Romain Guy0fd89bf2011-01-26 15:41:30 -08006513 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006514 }
6515
6516 if ((changed & DRAWING_CACHE_QUALITY_MASK) != 0) {
6517 destroyDrawingCache();
6518 mPrivateFlags &= ~DRAWING_CACHE_VALID;
6519 }
6520
6521 if ((changed & DRAW_MASK) != 0) {
6522 if ((mViewFlags & WILL_NOT_DRAW) != 0) {
6523 if (mBGDrawable != null) {
6524 mPrivateFlags &= ~SKIP_DRAW;
6525 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
6526 } else {
6527 mPrivateFlags |= SKIP_DRAW;
6528 }
6529 } else {
6530 mPrivateFlags &= ~SKIP_DRAW;
6531 }
6532 requestLayout();
Romain Guy0fd89bf2011-01-26 15:41:30 -08006533 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006534 }
6535
6536 if ((changed & KEEP_SCREEN_ON) != 0) {
Joe Onorato664644d2011-01-23 17:53:23 -08006537 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006538 mParent.recomputeViewAttributes(this);
6539 }
6540 }
Cibu Johny7632cb92010-02-22 13:01:02 -08006541
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07006542 if ((changed & LAYOUT_DIRECTION_MASK) != 0) {
Cibu Johny7632cb92010-02-22 13:01:02 -08006543 requestLayout();
6544 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006545 }
6546
6547 /**
6548 * Change the view's z order in the tree, so it's on top of other sibling
6549 * views
6550 */
6551 public void bringToFront() {
6552 if (mParent != null) {
6553 mParent.bringChildToFront(this);
6554 }
6555 }
6556
6557 /**
6558 * This is called in response to an internal scroll in this view (i.e., the
6559 * view scrolled its own contents). This is typically as a result of
6560 * {@link #scrollBy(int, int)} or {@link #scrollTo(int, int)} having been
6561 * called.
6562 *
6563 * @param l Current horizontal scroll origin.
6564 * @param t Current vertical scroll origin.
6565 * @param oldl Previous horizontal scroll origin.
6566 * @param oldt Previous vertical scroll origin.
6567 */
6568 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
Svetoslav Ganova0156172011-06-26 17:55:44 -07006569 if (AccessibilityManager.getInstance(mContext).isEnabled()) {
6570 postSendViewScrolledAccessibilityEventCallback();
6571 }
6572
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006573 mBackgroundSizeChanged = true;
6574
6575 final AttachInfo ai = mAttachInfo;
6576 if (ai != null) {
6577 ai.mViewScrollChanged = true;
6578 }
6579 }
6580
6581 /**
Chet Haase21cd1382010-09-01 17:42:29 -07006582 * Interface definition for a callback to be invoked when the layout bounds of a view
6583 * changes due to layout processing.
6584 */
6585 public interface OnLayoutChangeListener {
6586 /**
6587 * Called when the focus state of a view has changed.
6588 *
6589 * @param v The view whose state has changed.
6590 * @param left The new value of the view's left property.
6591 * @param top The new value of the view's top property.
6592 * @param right The new value of the view's right property.
6593 * @param bottom The new value of the view's bottom property.
6594 * @param oldLeft The previous value of the view's left property.
6595 * @param oldTop The previous value of the view's top property.
6596 * @param oldRight The previous value of the view's right property.
6597 * @param oldBottom The previous value of the view's bottom property.
6598 */
6599 void onLayoutChange(View v, int left, int top, int right, int bottom,
6600 int oldLeft, int oldTop, int oldRight, int oldBottom);
6601 }
6602
6603 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006604 * This is called during layout when the size of this view has changed. If
6605 * you were just added to the view hierarchy, you're called with the old
6606 * values of 0.
6607 *
6608 * @param w Current width of this view.
6609 * @param h Current height of this view.
6610 * @param oldw Old width of this view.
6611 * @param oldh Old height of this view.
6612 */
6613 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
6614 }
6615
6616 /**
6617 * Called by draw to draw the child views. This may be overridden
6618 * by derived classes to gain control just before its children are drawn
6619 * (but after its own view has been drawn).
6620 * @param canvas the canvas on which to draw the view
6621 */
6622 protected void dispatchDraw(Canvas canvas) {
6623 }
6624
6625 /**
6626 * Gets the parent of this view. Note that the parent is a
6627 * ViewParent and not necessarily a View.
6628 *
6629 * @return Parent of this view.
6630 */
6631 public final ViewParent getParent() {
6632 return mParent;
6633 }
6634
6635 /**
Chet Haasecca2c982011-05-20 14:34:18 -07006636 * Set the horizontal scrolled position of your view. This will cause a call to
6637 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6638 * invalidated.
6639 * @param value the x position to scroll to
6640 */
6641 public void setScrollX(int value) {
6642 scrollTo(value, mScrollY);
6643 }
6644
6645 /**
6646 * Set the vertical scrolled position of your view. This will cause a call to
6647 * {@link #onScrollChanged(int, int, int, int)} and the view will be
6648 * invalidated.
6649 * @param value the y position to scroll to
6650 */
6651 public void setScrollY(int value) {
6652 scrollTo(mScrollX, value);
6653 }
6654
6655 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006656 * Return the scrolled left position of this view. This is the left edge of
6657 * the displayed part of your view. You do not need to draw any pixels
6658 * farther left, since those are outside of the frame of your view on
6659 * screen.
6660 *
6661 * @return The left edge of the displayed part of your view, in pixels.
6662 */
6663 public final int getScrollX() {
6664 return mScrollX;
6665 }
6666
6667 /**
6668 * Return the scrolled top position of this view. This is the top edge of
6669 * the displayed part of your view. You do not need to draw any pixels above
6670 * it, since those are outside of the frame of your view on screen.
6671 *
6672 * @return The top edge of the displayed part of your view, in pixels.
6673 */
6674 public final int getScrollY() {
6675 return mScrollY;
6676 }
6677
6678 /**
6679 * Return the width of the your view.
6680 *
6681 * @return The width of your view, in pixels.
6682 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006683 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006684 public final int getWidth() {
6685 return mRight - mLeft;
6686 }
6687
6688 /**
6689 * Return the height of your view.
6690 *
6691 * @return The height of your view, in pixels.
6692 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07006693 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006694 public final int getHeight() {
6695 return mBottom - mTop;
6696 }
6697
6698 /**
6699 * Return the visible drawing bounds of your view. Fills in the output
6700 * rectangle with the values from getScrollX(), getScrollY(),
6701 * getWidth(), and getHeight().
6702 *
6703 * @param outRect The (scrolled) drawing bounds of the view.
6704 */
6705 public void getDrawingRect(Rect outRect) {
6706 outRect.left = mScrollX;
6707 outRect.top = mScrollY;
6708 outRect.right = mScrollX + (mRight - mLeft);
6709 outRect.bottom = mScrollY + (mBottom - mTop);
6710 }
6711
6712 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006713 * Like {@link #getMeasuredWidthAndState()}, but only returns the
6714 * raw width component (that is the result is masked by
6715 * {@link #MEASURED_SIZE_MASK}).
6716 *
6717 * @return The raw measured width of this view.
6718 */
6719 public final int getMeasuredWidth() {
6720 return mMeasuredWidth & MEASURED_SIZE_MASK;
6721 }
6722
6723 /**
6724 * Return the full width measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006725 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006726 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006727 * This should be used during measurement and layout calculations only. Use
6728 * {@link #getWidth()} to see how wide a view is after layout.
6729 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006730 * @return The measured width of this view as a bit mask.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006731 */
Dianne Hackborn189ee182010-12-02 21:48:53 -08006732 public final int getMeasuredWidthAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006733 return mMeasuredWidth;
6734 }
6735
6736 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006737 * Like {@link #getMeasuredHeightAndState()}, but only returns the
6738 * raw width component (that is the result is masked by
6739 * {@link #MEASURED_SIZE_MASK}).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006740 *
Dianne Hackborn189ee182010-12-02 21:48:53 -08006741 * @return The raw measured height of this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006742 */
6743 public final int getMeasuredHeight() {
Dianne Hackborn189ee182010-12-02 21:48:53 -08006744 return mMeasuredHeight & MEASURED_SIZE_MASK;
6745 }
6746
6747 /**
6748 * Return the full height measurement information for this view as computed
Romain Guy5c22a8c2011-05-13 11:48:45 -07006749 * by the most recent call to {@link #measure(int, int)}. This result is a bit mask
Dianne Hackborn189ee182010-12-02 21:48:53 -08006750 * as defined by {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
6751 * This should be used during measurement and layout calculations only. Use
6752 * {@link #getHeight()} to see how wide a view is after layout.
6753 *
6754 * @return The measured width of this view as a bit mask.
6755 */
6756 public final int getMeasuredHeightAndState() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006757 return mMeasuredHeight;
6758 }
6759
6760 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -08006761 * Return only the state bits of {@link #getMeasuredWidthAndState()}
6762 * and {@link #getMeasuredHeightAndState()}, combined into one integer.
6763 * The width component is in the regular bits {@link #MEASURED_STATE_MASK}
6764 * and the height component is at the shifted bits
6765 * {@link #MEASURED_HEIGHT_STATE_SHIFT}>>{@link #MEASURED_STATE_MASK}.
6766 */
6767 public final int getMeasuredState() {
6768 return (mMeasuredWidth&MEASURED_STATE_MASK)
6769 | ((mMeasuredHeight>>MEASURED_HEIGHT_STATE_SHIFT)
6770 & (MEASURED_STATE_MASK>>MEASURED_HEIGHT_STATE_SHIFT));
6771 }
6772
6773 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006774 * The transform matrix of this view, which is calculated based on the current
6775 * roation, scale, and pivot properties.
6776 *
6777 * @see #getRotation()
6778 * @see #getScaleX()
6779 * @see #getScaleY()
6780 * @see #getPivotX()
6781 * @see #getPivotY()
6782 * @return The current transform matrix for the view
6783 */
6784 public Matrix getMatrix() {
Jeff Brown86671742010-09-30 20:00:15 -07006785 updateMatrix();
Romain Guy33e72ae2010-07-17 12:40:29 -07006786 return mMatrix;
6787 }
6788
6789 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006790 * Utility function to determine if the value is far enough away from zero to be
6791 * considered non-zero.
6792 * @param value A floating point value to check for zero-ness
6793 * @return whether the passed-in value is far enough away from zero to be considered non-zero
6794 */
6795 private static boolean nonzero(float value) {
6796 return (value < -NONZERO_EPSILON || value > NONZERO_EPSILON);
6797 }
6798
6799 /**
Jeff Brown86671742010-09-30 20:00:15 -07006800 * Returns true if the transform matrix is the identity matrix.
6801 * Recomputes the matrix if necessary.
Joe Malin32736f02011-01-19 16:14:20 -08006802 *
Romain Guy33e72ae2010-07-17 12:40:29 -07006803 * @return True if the transform matrix is the identity matrix, false otherwise.
6804 */
Jeff Brown86671742010-09-30 20:00:15 -07006805 final boolean hasIdentityMatrix() {
6806 updateMatrix();
6807 return mMatrixIsIdentity;
6808 }
6809
6810 /**
6811 * Recomputes the transform matrix if necessary.
6812 */
Romain Guy2fe9a8f2010-10-04 20:17:01 -07006813 private void updateMatrix() {
Chet Haasec3aa3612010-06-17 08:50:37 -07006814 if (mMatrixDirty) {
6815 // transform-related properties have changed since the last time someone
6816 // asked for the matrix; recalculate it with the current values
Chet Haasefd2b0022010-08-06 13:08:56 -07006817
6818 // Figure out if we need to update the pivot point
6819 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08006820 if ((mRight - mLeft) != mPrevWidth || (mBottom - mTop) != mPrevHeight) {
Chet Haasefd2b0022010-08-06 13:08:56 -07006821 mPrevWidth = mRight - mLeft;
6822 mPrevHeight = mBottom - mTop;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -08006823 mPivotX = mPrevWidth / 2f;
6824 mPivotY = mPrevHeight / 2f;
Chet Haasefd2b0022010-08-06 13:08:56 -07006825 }
6826 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006827 mMatrix.reset();
Chet Haase897247b2010-09-09 14:54:47 -07006828 if (!nonzero(mRotationX) && !nonzero(mRotationY)) {
6829 mMatrix.setTranslate(mTranslationX, mTranslationY);
6830 mMatrix.preRotate(mRotation, mPivotX, mPivotY);
6831 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
6832 } else {
Chet Haasefd2b0022010-08-06 13:08:56 -07006833 if (mCamera == null) {
6834 mCamera = new Camera();
6835 matrix3D = new Matrix();
6836 }
6837 mCamera.save();
Chet Haase897247b2010-09-09 14:54:47 -07006838 mMatrix.preScale(mScaleX, mScaleY, mPivotX, mPivotY);
Romain Guy47b8ade2011-02-23 19:46:33 -08006839 mCamera.rotate(mRotationX, mRotationY, -mRotation);
Chet Haasefd2b0022010-08-06 13:08:56 -07006840 mCamera.getMatrix(matrix3D);
6841 matrix3D.preTranslate(-mPivotX, -mPivotY);
Chet Haase897247b2010-09-09 14:54:47 -07006842 matrix3D.postTranslate(mPivotX + mTranslationX, mPivotY + mTranslationY);
Chet Haasefd2b0022010-08-06 13:08:56 -07006843 mMatrix.postConcat(matrix3D);
6844 mCamera.restore();
6845 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006846 mMatrixDirty = false;
6847 mMatrixIsIdentity = mMatrix.isIdentity();
6848 mInverseMatrixDirty = true;
6849 }
Chet Haasec3aa3612010-06-17 08:50:37 -07006850 }
6851
6852 /**
6853 * Utility method to retrieve the inverse of the current mMatrix property.
6854 * We cache the matrix to avoid recalculating it when transform properties
6855 * have not changed.
6856 *
6857 * @return The inverse of the current matrix of this view.
6858 */
Jeff Brown86671742010-09-30 20:00:15 -07006859 final Matrix getInverseMatrix() {
6860 updateMatrix();
Chet Haasec3aa3612010-06-17 08:50:37 -07006861 if (mInverseMatrixDirty) {
6862 if (mInverseMatrix == null) {
6863 mInverseMatrix = new Matrix();
6864 }
6865 mMatrix.invert(mInverseMatrix);
6866 mInverseMatrixDirty = false;
6867 }
6868 return mInverseMatrix;
6869 }
6870
6871 /**
Romain Guya5364ee2011-02-24 14:46:04 -08006872 * <p>Sets the distance along the Z axis (orthogonal to the X/Y plane on which
6873 * views are drawn) from the camera to this view. The camera's distance
6874 * affects 3D transformations, for instance rotations around the X and Y
6875 * axis. If the rotationX or rotationY properties are changed and this view is
6876 * large (more than half the size of the screen), it is recommended to always
6877 * use a camera distance that's greater than the height (X axis rotation) or
6878 * the width (Y axis rotation) of this view.</p>
6879 *
6880 * <p>The distance of the camera from the view plane can have an affect on the
6881 * perspective distortion of the view when it is rotated around the x or y axis.
6882 * For example, a large distance will result in a large viewing angle, and there
6883 * will not be much perspective distortion of the view as it rotates. A short
6884 * distance may cause much more perspective distortion upon rotation, and can
6885 * also result in some drawing artifacts if the rotated view ends up partially
6886 * behind the camera (which is why the recommendation is to use a distance at
6887 * least as far as the size of the view, if the view is to be rotated.)</p>
6888 *
6889 * <p>The distance is expressed in "depth pixels." The default distance depends
6890 * on the screen density. For instance, on a medium density display, the
6891 * default distance is 1280. On a high density display, the default distance
6892 * is 1920.</p>
6893 *
6894 * <p>If you want to specify a distance that leads to visually consistent
6895 * results across various densities, use the following formula:</p>
6896 * <pre>
6897 * float scale = context.getResources().getDisplayMetrics().density;
6898 * view.setCameraDistance(distance * scale);
6899 * </pre>
6900 *
6901 * <p>The density scale factor of a high density display is 1.5,
6902 * and 1920 = 1280 * 1.5.</p>
6903 *
6904 * @param distance The distance in "depth pixels", if negative the opposite
6905 * value is used
6906 *
6907 * @see #setRotationX(float)
6908 * @see #setRotationY(float)
6909 */
6910 public void setCameraDistance(float distance) {
6911 invalidateParentCaches();
6912 invalidate(false);
6913
6914 final float dpi = mResources.getDisplayMetrics().densityDpi;
6915 if (mCamera == null) {
6916 mCamera = new Camera();
6917 matrix3D = new Matrix();
6918 }
6919
6920 mCamera.setLocation(0.0f, 0.0f, -Math.abs(distance) / dpi);
6921 mMatrixDirty = true;
6922
6923 invalidate(false);
6924 }
6925
6926 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07006927 * The degrees that the view is rotated around the pivot point.
6928 *
Romain Guya5364ee2011-02-24 14:46:04 -08006929 * @see #setRotation(float)
Chet Haasec3aa3612010-06-17 08:50:37 -07006930 * @see #getPivotX()
6931 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006932 *
Chet Haasec3aa3612010-06-17 08:50:37 -07006933 * @return The degrees of rotation.
6934 */
6935 public float getRotation() {
6936 return mRotation;
6937 }
6938
6939 /**
Chet Haase897247b2010-09-09 14:54:47 -07006940 * Sets the degrees that the view is rotated around the pivot point. Increasing values
6941 * result in clockwise rotation.
Chet Haasec3aa3612010-06-17 08:50:37 -07006942 *
6943 * @param rotation The degrees of rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006944 *
6945 * @see #getRotation()
Chet Haasec3aa3612010-06-17 08:50:37 -07006946 * @see #getPivotX()
6947 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006948 * @see #setRotationX(float)
6949 * @see #setRotationY(float)
Chet Haase73066682010-11-29 15:55:32 -08006950 *
6951 * @attr ref android.R.styleable#View_rotation
Chet Haasec3aa3612010-06-17 08:50:37 -07006952 */
6953 public void setRotation(float rotation) {
6954 if (mRotation != rotation) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006955 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07006956 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07006957 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006958 mRotation = rotation;
6959 mMatrixDirty = true;
6960 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07006961 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07006962 }
6963 }
6964
6965 /**
Chet Haasefd2b0022010-08-06 13:08:56 -07006966 * The degrees that the view is rotated around the vertical axis through the pivot point.
6967 *
6968 * @see #getPivotX()
6969 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006970 * @see #setRotationY(float)
6971 *
Chet Haasefd2b0022010-08-06 13:08:56 -07006972 * @return The degrees of Y rotation.
6973 */
6974 public float getRotationY() {
6975 return mRotationY;
6976 }
6977
6978 /**
Chet Haase897247b2010-09-09 14:54:47 -07006979 * Sets the degrees that the view is rotated around the vertical axis through the pivot point.
6980 * Increasing values result in counter-clockwise rotation from the viewpoint of looking
6981 * down the y axis.
Romain Guya5364ee2011-02-24 14:46:04 -08006982 *
6983 * When rotating large views, it is recommended to adjust the camera distance
6984 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07006985 *
6986 * @param rotationY The degrees of Y rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08006987 *
6988 * @see #getRotationY()
Chet Haasefd2b0022010-08-06 13:08:56 -07006989 * @see #getPivotX()
6990 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08006991 * @see #setRotation(float)
6992 * @see #setRotationX(float)
6993 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08006994 *
6995 * @attr ref android.R.styleable#View_rotationY
Chet Haasefd2b0022010-08-06 13:08:56 -07006996 */
6997 public void setRotationY(float rotationY) {
6998 if (mRotationY != rotationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08006999 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007000 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007001 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007002 mRotationY = rotationY;
7003 mMatrixDirty = true;
7004 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007005 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007006 }
7007 }
7008
7009 /**
7010 * The degrees that the view is rotated around the horizontal axis through the pivot point.
7011 *
7012 * @see #getPivotX()
7013 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007014 * @see #setRotationX(float)
7015 *
Chet Haasefd2b0022010-08-06 13:08:56 -07007016 * @return The degrees of X rotation.
7017 */
7018 public float getRotationX() {
7019 return mRotationX;
7020 }
7021
7022 /**
Chet Haase897247b2010-09-09 14:54:47 -07007023 * Sets the degrees that the view is rotated around the horizontal axis through the pivot point.
7024 * Increasing values result in clockwise rotation from the viewpoint of looking down the
7025 * x axis.
Romain Guya5364ee2011-02-24 14:46:04 -08007026 *
7027 * When rotating large views, it is recommended to adjust the camera distance
7028 * accordingly. Refer to {@link #setCameraDistance(float)} for more information.
Chet Haasefd2b0022010-08-06 13:08:56 -07007029 *
7030 * @param rotationX The degrees of X rotation.
Romain Guya5364ee2011-02-24 14:46:04 -08007031 *
7032 * @see #getRotationX()
Chet Haasefd2b0022010-08-06 13:08:56 -07007033 * @see #getPivotX()
7034 * @see #getPivotY()
Romain Guya5364ee2011-02-24 14:46:04 -08007035 * @see #setRotation(float)
7036 * @see #setRotationY(float)
7037 * @see #setCameraDistance(float)
Chet Haase73066682010-11-29 15:55:32 -08007038 *
7039 * @attr ref android.R.styleable#View_rotationX
Chet Haasefd2b0022010-08-06 13:08:56 -07007040 */
7041 public void setRotationX(float rotationX) {
7042 if (mRotationX != rotationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007043 invalidateParentCaches();
Chet Haasefd2b0022010-08-06 13:08:56 -07007044 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007045 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007046 mRotationX = rotationX;
7047 mMatrixDirty = true;
7048 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007049 invalidate(false);
Chet Haasefd2b0022010-08-06 13:08:56 -07007050 }
7051 }
7052
7053 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007054 * The amount that the view is scaled in x around the pivot point, as a proportion of
7055 * the view's unscaled width. A value of 1, the default, means that no scaling is applied.
7056 *
Joe Onorato93162322010-09-16 15:42:01 -04007057 * <p>By default, this is 1.0f.
7058 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007059 * @see #getPivotX()
7060 * @see #getPivotY()
7061 * @return The scaling factor.
7062 */
7063 public float getScaleX() {
7064 return mScaleX;
7065 }
7066
7067 /**
7068 * Sets the amount that the view is scaled in x around the pivot point, as a proportion of
7069 * the view's unscaled width. A value of 1 means that no scaling is applied.
7070 *
7071 * @param scaleX The scaling factor.
7072 * @see #getPivotX()
7073 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007074 *
7075 * @attr ref android.R.styleable#View_scaleX
Chet Haasec3aa3612010-06-17 08:50:37 -07007076 */
7077 public void setScaleX(float scaleX) {
7078 if (mScaleX != scaleX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007079 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007080 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007081 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007082 mScaleX = scaleX;
7083 mMatrixDirty = true;
7084 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007085 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007086 }
7087 }
7088
7089 /**
7090 * The amount that the view is scaled in y around the pivot point, as a proportion of
7091 * the view's unscaled height. A value of 1, the default, means that no scaling is applied.
7092 *
Joe Onorato93162322010-09-16 15:42:01 -04007093 * <p>By default, this is 1.0f.
7094 *
Chet Haasec3aa3612010-06-17 08:50:37 -07007095 * @see #getPivotX()
7096 * @see #getPivotY()
7097 * @return The scaling factor.
7098 */
7099 public float getScaleY() {
7100 return mScaleY;
7101 }
7102
7103 /**
7104 * Sets the amount that the view is scaled in Y around the pivot point, as a proportion of
7105 * the view's unscaled width. A value of 1 means that no scaling is applied.
7106 *
7107 * @param scaleY The scaling factor.
7108 * @see #getPivotX()
7109 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007110 *
7111 * @attr ref android.R.styleable#View_scaleY
Chet Haasec3aa3612010-06-17 08:50:37 -07007112 */
7113 public void setScaleY(float scaleY) {
7114 if (mScaleY != scaleY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007115 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007116 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007117 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007118 mScaleY = scaleY;
7119 mMatrixDirty = true;
7120 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007121 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007122 }
7123 }
7124
7125 /**
7126 * The x location of the point around which the view is {@link #setRotation(float) rotated}
7127 * and {@link #setScaleX(float) scaled}.
7128 *
7129 * @see #getRotation()
7130 * @see #getScaleX()
7131 * @see #getScaleY()
7132 * @see #getPivotY()
7133 * @return The x location of the pivot point.
7134 */
7135 public float getPivotX() {
7136 return mPivotX;
7137 }
7138
7139 /**
7140 * Sets the x location of the point around which the view is
7141 * {@link #setRotation(float) rotated} and {@link #setScaleX(float) scaled}.
Chet Haasefd2b0022010-08-06 13:08:56 -07007142 * By default, the pivot point is centered on the object.
7143 * Setting this property disables this behavior and causes the view to use only the
7144 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007145 *
7146 * @param pivotX The x location of the pivot point.
7147 * @see #getRotation()
7148 * @see #getScaleX()
7149 * @see #getScaleY()
7150 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007151 *
7152 * @attr ref android.R.styleable#View_transformPivotX
Chet Haasec3aa3612010-06-17 08:50:37 -07007153 */
7154 public void setPivotX(float pivotX) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007155 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007156 if (mPivotX != pivotX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007157 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007158 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007159 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007160 mPivotX = pivotX;
7161 mMatrixDirty = true;
7162 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007163 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007164 }
7165 }
7166
7167 /**
7168 * The y location of the point around which the view is {@link #setRotation(float) rotated}
7169 * and {@link #setScaleY(float) scaled}.
7170 *
7171 * @see #getRotation()
7172 * @see #getScaleX()
7173 * @see #getScaleY()
7174 * @see #getPivotY()
7175 * @return The y location of the pivot point.
7176 */
7177 public float getPivotY() {
7178 return mPivotY;
7179 }
7180
7181 /**
7182 * Sets the y location of the point around which the view is {@link #setRotation(float) rotated}
Chet Haasefd2b0022010-08-06 13:08:56 -07007183 * and {@link #setScaleY(float) scaled}. By default, the pivot point is centered on the object.
7184 * Setting this property disables this behavior and causes the view to use only the
7185 * explicitly set pivotX and pivotY values.
Chet Haasec3aa3612010-06-17 08:50:37 -07007186 *
7187 * @param pivotY The y location of the pivot point.
7188 * @see #getRotation()
7189 * @see #getScaleX()
7190 * @see #getScaleY()
7191 * @see #getPivotY()
Chet Haase73066682010-11-29 15:55:32 -08007192 *
7193 * @attr ref android.R.styleable#View_transformPivotY
Chet Haasec3aa3612010-06-17 08:50:37 -07007194 */
7195 public void setPivotY(float pivotY) {
Chet Haasefd2b0022010-08-06 13:08:56 -07007196 mPrivateFlags |= PIVOT_EXPLICITLY_SET;
Chet Haasec3aa3612010-06-17 08:50:37 -07007197 if (mPivotY != pivotY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007198 invalidateParentCaches();
Chet Haasec3aa3612010-06-17 08:50:37 -07007199 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007200 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007201 mPivotY = pivotY;
7202 mMatrixDirty = true;
7203 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007204 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007205 }
7206 }
7207
7208 /**
7209 * The opacity of the view. This is a value from 0 to 1, where 0 means the view is
7210 * completely transparent and 1 means the view is completely opaque.
7211 *
Joe Onorato93162322010-09-16 15:42:01 -04007212 * <p>By default this is 1.0f.
Chet Haasec3aa3612010-06-17 08:50:37 -07007213 * @return The opacity of the view.
7214 */
7215 public float getAlpha() {
7216 return mAlpha;
7217 }
7218
7219 /**
Romain Guy171c5922011-01-06 10:04:23 -08007220 * <p>Sets the opacity of the view. This is a value from 0 to 1, where 0 means the view is
7221 * completely transparent and 1 means the view is completely opaque.</p>
Joe Malin32736f02011-01-19 16:14:20 -08007222 *
Romain Guy171c5922011-01-06 10:04:23 -08007223 * <p>If this view overrides {@link #onSetAlpha(int)} to return true, then this view is
7224 * responsible for applying the opacity itself. Otherwise, calling this method is
7225 * equivalent to calling {@link #setLayerType(int, android.graphics.Paint)} and
Joe Malin32736f02011-01-19 16:14:20 -08007226 * setting a hardware layer.</p>
Chet Haasec3aa3612010-06-17 08:50:37 -07007227 *
7228 * @param alpha The opacity of the view.
Chet Haase73066682010-11-29 15:55:32 -08007229 *
Joe Malin32736f02011-01-19 16:14:20 -08007230 * @see #setLayerType(int, android.graphics.Paint)
7231 *
Chet Haase73066682010-11-29 15:55:32 -08007232 * @attr ref android.R.styleable#View_alpha
Chet Haasec3aa3612010-06-17 08:50:37 -07007233 */
7234 public void setAlpha(float alpha) {
7235 mAlpha = alpha;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007236 invalidateParentCaches();
Chet Haaseed032702010-10-01 14:05:54 -07007237 if (onSetAlpha((int) (alpha * 255))) {
Romain Guya3496a92010-10-12 11:53:24 -07007238 mPrivateFlags |= ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007239 // subclass is handling alpha - don't optimize rendering cache invalidation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007240 invalidate(true);
Chet Haaseed032702010-10-01 14:05:54 -07007241 } else {
Romain Guya3496a92010-10-12 11:53:24 -07007242 mPrivateFlags &= ~ALPHA_SET;
Chet Haaseed032702010-10-01 14:05:54 -07007243 invalidate(false);
7244 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007245 }
7246
7247 /**
Chet Haasea00f3862011-02-22 06:34:40 -08007248 * Faster version of setAlpha() which performs the same steps except there are
7249 * no calls to invalidate(). The caller of this function should perform proper invalidation
7250 * on the parent and this object. The return value indicates whether the subclass handles
7251 * alpha (the return value for onSetAlpha()).
7252 *
7253 * @param alpha The new value for the alpha property
7254 * @return true if the View subclass handles alpha (the return value for onSetAlpha())
7255 */
7256 boolean setAlphaNoInvalidation(float alpha) {
7257 mAlpha = alpha;
7258 boolean subclassHandlesAlpha = onSetAlpha((int) (alpha * 255));
7259 if (subclassHandlesAlpha) {
7260 mPrivateFlags |= ALPHA_SET;
7261 } else {
7262 mPrivateFlags &= ~ALPHA_SET;
7263 }
7264 return subclassHandlesAlpha;
7265 }
7266
7267 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007268 * Top position of this view relative to its parent.
7269 *
7270 * @return The top of this view, in pixels.
7271 */
7272 @ViewDebug.CapturedViewProperty
7273 public final int getTop() {
7274 return mTop;
7275 }
7276
7277 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007278 * Sets the top position of this view relative to its parent. This method is meant to be called
7279 * by the layout system and should not generally be called otherwise, because the property
7280 * may be changed at any time by the layout.
7281 *
7282 * @param top The top of this view, in pixels.
7283 */
7284 public final void setTop(int top) {
7285 if (top != mTop) {
Jeff Brown86671742010-09-30 20:00:15 -07007286 updateMatrix();
7287 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007288 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007289 int minTop;
7290 int yLoc;
7291 if (top < mTop) {
7292 minTop = top;
7293 yLoc = top - mTop;
7294 } else {
7295 minTop = mTop;
7296 yLoc = 0;
7297 }
Chet Haasee9140a72011-02-16 16:23:29 -08007298 invalidate(0, yLoc, mRight - mLeft, mBottom - minTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007299 }
7300 } else {
7301 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007302 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007303 }
7304
Chet Haaseed032702010-10-01 14:05:54 -07007305 int width = mRight - mLeft;
7306 int oldHeight = mBottom - mTop;
7307
Chet Haase21cd1382010-09-01 17:42:29 -07007308 mTop = top;
7309
Chet Haaseed032702010-10-01 14:05:54 -07007310 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7311
Chet Haase21cd1382010-09-01 17:42:29 -07007312 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007313 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7314 // A change in dimension means an auto-centered pivot point changes, too
7315 mMatrixDirty = true;
7316 }
Chet Haase21cd1382010-09-01 17:42:29 -07007317 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007318 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007319 }
Chet Haase55dbb652010-12-21 20:15:08 -08007320 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007321 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007322 }
7323 }
7324
7325 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007326 * Bottom position of this view relative to its parent.
7327 *
7328 * @return The bottom of this view, in pixels.
7329 */
7330 @ViewDebug.CapturedViewProperty
7331 public final int getBottom() {
7332 return mBottom;
7333 }
7334
7335 /**
Michael Jurkadab559a2011-01-04 20:31:51 -08007336 * True if this view has changed since the last time being drawn.
7337 *
7338 * @return The dirty state of this view.
7339 */
7340 public boolean isDirty() {
7341 return (mPrivateFlags & DIRTY_MASK) != 0;
7342 }
7343
7344 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007345 * Sets the bottom position of this view relative to its parent. This method is meant to be
7346 * called by the layout system and should not generally be called otherwise, because the
7347 * property may be changed at any time by the layout.
7348 *
7349 * @param bottom The bottom of this view, in pixels.
7350 */
7351 public final void setBottom(int bottom) {
7352 if (bottom != mBottom) {
Jeff Brown86671742010-09-30 20:00:15 -07007353 updateMatrix();
7354 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007355 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007356 int maxBottom;
7357 if (bottom < mBottom) {
7358 maxBottom = mBottom;
7359 } else {
7360 maxBottom = bottom;
7361 }
Chet Haasee9140a72011-02-16 16:23:29 -08007362 invalidate(0, 0, mRight - mLeft, maxBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007363 }
7364 } else {
7365 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007366 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007367 }
7368
Chet Haaseed032702010-10-01 14:05:54 -07007369 int width = mRight - mLeft;
7370 int oldHeight = mBottom - mTop;
7371
Chet Haase21cd1382010-09-01 17:42:29 -07007372 mBottom = bottom;
7373
Chet Haaseed032702010-10-01 14:05:54 -07007374 onSizeChanged(width, mBottom - mTop, width, oldHeight);
7375
Chet Haase21cd1382010-09-01 17:42:29 -07007376 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007377 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7378 // A change in dimension means an auto-centered pivot point changes, too
7379 mMatrixDirty = true;
7380 }
Chet Haase21cd1382010-09-01 17:42:29 -07007381 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007382 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007383 }
Chet Haase55dbb652010-12-21 20:15:08 -08007384 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007385 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007386 }
7387 }
7388
7389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007390 * Left position of this view relative to its parent.
7391 *
7392 * @return The left edge of this view, in pixels.
7393 */
7394 @ViewDebug.CapturedViewProperty
7395 public final int getLeft() {
7396 return mLeft;
7397 }
7398
7399 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007400 * Sets the left position of this view relative to its parent. This method is meant to be called
7401 * by the layout system and should not generally be called otherwise, because the property
7402 * may be changed at any time by the layout.
7403 *
7404 * @param left The bottom of this view, in pixels.
7405 */
7406 public final void setLeft(int left) {
7407 if (left != mLeft) {
Jeff Brown86671742010-09-30 20:00:15 -07007408 updateMatrix();
7409 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007410 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007411 int minLeft;
7412 int xLoc;
7413 if (left < mLeft) {
7414 minLeft = left;
7415 xLoc = left - mLeft;
7416 } else {
7417 minLeft = mLeft;
7418 xLoc = 0;
7419 }
Chet Haasee9140a72011-02-16 16:23:29 -08007420 invalidate(xLoc, 0, mRight - minLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007421 }
7422 } else {
7423 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007424 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007425 }
7426
Chet Haaseed032702010-10-01 14:05:54 -07007427 int oldWidth = mRight - mLeft;
7428 int height = mBottom - mTop;
7429
Chet Haase21cd1382010-09-01 17:42:29 -07007430 mLeft = left;
7431
Chet Haaseed032702010-10-01 14:05:54 -07007432 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7433
Chet Haase21cd1382010-09-01 17:42:29 -07007434 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007435 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7436 // A change in dimension means an auto-centered pivot point changes, too
7437 mMatrixDirty = true;
7438 }
Chet Haase21cd1382010-09-01 17:42:29 -07007439 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007440 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007441 }
Chet Haase55dbb652010-12-21 20:15:08 -08007442 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007443 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007444 }
7445 }
7446
7447 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007448 * Right position of this view relative to its parent.
7449 *
7450 * @return The right edge of this view, in pixels.
7451 */
7452 @ViewDebug.CapturedViewProperty
7453 public final int getRight() {
7454 return mRight;
7455 }
7456
7457 /**
Chet Haase21cd1382010-09-01 17:42:29 -07007458 * Sets the right position of this view relative to its parent. This method is meant to be called
7459 * by the layout system and should not generally be called otherwise, because the property
7460 * may be changed at any time by the layout.
7461 *
7462 * @param right The bottom of this view, in pixels.
7463 */
7464 public final void setRight(int right) {
7465 if (right != mRight) {
Jeff Brown86671742010-09-30 20:00:15 -07007466 updateMatrix();
7467 if (mMatrixIsIdentity) {
Chet Haasee9140a72011-02-16 16:23:29 -08007468 if (mAttachInfo != null) {
Chet Haase21cd1382010-09-01 17:42:29 -07007469 int maxRight;
7470 if (right < mRight) {
7471 maxRight = mRight;
7472 } else {
7473 maxRight = right;
7474 }
Chet Haasee9140a72011-02-16 16:23:29 -08007475 invalidate(0, 0, maxRight - mLeft, mBottom - mTop);
Chet Haase21cd1382010-09-01 17:42:29 -07007476 }
7477 } else {
7478 // Double-invalidation is necessary to capture view's old and new areas
Romain Guy0fd89bf2011-01-26 15:41:30 -08007479 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007480 }
7481
Chet Haaseed032702010-10-01 14:05:54 -07007482 int oldWidth = mRight - mLeft;
7483 int height = mBottom - mTop;
7484
Chet Haase21cd1382010-09-01 17:42:29 -07007485 mRight = right;
7486
Chet Haaseed032702010-10-01 14:05:54 -07007487 onSizeChanged(mRight - mLeft, height, oldWidth, height);
7488
Chet Haase21cd1382010-09-01 17:42:29 -07007489 if (!mMatrixIsIdentity) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -08007490 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
7491 // A change in dimension means an auto-centered pivot point changes, too
7492 mMatrixDirty = true;
7493 }
Chet Haase21cd1382010-09-01 17:42:29 -07007494 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Romain Guy0fd89bf2011-01-26 15:41:30 -08007495 invalidate(true);
Chet Haase21cd1382010-09-01 17:42:29 -07007496 }
Chet Haase55dbb652010-12-21 20:15:08 -08007497 mBackgroundSizeChanged = true;
Chet Haase678e0ad2011-01-25 09:37:18 -08007498 invalidateParentIfNeeded();
Chet Haase21cd1382010-09-01 17:42:29 -07007499 }
7500 }
7501
7502 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007503 * The visual x position of this view, in pixels. This is equivalent to the
7504 * {@link #setTranslationX(float) translationX} property plus the current
Joe Malin32736f02011-01-19 16:14:20 -08007505 * {@link #getLeft() left} property.
Chet Haasec3aa3612010-06-17 08:50:37 -07007506 *
Chet Haasedf030d22010-07-30 17:22:38 -07007507 * @return The visual x position of this view, in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007508 */
Chet Haasedf030d22010-07-30 17:22:38 -07007509 public float getX() {
7510 return mLeft + mTranslationX;
7511 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007512
Chet Haasedf030d22010-07-30 17:22:38 -07007513 /**
7514 * Sets the visual x position of this view, in pixels. This is equivalent to setting the
7515 * {@link #setTranslationX(float) translationX} property to be the difference between
7516 * the x value passed in and the current {@link #getLeft() left} property.
7517 *
7518 * @param x The visual x position of this view, in pixels.
7519 */
7520 public void setX(float x) {
7521 setTranslationX(x - mLeft);
7522 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007523
Chet Haasedf030d22010-07-30 17:22:38 -07007524 /**
7525 * The visual y position of this view, in pixels. This is equivalent to the
7526 * {@link #setTranslationY(float) translationY} property plus the current
7527 * {@link #getTop() top} property.
7528 *
7529 * @return The visual y position of this view, in pixels.
7530 */
7531 public float getY() {
7532 return mTop + mTranslationY;
7533 }
7534
7535 /**
7536 * Sets the visual y position of this view, in pixels. This is equivalent to setting the
7537 * {@link #setTranslationY(float) translationY} property to be the difference between
7538 * the y value passed in and the current {@link #getTop() top} property.
7539 *
7540 * @param y The visual y position of this view, in pixels.
7541 */
7542 public void setY(float y) {
7543 setTranslationY(y - mTop);
7544 }
7545
7546
7547 /**
7548 * The horizontal location of this view relative to its {@link #getLeft() left} position.
7549 * This position is post-layout, in addition to wherever the object's
7550 * layout placed it.
7551 *
7552 * @return The horizontal position of this view relative to its left position, in pixels.
7553 */
7554 public float getTranslationX() {
7555 return mTranslationX;
7556 }
7557
7558 /**
7559 * Sets the horizontal location of this view relative to its {@link #getLeft() left} position.
7560 * This effectively positions the object post-layout, in addition to wherever the object's
7561 * layout placed it.
7562 *
7563 * @param translationX The horizontal position of this view relative to its left position,
7564 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007565 *
7566 * @attr ref android.R.styleable#View_translationX
Chet Haasedf030d22010-07-30 17:22:38 -07007567 */
7568 public void setTranslationX(float translationX) {
7569 if (mTranslationX != translationX) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007570 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007571 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007572 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007573 mTranslationX = translationX;
7574 mMatrixDirty = true;
7575 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007576 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007577 }
7578 }
7579
7580 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007581 * The horizontal location of this view relative to its {@link #getTop() top} position.
7582 * This position is post-layout, in addition to wherever the object's
7583 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007584 *
Chet Haasedf030d22010-07-30 17:22:38 -07007585 * @return The vertical position of this view relative to its top position,
7586 * in pixels.
Chet Haasec3aa3612010-06-17 08:50:37 -07007587 */
Chet Haasedf030d22010-07-30 17:22:38 -07007588 public float getTranslationY() {
7589 return mTranslationY;
Chet Haasec3aa3612010-06-17 08:50:37 -07007590 }
7591
7592 /**
Chet Haasedf030d22010-07-30 17:22:38 -07007593 * Sets the vertical location of this view relative to its {@link #getTop() top} position.
7594 * This effectively positions the object post-layout, in addition to wherever the object's
7595 * layout placed it.
Chet Haasec3aa3612010-06-17 08:50:37 -07007596 *
Chet Haasedf030d22010-07-30 17:22:38 -07007597 * @param translationY The vertical position of this view relative to its top position,
7598 * in pixels.
Chet Haase73066682010-11-29 15:55:32 -08007599 *
7600 * @attr ref android.R.styleable#View_translationY
Chet Haasec3aa3612010-06-17 08:50:37 -07007601 */
Chet Haasedf030d22010-07-30 17:22:38 -07007602 public void setTranslationY(float translationY) {
7603 if (mTranslationY != translationY) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007604 invalidateParentCaches();
Chet Haasedf030d22010-07-30 17:22:38 -07007605 // Double-invalidation is necessary to capture view's old and new areas
Chet Haaseed032702010-10-01 14:05:54 -07007606 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007607 mTranslationY = translationY;
7608 mMatrixDirty = true;
7609 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007610 invalidate(false);
Chet Haasedf030d22010-07-30 17:22:38 -07007611 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007612 }
7613
7614 /**
Romain Guyda489792011-02-03 01:05:15 -08007615 * @hide
7616 */
Michael Jurkadece29f2011-02-03 01:41:49 -08007617 public void setFastTranslationX(float x) {
7618 mTranslationX = x;
7619 mMatrixDirty = true;
7620 }
7621
7622 /**
7623 * @hide
7624 */
7625 public void setFastTranslationY(float y) {
7626 mTranslationY = y;
7627 mMatrixDirty = true;
7628 }
7629
7630 /**
7631 * @hide
7632 */
Romain Guyda489792011-02-03 01:05:15 -08007633 public void setFastX(float x) {
7634 mTranslationX = x - mLeft;
7635 mMatrixDirty = true;
7636 }
7637
7638 /**
7639 * @hide
7640 */
7641 public void setFastY(float y) {
7642 mTranslationY = y - mTop;
7643 mMatrixDirty = true;
7644 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007645
Romain Guyda489792011-02-03 01:05:15 -08007646 /**
7647 * @hide
7648 */
7649 public void setFastScaleX(float x) {
7650 mScaleX = x;
7651 mMatrixDirty = true;
7652 }
7653
7654 /**
7655 * @hide
7656 */
7657 public void setFastScaleY(float y) {
7658 mScaleY = y;
7659 mMatrixDirty = true;
7660 }
7661
7662 /**
7663 * @hide
7664 */
7665 public void setFastAlpha(float alpha) {
7666 mAlpha = alpha;
7667 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007668
Romain Guyda489792011-02-03 01:05:15 -08007669 /**
7670 * @hide
7671 */
7672 public void setFastRotationY(float y) {
7673 mRotationY = y;
7674 mMatrixDirty = true;
7675 }
Gilles Debunne2ed2eac2011-02-24 16:29:48 -08007676
Romain Guyda489792011-02-03 01:05:15 -08007677 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007678 * Hit rectangle in parent's coordinates
7679 *
7680 * @param outRect The hit rectangle of the view.
7681 */
7682 public void getHitRect(Rect outRect) {
Jeff Brown86671742010-09-30 20:00:15 -07007683 updateMatrix();
7684 if (mMatrixIsIdentity || mAttachInfo == null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007685 outRect.set(mLeft, mTop, mRight, mBottom);
7686 } else {
7687 final RectF tmpRect = mAttachInfo.mTmpTransformRect;
Romain Guy33e72ae2010-07-17 12:40:29 -07007688 tmpRect.set(-mPivotX, -mPivotY, getWidth() - mPivotX, getHeight() - mPivotY);
Jeff Brown86671742010-09-30 20:00:15 -07007689 mMatrix.mapRect(tmpRect);
Romain Guy33e72ae2010-07-17 12:40:29 -07007690 outRect.set((int) tmpRect.left + mLeft, (int) tmpRect.top + mTop,
7691 (int) tmpRect.right + mLeft, (int) tmpRect.bottom + mTop);
Chet Haasec3aa3612010-06-17 08:50:37 -07007692 }
7693 }
7694
7695 /**
Jeff Brown20e987b2010-08-23 12:01:02 -07007696 * Determines whether the given point, in local coordinates is inside the view.
7697 */
7698 /*package*/ final boolean pointInView(float localX, float localY) {
7699 return localX >= 0 && localX < (mRight - mLeft)
7700 && localY >= 0 && localY < (mBottom - mTop);
7701 }
7702
7703 /**
Chet Haasec3aa3612010-06-17 08:50:37 -07007704 * Utility method to determine whether the given point, in local coordinates,
7705 * is inside the view, where the area of the view is expanded by the slop factor.
7706 * This method is called while processing touch-move events to determine if the event
7707 * is still within the view.
7708 */
7709 private boolean pointInView(float localX, float localY, float slop) {
Jeff Brown20e987b2010-08-23 12:01:02 -07007710 return localX >= -slop && localY >= -slop && localX < ((mRight - mLeft) + slop) &&
Romain Guy33e72ae2010-07-17 12:40:29 -07007711 localY < ((mBottom - mTop) + slop);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007712 }
7713
7714 /**
7715 * When a view has focus and the user navigates away from it, the next view is searched for
7716 * starting from the rectangle filled in by this method.
7717 *
Romain Guy5c22a8c2011-05-13 11:48:45 -07007718 * By default, the rectange is the {@link #getDrawingRect(android.graphics.Rect)})
7719 * of the view. However, if your view maintains some idea of internal selection,
7720 * such as a cursor, or a selected row or column, you should override this method and
7721 * fill in a more specific rectangle.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007722 *
7723 * @param r The rectangle to fill in, in this view's coordinates.
7724 */
7725 public void getFocusedRect(Rect r) {
7726 getDrawingRect(r);
7727 }
7728
7729 /**
7730 * If some part of this view is not clipped by any of its parents, then
7731 * return that area in r in global (root) coordinates. To convert r to local
7732 * coordinates, offset it by -globalOffset (e.g. r.offset(-globalOffset.x,
7733 * -globalOffset.y)) If the view is completely clipped or translated out,
7734 * return false.
7735 *
7736 * @param r If true is returned, r holds the global coordinates of the
7737 * visible portion of this view.
7738 * @param globalOffset If true is returned, globalOffset holds the dx,dy
7739 * between this view and its root. globalOffet may be null.
7740 * @return true if r is non-empty (i.e. part of the view is visible at the
7741 * root level.
7742 */
7743 public boolean getGlobalVisibleRect(Rect r, Point globalOffset) {
7744 int width = mRight - mLeft;
7745 int height = mBottom - mTop;
7746 if (width > 0 && height > 0) {
7747 r.set(0, 0, width, height);
7748 if (globalOffset != null) {
7749 globalOffset.set(-mScrollX, -mScrollY);
7750 }
7751 return mParent == null || mParent.getChildVisibleRect(this, r, globalOffset);
7752 }
7753 return false;
7754 }
7755
7756 public final boolean getGlobalVisibleRect(Rect r) {
7757 return getGlobalVisibleRect(r, null);
7758 }
7759
7760 public final boolean getLocalVisibleRect(Rect r) {
7761 Point offset = new Point();
7762 if (getGlobalVisibleRect(r, offset)) {
7763 r.offset(-offset.x, -offset.y); // make r local
7764 return true;
7765 }
7766 return false;
7767 }
7768
7769 /**
7770 * Offset this view's vertical location by the specified number of pixels.
7771 *
7772 * @param offset the number of pixels to offset the view by
7773 */
7774 public void offsetTopAndBottom(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007775 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007776 updateMatrix();
7777 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007778 final ViewParent p = mParent;
7779 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007780 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007781 int minTop;
7782 int maxBottom;
7783 int yLoc;
7784 if (offset < 0) {
7785 minTop = mTop + offset;
7786 maxBottom = mBottom;
7787 yLoc = offset;
7788 } else {
7789 minTop = mTop;
7790 maxBottom = mBottom + offset;
7791 yLoc = 0;
7792 }
7793 r.set(0, yLoc, mRight - mLeft, maxBottom - minTop);
7794 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007795 }
7796 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007797 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007798 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007799
Chet Haasec3aa3612010-06-17 08:50:37 -07007800 mTop += offset;
7801 mBottom += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007802
Chet Haasec3aa3612010-06-17 08:50:37 -07007803 if (!mMatrixIsIdentity) {
7804 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007805 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007806 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007807 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007808 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007809 }
7810
7811 /**
7812 * Offset this view's horizontal location by the specified amount of pixels.
7813 *
7814 * @param offset the numer of pixels to offset the view by
7815 */
7816 public void offsetLeftAndRight(int offset) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007817 if (offset != 0) {
Jeff Brown86671742010-09-30 20:00:15 -07007818 updateMatrix();
7819 if (mMatrixIsIdentity) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007820 final ViewParent p = mParent;
7821 if (p != null && mAttachInfo != null) {
Chet Haasec3aa3612010-06-17 08:50:37 -07007822 final Rect r = mAttachInfo.mTmpInvalRect;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007823 int minLeft;
7824 int maxRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007825 if (offset < 0) {
7826 minLeft = mLeft + offset;
7827 maxRight = mRight;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007828 } else {
7829 minLeft = mLeft;
7830 maxRight = mRight + offset;
Chet Haase8fbf8d22010-07-30 15:01:32 -07007831 }
Chet Haasec3aa3612010-06-17 08:50:37 -07007832 r.set(0, 0, maxRight - minLeft, mBottom - mTop);
Chet Haase8fbf8d22010-07-30 15:01:32 -07007833 p.invalidateChild(this, r);
Chet Haasec3aa3612010-06-17 08:50:37 -07007834 }
7835 } else {
Chet Haaseed032702010-10-01 14:05:54 -07007836 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007837 }
Romain Guy33e72ae2010-07-17 12:40:29 -07007838
Chet Haasec3aa3612010-06-17 08:50:37 -07007839 mLeft += offset;
7840 mRight += offset;
Romain Guy33e72ae2010-07-17 12:40:29 -07007841
Chet Haasec3aa3612010-06-17 08:50:37 -07007842 if (!mMatrixIsIdentity) {
7843 mPrivateFlags |= DRAWN; // force another invalidation with the new orientation
Chet Haaseed032702010-10-01 14:05:54 -07007844 invalidate(false);
Chet Haasec3aa3612010-06-17 08:50:37 -07007845 }
Chet Haase678e0ad2011-01-25 09:37:18 -08007846 invalidateParentIfNeeded();
Chet Haasec3aa3612010-06-17 08:50:37 -07007847 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007848 }
7849
7850 /**
7851 * Get the LayoutParams associated with this view. All views should have
7852 * layout parameters. These supply parameters to the <i>parent</i> of this
7853 * view specifying how it should be arranged. There are many subclasses of
7854 * ViewGroup.LayoutParams, and these correspond to the different subclasses
7855 * of ViewGroup that are responsible for arranging their children.
Romain Guy01c174b2011-02-22 11:51:06 -08007856 *
7857 * This method may return null if this View is not attached to a parent
7858 * ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
7859 * was not invoked successfully. When a View is attached to a parent
7860 * ViewGroup, this method must not return null.
7861 *
7862 * @return The LayoutParams associated with this view, or null if no
7863 * parameters have been set yet
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007864 */
Konstantin Lopyrev91a7f5f2010-08-10 18:54:54 -07007865 @ViewDebug.ExportedProperty(deepExport = true, prefix = "layout_")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007866 public ViewGroup.LayoutParams getLayoutParams() {
7867 return mLayoutParams;
7868 }
7869
7870 /**
7871 * Set the layout parameters associated with this view. These supply
7872 * parameters to the <i>parent</i> of this view specifying how it should be
7873 * arranged. There are many subclasses of ViewGroup.LayoutParams, and these
7874 * correspond to the different subclasses of ViewGroup that are responsible
7875 * for arranging their children.
7876 *
Romain Guy01c174b2011-02-22 11:51:06 -08007877 * @param params The layout parameters for this view, cannot be null
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007878 */
7879 public void setLayoutParams(ViewGroup.LayoutParams params) {
7880 if (params == null) {
Romain Guy01c174b2011-02-22 11:51:06 -08007881 throw new NullPointerException("Layout parameters cannot be null");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007882 }
7883 mLayoutParams = params;
7884 requestLayout();
7885 }
7886
7887 /**
7888 * Set the scrolled position of your view. This will cause a call to
7889 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7890 * invalidated.
7891 * @param x the x position to scroll to
7892 * @param y the y position to scroll to
7893 */
7894 public void scrollTo(int x, int y) {
7895 if (mScrollX != x || mScrollY != y) {
7896 int oldX = mScrollX;
7897 int oldY = mScrollY;
7898 mScrollX = x;
7899 mScrollY = y;
Romain Guy0fd89bf2011-01-26 15:41:30 -08007900 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007901 onScrollChanged(mScrollX, mScrollY, oldX, oldY);
Mike Cleronf116bf82009-09-27 19:14:12 -07007902 if (!awakenScrollBars()) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08007903 invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007904 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007905 }
7906 }
7907
7908 /**
7909 * Move the scrolled position of your view. This will cause a call to
7910 * {@link #onScrollChanged(int, int, int, int)} and the view will be
7911 * invalidated.
7912 * @param x the amount of pixels to scroll by horizontally
7913 * @param y the amount of pixels to scroll by vertically
7914 */
7915 public void scrollBy(int x, int y) {
7916 scrollTo(mScrollX + x, mScrollY + y);
7917 }
7918
7919 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007920 * <p>Trigger the scrollbars to draw. When invoked this method starts an
7921 * animation to fade the scrollbars out after a default delay. If a subclass
7922 * provides animated scrolling, the start delay should equal the duration
7923 * of the scrolling animation.</p>
7924 *
7925 * <p>The animation starts only if at least one of the scrollbars is
7926 * enabled, as specified by {@link #isHorizontalScrollBarEnabled()} and
7927 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7928 * this method returns true, and false otherwise. If the animation is
7929 * started, this method calls {@link #invalidate()}; in that case the
7930 * caller should not call {@link #invalidate()}.</p>
7931 *
7932 * <p>This method should be invoked every time a subclass directly updates
Mike Cleronfe81d382009-09-28 14:22:16 -07007933 * the scroll parameters.</p>
Mike Cleronf116bf82009-09-27 19:14:12 -07007934 *
7935 * <p>This method is automatically invoked by {@link #scrollBy(int, int)}
7936 * and {@link #scrollTo(int, int)}.</p>
7937 *
7938 * @return true if the animation is played, false otherwise
7939 *
7940 * @see #awakenScrollBars(int)
Mike Cleronf116bf82009-09-27 19:14:12 -07007941 * @see #scrollBy(int, int)
7942 * @see #scrollTo(int, int)
7943 * @see #isHorizontalScrollBarEnabled()
7944 * @see #isVerticalScrollBarEnabled()
7945 * @see #setHorizontalScrollBarEnabled(boolean)
7946 * @see #setVerticalScrollBarEnabled(boolean)
7947 */
7948 protected boolean awakenScrollBars() {
7949 return mScrollCache != null &&
Mike Cleron290947b2009-09-29 18:34:32 -07007950 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade, true);
Mike Cleronf116bf82009-09-27 19:14:12 -07007951 }
7952
7953 /**
Adam Powell8568c3a2010-04-19 14:26:11 -07007954 * Trigger the scrollbars to draw.
7955 * This method differs from awakenScrollBars() only in its default duration.
7956 * initialAwakenScrollBars() will show the scroll bars for longer than
7957 * usual to give the user more of a chance to notice them.
7958 *
7959 * @return true if the animation is played, false otherwise.
7960 */
7961 private boolean initialAwakenScrollBars() {
7962 return mScrollCache != null &&
7963 awakenScrollBars(mScrollCache.scrollBarDefaultDelayBeforeFade * 4, true);
7964 }
7965
7966 /**
Mike Cleronf116bf82009-09-27 19:14:12 -07007967 * <p>
7968 * Trigger the scrollbars to draw. When invoked this method starts an
7969 * animation to fade the scrollbars out after a fixed delay. If a subclass
7970 * provides animated scrolling, the start delay should equal the duration of
7971 * the scrolling animation.
7972 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007973 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007974 * <p>
7975 * The animation starts only if at least one of the scrollbars is enabled,
7976 * as specified by {@link #isHorizontalScrollBarEnabled()} and
7977 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
7978 * this method returns true, and false otherwise. If the animation is
7979 * started, this method calls {@link #invalidate()}; in that case the caller
7980 * should not call {@link #invalidate()}.
7981 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007982 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007983 * <p>
7984 * This method should be invoked everytime a subclass directly updates the
Mike Cleronfe81d382009-09-28 14:22:16 -07007985 * scroll parameters.
Mike Cleronf116bf82009-09-27 19:14:12 -07007986 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08007987 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007988 * @param startDelay the delay, in milliseconds, after which the animation
7989 * should start; when the delay is 0, the animation starts
7990 * immediately
7991 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08007992 *
Mike Cleronf116bf82009-09-27 19:14:12 -07007993 * @see #scrollBy(int, int)
7994 * @see #scrollTo(int, int)
7995 * @see #isHorizontalScrollBarEnabled()
7996 * @see #isVerticalScrollBarEnabled()
7997 * @see #setHorizontalScrollBarEnabled(boolean)
7998 * @see #setVerticalScrollBarEnabled(boolean)
7999 */
8000 protected boolean awakenScrollBars(int startDelay) {
Mike Cleron290947b2009-09-29 18:34:32 -07008001 return awakenScrollBars(startDelay, true);
8002 }
Joe Malin32736f02011-01-19 16:14:20 -08008003
Mike Cleron290947b2009-09-29 18:34:32 -07008004 /**
8005 * <p>
8006 * Trigger the scrollbars to draw. When invoked this method starts an
8007 * animation to fade the scrollbars out after a fixed delay. If a subclass
8008 * provides animated scrolling, the start delay should equal the duration of
8009 * the scrolling animation.
8010 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008011 *
Mike Cleron290947b2009-09-29 18:34:32 -07008012 * <p>
8013 * The animation starts only if at least one of the scrollbars is enabled,
8014 * as specified by {@link #isHorizontalScrollBarEnabled()} and
8015 * {@link #isVerticalScrollBarEnabled()}. When the animation is started,
8016 * this method returns true, and false otherwise. If the animation is
Joe Malin32736f02011-01-19 16:14:20 -08008017 * started, this method calls {@link #invalidate()} if the invalidate parameter
Mike Cleron290947b2009-09-29 18:34:32 -07008018 * is set to true; in that case the caller
8019 * should not call {@link #invalidate()}.
8020 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008021 *
Mike Cleron290947b2009-09-29 18:34:32 -07008022 * <p>
8023 * This method should be invoked everytime a subclass directly updates the
8024 * scroll parameters.
8025 * </p>
Joe Malin32736f02011-01-19 16:14:20 -08008026 *
Mike Cleron290947b2009-09-29 18:34:32 -07008027 * @param startDelay the delay, in milliseconds, after which the animation
8028 * should start; when the delay is 0, the animation starts
8029 * immediately
Joe Malin32736f02011-01-19 16:14:20 -08008030 *
Mike Cleron290947b2009-09-29 18:34:32 -07008031 * @param invalidate Wheter this method should call invalidate
Joe Malin32736f02011-01-19 16:14:20 -08008032 *
Mike Cleron290947b2009-09-29 18:34:32 -07008033 * @return true if the animation is played, false otherwise
Joe Malin32736f02011-01-19 16:14:20 -08008034 *
Mike Cleron290947b2009-09-29 18:34:32 -07008035 * @see #scrollBy(int, int)
8036 * @see #scrollTo(int, int)
8037 * @see #isHorizontalScrollBarEnabled()
8038 * @see #isVerticalScrollBarEnabled()
8039 * @see #setHorizontalScrollBarEnabled(boolean)
8040 * @see #setVerticalScrollBarEnabled(boolean)
8041 */
8042 protected boolean awakenScrollBars(int startDelay, boolean invalidate) {
Mike Cleronf116bf82009-09-27 19:14:12 -07008043 final ScrollabilityCache scrollCache = mScrollCache;
Joe Malin32736f02011-01-19 16:14:20 -08008044
Mike Cleronf116bf82009-09-27 19:14:12 -07008045 if (scrollCache == null || !scrollCache.fadeScrollBars) {
8046 return false;
8047 }
8048
8049 if (scrollCache.scrollBar == null) {
8050 scrollCache.scrollBar = new ScrollBarDrawable();
8051 }
8052
8053 if (isHorizontalScrollBarEnabled() || isVerticalScrollBarEnabled()) {
8054
Mike Cleron290947b2009-09-29 18:34:32 -07008055 if (invalidate) {
8056 // Invalidate to show the scrollbars
Romain Guy0fd89bf2011-01-26 15:41:30 -08008057 invalidate(true);
Mike Cleron290947b2009-09-29 18:34:32 -07008058 }
Mike Cleronf116bf82009-09-27 19:14:12 -07008059
8060 if (scrollCache.state == ScrollabilityCache.OFF) {
8061 // FIXME: this is copied from WindowManagerService.
8062 // We should get this value from the system when it
8063 // is possible to do so.
8064 final int KEY_REPEAT_FIRST_DELAY = 750;
8065 startDelay = Math.max(KEY_REPEAT_FIRST_DELAY, startDelay);
8066 }
8067
8068 // Tell mScrollCache when we should start fading. This may
8069 // extend the fade start time if one was already scheduled
Mike Cleron3ecd58c2009-09-28 11:39:02 -07008070 long fadeStartTime = AnimationUtils.currentAnimationTimeMillis() + startDelay;
Mike Cleronf116bf82009-09-27 19:14:12 -07008071 scrollCache.fadeStartTime = fadeStartTime;
8072 scrollCache.state = ScrollabilityCache.ON;
8073
8074 // Schedule our fader to run, unscheduling any old ones first
8075 if (mAttachInfo != null) {
8076 mAttachInfo.mHandler.removeCallbacks(scrollCache);
8077 mAttachInfo.mHandler.postAtTime(scrollCache, fadeStartTime);
8078 }
8079
8080 return true;
8081 }
8082
8083 return false;
8084 }
8085
8086 /**
Chet Haaseaceafe62011-08-26 15:44:33 -07008087 * Do not invalidate views which are not visible and which are not running an animation. They
8088 * will not get drawn and they should not set dirty flags as if they will be drawn
8089 */
8090 private boolean skipInvalidate() {
8091 return (mViewFlags & VISIBILITY_MASK) != VISIBLE && mCurrentAnimation == null &&
8092 (!(mParent instanceof ViewGroup) ||
8093 !((ViewGroup) mParent).isViewTransitioning(this));
8094 }
8095 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008096 * Mark the the area defined by dirty as needing to be drawn. If the view is
Romain Guy5c22a8c2011-05-13 11:48:45 -07008097 * visible, {@link #onDraw(android.graphics.Canvas)} will be called at some point
8098 * in the future. This must be called from a UI thread. To call from a non-UI
8099 * thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008100 *
8101 * WARNING: This method is destructive to dirty.
8102 * @param dirty the rectangle representing the bounds of the dirty region
8103 */
8104 public void invalidate(Rect dirty) {
8105 if (ViewDebug.TRACE_HIERARCHY) {
8106 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8107 }
8108
Chet Haaseaceafe62011-08-26 15:44:33 -07008109 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008110 return;
8111 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008112 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008113 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8114 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008115 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008116 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008117 final ViewParent p = mParent;
8118 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008119 //noinspection PointlessBooleanExpression,ConstantConditions
8120 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8121 if (p != null && ai != null && ai.mHardwareAccelerated) {
8122 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008123 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008124 p.invalidateChild(this, null);
8125 return;
8126 }
Romain Guyaf636eb2010-12-09 17:47:21 -08008127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008128 if (p != null && ai != null) {
8129 final int scrollX = mScrollX;
8130 final int scrollY = mScrollY;
8131 final Rect r = ai.mTmpInvalRect;
8132 r.set(dirty.left - scrollX, dirty.top - scrollY,
8133 dirty.right - scrollX, dirty.bottom - scrollY);
8134 mParent.invalidateChild(this, r);
8135 }
8136 }
8137 }
8138
8139 /**
8140 * Mark the the area defined by the rect (l,t,r,b) as needing to be drawn.
8141 * The coordinates of the dirty rect are relative to the view.
Romain Guy5c22a8c2011-05-13 11:48:45 -07008142 * If the view is visible, {@link #onDraw(android.graphics.Canvas)}
8143 * will be called at some point in the future. This must be called from
8144 * a UI thread. To call from a non-UI thread, call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008145 * @param l the left position of the dirty region
8146 * @param t the top position of the dirty region
8147 * @param r the right position of the dirty region
8148 * @param b the bottom position of the dirty region
8149 */
8150 public void invalidate(int l, int t, int r, int b) {
8151 if (ViewDebug.TRACE_HIERARCHY) {
8152 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8153 }
8154
Chet Haaseaceafe62011-08-26 15:44:33 -07008155 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008156 return;
8157 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008158 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Chet Haasedaf98e92011-01-10 14:10:36 -08008159 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8160 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008161 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Chet Haasedaf98e92011-01-10 14:10:36 -08008162 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008163 final ViewParent p = mParent;
8164 final AttachInfo ai = mAttachInfo;
Romain Guy7d7b5492011-01-24 16:33:45 -08008165 //noinspection PointlessBooleanExpression,ConstantConditions
8166 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8167 if (p != null && ai != null && ai.mHardwareAccelerated) {
8168 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008169 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008170 p.invalidateChild(this, null);
8171 return;
8172 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008173 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008174 if (p != null && ai != null && l < r && t < b) {
8175 final int scrollX = mScrollX;
8176 final int scrollY = mScrollY;
8177 final Rect tmpr = ai.mTmpInvalRect;
8178 tmpr.set(l - scrollX, t - scrollY, r - scrollX, b - scrollY);
8179 p.invalidateChild(this, tmpr);
8180 }
8181 }
8182 }
8183
8184 /**
Romain Guy5c22a8c2011-05-13 11:48:45 -07008185 * Invalidate the whole view. If the view is visible,
8186 * {@link #onDraw(android.graphics.Canvas)} will be called at some point in
8187 * the future. This must be called from a UI thread. To call from a non-UI thread,
8188 * call {@link #postInvalidate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008189 */
8190 public void invalidate() {
Chet Haaseed032702010-10-01 14:05:54 -07008191 invalidate(true);
8192 }
Joe Malin32736f02011-01-19 16:14:20 -08008193
Chet Haaseed032702010-10-01 14:05:54 -07008194 /**
8195 * This is where the invalidate() work actually happens. A full invalidate()
8196 * causes the drawing cache to be invalidated, but this function can be called with
8197 * invalidateCache set to false to skip that invalidation step for cases that do not
8198 * need it (for example, a component that remains at the same dimensions with the same
8199 * content).
8200 *
8201 * @param invalidateCache Whether the drawing cache for this view should be invalidated as
8202 * well. This is usually true for a full invalidate, but may be set to false if the
8203 * View's contents or dimensions have not changed.
8204 */
Romain Guy849d0a32011-02-01 17:20:48 -08008205 void invalidate(boolean invalidateCache) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008206 if (ViewDebug.TRACE_HIERARCHY) {
8207 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE);
8208 }
8209
Chet Haaseaceafe62011-08-26 15:44:33 -07008210 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008211 return;
8212 }
Romain Guy2fe9a8f2010-10-04 20:17:01 -07008213 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
Romain Guyc5d55862011-01-21 19:01:46 -08008214 (invalidateCache && (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) ||
Romain Guy0fd89bf2011-01-26 15:41:30 -08008215 (mPrivateFlags & INVALIDATED) != INVALIDATED || isOpaque() != mLastIsOpaque) {
8216 mLastIsOpaque = isOpaque();
Chet Haaseed032702010-10-01 14:05:54 -07008217 mPrivateFlags &= ~DRAWN;
8218 if (invalidateCache) {
Chet Haasedaf98e92011-01-10 14:10:36 -08008219 mPrivateFlags |= INVALIDATED;
Chet Haaseed032702010-10-01 14:05:54 -07008220 mPrivateFlags &= ~DRAWING_CACHE_VALID;
8221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008222 final AttachInfo ai = mAttachInfo;
Chet Haase70d4ba12010-10-06 09:46:45 -07008223 final ViewParent p = mParent;
Romain Guy7d7b5492011-01-24 16:33:45 -08008224 //noinspection PointlessBooleanExpression,ConstantConditions
8225 if (!HardwareRenderer.RENDER_DIRTY_REGIONS) {
8226 if (p != null && ai != null && ai.mHardwareAccelerated) {
8227 // fast-track for GL-enabled applications; just invalidate the whole hierarchy
Joe Onoratoc6cc0f82011-04-12 11:53:13 -07008228 // with a null dirty rect, which tells the ViewAncestor to redraw everything
Romain Guy7d7b5492011-01-24 16:33:45 -08008229 p.invalidateChild(this, null);
8230 return;
8231 }
Chet Haasef2f7d8f2010-12-03 14:08:14 -08008232 }
Michael Jurkaebefea42010-11-15 16:04:01 -08008233
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008234 if (p != null && ai != null) {
8235 final Rect r = ai.mTmpInvalRect;
8236 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8237 // Don't call invalidate -- we don't want to internally scroll
8238 // our own bounds
8239 p.invalidateChild(this, r);
8240 }
8241 }
8242 }
8243
8244 /**
Romain Guyda489792011-02-03 01:05:15 -08008245 * @hide
8246 */
8247 public void fastInvalidate() {
Chet Haaseaceafe62011-08-26 15:44:33 -07008248 if (skipInvalidate()) {
Chet Haasea68c5cf2011-08-22 14:27:51 -07008249 return;
8250 }
Romain Guyda489792011-02-03 01:05:15 -08008251 if ((mPrivateFlags & (DRAWN | HAS_BOUNDS)) == (DRAWN | HAS_BOUNDS) ||
8252 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID ||
8253 (mPrivateFlags & INVALIDATED) != INVALIDATED) {
8254 if (mParent instanceof View) {
8255 ((View) mParent).mPrivateFlags |= INVALIDATED;
8256 }
8257 mPrivateFlags &= ~DRAWN;
8258 mPrivateFlags |= INVALIDATED;
8259 mPrivateFlags &= ~DRAWING_CACHE_VALID;
Michael Jurkad0872bd2011-03-24 15:44:19 -07008260 if (mParent != null && mAttachInfo != null) {
8261 if (mAttachInfo.mHardwareAccelerated) {
8262 mParent.invalidateChild(this, null);
8263 } else {
8264 final Rect r = mAttachInfo.mTmpInvalRect;
8265 r.set(0, 0, mRight - mLeft, mBottom - mTop);
8266 // Don't call invalidate -- we don't want to internally scroll
8267 // our own bounds
8268 mParent.invalidateChild(this, r);
8269 }
Romain Guyda489792011-02-03 01:05:15 -08008270 }
8271 }
8272 }
8273
8274 /**
Romain Guy0fd89bf2011-01-26 15:41:30 -08008275 * Used to indicate that the parent of this view should clear its caches. This functionality
Chet Haasedaf98e92011-01-10 14:10:36 -08008276 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8277 * which is necessary when various parent-managed properties of the view change, such as
Romain Guy0fd89bf2011-01-26 15:41:30 -08008278 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method only
8279 * clears the parent caches and does not causes an invalidate event.
Chet Haasedaf98e92011-01-10 14:10:36 -08008280 *
8281 * @hide
8282 */
Romain Guy0fd89bf2011-01-26 15:41:30 -08008283 protected void invalidateParentCaches() {
8284 if (mParent instanceof View) {
8285 ((View) mParent).mPrivateFlags |= INVALIDATED;
8286 }
8287 }
Joe Malin32736f02011-01-19 16:14:20 -08008288
Romain Guy0fd89bf2011-01-26 15:41:30 -08008289 /**
8290 * Used to indicate that the parent of this view should be invalidated. This functionality
8291 * is used to force the parent to rebuild its display list (when hardware-accelerated),
8292 * which is necessary when various parent-managed properties of the view change, such as
8293 * alpha, translationX/Y, scrollX/Y, scaleX/Y, and rotation/X/Y. This method will propagate
8294 * an invalidation event to the parent.
8295 *
8296 * @hide
8297 */
8298 protected void invalidateParentIfNeeded() {
Chet Haasedaf98e92011-01-10 14:10:36 -08008299 if (isHardwareAccelerated() && mParent instanceof View) {
Romain Guy0fd89bf2011-01-26 15:41:30 -08008300 ((View) mParent).invalidate(true);
Chet Haasedaf98e92011-01-10 14:10:36 -08008301 }
8302 }
8303
8304 /**
Romain Guy24443ea2009-05-11 11:56:30 -07008305 * Indicates whether this View is opaque. An opaque View guarantees that it will
8306 * draw all the pixels overlapping its bounds using a fully opaque color.
8307 *
8308 * Subclasses of View should override this method whenever possible to indicate
8309 * whether an instance is opaque. Opaque Views are treated in a special way by
8310 * the View hierarchy, possibly allowing it to perform optimizations during
8311 * invalidate/draw passes.
Romain Guy8506ab42009-06-11 17:35:47 -07008312 *
Romain Guy24443ea2009-05-11 11:56:30 -07008313 * @return True if this View is guaranteed to be fully opaque, false otherwise.
Romain Guy24443ea2009-05-11 11:56:30 -07008314 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -07008315 @ViewDebug.ExportedProperty(category = "drawing")
Romain Guy24443ea2009-05-11 11:56:30 -07008316 public boolean isOpaque() {
Chet Haase70d4ba12010-10-06 09:46:45 -07008317 return (mPrivateFlags & OPAQUE_MASK) == OPAQUE_MASK &&
8318 (mAlpha >= 1.0f - ViewConfiguration.ALPHA_THRESHOLD);
Romain Guy8f1344f52009-05-15 16:03:59 -07008319 }
8320
Adam Powell20232d02010-12-08 21:08:53 -08008321 /**
8322 * @hide
8323 */
8324 protected void computeOpaqueFlags() {
Romain Guy8f1344f52009-05-15 16:03:59 -07008325 // Opaque if:
8326 // - Has a background
8327 // - Background is opaque
8328 // - Doesn't have scrollbars or scrollbars are inside overlay
8329
8330 if (mBGDrawable != null && mBGDrawable.getOpacity() == PixelFormat.OPAQUE) {
8331 mPrivateFlags |= OPAQUE_BACKGROUND;
8332 } else {
8333 mPrivateFlags &= ~OPAQUE_BACKGROUND;
8334 }
8335
8336 final int flags = mViewFlags;
8337 if (((flags & SCROLLBARS_VERTICAL) == 0 && (flags & SCROLLBARS_HORIZONTAL) == 0) ||
8338 (flags & SCROLLBARS_STYLE_MASK) == SCROLLBARS_INSIDE_OVERLAY) {
8339 mPrivateFlags |= OPAQUE_SCROLLBARS;
8340 } else {
8341 mPrivateFlags &= ~OPAQUE_SCROLLBARS;
8342 }
8343 }
8344
8345 /**
8346 * @hide
8347 */
8348 protected boolean hasOpaqueScrollbars() {
8349 return (mPrivateFlags & OPAQUE_SCROLLBARS) == OPAQUE_SCROLLBARS;
Romain Guy24443ea2009-05-11 11:56:30 -07008350 }
8351
8352 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008353 * @return A handler associated with the thread running the View. This
8354 * handler can be used to pump events in the UI events queue.
8355 */
8356 public Handler getHandler() {
8357 if (mAttachInfo != null) {
8358 return mAttachInfo.mHandler;
8359 }
8360 return null;
8361 }
8362
8363 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008364 * <p>Causes the Runnable to be added to the message queue.
8365 * The runnable will be run on the user interface thread.</p>
8366 *
8367 * <p>This method can be invoked from outside of the UI thread
8368 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008369 *
8370 * @param action The Runnable that will be executed.
8371 *
8372 * @return Returns true if the Runnable was successfully placed in to the
8373 * message queue. Returns false on failure, usually because the
8374 * looper processing the message queue is exiting.
8375 */
8376 public boolean post(Runnable action) {
8377 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008378 AttachInfo attachInfo = mAttachInfo;
8379 if (attachInfo != null) {
8380 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008381 } else {
8382 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008383 ViewRootImpl.getRunQueue().post(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008384 return true;
8385 }
8386
8387 return handler.post(action);
8388 }
8389
8390 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008391 * <p>Causes the Runnable to be added to the message queue, to be run
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008392 * after the specified amount of time elapses.
Romain Guye63a4f32011-08-11 11:33:31 -07008393 * The runnable will be run on the user interface thread.</p>
8394 *
8395 * <p>This method can be invoked from outside of the UI thread
8396 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008397 *
8398 * @param action The Runnable that will be executed.
8399 * @param delayMillis The delay (in milliseconds) until the Runnable
8400 * will be executed.
8401 *
8402 * @return true if the Runnable was successfully placed in to the
8403 * message queue. Returns false on failure, usually because the
8404 * looper processing the message queue is exiting. Note that a
8405 * result of true does not mean the Runnable will be processed --
8406 * if the looper is quit before the delivery time of the message
8407 * occurs then the message will be dropped.
8408 */
8409 public boolean postDelayed(Runnable action, long delayMillis) {
8410 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008411 AttachInfo attachInfo = mAttachInfo;
8412 if (attachInfo != null) {
8413 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008414 } else {
8415 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008416 ViewRootImpl.getRunQueue().postDelayed(action, delayMillis);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008417 return true;
8418 }
8419
8420 return handler.postDelayed(action, delayMillis);
8421 }
8422
8423 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008424 * <p>Removes the specified Runnable from the message queue.</p>
8425 *
8426 * <p>This method can be invoked from outside of the UI thread
8427 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008428 *
8429 * @param action The Runnable to remove from the message handling queue
8430 *
8431 * @return true if this view could ask the Handler to remove the Runnable,
8432 * false otherwise. When the returned value is true, the Runnable
8433 * may or may not have been actually removed from the message queue
8434 * (for instance, if the Runnable was not in the queue already.)
8435 */
8436 public boolean removeCallbacks(Runnable action) {
8437 Handler handler;
Romain Guyc5a43a22011-03-24 13:28:56 -07008438 AttachInfo attachInfo = mAttachInfo;
8439 if (attachInfo != null) {
8440 handler = attachInfo.mHandler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008441 } else {
8442 // Assume that post will succeed later
Dianne Hackborn6dd005b2011-07-18 13:22:50 -07008443 ViewRootImpl.getRunQueue().removeCallbacks(action);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008444 return true;
8445 }
8446
8447 handler.removeCallbacks(action);
8448 return true;
8449 }
8450
8451 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008452 * <p>Cause an invalidate to happen on a subsequent cycle through the event loop.
8453 * Use this to invalidate the View from a non-UI thread.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008454 *
Romain Guye63a4f32011-08-11 11:33:31 -07008455 * <p>This method can be invoked from outside of the UI thread
8456 * only when this View is attached to a window.</p>
8457 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008458 * @see #invalidate()
8459 */
8460 public void postInvalidate() {
8461 postInvalidateDelayed(0);
8462 }
8463
8464 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008465 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8466 * through the event loop. Use this to invalidate the View from a non-UI thread.</p>
8467 *
8468 * <p>This method can be invoked from outside of the UI thread
8469 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008470 *
8471 * @param left The left coordinate of the rectangle to invalidate.
8472 * @param top The top coordinate of the rectangle to invalidate.
8473 * @param right The right coordinate of the rectangle to invalidate.
8474 * @param bottom The bottom coordinate of the rectangle to invalidate.
8475 *
8476 * @see #invalidate(int, int, int, int)
8477 * @see #invalidate(Rect)
8478 */
8479 public void postInvalidate(int left, int top, int right, int bottom) {
8480 postInvalidateDelayed(0, left, top, right, bottom);
8481 }
8482
8483 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008484 * <p>Cause an invalidate to happen on a subsequent cycle through the event
8485 * loop. Waits for the specified amount of time.</p>
8486 *
8487 * <p>This method can be invoked from outside of the UI thread
8488 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008489 *
8490 * @param delayMilliseconds the duration in milliseconds to delay the
8491 * invalidation by
8492 */
8493 public void postInvalidateDelayed(long delayMilliseconds) {
8494 // We try only with the AttachInfo because there's no point in invalidating
8495 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008496 AttachInfo attachInfo = mAttachInfo;
8497 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008498 Message msg = Message.obtain();
8499 msg.what = AttachInfo.INVALIDATE_MSG;
8500 msg.obj = this;
Romain Guyc5a43a22011-03-24 13:28:56 -07008501 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008502 }
8503 }
8504
8505 /**
Romain Guye63a4f32011-08-11 11:33:31 -07008506 * <p>Cause an invalidate of the specified area to happen on a subsequent cycle
8507 * through the event loop. Waits for the specified amount of time.</p>
8508 *
8509 * <p>This method can be invoked from outside of the UI thread
8510 * only when this View is attached to a window.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008511 *
8512 * @param delayMilliseconds the duration in milliseconds to delay the
8513 * invalidation by
8514 * @param left The left coordinate of the rectangle to invalidate.
8515 * @param top The top coordinate of the rectangle to invalidate.
8516 * @param right The right coordinate of the rectangle to invalidate.
8517 * @param bottom The bottom coordinate of the rectangle to invalidate.
8518 */
8519 public void postInvalidateDelayed(long delayMilliseconds, int left, int top,
8520 int right, int bottom) {
8521
8522 // We try only with the AttachInfo because there's no point in invalidating
8523 // if we are not attached to our window
Romain Guyc5a43a22011-03-24 13:28:56 -07008524 AttachInfo attachInfo = mAttachInfo;
8525 if (attachInfo != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008526 final AttachInfo.InvalidateInfo info = AttachInfo.InvalidateInfo.acquire();
8527 info.target = this;
8528 info.left = left;
8529 info.top = top;
8530 info.right = right;
8531 info.bottom = bottom;
8532
8533 final Message msg = Message.obtain();
8534 msg.what = AttachInfo.INVALIDATE_RECT_MSG;
8535 msg.obj = info;
Romain Guyc5a43a22011-03-24 13:28:56 -07008536 attachInfo.mHandler.sendMessageDelayed(msg, delayMilliseconds);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008537 }
8538 }
8539
8540 /**
Svetoslav Ganova0156172011-06-26 17:55:44 -07008541 * Post a callback to send a {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} event.
8542 * This event is sent at most once every
8543 * {@link ViewConfiguration#getSendRecurringAccessibilityEventsInterval()}.
8544 */
8545 private void postSendViewScrolledAccessibilityEventCallback() {
8546 if (mSendViewScrolledAccessibilityEvent == null) {
8547 mSendViewScrolledAccessibilityEvent = new SendViewScrolledAccessibilityEvent();
8548 }
8549 if (!mSendViewScrolledAccessibilityEvent.mIsPending) {
8550 mSendViewScrolledAccessibilityEvent.mIsPending = true;
8551 postDelayed(mSendViewScrolledAccessibilityEvent,
8552 ViewConfiguration.getSendRecurringAccessibilityEventsInterval());
8553 }
8554 }
8555
8556 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008557 * Called by a parent to request that a child update its values for mScrollX
8558 * and mScrollY if necessary. This will typically be done if the child is
8559 * animating a scroll using a {@link android.widget.Scroller Scroller}
8560 * object.
8561 */
8562 public void computeScroll() {
8563 }
8564
8565 /**
8566 * <p>Indicate whether the horizontal edges are faded when the view is
8567 * scrolled horizontally.</p>
8568 *
8569 * @return true if the horizontal edges should are faded on scroll, false
8570 * otherwise
8571 *
8572 * @see #setHorizontalFadingEdgeEnabled(boolean)
8573 * @attr ref android.R.styleable#View_fadingEdge
8574 */
8575 public boolean isHorizontalFadingEdgeEnabled() {
8576 return (mViewFlags & FADING_EDGE_HORIZONTAL) == FADING_EDGE_HORIZONTAL;
8577 }
8578
8579 /**
8580 * <p>Define whether the horizontal edges should be faded when this view
8581 * is scrolled horizontally.</p>
8582 *
8583 * @param horizontalFadingEdgeEnabled true if the horizontal edges should
8584 * be faded when the view is scrolled
8585 * horizontally
8586 *
8587 * @see #isHorizontalFadingEdgeEnabled()
8588 * @attr ref android.R.styleable#View_fadingEdge
8589 */
8590 public void setHorizontalFadingEdgeEnabled(boolean horizontalFadingEdgeEnabled) {
8591 if (isHorizontalFadingEdgeEnabled() != horizontalFadingEdgeEnabled) {
8592 if (horizontalFadingEdgeEnabled) {
8593 initScrollCache();
8594 }
8595
8596 mViewFlags ^= FADING_EDGE_HORIZONTAL;
8597 }
8598 }
8599
8600 /**
8601 * <p>Indicate whether the vertical edges are faded when the view is
8602 * scrolled horizontally.</p>
8603 *
8604 * @return true if the vertical edges should are faded on scroll, false
8605 * otherwise
8606 *
8607 * @see #setVerticalFadingEdgeEnabled(boolean)
8608 * @attr ref android.R.styleable#View_fadingEdge
8609 */
8610 public boolean isVerticalFadingEdgeEnabled() {
8611 return (mViewFlags & FADING_EDGE_VERTICAL) == FADING_EDGE_VERTICAL;
8612 }
8613
8614 /**
8615 * <p>Define whether the vertical edges should be faded when this view
8616 * is scrolled vertically.</p>
8617 *
8618 * @param verticalFadingEdgeEnabled true if the vertical edges should
8619 * be faded when the view is scrolled
8620 * vertically
8621 *
8622 * @see #isVerticalFadingEdgeEnabled()
8623 * @attr ref android.R.styleable#View_fadingEdge
8624 */
8625 public void setVerticalFadingEdgeEnabled(boolean verticalFadingEdgeEnabled) {
8626 if (isVerticalFadingEdgeEnabled() != verticalFadingEdgeEnabled) {
8627 if (verticalFadingEdgeEnabled) {
8628 initScrollCache();
8629 }
8630
8631 mViewFlags ^= FADING_EDGE_VERTICAL;
8632 }
8633 }
8634
8635 /**
8636 * Returns the strength, or intensity, of the top faded edge. The strength is
8637 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8638 * returns 0.0 or 1.0 but no value in between.
8639 *
8640 * Subclasses should override this method to provide a smoother fade transition
8641 * when scrolling occurs.
8642 *
8643 * @return the intensity of the top fade as a float between 0.0f and 1.0f
8644 */
8645 protected float getTopFadingEdgeStrength() {
8646 return computeVerticalScrollOffset() > 0 ? 1.0f : 0.0f;
8647 }
8648
8649 /**
8650 * Returns the strength, or intensity, of the bottom faded edge. The strength is
8651 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8652 * returns 0.0 or 1.0 but no value in between.
8653 *
8654 * Subclasses should override this method to provide a smoother fade transition
8655 * when scrolling occurs.
8656 *
8657 * @return the intensity of the bottom fade as a float between 0.0f and 1.0f
8658 */
8659 protected float getBottomFadingEdgeStrength() {
8660 return computeVerticalScrollOffset() + computeVerticalScrollExtent() <
8661 computeVerticalScrollRange() ? 1.0f : 0.0f;
8662 }
8663
8664 /**
8665 * Returns the strength, or intensity, of the left faded edge. The strength is
8666 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8667 * returns 0.0 or 1.0 but no value in between.
8668 *
8669 * Subclasses should override this method to provide a smoother fade transition
8670 * when scrolling occurs.
8671 *
8672 * @return the intensity of the left fade as a float between 0.0f and 1.0f
8673 */
8674 protected float getLeftFadingEdgeStrength() {
8675 return computeHorizontalScrollOffset() > 0 ? 1.0f : 0.0f;
8676 }
8677
8678 /**
8679 * Returns the strength, or intensity, of the right faded edge. The strength is
8680 * a value between 0.0 (no fade) and 1.0 (full fade). The default implementation
8681 * returns 0.0 or 1.0 but no value in between.
8682 *
8683 * Subclasses should override this method to provide a smoother fade transition
8684 * when scrolling occurs.
8685 *
8686 * @return the intensity of the right fade as a float between 0.0f and 1.0f
8687 */
8688 protected float getRightFadingEdgeStrength() {
8689 return computeHorizontalScrollOffset() + computeHorizontalScrollExtent() <
8690 computeHorizontalScrollRange() ? 1.0f : 0.0f;
8691 }
8692
8693 /**
8694 * <p>Indicate whether the horizontal scrollbar should be drawn or not. The
8695 * scrollbar is not drawn by default.</p>
8696 *
8697 * @return true if the horizontal scrollbar should be painted, false
8698 * otherwise
8699 *
8700 * @see #setHorizontalScrollBarEnabled(boolean)
8701 */
8702 public boolean isHorizontalScrollBarEnabled() {
8703 return (mViewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
8704 }
8705
8706 /**
8707 * <p>Define whether the horizontal scrollbar should be drawn or not. The
8708 * scrollbar is not drawn by default.</p>
8709 *
8710 * @param horizontalScrollBarEnabled true if the horizontal scrollbar should
8711 * be painted
8712 *
8713 * @see #isHorizontalScrollBarEnabled()
8714 */
8715 public void setHorizontalScrollBarEnabled(boolean horizontalScrollBarEnabled) {
8716 if (isHorizontalScrollBarEnabled() != horizontalScrollBarEnabled) {
8717 mViewFlags ^= SCROLLBARS_HORIZONTAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008718 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008719 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008720 }
8721 }
8722
8723 /**
8724 * <p>Indicate whether the vertical scrollbar should be drawn or not. The
8725 * scrollbar is not drawn by default.</p>
8726 *
8727 * @return true if the vertical scrollbar should be painted, false
8728 * otherwise
8729 *
8730 * @see #setVerticalScrollBarEnabled(boolean)
8731 */
8732 public boolean isVerticalScrollBarEnabled() {
8733 return (mViewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL;
8734 }
8735
8736 /**
8737 * <p>Define whether the vertical scrollbar should be drawn or not. The
8738 * scrollbar is not drawn by default.</p>
8739 *
8740 * @param verticalScrollBarEnabled true if the vertical scrollbar should
8741 * be painted
8742 *
8743 * @see #isVerticalScrollBarEnabled()
8744 */
8745 public void setVerticalScrollBarEnabled(boolean verticalScrollBarEnabled) {
8746 if (isVerticalScrollBarEnabled() != verticalScrollBarEnabled) {
8747 mViewFlags ^= SCROLLBARS_VERTICAL;
Romain Guy8f1344f52009-05-15 16:03:59 -07008748 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008749 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008750 }
8751 }
8752
Adam Powell20232d02010-12-08 21:08:53 -08008753 /**
8754 * @hide
8755 */
8756 protected void recomputePadding() {
8757 setPadding(mUserPaddingLeft, mPaddingTop, mUserPaddingRight, mUserPaddingBottom);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008758 }
Joe Malin32736f02011-01-19 16:14:20 -08008759
Mike Cleronfe81d382009-09-28 14:22:16 -07008760 /**
8761 * Define whether scrollbars will fade when the view is not scrolling.
Joe Malin32736f02011-01-19 16:14:20 -08008762 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008763 * @param fadeScrollbars wheter to enable fading
Joe Malin32736f02011-01-19 16:14:20 -08008764 *
Mike Cleronfe81d382009-09-28 14:22:16 -07008765 */
8766 public void setScrollbarFadingEnabled(boolean fadeScrollbars) {
8767 initScrollCache();
8768 final ScrollabilityCache scrollabilityCache = mScrollCache;
8769 scrollabilityCache.fadeScrollBars = fadeScrollbars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008770 if (fadeScrollbars) {
8771 scrollabilityCache.state = ScrollabilityCache.OFF;
8772 } else {
Mike Cleronfe81d382009-09-28 14:22:16 -07008773 scrollabilityCache.state = ScrollabilityCache.ON;
8774 }
8775 }
Joe Malin32736f02011-01-19 16:14:20 -08008776
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008777 /**
Joe Malin32736f02011-01-19 16:14:20 -08008778 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008779 * Returns true if scrollbars will fade when this view is not scrolling
Joe Malin32736f02011-01-19 16:14:20 -08008780 *
Mike Cleron52f0a642009-09-28 18:21:37 -07008781 * @return true if scrollbar fading is enabled
8782 */
8783 public boolean isScrollbarFadingEnabled() {
Joe Malin32736f02011-01-19 16:14:20 -08008784 return mScrollCache != null && mScrollCache.fadeScrollBars;
Mike Cleron52f0a642009-09-28 18:21:37 -07008785 }
Joe Malin32736f02011-01-19 16:14:20 -08008786
Mike Cleron52f0a642009-09-28 18:21:37 -07008787 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008788 * <p>Specify the style of the scrollbars. The scrollbars can be overlaid or
8789 * inset. When inset, they add to the padding of the view. And the scrollbars
8790 * can be drawn inside the padding area or on the edge of the view. For example,
8791 * if a view has a background drawable and you want to draw the scrollbars
8792 * inside the padding specified by the drawable, you can use
8793 * SCROLLBARS_INSIDE_OVERLAY or SCROLLBARS_INSIDE_INSET. If you want them to
8794 * appear at the edge of the view, ignoring the padding, then you can use
8795 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.</p>
8796 * @param style the style of the scrollbars. Should be one of
8797 * SCROLLBARS_INSIDE_OVERLAY, SCROLLBARS_INSIDE_INSET,
8798 * SCROLLBARS_OUTSIDE_OVERLAY or SCROLLBARS_OUTSIDE_INSET.
8799 * @see #SCROLLBARS_INSIDE_OVERLAY
8800 * @see #SCROLLBARS_INSIDE_INSET
8801 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8802 * @see #SCROLLBARS_OUTSIDE_INSET
8803 */
8804 public void setScrollBarStyle(int style) {
8805 if (style != (mViewFlags & SCROLLBARS_STYLE_MASK)) {
8806 mViewFlags = (mViewFlags & ~SCROLLBARS_STYLE_MASK) | (style & SCROLLBARS_STYLE_MASK);
Romain Guy8f1344f52009-05-15 16:03:59 -07008807 computeOpaqueFlags();
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07008808 resolvePadding();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008809 }
8810 }
8811
8812 /**
8813 * <p>Returns the current scrollbar style.</p>
8814 * @return the current scrollbar style
8815 * @see #SCROLLBARS_INSIDE_OVERLAY
8816 * @see #SCROLLBARS_INSIDE_INSET
8817 * @see #SCROLLBARS_OUTSIDE_OVERLAY
8818 * @see #SCROLLBARS_OUTSIDE_INSET
8819 */
Jeff Sharkey010d7e52011-08-08 21:05:02 -07008820 @ViewDebug.ExportedProperty(mapping = {
8821 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_OVERLAY, to = "INSIDE_OVERLAY"),
8822 @ViewDebug.IntToString(from = SCROLLBARS_INSIDE_INSET, to = "INSIDE_INSET"),
8823 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_OVERLAY, to = "OUTSIDE_OVERLAY"),
8824 @ViewDebug.IntToString(from = SCROLLBARS_OUTSIDE_INSET, to = "OUTSIDE_INSET")
8825 })
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008826 public int getScrollBarStyle() {
8827 return mViewFlags & SCROLLBARS_STYLE_MASK;
8828 }
8829
8830 /**
8831 * <p>Compute the horizontal range that the horizontal scrollbar
8832 * represents.</p>
8833 *
8834 * <p>The range is expressed in arbitrary units that must be the same as the
8835 * units used by {@link #computeHorizontalScrollExtent()} and
8836 * {@link #computeHorizontalScrollOffset()}.</p>
8837 *
8838 * <p>The default range is the drawing width of this view.</p>
8839 *
8840 * @return the total horizontal range represented by the horizontal
8841 * scrollbar
8842 *
8843 * @see #computeHorizontalScrollExtent()
8844 * @see #computeHorizontalScrollOffset()
8845 * @see android.widget.ScrollBarDrawable
8846 */
8847 protected int computeHorizontalScrollRange() {
8848 return getWidth();
8849 }
8850
8851 /**
8852 * <p>Compute the horizontal offset of the horizontal scrollbar's thumb
8853 * within the horizontal range. This value is used to compute the position
8854 * of the thumb within the scrollbar's track.</p>
8855 *
8856 * <p>The range is expressed in arbitrary units that must be the same as the
8857 * units used by {@link #computeHorizontalScrollRange()} and
8858 * {@link #computeHorizontalScrollExtent()}.</p>
8859 *
8860 * <p>The default offset is the scroll offset of this view.</p>
8861 *
8862 * @return the horizontal offset of the scrollbar's thumb
8863 *
8864 * @see #computeHorizontalScrollRange()
8865 * @see #computeHorizontalScrollExtent()
8866 * @see android.widget.ScrollBarDrawable
8867 */
8868 protected int computeHorizontalScrollOffset() {
8869 return mScrollX;
8870 }
8871
8872 /**
8873 * <p>Compute the horizontal extent of the horizontal scrollbar's thumb
8874 * within the horizontal range. This value is used to compute the length
8875 * of the thumb within the scrollbar's track.</p>
8876 *
8877 * <p>The range is expressed in arbitrary units that must be the same as the
8878 * units used by {@link #computeHorizontalScrollRange()} and
8879 * {@link #computeHorizontalScrollOffset()}.</p>
8880 *
8881 * <p>The default extent is the drawing width of this view.</p>
8882 *
8883 * @return the horizontal extent of the scrollbar's thumb
8884 *
8885 * @see #computeHorizontalScrollRange()
8886 * @see #computeHorizontalScrollOffset()
8887 * @see android.widget.ScrollBarDrawable
8888 */
8889 protected int computeHorizontalScrollExtent() {
8890 return getWidth();
8891 }
8892
8893 /**
8894 * <p>Compute the vertical range that the vertical scrollbar represents.</p>
8895 *
8896 * <p>The range is expressed in arbitrary units that must be the same as the
8897 * units used by {@link #computeVerticalScrollExtent()} and
8898 * {@link #computeVerticalScrollOffset()}.</p>
8899 *
8900 * @return the total vertical range represented by the vertical scrollbar
8901 *
8902 * <p>The default range is the drawing height of this view.</p>
8903 *
8904 * @see #computeVerticalScrollExtent()
8905 * @see #computeVerticalScrollOffset()
8906 * @see android.widget.ScrollBarDrawable
8907 */
8908 protected int computeVerticalScrollRange() {
8909 return getHeight();
8910 }
8911
8912 /**
8913 * <p>Compute the vertical offset of the vertical scrollbar's thumb
8914 * within the horizontal range. This value is used to compute the position
8915 * of the thumb within the scrollbar's track.</p>
8916 *
8917 * <p>The range is expressed in arbitrary units that must be the same as the
8918 * units used by {@link #computeVerticalScrollRange()} and
8919 * {@link #computeVerticalScrollExtent()}.</p>
8920 *
8921 * <p>The default offset is the scroll offset of this view.</p>
8922 *
8923 * @return the vertical offset of the scrollbar's thumb
8924 *
8925 * @see #computeVerticalScrollRange()
8926 * @see #computeVerticalScrollExtent()
8927 * @see android.widget.ScrollBarDrawable
8928 */
8929 protected int computeVerticalScrollOffset() {
8930 return mScrollY;
8931 }
8932
8933 /**
8934 * <p>Compute the vertical extent of the horizontal scrollbar's thumb
8935 * within the vertical range. This value is used to compute the length
8936 * of the thumb within the scrollbar's track.</p>
8937 *
8938 * <p>The range is expressed in arbitrary units that must be the same as the
Gilles Debunne52964242010-02-24 11:05:19 -08008939 * units used by {@link #computeVerticalScrollRange()} and
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008940 * {@link #computeVerticalScrollOffset()}.</p>
8941 *
8942 * <p>The default extent is the drawing height of this view.</p>
8943 *
8944 * @return the vertical extent of the scrollbar's thumb
8945 *
8946 * @see #computeVerticalScrollRange()
8947 * @see #computeVerticalScrollOffset()
8948 * @see android.widget.ScrollBarDrawable
8949 */
8950 protected int computeVerticalScrollExtent() {
8951 return getHeight();
8952 }
8953
8954 /**
Adam Powell69159442011-06-13 17:53:06 -07008955 * Check if this view can be scrolled horizontally in a certain direction.
8956 *
8957 * @param direction Negative to check scrolling left, positive to check scrolling right.
8958 * @return true if this view can be scrolled in the specified direction, false otherwise.
8959 */
8960 public boolean canScrollHorizontally(int direction) {
8961 final int offset = computeHorizontalScrollOffset();
8962 final int range = computeHorizontalScrollRange() - computeHorizontalScrollExtent();
8963 if (range == 0) return false;
8964 if (direction < 0) {
8965 return offset > 0;
8966 } else {
8967 return offset < range - 1;
8968 }
8969 }
8970
8971 /**
8972 * Check if this view can be scrolled vertically in a certain direction.
8973 *
8974 * @param direction Negative to check scrolling up, positive to check scrolling down.
8975 * @return true if this view can be scrolled in the specified direction, false otherwise.
8976 */
8977 public boolean canScrollVertically(int direction) {
8978 final int offset = computeVerticalScrollOffset();
8979 final int range = computeVerticalScrollRange() - computeVerticalScrollExtent();
8980 if (range == 0) return false;
8981 if (direction < 0) {
8982 return offset > 0;
8983 } else {
8984 return offset < range - 1;
8985 }
8986 }
8987
8988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008989 * <p>Request the drawing of the horizontal and the vertical scrollbar. The
8990 * scrollbars are painted only if they have been awakened first.</p>
8991 *
8992 * @param canvas the canvas on which to draw the scrollbars
Joe Malin32736f02011-01-19 16:14:20 -08008993 *
Mike Cleronf116bf82009-09-27 19:14:12 -07008994 * @see #awakenScrollBars(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008995 */
Romain Guy1d5b3a62009-11-05 18:44:12 -08008996 protected final void onDrawScrollBars(Canvas canvas) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008997 // scrollbars are drawn only when the animation is running
8998 final ScrollabilityCache cache = mScrollCache;
8999 if (cache != null) {
Joe Malin32736f02011-01-19 16:14:20 -08009000
Mike Cleronf116bf82009-09-27 19:14:12 -07009001 int state = cache.state;
Joe Malin32736f02011-01-19 16:14:20 -08009002
Mike Cleronf116bf82009-09-27 19:14:12 -07009003 if (state == ScrollabilityCache.OFF) {
9004 return;
9005 }
Joe Malin32736f02011-01-19 16:14:20 -08009006
Mike Cleronf116bf82009-09-27 19:14:12 -07009007 boolean invalidate = false;
Joe Malin32736f02011-01-19 16:14:20 -08009008
Mike Cleronf116bf82009-09-27 19:14:12 -07009009 if (state == ScrollabilityCache.FADING) {
9010 // We're fading -- get our fade interpolation
9011 if (cache.interpolatorValues == null) {
9012 cache.interpolatorValues = new float[1];
9013 }
Joe Malin32736f02011-01-19 16:14:20 -08009014
Mike Cleronf116bf82009-09-27 19:14:12 -07009015 float[] values = cache.interpolatorValues;
Joe Malin32736f02011-01-19 16:14:20 -08009016
Mike Cleronf116bf82009-09-27 19:14:12 -07009017 // Stops the animation if we're done
9018 if (cache.scrollBarInterpolator.timeToValues(values) ==
9019 Interpolator.Result.FREEZE_END) {
9020 cache.state = ScrollabilityCache.OFF;
9021 } else {
9022 cache.scrollBar.setAlpha(Math.round(values[0]));
9023 }
Joe Malin32736f02011-01-19 16:14:20 -08009024
9025 // This will make the scroll bars inval themselves after
Mike Cleronf116bf82009-09-27 19:14:12 -07009026 // drawing. We only want this when we're fading so that
9027 // we prevent excessive redraws
9028 invalidate = true;
9029 } else {
9030 // We're just on -- but we may have been fading before so
9031 // reset alpha
9032 cache.scrollBar.setAlpha(255);
9033 }
9034
Joe Malin32736f02011-01-19 16:14:20 -08009035
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009036 final int viewFlags = mViewFlags;
9037
9038 final boolean drawHorizontalScrollBar =
9039 (viewFlags & SCROLLBARS_HORIZONTAL) == SCROLLBARS_HORIZONTAL;
9040 final boolean drawVerticalScrollBar =
9041 (viewFlags & SCROLLBARS_VERTICAL) == SCROLLBARS_VERTICAL
9042 && !isVerticalScrollBarHidden();
9043
9044 if (drawVerticalScrollBar || drawHorizontalScrollBar) {
9045 final int width = mRight - mLeft;
9046 final int height = mBottom - mTop;
9047
9048 final ScrollBarDrawable scrollBar = cache.scrollBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009049
Mike Reede8853fc2009-09-04 14:01:48 -04009050 final int scrollX = mScrollX;
9051 final int scrollY = mScrollY;
9052 final int inside = (viewFlags & SCROLLBARS_OUTSIDE_MASK) == 0 ? ~0 : 0;
9053
Mike Cleronf116bf82009-09-27 19:14:12 -07009054 int left, top, right, bottom;
Joe Malin32736f02011-01-19 16:14:20 -08009055
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009056 if (drawHorizontalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009057 int size = scrollBar.getSize(false);
9058 if (size <= 0) {
9059 size = cache.scrollBarSize;
9060 }
9061
Mike Cleronf116bf82009-09-27 19:14:12 -07009062 scrollBar.setParameters(computeHorizontalScrollRange(),
Mike Reede8853fc2009-09-04 14:01:48 -04009063 computeHorizontalScrollOffset(),
9064 computeHorizontalScrollExtent(), false);
Mike Reede8853fc2009-09-04 14:01:48 -04009065 final int verticalScrollBarGap = drawVerticalScrollBar ?
Mike Cleronf116bf82009-09-27 19:14:12 -07009066 getVerticalScrollbarWidth() : 0;
Joe Malin32736f02011-01-19 16:14:20 -08009067 top = scrollY + height - size - (mUserPaddingBottom & inside);
Mike Cleronf116bf82009-09-27 19:14:12 -07009068 left = scrollX + (mPaddingLeft & inside);
9069 right = scrollX + width - (mUserPaddingRight & inside) - verticalScrollBarGap;
9070 bottom = top + size;
9071 onDrawHorizontalScrollBar(canvas, scrollBar, left, top, right, bottom);
9072 if (invalidate) {
9073 invalidate(left, top, right, bottom);
9074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009075 }
9076
9077 if (drawVerticalScrollBar) {
Adam Powell3ba67742011-01-27 14:16:55 -08009078 int size = scrollBar.getSize(true);
9079 if (size <= 0) {
9080 size = cache.scrollBarSize;
9081 }
9082
Mike Reede8853fc2009-09-04 14:01:48 -04009083 scrollBar.setParameters(computeVerticalScrollRange(),
9084 computeVerticalScrollOffset(),
9085 computeVerticalScrollExtent(), true);
Adam Powell20232d02010-12-08 21:08:53 -08009086 switch (mVerticalScrollbarPosition) {
9087 default:
9088 case SCROLLBAR_POSITION_DEFAULT:
9089 case SCROLLBAR_POSITION_RIGHT:
9090 left = scrollX + width - size - (mUserPaddingRight & inside);
9091 break;
9092 case SCROLLBAR_POSITION_LEFT:
9093 left = scrollX + (mUserPaddingLeft & inside);
9094 break;
9095 }
Mike Cleronf116bf82009-09-27 19:14:12 -07009096 top = scrollY + (mPaddingTop & inside);
9097 right = left + size;
9098 bottom = scrollY + height - (mUserPaddingBottom & inside);
9099 onDrawVerticalScrollBar(canvas, scrollBar, left, top, right, bottom);
9100 if (invalidate) {
9101 invalidate(left, top, right, bottom);
9102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009103 }
9104 }
9105 }
9106 }
Romain Guy8506ab42009-06-11 17:35:47 -07009107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009108 /**
Romain Guy8506ab42009-06-11 17:35:47 -07009109 * 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 -08009110 * FastScroller is visible.
9111 * @return whether to temporarily hide the vertical scrollbar
9112 * @hide
9113 */
9114 protected boolean isVerticalScrollBarHidden() {
9115 return false;
9116 }
9117
9118 /**
9119 * <p>Draw the horizontal scrollbar if
9120 * {@link #isHorizontalScrollBarEnabled()} returns true.</p>
9121 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009122 * @param canvas the canvas on which to draw the scrollbar
9123 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009124 *
9125 * @see #isHorizontalScrollBarEnabled()
9126 * @see #computeHorizontalScrollRange()
9127 * @see #computeHorizontalScrollExtent()
9128 * @see #computeHorizontalScrollOffset()
9129 * @see android.widget.ScrollBarDrawable
Mike Cleronf116bf82009-09-27 19:14:12 -07009130 * @hide
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009131 */
Romain Guy8fb95422010-08-17 18:38:51 -07009132 protected void onDrawHorizontalScrollBar(Canvas canvas, Drawable scrollBar,
9133 int l, int t, int r, int b) {
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009134 scrollBar.setBounds(l, t, r, b);
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009135 scrollBar.draw(canvas);
9136 }
Mike Reede8853fc2009-09-04 14:01:48 -04009137
Mike Reed4d6fe5f2009-09-03 13:29:05 -04009138 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009139 * <p>Draw the vertical scrollbar if {@link #isVerticalScrollBarEnabled()}
9140 * returns true.</p>
9141 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009142 * @param canvas the canvas on which to draw the scrollbar
9143 * @param scrollBar the scrollbar's drawable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009144 *
9145 * @see #isVerticalScrollBarEnabled()
9146 * @see #computeVerticalScrollRange()
9147 * @see #computeVerticalScrollExtent()
9148 * @see #computeVerticalScrollOffset()
9149 * @see android.widget.ScrollBarDrawable
Mike Reede8853fc2009-09-04 14:01:48 -04009150 * @hide
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009151 */
Romain Guy8fb95422010-08-17 18:38:51 -07009152 protected void onDrawVerticalScrollBar(Canvas canvas, Drawable scrollBar,
9153 int l, int t, int r, int b) {
Mike Reede8853fc2009-09-04 14:01:48 -04009154 scrollBar.setBounds(l, t, r, b);
9155 scrollBar.draw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009156 }
9157
9158 /**
9159 * Implement this to do your drawing.
9160 *
9161 * @param canvas the canvas on which the background will be drawn
9162 */
9163 protected void onDraw(Canvas canvas) {
9164 }
9165
9166 /*
9167 * Caller is responsible for calling requestLayout if necessary.
9168 * (This allows addViewInLayout to not request a new layout.)
9169 */
9170 void assignParent(ViewParent parent) {
9171 if (mParent == null) {
9172 mParent = parent;
9173 } else if (parent == null) {
9174 mParent = null;
9175 } else {
9176 throw new RuntimeException("view " + this + " being added, but"
9177 + " it already has a parent");
9178 }
9179 }
9180
9181 /**
9182 * This is called when the view is attached to a window. At this point it
9183 * has a Surface and will start drawing. Note that this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -07009184 * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
9185 * however it may be called any time before the first onDraw -- including
9186 * before or after {@link #onMeasure(int, int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009187 *
9188 * @see #onDetachedFromWindow()
9189 */
9190 protected void onAttachedToWindow() {
9191 if ((mPrivateFlags & REQUEST_TRANSPARENT_REGIONS) != 0) {
9192 mParent.requestTransparentRegion(this);
9193 }
Adam Powell8568c3a2010-04-19 14:26:11 -07009194 if ((mPrivateFlags & AWAKEN_SCROLL_BARS_ON_ATTACH) != 0) {
9195 initialAwakenScrollBars();
9196 mPrivateFlags &= ~AWAKEN_SCROLL_BARS_ON_ATTACH;
9197 }
Chet Haasea9b61ac2010-12-20 07:40:25 -08009198 jumpDrawablesToCurrentState();
Fabrice Di Meglioa6461452011-08-19 15:42:04 -07009199 // Order is important here: LayoutDirection MUST be resolved before Padding
9200 // and TextDirection
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009201 resolveLayoutDirectionIfNeeded();
9202 resolvePadding();
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009203 resolveTextDirection();
Amith Yamasani4503c8d2011-06-17 12:36:14 -07009204 if (isFocused()) {
9205 InputMethodManager imm = InputMethodManager.peekInstance();
9206 imm.focusIn(this);
9207 }
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009208 }
Cibu Johny86666632010-02-22 13:01:02 -08009209
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009210 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009211 * Resolve and cache the layout direction. LTR is set initially. This is implicitly supposing
9212 * that the parent directionality can and will be resolved before its children.
Fabrice Di Meglio4f5aa912011-05-31 15:20:50 -07009213 */
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009214 private void resolveLayoutDirectionIfNeeded() {
9215 // Do not resolve if it is not needed
9216 if ((mPrivateFlags2 & LAYOUT_DIRECTION_RESOLVED) == LAYOUT_DIRECTION_RESOLVED) return;
9217
9218 // Clear any previous layout direction resolution
9219 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED_RTL;
9220
Fabrice Di Meglio4b60c302011-08-17 16:56:55 -07009221 // Reset also TextDirection as a change into LayoutDirection may impact the selected
9222 // TextDirectionHeuristic
9223 resetResolvedTextDirection();
9224
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009225 // Set resolved depending on layout direction
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009226 switch (getLayoutDirection()) {
9227 case LAYOUT_DIRECTION_INHERIT:
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009228 // We cannot do the resolution if there is no parent
9229 if (mParent == null) return;
9230
Cibu Johny86666632010-02-22 13:01:02 -08009231 // If this is root view, no need to look at parent's layout dir.
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009232 if (mParent instanceof ViewGroup) {
9233 ViewGroup viewGroup = ((ViewGroup) mParent);
9234
9235 // Check if the parent view group can resolve
9236 if (! viewGroup.canResolveLayoutDirection()) {
9237 return;
9238 }
9239 if (viewGroup.getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
9240 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
9241 }
Cibu Johny86666632010-02-22 13:01:02 -08009242 }
9243 break;
Fabrice Di Meglioc46f7ff2011-06-06 18:23:10 -07009244 case LAYOUT_DIRECTION_RTL:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009245 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Cibu Johny86666632010-02-22 13:01:02 -08009246 break;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009247 case LAYOUT_DIRECTION_LOCALE:
9248 if(isLayoutDirectionRtl(Locale.getDefault())) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009249 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED_RTL;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009250 }
9251 break;
9252 default:
9253 // Nothing to do, LTR by default
9254 }
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009255
9256 // Set to resolved
9257 mPrivateFlags2 |= LAYOUT_DIRECTION_RESOLVED;
9258 }
9259
Fabrice Di Meglioaff599b2011-07-20 19:05:01 -07009260 /**
9261 * @hide
9262 */
9263 protected void resolvePadding() {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009264 // If the user specified the absolute padding (either with android:padding or
9265 // android:paddingLeft/Top/Right/Bottom), use this padding, otherwise
9266 // use the default padding or the padding from the background drawable
9267 // (stored at this point in mPadding*)
9268 switch (getResolvedLayoutDirection()) {
9269 case LAYOUT_DIRECTION_RTL:
9270 // Start user padding override Right user padding. Otherwise, if Right user
9271 // padding is not defined, use the default Right padding. If Right user padding
9272 // is defined, just use it.
9273 if (mUserPaddingStart >= 0) {
9274 mUserPaddingRight = mUserPaddingStart;
9275 } else if (mUserPaddingRight < 0) {
9276 mUserPaddingRight = mPaddingRight;
9277 }
9278 if (mUserPaddingEnd >= 0) {
9279 mUserPaddingLeft = mUserPaddingEnd;
9280 } else if (mUserPaddingLeft < 0) {
9281 mUserPaddingLeft = mPaddingLeft;
9282 }
9283 break;
9284 case LAYOUT_DIRECTION_LTR:
9285 default:
9286 // Start user padding override Left user padding. Otherwise, if Left user
9287 // padding is not defined, use the default left padding. If Left user padding
9288 // is defined, just use it.
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009289 if (mUserPaddingStart >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009290 mUserPaddingLeft = mUserPaddingStart;
9291 } else if (mUserPaddingLeft < 0) {
9292 mUserPaddingLeft = mPaddingLeft;
9293 }
Fabrice Di Megliof3e1a932011-07-15 17:15:39 -07009294 if (mUserPaddingEnd >= 0) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009295 mUserPaddingRight = mUserPaddingEnd;
9296 } else if (mUserPaddingRight < 0) {
9297 mUserPaddingRight = mPaddingRight;
9298 }
9299 }
9300
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009301 mUserPaddingBottom = (mUserPaddingBottom >= 0) ? mUserPaddingBottom : mPaddingBottom;
9302
9303 recomputePadding();
9304 }
9305
Fabrice Di Megliofe7e40d2011-07-13 12:47:36 -07009306 protected boolean canResolveLayoutDirection() {
9307 switch (getLayoutDirection()) {
9308 case LAYOUT_DIRECTION_INHERIT:
9309 return (mParent != null);
9310 default:
9311 return true;
9312 }
9313 }
9314
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009315 /**
Doug Feltc0ccf0c2011-06-23 16:13:18 -07009316 * Reset the resolved layout direction.
9317 *
9318 * Subclasses need to override this method to clear cached information that depends on the
9319 * resolved layout direction, or to inform child views that inherit their layout direction.
9320 * Overrides must also call the superclass implementation at the start of their implementation.
9321 *
9322 * @hide
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009323 */
Fabrice Di Meglio7f86c802011-07-01 15:09:24 -07009324 protected void resetResolvedLayoutDirection() {
Fabrice Di Meglio80dc53d2011-06-21 18:36:33 -07009325 // Reset the current View resolution
Fabrice Di Megliod8703a92011-06-16 18:54:08 -07009326 mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
Fabrice Di Meglio26e432d2011-06-10 14:19:18 -07009327 }
9328
9329 /**
9330 * Check if a Locale is corresponding to a RTL script.
9331 *
9332 * @param locale Locale to check
9333 * @return true if a Locale is corresponding to a RTL script.
9334 */
Fabrice Di Meglio22268862011-06-27 18:13:18 -07009335 protected static boolean isLayoutDirectionRtl(Locale locale) {
Fabrice Di Meglioa47f45e2011-06-15 14:22:12 -07009336 return (LocaleUtil.TEXT_LAYOUT_DIRECTION_RTL_DO_NOT_USE ==
9337 LocaleUtil.getLayoutDirectionFromLocale(locale));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009338 }
9339
9340 /**
9341 * This is called when the view is detached from a window. At this point it
9342 * no longer has a surface for drawing.
9343 *
9344 * @see #onAttachedToWindow()
9345 */
9346 protected void onDetachedFromWindow() {
Romain Guy8afa5152010-02-26 11:56:30 -08009347 mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT;
Romain Guy6c319ca2011-01-11 14:29:25 -08009348
Romain Guya440b002010-02-24 15:57:54 -08009349 removeUnsetPressCallback();
Maryam Garrett1549dd12009-12-15 16:06:36 -05009350 removeLongPressCallback();
Adam Powell3cb8b632011-01-21 15:34:14 -08009351 removePerformClickCallback();
Svetoslav Ganova0156172011-06-26 17:55:44 -07009352 removeSendViewScrolledAccessibilityEventCallback();
Romain Guy6c319ca2011-01-11 14:29:25 -08009353
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08009354 destroyDrawingCache();
Romain Guy6c319ca2011-01-11 14:29:25 -08009355
Romain Guy6d7475d2011-07-27 16:28:21 -07009356 destroyLayer();
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009357
Chet Haasedaf98e92011-01-10 14:10:36 -08009358 if (mDisplayList != null) {
9359 mDisplayList.invalidate();
9360 }
9361
Romain Guy8dd5b1e2011-01-14 17:28:51 -08009362 if (mAttachInfo != null) {
9363 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_MSG, this);
9364 mAttachInfo.mHandler.removeMessages(AttachInfo.INVALIDATE_RECT_MSG, this);
9365 }
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
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010323 bitmap.setDensity(getResources().getDisplayMetrics().densityDpi);
Joe Malin32736f02011-01-19 16:14:20 -080010324
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010325 Canvas canvas;
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010326 if (attachInfo != null) {
10327 canvas = attachInfo.mCanvas;
10328 if (canvas == null) {
10329 canvas = new Canvas();
10330 }
10331 canvas.setBitmap(bitmap);
10332 // Temporarily clobber the cached Canvas in case one of our children
10333 // is also using a drawing cache. Without this, the children would
10334 // steal the canvas by attaching their own bitmap to it and bad, bad
10335 // things would happen (invisible views, corrupted drawings, etc.)
10336 attachInfo.mCanvas = null;
10337 } else {
10338 // This case should hopefully never or seldom happen
10339 canvas = new Canvas(bitmap);
10340 }
10341
Romain Guy5bcdff42009-05-14 21:27:18 -070010342 if ((backgroundColor & 0xff000000) != 0) {
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010343 bitmap.eraseColor(backgroundColor);
10344 }
10345
10346 computeScroll();
10347 final int restoreCount = canvas.save();
Dianne Hackborn8cae1242009-09-10 14:32:16 -070010348 canvas.scale(scale, scale);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010349 canvas.translate(-mScrollX, -mScrollY);
10350
Romain Guy5bcdff42009-05-14 21:27:18 -070010351 // Temporarily remove the dirty mask
10352 int flags = mPrivateFlags;
10353 mPrivateFlags &= ~DIRTY_MASK;
10354
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010355 // Fast path for layouts with no backgrounds
10356 if ((mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) {
10357 dispatchDraw(canvas);
10358 } else {
10359 draw(canvas);
10360 }
10361
Romain Guy5bcdff42009-05-14 21:27:18 -070010362 mPrivateFlags = flags;
10363
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010364 canvas.restoreToCount(restoreCount);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -070010365 canvas.setBitmap(null);
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010366
10367 if (attachInfo != null) {
10368 // Restore the cached Canvas for our siblings
10369 attachInfo.mCanvas = canvas;
10370 }
Romain Guy8506ab42009-06-11 17:35:47 -070010371
Dianne Hackborn958b9ad2009-03-31 18:00:36 -070010372 return bitmap;
10373 }
10374
10375 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010376 * Indicates whether this View is currently in edit mode. A View is usually
10377 * in edit mode when displayed within a developer tool. For instance, if
10378 * this View is being drawn by a visual user interface builder, this method
10379 * should return true.
10380 *
10381 * Subclasses should check the return value of this method to provide
10382 * different behaviors if their normal behavior might interfere with the
10383 * host environment. For instance: the class spawns a thread in its
10384 * constructor, the drawing code relies on device-specific features, etc.
10385 *
10386 * This method is usually checked in the drawing code of custom widgets.
10387 *
10388 * @return True if this View is in edit mode, false otherwise.
10389 */
10390 public boolean isInEditMode() {
10391 return false;
10392 }
10393
10394 /**
10395 * If the View draws content inside its padding and enables fading edges,
10396 * it needs to support padding offsets. Padding offsets are added to the
10397 * fading edges to extend the length of the fade so that it covers pixels
10398 * drawn inside the padding.
10399 *
10400 * Subclasses of this class should override this method if they need
10401 * to draw content inside the padding.
10402 *
10403 * @return True if padding offset must be applied, false otherwise.
10404 *
10405 * @see #getLeftPaddingOffset()
10406 * @see #getRightPaddingOffset()
10407 * @see #getTopPaddingOffset()
10408 * @see #getBottomPaddingOffset()
10409 *
10410 * @since CURRENT
10411 */
10412 protected boolean isPaddingOffsetRequired() {
10413 return false;
10414 }
10415
10416 /**
10417 * Amount by which to extend the left fading region. Called only when
10418 * {@link #isPaddingOffsetRequired()} returns true.
10419 *
10420 * @return The left padding offset in pixels.
10421 *
10422 * @see #isPaddingOffsetRequired()
10423 *
10424 * @since CURRENT
10425 */
10426 protected int getLeftPaddingOffset() {
10427 return 0;
10428 }
10429
10430 /**
10431 * Amount by which to extend the right fading region. Called only when
10432 * {@link #isPaddingOffsetRequired()} returns true.
10433 *
10434 * @return The right padding offset in pixels.
10435 *
10436 * @see #isPaddingOffsetRequired()
10437 *
10438 * @since CURRENT
10439 */
10440 protected int getRightPaddingOffset() {
10441 return 0;
10442 }
10443
10444 /**
10445 * Amount by which to extend the top fading region. Called only when
10446 * {@link #isPaddingOffsetRequired()} returns true.
10447 *
10448 * @return The top padding offset in pixels.
10449 *
10450 * @see #isPaddingOffsetRequired()
10451 *
10452 * @since CURRENT
10453 */
10454 protected int getTopPaddingOffset() {
10455 return 0;
10456 }
10457
10458 /**
10459 * Amount by which to extend the bottom fading region. Called only when
10460 * {@link #isPaddingOffsetRequired()} returns true.
10461 *
10462 * @return The bottom padding offset in pixels.
10463 *
10464 * @see #isPaddingOffsetRequired()
10465 *
10466 * @since CURRENT
10467 */
10468 protected int getBottomPaddingOffset() {
10469 return 0;
10470 }
10471
10472 /**
Romain Guyf2fc4602011-07-19 15:20:03 -070010473 * @hide
10474 * @param offsetRequired
10475 */
10476 protected int getFadeTop(boolean offsetRequired) {
10477 int top = mPaddingTop;
10478 if (offsetRequired) top += getTopPaddingOffset();
10479 return top;
10480 }
10481
10482 /**
10483 * @hide
10484 * @param offsetRequired
10485 */
10486 protected int getFadeHeight(boolean offsetRequired) {
10487 int padding = mPaddingTop;
10488 if (offsetRequired) padding += getTopPaddingOffset();
10489 return mBottom - mTop - mPaddingBottom - padding;
10490 }
10491
10492 /**
Romain Guy2bffd262010-09-12 17:40:02 -070010493 * <p>Indicates whether this view is attached to an hardware accelerated
10494 * window or not.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010495 *
Romain Guy2bffd262010-09-12 17:40:02 -070010496 * <p>Even if this method returns true, it does not mean that every call
10497 * to {@link #draw(android.graphics.Canvas)} will be made with an hardware
10498 * accelerated {@link android.graphics.Canvas}. For instance, if this view
10499 * is drawn onto an offscren {@link android.graphics.Bitmap} and its
10500 * window is hardware accelerated,
10501 * {@link android.graphics.Canvas#isHardwareAccelerated()} will likely
10502 * return false, and this method will return true.</p>
Joe Malin32736f02011-01-19 16:14:20 -080010503 *
Romain Guy2bffd262010-09-12 17:40:02 -070010504 * @return True if the view is attached to a window and the window is
10505 * hardware accelerated; false in any other case.
10506 */
10507 public boolean isHardwareAccelerated() {
10508 return mAttachInfo != null && mAttachInfo.mHardwareAccelerated;
10509 }
Joe Malin32736f02011-01-19 16:14:20 -080010510
Romain Guy2bffd262010-09-12 17:40:02 -070010511 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010512 * Manually render this view (and all of its children) to the given Canvas.
10513 * The view must have already done a full layout before this function is
Romain Guy5c22a8c2011-05-13 11:48:45 -070010514 * called. When implementing a view, implement
10515 * {@link #onDraw(android.graphics.Canvas)} instead of overriding this method.
10516 * If you do need to override this method, call the superclass version.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010517 *
10518 * @param canvas The Canvas to which the View is rendered.
10519 */
10520 public void draw(Canvas canvas) {
10521 if (ViewDebug.TRACE_HIERARCHY) {
10522 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW);
10523 }
10524
Romain Guy5bcdff42009-05-14 21:27:18 -070010525 final int privateFlags = mPrivateFlags;
10526 final boolean dirtyOpaque = (privateFlags & DIRTY_MASK) == DIRTY_OPAQUE &&
10527 (mAttachInfo == null || !mAttachInfo.mIgnoreDirtyState);
10528 mPrivateFlags = (privateFlags & ~DIRTY_MASK) | DRAWN;
Romain Guy24443ea2009-05-11 11:56:30 -070010529
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010530 /*
10531 * Draw traversal performs several drawing steps which must be executed
10532 * in the appropriate order:
10533 *
10534 * 1. Draw the background
10535 * 2. If necessary, save the canvas' layers to prepare for fading
10536 * 3. Draw view's content
10537 * 4. Draw children
10538 * 5. If necessary, draw the fading edges and restore layers
10539 * 6. Draw decorations (scrollbars for instance)
10540 */
10541
10542 // Step 1, draw the background, if needed
10543 int saveCount;
10544
Romain Guy24443ea2009-05-11 11:56:30 -070010545 if (!dirtyOpaque) {
10546 final Drawable background = mBGDrawable;
10547 if (background != null) {
10548 final int scrollX = mScrollX;
10549 final int scrollY = mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010550
Romain Guy24443ea2009-05-11 11:56:30 -070010551 if (mBackgroundSizeChanged) {
10552 background.setBounds(0, 0, mRight - mLeft, mBottom - mTop);
10553 mBackgroundSizeChanged = false;
10554 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010555
Romain Guy24443ea2009-05-11 11:56:30 -070010556 if ((scrollX | scrollY) == 0) {
10557 background.draw(canvas);
10558 } else {
10559 canvas.translate(scrollX, scrollY);
10560 background.draw(canvas);
10561 canvas.translate(-scrollX, -scrollY);
10562 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010563 }
10564 }
10565
10566 // skip step 2 & 5 if possible (common case)
10567 final int viewFlags = mViewFlags;
10568 boolean horizontalEdges = (viewFlags & FADING_EDGE_HORIZONTAL) != 0;
10569 boolean verticalEdges = (viewFlags & FADING_EDGE_VERTICAL) != 0;
10570 if (!verticalEdges && !horizontalEdges) {
10571 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010572 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010573
10574 // Step 4, draw the children
10575 dispatchDraw(canvas);
10576
10577 // Step 6, draw decorations (scrollbars)
10578 onDrawScrollBars(canvas);
10579
10580 // we're done...
10581 return;
10582 }
10583
10584 /*
10585 * Here we do the full fledged routine...
10586 * (this is an uncommon case where speed matters less,
10587 * this is why we repeat some of the tests that have been
10588 * done above)
10589 */
10590
10591 boolean drawTop = false;
10592 boolean drawBottom = false;
10593 boolean drawLeft = false;
10594 boolean drawRight = false;
10595
10596 float topFadeStrength = 0.0f;
10597 float bottomFadeStrength = 0.0f;
10598 float leftFadeStrength = 0.0f;
10599 float rightFadeStrength = 0.0f;
10600
10601 // Step 2, save the canvas' layers
10602 int paddingLeft = mPaddingLeft;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010603
10604 final boolean offsetRequired = isPaddingOffsetRequired();
10605 if (offsetRequired) {
10606 paddingLeft += getLeftPaddingOffset();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010607 }
10608
10609 int left = mScrollX + paddingLeft;
10610 int right = left + mRight - mLeft - mPaddingRight - paddingLeft;
Romain Guyf2fc4602011-07-19 15:20:03 -070010611 int top = mScrollY + getFadeTop(offsetRequired);
10612 int bottom = top + getFadeHeight(offsetRequired);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010613
10614 if (offsetRequired) {
10615 right += getRightPaddingOffset();
10616 bottom += getBottomPaddingOffset();
10617 }
10618
10619 final ScrollabilityCache scrollabilityCache = mScrollCache;
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010620 final float fadeHeight = scrollabilityCache.fadingEdgeLength;
10621 int length = (int) fadeHeight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010622
10623 // clip the fade length if top and bottom fades overlap
10624 // overlapping fades produce odd-looking artifacts
10625 if (verticalEdges && (top + length > bottom - length)) {
10626 length = (bottom - top) / 2;
10627 }
10628
10629 // also clip horizontal fades if necessary
10630 if (horizontalEdges && (left + length > right - length)) {
10631 length = (right - left) / 2;
10632 }
10633
10634 if (verticalEdges) {
10635 topFadeStrength = Math.max(0.0f, Math.min(1.0f, getTopFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010636 drawTop = topFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010637 bottomFadeStrength = Math.max(0.0f, Math.min(1.0f, getBottomFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010638 drawBottom = bottomFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010639 }
10640
10641 if (horizontalEdges) {
10642 leftFadeStrength = Math.max(0.0f, Math.min(1.0f, getLeftFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010643 drawLeft = leftFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010644 rightFadeStrength = Math.max(0.0f, Math.min(1.0f, getRightFadingEdgeStrength()));
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010645 drawRight = rightFadeStrength * fadeHeight > 1.0f;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010646 }
10647
10648 saveCount = canvas.getSaveCount();
10649
10650 int solidColor = getSolidColor();
Romain Guyf607bdc2010-09-10 19:20:06 -070010651 if (solidColor == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010652 final int flags = Canvas.HAS_ALPHA_LAYER_SAVE_FLAG;
10653
10654 if (drawTop) {
10655 canvas.saveLayer(left, top, right, top + length, null, flags);
10656 }
10657
10658 if (drawBottom) {
10659 canvas.saveLayer(left, bottom - length, right, bottom, null, flags);
10660 }
10661
10662 if (drawLeft) {
10663 canvas.saveLayer(left, top, left + length, bottom, null, flags);
10664 }
10665
10666 if (drawRight) {
10667 canvas.saveLayer(right - length, top, right, bottom, null, flags);
10668 }
10669 } else {
10670 scrollabilityCache.setFadeColor(solidColor);
10671 }
10672
10673 // Step 3, draw the content
Romain Guy24443ea2009-05-11 11:56:30 -070010674 if (!dirtyOpaque) onDraw(canvas);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010675
10676 // Step 4, draw the children
10677 dispatchDraw(canvas);
10678
10679 // Step 5, draw the fade effect and restore layers
10680 final Paint p = scrollabilityCache.paint;
10681 final Matrix matrix = scrollabilityCache.matrix;
10682 final Shader fade = scrollabilityCache.shader;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010683
10684 if (drawTop) {
10685 matrix.setScale(1, fadeHeight * topFadeStrength);
10686 matrix.postTranslate(left, top);
10687 fade.setLocalMatrix(matrix);
10688 canvas.drawRect(left, top, right, top + length, p);
10689 }
10690
10691 if (drawBottom) {
10692 matrix.setScale(1, fadeHeight * bottomFadeStrength);
10693 matrix.postRotate(180);
10694 matrix.postTranslate(left, bottom);
10695 fade.setLocalMatrix(matrix);
10696 canvas.drawRect(left, bottom - length, right, bottom, p);
10697 }
10698
10699 if (drawLeft) {
10700 matrix.setScale(1, fadeHeight * leftFadeStrength);
10701 matrix.postRotate(-90);
10702 matrix.postTranslate(left, top);
10703 fade.setLocalMatrix(matrix);
10704 canvas.drawRect(left, top, left + length, bottom, p);
10705 }
10706
10707 if (drawRight) {
10708 matrix.setScale(1, fadeHeight * rightFadeStrength);
10709 matrix.postRotate(90);
10710 matrix.postTranslate(right, top);
10711 fade.setLocalMatrix(matrix);
10712 canvas.drawRect(right - length, top, right, bottom, p);
10713 }
10714
10715 canvas.restoreToCount(saveCount);
10716
10717 // Step 6, draw decorations (scrollbars)
10718 onDrawScrollBars(canvas);
10719 }
10720
10721 /**
10722 * Override this if your view is known to always be drawn on top of a solid color background,
10723 * and needs to draw fading edges. Returning a non-zero color enables the view system to
10724 * optimize the drawing of the fading edges. If you do return a non-zero color, the alpha
10725 * should be set to 0xFF.
10726 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070010727 * @see #setVerticalFadingEdgeEnabled(boolean)
10728 * @see #setHorizontalFadingEdgeEnabled(boolean)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010729 *
10730 * @return The known solid color background for this view, or 0 if the color may vary
10731 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070010732 @ViewDebug.ExportedProperty(category = "drawing")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010733 public int getSolidColor() {
10734 return 0;
10735 }
10736
10737 /**
10738 * Build a human readable string representation of the specified view flags.
10739 *
10740 * @param flags the view flags to convert to a string
10741 * @return a String representing the supplied flags
10742 */
10743 private static String printFlags(int flags) {
10744 String output = "";
10745 int numFlags = 0;
10746 if ((flags & FOCUSABLE_MASK) == FOCUSABLE) {
10747 output += "TAKES_FOCUS";
10748 numFlags++;
10749 }
10750
10751 switch (flags & VISIBILITY_MASK) {
10752 case INVISIBLE:
10753 if (numFlags > 0) {
10754 output += " ";
10755 }
10756 output += "INVISIBLE";
10757 // USELESS HERE numFlags++;
10758 break;
10759 case GONE:
10760 if (numFlags > 0) {
10761 output += " ";
10762 }
10763 output += "GONE";
10764 // USELESS HERE numFlags++;
10765 break;
10766 default:
10767 break;
10768 }
10769 return output;
10770 }
10771
10772 /**
10773 * Build a human readable string representation of the specified private
10774 * view flags.
10775 *
10776 * @param privateFlags the private view flags to convert to a string
10777 * @return a String representing the supplied flags
10778 */
10779 private static String printPrivateFlags(int privateFlags) {
10780 String output = "";
10781 int numFlags = 0;
10782
10783 if ((privateFlags & WANTS_FOCUS) == WANTS_FOCUS) {
10784 output += "WANTS_FOCUS";
10785 numFlags++;
10786 }
10787
10788 if ((privateFlags & FOCUSED) == FOCUSED) {
10789 if (numFlags > 0) {
10790 output += " ";
10791 }
10792 output += "FOCUSED";
10793 numFlags++;
10794 }
10795
10796 if ((privateFlags & SELECTED) == SELECTED) {
10797 if (numFlags > 0) {
10798 output += " ";
10799 }
10800 output += "SELECTED";
10801 numFlags++;
10802 }
10803
10804 if ((privateFlags & IS_ROOT_NAMESPACE) == IS_ROOT_NAMESPACE) {
10805 if (numFlags > 0) {
10806 output += " ";
10807 }
10808 output += "IS_ROOT_NAMESPACE";
10809 numFlags++;
10810 }
10811
10812 if ((privateFlags & HAS_BOUNDS) == HAS_BOUNDS) {
10813 if (numFlags > 0) {
10814 output += " ";
10815 }
10816 output += "HAS_BOUNDS";
10817 numFlags++;
10818 }
10819
10820 if ((privateFlags & DRAWN) == DRAWN) {
10821 if (numFlags > 0) {
10822 output += " ";
10823 }
10824 output += "DRAWN";
10825 // USELESS HERE numFlags++;
10826 }
10827 return output;
10828 }
10829
10830 /**
10831 * <p>Indicates whether or not this view's layout will be requested during
10832 * the next hierarchy layout pass.</p>
10833 *
10834 * @return true if the layout will be forced during next layout pass
10835 */
10836 public boolean isLayoutRequested() {
10837 return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
10838 }
10839
10840 /**
10841 * Assign a size and position to a view and all of its
10842 * descendants
10843 *
10844 * <p>This is the second phase of the layout mechanism.
10845 * (The first is measuring). In this phase, each parent calls
10846 * layout on all of its children to position them.
10847 * This is typically done using the child measurements
Chet Haase9c087442011-01-12 16:20:16 -080010848 * that were stored in the measure pass().</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010849 *
Chet Haase9c087442011-01-12 16:20:16 -080010850 * <p>Derived classes should not override this method.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010851 * Derived classes with children should override
10852 * onLayout. In that method, they should
Chet Haase9c087442011-01-12 16:20:16 -080010853 * call layout on each of their children.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010854 *
10855 * @param l Left position, relative to parent
10856 * @param t Top position, relative to parent
10857 * @param r Right position, relative to parent
10858 * @param b Bottom position, relative to parent
10859 */
Romain Guy5429e1d2010-09-07 12:38:00 -070010860 @SuppressWarnings({"unchecked"})
Chet Haase9c087442011-01-12 16:20:16 -080010861 public void layout(int l, int t, int r, int b) {
Chet Haase21cd1382010-09-01 17:42:29 -070010862 int oldL = mLeft;
10863 int oldT = mTop;
10864 int oldB = mBottom;
10865 int oldR = mRight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010866 boolean changed = setFrame(l, t, r, b);
10867 if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {
10868 if (ViewDebug.TRACE_HIERARCHY) {
10869 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);
10870 }
10871
10872 onLayout(changed, l, t, r, b);
10873 mPrivateFlags &= ~LAYOUT_REQUIRED;
Chet Haase21cd1382010-09-01 17:42:29 -070010874
10875 if (mOnLayoutChangeListeners != null) {
10876 ArrayList<OnLayoutChangeListener> listenersCopy =
10877 (ArrayList<OnLayoutChangeListener>) mOnLayoutChangeListeners.clone();
10878 int numListeners = listenersCopy.size();
10879 for (int i = 0; i < numListeners; ++i) {
Chet Haase7c608f22010-10-22 17:54:04 -070010880 listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);
Chet Haase21cd1382010-09-01 17:42:29 -070010881 }
10882 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010883 }
10884 mPrivateFlags &= ~FORCE_LAYOUT;
10885 }
10886
10887 /**
10888 * Called from layout when this view should
10889 * assign a size and position to each of its children.
10890 *
10891 * Derived classes with children should override
10892 * this method and call layout on each of
Chet Haase21cd1382010-09-01 17:42:29 -070010893 * their children.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010894 * @param changed This is a new size or position for this view
10895 * @param left Left position, relative to parent
10896 * @param top Top position, relative to parent
10897 * @param right Right position, relative to parent
10898 * @param bottom Bottom position, relative to parent
10899 */
10900 protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
10901 }
10902
10903 /**
10904 * Assign a size and position to this view.
10905 *
10906 * This is called from layout.
10907 *
10908 * @param left Left position, relative to parent
10909 * @param top Top position, relative to parent
10910 * @param right Right position, relative to parent
10911 * @param bottom Bottom position, relative to parent
10912 * @return true if the new size and position are different than the
10913 * previous ones
10914 * {@hide}
10915 */
10916 protected boolean setFrame(int left, int top, int right, int bottom) {
10917 boolean changed = false;
10918
10919 if (DBG) {
Mitsuru Oshima64f59342009-06-21 00:03:11 -070010920 Log.d("View", this + " View.setFrame(" + left + "," + top + ","
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010921 + right + "," + bottom + ")");
10922 }
10923
10924 if (mLeft != left || mRight != right || mTop != top || mBottom != bottom) {
10925 changed = true;
10926
10927 // Remember our drawn bit
10928 int drawn = mPrivateFlags & DRAWN;
10929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010930 int oldWidth = mRight - mLeft;
10931 int oldHeight = mBottom - mTop;
Chet Haase75755e22011-07-18 17:48:25 -070010932 int newWidth = right - left;
10933 int newHeight = bottom - top;
10934 boolean sizeChanged = (newWidth != oldWidth) || (newHeight != oldHeight);
10935
10936 // Invalidate our old position
10937 invalidate(sizeChanged);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010938
10939 mLeft = left;
10940 mTop = top;
10941 mRight = right;
10942 mBottom = bottom;
10943
10944 mPrivateFlags |= HAS_BOUNDS;
10945
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010946
Chet Haase75755e22011-07-18 17:48:25 -070010947 if (sizeChanged) {
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010948 if ((mPrivateFlags & PIVOT_EXPLICITLY_SET) == 0) {
10949 // A change in dimension means an auto-centered pivot point changes, too
10950 mMatrixDirty = true;
10951 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010952 onSizeChanged(newWidth, newHeight, oldWidth, oldHeight);
10953 }
10954
10955 if ((mViewFlags & VISIBILITY_MASK) == VISIBLE) {
10956 // If we are visible, force the DRAWN bit to on so that
10957 // this invalidate will go through (at least to our parent).
10958 // This is because someone may have invalidated this view
Chet Haase6c7ad5d2010-12-28 08:40:00 -080010959 // before this call to setFrame came in, thereby clearing
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010960 // the DRAWN bit.
10961 mPrivateFlags |= DRAWN;
Chet Haase75755e22011-07-18 17:48:25 -070010962 invalidate(sizeChanged);
Chet Haasef28595e2011-01-31 18:52:12 -080010963 // parent display list may need to be recreated based on a change in the bounds
10964 // of any child
10965 invalidateParentCaches();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010966 }
10967
10968 // Reset drawn bit to original value (invalidate turns it off)
10969 mPrivateFlags |= drawn;
10970
10971 mBackgroundSizeChanged = true;
10972 }
10973 return changed;
10974 }
10975
10976 /**
10977 * Finalize inflating a view from XML. This is called as the last phase
10978 * of inflation, after all child views have been added.
10979 *
10980 * <p>Even if the subclass overrides onFinishInflate, they should always be
10981 * sure to call the super method, so that we get called.
10982 */
10983 protected void onFinishInflate() {
10984 }
10985
10986 /**
10987 * Returns the resources associated with this view.
10988 *
10989 * @return Resources object.
10990 */
10991 public Resources getResources() {
10992 return mResources;
10993 }
10994
10995 /**
10996 * Invalidates the specified Drawable.
10997 *
10998 * @param drawable the drawable to invalidate
10999 */
11000 public void invalidateDrawable(Drawable drawable) {
11001 if (verifyDrawable(drawable)) {
11002 final Rect dirty = drawable.getBounds();
11003 final int scrollX = mScrollX;
11004 final int scrollY = mScrollY;
11005
11006 invalidate(dirty.left + scrollX, dirty.top + scrollY,
11007 dirty.right + scrollX, dirty.bottom + scrollY);
11008 }
11009 }
11010
11011 /**
11012 * Schedules an action on a drawable to occur at a specified time.
11013 *
11014 * @param who the recipient of the action
11015 * @param what the action to run on the drawable
11016 * @param when the time at which the action must occur. Uses the
11017 * {@link SystemClock#uptimeMillis} timebase.
11018 */
11019 public void scheduleDrawable(Drawable who, Runnable what, long when) {
11020 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11021 mAttachInfo.mHandler.postAtTime(what, who, when);
11022 }
11023 }
11024
11025 /**
11026 * Cancels a scheduled action on a drawable.
11027 *
11028 * @param who the recipient of the action
11029 * @param what the action to cancel
11030 */
11031 public void unscheduleDrawable(Drawable who, Runnable what) {
11032 if (verifyDrawable(who) && what != null && mAttachInfo != null) {
11033 mAttachInfo.mHandler.removeCallbacks(what, who);
11034 }
11035 }
11036
11037 /**
11038 * Unschedule any events associated with the given Drawable. This can be
11039 * used when selecting a new Drawable into a view, so that the previous
11040 * one is completely unscheduled.
11041 *
11042 * @param who The Drawable to unschedule.
11043 *
11044 * @see #drawableStateChanged
11045 */
11046 public void unscheduleDrawable(Drawable who) {
11047 if (mAttachInfo != null) {
11048 mAttachInfo.mHandler.removeCallbacksAndMessages(who);
11049 }
11050 }
11051
Fabrice Di Meglioc0053222011-06-13 12:16:51 -070011052 /**
11053 * Return the layout direction of a given Drawable.
11054 *
11055 * @param who the Drawable to query
11056 *
11057 * @hide
11058 */
11059 public int getResolvedLayoutDirection(Drawable who) {
11060 return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
Fabrice Di Meglio6a036402011-05-23 14:43:23 -070011061 }
11062
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011063 /**
11064 * If your view subclass is displaying its own Drawable objects, it should
11065 * override this function and return true for any Drawable it is
11066 * displaying. This allows animations for those drawables to be
11067 * scheduled.
11068 *
11069 * <p>Be sure to call through to the super class when overriding this
11070 * function.
11071 *
11072 * @param who The Drawable to verify. Return true if it is one you are
11073 * displaying, else return the result of calling through to the
11074 * super class.
11075 *
11076 * @return boolean If true than the Drawable is being displayed in the
11077 * view; else false and it is not allowed to animate.
11078 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011079 * @see #unscheduleDrawable(android.graphics.drawable.Drawable)
11080 * @see #drawableStateChanged()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011081 */
11082 protected boolean verifyDrawable(Drawable who) {
11083 return who == mBGDrawable;
11084 }
11085
11086 /**
11087 * This function is called whenever the state of the view changes in such
11088 * a way that it impacts the state of drawables being shown.
11089 *
11090 * <p>Be sure to call through to the superclass when overriding this
11091 * function.
11092 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011093 * @see Drawable#setState(int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011094 */
11095 protected void drawableStateChanged() {
11096 Drawable d = mBGDrawable;
11097 if (d != null && d.isStateful()) {
11098 d.setState(getDrawableState());
11099 }
11100 }
11101
11102 /**
11103 * Call this to force a view to update its drawable state. This will cause
11104 * drawableStateChanged to be called on this view. Views that are interested
11105 * in the new state should call getDrawableState.
11106 *
11107 * @see #drawableStateChanged
11108 * @see #getDrawableState
11109 */
11110 public void refreshDrawableState() {
11111 mPrivateFlags |= DRAWABLE_STATE_DIRTY;
11112 drawableStateChanged();
11113
11114 ViewParent parent = mParent;
11115 if (parent != null) {
11116 parent.childDrawableStateChanged(this);
11117 }
11118 }
11119
11120 /**
11121 * Return an array of resource IDs of the drawable states representing the
11122 * current state of the view.
11123 *
11124 * @return The current drawable state
11125 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011126 * @see Drawable#setState(int[])
11127 * @see #drawableStateChanged()
11128 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011129 */
11130 public final int[] getDrawableState() {
11131 if ((mDrawableState != null) && ((mPrivateFlags & DRAWABLE_STATE_DIRTY) == 0)) {
11132 return mDrawableState;
11133 } else {
11134 mDrawableState = onCreateDrawableState(0);
11135 mPrivateFlags &= ~DRAWABLE_STATE_DIRTY;
11136 return mDrawableState;
11137 }
11138 }
11139
11140 /**
11141 * Generate the new {@link android.graphics.drawable.Drawable} state for
11142 * this view. This is called by the view
11143 * system when the cached Drawable state is determined to be invalid. To
11144 * retrieve the current state, you should use {@link #getDrawableState}.
11145 *
11146 * @param extraSpace if non-zero, this is the number of extra entries you
11147 * would like in the returned array in which you can place your own
11148 * states.
11149 *
11150 * @return Returns an array holding the current {@link Drawable} state of
11151 * the view.
11152 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011153 * @see #mergeDrawableStates(int[], int[])
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011154 */
11155 protected int[] onCreateDrawableState(int extraSpace) {
11156 if ((mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE &&
11157 mParent instanceof View) {
11158 return ((View) mParent).onCreateDrawableState(extraSpace);
11159 }
11160
11161 int[] drawableState;
11162
11163 int privateFlags = mPrivateFlags;
11164
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011165 int viewStateIndex = 0;
11166 if ((privateFlags & PRESSED) != 0) viewStateIndex |= VIEW_STATE_PRESSED;
11167 if ((mViewFlags & ENABLED_MASK) == ENABLED) viewStateIndex |= VIEW_STATE_ENABLED;
11168 if (isFocused()) viewStateIndex |= VIEW_STATE_FOCUSED;
Neel Parekhe5378582010-10-06 11:36:50 -070011169 if ((privateFlags & SELECTED) != 0) viewStateIndex |= VIEW_STATE_SELECTED;
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011170 if (hasWindowFocus()) viewStateIndex |= VIEW_STATE_WINDOW_FOCUSED;
11171 if ((privateFlags & ACTIVATED) != 0) viewStateIndex |= VIEW_STATE_ACTIVATED;
Adam Powell5a7e94e2011-04-25 15:30:43 -070011172 if (mAttachInfo != null && mAttachInfo.mHardwareAccelerationRequested &&
11173 HardwareRenderer.isAvailable()) {
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080011174 // This is set if HW acceleration is requested, even if the current
11175 // process doesn't allow it. This is just to allow app preview
11176 // windows to better match their app.
11177 viewStateIndex |= VIEW_STATE_ACCELERATED;
11178 }
PY Laligandc33d8d49e2011-03-14 18:22:53 -070011179 if ((privateFlags & HOVERED) != 0) viewStateIndex |= VIEW_STATE_HOVERED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011180
Christopher Tate3d4bf172011-03-28 16:16:46 -070011181 final int privateFlags2 = mPrivateFlags2;
11182 if ((privateFlags2 & DRAG_CAN_ACCEPT) != 0) viewStateIndex |= VIEW_STATE_DRAG_CAN_ACCEPT;
11183 if ((privateFlags2 & DRAG_HOVERED) != 0) viewStateIndex |= VIEW_STATE_DRAG_HOVERED;
11184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011185 drawableState = VIEW_STATE_SETS[viewStateIndex];
11186
11187 //noinspection ConstantIfStatement
11188 if (false) {
11189 Log.i("View", "drawableStateIndex=" + viewStateIndex);
11190 Log.i("View", toString()
11191 + " pressed=" + ((privateFlags & PRESSED) != 0)
11192 + " en=" + ((mViewFlags & ENABLED_MASK) == ENABLED)
11193 + " fo=" + hasFocus()
11194 + " sl=" + ((privateFlags & SELECTED) != 0)
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011195 + " wf=" + hasWindowFocus()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011196 + ": " + Arrays.toString(drawableState));
11197 }
11198
11199 if (extraSpace == 0) {
11200 return drawableState;
11201 }
11202
11203 final int[] fullState;
11204 if (drawableState != null) {
11205 fullState = new int[drawableState.length + extraSpace];
11206 System.arraycopy(drawableState, 0, fullState, 0, drawableState.length);
11207 } else {
11208 fullState = new int[extraSpace];
11209 }
11210
11211 return fullState;
11212 }
11213
11214 /**
11215 * Merge your own state values in <var>additionalState</var> into the base
11216 * state values <var>baseState</var> that were returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011217 * {@link #onCreateDrawableState(int)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011218 *
11219 * @param baseState The base state values returned by
Romain Guy5c22a8c2011-05-13 11:48:45 -070011220 * {@link #onCreateDrawableState(int)}, which will be modified to also hold your
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011221 * own additional state values.
11222 *
11223 * @param additionalState The additional state values you would like
11224 * added to <var>baseState</var>; this array is not modified.
11225 *
11226 * @return As a convenience, the <var>baseState</var> array you originally
11227 * passed into the function is returned.
11228 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011229 * @see #onCreateDrawableState(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011230 */
11231 protected static int[] mergeDrawableStates(int[] baseState, int[] additionalState) {
11232 final int N = baseState.length;
11233 int i = N - 1;
11234 while (i >= 0 && baseState[i] == 0) {
11235 i--;
11236 }
11237 System.arraycopy(additionalState, 0, baseState, i + 1, additionalState.length);
11238 return baseState;
11239 }
11240
11241 /**
Dianne Hackborn079e2352010-10-18 17:02:43 -070011242 * Call {@link Drawable#jumpToCurrentState() Drawable.jumpToCurrentState()}
11243 * on all Drawable objects associated with this view.
11244 */
11245 public void jumpDrawablesToCurrentState() {
11246 if (mBGDrawable != null) {
11247 mBGDrawable.jumpToCurrentState();
11248 }
11249 }
11250
11251 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011252 * Sets the background color for this view.
11253 * @param color the color of the background
11254 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011255 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011256 public void setBackgroundColor(int color) {
Chet Haase70d4ba12010-10-06 09:46:45 -070011257 if (mBGDrawable instanceof ColorDrawable) {
11258 ((ColorDrawable) mBGDrawable).setColor(color);
11259 } else {
11260 setBackgroundDrawable(new ColorDrawable(color));
11261 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011262 }
11263
11264 /**
11265 * Set the background to a given resource. The resource should refer to
Wink Saville7cd88e12009-08-04 14:45:10 -070011266 * a Drawable object or 0 to remove the background.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011267 * @param resid The identifier of the resource.
11268 * @attr ref android.R.styleable#View_background
11269 */
Bjorn Bringert8354fa62010-02-24 23:54:29 +000011270 @RemotableViewMethod
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011271 public void setBackgroundResource(int resid) {
11272 if (resid != 0 && resid == mBackgroundResource) {
11273 return;
11274 }
11275
11276 Drawable d= null;
11277 if (resid != 0) {
11278 d = mResources.getDrawable(resid);
11279 }
11280 setBackgroundDrawable(d);
11281
11282 mBackgroundResource = resid;
11283 }
11284
11285 /**
11286 * Set the background to a given Drawable, or remove the background. If the
11287 * background has padding, this View's padding is set to the background's
11288 * padding. However, when a background is removed, this View's padding isn't
11289 * touched. If setting the padding is desired, please use
11290 * {@link #setPadding(int, int, int, int)}.
11291 *
11292 * @param d The Drawable to use as the background, or null to remove the
11293 * background
11294 */
11295 public void setBackgroundDrawable(Drawable d) {
Adam Powell4d36ec12011-07-17 16:44:16 -070011296 if (d == mBGDrawable) {
11297 return;
11298 }
11299
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011300 boolean requestLayout = false;
11301
11302 mBackgroundResource = 0;
11303
11304 /*
11305 * Regardless of whether we're setting a new background or not, we want
11306 * to clear the previous drawable.
11307 */
11308 if (mBGDrawable != null) {
11309 mBGDrawable.setCallback(null);
11310 unscheduleDrawable(mBGDrawable);
11311 }
11312
11313 if (d != null) {
11314 Rect padding = sThreadLocal.get();
11315 if (padding == null) {
11316 padding = new Rect();
11317 sThreadLocal.set(padding);
11318 }
11319 if (d.getPadding(padding)) {
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011320 switch (d.getResolvedLayoutDirectionSelf()) {
11321 case LAYOUT_DIRECTION_RTL:
11322 setPadding(padding.right, padding.top, padding.left, padding.bottom);
11323 break;
11324 case LAYOUT_DIRECTION_LTR:
11325 default:
11326 setPadding(padding.left, padding.top, padding.right, padding.bottom);
11327 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011328 }
11329
11330 // Compare the minimum sizes of the old Drawable and the new. If there isn't an old or
11331 // if it has a different minimum size, we should layout again
11332 if (mBGDrawable == null || mBGDrawable.getMinimumHeight() != d.getMinimumHeight() ||
11333 mBGDrawable.getMinimumWidth() != d.getMinimumWidth()) {
11334 requestLayout = true;
11335 }
11336
11337 d.setCallback(this);
11338 if (d.isStateful()) {
11339 d.setState(getDrawableState());
11340 }
11341 d.setVisible(getVisibility() == VISIBLE, false);
11342 mBGDrawable = d;
11343
11344 if ((mPrivateFlags & SKIP_DRAW) != 0) {
11345 mPrivateFlags &= ~SKIP_DRAW;
11346 mPrivateFlags |= ONLY_DRAWS_BACKGROUND;
11347 requestLayout = true;
11348 }
11349 } else {
11350 /* Remove the background */
11351 mBGDrawable = null;
11352
11353 if ((mPrivateFlags & ONLY_DRAWS_BACKGROUND) != 0) {
11354 /*
11355 * This view ONLY drew the background before and we're removing
11356 * the background, so now it won't draw anything
11357 * (hence we SKIP_DRAW)
11358 */
11359 mPrivateFlags &= ~ONLY_DRAWS_BACKGROUND;
11360 mPrivateFlags |= SKIP_DRAW;
11361 }
11362
11363 /*
11364 * When the background is set, we try to apply its padding to this
11365 * View. When the background is removed, we don't touch this View's
11366 * padding. This is noted in the Javadocs. Hence, we don't need to
11367 * requestLayout(), the invalidate() below is sufficient.
11368 */
11369
11370 // The old background's minimum size could have affected this
11371 // View's layout, so let's requestLayout
11372 requestLayout = true;
11373 }
11374
Romain Guy8f1344f52009-05-15 16:03:59 -070011375 computeOpaqueFlags();
11376
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011377 if (requestLayout) {
11378 requestLayout();
11379 }
11380
11381 mBackgroundSizeChanged = true;
Romain Guy0fd89bf2011-01-26 15:41:30 -080011382 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011383 }
11384
11385 /**
11386 * Gets the background drawable
11387 * @return The drawable used as the background for this view, if any.
11388 */
11389 public Drawable getBackground() {
11390 return mBGDrawable;
11391 }
11392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011393 /**
11394 * Sets the padding. The view may add on the space required to display
11395 * the scrollbars, depending on the style and visibility of the scrollbars.
11396 * So the values returned from {@link #getPaddingLeft}, {@link #getPaddingTop},
11397 * {@link #getPaddingRight} and {@link #getPaddingBottom} may be different
11398 * from the values set in this call.
11399 *
11400 * @attr ref android.R.styleable#View_padding
11401 * @attr ref android.R.styleable#View_paddingBottom
11402 * @attr ref android.R.styleable#View_paddingLeft
11403 * @attr ref android.R.styleable#View_paddingRight
11404 * @attr ref android.R.styleable#View_paddingTop
11405 * @param left the left padding in pixels
11406 * @param top the top padding in pixels
11407 * @param right the right padding in pixels
11408 * @param bottom the bottom padding in pixels
11409 */
11410 public void setPadding(int left, int top, int right, int bottom) {
11411 boolean changed = false;
11412
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011413 mUserPaddingRelative = false;
11414
Adam Powell20232d02010-12-08 21:08:53 -080011415 mUserPaddingLeft = left;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011416 mUserPaddingRight = right;
11417 mUserPaddingBottom = bottom;
11418
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011419 final int viewFlags = mViewFlags;
Romain Guy8506ab42009-06-11 17:35:47 -070011420
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011421 // Common case is there are no scroll bars.
11422 if ((viewFlags & (SCROLLBARS_VERTICAL|SCROLLBARS_HORIZONTAL)) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011423 if ((viewFlags & SCROLLBARS_VERTICAL) != 0) {
Adam Powell20232d02010-12-08 21:08:53 -080011424 final int offset = (viewFlags & SCROLLBARS_INSET_MASK) == 0
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011425 ? 0 : getVerticalScrollbarWidth();
Adam Powell20232d02010-12-08 21:08:53 -080011426 switch (mVerticalScrollbarPosition) {
11427 case SCROLLBAR_POSITION_DEFAULT:
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011428 if (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
11429 left += offset;
11430 } else {
11431 right += offset;
11432 }
11433 break;
Adam Powell20232d02010-12-08 21:08:53 -080011434 case SCROLLBAR_POSITION_RIGHT:
11435 right += offset;
11436 break;
11437 case SCROLLBAR_POSITION_LEFT:
11438 left += offset;
11439 break;
11440 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011441 }
Adam Powell20232d02010-12-08 21:08:53 -080011442 if ((viewFlags & SCROLLBARS_HORIZONTAL) != 0) {
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011443 bottom += (viewFlags & SCROLLBARS_INSET_MASK) == 0
11444 ? 0 : getHorizontalScrollbarHeight();
11445 }
11446 }
Romain Guy8506ab42009-06-11 17:35:47 -070011447
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011448 if (mPaddingLeft != left) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011449 changed = true;
11450 mPaddingLeft = left;
11451 }
11452 if (mPaddingTop != top) {
11453 changed = true;
11454 mPaddingTop = top;
11455 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011456 if (mPaddingRight != right) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011457 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011458 mPaddingRight = right;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011459 }
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011460 if (mPaddingBottom != bottom) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011461 changed = true;
The Android Open Source Projectb2a3dd82009-03-09 11:52:12 -070011462 mPaddingBottom = bottom;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011463 }
11464
11465 if (changed) {
11466 requestLayout();
11467 }
11468 }
11469
11470 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011471 * Sets the relative padding. The view may add on the space required to display
11472 * the scrollbars, depending on the style and visibility of the scrollbars.
11473 * So the values returned from {@link #getPaddingStart}, {@link #getPaddingTop},
11474 * {@link #getPaddingEnd} and {@link #getPaddingBottom} may be different
11475 * from the values set in this call.
11476 *
11477 * @attr ref android.R.styleable#View_padding
11478 * @attr ref android.R.styleable#View_paddingBottom
11479 * @attr ref android.R.styleable#View_paddingStart
11480 * @attr ref android.R.styleable#View_paddingEnd
11481 * @attr ref android.R.styleable#View_paddingTop
11482 * @param start the start padding in pixels
11483 * @param top the top padding in pixels
11484 * @param end the end padding in pixels
11485 * @param bottom the bottom padding in pixels
11486 *
11487 * @hide
11488 */
11489 public void setPaddingRelative(int start, int top, int end, int bottom) {
11490 mUserPaddingRelative = true;
Fabrice Di Megliof9e36502011-06-21 18:41:48 -070011491
11492 mUserPaddingStart = start;
11493 mUserPaddingEnd = end;
11494
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011495 switch(getResolvedLayoutDirection()) {
11496 case LAYOUT_DIRECTION_RTL:
11497 setPadding(end, top, start, bottom);
11498 break;
11499 case LAYOUT_DIRECTION_LTR:
11500 default:
11501 setPadding(start, top, end, bottom);
11502 }
11503 }
11504
11505 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011506 * Returns the top padding of this view.
11507 *
11508 * @return the top padding in pixels
11509 */
11510 public int getPaddingTop() {
11511 return mPaddingTop;
11512 }
11513
11514 /**
11515 * Returns the bottom padding of this view. If there are inset and enabled
11516 * scrollbars, this value may include the space required to display the
11517 * scrollbars as well.
11518 *
11519 * @return the bottom padding in pixels
11520 */
11521 public int getPaddingBottom() {
11522 return mPaddingBottom;
11523 }
11524
11525 /**
11526 * Returns the left padding of this view. If there are inset and enabled
11527 * scrollbars, this value may include the space required to display the
11528 * scrollbars as well.
11529 *
11530 * @return the left padding in pixels
11531 */
11532 public int getPaddingLeft() {
11533 return mPaddingLeft;
11534 }
11535
11536 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011537 * Returns the start padding of this view. If there are inset and enabled
11538 * scrollbars, this value may include the space required to display the
11539 * scrollbars as well.
11540 *
11541 * @return the start padding in pixels
11542 *
11543 * @hide
11544 */
11545 public int getPaddingStart() {
11546 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11547 mPaddingRight : mPaddingLeft;
11548 }
11549
11550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011551 * Returns the right padding of this view. If there are inset and enabled
11552 * scrollbars, this value may include the space required to display the
11553 * scrollbars as well.
11554 *
11555 * @return the right padding in pixels
11556 */
11557 public int getPaddingRight() {
11558 return mPaddingRight;
11559 }
11560
11561 /**
Fabrice Di Megliod8703a92011-06-16 18:54:08 -070011562 * Returns the end padding of this view. If there are inset and enabled
11563 * scrollbars, this value may include the space required to display the
11564 * scrollbars as well.
11565 *
11566 * @return the end padding in pixels
11567 *
11568 * @hide
11569 */
11570 public int getPaddingEnd() {
11571 return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
11572 mPaddingLeft : mPaddingRight;
11573 }
11574
11575 /**
11576 * Return if the padding as been set thru relative values
11577 * {@link #setPaddingRelative(int, int, int, int)} or thru
11578 * @attr ref android.R.styleable#View_paddingStart or
11579 * @attr ref android.R.styleable#View_paddingEnd
11580 *
11581 * @return true if the padding is relative or false if it is not.
11582 *
11583 * @hide
11584 */
11585 public boolean isPaddingRelative() {
11586 return mUserPaddingRelative;
11587 }
11588
11589 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011590 * Changes the selection state of this view. A view can be selected or not.
11591 * Note that selection is not the same as focus. Views are typically
11592 * selected in the context of an AdapterView like ListView or GridView;
11593 * the selected view is the view that is highlighted.
11594 *
11595 * @param selected true if the view must be selected, false otherwise
11596 */
11597 public void setSelected(boolean selected) {
11598 if (((mPrivateFlags & SELECTED) != 0) != selected) {
11599 mPrivateFlags = (mPrivateFlags & ~SELECTED) | (selected ? SELECTED : 0);
Romain Guya2431d02009-04-30 16:30:00 -070011600 if (!selected) resetPressedState();
Romain Guy0fd89bf2011-01-26 15:41:30 -080011601 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011602 refreshDrawableState();
11603 dispatchSetSelected(selected);
11604 }
11605 }
11606
11607 /**
11608 * Dispatch setSelected to all of this View's children.
11609 *
11610 * @see #setSelected(boolean)
11611 *
11612 * @param selected The new selected state
11613 */
11614 protected void dispatchSetSelected(boolean selected) {
11615 }
11616
11617 /**
11618 * Indicates the selection state of this view.
11619 *
11620 * @return true if the view is selected, false otherwise
11621 */
11622 @ViewDebug.ExportedProperty
11623 public boolean isSelected() {
11624 return (mPrivateFlags & SELECTED) != 0;
11625 }
11626
11627 /**
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011628 * Changes the activated state of this view. A view can be activated or not.
11629 * Note that activation is not the same as selection. Selection is
11630 * a transient property, representing the view (hierarchy) the user is
11631 * currently interacting with. Activation is a longer-term state that the
11632 * user can move views in and out of. For example, in a list view with
11633 * single or multiple selection enabled, the views in the current selection
11634 * set are activated. (Um, yeah, we are deeply sorry about the terminology
11635 * here.) The activated state is propagated down to children of the view it
11636 * is set on.
11637 *
11638 * @param activated true if the view must be activated, false otherwise
11639 */
11640 public void setActivated(boolean activated) {
11641 if (((mPrivateFlags & ACTIVATED) != 0) != activated) {
11642 mPrivateFlags = (mPrivateFlags & ~ACTIVATED) | (activated ? ACTIVATED : 0);
Romain Guy0fd89bf2011-01-26 15:41:30 -080011643 invalidate(true);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011644 refreshDrawableState();
Dianne Hackbornc6669ca2010-09-16 01:33:24 -070011645 dispatchSetActivated(activated);
Dianne Hackbornd0fa3712010-09-14 18:57:14 -070011646 }
11647 }
11648
11649 /**
11650 * Dispatch setActivated to all of this View's children.
11651 *
11652 * @see #setActivated(boolean)
11653 *
11654 * @param activated The new activated state
11655 */
11656 protected void dispatchSetActivated(boolean activated) {
11657 }
11658
11659 /**
11660 * Indicates the activation state of this view.
11661 *
11662 * @return true if the view is activated, false otherwise
11663 */
11664 @ViewDebug.ExportedProperty
11665 public boolean isActivated() {
11666 return (mPrivateFlags & ACTIVATED) != 0;
11667 }
11668
11669 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011670 * Returns the ViewTreeObserver for this view's hierarchy. The view tree
11671 * observer can be used to get notifications when global events, like
11672 * layout, happen.
11673 *
11674 * The returned ViewTreeObserver observer is not guaranteed to remain
11675 * valid for the lifetime of this View. If the caller of this method keeps
11676 * a long-lived reference to ViewTreeObserver, it should always check for
11677 * the return value of {@link ViewTreeObserver#isAlive()}.
11678 *
11679 * @return The ViewTreeObserver for this view's hierarchy.
11680 */
11681 public ViewTreeObserver getViewTreeObserver() {
11682 if (mAttachInfo != null) {
11683 return mAttachInfo.mTreeObserver;
11684 }
11685 if (mFloatingTreeObserver == null) {
11686 mFloatingTreeObserver = new ViewTreeObserver();
11687 }
11688 return mFloatingTreeObserver;
11689 }
11690
11691 /**
11692 * <p>Finds the topmost view in the current view hierarchy.</p>
11693 *
11694 * @return the topmost view containing this view
11695 */
11696 public View getRootView() {
11697 if (mAttachInfo != null) {
11698 final View v = mAttachInfo.mRootView;
11699 if (v != null) {
11700 return v;
11701 }
11702 }
Romain Guy8506ab42009-06-11 17:35:47 -070011703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011704 View parent = this;
11705
11706 while (parent.mParent != null && parent.mParent instanceof View) {
11707 parent = (View) parent.mParent;
11708 }
11709
11710 return parent;
11711 }
11712
11713 /**
11714 * <p>Computes the coordinates of this view on the screen. The argument
11715 * must be an array of two integers. After the method returns, the array
11716 * contains the x and y location in that order.</p>
11717 *
11718 * @param location an array of two integers in which to hold the coordinates
11719 */
11720 public void getLocationOnScreen(int[] location) {
11721 getLocationInWindow(location);
11722
11723 final AttachInfo info = mAttachInfo;
Romain Guy779398e2009-06-16 13:17:50 -070011724 if (info != null) {
11725 location[0] += info.mWindowLeft;
11726 location[1] += info.mWindowTop;
11727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011728 }
11729
11730 /**
11731 * <p>Computes the coordinates of this view in its window. The argument
11732 * must be an array of two integers. After the method returns, the array
11733 * contains the x and y location in that order.</p>
11734 *
11735 * @param location an array of two integers in which to hold the coordinates
11736 */
11737 public void getLocationInWindow(int[] location) {
11738 if (location == null || location.length < 2) {
11739 throw new IllegalArgumentException("location must be an array of "
11740 + "two integers");
11741 }
11742
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011743 location[0] = mLeft + (int) (mTranslationX + 0.5f);
11744 location[1] = mTop + (int) (mTranslationY + 0.5f);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011745
11746 ViewParent viewParent = mParent;
11747 while (viewParent instanceof View) {
11748 final View view = (View)viewParent;
Michael Jurka4d2bd4c2010-11-30 18:15:11 -080011749 location[0] += view.mLeft + (int) (view.mTranslationX + 0.5f) - view.mScrollX;
11750 location[1] += view.mTop + (int) (view.mTranslationY + 0.5f) - view.mScrollY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011751 viewParent = view.mParent;
11752 }
Romain Guy8506ab42009-06-11 17:35:47 -070011753
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011754 if (viewParent instanceof ViewRootImpl) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011755 // *cough*
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070011756 final ViewRootImpl vr = (ViewRootImpl)viewParent;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011757 location[1] -= vr.mCurScrollY;
11758 }
11759 }
11760
11761 /**
11762 * {@hide}
11763 * @param id the id of the view to be found
11764 * @return the view of the specified id, null if cannot be found
11765 */
11766 protected View findViewTraversal(int id) {
11767 if (id == mID) {
11768 return this;
11769 }
11770 return null;
11771 }
11772
11773 /**
11774 * {@hide}
11775 * @param tag the tag of the view to be found
11776 * @return the view of specified tag, null if cannot be found
11777 */
11778 protected View findViewWithTagTraversal(Object tag) {
11779 if (tag != null && tag.equals(mTag)) {
11780 return this;
11781 }
11782 return null;
11783 }
11784
11785 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011786 * {@hide}
11787 * @param predicate The predicate to evaluate.
Jeff Brown4dfbec22011-08-15 14:55:37 -070011788 * @param childToSkip If not null, ignores this child during the recursive traversal.
Jeff Brown4e6319b2010-12-13 10:36:51 -080011789 * @return The first view that matches the predicate or null.
11790 */
Jeff Brown4dfbec22011-08-15 14:55:37 -070011791 protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) {
Jeff Brown4e6319b2010-12-13 10:36:51 -080011792 if (predicate.apply(this)) {
11793 return this;
11794 }
11795 return null;
11796 }
11797
11798 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011799 * Look for a child view with the given id. If this view has the given
11800 * id, return this view.
11801 *
11802 * @param id The id to search for.
11803 * @return The view that has the given id in the hierarchy or null
11804 */
11805 public final View findViewById(int id) {
11806 if (id < 0) {
11807 return null;
11808 }
11809 return findViewTraversal(id);
11810 }
11811
11812 /**
11813 * Look for a child view with the given tag. If this view has the given
11814 * tag, return this view.
11815 *
11816 * @param tag The tag to search for, using "tag.equals(getTag())".
11817 * @return The View that has the given tag in the hierarchy or null
11818 */
11819 public final View findViewWithTag(Object tag) {
11820 if (tag == null) {
11821 return null;
11822 }
11823 return findViewWithTagTraversal(tag);
11824 }
11825
11826 /**
Jeff Brown4e6319b2010-12-13 10:36:51 -080011827 * {@hide}
11828 * Look for a child view that matches the specified predicate.
11829 * If this view matches the predicate, return this view.
11830 *
11831 * @param predicate The predicate to evaluate.
11832 * @return The first view that matches the predicate or null.
11833 */
11834 public final View findViewByPredicate(Predicate<View> predicate) {
Jeff Brown4dfbec22011-08-15 14:55:37 -070011835 return findViewByPredicateTraversal(predicate, null);
11836 }
11837
11838 /**
11839 * {@hide}
11840 * Look for a child view that matches the specified predicate,
11841 * starting with the specified view and its descendents and then
11842 * recusively searching the ancestors and siblings of that view
11843 * until this view is reached.
11844 *
11845 * This method is useful in cases where the predicate does not match
11846 * a single unique view (perhaps multiple views use the same id)
11847 * and we are trying to find the view that is "closest" in scope to the
11848 * starting view.
11849 *
11850 * @param start The view to start from.
11851 * @param predicate The predicate to evaluate.
11852 * @return The first view that matches the predicate or null.
11853 */
11854 public final View findViewByPredicateInsideOut(View start, Predicate<View> predicate) {
11855 View childToSkip = null;
11856 for (;;) {
11857 View view = start.findViewByPredicateTraversal(predicate, childToSkip);
11858 if (view != null || start == this) {
11859 return view;
11860 }
11861
11862 ViewParent parent = start.getParent();
11863 if (parent == null || !(parent instanceof View)) {
11864 return null;
11865 }
11866
11867 childToSkip = start;
11868 start = (View) parent;
11869 }
Jeff Brown4e6319b2010-12-13 10:36:51 -080011870 }
11871
11872 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011873 * Sets the identifier for this view. The identifier does not have to be
11874 * unique in this view's hierarchy. The identifier should be a positive
11875 * number.
11876 *
11877 * @see #NO_ID
Romain Guy5c22a8c2011-05-13 11:48:45 -070011878 * @see #getId()
11879 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011880 *
11881 * @param id a number used to identify the view
11882 *
11883 * @attr ref android.R.styleable#View_id
11884 */
11885 public void setId(int id) {
11886 mID = id;
11887 }
11888
11889 /**
11890 * {@hide}
11891 *
11892 * @param isRoot true if the view belongs to the root namespace, false
11893 * otherwise
11894 */
11895 public void setIsRootNamespace(boolean isRoot) {
11896 if (isRoot) {
11897 mPrivateFlags |= IS_ROOT_NAMESPACE;
11898 } else {
11899 mPrivateFlags &= ~IS_ROOT_NAMESPACE;
11900 }
11901 }
11902
11903 /**
11904 * {@hide}
11905 *
11906 * @return true if the view belongs to the root namespace, false otherwise
11907 */
11908 public boolean isRootNamespace() {
11909 return (mPrivateFlags&IS_ROOT_NAMESPACE) != 0;
11910 }
11911
11912 /**
11913 * Returns this view's identifier.
11914 *
11915 * @return a positive integer used to identify the view or {@link #NO_ID}
11916 * if the view has no ID
11917 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070011918 * @see #setId(int)
11919 * @see #findViewById(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011920 * @attr ref android.R.styleable#View_id
11921 */
11922 @ViewDebug.CapturedViewProperty
11923 public int getId() {
11924 return mID;
11925 }
11926
11927 /**
11928 * Returns this view's tag.
11929 *
11930 * @return the Object stored in this view as a tag
Romain Guyd90a3312009-05-06 14:54:28 -070011931 *
11932 * @see #setTag(Object)
11933 * @see #getTag(int)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011934 */
11935 @ViewDebug.ExportedProperty
11936 public Object getTag() {
11937 return mTag;
11938 }
11939
11940 /**
11941 * Sets the tag associated with this view. A tag can be used to mark
11942 * a view in its hierarchy and does not have to be unique within the
11943 * hierarchy. Tags can also be used to store data within a view without
11944 * resorting to another data structure.
11945 *
11946 * @param tag an Object to tag the view with
Romain Guyd90a3312009-05-06 14:54:28 -070011947 *
11948 * @see #getTag()
11949 * @see #setTag(int, Object)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080011950 */
11951 public void setTag(final Object tag) {
11952 mTag = tag;
11953 }
11954
11955 /**
Romain Guyd90a3312009-05-06 14:54:28 -070011956 * Returns the tag associated with this view and the specified key.
11957 *
11958 * @param key The key identifying the tag
11959 *
11960 * @return the Object stored in this view as a tag
11961 *
11962 * @see #setTag(int, Object)
Romain Guy8506ab42009-06-11 17:35:47 -070011963 * @see #getTag()
Romain Guyd90a3312009-05-06 14:54:28 -070011964 */
11965 public Object getTag(int key) {
11966 SparseArray<Object> tags = null;
11967 synchronized (sTagsLock) {
11968 if (sTags != null) {
11969 tags = sTags.get(this);
11970 }
11971 }
11972
11973 if (tags != null) return tags.get(key);
11974 return null;
11975 }
11976
11977 /**
11978 * Sets a tag associated with this view and a key. A tag can be used
11979 * to mark a view in its hierarchy and does not have to be unique within
11980 * the hierarchy. Tags can also be used to store data within a view
11981 * without resorting to another data structure.
11982 *
11983 * The specified key should be an id declared in the resources of the
Scott Maindfe5c202010-06-08 15:54:52 -070011984 * application to ensure it is unique (see the <a
11985 * href={@docRoot}guide/topics/resources/more-resources.html#Id">ID resource type</a>).
11986 * Keys identified as belonging to
Romain Guyd90a3312009-05-06 14:54:28 -070011987 * the Android framework or not associated with any package will cause
11988 * an {@link IllegalArgumentException} to be thrown.
11989 *
11990 * @param key The key identifying the tag
11991 * @param tag An Object to tag the view with
11992 *
11993 * @throws IllegalArgumentException If they specified key is not valid
11994 *
11995 * @see #setTag(Object)
11996 * @see #getTag(int)
11997 */
11998 public void setTag(int key, final Object tag) {
11999 // If the package id is 0x00 or 0x01, it's either an undefined package
12000 // or a framework id
12001 if ((key >>> 24) < 2) {
12002 throw new IllegalArgumentException("The key must be an application-specific "
12003 + "resource id.");
12004 }
12005
12006 setTagInternal(this, key, tag);
12007 }
12008
12009 /**
12010 * Variation of {@link #setTag(int, Object)} that enforces the key to be a
12011 * framework id.
12012 *
12013 * @hide
12014 */
12015 public void setTagInternal(int key, Object tag) {
12016 if ((key >>> 24) != 0x1) {
12017 throw new IllegalArgumentException("The key must be a framework-specific "
12018 + "resource id.");
12019 }
12020
Romain Guy8506ab42009-06-11 17:35:47 -070012021 setTagInternal(this, key, tag);
Romain Guyd90a3312009-05-06 14:54:28 -070012022 }
12023
12024 private static void setTagInternal(View view, int key, Object tag) {
12025 SparseArray<Object> tags = null;
12026 synchronized (sTagsLock) {
12027 if (sTags == null) {
12028 sTags = new WeakHashMap<View, SparseArray<Object>>();
12029 } else {
12030 tags = sTags.get(view);
12031 }
12032 }
12033
12034 if (tags == null) {
12035 tags = new SparseArray<Object>(2);
12036 synchronized (sTagsLock) {
12037 sTags.put(view, tags);
12038 }
12039 }
12040
12041 tags.put(key, tag);
12042 }
12043
12044 /**
Romain Guy13922e02009-05-12 17:56:14 -070012045 * @param consistency The type of consistency. See ViewDebug for more information.
12046 *
12047 * @hide
12048 */
12049 protected boolean dispatchConsistencyCheck(int consistency) {
12050 return onConsistencyCheck(consistency);
12051 }
12052
12053 /**
12054 * Method that subclasses should implement to check their consistency. The type of
12055 * consistency check is indicated by the bit field passed as a parameter.
Romain Guy8506ab42009-06-11 17:35:47 -070012056 *
Romain Guy13922e02009-05-12 17:56:14 -070012057 * @param consistency The type of consistency. See ViewDebug for more information.
12058 *
12059 * @throws IllegalStateException if the view is in an inconsistent state.
12060 *
12061 * @hide
12062 */
12063 protected boolean onConsistencyCheck(int consistency) {
12064 boolean result = true;
12065
12066 final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0;
12067 final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0;
12068
12069 if (checkLayout) {
12070 if (getParent() == null) {
12071 result = false;
12072 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12073 "View " + this + " does not have a parent.");
12074 }
12075
12076 if (mAttachInfo == null) {
12077 result = false;
12078 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12079 "View " + this + " is not attached to a window.");
12080 }
12081 }
12082
12083 if (checkDrawing) {
12084 // Do not check the DIRTY/DRAWN flags because views can call invalidate()
12085 // from their draw() method
12086
12087 if ((mPrivateFlags & DRAWN) != DRAWN &&
12088 (mPrivateFlags & DRAWING_CACHE_VALID) == DRAWING_CACHE_VALID) {
12089 result = false;
12090 android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG,
12091 "View " + this + " was invalidated but its drawing cache is valid.");
12092 }
12093 }
12094
12095 return result;
12096 }
12097
12098 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012099 * Prints information about this view in the log output, with the tag
12100 * {@link #VIEW_LOG_TAG}.
12101 *
12102 * @hide
12103 */
12104 public void debug() {
12105 debug(0);
12106 }
12107
12108 /**
12109 * Prints information about this view in the log output, with the tag
12110 * {@link #VIEW_LOG_TAG}. Each line in the output is preceded with an
12111 * indentation defined by the <code>depth</code>.
12112 *
12113 * @param depth the indentation level
12114 *
12115 * @hide
12116 */
12117 protected void debug(int depth) {
12118 String output = debugIndent(depth - 1);
12119
12120 output += "+ " + this;
12121 int id = getId();
12122 if (id != -1) {
12123 output += " (id=" + id + ")";
12124 }
12125 Object tag = getTag();
12126 if (tag != null) {
12127 output += " (tag=" + tag + ")";
12128 }
12129 Log.d(VIEW_LOG_TAG, output);
12130
12131 if ((mPrivateFlags & FOCUSED) != 0) {
12132 output = debugIndent(depth) + " FOCUSED";
12133 Log.d(VIEW_LOG_TAG, output);
12134 }
12135
12136 output = debugIndent(depth);
12137 output += "frame={" + mLeft + ", " + mTop + ", " + mRight
12138 + ", " + mBottom + "} scroll={" + mScrollX + ", " + mScrollY
12139 + "} ";
12140 Log.d(VIEW_LOG_TAG, output);
12141
12142 if (mPaddingLeft != 0 || mPaddingTop != 0 || mPaddingRight != 0
12143 || mPaddingBottom != 0) {
12144 output = debugIndent(depth);
12145 output += "padding={" + mPaddingLeft + ", " + mPaddingTop
12146 + ", " + mPaddingRight + ", " + mPaddingBottom + "}";
12147 Log.d(VIEW_LOG_TAG, output);
12148 }
12149
12150 output = debugIndent(depth);
12151 output += "mMeasureWidth=" + mMeasuredWidth +
12152 " mMeasureHeight=" + mMeasuredHeight;
12153 Log.d(VIEW_LOG_TAG, output);
12154
12155 output = debugIndent(depth);
12156 if (mLayoutParams == null) {
12157 output += "BAD! no layout params";
12158 } else {
12159 output = mLayoutParams.debug(output);
12160 }
12161 Log.d(VIEW_LOG_TAG, output);
12162
12163 output = debugIndent(depth);
12164 output += "flags={";
12165 output += View.printFlags(mViewFlags);
12166 output += "}";
12167 Log.d(VIEW_LOG_TAG, output);
12168
12169 output = debugIndent(depth);
12170 output += "privateFlags={";
12171 output += View.printPrivateFlags(mPrivateFlags);
12172 output += "}";
12173 Log.d(VIEW_LOG_TAG, output);
12174 }
12175
12176 /**
12177 * Creates an string of whitespaces used for indentation.
12178 *
12179 * @param depth the indentation level
12180 * @return a String containing (depth * 2 + 3) * 2 white spaces
12181 *
12182 * @hide
12183 */
12184 protected static String debugIndent(int depth) {
12185 StringBuilder spaces = new StringBuilder((depth * 2 + 3) * 2);
12186 for (int i = 0; i < (depth * 2) + 3; i++) {
12187 spaces.append(' ').append(' ');
12188 }
12189 return spaces.toString();
12190 }
12191
12192 /**
12193 * <p>Return the offset of the widget's text baseline from the widget's top
12194 * boundary. If this widget does not support baseline alignment, this
12195 * method returns -1. </p>
12196 *
12197 * @return the offset of the baseline within the widget's bounds or -1
12198 * if baseline alignment is not supported
12199 */
Konstantin Lopyrevbea95162010-08-10 17:02:18 -070012200 @ViewDebug.ExportedProperty(category = "layout")
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012201 public int getBaseline() {
12202 return -1;
12203 }
12204
12205 /**
12206 * Call this when something has changed which has invalidated the
12207 * layout of this view. This will schedule a layout pass of the view
12208 * tree.
12209 */
12210 public void requestLayout() {
12211 if (ViewDebug.TRACE_HIERARCHY) {
12212 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.REQUEST_LAYOUT);
12213 }
12214
12215 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012216 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012217
Fabrice Di Megliod794aca2011-07-22 18:19:36 -070012218 if (mParent != null) {
12219 if (mLayoutParams != null) {
12220 mLayoutParams.resolveWithDirection(getResolvedLayoutDirection());
12221 }
12222 if (!mParent.isLayoutRequested()) {
12223 mParent.requestLayout();
12224 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012225 }
12226 }
12227
12228 /**
12229 * Forces this view to be laid out during the next layout pass.
12230 * This method does not call requestLayout() or forceLayout()
12231 * on the parent.
12232 */
12233 public void forceLayout() {
12234 mPrivateFlags |= FORCE_LAYOUT;
Chet Haase5af048c2011-01-24 17:00:32 -080012235 mPrivateFlags |= INVALIDATED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012236 }
12237
12238 /**
12239 * <p>
12240 * This is called to find out how big a view should be. The parent
12241 * supplies constraint information in the width and height parameters.
12242 * </p>
12243 *
12244 * <p>
12245 * The actual mesurement work of a view is performed in
12246 * {@link #onMeasure(int, int)}, called by this method. Therefore, only
12247 * {@link #onMeasure(int, int)} can and must be overriden by subclasses.
12248 * </p>
12249 *
12250 *
12251 * @param widthMeasureSpec Horizontal space requirements as imposed by the
12252 * parent
12253 * @param heightMeasureSpec Vertical space requirements as imposed by the
12254 * parent
12255 *
12256 * @see #onMeasure(int, int)
12257 */
12258 public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
12259 if ((mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT ||
12260 widthMeasureSpec != mOldWidthMeasureSpec ||
12261 heightMeasureSpec != mOldHeightMeasureSpec) {
12262
12263 // first clears the measured dimension flag
12264 mPrivateFlags &= ~MEASURED_DIMENSION_SET;
12265
12266 if (ViewDebug.TRACE_HIERARCHY) {
12267 ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_MEASURE);
12268 }
12269
12270 // measure ourselves, this should set the measured dimension flag back
12271 onMeasure(widthMeasureSpec, heightMeasureSpec);
12272
12273 // flag not set, setMeasuredDimension() was not invoked, we raise
12274 // an exception to warn the developer
12275 if ((mPrivateFlags & MEASURED_DIMENSION_SET) != MEASURED_DIMENSION_SET) {
12276 throw new IllegalStateException("onMeasure() did not set the"
12277 + " measured dimension by calling"
12278 + " setMeasuredDimension()");
12279 }
12280
12281 mPrivateFlags |= LAYOUT_REQUIRED;
12282 }
12283
12284 mOldWidthMeasureSpec = widthMeasureSpec;
12285 mOldHeightMeasureSpec = heightMeasureSpec;
12286 }
12287
12288 /**
12289 * <p>
12290 * Measure the view and its content to determine the measured width and the
12291 * measured height. This method is invoked by {@link #measure(int, int)} and
12292 * should be overriden by subclasses to provide accurate and efficient
12293 * measurement of their contents.
12294 * </p>
12295 *
12296 * <p>
12297 * <strong>CONTRACT:</strong> When overriding this method, you
12298 * <em>must</em> call {@link #setMeasuredDimension(int, int)} to store the
12299 * measured width and height of this view. Failure to do so will trigger an
12300 * <code>IllegalStateException</code>, thrown by
12301 * {@link #measure(int, int)}. Calling the superclass'
12302 * {@link #onMeasure(int, int)} is a valid use.
12303 * </p>
12304 *
12305 * <p>
12306 * The base class implementation of measure defaults to the background size,
12307 * unless a larger size is allowed by the MeasureSpec. Subclasses should
12308 * override {@link #onMeasure(int, int)} to provide better measurements of
12309 * their content.
12310 * </p>
12311 *
12312 * <p>
12313 * If this method is overridden, it is the subclass's responsibility to make
12314 * sure the measured height and width are at least the view's minimum height
12315 * and width ({@link #getSuggestedMinimumHeight()} and
12316 * {@link #getSuggestedMinimumWidth()}).
12317 * </p>
12318 *
12319 * @param widthMeasureSpec horizontal space requirements as imposed by the parent.
12320 * The requirements are encoded with
12321 * {@link android.view.View.MeasureSpec}.
12322 * @param heightMeasureSpec vertical space requirements as imposed by the parent.
12323 * The requirements are encoded with
12324 * {@link android.view.View.MeasureSpec}.
12325 *
12326 * @see #getMeasuredWidth()
12327 * @see #getMeasuredHeight()
12328 * @see #setMeasuredDimension(int, int)
12329 * @see #getSuggestedMinimumHeight()
12330 * @see #getSuggestedMinimumWidth()
12331 * @see android.view.View.MeasureSpec#getMode(int)
12332 * @see android.view.View.MeasureSpec#getSize(int)
12333 */
12334 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
12335 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
12336 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
12337 }
12338
12339 /**
12340 * <p>This mehod must be called by {@link #onMeasure(int, int)} to store the
12341 * measured width and measured height. Failing to do so will trigger an
12342 * exception at measurement time.</p>
12343 *
Dianne Hackborn189ee182010-12-02 21:48:53 -080012344 * @param measuredWidth The measured width of this view. May be a complex
12345 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12346 * {@link #MEASURED_STATE_TOO_SMALL}.
12347 * @param measuredHeight The measured height of this view. May be a complex
12348 * bit mask as defined by {@link #MEASURED_SIZE_MASK} and
12349 * {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012350 */
12351 protected final void setMeasuredDimension(int measuredWidth, int measuredHeight) {
12352 mMeasuredWidth = measuredWidth;
12353 mMeasuredHeight = measuredHeight;
12354
12355 mPrivateFlags |= MEASURED_DIMENSION_SET;
12356 }
12357
12358 /**
Dianne Hackborn189ee182010-12-02 21:48:53 -080012359 * Merge two states as returned by {@link #getMeasuredState()}.
12360 * @param curState The current state as returned from a view or the result
12361 * of combining multiple views.
12362 * @param newState The new view state to combine.
12363 * @return Returns a new integer reflecting the combination of the two
12364 * states.
12365 */
12366 public static int combineMeasuredStates(int curState, int newState) {
12367 return curState | newState;
12368 }
12369
12370 /**
12371 * Version of {@link #resolveSizeAndState(int, int, int)}
12372 * returning only the {@link #MEASURED_SIZE_MASK} bits of the result.
12373 */
12374 public static int resolveSize(int size, int measureSpec) {
12375 return resolveSizeAndState(size, measureSpec, 0) & MEASURED_SIZE_MASK;
12376 }
12377
12378 /**
12379 * Utility to reconcile a desired size and state, with constraints imposed
12380 * by a MeasureSpec. Will take the desired size, unless a different size
12381 * is imposed by the constraints. The returned value is a compound integer,
12382 * with the resolved size in the {@link #MEASURED_SIZE_MASK} bits and
12383 * optionally the bit {@link #MEASURED_STATE_TOO_SMALL} set if the resulting
12384 * size is smaller than the size the view wants to be.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012385 *
12386 * @param size How big the view wants to be
12387 * @param measureSpec Constraints imposed by the parent
Dianne Hackborn189ee182010-12-02 21:48:53 -080012388 * @return Size information bit mask as defined by
12389 * {@link #MEASURED_SIZE_MASK} and {@link #MEASURED_STATE_TOO_SMALL}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012390 */
Dianne Hackborn189ee182010-12-02 21:48:53 -080012391 public static int resolveSizeAndState(int size, int measureSpec, int childMeasuredState) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012392 int result = size;
12393 int specMode = MeasureSpec.getMode(measureSpec);
12394 int specSize = MeasureSpec.getSize(measureSpec);
12395 switch (specMode) {
12396 case MeasureSpec.UNSPECIFIED:
12397 result = size;
12398 break;
12399 case MeasureSpec.AT_MOST:
Dianne Hackborn189ee182010-12-02 21:48:53 -080012400 if (specSize < size) {
12401 result = specSize | MEASURED_STATE_TOO_SMALL;
12402 } else {
12403 result = size;
12404 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012405 break;
12406 case MeasureSpec.EXACTLY:
12407 result = specSize;
12408 break;
12409 }
Dianne Hackborn189ee182010-12-02 21:48:53 -080012410 return result | (childMeasuredState&MEASURED_STATE_MASK);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012411 }
12412
12413 /**
12414 * Utility to return a default size. Uses the supplied size if the
Romain Guy98029c82011-06-17 15:47:07 -070012415 * MeasureSpec imposed no constraints. Will get larger if allowed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012416 * by the MeasureSpec.
12417 *
12418 * @param size Default size for this view
12419 * @param measureSpec Constraints imposed by the parent
12420 * @return The size this view should be.
12421 */
12422 public static int getDefaultSize(int size, int measureSpec) {
12423 int result = size;
12424 int specMode = MeasureSpec.getMode(measureSpec);
Romain Guy98029c82011-06-17 15:47:07 -070012425 int specSize = MeasureSpec.getSize(measureSpec);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012426
12427 switch (specMode) {
12428 case MeasureSpec.UNSPECIFIED:
12429 result = size;
12430 break;
12431 case MeasureSpec.AT_MOST:
12432 case MeasureSpec.EXACTLY:
12433 result = specSize;
12434 break;
12435 }
12436 return result;
12437 }
12438
12439 /**
12440 * Returns the suggested minimum height that the view should use. This
12441 * returns the maximum of the view's minimum height
12442 * and the background's minimum height
12443 * ({@link android.graphics.drawable.Drawable#getMinimumHeight()}).
12444 * <p>
12445 * When being used in {@link #onMeasure(int, int)}, the caller should still
12446 * ensure the returned height is within the requirements of the parent.
12447 *
12448 * @return The suggested minimum height of the view.
12449 */
12450 protected int getSuggestedMinimumHeight() {
12451 int suggestedMinHeight = mMinHeight;
12452
12453 if (mBGDrawable != null) {
12454 final int bgMinHeight = mBGDrawable.getMinimumHeight();
12455 if (suggestedMinHeight < bgMinHeight) {
12456 suggestedMinHeight = bgMinHeight;
12457 }
12458 }
12459
12460 return suggestedMinHeight;
12461 }
12462
12463 /**
12464 * Returns the suggested minimum width that the view should use. This
12465 * returns the maximum of the view's minimum width)
12466 * and the background's minimum width
12467 * ({@link android.graphics.drawable.Drawable#getMinimumWidth()}).
12468 * <p>
12469 * When being used in {@link #onMeasure(int, int)}, the caller should still
12470 * ensure the returned width is within the requirements of the parent.
12471 *
12472 * @return The suggested minimum width of the view.
12473 */
12474 protected int getSuggestedMinimumWidth() {
12475 int suggestedMinWidth = mMinWidth;
12476
12477 if (mBGDrawable != null) {
12478 final int bgMinWidth = mBGDrawable.getMinimumWidth();
12479 if (suggestedMinWidth < bgMinWidth) {
12480 suggestedMinWidth = bgMinWidth;
12481 }
12482 }
12483
12484 return suggestedMinWidth;
12485 }
12486
12487 /**
12488 * Sets the minimum height of the view. It is not guaranteed the view will
12489 * be able to achieve this minimum height (for example, if its parent layout
12490 * constrains it with less available height).
12491 *
12492 * @param minHeight The minimum height the view will try to be.
12493 */
12494 public void setMinimumHeight(int minHeight) {
12495 mMinHeight = minHeight;
12496 }
12497
12498 /**
12499 * Sets the minimum width of the view. It is not guaranteed the view will
12500 * be able to achieve this minimum width (for example, if its parent layout
12501 * constrains it with less available width).
12502 *
12503 * @param minWidth The minimum width the view will try to be.
12504 */
12505 public void setMinimumWidth(int minWidth) {
12506 mMinWidth = minWidth;
12507 }
12508
12509 /**
12510 * Get the animation currently associated with this view.
12511 *
12512 * @return The animation that is currently playing or
12513 * scheduled to play for this view.
12514 */
12515 public Animation getAnimation() {
12516 return mCurrentAnimation;
12517 }
12518
12519 /**
12520 * Start the specified animation now.
12521 *
12522 * @param animation the animation to start now
12523 */
12524 public void startAnimation(Animation animation) {
12525 animation.setStartTime(Animation.START_ON_FIRST_FRAME);
12526 setAnimation(animation);
Romain Guy0fd89bf2011-01-26 15:41:30 -080012527 invalidateParentCaches();
12528 invalidate(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012529 }
12530
12531 /**
12532 * Cancels any animations for this view.
12533 */
12534 public void clearAnimation() {
Romain Guy305a2eb2010-02-09 11:30:44 -080012535 if (mCurrentAnimation != null) {
Romain Guyb4a107d2010-02-09 18:50:08 -080012536 mCurrentAnimation.detach();
Romain Guy305a2eb2010-02-09 11:30:44 -080012537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012538 mCurrentAnimation = null;
Romain Guy0fd89bf2011-01-26 15:41:30 -080012539 invalidateParentIfNeeded();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012540 }
12541
12542 /**
12543 * Sets the next animation to play for this view.
12544 * If you want the animation to play immediately, use
12545 * startAnimation. This method provides allows fine-grained
12546 * control over the start time and invalidation, but you
12547 * must make sure that 1) the animation has a start time set, and
12548 * 2) the view will be invalidated when the animation is supposed to
12549 * start.
12550 *
12551 * @param animation The next animation, or null.
12552 */
12553 public void setAnimation(Animation animation) {
12554 mCurrentAnimation = animation;
12555 if (animation != null) {
12556 animation.reset();
12557 }
12558 }
12559
12560 /**
12561 * Invoked by a parent ViewGroup to notify the start of the animation
12562 * currently associated with this view. If you override this method,
12563 * always call super.onAnimationStart();
12564 *
12565 * @see #setAnimation(android.view.animation.Animation)
12566 * @see #getAnimation()
12567 */
12568 protected void onAnimationStart() {
12569 mPrivateFlags |= ANIMATION_STARTED;
12570 }
12571
12572 /**
12573 * Invoked by a parent ViewGroup to notify the end of the animation
12574 * currently associated with this view. If you override this method,
12575 * always call super.onAnimationEnd();
12576 *
12577 * @see #setAnimation(android.view.animation.Animation)
12578 * @see #getAnimation()
12579 */
12580 protected void onAnimationEnd() {
12581 mPrivateFlags &= ~ANIMATION_STARTED;
12582 }
12583
12584 /**
12585 * Invoked if there is a Transform that involves alpha. Subclass that can
12586 * draw themselves with the specified alpha should return true, and then
12587 * respect that alpha when their onDraw() is called. If this returns false
12588 * then the view may be redirected to draw into an offscreen buffer to
12589 * fulfill the request, which will look fine, but may be slower than if the
12590 * subclass handles it internally. The default implementation returns false.
12591 *
12592 * @param alpha The alpha (0..255) to apply to the view's drawing
12593 * @return true if the view can draw with the specified alpha.
12594 */
12595 protected boolean onSetAlpha(int alpha) {
12596 return false;
12597 }
12598
12599 /**
12600 * This is used by the RootView to perform an optimization when
12601 * the view hierarchy contains one or several SurfaceView.
12602 * SurfaceView is always considered transparent, but its children are not,
12603 * therefore all View objects remove themselves from the global transparent
12604 * region (passed as a parameter to this function).
12605 *
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070012606 * @param region The transparent region for this ViewAncestor (window).
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012607 *
12608 * @return Returns true if the effective visibility of the view at this
12609 * point is opaque, regardless of the transparent region; returns false
12610 * if it is possible for underlying windows to be seen behind the view.
12611 *
12612 * {@hide}
12613 */
12614 public boolean gatherTransparentRegion(Region region) {
12615 final AttachInfo attachInfo = mAttachInfo;
12616 if (region != null && attachInfo != null) {
12617 final int pflags = mPrivateFlags;
12618 if ((pflags & SKIP_DRAW) == 0) {
12619 // The SKIP_DRAW flag IS NOT set, so this view draws. We need to
12620 // remove it from the transparent region.
12621 final int[] location = attachInfo.mTransparentLocation;
12622 getLocationInWindow(location);
12623 region.op(location[0], location[1], location[0] + mRight - mLeft,
12624 location[1] + mBottom - mTop, Region.Op.DIFFERENCE);
12625 } else if ((pflags & ONLY_DRAWS_BACKGROUND) != 0 && mBGDrawable != null) {
12626 // The ONLY_DRAWS_BACKGROUND flag IS set and the background drawable
12627 // exists, so we remove the background drawable's non-transparent
12628 // parts from this transparent region.
12629 applyDrawableToTransparentRegion(mBGDrawable, region);
12630 }
12631 }
12632 return true;
12633 }
12634
12635 /**
12636 * Play a sound effect for this view.
12637 *
12638 * <p>The framework will play sound effects for some built in actions, such as
12639 * clicking, but you may wish to play these effects in your widget,
12640 * for instance, for internal navigation.
12641 *
12642 * <p>The sound effect will only be played if sound effects are enabled by the user, and
12643 * {@link #isSoundEffectsEnabled()} is true.
12644 *
12645 * @param soundConstant One of the constants defined in {@link SoundEffectConstants}
12646 */
12647 public void playSoundEffect(int soundConstant) {
12648 if (mAttachInfo == null || mAttachInfo.mRootCallbacks == null || !isSoundEffectsEnabled()) {
12649 return;
12650 }
12651 mAttachInfo.mRootCallbacks.playSoundEffect(soundConstant);
12652 }
12653
12654 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012655 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012656 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012657 * <p>Provide haptic feedback to the user for this view.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012658 *
12659 * <p>The framework will provide haptic feedback for some built in actions,
12660 * such as long presses, but you may wish to provide feedback for your
12661 * own widget.
12662 *
12663 * <p>The feedback will only be performed if
12664 * {@link #isHapticFeedbackEnabled()} is true.
12665 *
12666 * @param feedbackConstant One of the constants defined in
12667 * {@link HapticFeedbackConstants}
12668 */
12669 public boolean performHapticFeedback(int feedbackConstant) {
12670 return performHapticFeedback(feedbackConstant, 0);
12671 }
12672
12673 /**
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012674 * BZZZTT!!1!
Romain Guy8506ab42009-06-11 17:35:47 -070012675 *
Andy Stadlerf8a7cea2009-04-10 16:24:47 -070012676 * <p>Like {@link #performHapticFeedback(int)}, with additional options.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012677 *
12678 * @param feedbackConstant One of the constants defined in
12679 * {@link HapticFeedbackConstants}
12680 * @param flags Additional flags as per {@link HapticFeedbackConstants}.
12681 */
12682 public boolean performHapticFeedback(int feedbackConstant, int flags) {
12683 if (mAttachInfo == null) {
12684 return false;
12685 }
Romain Guyf607bdc2010-09-10 19:20:06 -070012686 //noinspection SimplifiableIfStatement
Romain Guy812ccbe2010-06-01 14:07:24 -070012687 if ((flags & HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING) == 0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012688 && !isHapticFeedbackEnabled()) {
12689 return false;
12690 }
Romain Guy812ccbe2010-06-01 14:07:24 -070012691 return mAttachInfo.mRootCallbacks.performHapticFeedback(feedbackConstant,
12692 (flags & HapticFeedbackConstants.FLAG_IGNORE_GLOBAL_SETTING) != 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080012693 }
12694
12695 /**
Joe Onorato664644d2011-01-23 17:53:23 -080012696 * Request that the visibility of the status bar be changed.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012697 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12698 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Daniel Sandlerfad95552011-08-17 14:06:16 -040012699 *
12700 * This value will be re-applied immediately, even if the flags have not changed, so a view may
12701 * easily reassert a particular SystemUiVisibility condition even if the system UI itself has
12702 * since countermanded the original request.
Joe Onorato664644d2011-01-23 17:53:23 -080012703 */
12704 public void setSystemUiVisibility(int visibility) {
Daniel Sandlerfad95552011-08-17 14:06:16 -040012705 mSystemUiVisibility = visibility;
12706 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12707 mParent.recomputeViewAttributes(this);
Joe Onorato664644d2011-01-23 17:53:23 -080012708 }
12709 }
12710
12711 /**
12712 * Returns the status bar visibility that this view has requested.
Daniel Sandler60ee2562011-07-22 12:34:33 -040012713 * @return Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
12714 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080012715 */
Joe Onoratoe595cad2011-01-24 09:22:12 -080012716 public int getSystemUiVisibility() {
Joe Onorato664644d2011-01-23 17:53:23 -080012717 return mSystemUiVisibility;
12718 }
12719
Scott Mainec6331b2011-05-24 16:55:56 -070012720 /**
12721 * Set a listener to receive callbacks when the visibility of the system bar changes.
12722 * @param l The {@link OnSystemUiVisibilityChangeListener} to receive callbacks.
12723 */
Joe Onorato664644d2011-01-23 17:53:23 -080012724 public void setOnSystemUiVisibilityChangeListener(OnSystemUiVisibilityChangeListener l) {
12725 mOnSystemUiVisibilityChangeListener = l;
12726 if (mParent != null && mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) {
12727 mParent.recomputeViewAttributes(this);
12728 }
12729 }
12730
12731 /**
12732 */
12733 public void dispatchSystemUiVisibilityChanged(int visibility) {
12734 if (mOnSystemUiVisibilityChangeListener != null) {
Joe Onorato7bb8eeb2011-01-27 16:00:58 -080012735 mOnSystemUiVisibilityChangeListener.onSystemUiVisibilityChange(
Joe Onorato6ab77bd2011-01-31 11:21:10 -080012736 visibility & PUBLIC_STATUS_BAR_VISIBILITY_MASK);
Joe Onorato664644d2011-01-23 17:53:23 -080012737 }
12738 }
12739
12740 /**
Joe Malin32736f02011-01-19 16:14:20 -080012741 * Creates an image that the system displays during the drag and drop
12742 * operation. This is called a &quot;drag shadow&quot;. The default implementation
12743 * for a DragShadowBuilder based on a View returns an image that has exactly the same
12744 * appearance as the given View. The default also positions the center of the drag shadow
12745 * directly under the touch point. If no View is provided (the constructor with no parameters
12746 * is used), and {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} and
12747 * {@link #onDrawShadow(Canvas) onDrawShadow()} are not overriden, then the
12748 * default is an invisible drag shadow.
12749 * <p>
12750 * You are not required to use the View you provide to the constructor as the basis of the
12751 * drag shadow. The {@link #onDrawShadow(Canvas) onDrawShadow()} method allows you to draw
12752 * anything you want as the drag shadow.
12753 * </p>
12754 * <p>
12755 * You pass a DragShadowBuilder object to the system when you start the drag. The system
12756 * calls {@link #onProvideShadowMetrics(Point,Point) onProvideShadowMetrics()} to get the
12757 * size and position of the drag shadow. It uses this data to construct a
12758 * {@link android.graphics.Canvas} object, then it calls {@link #onDrawShadow(Canvas) onDrawShadow()}
12759 * so that your application can draw the shadow image in the Canvas.
12760 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012761 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012762 public static class DragShadowBuilder {
Christopher Tatea0374192010-10-05 13:06:41 -070012763 private final WeakReference<View> mView;
Christopher Tate2c095f32010-10-04 14:13:40 -070012764
12765 /**
Joe Malin32736f02011-01-19 16:14:20 -080012766 * Constructs a shadow image builder based on a View. By default, the resulting drag
12767 * shadow will have the same appearance and dimensions as the View, with the touch point
12768 * over the center of the View.
12769 * @param view A View. Any View in scope can be used.
Christopher Tate2c095f32010-10-04 14:13:40 -070012770 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012771 public DragShadowBuilder(View view) {
Christopher Tatea0374192010-10-05 13:06:41 -070012772 mView = new WeakReference<View>(view);
Christopher Tate2c095f32010-10-04 14:13:40 -070012773 }
12774
Christopher Tate17ed60c2011-01-18 12:50:26 -080012775 /**
12776 * Construct a shadow builder object with no associated View. This
12777 * constructor variant is only useful when the {@link #onProvideShadowMetrics(Point, Point)}
12778 * and {@link #onDrawShadow(Canvas)} methods are also overridden in order
12779 * to supply the drag shadow's dimensions and appearance without
Joe Malin32736f02011-01-19 16:14:20 -080012780 * reference to any View object. If they are not overridden, then the result is an
12781 * invisible drag shadow.
Christopher Tate17ed60c2011-01-18 12:50:26 -080012782 */
12783 public DragShadowBuilder() {
12784 mView = new WeakReference<View>(null);
12785 }
12786
12787 /**
12788 * Returns the View object that had been passed to the
12789 * {@link #View.DragShadowBuilder(View)}
12790 * constructor. If that View parameter was {@code null} or if the
12791 * {@link #View.DragShadowBuilder()}
12792 * constructor was used to instantiate the builder object, this method will return
12793 * null.
12794 *
12795 * @return The View object associate with this builder object.
12796 */
Romain Guy5c22a8c2011-05-13 11:48:45 -070012797 @SuppressWarnings({"JavadocReference"})
Chris Tate6b391282010-10-14 15:48:59 -070012798 final public View getView() {
12799 return mView.get();
12800 }
12801
Christopher Tate2c095f32010-10-04 14:13:40 -070012802 /**
Joe Malin32736f02011-01-19 16:14:20 -080012803 * Provides the metrics for the shadow image. These include the dimensions of
12804 * the shadow image, and the point within that shadow that should
Christopher Tate2c095f32010-10-04 14:13:40 -070012805 * be centered under the touch location while dragging.
12806 * <p>
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012807 * The default implementation sets the dimensions of the shadow to be the
Joe Malin32736f02011-01-19 16:14:20 -080012808 * same as the dimensions of the View itself and centers the shadow under
12809 * the touch point.
12810 * </p>
Christopher Tate2c095f32010-10-04 14:13:40 -070012811 *
Joe Malin32736f02011-01-19 16:14:20 -080012812 * @param shadowSize A {@link android.graphics.Point} containing the width and height
12813 * of the shadow image. Your application must set {@link android.graphics.Point#x} to the
12814 * desired width and must set {@link android.graphics.Point#y} to the desired height of the
12815 * image.
12816 *
12817 * @param shadowTouchPoint A {@link android.graphics.Point} for the position within the
12818 * shadow image that should be underneath the touch point during the drag and drop
12819 * operation. Your application must set {@link android.graphics.Point#x} to the
12820 * X coordinate and {@link android.graphics.Point#y} to the Y coordinate of this position.
Christopher Tate2c095f32010-10-04 14:13:40 -070012821 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012822 public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
Christopher Tatea0374192010-10-05 13:06:41 -070012823 final View view = mView.get();
12824 if (view != null) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012825 shadowSize.set(view.getWidth(), view.getHeight());
12826 shadowTouchPoint.set(shadowSize.x / 2, shadowSize.y / 2);
Christopher Tatea0374192010-10-05 13:06:41 -070012827 } else {
12828 Log.e(View.VIEW_LOG_TAG, "Asked for drag thumb metrics but no view");
12829 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012830 }
12831
12832 /**
Joe Malin32736f02011-01-19 16:14:20 -080012833 * Draws the shadow image. The system creates the {@link android.graphics.Canvas} object
12834 * based on the dimensions it received from the
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012835 * {@link #onProvideShadowMetrics(Point, Point)} callback.
Christopher Tate2c095f32010-10-04 14:13:40 -070012836 *
Joe Malin32736f02011-01-19 16:14:20 -080012837 * @param canvas A {@link android.graphics.Canvas} object in which to draw the shadow image.
Christopher Tate2c095f32010-10-04 14:13:40 -070012838 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012839 public void onDrawShadow(Canvas canvas) {
Christopher Tatea0374192010-10-05 13:06:41 -070012840 final View view = mView.get();
12841 if (view != null) {
12842 view.draw(canvas);
12843 } else {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012844 Log.e(View.VIEW_LOG_TAG, "Asked to draw drag shadow but no view");
Christopher Tatea0374192010-10-05 13:06:41 -070012845 }
Christopher Tate2c095f32010-10-04 14:13:40 -070012846 }
12847 }
12848
12849 /**
Joe Malin32736f02011-01-19 16:14:20 -080012850 * Starts a drag and drop operation. When your application calls this method, it passes a
12851 * {@link android.view.View.DragShadowBuilder} object to the system. The
12852 * system calls this object's {@link DragShadowBuilder#onProvideShadowMetrics(Point, Point)}
12853 * to get metrics for the drag shadow, and then calls the object's
12854 * {@link DragShadowBuilder#onDrawShadow(Canvas)} to draw the drag shadow itself.
12855 * <p>
12856 * Once the system has the drag shadow, it begins the drag and drop operation by sending
12857 * drag events to all the View objects in your application that are currently visible. It does
12858 * this either by calling the View object's drag listener (an implementation of
12859 * {@link android.view.View.OnDragListener#onDrag(View,DragEvent) onDrag()} or by calling the
12860 * View object's {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} method.
12861 * Both are passed a {@link android.view.DragEvent} object that has a
12862 * {@link android.view.DragEvent#getAction()} value of
12863 * {@link android.view.DragEvent#ACTION_DRAG_STARTED}.
12864 * </p>
12865 * <p>
12866 * Your application can invoke startDrag() on any attached View object. The View object does not
12867 * need to be the one used in {@link android.view.View.DragShadowBuilder}, nor does it need to
12868 * be related to the View the user selected for dragging.
12869 * </p>
12870 * @param data A {@link android.content.ClipData} object pointing to the data to be
12871 * transferred by the drag and drop operation.
12872 * @param shadowBuilder A {@link android.view.View.DragShadowBuilder} object for building the
12873 * drag shadow.
12874 * @param myLocalState An {@link java.lang.Object} containing local data about the drag and
12875 * drop operation. This Object is put into every DragEvent object sent by the system during the
12876 * current drag.
12877 * <p>
12878 * myLocalState is a lightweight mechanism for the sending information from the dragged View
12879 * to the target Views. For example, it can contain flags that differentiate between a
12880 * a copy operation and a move operation.
12881 * </p>
12882 * @param flags Flags that control the drag and drop operation. No flags are currently defined,
12883 * so the parameter should be set to 0.
12884 * @return {@code true} if the method completes successfully, or
12885 * {@code false} if it fails anywhere. Returning {@code false} means the system was unable to
12886 * do a drag, and so no drag operation is in progress.
Christopher Tatea53146c2010-09-07 11:57:52 -070012887 */
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012888 public final boolean startDrag(ClipData data, DragShadowBuilder shadowBuilder,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012889 Object myLocalState, int flags) {
Christopher Tate2c095f32010-10-04 14:13:40 -070012890 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012891 Log.d(VIEW_LOG_TAG, "startDrag: data=" + data + " flags=" + flags);
Christopher Tatea53146c2010-09-07 11:57:52 -070012892 }
12893 boolean okay = false;
12894
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012895 Point shadowSize = new Point();
12896 Point shadowTouchPoint = new Point();
12897 shadowBuilder.onProvideShadowMetrics(shadowSize, shadowTouchPoint);
Christopher Tate2c095f32010-10-04 14:13:40 -070012898
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012899 if ((shadowSize.x < 0) || (shadowSize.y < 0) ||
12900 (shadowTouchPoint.x < 0) || (shadowTouchPoint.y < 0)) {
12901 throw new IllegalStateException("Drag shadow dimensions must not be negative");
Christopher Tate2c095f32010-10-04 14:13:40 -070012902 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012903
Chris Tatea32dcf72010-10-14 12:13:50 -070012904 if (ViewDebug.DEBUG_DRAG) {
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012905 Log.d(VIEW_LOG_TAG, "drag shadow: width=" + shadowSize.x + " height=" + shadowSize.y
12906 + " shadowX=" + shadowTouchPoint.x + " shadowY=" + shadowTouchPoint.y);
Chris Tatea32dcf72010-10-14 12:13:50 -070012907 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012908 Surface surface = new Surface();
12909 try {
12910 IBinder token = mAttachInfo.mSession.prepareDrag(mAttachInfo.mWindow,
Christopher Tate02d2b3b2011-01-10 20:43:53 -080012911 flags, shadowSize.x, shadowSize.y, surface);
Christopher Tate2c095f32010-10-04 14:13:40 -070012912 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "prepareDrag returned token=" + token
Christopher Tatea53146c2010-09-07 11:57:52 -070012913 + " surface=" + surface);
12914 if (token != null) {
12915 Canvas canvas = surface.lockCanvas(null);
Romain Guy0bb56672010-10-01 00:25:02 -070012916 try {
Chris Tate6b391282010-10-14 15:48:59 -070012917 canvas.drawColor(0, PorterDuff.Mode.CLEAR);
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012918 shadowBuilder.onDrawShadow(canvas);
Romain Guy0bb56672010-10-01 00:25:02 -070012919 } finally {
12920 surface.unlockCanvasAndPost(canvas);
12921 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012922
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070012923 final ViewRootImpl root = getViewRootImpl();
Christopher Tate407b4e92010-11-30 17:14:08 -080012924
12925 // Cache the local state object for delivery with DragEvents
12926 root.setLocalDragState(myLocalState);
12927
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012928 // repurpose 'shadowSize' for the last touch point
12929 root.getLastTouchPoint(shadowSize);
Christopher Tate2c095f32010-10-04 14:13:40 -070012930
Christopher Tatea53146c2010-09-07 11:57:52 -070012931 okay = mAttachInfo.mSession.performDrag(mAttachInfo.mWindow, token,
Christopher Tate36d4c3f2011-01-07 13:34:24 -080012932 shadowSize.x, shadowSize.y,
12933 shadowTouchPoint.x, shadowTouchPoint.y, data);
Christopher Tate2c095f32010-10-04 14:13:40 -070012934 if (ViewDebug.DEBUG_DRAG) Log.d(VIEW_LOG_TAG, "performDrag returned " + okay);
Christopher Tatea53146c2010-09-07 11:57:52 -070012935 }
12936 } catch (Exception e) {
12937 Log.e(VIEW_LOG_TAG, "Unable to initiate drag", e);
12938 surface.destroy();
12939 }
12940
12941 return okay;
12942 }
12943
Christopher Tatea53146c2010-09-07 11:57:52 -070012944 /**
Joe Malin32736f02011-01-19 16:14:20 -080012945 * Handles drag events sent by the system following a call to
12946 * {@link android.view.View#startDrag(ClipData,DragShadowBuilder,Object,int) startDrag()}.
12947 *<p>
12948 * When the system calls this method, it passes a
12949 * {@link android.view.DragEvent} object. A call to
12950 * {@link android.view.DragEvent#getAction()} returns one of the action type constants defined
12951 * in DragEvent. The method uses these to determine what is happening in the drag and drop
12952 * operation.
12953 * @param event The {@link android.view.DragEvent} sent by the system.
12954 * The {@link android.view.DragEvent#getAction()} method returns an action type constant defined
12955 * in DragEvent, indicating the type of drag event represented by this object.
12956 * @return {@code true} if the method was successful, otherwise {@code false}.
12957 * <p>
12958 * The method should return {@code true} in response to an action type of
12959 * {@link android.view.DragEvent#ACTION_DRAG_STARTED} to receive drag events for the current
12960 * operation.
12961 * </p>
12962 * <p>
12963 * The method should also return {@code true} in response to an action type of
12964 * {@link android.view.DragEvent#ACTION_DROP} if it consumed the drop, or
12965 * {@code false} if it didn't.
12966 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012967 */
Christopher Tate5ada6cb2010-10-05 14:15:29 -070012968 public boolean onDragEvent(DragEvent event) {
Christopher Tatea53146c2010-09-07 11:57:52 -070012969 return false;
12970 }
12971
12972 /**
Joe Malin32736f02011-01-19 16:14:20 -080012973 * Detects if this View is enabled and has a drag event listener.
12974 * If both are true, then it calls the drag event listener with the
12975 * {@link android.view.DragEvent} it received. If the drag event listener returns
12976 * {@code true}, then dispatchDragEvent() returns {@code true}.
12977 * <p>
12978 * For all other cases, the method calls the
12979 * {@link android.view.View#onDragEvent(DragEvent) onDragEvent()} drag event handler
12980 * method and returns its result.
12981 * </p>
12982 * <p>
12983 * This ensures that a drag event is always consumed, even if the View does not have a drag
12984 * event listener. However, if the View has a listener and the listener returns true, then
12985 * onDragEvent() is not called.
12986 * </p>
Christopher Tatea53146c2010-09-07 11:57:52 -070012987 */
12988 public boolean dispatchDragEvent(DragEvent event) {
Romain Guy676b1732011-02-14 14:45:33 -080012989 //noinspection SimplifiableIfStatement
Chris Tate32affef2010-10-18 15:29:21 -070012990 if (mOnDragListener != null && (mViewFlags & ENABLED_MASK) == ENABLED
12991 && mOnDragListener.onDrag(this, event)) {
12992 return true;
12993 }
Christopher Tatea53146c2010-09-07 11:57:52 -070012994 return onDragEvent(event);
12995 }
12996
Christopher Tate3d4bf172011-03-28 16:16:46 -070012997 boolean canAcceptDrag() {
12998 return (mPrivateFlags2 & DRAG_CAN_ACCEPT) != 0;
12999 }
13000
Christopher Tatea53146c2010-09-07 11:57:52 -070013001 /**
Dianne Hackbornffa42482009-09-23 22:20:11 -070013002 * This needs to be a better API (NOT ON VIEW) before it is exposed. If
13003 * it is ever exposed at all.
Dianne Hackborn29e4a3c2009-09-30 22:35:40 -070013004 * @hide
Dianne Hackbornffa42482009-09-23 22:20:11 -070013005 */
13006 public void onCloseSystemDialogs(String reason) {
13007 }
Joe Malin32736f02011-01-19 16:14:20 -080013008
Dianne Hackbornffa42482009-09-23 22:20:11 -070013009 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013010 * Given a Drawable whose bounds have been set to draw into this view,
Romain Guy5c22a8c2011-05-13 11:48:45 -070013011 * update a Region being computed for
13012 * {@link #gatherTransparentRegion(android.graphics.Region)} so
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013013 * that any non-transparent parts of the Drawable are removed from the
13014 * given transparent region.
13015 *
13016 * @param dr The Drawable whose transparency is to be applied to the region.
13017 * @param region A Region holding the current transparency information,
13018 * where any parts of the region that are set are considered to be
13019 * transparent. On return, this region will be modified to have the
13020 * transparency information reduced by the corresponding parts of the
13021 * Drawable that are not transparent.
13022 * {@hide}
13023 */
13024 public void applyDrawableToTransparentRegion(Drawable dr, Region region) {
13025 if (DBG) {
13026 Log.i("View", "Getting transparent region for: " + this);
13027 }
13028 final Region r = dr.getTransparentRegion();
13029 final Rect db = dr.getBounds();
13030 final AttachInfo attachInfo = mAttachInfo;
13031 if (r != null && attachInfo != null) {
13032 final int w = getRight()-getLeft();
13033 final int h = getBottom()-getTop();
13034 if (db.left > 0) {
13035 //Log.i("VIEW", "Drawable left " + db.left + " > view 0");
13036 r.op(0, 0, db.left, h, Region.Op.UNION);
13037 }
13038 if (db.right < w) {
13039 //Log.i("VIEW", "Drawable right " + db.right + " < view " + w);
13040 r.op(db.right, 0, w, h, Region.Op.UNION);
13041 }
13042 if (db.top > 0) {
13043 //Log.i("VIEW", "Drawable top " + db.top + " > view 0");
13044 r.op(0, 0, w, db.top, Region.Op.UNION);
13045 }
13046 if (db.bottom < h) {
13047 //Log.i("VIEW", "Drawable bottom " + db.bottom + " < view " + h);
13048 r.op(0, db.bottom, w, h, Region.Op.UNION);
13049 }
13050 final int[] location = attachInfo.mTransparentLocation;
13051 getLocationInWindow(location);
13052 r.translate(location[0], location[1]);
13053 region.op(r, Region.Op.INTERSECT);
13054 } else {
13055 region.op(db, Region.Op.DIFFERENCE);
13056 }
13057 }
13058
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013059 private void checkForLongClick(int delayOffset) {
13060 if ((mViewFlags & LONG_CLICKABLE) == LONG_CLICKABLE) {
13061 mHasPerformedLongPress = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013062
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013063 if (mPendingCheckForLongPress == null) {
13064 mPendingCheckForLongPress = new CheckForLongPress();
13065 }
13066 mPendingCheckForLongPress.rememberWindowAttachCount();
13067 postDelayed(mPendingCheckForLongPress,
13068 ViewConfiguration.getLongPressTimeout() - delayOffset);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013070 }
13071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013072 /**
13073 * Inflate a view from an XML resource. This convenience method wraps the {@link
13074 * LayoutInflater} class, which provides a full range of options for view inflation.
13075 *
13076 * @param context The Context object for your activity or application.
13077 * @param resource The resource ID to inflate
13078 * @param root A view group that will be the parent. Used to properly inflate the
13079 * layout_* parameters.
13080 * @see LayoutInflater
13081 */
13082 public static View inflate(Context context, int resource, ViewGroup root) {
13083 LayoutInflater factory = LayoutInflater.from(context);
13084 return factory.inflate(resource, root);
13085 }
Romain Guy33e72ae2010-07-17 12:40:29 -070013086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013087 /**
Adam Powell637d3372010-08-25 14:37:03 -070013088 * Scroll the view with standard behavior for scrolling beyond the normal
13089 * content boundaries. Views that call this method should override
13090 * {@link #onOverScrolled(int, int, boolean, boolean)} to respond to the
13091 * results of an over-scroll operation.
13092 *
13093 * Views can use this method to handle any touch or fling-based scrolling.
13094 *
13095 * @param deltaX Change in X in pixels
13096 * @param deltaY Change in Y in pixels
13097 * @param scrollX Current X scroll value in pixels before applying deltaX
13098 * @param scrollY Current Y scroll value in pixels before applying deltaY
13099 * @param scrollRangeX Maximum content scroll range along the X axis
13100 * @param scrollRangeY Maximum content scroll range along the Y axis
13101 * @param maxOverScrollX Number of pixels to overscroll by in either direction
13102 * along the X axis.
13103 * @param maxOverScrollY Number of pixels to overscroll by in either direction
13104 * along the Y axis.
13105 * @param isTouchEvent true if this scroll operation is the result of a touch event.
13106 * @return true if scrolling was clamped to an over-scroll boundary along either
13107 * axis, false otherwise.
13108 */
Romain Guy7b5b6ab2011-03-14 18:05:08 -070013109 @SuppressWarnings({"UnusedParameters"})
Adam Powell637d3372010-08-25 14:37:03 -070013110 protected boolean overScrollBy(int deltaX, int deltaY,
13111 int scrollX, int scrollY,
13112 int scrollRangeX, int scrollRangeY,
13113 int maxOverScrollX, int maxOverScrollY,
13114 boolean isTouchEvent) {
13115 final int overScrollMode = mOverScrollMode;
13116 final boolean canScrollHorizontal =
13117 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
13118 final boolean canScrollVertical =
13119 computeVerticalScrollRange() > computeVerticalScrollExtent();
13120 final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
13121 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13122 final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
13123 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
13124
13125 int newScrollX = scrollX + deltaX;
13126 if (!overScrollHorizontal) {
13127 maxOverScrollX = 0;
13128 }
13129
13130 int newScrollY = scrollY + deltaY;
13131 if (!overScrollVertical) {
13132 maxOverScrollY = 0;
13133 }
13134
13135 // Clamp values if at the limits and record
13136 final int left = -maxOverScrollX;
13137 final int right = maxOverScrollX + scrollRangeX;
13138 final int top = -maxOverScrollY;
13139 final int bottom = maxOverScrollY + scrollRangeY;
13140
13141 boolean clampedX = false;
13142 if (newScrollX > right) {
13143 newScrollX = right;
13144 clampedX = true;
13145 } else if (newScrollX < left) {
13146 newScrollX = left;
13147 clampedX = true;
13148 }
13149
13150 boolean clampedY = false;
13151 if (newScrollY > bottom) {
13152 newScrollY = bottom;
13153 clampedY = true;
13154 } else if (newScrollY < top) {
13155 newScrollY = top;
13156 clampedY = true;
13157 }
13158
13159 onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
13160
13161 return clampedX || clampedY;
13162 }
13163
13164 /**
13165 * Called by {@link #overScrollBy(int, int, int, int, int, int, int, int, boolean)} to
13166 * respond to the results of an over-scroll operation.
13167 *
13168 * @param scrollX New X scroll value in pixels
13169 * @param scrollY New Y scroll value in pixels
13170 * @param clampedX True if scrollX was clamped to an over-scroll boundary
13171 * @param clampedY True if scrollY was clamped to an over-scroll boundary
13172 */
13173 protected void onOverScrolled(int scrollX, int scrollY,
13174 boolean clampedX, boolean clampedY) {
13175 // Intentionally empty.
13176 }
13177
13178 /**
13179 * Returns the over-scroll mode for this view. The result will be
13180 * one of {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13181 * (allow over-scrolling only if the view content is larger than the container),
13182 * or {@link #OVER_SCROLL_NEVER}.
13183 *
13184 * @return This view's over-scroll mode.
13185 */
13186 public int getOverScrollMode() {
13187 return mOverScrollMode;
13188 }
13189
13190 /**
13191 * Set the over-scroll mode for this view. Valid over-scroll modes are
13192 * {@link #OVER_SCROLL_ALWAYS} (default), {@link #OVER_SCROLL_IF_CONTENT_SCROLLS}
13193 * (allow over-scrolling only if the view content is larger than the container),
13194 * or {@link #OVER_SCROLL_NEVER}.
13195 *
13196 * Setting the over-scroll mode of a view will have an effect only if the
13197 * view is capable of scrolling.
13198 *
13199 * @param overScrollMode The new over-scroll mode for this view.
13200 */
13201 public void setOverScrollMode(int overScrollMode) {
13202 if (overScrollMode != OVER_SCROLL_ALWAYS &&
13203 overScrollMode != OVER_SCROLL_IF_CONTENT_SCROLLS &&
13204 overScrollMode != OVER_SCROLL_NEVER) {
13205 throw new IllegalArgumentException("Invalid overscroll mode " + overScrollMode);
13206 }
13207 mOverScrollMode = overScrollMode;
13208 }
13209
13210 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013211 * Gets a scale factor that determines the distance the view should scroll
13212 * vertically in response to {@link MotionEvent#ACTION_SCROLL}.
13213 * @return The vertical scroll scale factor.
13214 * @hide
13215 */
13216 protected float getVerticalScrollFactor() {
13217 if (mVerticalScrollFactor == 0) {
13218 TypedValue outValue = new TypedValue();
13219 if (!mContext.getTheme().resolveAttribute(
13220 com.android.internal.R.attr.listPreferredItemHeight, outValue, true)) {
13221 throw new IllegalStateException(
13222 "Expected theme to define listPreferredItemHeight.");
13223 }
13224 mVerticalScrollFactor = outValue.getDimension(
13225 mContext.getResources().getDisplayMetrics());
13226 }
13227 return mVerticalScrollFactor;
13228 }
13229
13230 /**
13231 * Gets a scale factor that determines the distance the view should scroll
13232 * horizontally in response to {@link MotionEvent#ACTION_SCROLL}.
13233 * @return The horizontal scroll scale factor.
13234 * @hide
13235 */
13236 protected float getHorizontalScrollFactor() {
13237 // TODO: Should use something else.
13238 return getVerticalScrollFactor();
13239 }
13240
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013241 /**
13242 * Return the value specifying the text direction or policy that was set with
13243 * {@link #setTextDirection(int)}.
13244 *
13245 * @return the defined text direction. It can be one of:
13246 *
13247 * {@link #TEXT_DIRECTION_INHERIT},
13248 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13249 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013250 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013251 * {@link #TEXT_DIRECTION_LTR},
13252 * {@link #TEXT_DIRECTION_RTL},
13253 *
13254 * @hide
13255 */
13256 public int getTextDirection() {
13257 return mTextDirection;
13258 }
13259
13260 /**
13261 * Set the text direction.
13262 *
13263 * @param textDirection the direction to set. Should be one of:
13264 *
13265 * {@link #TEXT_DIRECTION_INHERIT},
13266 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13267 * {@link #TEXT_DIRECTION_ANY_RTL},
Fabrice Di Meglio8f502652011-06-29 20:40:43 -070013268 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013269 * {@link #TEXT_DIRECTION_LTR},
13270 * {@link #TEXT_DIRECTION_RTL},
13271 *
13272 * @hide
13273 */
13274 public void setTextDirection(int textDirection) {
13275 if (textDirection != mTextDirection) {
13276 mTextDirection = textDirection;
13277 resetResolvedTextDirection();
13278 requestLayout();
13279 }
13280 }
13281
13282 /**
13283 * Return the resolved text direction.
13284 *
13285 * @return the resolved text direction. Return one of:
13286 *
Doug Feltcb3791202011-07-07 11:57:48 -070013287 * {@link #TEXT_DIRECTION_FIRST_STRONG}
13288 * {@link #TEXT_DIRECTION_ANY_RTL},
13289 * {@link #TEXT_DIRECTION_CHAR_COUNT},
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013290 * {@link #TEXT_DIRECTION_LTR},
13291 * {@link #TEXT_DIRECTION_RTL},
13292 *
13293 * @hide
13294 */
13295 public int getResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013296 if (mResolvedTextDirection == TEXT_DIRECTION_INHERIT) {
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013297 resolveTextDirection();
13298 }
13299 return mResolvedTextDirection;
13300 }
13301
13302 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013303 * Resolve the text direction.
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013304 */
13305 protected void resolveTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013306 if (mTextDirection != TEXT_DIRECTION_INHERIT) {
13307 mResolvedTextDirection = mTextDirection;
13308 return;
13309 }
13310 if (mParent != null && mParent instanceof ViewGroup) {
13311 mResolvedTextDirection = ((ViewGroup) mParent).getResolvedTextDirection();
13312 return;
13313 }
13314 mResolvedTextDirection = TEXT_DIRECTION_FIRST_STRONG;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013315 }
13316
13317 /**
Doug Feltcb3791202011-07-07 11:57:48 -070013318 * Reset resolved text direction. Will be resolved during a call to getResolvedTextDirection().
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013319 */
13320 protected void resetResolvedTextDirection() {
Doug Feltcb3791202011-07-07 11:57:48 -070013321 mResolvedTextDirection = TEXT_DIRECTION_INHERIT;
Fabrice Di Meglio22268862011-06-27 18:13:18 -070013322 }
13323
Chet Haaseb39f0512011-05-24 14:36:40 -070013324 //
13325 // Properties
13326 //
13327 /**
13328 * A Property wrapper around the <code>alpha</code> functionality handled by the
13329 * {@link View#setAlpha(float)} and {@link View#getAlpha()} methods.
13330 */
Romain Guy02ccac62011-06-24 13:20:23 -070013331 public static Property<View, Float> ALPHA = new FloatProperty<View>("alpha") {
Chet Haaseb39f0512011-05-24 14:36:40 -070013332 @Override
13333 public void setValue(View object, float value) {
13334 object.setAlpha(value);
13335 }
13336
13337 @Override
13338 public Float get(View object) {
13339 return object.getAlpha();
13340 }
13341 };
13342
13343 /**
13344 * A Property wrapper around the <code>translationX</code> functionality handled by the
13345 * {@link View#setTranslationX(float)} and {@link View#getTranslationX()} methods.
13346 */
13347 public static Property<View, Float> TRANSLATION_X = new FloatProperty<View>("translationX") {
13348 @Override
13349 public void setValue(View object, float value) {
13350 object.setTranslationX(value);
13351 }
13352
13353 @Override
13354 public Float get(View object) {
13355 return object.getTranslationX();
13356 }
13357 };
13358
13359 /**
13360 * A Property wrapper around the <code>translationY</code> functionality handled by the
13361 * {@link View#setTranslationY(float)} and {@link View#getTranslationY()} methods.
13362 */
13363 public static Property<View, Float> TRANSLATION_Y = new FloatProperty<View>("translationY") {
13364 @Override
13365 public void setValue(View object, float value) {
13366 object.setTranslationY(value);
13367 }
13368
13369 @Override
13370 public Float get(View object) {
13371 return object.getTranslationY();
13372 }
13373 };
13374
13375 /**
13376 * A Property wrapper around the <code>x</code> functionality handled by the
13377 * {@link View#setX(float)} and {@link View#getX()} methods.
13378 */
13379 public static Property<View, Float> X = new FloatProperty<View>("x") {
13380 @Override
13381 public void setValue(View object, float value) {
13382 object.setX(value);
13383 }
13384
13385 @Override
13386 public Float get(View object) {
13387 return object.getX();
13388 }
13389 };
13390
13391 /**
13392 * A Property wrapper around the <code>y</code> functionality handled by the
13393 * {@link View#setY(float)} and {@link View#getY()} methods.
13394 */
13395 public static Property<View, Float> Y = new FloatProperty<View>("y") {
13396 @Override
13397 public void setValue(View object, float value) {
13398 object.setY(value);
13399 }
13400
13401 @Override
13402 public Float get(View object) {
13403 return object.getY();
13404 }
13405 };
13406
13407 /**
13408 * A Property wrapper around the <code>rotation</code> functionality handled by the
13409 * {@link View#setRotation(float)} and {@link View#getRotation()} methods.
13410 */
13411 public static Property<View, Float> ROTATION = new FloatProperty<View>("rotation") {
13412 @Override
13413 public void setValue(View object, float value) {
13414 object.setRotation(value);
13415 }
13416
13417 @Override
13418 public Float get(View object) {
13419 return object.getRotation();
13420 }
13421 };
13422
13423 /**
13424 * A Property wrapper around the <code>rotationX</code> functionality handled by the
13425 * {@link View#setRotationX(float)} and {@link View#getRotationX()} methods.
13426 */
13427 public static Property<View, Float> ROTATION_X = new FloatProperty<View>("rotationX") {
13428 @Override
13429 public void setValue(View object, float value) {
13430 object.setRotationX(value);
13431 }
13432
13433 @Override
13434 public Float get(View object) {
13435 return object.getRotationX();
13436 }
13437 };
13438
13439 /**
13440 * A Property wrapper around the <code>rotationY</code> functionality handled by the
13441 * {@link View#setRotationY(float)} and {@link View#getRotationY()} methods.
13442 */
13443 public static Property<View, Float> ROTATION_Y = new FloatProperty<View>("rotationY") {
13444 @Override
13445 public void setValue(View object, float value) {
13446 object.setRotationY(value);
13447 }
13448
13449 @Override
13450 public Float get(View object) {
13451 return object.getRotationY();
13452 }
13453 };
13454
13455 /**
13456 * A Property wrapper around the <code>scaleX</code> functionality handled by the
13457 * {@link View#setScaleX(float)} and {@link View#getScaleX()} methods.
13458 */
13459 public static Property<View, Float> SCALE_X = new FloatProperty<View>("scaleX") {
13460 @Override
13461 public void setValue(View object, float value) {
13462 object.setScaleX(value);
13463 }
13464
13465 @Override
13466 public Float get(View object) {
13467 return object.getScaleX();
13468 }
13469 };
13470
13471 /**
13472 * A Property wrapper around the <code>scaleY</code> functionality handled by the
13473 * {@link View#setScaleY(float)} and {@link View#getScaleY()} methods.
13474 */
13475 public static Property<View, Float> SCALE_Y = new FloatProperty<View>("scaleY") {
13476 @Override
13477 public void setValue(View object, float value) {
13478 object.setScaleY(value);
13479 }
13480
13481 @Override
13482 public Float get(View object) {
13483 return object.getScaleY();
13484 }
13485 };
13486
Jeff Brown33bbfd22011-02-24 20:55:35 -080013487 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013488 * A MeasureSpec encapsulates the layout requirements passed from parent to child.
13489 * Each MeasureSpec represents a requirement for either the width or the height.
13490 * A MeasureSpec is comprised of a size and a mode. There are three possible
13491 * modes:
13492 * <dl>
13493 * <dt>UNSPECIFIED</dt>
13494 * <dd>
13495 * The parent has not imposed any constraint on the child. It can be whatever size
13496 * it wants.
13497 * </dd>
13498 *
13499 * <dt>EXACTLY</dt>
13500 * <dd>
13501 * The parent has determined an exact size for the child. The child is going to be
13502 * given those bounds regardless of how big it wants to be.
13503 * </dd>
13504 *
13505 * <dt>AT_MOST</dt>
13506 * <dd>
13507 * The child can be as large as it wants up to the specified size.
13508 * </dd>
13509 * </dl>
13510 *
13511 * MeasureSpecs are implemented as ints to reduce object allocation. This class
13512 * is provided to pack and unpack the &lt;size, mode&gt; tuple into the int.
13513 */
13514 public static class MeasureSpec {
13515 private static final int MODE_SHIFT = 30;
13516 private static final int MODE_MASK = 0x3 << MODE_SHIFT;
13517
13518 /**
13519 * Measure specification mode: The parent has not imposed any constraint
13520 * on the child. It can be whatever size it wants.
13521 */
13522 public static final int UNSPECIFIED = 0 << MODE_SHIFT;
13523
13524 /**
13525 * Measure specification mode: The parent has determined an exact size
13526 * for the child. The child is going to be given those bounds regardless
13527 * of how big it wants to be.
13528 */
13529 public static final int EXACTLY = 1 << MODE_SHIFT;
13530
13531 /**
13532 * Measure specification mode: The child can be as large as it wants up
13533 * to the specified size.
13534 */
13535 public static final int AT_MOST = 2 << MODE_SHIFT;
13536
13537 /**
13538 * Creates a measure specification based on the supplied size and mode.
13539 *
13540 * The mode must always be one of the following:
13541 * <ul>
13542 * <li>{@link android.view.View.MeasureSpec#UNSPECIFIED}</li>
13543 * <li>{@link android.view.View.MeasureSpec#EXACTLY}</li>
13544 * <li>{@link android.view.View.MeasureSpec#AT_MOST}</li>
13545 * </ul>
13546 *
13547 * @param size the size of the measure specification
13548 * @param mode the mode of the measure specification
13549 * @return the measure specification based on size and mode
13550 */
13551 public static int makeMeasureSpec(int size, int mode) {
13552 return size + mode;
13553 }
13554
13555 /**
13556 * Extracts the mode from the supplied measure specification.
13557 *
13558 * @param measureSpec the measure specification to extract the mode from
13559 * @return {@link android.view.View.MeasureSpec#UNSPECIFIED},
13560 * {@link android.view.View.MeasureSpec#AT_MOST} or
13561 * {@link android.view.View.MeasureSpec#EXACTLY}
13562 */
13563 public static int getMode(int measureSpec) {
13564 return (measureSpec & MODE_MASK);
13565 }
13566
13567 /**
13568 * Extracts the size from the supplied measure specification.
13569 *
13570 * @param measureSpec the measure specification to extract the size from
13571 * @return the size in pixels defined in the supplied measure specification
13572 */
13573 public static int getSize(int measureSpec) {
13574 return (measureSpec & ~MODE_MASK);
13575 }
13576
13577 /**
13578 * Returns a String representation of the specified measure
13579 * specification.
13580 *
13581 * @param measureSpec the measure specification to convert to a String
13582 * @return a String with the following format: "MeasureSpec: MODE SIZE"
13583 */
13584 public static String toString(int measureSpec) {
13585 int mode = getMode(measureSpec);
13586 int size = getSize(measureSpec);
13587
13588 StringBuilder sb = new StringBuilder("MeasureSpec: ");
13589
13590 if (mode == UNSPECIFIED)
13591 sb.append("UNSPECIFIED ");
13592 else if (mode == EXACTLY)
13593 sb.append("EXACTLY ");
13594 else if (mode == AT_MOST)
13595 sb.append("AT_MOST ");
13596 else
13597 sb.append(mode).append(" ");
13598
13599 sb.append(size);
13600 return sb.toString();
13601 }
13602 }
13603
13604 class CheckForLongPress implements Runnable {
13605
13606 private int mOriginalWindowAttachCount;
13607
13608 public void run() {
The Android Open Source Project10592532009-03-18 17:39:46 -070013609 if (isPressed() && (mParent != null)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013610 && mOriginalWindowAttachCount == mWindowAttachCount) {
13611 if (performLongClick()) {
13612 mHasPerformedLongPress = true;
13613 }
13614 }
13615 }
13616
13617 public void rememberWindowAttachCount() {
13618 mOriginalWindowAttachCount = mWindowAttachCount;
13619 }
13620 }
Joe Malin32736f02011-01-19 16:14:20 -080013621
Adam Powelle14579b2009-12-16 18:39:52 -080013622 private final class CheckForTap implements Runnable {
13623 public void run() {
13624 mPrivateFlags &= ~PREPRESSED;
13625 mPrivateFlags |= PRESSED;
13626 refreshDrawableState();
Patrick Dubroye0a799a2011-05-04 16:19:22 -070013627 checkForLongClick(ViewConfiguration.getTapTimeout());
Adam Powelle14579b2009-12-16 18:39:52 -080013628 }
13629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013630
Adam Powella35d7682010-03-12 14:48:13 -080013631 private final class PerformClick implements Runnable {
13632 public void run() {
13633 performClick();
13634 }
13635 }
13636
Dianne Hackborn63042d62011-01-26 18:56:29 -080013637 /** @hide */
13638 public void hackTurnOffWindowResizeAnim(boolean off) {
13639 mAttachInfo.mTurnOffWindowResizeAnim = off;
13640 }
Joe Malin32736f02011-01-19 16:14:20 -080013641
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013642 /**
Chet Haasea00f3862011-02-22 06:34:40 -080013643 * This method returns a ViewPropertyAnimator object, which can be used to animate
13644 * specific properties on this View.
13645 *
13646 * @return ViewPropertyAnimator The ViewPropertyAnimator associated with this View.
13647 */
13648 public ViewPropertyAnimator animate() {
13649 if (mAnimator == null) {
13650 mAnimator = new ViewPropertyAnimator(this);
13651 }
13652 return mAnimator;
13653 }
13654
13655 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013656 * Interface definition for a callback to be invoked when a key event is
13657 * dispatched to this view. The callback will be invoked before the key
13658 * event is given to the view.
13659 */
13660 public interface OnKeyListener {
13661 /**
13662 * Called when a key is dispatched to a view. This allows listeners to
13663 * get a chance to respond before the target view.
13664 *
13665 * @param v The view the key has been dispatched to.
13666 * @param keyCode The code for the physical key that was pressed
13667 * @param event The KeyEvent object containing full information about
13668 * the event.
13669 * @return True if the listener has consumed the event, false otherwise.
13670 */
13671 boolean onKey(View v, int keyCode, KeyEvent event);
13672 }
13673
13674 /**
13675 * Interface definition for a callback to be invoked when a touch event is
13676 * dispatched to this view. The callback will be invoked before the touch
13677 * event is given to the view.
13678 */
13679 public interface OnTouchListener {
13680 /**
13681 * Called when a touch event is dispatched to a view. This allows listeners to
13682 * get a chance to respond before the target view.
13683 *
13684 * @param v The view the touch event has been dispatched to.
13685 * @param event The MotionEvent object containing full information about
13686 * the event.
13687 * @return True if the listener has consumed the event, false otherwise.
13688 */
13689 boolean onTouch(View v, MotionEvent event);
13690 }
13691
13692 /**
Jeff Brown10b62902011-06-20 16:40:37 -070013693 * Interface definition for a callback to be invoked when a hover event is
13694 * dispatched to this view. The callback will be invoked before the hover
13695 * event is given to the view.
13696 */
13697 public interface OnHoverListener {
13698 /**
13699 * Called when a hover event is dispatched to a view. This allows listeners to
13700 * get a chance to respond before the target view.
13701 *
13702 * @param v The view the hover event has been dispatched to.
13703 * @param event The MotionEvent object containing full information about
13704 * the event.
13705 * @return True if the listener has consumed the event, false otherwise.
13706 */
13707 boolean onHover(View v, MotionEvent event);
13708 }
13709
13710 /**
Jeff Brown33bbfd22011-02-24 20:55:35 -080013711 * Interface definition for a callback to be invoked when a generic motion event is
13712 * dispatched to this view. The callback will be invoked before the generic motion
13713 * event is given to the view.
13714 */
13715 public interface OnGenericMotionListener {
13716 /**
13717 * Called when a generic motion event is dispatched to a view. This allows listeners to
13718 * get a chance to respond before the target view.
13719 *
13720 * @param v The view the generic motion event has been dispatched to.
13721 * @param event The MotionEvent object containing full information about
13722 * the event.
13723 * @return True if the listener has consumed the event, false otherwise.
13724 */
13725 boolean onGenericMotion(View v, MotionEvent event);
13726 }
13727
13728 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013729 * Interface definition for a callback to be invoked when a view has been clicked and held.
13730 */
13731 public interface OnLongClickListener {
13732 /**
13733 * Called when a view has been clicked and held.
13734 *
13735 * @param v The view that was clicked and held.
13736 *
Brad Fitzpatrick69ea4e12011-01-05 11:13:40 -080013737 * @return true if the callback consumed the long click, false otherwise.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013738 */
13739 boolean onLongClick(View v);
13740 }
13741
13742 /**
Chris Tate32affef2010-10-18 15:29:21 -070013743 * Interface definition for a callback to be invoked when a drag is being dispatched
13744 * to this view. The callback will be invoked before the hosting view's own
13745 * onDrag(event) method. If the listener wants to fall back to the hosting view's
13746 * onDrag(event) behavior, it should return 'false' from this callback.
13747 */
13748 public interface OnDragListener {
13749 /**
13750 * Called when a drag event is dispatched to a view. This allows listeners
13751 * to get a chance to override base View behavior.
13752 *
Joe Malin32736f02011-01-19 16:14:20 -080013753 * @param v The View that received the drag event.
13754 * @param event The {@link android.view.DragEvent} object for the drag event.
13755 * @return {@code true} if the drag event was handled successfully, or {@code false}
13756 * if the drag event was not handled. Note that {@code false} will trigger the View
13757 * to call its {@link #onDragEvent(DragEvent) onDragEvent()} handler.
Chris Tate32affef2010-10-18 15:29:21 -070013758 */
13759 boolean onDrag(View v, DragEvent event);
13760 }
13761
13762 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013763 * Interface definition for a callback to be invoked when the focus state of
13764 * a view changed.
13765 */
13766 public interface OnFocusChangeListener {
13767 /**
13768 * Called when the focus state of a view has changed.
13769 *
13770 * @param v The view whose state has changed.
13771 * @param hasFocus The new focus state of v.
13772 */
13773 void onFocusChange(View v, boolean hasFocus);
13774 }
13775
13776 /**
13777 * Interface definition for a callback to be invoked when a view is clicked.
13778 */
13779 public interface OnClickListener {
13780 /**
13781 * Called when a view has been clicked.
13782 *
13783 * @param v The view that was clicked.
13784 */
13785 void onClick(View v);
13786 }
13787
13788 /**
13789 * Interface definition for a callback to be invoked when the context menu
13790 * for this view is being built.
13791 */
13792 public interface OnCreateContextMenuListener {
13793 /**
13794 * Called when the context menu for this view is being built. It is not
13795 * safe to hold onto the menu after this method returns.
13796 *
13797 * @param menu The context menu that is being built
13798 * @param v The view for which the context menu is being built
13799 * @param menuInfo Extra information about the item for which the
13800 * context menu should be shown. This information will vary
13801 * depending on the class of v.
13802 */
13803 void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
13804 }
13805
Joe Onorato664644d2011-01-23 17:53:23 -080013806 /**
13807 * Interface definition for a callback to be invoked when the status bar changes
13808 * visibility.
13809 *
Romain Guy5c22a8c2011-05-13 11:48:45 -070013810 * @see View#setOnSystemUiVisibilityChangeListener(android.view.View.OnSystemUiVisibilityChangeListener)
Joe Onorato664644d2011-01-23 17:53:23 -080013811 */
13812 public interface OnSystemUiVisibilityChangeListener {
13813 /**
13814 * Called when the status bar changes visibility because of a call to
Romain Guy5c22a8c2011-05-13 11:48:45 -070013815 * {@link View#setSystemUiVisibility(int)}.
Joe Onorato664644d2011-01-23 17:53:23 -080013816 *
Daniel Sandler60ee2562011-07-22 12:34:33 -040013817 * @param visibility Bitwise-or of flags {@link #SYSTEM_UI_FLAG_LOW_PROFILE} or
13818 * {@link #SYSTEM_UI_FLAG_HIDE_NAVIGATION}.
Joe Onorato664644d2011-01-23 17:53:23 -080013819 */
13820 public void onSystemUiVisibilityChange(int visibility);
13821 }
13822
Adam Powell4afd62b2011-02-18 15:02:18 -080013823 /**
13824 * Interface definition for a callback to be invoked when this view is attached
13825 * or detached from its window.
13826 */
13827 public interface OnAttachStateChangeListener {
13828 /**
13829 * Called when the view is attached to a window.
13830 * @param v The view that was attached
13831 */
13832 public void onViewAttachedToWindow(View v);
13833 /**
13834 * Called when the view is detached from a window.
13835 * @param v The view that was detached
13836 */
13837 public void onViewDetachedFromWindow(View v);
13838 }
13839
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013840 private final class UnsetPressedState implements Runnable {
13841 public void run() {
13842 setPressed(false);
13843 }
13844 }
13845
13846 /**
13847 * Base class for derived classes that want to save and restore their own
13848 * state in {@link android.view.View#onSaveInstanceState()}.
13849 */
13850 public static class BaseSavedState extends AbsSavedState {
13851 /**
13852 * Constructor used when reading from a parcel. Reads the state of the superclass.
13853 *
13854 * @param source
13855 */
13856 public BaseSavedState(Parcel source) {
13857 super(source);
13858 }
13859
13860 /**
13861 * Constructor called by derived classes when creating their SavedState objects
13862 *
13863 * @param superState The state of the superclass of this view
13864 */
13865 public BaseSavedState(Parcelable superState) {
13866 super(superState);
13867 }
13868
13869 public static final Parcelable.Creator<BaseSavedState> CREATOR =
13870 new Parcelable.Creator<BaseSavedState>() {
13871 public BaseSavedState createFromParcel(Parcel in) {
13872 return new BaseSavedState(in);
13873 }
13874
13875 public BaseSavedState[] newArray(int size) {
13876 return new BaseSavedState[size];
13877 }
13878 };
13879 }
13880
13881 /**
13882 * A set of information given to a view when it is attached to its parent
13883 * window.
13884 */
13885 static class AttachInfo {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013886 interface Callbacks {
13887 void playSoundEffect(int effectId);
13888 boolean performHapticFeedback(int effectId, boolean always);
13889 }
13890
13891 /**
13892 * InvalidateInfo is used to post invalidate(int, int, int, int) messages
13893 * to a Handler. This class contains the target (View) to invalidate and
13894 * the coordinates of the dirty rectangle.
13895 *
13896 * For performance purposes, this class also implements a pool of up to
13897 * POOL_LIMIT objects that get reused. This reduces memory allocations
13898 * whenever possible.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013899 */
Romain Guyd928d682009-03-31 17:52:16 -070013900 static class InvalidateInfo implements Poolable<InvalidateInfo> {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013901 private static final int POOL_LIMIT = 10;
Romain Guy2e9bbce2009-04-01 10:40:10 -070013902 private static final Pool<InvalidateInfo> sPool = Pools.synchronizedPool(
13903 Pools.finitePool(new PoolableManager<InvalidateInfo>() {
Romain Guyd928d682009-03-31 17:52:16 -070013904 public InvalidateInfo newInstance() {
13905 return new InvalidateInfo();
13906 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013907
Romain Guyd928d682009-03-31 17:52:16 -070013908 public void onAcquired(InvalidateInfo element) {
13909 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013910
Romain Guyd928d682009-03-31 17:52:16 -070013911 public void onReleased(InvalidateInfo element) {
13912 }
13913 }, POOL_LIMIT)
13914 );
13915
13916 private InvalidateInfo mNext;
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013917 private boolean mIsPooled;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013918
13919 View target;
13920
13921 int left;
13922 int top;
13923 int right;
13924 int bottom;
13925
Romain Guyd928d682009-03-31 17:52:16 -070013926 public void setNextPoolable(InvalidateInfo element) {
13927 mNext = element;
13928 }
13929
13930 public InvalidateInfo getNextPoolable() {
13931 return mNext;
13932 }
13933
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013934 static InvalidateInfo acquire() {
Romain Guyd928d682009-03-31 17:52:16 -070013935 return sPool.acquire();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013936 }
13937
13938 void release() {
Romain Guyd928d682009-03-31 17:52:16 -070013939 sPool.release(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013940 }
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070013941
13942 public boolean isPooled() {
13943 return mIsPooled;
13944 }
13945
13946 public void setPooled(boolean isPooled) {
13947 mIsPooled = isPooled;
13948 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013949 }
13950
13951 final IWindowSession mSession;
13952
13953 final IWindow mWindow;
13954
13955 final IBinder mWindowToken;
13956
13957 final Callbacks mRootCallbacks;
13958
Romain Guy59a12ca2011-06-09 17:48:21 -070013959 HardwareCanvas mHardwareCanvas;
Chet Haasedaf98e92011-01-10 14:10:36 -080013960
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013961 /**
13962 * The top view of the hierarchy.
13963 */
13964 View mRootView;
Romain Guy8506ab42009-06-11 17:35:47 -070013965
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013966 IBinder mPanelParentWindowToken;
13967 Surface mSurface;
13968
Romain Guyb051e892010-09-28 19:09:36 -070013969 boolean mHardwareAccelerated;
Dianne Hackborn7eec10e2010-11-12 18:03:47 -080013970 boolean mHardwareAccelerationRequested;
Romain Guyb051e892010-09-28 19:09:36 -070013971 HardwareRenderer mHardwareRenderer;
Joe Malin32736f02011-01-19 16:14:20 -080013972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013973 /**
Romain Guy8506ab42009-06-11 17:35:47 -070013974 * Scale factor used by the compatibility mode
13975 */
13976 float mApplicationScale;
13977
13978 /**
13979 * Indicates whether the application is in compatibility mode
13980 */
13981 boolean mScalingRequired;
13982
13983 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070013984 * If set, ViewAncestor doesn't use its lame animation for when the window resizes.
Dianne Hackborn63042d62011-01-26 18:56:29 -080013985 */
13986 boolean mTurnOffWindowResizeAnim;
Joe Malin32736f02011-01-19 16:14:20 -080013987
Dianne Hackborn63042d62011-01-26 18:56:29 -080013988 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080013989 * Left position of this view's window
13990 */
13991 int mWindowLeft;
13992
13993 /**
13994 * Top position of this view's window
13995 */
13996 int mWindowTop;
13997
13998 /**
Adam Powell26153a32010-11-08 15:22:27 -080013999 * Indicates whether views need to use 32-bit drawing caches
Romain Guy35b38ce2009-10-07 13:38:55 -070014000 */
Adam Powell26153a32010-11-08 15:22:27 -080014001 boolean mUse32BitDrawingCache;
Romain Guy35b38ce2009-10-07 13:38:55 -070014002
14003 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014004 * For windows that are full-screen but using insets to layout inside
14005 * of the screen decorations, these are the current insets for the
14006 * content of the window.
14007 */
14008 final Rect mContentInsets = new Rect();
14009
14010 /**
14011 * For windows that are full-screen but using insets to layout inside
14012 * of the screen decorations, these are the current insets for the
14013 * actual visible parts of the window.
14014 */
14015 final Rect mVisibleInsets = new Rect();
14016
14017 /**
14018 * The internal insets given by this window. This value is
14019 * supplied by the client (through
14020 * {@link ViewTreeObserver.OnComputeInternalInsetsListener}) and will
14021 * be given to the window manager when changed to be used in laying
14022 * out windows behind it.
14023 */
14024 final ViewTreeObserver.InternalInsetsInfo mGivenInternalInsets
14025 = new ViewTreeObserver.InternalInsetsInfo();
14026
14027 /**
14028 * All views in the window's hierarchy that serve as scroll containers,
14029 * used to determine if the window can be resized or must be panned
14030 * to adjust for a soft input area.
14031 */
14032 final ArrayList<View> mScrollContainers = new ArrayList<View>();
14033
Dianne Hackborn83fe3f52009-09-12 23:38:30 -070014034 final KeyEvent.DispatcherState mKeyDispatchState
14035 = new KeyEvent.DispatcherState();
14036
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014037 /**
14038 * Indicates whether the view's window currently has the focus.
14039 */
14040 boolean mHasWindowFocus;
14041
14042 /**
14043 * The current visibility of the window.
14044 */
14045 int mWindowVisibility;
14046
14047 /**
14048 * Indicates the time at which drawing started to occur.
14049 */
14050 long mDrawingTime;
14051
14052 /**
Romain Guy5bcdff42009-05-14 21:27:18 -070014053 * Indicates whether or not ignoring the DIRTY_MASK flags.
14054 */
14055 boolean mIgnoreDirtyState;
14056
14057 /**
Romain Guy02ccac62011-06-24 13:20:23 -070014058 * This flag tracks when the mIgnoreDirtyState flag is set during draw(),
14059 * to avoid clearing that flag prematurely.
14060 */
14061 boolean mSetIgnoreDirtyState = false;
14062
14063 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014064 * Indicates whether the view's window is currently in touch mode.
14065 */
14066 boolean mInTouchMode;
14067
14068 /**
Joe Onoratoc6cc0f82011-04-12 11:53:13 -070014069 * Indicates that ViewAncestor should trigger a global layout change
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014070 * the next time it performs a traversal
14071 */
14072 boolean mRecomputeGlobalAttributes;
14073
14074 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014075 * Set during a traveral if any views want to keep the screen on.
14076 */
14077 boolean mKeepScreenOn;
14078
14079 /**
Joe Onorato664644d2011-01-23 17:53:23 -080014080 * Bitwise-or of all of the values that views have passed to setSystemUiVisibility().
14081 */
14082 int mSystemUiVisibility;
14083
14084 /**
14085 * True if a view in this hierarchy has an OnSystemUiVisibilityChangeListener
14086 * attached.
14087 */
14088 boolean mHasSystemUiListeners;
14089
14090 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014091 * Set if the visibility of any views has changed.
14092 */
14093 boolean mViewVisibilityChanged;
14094
14095 /**
14096 * Set to true if a view has been scrolled.
14097 */
14098 boolean mViewScrollChanged;
14099
14100 /**
14101 * Global to the view hierarchy used as a temporary for dealing with
14102 * x/y points in the transparent region computations.
14103 */
14104 final int[] mTransparentLocation = new int[2];
14105
14106 /**
14107 * Global to the view hierarchy used as a temporary for dealing with
14108 * x/y points in the ViewGroup.invalidateChild implementation.
14109 */
14110 final int[] mInvalidateChildLocation = new int[2];
14111
Chet Haasec3aa3612010-06-17 08:50:37 -070014112
14113 /**
14114 * Global to the view hierarchy used as a temporary for dealing with
14115 * x/y location when view is transformed.
14116 */
14117 final float[] mTmpTransformLocation = new float[2];
14118
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014119 /**
14120 * The view tree observer used to dispatch global events like
14121 * layout, pre-draw, touch mode change, etc.
14122 */
14123 final ViewTreeObserver mTreeObserver = new ViewTreeObserver();
14124
14125 /**
14126 * A Canvas used by the view hierarchy to perform bitmap caching.
14127 */
14128 Canvas mCanvas;
14129
14130 /**
Dianne Hackborn6dd005b2011-07-18 13:22:50 -070014131 * A Handler supplied by a view's {@link android.view.ViewRootImpl}. This
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014132 * handler can be used to pump events in the UI events queue.
14133 */
14134 final Handler mHandler;
14135
14136 /**
14137 * Identifier for messages requesting the view to be invalidated.
14138 * Such messages should be sent to {@link #mHandler}.
14139 */
14140 static final int INVALIDATE_MSG = 0x1;
14141
14142 /**
14143 * Identifier for messages requesting the view to invalidate a region.
14144 * Such messages should be sent to {@link #mHandler}.
14145 */
14146 static final int INVALIDATE_RECT_MSG = 0x2;
14147
14148 /**
14149 * Temporary for use in computing invalidate rectangles while
14150 * calling up the hierarchy.
14151 */
14152 final Rect mTmpInvalRect = new Rect();
svetoslavganov75986cf2009-05-14 22:28:01 -070014153
14154 /**
Chet Haasec3aa3612010-06-17 08:50:37 -070014155 * Temporary for use in computing hit areas with transformed views
14156 */
14157 final RectF mTmpTransformRect = new RectF();
14158
14159 /**
svetoslavganov75986cf2009-05-14 22:28:01 -070014160 * Temporary list for use in collecting focusable descendents of a view.
14161 */
14162 final ArrayList<View> mFocusablesTempList = new ArrayList<View>(24);
14163
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014164 /**
Svetoslav Ganov8643aa02011-04-20 12:12:33 -070014165 * The id of the window for accessibility purposes.
14166 */
14167 int mAccessibilityWindowId = View.NO_ID;
14168
14169 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014170 * Creates a new set of attachment information with the specified
14171 * events handler and thread.
14172 *
14173 * @param handler the events handler the view must use
14174 */
14175 AttachInfo(IWindowSession session, IWindow window,
14176 Handler handler, Callbacks effectPlayer) {
14177 mSession = session;
14178 mWindow = window;
14179 mWindowToken = window.asBinder();
14180 mHandler = handler;
14181 mRootCallbacks = effectPlayer;
14182 }
14183 }
14184
14185 /**
14186 * <p>ScrollabilityCache holds various fields used by a View when scrolling
14187 * is supported. This avoids keeping too many unused fields in most
14188 * instances of View.</p>
14189 */
Mike Cleronf116bf82009-09-27 19:14:12 -070014190 private static class ScrollabilityCache implements Runnable {
Joe Malin32736f02011-01-19 16:14:20 -080014191
Mike Cleronf116bf82009-09-27 19:14:12 -070014192 /**
14193 * Scrollbars are not visible
14194 */
14195 public static final int OFF = 0;
14196
14197 /**
14198 * Scrollbars are visible
14199 */
14200 public static final int ON = 1;
14201
14202 /**
14203 * Scrollbars are fading away
14204 */
14205 public static final int FADING = 2;
14206
14207 public boolean fadeScrollBars;
Joe Malin32736f02011-01-19 16:14:20 -080014208
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014209 public int fadingEdgeLength;
Mike Cleronf116bf82009-09-27 19:14:12 -070014210 public int scrollBarDefaultDelayBeforeFade;
14211 public int scrollBarFadeDuration;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014212
14213 public int scrollBarSize;
14214 public ScrollBarDrawable scrollBar;
Mike Cleronf116bf82009-09-27 19:14:12 -070014215 public float[] interpolatorValues;
14216 public View host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014217
14218 public final Paint paint;
14219 public final Matrix matrix;
14220 public Shader shader;
14221
Mike Cleronf116bf82009-09-27 19:14:12 -070014222 public final Interpolator scrollBarInterpolator = new Interpolator(1, 2);
14223
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014224 private static final float[] OPAQUE = { 255 };
14225 private static final float[] TRANSPARENT = { 0.0f };
Joe Malin32736f02011-01-19 16:14:20 -080014226
Mike Cleronf116bf82009-09-27 19:14:12 -070014227 /**
14228 * When fading should start. This time moves into the future every time
14229 * a new scroll happens. Measured based on SystemClock.uptimeMillis()
14230 */
14231 public long fadeStartTime;
14232
14233
14234 /**
14235 * The current state of the scrollbars: ON, OFF, or FADING
14236 */
14237 public int state = OFF;
14238
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014239 private int mLastColor;
14240
Mike Cleronf116bf82009-09-27 19:14:12 -070014241 public ScrollabilityCache(ViewConfiguration configuration, View host) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014242 fadingEdgeLength = configuration.getScaledFadingEdgeLength();
14243 scrollBarSize = configuration.getScaledScrollBarSize();
Romain Guy35b38ce2009-10-07 13:38:55 -070014244 scrollBarDefaultDelayBeforeFade = ViewConfiguration.getScrollDefaultDelay();
14245 scrollBarFadeDuration = ViewConfiguration.getScrollBarFadeDuration();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014246
14247 paint = new Paint();
14248 matrix = new Matrix();
14249 // use use a height of 1, and then wack the matrix each time we
14250 // actually use it.
14251 shader = new LinearGradient(0, 0, 0, 1, 0xFF000000, 0, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014252
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014253 paint.setShader(shader);
14254 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
Mike Cleronf116bf82009-09-27 19:14:12 -070014255 this.host = host;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014256 }
Romain Guy8506ab42009-06-11 17:35:47 -070014257
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014258 public void setFadeColor(int color) {
14259 if (color != 0 && color != mLastColor) {
14260 mLastColor = color;
14261 color |= 0xFF000000;
Romain Guy8506ab42009-06-11 17:35:47 -070014262
Romain Guye55e1a72009-08-27 10:42:26 -070014263 shader = new LinearGradient(0, 0, 0, 1, color | 0xFF000000,
14264 color & 0x00FFFFFF, Shader.TileMode.CLAMP);
Romain Guy8506ab42009-06-11 17:35:47 -070014265
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014266 paint.setShader(shader);
14267 // Restore the default transfer mode (src_over)
14268 paint.setXfermode(null);
14269 }
14270 }
Joe Malin32736f02011-01-19 16:14:20 -080014271
Mike Cleronf116bf82009-09-27 19:14:12 -070014272 public void run() {
Mike Cleron3ecd58c2009-09-28 11:39:02 -070014273 long now = AnimationUtils.currentAnimationTimeMillis();
Mike Cleronf116bf82009-09-27 19:14:12 -070014274 if (now >= fadeStartTime) {
14275
14276 // the animation fades the scrollbars out by changing
14277 // the opacity (alpha) from fully opaque to fully
14278 // transparent
14279 int nextFrame = (int) now;
14280 int framesCount = 0;
14281
14282 Interpolator interpolator = scrollBarInterpolator;
14283
14284 // Start opaque
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014285 interpolator.setKeyFrame(framesCount++, nextFrame, OPAQUE);
Mike Cleronf116bf82009-09-27 19:14:12 -070014286
14287 // End transparent
14288 nextFrame += scrollBarFadeDuration;
Gilles Debunne3dbf55c2010-12-16 10:31:51 -080014289 interpolator.setKeyFrame(framesCount, nextFrame, TRANSPARENT);
Mike Cleronf116bf82009-09-27 19:14:12 -070014290
14291 state = FADING;
14292
14293 // Kick off the fade animation
Romain Guy0fd89bf2011-01-26 15:41:30 -080014294 host.invalidate(true);
Mike Cleronf116bf82009-09-27 19:14:12 -070014295 }
14296 }
Svetoslav Ganova0156172011-06-26 17:55:44 -070014297 }
Mike Cleronf116bf82009-09-27 19:14:12 -070014298
Svetoslav Ganova0156172011-06-26 17:55:44 -070014299 /**
14300 * Resuable callback for sending
14301 * {@link AccessibilityEvent#TYPE_VIEW_SCROLLED} accessibility event.
14302 */
14303 private class SendViewScrolledAccessibilityEvent implements Runnable {
14304 public volatile boolean mIsPending;
14305
14306 public void run() {
14307 sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SCROLLED);
14308 mIsPending = false;
14309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080014310 }
14311}